blob: 92dee8fd31c2f80a5099d3fd09e50e7a45210a73 [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-Alves7b9cc432024-04-04 10:57:17 +010011#include "hf/arch/memcpy_trapped.h"
Federico Recanati4fd065d2021-12-13 20:06:23 +010012#include "hf/arch/mm.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020013#include "hf/arch/other_world.h"
Olivier Deprez55a189e2021-06-09 15:45:27 +020014#include "hf/arch/plat/ffa.h"
Andrew Walbran290b0c92020-02-03 16:37:14 +000015
J-Alves5952d942022-12-22 16:03:00 +000016#include "hf/addr.h"
Jose Marinho75509b42019-04-09 09:34:59 +010017#include "hf/api.h"
Daniel Boulbya2f8c662021-11-26 17:52:53 +000018#include "hf/assert.h"
Jose Marinho09b1db82019-08-08 09:16:59 +010019#include "hf/check.h"
Jose Marinho75509b42019-04-09 09:34:59 +010020#include "hf/dlog.h"
J-Alves3456e032023-07-20 12:20:05 +010021#include "hf/ffa.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010022#include "hf/ffa_internal.h"
J-Alves66652252022-07-06 09:49:51 +010023#include "hf/ffa_memory_internal.h"
J-Alves3456e032023-07-20 12:20:05 +010024#include "hf/ffa_partition_manifest.h"
J-Alves5952d942022-12-22 16:03:00 +000025#include "hf/mm.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000026#include "hf/mpool.h"
J-Alvescf6253e2024-01-03 13:48:48 +000027#include "hf/panic.h"
28#include "hf/plat/memory_protect.h"
Jose Marinho75509b42019-04-09 09:34:59 +010029#include "hf/std.h"
Andrew Scull3c257452019-11-26 13:32:50 +000030#include "hf/vm.h"
Daniel Boulby44e9b3b2024-01-17 12:21:44 +000031#include "hf/vm_ids.h"
Jose Marinho75509b42019-04-09 09:34:59 +010032
J-Alves2d8457f2022-10-05 11:06:41 +010033#include "vmapi/hf/ffa_v1_0.h"
34
J-Alves5da37d92022-10-24 16:33:48 +010035#define RECEIVERS_COUNT_IN_RETRIEVE_RESP 1
36
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000037/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010038 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000039 * by this lock.
40 */
41static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010042static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000043
44/**
J-Alvesed508c82023-05-04 16:09:48 +010045 * Return the offset to the first constituent within the
46 * `ffa_composite_memory_region` for the given receiver from an
47 * `ffa_memory_region`. The caller must check that the receiver_index is within
48 * bounds, and that it has a composite memory region offset.
49 */
50static uint32_t ffa_composite_constituent_offset(
51 struct ffa_memory_region *memory_region, uint32_t receiver_index)
52{
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +000053 struct ffa_memory_access *receiver;
54 uint32_t composite_offset;
J-Alvesed508c82023-05-04 16:09:48 +010055
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +000056 CHECK(receiver_index < memory_region->receiver_count);
57
58 receiver =
59 ffa_memory_region_get_receiver(memory_region, receiver_index);
60 CHECK(receiver != NULL);
61
62 composite_offset = receiver->composite_memory_region_offset;
63
64 CHECK(composite_offset != 0);
65
66 return composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alvesed508c82023-05-04 16:09:48 +010067}
68
69/**
J-Alves917d2f22020-10-30 18:39:30 +000070 * Extracts the index from a memory handle allocated by Hafnium's current world.
71 */
72uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle)
73{
74 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
75}
76
77/**
Karl Meakin52cdfe72023-06-30 14:49:10 +010078 * Initialises the next available `struct ffa_memory_share_state`. If `handle`
79 * is `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle,
80 * otherwise uses the provided handle which is assumed to be globally unique.
Andrew Walbranca808b12020-05-15 17:22:28 +010081 *
Karl Meakin52cdfe72023-06-30 14:49:10 +010082 * Returns a pointer to the allocated `ffa_memory_share_state` on success or
83 * `NULL` if none are available.
Andrew Walbranca808b12020-05-15 17:22:28 +010084 */
Karl Meakin52cdfe72023-06-30 14:49:10 +010085struct ffa_memory_share_state *allocate_share_state(
86 struct share_states_locked share_states, uint32_t share_func,
87 struct ffa_memory_region *memory_region, uint32_t fragment_length,
88 ffa_memory_handle_t handle)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000089{
Daniel Boulbya2f8c662021-11-26 17:52:53 +000090 assert(share_states.share_states != NULL);
91 assert(memory_region != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000092
Karl Meakin52cdfe72023-06-30 14:49:10 +010093 for (uint64_t i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +010094 if (share_states.share_states[i].share_func == 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010095 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +010096 &share_states.share_states[i];
97 struct ffa_composite_memory_region *composite =
98 ffa_memory_region_get_composite(memory_region,
99 0);
100
101 if (handle == FFA_MEMORY_HANDLE_INVALID) {
J-Alvesee68c542020-10-29 17:48:20 +0000102 memory_region->handle =
Olivier Deprez55a189e2021-06-09 15:45:27 +0200103 plat_ffa_memory_handle_make(i);
Andrew Walbranca808b12020-05-15 17:22:28 +0100104 } else {
J-Alvesee68c542020-10-29 17:48:20 +0000105 memory_region->handle = handle;
Andrew Walbranca808b12020-05-15 17:22:28 +0100106 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000107 allocated_state->share_func = share_func;
108 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +0100109 allocated_state->fragment_count = 1;
110 allocated_state->fragments[0] = composite->constituents;
111 allocated_state->fragment_constituent_counts[0] =
112 (fragment_length -
113 ffa_composite_constituent_offset(memory_region,
114 0)) /
115 sizeof(struct ffa_memory_region_constituent);
116 allocated_state->sending_complete = false;
Karl Meakin52cdfe72023-06-30 14:49:10 +0100117 for (uint32_t j = 0; j < MAX_MEM_SHARE_RECIPIENTS;
118 ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100119 allocated_state->retrieved_fragment_count[j] =
120 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000121 }
Karl Meakin52cdfe72023-06-30 14:49:10 +0100122 return allocated_state;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000123 }
124 }
125
Karl Meakin52cdfe72023-06-30 14:49:10 +0100126 return NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000127}
128
129/** Locks the share states lock. */
130struct share_states_locked share_states_lock(void)
131{
132 sl_lock(&share_states_lock_instance);
133
134 return (struct share_states_locked){.share_states = share_states};
135}
136
137/** Unlocks the share states lock. */
J-Alves66652252022-07-06 09:49:51 +0100138void share_states_unlock(struct share_states_locked *share_states)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000139{
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000140 assert(share_states->share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000141 share_states->share_states = NULL;
142 sl_unlock(&share_states_lock_instance);
143}
144
145/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100146 * If the given handle is a valid handle for an allocated share state then
Karl Meakin4a2854a2023-06-30 16:26:52 +0100147 * returns a pointer to the share state. Otherwise returns NULL.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000148 */
Karl Meakin4a2854a2023-06-30 16:26:52 +0100149struct ffa_memory_share_state *get_share_state(
150 struct share_states_locked share_states, ffa_memory_handle_t handle)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000151{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100152 struct ffa_memory_share_state *share_state;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000153
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000154 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100155
156 /*
157 * First look for a share_state allocated by us, in which case the
158 * handle is based on the index.
159 */
Olivier Deprez55a189e2021-06-09 15:45:27 +0200160 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100161 uint64_t index = ffa_memory_handle_get_index(handle);
162
Andrew Walbranca808b12020-05-15 17:22:28 +0100163 if (index < MAX_MEM_SHARES) {
164 share_state = &share_states.share_states[index];
165 if (share_state->share_func != 0) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100166 return share_state;
Andrew Walbranca808b12020-05-15 17:22:28 +0100167 }
168 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000169 }
170
Andrew Walbranca808b12020-05-15 17:22:28 +0100171 /* Fall back to a linear scan. */
Karl Meakin4a2854a2023-06-30 16:26:52 +0100172 for (uint64_t index = 0; index < MAX_MEM_SHARES; ++index) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100173 share_state = &share_states.share_states[index];
J-Alvesee68c542020-10-29 17:48:20 +0000174 if (share_state->memory_region != NULL &&
175 share_state->memory_region->handle == handle &&
Andrew Walbranca808b12020-05-15 17:22:28 +0100176 share_state->share_func != 0) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100177 return share_state;
Andrew Walbranca808b12020-05-15 17:22:28 +0100178 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000179 }
180
Karl Meakin4a2854a2023-06-30 16:26:52 +0100181 return NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000182}
183
184/** Marks a share state as unallocated. */
J-Alvesfdd29272022-07-19 13:16:31 +0100185void share_state_free(struct share_states_locked share_states,
186 struct ffa_memory_share_state *share_state,
187 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000188{
Andrew Walbranca808b12020-05-15 17:22:28 +0100189 uint32_t i;
190
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000191 assert(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000192 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100193 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000194 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100195 /*
196 * First fragment is part of the same page as the `memory_region`, so it
197 * doesn't need to be freed separately.
198 */
199 share_state->fragments[0] = NULL;
200 share_state->fragment_constituent_counts[0] = 0;
201 for (i = 1; i < share_state->fragment_count; ++i) {
202 mpool_free(page_pool, share_state->fragments[i]);
203 share_state->fragments[i] = NULL;
204 share_state->fragment_constituent_counts[i] = 0;
205 }
206 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000207 share_state->memory_region = NULL;
J-Alvesa9cd7e32022-07-01 13:49:33 +0100208 share_state->hypervisor_fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000209}
210
Andrew Walbranca808b12020-05-15 17:22:28 +0100211/** Checks whether the given share state has been fully sent. */
J-Alvesfdd29272022-07-19 13:16:31 +0100212bool share_state_sending_complete(struct share_states_locked share_states,
213 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000214{
Andrew Walbranca808b12020-05-15 17:22:28 +0100215 struct ffa_composite_memory_region *composite;
216 uint32_t expected_constituent_count;
217 uint32_t fragment_constituent_count_total = 0;
218 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000219
Andrew Walbranca808b12020-05-15 17:22:28 +0100220 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000221 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100222
223 /*
224 * Share state must already be valid, or it's not possible to get hold
225 * of it.
226 */
227 CHECK(share_state->memory_region != NULL &&
228 share_state->share_func != 0);
229
230 composite =
231 ffa_memory_region_get_composite(share_state->memory_region, 0);
232 expected_constituent_count = composite->constituent_count;
233 for (i = 0; i < share_state->fragment_count; ++i) {
234 fragment_constituent_count_total +=
235 share_state->fragment_constituent_counts[i];
236 }
237 dlog_verbose(
238 "Checking completion: constituent count %d/%d from %d "
239 "fragments.\n",
240 fragment_constituent_count_total, expected_constituent_count,
241 share_state->fragment_count);
242
243 return fragment_constituent_count_total == expected_constituent_count;
244}
245
246/**
247 * Calculates the offset of the next fragment expected for the given share
248 * state.
249 */
J-Alvesfdd29272022-07-19 13:16:31 +0100250uint32_t share_state_next_fragment_offset(
Andrew Walbranca808b12020-05-15 17:22:28 +0100251 struct share_states_locked share_states,
252 struct ffa_memory_share_state *share_state)
253{
254 uint32_t next_fragment_offset;
255 uint32_t i;
256
257 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000258 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100259
260 next_fragment_offset =
261 ffa_composite_constituent_offset(share_state->memory_region, 0);
262 for (i = 0; i < share_state->fragment_count; ++i) {
263 next_fragment_offset +=
264 share_state->fragment_constituent_counts[i] *
265 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000266 }
267
Andrew Walbranca808b12020-05-15 17:22:28 +0100268 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000269}
270
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100271static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000272{
273 uint32_t i;
274
275 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
276 return;
277 }
278
Karl Meakine8937d92024-03-19 16:04:25 +0000279 dlog("from VM %#x, attributes (shareability = %s, cacheability = %s, "
280 "type = %s, security = %s), flags %#x, handle %#lx "
281 "tag %lu, memory access descriptor size %u, to %u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100282 "recipients [",
Karl Meakine8937d92024-03-19 16:04:25 +0000283 memory_region->sender,
284 ffa_memory_shareability_name(
285 memory_region->attributes.shareability),
286 ffa_memory_cacheability_name(
287 memory_region->attributes.cacheability),
288 ffa_memory_type_name(memory_region->attributes.type),
289 ffa_memory_security_name(memory_region->attributes.security),
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000290 memory_region->flags, memory_region->handle, memory_region->tag,
291 memory_region->memory_access_desc_size,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100292 memory_region->receiver_count);
293 for (i = 0; i < memory_region->receiver_count; ++i) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000294 struct ffa_memory_access *receiver =
295 ffa_memory_region_get_receiver(memory_region, i);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000296 if (i != 0) {
297 dlog(", ");
298 }
Karl Meakine8937d92024-03-19 16:04:25 +0000299 dlog("Receiver %#x: permissions (%s, %s) (offset %u)",
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000300 receiver->receiver_permissions.receiver,
Karl Meakine8937d92024-03-19 16:04:25 +0000301 ffa_data_access_name(receiver->receiver_permissions
302 .permissions.data_access),
303 ffa_instruction_access_name(
304 receiver->receiver_permissions.permissions
305 .instruction_access),
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000306 receiver->composite_memory_region_offset);
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000307 /* The impdef field is only present from v1.2 and later */
308 if (ffa_version_from_memory_access_desc_size(
309 memory_region->memory_access_desc_size) >=
Karl Meakin0e617d92024-04-05 12:55:22 +0100310 FFA_VERSION_1_2) {
Karl Meakine8937d92024-03-19 16:04:25 +0000311 dlog(", impdef: %#lx %#lx", receiver->impdef.val[0],
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000312 receiver->impdef.val[1]);
313 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000314 }
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000315 dlog("] at offset %u", memory_region->receivers_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000316}
317
J-Alves66652252022-07-06 09:49:51 +0100318void dump_share_states(void)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000319{
320 uint32_t i;
321
322 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
323 return;
324 }
325
326 dlog("Current share states:\n");
327 sl_lock(&share_states_lock_instance);
328 for (i = 0; i < MAX_MEM_SHARES; ++i) {
329 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000330 switch (share_states[i].share_func) {
J-Alves95fbb312024-03-20 15:19:16 +0000331 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100332 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000333 dlog("SHARE");
334 break;
J-Alves95fbb312024-03-20 15:19:16 +0000335 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100336 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000337 dlog("LEND");
338 break;
J-Alves95fbb312024-03-20 15:19:16 +0000339 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100340 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000341 dlog("DONATE");
342 break;
343 default:
344 dlog("invalid share_func %#x",
345 share_states[i].share_func);
346 }
Karl Meakine8937d92024-03-19 16:04:25 +0000347 dlog(" %#lx (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000348 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100349 if (share_states[i].sending_complete) {
350 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000351 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100352 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000353 }
J-Alves2a0d2882020-10-29 14:49:50 +0000354 dlog(" with %d fragments, %d retrieved, "
355 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100356 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000357 share_states[i].retrieved_fragment_count[0],
358 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000359 }
360 }
361 sl_unlock(&share_states_lock_instance);
362}
363
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100364static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100365 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000366{
367 uint32_t mode = 0;
368
Karl Meakin84710f32023-10-12 15:14:49 +0100369 switch (permissions.data_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100370 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000371 mode = MM_MODE_R;
372 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100373 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000374 mode = MM_MODE_R | MM_MODE_W;
375 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100376 case FFA_DATA_ACCESS_NOT_SPECIFIED:
377 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
378 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100379 case FFA_DATA_ACCESS_RESERVED:
380 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100381 }
382
Karl Meakin84710f32023-10-12 15:14:49 +0100383 switch (permissions.instruction_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100384 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000385 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100386 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100387 mode |= MM_MODE_X;
388 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100389 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
390 mode |= (default_mode & MM_MODE_X);
391 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100392 case FFA_INSTRUCTION_ACCESS_RESERVED:
393 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000394 }
395
Olivier Deprez878bd5b2021-04-15 19:05:10 +0200396 /* Set the security state bit if necessary. */
397 if ((default_mode & plat_ffa_other_world_mode()) != 0) {
398 mode |= plat_ffa_other_world_mode();
399 }
400
Daniel Boulby6e261362024-06-13 16:53:00 +0100401 mode |= default_mode & MM_MODE_D;
402
Andrew Walbran475c1452020-02-07 13:22:22 +0000403 return mode;
404}
405
Jose Marinho75509b42019-04-09 09:34:59 +0100406/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000407 * Get the current mode in the stage-2 page table of the given vm of all the
408 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100409 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100410 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100411static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000412 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100413 struct ffa_memory_region_constituent **fragments,
414 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100415{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100416 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100417 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100418
Andrew Walbranca808b12020-05-15 17:22:28 +0100419 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100420 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000421 * Fail if there are no constituents. Otherwise we would get an
422 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100423 */
Karl Meakin5df422c2023-07-11 17:31:38 +0100424 dlog_verbose("%s: no constituents\n", __func__);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100425 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100426 }
427
Andrew Walbranca808b12020-05-15 17:22:28 +0100428 for (i = 0; i < fragment_count; ++i) {
429 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
430 ipaddr_t begin = ipa_init(fragments[i][j].address);
431 size_t size = fragments[i][j].page_count * PAGE_SIZE;
432 ipaddr_t end = ipa_add(begin, size);
433 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100434
Andrew Walbranca808b12020-05-15 17:22:28 +0100435 /* Fail if addresses are not page-aligned. */
436 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
437 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100438 dlog_verbose("%s: addresses not page-aligned\n",
439 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +0100440 return ffa_error(FFA_INVALID_PARAMETERS);
441 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100442
Andrew Walbranca808b12020-05-15 17:22:28 +0100443 /*
444 * Ensure that this constituent memory range is all
445 * mapped with the same mode.
446 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800447 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100448 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +0000449 "%s: constituent memory range "
450 "%#lx..%#lx "
Karl Meakin5df422c2023-07-11 17:31:38 +0100451 "not mapped with the same mode\n",
Karl Meakine8937d92024-03-19 16:04:25 +0000452 __func__, begin.ipa, end.ipa);
Andrew Walbranca808b12020-05-15 17:22:28 +0100453 return ffa_error(FFA_DENIED);
454 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100455
Andrew Walbranca808b12020-05-15 17:22:28 +0100456 /*
457 * Ensure that all constituents are mapped with the same
458 * mode.
459 */
460 if (i == 0) {
461 *orig_mode = current_mode;
462 } else if (current_mode != *orig_mode) {
463 dlog_verbose(
Karl Meakin5df422c2023-07-11 17:31:38 +0100464 "%s: expected mode %#x but was %#x for "
Karl Meakine8937d92024-03-19 16:04:25 +0000465 "%d pages at %#lx.\n",
Karl Meakin5df422c2023-07-11 17:31:38 +0100466 __func__, *orig_mode, current_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100467 fragments[i][j].page_count,
468 ipa_addr(begin));
469 return ffa_error(FFA_DENIED);
470 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100471 }
Jose Marinho75509b42019-04-09 09:34:59 +0100472 }
473
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100474 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000475}
476
Karl Meakin0e617d92024-04-05 12:55:22 +0100477enum ffa_version ffa_version_from_memory_access_desc_size(
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100478 uint32_t memory_access_desc_size)
479{
480 switch (memory_access_desc_size) {
481 /*
482 * v1.0 and v1.1 memory access descriptors are the same size however
483 * v1.1 is the first version to include the memory access descriptor
484 * size field so return v1.1.
485 */
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000486 case sizeof(struct ffa_memory_access_v1_0):
Karl Meakin0e617d92024-04-05 12:55:22 +0100487 return FFA_VERSION_1_1;
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000488 case sizeof(struct ffa_memory_access):
Karl Meakin0e617d92024-04-05 12:55:22 +0100489 return FFA_VERSION_1_2;
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100490 }
491 return 0;
492}
493
494/**
495 * Check if the receivers size and offset given is valid for the senders
496 * FF-A version.
497 */
498static bool receiver_size_and_offset_valid_for_version(
499 uint32_t receivers_size, uint32_t receivers_offset,
Karl Meakin0e617d92024-04-05 12:55:22 +0100500 enum ffa_version ffa_version)
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100501{
502 /*
503 * Check that the version that the memory access descriptor size belongs
504 * to is compatible with the FF-A version we believe the sender to be.
505 */
Karl Meakin0e617d92024-04-05 12:55:22 +0100506 enum ffa_version expected_ffa_version =
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100507 ffa_version_from_memory_access_desc_size(receivers_size);
Karl Meakin0e617d92024-04-05 12:55:22 +0100508 if (!ffa_versions_are_compatible(expected_ffa_version, ffa_version)) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100509 return false;
510 }
511
512 /*
513 * Check the receivers_offset matches the version we found from
514 * memory access descriptor size.
515 */
516 switch (expected_ffa_version) {
Karl Meakin0e617d92024-04-05 12:55:22 +0100517 case FFA_VERSION_1_1:
518 case FFA_VERSION_1_2:
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100519 return receivers_offset == sizeof(struct ffa_memory_region);
520 default:
521 return false;
522 }
523}
524
525/**
526 * Check the values set for fields in the memory region are valid and safe.
527 * Offset values are within safe bounds, receiver count will not cause overflows
528 * and reserved fields are 0.
529 */
530bool ffa_memory_region_sanity_check(struct ffa_memory_region *memory_region,
Karl Meakin0e617d92024-04-05 12:55:22 +0100531 enum ffa_version ffa_version,
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100532 uint32_t fragment_length,
533 bool send_transaction)
534{
535 uint32_t receiver_count;
536 struct ffa_memory_access *receiver;
537 uint32_t composite_offset_0;
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000538 struct ffa_memory_region_v1_0 *memory_region_v1_0 =
539 (struct ffa_memory_region_v1_0 *)memory_region;
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100540
Karl Meakin0e617d92024-04-05 12:55:22 +0100541 if (ffa_version == FFA_VERSION_1_0) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100542 /* Check the reserved fields are 0. */
543 if (memory_region_v1_0->reserved_0 != 0 ||
544 memory_region_v1_0->reserved_1 != 0) {
545 dlog_verbose("Reserved fields must be 0.\n");
546 return false;
547 }
548
549 receiver_count = memory_region_v1_0->receiver_count;
550 } else {
551 uint32_t receivers_size =
552 memory_region->memory_access_desc_size;
553 uint32_t receivers_offset = memory_region->receivers_offset;
554
555 /* Check the reserved field is 0. */
556 if (memory_region->reserved[0] != 0 ||
557 memory_region->reserved[1] != 0 ||
558 memory_region->reserved[2] != 0) {
559 dlog_verbose("Reserved fields must be 0.\n");
560 return false;
561 }
562
563 /*
564 * Check memory_access_desc_size matches the size of the struct
565 * for the senders FF-A version.
566 */
567 if (!receiver_size_and_offset_valid_for_version(
568 receivers_size, receivers_offset, ffa_version)) {
569 dlog_verbose(
570 "Invalid memory access descriptor size %d, "
571 " or receiver offset %d, "
572 "for FF-A version %#x\n",
573 receivers_size, receivers_offset, ffa_version);
574 return false;
575 }
576
577 receiver_count = memory_region->receiver_count;
578 }
579
580 /* Check receiver count is not too large. */
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000581 if (receiver_count > MAX_MEM_SHARE_RECIPIENTS || receiver_count < 1) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100582 dlog_verbose(
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000583 "Receiver count must be 0 < receiver_count < %u "
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100584 "specified %u\n",
585 MAX_MEM_SHARE_RECIPIENTS, receiver_count);
586 return false;
587 }
588
589 /* Check values in the memory access descriptors. */
590 /*
591 * The composite offset values must be the same for all recievers so
592 * check the first one is valid and then they are all the same.
593 */
Karl Meakin0e617d92024-04-05 12:55:22 +0100594 receiver = ffa_version == FFA_VERSION_1_0
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000595 ? (struct ffa_memory_access *)&memory_region_v1_0
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100596 ->receivers[0]
597 : ffa_memory_region_get_receiver(memory_region, 0);
598 assert(receiver != NULL);
599 composite_offset_0 = receiver->composite_memory_region_offset;
600
601 if (!send_transaction) {
602 if (composite_offset_0 != 0) {
603 dlog_verbose(
604 "Composite offset memory region descriptor "
605 "offset must be 0 for retrieve requests. "
606 "Currently %d",
607 composite_offset_0);
608 return false;
609 }
610 } else {
611 bool comp_offset_is_zero = composite_offset_0 == 0U;
612 bool comp_offset_lt_transaction_descriptor_size =
613 composite_offset_0 <
614 (sizeof(struct ffa_memory_region) +
615 (uint32_t)(memory_region->memory_access_desc_size *
616 memory_region->receiver_count));
617 bool comp_offset_with_comp_gt_fragment_length =
618 composite_offset_0 +
619 sizeof(struct ffa_composite_memory_region) >
620 fragment_length;
621 if (comp_offset_is_zero ||
622 comp_offset_lt_transaction_descriptor_size ||
623 comp_offset_with_comp_gt_fragment_length) {
624 dlog_verbose(
625 "Invalid composite memory region descriptor "
626 "offset for send transaction %u\n",
627 composite_offset_0);
628 return false;
629 }
630 }
631
Karl Meakin824b63d2024-06-03 19:04:53 +0100632 for (size_t i = 0; i < memory_region->receiver_count; i++) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100633 uint32_t composite_offset;
634
Karl Meakin0e617d92024-04-05 12:55:22 +0100635 if (ffa_version == FFA_VERSION_1_0) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100636 struct ffa_memory_access_v1_0 *receiver_v1_0 =
637 &memory_region_v1_0->receivers[i];
638 /* Check reserved fields are 0 */
639 if (receiver_v1_0->reserved_0 != 0) {
640 dlog_verbose(
641 "Reserved field in the memory access "
Karl Meakine8937d92024-03-19 16:04:25 +0000642 "descriptor must be zero. Currently "
643 "reciever %zu has a reserved field "
644 "with a value of %lu\n",
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100645 i, receiver_v1_0->reserved_0);
646 return false;
647 }
648 /*
649 * We can cast to the current version receiver as the
650 * remaining fields we are checking have the same
651 * offsets for all versions since memory access
652 * descriptors are forwards compatible.
653 */
654 receiver = (struct ffa_memory_access *)receiver_v1_0;
655 } else {
656 receiver = ffa_memory_region_get_receiver(memory_region,
657 i);
658 assert(receiver != NULL);
659
660 if (receiver->reserved_0 != 0) {
661 dlog_verbose(
662 "Reserved field in the memory access "
Karl Meakine8937d92024-03-19 16:04:25 +0000663 "descriptor must be zero. Currently "
664 "reciever %zu has a reserved field "
665 "with a value of %lu\n",
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100666 i, receiver->reserved_0);
667 return false;
668 }
669 }
670
671 /* Check composite offset values are equal for all receivers. */
672 composite_offset = receiver->composite_memory_region_offset;
673 if (composite_offset != composite_offset_0) {
674 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +0000675 "Composite offset %x differs from %x in "
676 "index\n",
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100677 composite_offset, composite_offset_0);
678 return false;
679 }
680 }
681 return true;
682}
683
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000684/**
J-Alves460d36c2023-10-12 17:02:15 +0100685 * If the receivers for the memory management operation are all from the
686 * secure world and this isn't a FFA_MEM_SHARE, then request memory security
687 * state update by returning MAP_ACTION_CHECK_PROTECT.
688 */
689static enum ffa_map_action ffa_mem_send_get_map_action(
690 bool all_receivers_from_current_world, ffa_id_t sender_id,
691 uint32_t mem_func_id)
692{
J-Alves95fbb312024-03-20 15:19:16 +0000693 const bool is_memory_share_abi = mem_func_id == FFA_MEM_SHARE_32 ||
694 mem_func_id == FFA_MEM_SHARE_64;
695 const bool protect_memory =
696 (!is_memory_share_abi && all_receivers_from_current_world &&
697 ffa_is_vm_id(sender_id));
J-Alves460d36c2023-10-12 17:02:15 +0100698
699 return protect_memory ? MAP_ACTION_CHECK_PROTECT : MAP_ACTION_CHECK;
700}
701
702/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000703 * Verify that all pages have the same mode, that the starting mode
704 * constitutes a valid state and obtain the next mode to apply
J-Alves460d36c2023-10-12 17:02:15 +0100705 * to the sending VM. It outputs the mapping action that needs to be
706 * invoked for the given memory range. On memory lend/donate there
707 * could be a need to protect the memory from the normal world.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000708 *
709 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100710 * 1) FFA_DENIED if a state transition was not found;
711 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100712 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100713 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100714 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100715 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
716 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000717 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100718static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100719 struct vm_locked from, uint32_t share_func,
Daniel Boulbya76fd912024-02-22 14:22:15 +0000720 struct ffa_memory_region *memory_region, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100721 struct ffa_memory_region_constituent **fragments,
722 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby4b846eb2024-05-23 17:32:23 +0100723 uint32_t *from_mode, enum ffa_map_action *map_action, bool zero)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000724{
725 const uint32_t state_mask =
726 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100727 struct ffa_value ret;
J-Alves460d36c2023-10-12 17:02:15 +0100728 bool all_receivers_from_current_world = true;
Daniel Boulbya76fd912024-02-22 14:22:15 +0000729 uint32_t receivers_count = memory_region->receiver_count;
J-Alves95fbb312024-03-20 15:19:16 +0000730 const bool is_memory_lend = (share_func == FFA_MEM_LEND_32) ||
731 (share_func == FFA_MEM_LEND_64);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000732
Andrew Walbranca808b12020-05-15 17:22:28 +0100733 ret = constituents_get_mode(from, orig_from_mode, fragments,
734 fragment_constituent_counts,
735 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100736 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100737 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100738 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100739 }
740
Daniel Boulby6e261362024-06-13 16:53:00 +0100741 /*
742 * Check requested memory type is valid with the memory type of the
743 * owner. E.g. they follow the memory type precedence where Normal
744 * memory is more permissive than device and therefore device memory
745 * can only be shared as device memory.
746 */
747 if (memory_region->attributes.type == FFA_MEMORY_NORMAL_MEM &&
748 (*orig_from_mode & MM_MODE_D) != 0U) {
749 dlog_verbose(
750 "Send device memory as Normal memory is not allowed\n");
751 return ffa_error(FFA_DENIED);
752 }
753
Daniel Boulby63af1fa2024-03-18 14:17:31 +0000754 /* Device memory regions can only be lent a single borrower. */
Daniel Boulby9764ff62024-01-30 17:47:39 +0000755 if ((*orig_from_mode & MM_MODE_D) != 0U &&
J-Alves95fbb312024-03-20 15:19:16 +0000756 !(is_memory_lend && receivers_count == 1)) {
Daniel Boulby9764ff62024-01-30 17:47:39 +0000757 dlog_verbose(
Daniel Boulby63af1fa2024-03-18 14:17:31 +0000758 "Device memory can only be lent to a single borrower "
759 "(mode is %#x).\n",
Daniel Boulby9764ff62024-01-30 17:47:39 +0000760 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100761 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000762 }
763
764 /*
765 * Ensure the sender is the owner and has exclusive access to the
766 * memory.
767 */
768 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100769 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100770 }
771
Daniel Boulby4b846eb2024-05-23 17:32:23 +0100772 /*
773 * Memory cannot be zeroed during the lend/donate operation if the
774 * sender only has RO access.
775 */
776 if ((*orig_from_mode & MM_MODE_W) == 0 && zero == true) {
777 dlog_verbose(
778 "Cannot zero memory when the sender doesn't have "
779 "write access\n");
780 return ffa_error(FFA_DENIED);
781 }
782
Daniel Boulbya76fd912024-02-22 14:22:15 +0000783 assert(receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100784
J-Alves363f5722022-04-25 17:37:37 +0100785 for (uint32_t i = 0U; i < receivers_count; i++) {
Daniel Boulbya76fd912024-02-22 14:22:15 +0000786 struct ffa_memory_access *receiver =
787 ffa_memory_region_get_receiver(memory_region, i);
788 assert(receiver != NULL);
J-Alves363f5722022-04-25 17:37:37 +0100789 ffa_memory_access_permissions_t permissions =
Daniel Boulbya76fd912024-02-22 14:22:15 +0000790 receiver->receiver_permissions.permissions;
J-Alves363f5722022-04-25 17:37:37 +0100791 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
792 permissions, *orig_from_mode);
793
J-Alves788b4492023-04-18 14:01:23 +0100794 /*
795 * The assumption is that at this point, the operation from
796 * SP to a receiver VM, should have returned an FFA_ERROR
797 * already.
798 */
799 if (!ffa_is_vm_id(from.vm->id)) {
800 assert(!ffa_is_vm_id(
Daniel Boulbya76fd912024-02-22 14:22:15 +0000801 receiver->receiver_permissions.receiver));
J-Alves788b4492023-04-18 14:01:23 +0100802 }
803
J-Alves460d36c2023-10-12 17:02:15 +0100804 /* Track if all senders are from current world. */
805 all_receivers_from_current_world =
806 all_receivers_from_current_world &&
807 vm_id_is_current_world(
Daniel Boulbya76fd912024-02-22 14:22:15 +0000808 receiver->receiver_permissions.receiver);
J-Alves460d36c2023-10-12 17:02:15 +0100809
J-Alves363f5722022-04-25 17:37:37 +0100810 if ((*orig_from_mode & required_from_mode) !=
811 required_from_mode) {
812 dlog_verbose(
813 "Sender tried to send memory with permissions "
J-Alves788b4492023-04-18 14:01:23 +0100814 "which required mode %#x but only had %#x "
815 "itself.\n",
J-Alves363f5722022-04-25 17:37:37 +0100816 required_from_mode, *orig_from_mode);
817 return ffa_error(FFA_DENIED);
818 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000819 }
820
J-Alves460d36c2023-10-12 17:02:15 +0100821 *map_action = ffa_mem_send_get_map_action(
822 all_receivers_from_current_world, from.vm->id, share_func);
823
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000824 /* Find the appropriate new mode. */
825 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000826 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +0000827 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100828 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000829 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100830 break;
J-Alves95fbb312024-03-20 15:19:16 +0000831 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100832 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000833 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100834 break;
J-Alves95fbb312024-03-20 15:19:16 +0000835 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100836 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000837 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100838 break;
839
Jose Marinho75509b42019-04-09 09:34:59 +0100840 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100841 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100842 }
843
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100844 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000845}
846
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100847static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000848 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100849 struct ffa_memory_region_constituent **fragments,
850 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves69cdfd92024-04-26 11:40:59 +0100851 uint32_t *from_mode, enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000852{
853 const uint32_t state_mask =
854 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
855 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100856 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000857
J-Alves69cdfd92024-04-26 11:40:59 +0100858 assert(map_action != NULL);
859 if (vm_id_is_current_world(from.vm->id)) {
860 *map_action = MAP_ACTION_COMMIT;
861 } else {
862 /*
863 * No need to check the attributes of caller.
864 * The assumption is that the retrieve request of the receiver
865 * also used the MAP_ACTION_NONE, and no update was done to the
866 * page tables. When the receiver is not at the secure virtual
867 * instance SPMC doesn't manage its S2 translation (i.e. when
868 * the receiver is a VM).
869 */
870 *map_action = MAP_ACTION_NONE;
871
872 return (struct ffa_value){.func = FFA_SUCCESS_32};
873 }
874
Andrew Walbranca808b12020-05-15 17:22:28 +0100875 ret = constituents_get_mode(from, orig_from_mode, fragments,
876 fragment_constituent_counts,
877 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100878 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100879 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000880 }
881
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000882 /*
883 * Ensure the relinquishing VM is not the owner but has access to the
884 * memory.
885 */
886 orig_from_state = *orig_from_mode & state_mask;
887 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
888 dlog_verbose(
889 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100890 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000891 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100892 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000893 }
894
895 /* Find the appropriate new mode. */
896 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
897
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100898 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000899}
900
901/**
902 * Verify that all pages have the same mode, that the starting mode
903 * constitutes a valid state and obtain the next mode to apply
904 * to the retrieving VM.
905 *
906 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100907 * 1) FFA_DENIED if a state transition was not found;
908 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100909 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100910 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100911 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100912 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
913 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000914 */
J-Alvesfc19b372022-07-06 12:17:35 +0100915struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000916 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100917 struct ffa_memory_region_constituent **fragments,
918 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby71d887b2024-06-28 16:38:06 +0100919 uint32_t sender_orig_mode, uint32_t *to_mode, bool memory_protected,
J-Alvesfd206052023-05-22 16:45:00 +0100920 enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000921{
922 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100923 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000924
Andrew Walbranca808b12020-05-15 17:22:28 +0100925 ret = constituents_get_mode(to, &orig_to_mode, fragments,
926 fragment_constituent_counts,
927 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100928 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100929 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100930 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000931 }
932
J-Alves460d36c2023-10-12 17:02:15 +0100933 /* Find the appropriate new mode. */
Daniel Boulby71d887b2024-06-28 16:38:06 +0100934 *to_mode = sender_orig_mode;
J-Alves460d36c2023-10-12 17:02:15 +0100935
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100936 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000937 /*
938 * If the original ffa memory send call has been processed
939 * successfully, it is expected the orig_to_mode would overlay
940 * with `state_mask`, as a result of the function
941 * `ffa_send_check_transition`.
J-Alvesfd206052023-05-22 16:45:00 +0100942 *
943 * If Hafnium is the SPMC:
944 * - Caller of the reclaim interface is an SP, the memory shall
945 * have been protected throughout the flow.
946 * - Caller of the reclaim is from the NWd, the memory may have
947 * been protected at the time of lending/donating the memory.
948 * In such case, set action to unprotect memory in the
949 * handling of reclaim operation.
950 * - If Hafnium is the hypervisor memory shall never have been
951 * protected in memory lend/share/donate.
952 *
953 * More details in the doc comment of the function
954 * `ffa_region_group_identity_map`.
J-Alves9256f162021-12-09 13:18:43 +0000955 */
J-Alves59ed0042022-07-28 18:26:41 +0100956 if (vm_id_is_current_world(to.vm->id)) {
957 assert((orig_to_mode &
958 (MM_MODE_INVALID | MM_MODE_UNOWNED |
959 MM_MODE_SHARED)) != 0U);
J-Alvesfd206052023-05-22 16:45:00 +0100960 assert(!memory_protected);
961 } else if (to.vm->id == HF_OTHER_WORLD_ID &&
962 map_action != NULL && memory_protected) {
963 *map_action = MAP_ACTION_COMMIT_UNPROTECT;
J-Alves59ed0042022-07-28 18:26:41 +0100964 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000965 } else {
J-Alves69cdfd92024-04-26 11:40:59 +0100966 if (!vm_id_is_current_world(to.vm->id)) {
967 assert(map_action != NULL);
968 *map_action = MAP_ACTION_NONE;
969 return (struct ffa_value){.func = FFA_SUCCESS_32};
970 }
971
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000972 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +0100973 * If the retriever is from virtual FF-A instance:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000974 * Ensure the retriever has the expected state. We don't care
975 * about the MM_MODE_SHARED bit; either with or without it set
976 * are both valid representations of the !O-NA state.
977 */
J-Alvesa9cd7e32022-07-01 13:49:33 +0100978 if (vm_id_is_current_world(to.vm->id) &&
Karl Meakin5e996992024-05-20 11:27:07 +0100979 !vm_is_primary(to.vm) &&
J-Alvesa9cd7e32022-07-01 13:49:33 +0100980 (orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
981 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100982 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000983 }
J-Alves460d36c2023-10-12 17:02:15 +0100984
985 /*
986 * If memory has been protected before, clear the NS bit to
987 * allow the secure access from the SP.
988 */
989 if (memory_protected) {
990 *to_mode &= ~plat_ffa_other_world_mode();
991 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000992 }
993
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000994 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +0000995 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100996 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000997 *to_mode |= 0;
998 break;
J-Alves95fbb312024-03-20 15:19:16 +0000999 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001000 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001001 *to_mode |= MM_MODE_UNOWNED;
1002 break;
J-Alves95fbb312024-03-20 15:19:16 +00001003 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001004 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001005 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
1006 break;
1007
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001008 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001009 *to_mode |= 0;
1010 break;
1011
1012 default:
Andrew Walbranca808b12020-05-15 17:22:28 +01001013 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001014 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001015 }
1016
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001017 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +01001018}
Jose Marinho09b1db82019-08-08 09:16:59 +01001019
J-Alvescf6253e2024-01-03 13:48:48 +00001020/*
1021 * Performs the operations related to the `action` MAP_ACTION_CHECK*.
1022 * Returns:
1023 * - FFA_SUCCESS_32: if all goes well.
1024 * - FFA_ERROR_32: with FFA_NO_MEMORY, if there is no memory to manage
1025 * the page table update. Or error code provided by the function
1026 * `arch_memory_protect`.
1027 */
1028static struct ffa_value ffa_region_group_check_actions(
1029 struct vm_locked vm_locked, paddr_t pa_begin, paddr_t pa_end,
1030 struct mpool *ppool, uint32_t mode, enum ffa_map_action action,
1031 bool *memory_protected)
1032{
1033 struct ffa_value ret;
1034 bool is_memory_protected;
1035
1036 if (!vm_identity_prepare(vm_locked, pa_begin, pa_end, mode, ppool)) {
1037 dlog_verbose(
1038 "%s: memory can't be mapped to %x due to lack of "
Karl Meakine8937d92024-03-19 16:04:25 +00001039 "memory. Base: %lx end: %lx\n",
J-Alvescf6253e2024-01-03 13:48:48 +00001040 __func__, vm_locked.vm->id, pa_addr(pa_begin),
1041 pa_addr(pa_end));
1042 return ffa_error(FFA_NO_MEMORY);
1043 }
1044
1045 switch (action) {
1046 case MAP_ACTION_CHECK:
1047 /* No protect requested. */
1048 is_memory_protected = false;
1049 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1050 break;
1051 case MAP_ACTION_CHECK_PROTECT: {
1052 paddr_t last_protected_pa = pa_init(0);
1053
1054 ret = arch_memory_protect(pa_begin, pa_end, &last_protected_pa);
1055
1056 is_memory_protected = (ret.func == FFA_SUCCESS_32);
1057
1058 /*
1059 * - If protect memory has failed with FFA_DENIED, means some
1060 * range of memory was in the wrong state. In such case, SPM
1061 * reverts the state of the pages that were successfully
1062 * updated.
1063 * - If protect memory has failed with FFA_NOT_SUPPORTED, it
1064 * means the platform doesn't support the protection mechanism.
1065 * That said, it still permits the page table update to go
1066 * through. The variable
1067 * `is_memory_protected` will be equal to false.
1068 * - If protect memory has failed with FFA_INVALID_PARAMETERS,
1069 * break from switch and return the error.
1070 */
1071 if (ret.func == FFA_ERROR_32) {
1072 assert(!is_memory_protected);
1073 if (ffa_error_code(ret) == FFA_DENIED &&
1074 pa_addr(last_protected_pa) != (uintptr_t)0) {
1075 CHECK(arch_memory_unprotect(
1076 pa_begin,
1077 pa_add(last_protected_pa, PAGE_SIZE)));
1078 } else if (ffa_error_code(ret) == FFA_NOT_SUPPORTED) {
1079 ret = (struct ffa_value){
1080 .func = FFA_SUCCESS_32,
1081 };
1082 }
1083 }
1084 } break;
1085 default:
1086 panic("%s: invalid action to process %x\n", __func__, action);
1087 }
1088
1089 if (memory_protected != NULL) {
1090 *memory_protected = is_memory_protected;
1091 }
1092
1093 return ret;
1094}
1095
1096static void ffa_region_group_commit_actions(struct vm_locked vm_locked,
1097 paddr_t pa_begin, paddr_t pa_end,
1098 struct mpool *ppool, uint32_t mode,
1099 enum ffa_map_action action)
1100{
1101 switch (action) {
1102 case MAP_ACTION_COMMIT_UNPROTECT:
1103 /*
1104 * Checking that it should succeed because SPM should be
1105 * unprotecting memory that it had protected before.
1106 */
1107 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1108 case MAP_ACTION_COMMIT:
1109 vm_identity_commit(vm_locked, pa_begin, pa_end, mode, ppool,
1110 NULL);
1111 break;
1112 default:
1113 panic("%s: invalid action to process %x\n", __func__, action);
1114 }
1115}
1116
Jose Marinho09b1db82019-08-08 09:16:59 +01001117/**
J-Alves063ad832023-10-03 18:05:40 +01001118 * Helper function to revert a failed "Protect" action from the SPMC:
1119 * - `fragment_count`: should specify the number of fragments to traverse from
1120 * `fragments`. This may not be the full amount of fragments that are part of
1121 * the share_state structure.
1122 * - `fragment_constituent_counts`: array holding the amount of constituents
1123 * per fragment.
1124 * - `end`: pointer to the constituent that failed the "protect" action. It
1125 * shall be part of the last fragment, and it shall make the loop below break.
1126 */
1127static void ffa_region_group_fragments_revert_protect(
1128 struct ffa_memory_region_constituent **fragments,
1129 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1130 const struct ffa_memory_region_constituent *end)
1131{
1132 for (uint32_t i = 0; i < fragment_count; ++i) {
1133 for (uint32_t j = 0; j < fragment_constituent_counts[i]; ++j) {
1134 struct ffa_memory_region_constituent *constituent =
1135 &fragments[i][j];
1136 size_t size = constituent->page_count * PAGE_SIZE;
1137 paddr_t pa_begin =
1138 pa_from_ipa(ipa_init(constituent->address));
1139 paddr_t pa_end = pa_add(pa_begin, size);
1140
Karl Meakine8937d92024-03-19 16:04:25 +00001141 dlog_verbose("%s: reverting fragment %lx size %zx\n",
J-Alves063ad832023-10-03 18:05:40 +01001142 __func__, pa_addr(pa_begin), size);
1143
1144 if (constituent == end) {
1145 /*
1146 * The last constituent is expected to be in the
1147 * last fragment.
1148 */
1149 assert(i == fragment_count - 1);
1150 break;
1151 }
1152
1153 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1154 }
1155 }
1156}
1157
1158/**
Jose Marinho09b1db82019-08-08 09:16:59 +01001159 * Updates a VM's page table such that the given set of physical address ranges
1160 * are mapped in the address space at the corresponding address ranges, in the
1161 * mode provided.
1162 *
J-Alves0a83dc22023-05-05 09:50:37 +01001163 * The enum ffa_map_action determines the action taken from a call to the
1164 * function below:
1165 * - If action is MAP_ACTION_CHECK, the page tables will be allocated from the
1166 * mpool but no mappings will actually be updated. This function must always
1167 * be called first with action set to MAP_ACTION_CHECK to check that it will
1168 * succeed before calling ffa_region_group_identity_map with whichever one of
1169 * the remaining actions, to avoid leaving the page table in a half-updated
1170 * state.
1171 * - The action MAP_ACTION_COMMIT allocates the page tables from the mpool, and
1172 * changes the memory mappings.
J-Alvescf6253e2024-01-03 13:48:48 +00001173 * - The action MAP_ACTION_CHECK_PROTECT extends the MAP_ACTION_CHECK with an
1174 * invocation to the monitor to update the security state of the memory,
1175 * to that of the SPMC.
1176 * - The action MAP_ACTION_COMMIT_UNPROTECT extends the MAP_ACTION_COMMIT
1177 * with a call into the monitor, to reset the security state of memory
1178 * that has priorly been mapped with the MAP_ACTION_CHECK_PROTECT action.
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001179 * vm_ptable_defrag should always be called after a series of page table
1180 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +01001181 *
J-Alvescf6253e2024-01-03 13:48:48 +00001182 * If all goes well, returns FFA_SUCCESS_32; or FFA_ERROR, with following
1183 * error codes:
1184 * - FFA_INVALID_PARAMETERS: invalid range of memory.
1185 * - FFA_DENIED:
1186 *
Jose Marinho09b1db82019-08-08 09:16:59 +01001187 * made to memory mappings.
1188 */
J-Alvescf6253e2024-01-03 13:48:48 +00001189struct ffa_value ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +00001190 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001191 struct ffa_memory_region_constituent **fragments,
1192 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alvescf6253e2024-01-03 13:48:48 +00001193 uint32_t mode, struct mpool *ppool, enum ffa_map_action action,
1194 bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001195{
Andrew Walbranca808b12020-05-15 17:22:28 +01001196 uint32_t i;
1197 uint32_t j;
J-Alvescf6253e2024-01-03 13:48:48 +00001198 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001199
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001200 if (vm_locked.vm->el0_partition) {
1201 mode |= MM_MODE_USER | MM_MODE_NG;
1202 }
1203
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 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
J-Alves063ad832023-10-03 18:05:40 +01001207 struct ffa_memory_region_constituent *constituent =
1208 &fragments[i][j];
1209 size_t size = constituent->page_count * PAGE_SIZE;
Andrew Walbranca808b12020-05-15 17:22:28 +01001210 paddr_t pa_begin =
J-Alves063ad832023-10-03 18:05:40 +01001211 pa_from_ipa(ipa_init(constituent->address));
Andrew Walbranca808b12020-05-15 17:22:28 +01001212 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001213 uint32_t pa_bits =
1214 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +01001215
1216 /*
1217 * Ensure the requested region falls into system's PA
1218 * range.
1219 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001220 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
1221 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +01001222 dlog_error("Region is outside of PA Range\n");
J-Alvescf6253e2024-01-03 13:48:48 +00001223 return ffa_error(FFA_INVALID_PARAMETERS);
Federico Recanati4fd065d2021-12-13 20:06:23 +01001224 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001225
J-Alvescf6253e2024-01-03 13:48:48 +00001226 if (action <= MAP_ACTION_CHECK_PROTECT) {
1227 ret = ffa_region_group_check_actions(
1228 vm_locked, pa_begin, pa_end, ppool,
1229 mode, action, memory_protected);
J-Alves063ad832023-10-03 18:05:40 +01001230
1231 if (ret.func == FFA_ERROR_32 &&
1232 ffa_error_code(ret) == FFA_DENIED) {
1233 if (memory_protected != NULL) {
1234 assert(!*memory_protected);
1235 }
1236
1237 ffa_region_group_fragments_revert_protect(
1238 fragments,
1239 fragment_constituent_counts,
1240 i + 1, constituent);
1241 break;
1242 }
J-Alvescf6253e2024-01-03 13:48:48 +00001243 } else if (action >= MAP_ACTION_COMMIT &&
1244 action < MAP_ACTION_MAX) {
1245 ffa_region_group_commit_actions(
1246 vm_locked, pa_begin, pa_end, ppool,
1247 mode, action);
1248 ret = (struct ffa_value){
1249 .func = FFA_SUCCESS_32};
1250 } else {
1251 panic("%s: Unknown ffa_map_action.\n",
1252 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001253 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001254 }
1255 }
1256
J-Alvescf6253e2024-01-03 13:48:48 +00001257 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001258}
1259
1260/**
1261 * Clears a region of physical memory by overwriting it with zeros. The data is
1262 * flushed from the cache so the memory has been cleared across the system.
1263 */
J-Alves7db32002021-12-14 14:44:50 +00001264static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
1265 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +01001266{
1267 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +00001268 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +01001269 * global mapping of the whole range. Such an approach will limit
1270 * the changes to stage-1 tables and will allow only local
1271 * invalidation.
1272 */
1273 bool ret;
1274 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +00001275 void *ptr = mm_identity_map(stage1_locked, begin, end,
1276 MM_MODE_W | (extra_mode_attributes &
1277 plat_ffa_other_world_mode()),
1278 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001279 size_t size = pa_difference(begin, end);
1280
1281 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001282 goto fail;
1283 }
1284
1285 memset_s(ptr, size, 0, size);
1286 arch_mm_flush_dcache(ptr, size);
1287 mm_unmap(stage1_locked, begin, end, ppool);
1288
1289 ret = true;
1290 goto out;
1291
1292fail:
1293 ret = false;
1294
1295out:
1296 mm_unlock_stage1(&stage1_locked);
1297
1298 return ret;
1299}
1300
1301/**
1302 * Clears a region of physical memory by overwriting it with zeros. The data is
1303 * flushed from the cache so the memory has been cleared across the system.
1304 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001305static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +00001306 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01001307 struct ffa_memory_region_constituent **fragments,
1308 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1309 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001310{
1311 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +01001312 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +01001313 bool ret = false;
1314
1315 /*
1316 * Create a local pool so any freed memory can't be used by another
1317 * thread. This is to ensure each constituent that is mapped can be
1318 * unmapped again afterwards.
1319 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001320 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001321
Andrew Walbranca808b12020-05-15 17:22:28 +01001322 /* Iterate over the memory region constituents within each fragment. */
1323 for (i = 0; i < fragment_count; ++i) {
1324 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001325
J-Alves8457f932023-10-11 16:41:45 +01001326 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001327 size_t size = fragments[i][j].page_count * PAGE_SIZE;
1328 paddr_t begin =
1329 pa_from_ipa(ipa_init(fragments[i][j].address));
1330 paddr_t end = pa_add(begin, size);
1331
J-Alves7db32002021-12-14 14:44:50 +00001332 if (!clear_memory(begin, end, &local_page_pool,
1333 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001334 /*
1335 * api_clear_memory will defrag on failure, so
1336 * no need to do it here.
1337 */
1338 goto out;
1339 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001340 }
1341 }
1342
Jose Marinho09b1db82019-08-08 09:16:59 +01001343 ret = true;
1344
1345out:
1346 mpool_fini(&local_page_pool);
1347 return ret;
1348}
1349
J-Alves5952d942022-12-22 16:03:00 +00001350static bool is_memory_range_within(ipaddr_t begin, ipaddr_t end,
1351 ipaddr_t in_begin, ipaddr_t in_end)
1352{
1353 return (ipa_addr(begin) >= ipa_addr(in_begin) &&
1354 ipa_addr(begin) < ipa_addr(in_end)) ||
1355 (ipa_addr(end) <= ipa_addr(in_end) &&
1356 ipa_addr(end) > ipa_addr(in_begin));
1357}
1358
1359/**
1360 * Receives a memory range and looks for overlaps with the remainder
1361 * constituents of the memory share/lend/donate operation. Assumes they are
1362 * passed in order to avoid having to loop over all the elements at each call.
1363 * The function only compares the received memory ranges with those that follow
1364 * within the same fragment, and subsequent fragments from the same operation.
1365 */
1366static bool ffa_memory_check_overlap(
1367 struct ffa_memory_region_constituent **fragments,
1368 const uint32_t *fragment_constituent_counts,
1369 const uint32_t fragment_count, const uint32_t current_fragment,
1370 const uint32_t current_constituent)
1371{
1372 uint32_t i = current_fragment;
1373 uint32_t j = current_constituent;
1374 ipaddr_t current_begin = ipa_init(fragments[i][j].address);
1375 const uint32_t current_page_count = fragments[i][j].page_count;
1376 size_t current_size = current_page_count * PAGE_SIZE;
1377 ipaddr_t current_end = ipa_add(current_begin, current_size - 1);
1378
1379 if (current_size == 0 ||
1380 current_size > UINT64_MAX - ipa_addr(current_begin)) {
Karl Meakine8937d92024-03-19 16:04:25 +00001381 dlog_verbose("Invalid page count. Addr: %zx page_count: %x\n",
1382 current_begin.ipa, current_page_count);
J-Alves5952d942022-12-22 16:03:00 +00001383 return false;
1384 }
1385
1386 for (; i < fragment_count; i++) {
1387 j = (i == current_fragment) ? j + 1 : 0;
1388
1389 for (; j < fragment_constituent_counts[i]; j++) {
1390 ipaddr_t begin = ipa_init(fragments[i][j].address);
1391 const uint32_t page_count = fragments[i][j].page_count;
1392 size_t size = page_count * PAGE_SIZE;
1393 ipaddr_t end = ipa_add(begin, size - 1);
1394
1395 if (size == 0 || size > UINT64_MAX - ipa_addr(begin)) {
1396 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00001397 "Invalid page count. Addr: %lx "
J-Alves5952d942022-12-22 16:03:00 +00001398 "page_count: %x\n",
Karl Meakine8937d92024-03-19 16:04:25 +00001399 begin.ipa, page_count);
J-Alves5952d942022-12-22 16:03:00 +00001400 return false;
1401 }
1402
1403 /*
1404 * Check if current ranges is within begin and end, as
1405 * well as the reverse. This should help optimize the
1406 * loop, and reduce the number of iterations.
1407 */
1408 if (is_memory_range_within(begin, end, current_begin,
1409 current_end) ||
1410 is_memory_range_within(current_begin, current_end,
1411 begin, end)) {
1412 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00001413 "Overlapping memory ranges: %#lx - "
1414 "%#lx with %#lx - %#lx\n",
J-Alves5952d942022-12-22 16:03:00 +00001415 ipa_addr(begin), ipa_addr(end),
1416 ipa_addr(current_begin),
1417 ipa_addr(current_end));
1418 return true;
1419 }
1420 }
1421 }
1422
1423 return false;
1424}
1425
Jose Marinho09b1db82019-08-08 09:16:59 +01001426/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001427 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +01001428 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001429 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +01001430 *
1431 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001432 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001433 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +01001434 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001435 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
1436 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001437 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +01001438 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001439 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +01001440 */
Daniel Boulbya76fd912024-02-22 14:22:15 +00001441static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001442 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001443 struct ffa_memory_region_constituent **fragments,
1444 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves8f11cde2022-12-21 16:18:22 +00001445 uint32_t composite_total_page_count, uint32_t share_func,
Daniel Boulbya76fd912024-02-22 14:22:15 +00001446 struct ffa_memory_region *memory_region, struct mpool *page_pool,
1447 uint32_t *orig_from_mode_ret, bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001448{
Andrew Walbranca808b12020-05-15 17:22:28 +01001449 uint32_t i;
J-Alves8f11cde2022-12-21 16:18:22 +00001450 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001451 uint32_t orig_from_mode;
J-Alves460d36c2023-10-12 17:02:15 +01001452 uint32_t clean_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001453 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001454 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001455 struct ffa_value ret;
J-Alves8f11cde2022-12-21 16:18:22 +00001456 uint32_t constituents_total_page_count = 0;
J-Alves460d36c2023-10-12 17:02:15 +01001457 enum ffa_map_action map_action = MAP_ACTION_CHECK;
Daniel Boulbya76fd912024-02-22 14:22:15 +00001458 bool clear = memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Jose Marinho09b1db82019-08-08 09:16:59 +01001459
1460 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001461 * Make sure constituents are properly aligned to a 64-bit boundary. If
1462 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +01001463 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001464 for (i = 0; i < fragment_count; ++i) {
1465 if (!is_aligned(fragments[i], 8)) {
1466 dlog_verbose("Constituents not aligned.\n");
1467 return ffa_error(FFA_INVALID_PARAMETERS);
1468 }
J-Alves8f11cde2022-12-21 16:18:22 +00001469 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
1470 constituents_total_page_count +=
1471 fragments[i][j].page_count;
J-Alves5952d942022-12-22 16:03:00 +00001472 if (ffa_memory_check_overlap(
1473 fragments, fragment_constituent_counts,
1474 fragment_count, i, j)) {
1475 return ffa_error(FFA_INVALID_PARAMETERS);
1476 }
J-Alves8f11cde2022-12-21 16:18:22 +00001477 }
1478 }
1479
1480 if (constituents_total_page_count != composite_total_page_count) {
1481 dlog_verbose(
1482 "Composite page count differs from calculated page "
1483 "count from constituents.\n");
1484 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +01001485 }
1486
1487 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001488 * Check if the state transition is lawful for the sender, ensure that
1489 * all constituents of a memory region being shared are at the same
1490 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +01001491 */
J-Alves460d36c2023-10-12 17:02:15 +01001492 ret = ffa_send_check_transition(
Daniel Boulbya76fd912024-02-22 14:22:15 +00001493 from_locked, share_func, memory_region, &orig_from_mode,
1494 fragments, fragment_constituent_counts, fragment_count,
Daniel Boulby4b846eb2024-05-23 17:32:23 +01001495 &from_mode, &map_action, clear);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001496 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001497 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001498 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001499 }
1500
Andrew Walbran37c574e2020-06-03 11:45:46 +01001501 if (orig_from_mode_ret != NULL) {
1502 *orig_from_mode_ret = orig_from_mode;
1503 }
1504
Jose Marinho09b1db82019-08-08 09:16:59 +01001505 /*
1506 * Create a local pool so any freed memory can't be used by another
1507 * thread. This is to ensure the original mapping can be restored if the
1508 * clear fails.
1509 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001510 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001511
1512 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001513 * First reserve all required memory for the new page table entries
1514 * without committing, to make sure the entire operation will succeed
1515 * without exhausting the page pool.
J-Alves460d36c2023-10-12 17:02:15 +01001516 * Provide the map_action as populated by 'ffa_send_check_transition'.
1517 * It may request memory to be protected.
Jose Marinho09b1db82019-08-08 09:16:59 +01001518 */
J-Alvescf6253e2024-01-03 13:48:48 +00001519 ret = ffa_region_group_identity_map(
1520 from_locked, fragments, fragment_constituent_counts,
J-Alves460d36c2023-10-12 17:02:15 +01001521 fragment_count, from_mode, page_pool, map_action,
1522 memory_protected);
J-Alvescf6253e2024-01-03 13:48:48 +00001523 if (ret.func == FFA_ERROR_32) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001524 goto out;
1525 }
1526
1527 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001528 * Update the mapping for the sender. This won't allocate because the
1529 * transaction was already prepared above, but may free pages in the
1530 * case that a whole block is being unmapped that was previously
1531 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +01001532 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001533 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001534 from_locked, fragments, fragment_constituent_counts,
1535 fragment_count, from_mode, &local_page_pool,
1536 MAP_ACTION_COMMIT, NULL)
1537 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001538
J-Alves460d36c2023-10-12 17:02:15 +01001539 /*
1540 * If memory has been protected, it is now part of the secure PAS
1541 * (happens for lend/donate from NWd to SWd), and the `orig_from_mode`
1542 * should have the MM_MODE_NS set, as such mask it in `clean_mode` for
1543 * SPM's S1 translation.
1544 * In case memory hasn't been protected, and it is in the non-secure
1545 * PAS (e.g. memory share from NWd to SWd), as such the SPM needs to
1546 * perform a non-secure memory access. In such case `clean_mode` takes
1547 * the same mode as `orig_from_mode`.
1548 */
1549 clean_mode = (memory_protected != NULL && *memory_protected)
1550 ? orig_from_mode & ~plat_ffa_other_world_mode()
1551 : orig_from_mode;
1552
Jose Marinho09b1db82019-08-08 09:16:59 +01001553 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves460d36c2023-10-12 17:02:15 +01001554 if (clear && !ffa_clear_memory_constituents(
1555 clean_mode, fragments, fragment_constituent_counts,
1556 fragment_count, page_pool)) {
1557 map_action = (memory_protected != NULL && *memory_protected)
1558 ? MAP_ACTION_COMMIT_UNPROTECT
1559 : MAP_ACTION_COMMIT;
1560
Jose Marinho09b1db82019-08-08 09:16:59 +01001561 /*
1562 * On failure, roll back by returning memory to the sender. This
1563 * may allocate pages which were previously freed into
1564 * `local_page_pool` by the call above, but will never allocate
1565 * more pages than that so can never fail.
1566 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001567 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001568 from_locked, fragments,
1569 fragment_constituent_counts, fragment_count,
1570 orig_from_mode, &local_page_pool,
1571 MAP_ACTION_COMMIT, NULL)
1572 .func == FFA_SUCCESS_32);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001573 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +01001574 goto out;
1575 }
1576
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001577 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001578
1579out:
1580 mpool_fini(&local_page_pool);
1581
1582 /*
1583 * Tidy up the page table by reclaiming failed mappings (if there was an
1584 * error) or merging entries into blocks where possible (on success).
1585 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001586 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001587
1588 return ret;
1589}
1590
1591/**
1592 * Validates and maps memory shared from one VM to another.
1593 *
1594 * This function requires the calling context to hold the <to> lock.
1595 *
1596 * Returns:
1597 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001598 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001599 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001600 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001601 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001602 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001603 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001604struct ffa_value ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01001605 struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001606 struct ffa_memory_region_constituent **fragments,
1607 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves26483382023-04-20 12:01:49 +01001608 uint32_t sender_orig_mode, uint32_t share_func, bool clear,
J-Alves460d36c2023-10-12 17:02:15 +01001609 struct mpool *page_pool, uint32_t *response_mode, bool memory_protected)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001610{
Andrew Walbranca808b12020-05-15 17:22:28 +01001611 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001612 uint32_t to_mode;
1613 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001614 struct ffa_value ret;
J-Alvesfd206052023-05-22 16:45:00 +01001615 enum ffa_map_action map_action = MAP_ACTION_COMMIT;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001616
1617 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001618 * Make sure constituents are properly aligned to a 64-bit boundary. If
1619 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001620 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001621 for (i = 0; i < fragment_count; ++i) {
1622 if (!is_aligned(fragments[i], 8)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001623 dlog_verbose("Fragment not properly aligned.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +01001624 return ffa_error(FFA_INVALID_PARAMETERS);
1625 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001626 }
1627
1628 /*
Daniel Boulby4b846eb2024-05-23 17:32:23 +01001629 * Ensure the sender has write permissions if the memory needs to be
1630 * cleared.
1631 */
1632 if ((sender_orig_mode & MM_MODE_W) == 0 && clear == true) {
1633 dlog_verbose(
1634 "Cannot zero memory when the sender does not have "
1635 "write access\n");
1636 return ffa_error(FFA_DENIED);
1637 }
1638
1639 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001640 * Check if the state transition is lawful for the recipient, and ensure
1641 * that all constituents of the memory region being retrieved are at the
1642 * same state.
1643 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001644 ret = ffa_retrieve_check_transition(
1645 to_locked, share_func, fragments, fragment_constituent_counts,
J-Alvesfd206052023-05-22 16:45:00 +01001646 fragment_count, sender_orig_mode, &to_mode, memory_protected,
1647 &map_action);
J-Alves460d36c2023-10-12 17:02:15 +01001648
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001649 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001650 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001651 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001652 }
1653
1654 /*
J-Alves69cdfd92024-04-26 11:40:59 +01001655 * Create a local pool so any freed memory can't be used by
1656 * another thread. This is to ensure the original mapping can be
1657 * restored if the clear fails.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001658 */
1659 mpool_init_with_fallback(&local_page_pool, page_pool);
1660
1661 /*
J-Alves69cdfd92024-04-26 11:40:59 +01001662 * Memory retrieves from the NWd VMs don't require update to S2 PTs on
1663 * retrieve request.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001664 */
J-Alves69cdfd92024-04-26 11:40:59 +01001665 if (map_action != MAP_ACTION_NONE) {
1666 /*
1667 * First reserve all required memory for the new page table
1668 * entries in the recipient page tables without committing, to
1669 * make sure the entire operation will succeed without
1670 * exhausting the page pool.
1671 */
1672 ret = ffa_region_group_identity_map(
1673 to_locked, fragments, fragment_constituent_counts,
1674 fragment_count, to_mode, page_pool, MAP_ACTION_CHECK,
1675 NULL);
1676 if (ret.func == FFA_ERROR_32) {
1677 /* TODO: partial defrag of failed range. */
1678 goto out;
1679 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001680 }
1681
1682 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001683 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001684 !ffa_clear_memory_constituents(sender_orig_mode, fragments,
1685 fragment_constituent_counts,
1686 fragment_count, page_pool)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001687 dlog_verbose("Couldn't clear constituents.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001688 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001689 goto out;
1690 }
1691
J-Alves69cdfd92024-04-26 11:40:59 +01001692 if (map_action != MAP_ACTION_NONE) {
1693 /*
1694 * Complete the transfer by mapping the memory into the
1695 * recipient. This won't allocate because the transaction was
1696 * already prepared above, so it doesn't need to use the
1697 * `local_page_pool`.
1698 */
1699 CHECK(ffa_region_group_identity_map(to_locked, fragments,
1700 fragment_constituent_counts,
1701 fragment_count, to_mode,
1702 page_pool, map_action, NULL)
1703 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001704
J-Alves69cdfd92024-04-26 11:40:59 +01001705 /*
1706 * Return the mode used in mapping the memory in retriever's PT.
1707 */
1708 if (response_mode != NULL) {
1709 *response_mode = to_mode;
1710 }
J-Alves460d36c2023-10-12 17:02:15 +01001711 }
1712
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001713 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001714
1715out:
1716 mpool_fini(&local_page_pool);
1717
1718 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001719 * Tidy up the page table by reclaiming failed mappings (if there was an
1720 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001721 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001722 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001723
1724 return ret;
1725}
1726
Andrew Walbran996d1d12020-05-27 14:08:43 +01001727static struct ffa_value ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01001728 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001729 struct ffa_memory_region_constituent **fragments,
1730 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves69cdfd92024-04-26 11:40:59 +01001731 uint32_t sender_orig_mode, struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001732{
1733 uint32_t orig_from_mode;
J-Alves69cdfd92024-04-26 11:40:59 +01001734 uint32_t clearing_mode;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001735 uint32_t from_mode;
1736 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001737 struct ffa_value ret;
J-Alves69cdfd92024-04-26 11:40:59 +01001738 enum ffa_map_action map_action;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001739
Andrew Walbranca808b12020-05-15 17:22:28 +01001740 ret = ffa_relinquish_check_transition(
1741 from_locked, &orig_from_mode, fragments,
J-Alves69cdfd92024-04-26 11:40:59 +01001742 fragment_constituent_counts, fragment_count, &from_mode,
1743 &map_action);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001744 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001745 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001746 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001747 }
1748
1749 /*
1750 * Create a local pool so any freed memory can't be used by another
1751 * thread. This is to ensure the original mapping can be restored if the
1752 * clear fails.
1753 */
1754 mpool_init_with_fallback(&local_page_pool, page_pool);
1755
J-Alves69cdfd92024-04-26 11:40:59 +01001756 if (map_action != MAP_ACTION_NONE) {
1757 clearing_mode = orig_from_mode;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001758
J-Alves69cdfd92024-04-26 11:40:59 +01001759 /*
1760 * First reserve all required memory for the new page table
1761 * entries without committing, to make sure the entire operation
1762 * will succeed without exhausting the page pool.
1763 */
1764 ret = ffa_region_group_identity_map(
1765 from_locked, fragments, fragment_constituent_counts,
1766 fragment_count, from_mode, page_pool, MAP_ACTION_CHECK,
1767 NULL);
1768 if (ret.func == FFA_ERROR_32) {
1769 goto out;
1770 }
1771
1772 /*
1773 * Update the mapping for the sender. This won't allocate
1774 * because the transaction was already prepared above, but may
1775 * free pages in the case that a whole block is being unmapped
1776 * that was previously partially mapped.
1777 */
1778 CHECK(ffa_region_group_identity_map(from_locked, fragments,
1779 fragment_constituent_counts,
1780 fragment_count, from_mode,
1781 &local_page_pool,
1782 MAP_ACTION_COMMIT, NULL)
1783 .func == FFA_SUCCESS_32);
1784 } else {
1785 /*
1786 * If the `map_action` is set to `MAP_ACTION_NONE`, S2 PTs
1787 * were not updated on retrieve/relinquish. These were updating
1788 * only the `share_state` structures. As such, use the sender's
1789 * original mode.
1790 */
1791 clearing_mode = sender_orig_mode;
1792 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001793
1794 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001795 if (clear &&
J-Alves69cdfd92024-04-26 11:40:59 +01001796 !ffa_clear_memory_constituents(clearing_mode, fragments,
J-Alves26483382023-04-20 12:01:49 +01001797 fragment_constituent_counts,
1798 fragment_count, page_pool)) {
J-Alves69cdfd92024-04-26 11:40:59 +01001799 if (map_action != MAP_ACTION_NONE) {
1800 /*
1801 * On failure, roll back by returning memory to the
1802 * sender. This may allocate pages which were previously
1803 * freed into `local_page_pool` by the call above, but
1804 * will never allocate more pages than that so can never
1805 * fail.
1806 */
1807 CHECK(ffa_region_group_identity_map(
1808 from_locked, fragments,
1809 fragment_constituent_counts,
1810 fragment_count, orig_from_mode,
1811 &local_page_pool, MAP_ACTION_COMMIT, NULL)
1812 .func == FFA_SUCCESS_32);
1813 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001814 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001815 goto out;
1816 }
1817
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001818 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001819
1820out:
1821 mpool_fini(&local_page_pool);
1822
1823 /*
1824 * Tidy up the page table by reclaiming failed mappings (if there was an
1825 * error) or merging entries into blocks where possible (on success).
1826 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001827 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001828
1829 return ret;
1830}
1831
1832/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001833 * Complete a memory sending operation by checking that it is valid, updating
1834 * the sender page table, and then either marking the share state as having
1835 * completed sending (on success) or freeing it (on failure).
1836 *
1837 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1838 */
J-Alvesfdd29272022-07-19 13:16:31 +01001839struct ffa_value ffa_memory_send_complete(
Andrew Walbranca808b12020-05-15 17:22:28 +01001840 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001841 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1842 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001843{
1844 struct ffa_memory_region *memory_region = share_state->memory_region;
J-Alves8f11cde2022-12-21 16:18:22 +00001845 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01001846 struct ffa_value ret;
1847
1848 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001849 assert(share_states.share_states != NULL);
J-Alves8f11cde2022-12-21 16:18:22 +00001850 assert(memory_region != NULL);
1851 composite = ffa_memory_region_get_composite(memory_region, 0);
1852 assert(composite != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001853
1854 /* Check that state is valid in sender page table and update. */
1855 ret = ffa_send_check_update(
1856 from_locked, share_state->fragments,
1857 share_state->fragment_constituent_counts,
J-Alves8f11cde2022-12-21 16:18:22 +00001858 share_state->fragment_count, composite->page_count,
Daniel Boulbya76fd912024-02-22 14:22:15 +00001859 share_state->share_func, memory_region, page_pool,
J-Alves460d36c2023-10-12 17:02:15 +01001860 orig_from_mode_ret, &share_state->memory_protected);
Andrew Walbranca808b12020-05-15 17:22:28 +01001861 if (ret.func != FFA_SUCCESS_32) {
1862 /*
1863 * Free share state, it failed to send so it can't be retrieved.
1864 */
Karl Meakin4cec5e82023-06-30 16:30:22 +01001865 dlog_verbose("%s: failed to send check update: %s(%s)\n",
1866 __func__, ffa_func_name(ret.func),
1867 ffa_error_name(ffa_error_code(ret)));
Andrew Walbranca808b12020-05-15 17:22:28 +01001868 share_state_free(share_states, share_state, page_pool);
1869 return ret;
1870 }
1871
1872 share_state->sending_complete = true;
Karl Meakin4cec5e82023-06-30 16:30:22 +01001873 dlog_verbose("%s: marked sending complete.\n", __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001874
J-Alvesee68c542020-10-29 17:48:20 +00001875 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001876}
1877
1878/**
Daniel Boulby9764ff62024-01-30 17:47:39 +00001879 * Check that the memory attributes match Hafnium expectations.
1880 * Cacheability:
1881 * - Normal Memory as `FFA_MEMORY_CACHE_WRITE_BACK`.
1882 * - Device memory as `FFA_MEMORY_DEV_NGNRNE`.
1883 *
1884 * Shareability:
1885 * - Inner Shareable.
Federico Recanatia98603a2021-12-20 18:04:03 +01001886 */
1887static struct ffa_value ffa_memory_attributes_validate(
J-Alves7a99d0d2023-02-08 13:49:48 +00001888 ffa_memory_attributes_t attributes)
Federico Recanatia98603a2021-12-20 18:04:03 +01001889{
1890 enum ffa_memory_type memory_type;
1891 enum ffa_memory_cacheability cacheability;
1892 enum ffa_memory_shareability shareability;
1893
Karl Meakin84710f32023-10-12 15:14:49 +01001894 memory_type = attributes.type;
Daniel Boulby9764ff62024-01-30 17:47:39 +00001895 cacheability = attributes.cacheability;
1896 if (memory_type == FFA_MEMORY_NORMAL_MEM &&
1897 cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1898 dlog_verbose(
1899 "Normal Memory: Invalid cacheability %s, "
1900 "expected %s.\n",
1901 ffa_memory_cacheability_name(cacheability),
1902 ffa_memory_cacheability_name(
1903 FFA_MEMORY_CACHE_WRITE_BACK));
Federico Recanati3d953f32022-02-17 09:31:29 +01001904 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001905 }
Daniel Boulby9764ff62024-01-30 17:47:39 +00001906 if (memory_type == FFA_MEMORY_DEVICE_MEM &&
1907 cacheability != FFA_MEMORY_DEV_NGNRNE) {
1908 dlog_verbose(
1909 "Device Memory: Invalid cacheability %s, "
1910 "expected %s.\n",
1911 ffa_device_memory_cacheability_name(cacheability),
1912 ffa_device_memory_cacheability_name(
1913 FFA_MEMORY_DEV_NGNRNE));
Federico Recanati3d953f32022-02-17 09:31:29 +01001914 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001915 }
1916
Karl Meakin84710f32023-10-12 15:14:49 +01001917 shareability = attributes.shareability;
Federico Recanatia98603a2021-12-20 18:04:03 +01001918 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
Karl Meakinf98b2aa2023-10-12 16:09:59 +01001919 dlog_verbose("Invalid shareability %s, expected %s.\n",
1920 ffa_memory_shareability_name(shareability),
1921 ffa_memory_shareability_name(
1922 FFA_MEMORY_INNER_SHAREABLE));
Federico Recanati3d953f32022-02-17 09:31:29 +01001923 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001924 }
1925
1926 return (struct ffa_value){.func = FFA_SUCCESS_32};
1927}
1928
1929/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001930 * Check that the given `memory_region` represents a valid memory send request
1931 * of the given `share_func` type, return the clear flag and permissions via the
1932 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001933 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001934 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001935 * not.
1936 */
J-Alves66652252022-07-06 09:49:51 +01001937struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001938 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1939 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001940 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001941{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001942 struct ffa_composite_memory_region *composite;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001943 struct ffa_memory_access *receiver =
1944 ffa_memory_region_get_receiver(memory_region, 0);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001945 uint64_t receivers_end;
1946 uint64_t min_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001947 uint32_t composite_memory_region_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001948 uint32_t constituents_start;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001949 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001950 enum ffa_data_access data_access;
1951 enum ffa_instruction_access instruction_access;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001952 enum ffa_memory_security security_state;
Karl Meakinf98b2aa2023-10-12 16:09:59 +01001953 enum ffa_memory_type type;
Federico Recanatia98603a2021-12-20 18:04:03 +01001954 struct ffa_value ret;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001955 const size_t minimum_first_fragment_length =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001956 memory_region->receivers_offset +
1957 memory_region->memory_access_desc_size +
1958 sizeof(struct ffa_composite_memory_region);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001959
1960 if (fragment_length < minimum_first_fragment_length) {
Karl Meakine8937d92024-03-19 16:04:25 +00001961 dlog_verbose("Fragment length %u too short (min %zu).\n",
1962 fragment_length, minimum_first_fragment_length);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001963 return ffa_error(FFA_INVALID_PARAMETERS);
1964 }
1965
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05001966 static_assert(sizeof(struct ffa_memory_region_constituent) == 16,
1967 "struct ffa_memory_region_constituent must be 16 bytes");
1968 if (!is_aligned(fragment_length,
1969 sizeof(struct ffa_memory_region_constituent)) ||
1970 !is_aligned(memory_share_length,
1971 sizeof(struct ffa_memory_region_constituent))) {
1972 dlog_verbose(
1973 "Fragment length %u or total length %u"
1974 " is not 16-byte aligned.\n",
1975 fragment_length, memory_share_length);
1976 return ffa_error(FFA_INVALID_PARAMETERS);
1977 }
1978
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001979 if (fragment_length > memory_share_length) {
1980 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00001981 "Fragment length %zu greater than total length %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001982 (size_t)fragment_length, (size_t)memory_share_length);
1983 return ffa_error(FFA_INVALID_PARAMETERS);
1984 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001985
J-Alves95df0ef2022-12-07 10:09:48 +00001986 /* The sender must match the caller. */
1987 if ((!vm_id_is_current_world(from_locked.vm->id) &&
1988 vm_id_is_current_world(memory_region->sender)) ||
1989 (vm_id_is_current_world(from_locked.vm->id) &&
1990 memory_region->sender != from_locked.vm->id)) {
1991 dlog_verbose("Invalid memory sender ID.\n");
1992 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001993 }
1994
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001995 if (memory_region->receiver_count <= 0) {
1996 dlog_verbose("No receivers!\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001997 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001998 }
1999
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002000 /*
2001 * Ensure that the composite header is within the memory bounds and
2002 * doesn't overlap the first part of the message. Cast to uint64_t
2003 * to prevent overflow.
2004 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002005 receivers_end = ((uint64_t)memory_region->memory_access_desc_size *
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002006 (uint64_t)memory_region->receiver_count) +
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01002007 memory_region->receivers_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002008 min_length = receivers_end +
2009 sizeof(struct ffa_composite_memory_region) +
2010 sizeof(struct ffa_memory_region_constituent);
2011 if (min_length > memory_share_length) {
Karl Meakine8937d92024-03-19 16:04:25 +00002012 dlog_verbose("Share too short: got %zu but minimum is %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002013 (size_t)memory_share_length, (size_t)min_length);
2014 return ffa_error(FFA_INVALID_PARAMETERS);
2015 }
2016
2017 composite_memory_region_offset =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002018 receiver->composite_memory_region_offset;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002019
2020 /*
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002021 * Check that the composite memory region descriptor is after the access
2022 * descriptors, is at least 16-byte aligned, and fits in the first
2023 * fragment.
Andrew Walbrana65a1322020-04-06 19:32:32 +01002024 */
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002025 if ((composite_memory_region_offset < receivers_end) ||
2026 (composite_memory_region_offset % 16 != 0) ||
2027 (composite_memory_region_offset >
2028 fragment_length - sizeof(struct ffa_composite_memory_region))) {
2029 dlog_verbose(
2030 "Invalid composite memory region descriptor offset "
Karl Meakine8937d92024-03-19 16:04:25 +00002031 "%zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002032 (size_t)composite_memory_region_offset);
2033 return ffa_error(FFA_INVALID_PARAMETERS);
2034 }
2035
2036 /*
2037 * Compute the start of the constituent regions. Already checked
2038 * to be not more than fragment_length and thus not more than
2039 * memory_share_length.
2040 */
2041 constituents_start = composite_memory_region_offset +
2042 sizeof(struct ffa_composite_memory_region);
2043 constituents_length = memory_share_length - constituents_start;
2044
2045 /*
2046 * Check that the number of constituents is consistent with the length
2047 * of the constituent region.
2048 */
2049 composite = ffa_memory_region_get_composite(memory_region, 0);
2050 if ((constituents_length %
2051 sizeof(struct ffa_memory_region_constituent) !=
2052 0) ||
2053 ((constituents_length /
2054 sizeof(struct ffa_memory_region_constituent)) !=
2055 composite->constituent_count)) {
Karl Meakine8937d92024-03-19 16:04:25 +00002056 dlog_verbose("Invalid length %zu or composite offset %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002057 (size_t)memory_share_length,
2058 (size_t)composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002059 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002060 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002061 if (fragment_length < memory_share_length &&
2062 fragment_length < HF_MAILBOX_SIZE) {
2063 dlog_warning(
2064 "Initial fragment length %d smaller than mailbox "
2065 "size.\n",
2066 fragment_length);
2067 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002068
Andrew Walbrana65a1322020-04-06 19:32:32 +01002069 /*
2070 * Clear is not allowed for memory sharing, as the sender still has
2071 * access to the memory.
2072 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002073 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
J-Alves95fbb312024-03-20 15:19:16 +00002074 (share_func == FFA_MEM_SHARE_32 ||
2075 share_func == FFA_MEM_SHARE_64)) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01002076 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002077 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002078 }
2079
2080 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002081 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01002082 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002083 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002084 }
2085
J-Alves363f5722022-04-25 17:37:37 +01002086 /* Check that the permissions are valid, for each specified receiver. */
2087 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002088 struct ffa_memory_region_attributes receiver_permissions;
2089
2090 receiver = ffa_memory_region_get_receiver(memory_region, i);
2091 assert(receiver != NULL);
2092 receiver_permissions = receiver->receiver_permissions;
J-Alves363f5722022-04-25 17:37:37 +01002093 ffa_memory_access_permissions_t permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002094 receiver_permissions.permissions;
2095 ffa_id_t receiver_id = receiver_permissions.receiver;
J-Alves363f5722022-04-25 17:37:37 +01002096
2097 if (memory_region->sender == receiver_id) {
2098 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002099 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002100 }
Federico Recanati85090c42021-12-15 13:17:54 +01002101
J-Alves363f5722022-04-25 17:37:37 +01002102 for (uint32_t j = i + 1; j < memory_region->receiver_count;
2103 j++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002104 struct ffa_memory_access *other_receiver =
2105 ffa_memory_region_get_receiver(memory_region,
2106 j);
2107 assert(other_receiver != NULL);
2108
J-Alves363f5722022-04-25 17:37:37 +01002109 if (receiver_id ==
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002110 other_receiver->receiver_permissions.receiver) {
J-Alves363f5722022-04-25 17:37:37 +01002111 dlog_verbose(
2112 "Repeated receiver(%x) in memory send "
2113 "operation.\n",
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002114 other_receiver->receiver_permissions
2115 .receiver);
J-Alves363f5722022-04-25 17:37:37 +01002116 return ffa_error(FFA_INVALID_PARAMETERS);
2117 }
2118 }
2119
2120 if (composite_memory_region_offset !=
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002121 receiver->composite_memory_region_offset) {
J-Alves363f5722022-04-25 17:37:37 +01002122 dlog_verbose(
2123 "All ffa_memory_access should point to the "
2124 "same composite memory region offset.\n");
2125 return ffa_error(FFA_INVALID_PARAMETERS);
2126 }
2127
Karl Meakin84710f32023-10-12 15:14:49 +01002128 data_access = permissions.data_access;
2129 instruction_access = permissions.instruction_access;
J-Alves363f5722022-04-25 17:37:37 +01002130 if (data_access == FFA_DATA_ACCESS_RESERVED ||
2131 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
2132 dlog_verbose(
2133 "Reserved value for receiver permissions "
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002134 "(data_access = %s, instruction_access = %s)\n",
2135 ffa_data_access_name(data_access),
2136 ffa_instruction_access_name(
2137 instruction_access));
J-Alves363f5722022-04-25 17:37:37 +01002138 return ffa_error(FFA_INVALID_PARAMETERS);
2139 }
2140 if (instruction_access !=
2141 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2142 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002143 "Invalid instruction access permissions %s "
2144 "for sending memory, expected %s.\n",
2145 ffa_instruction_access_name(instruction_access),
2146 ffa_instruction_access_name(
Daniel Boulby91052c32024-05-21 14:09:48 +01002147 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002148 return ffa_error(FFA_INVALID_PARAMETERS);
2149 }
J-Alves95fbb312024-03-20 15:19:16 +00002150 if (share_func == FFA_MEM_SHARE_32 ||
2151 share_func == FFA_MEM_SHARE_64) {
J-Alves363f5722022-04-25 17:37:37 +01002152 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
2153 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002154 "Invalid data access permissions %s "
2155 "for sharing memory, expected %s.\n",
2156 ffa_data_access_name(data_access),
2157 ffa_data_access_name(
2158 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002159 return ffa_error(FFA_INVALID_PARAMETERS);
2160 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002161 /*
2162 * According to section 10.10.3 of the FF-A v1.1 EAC0
2163 * spec, NX is required for share operations (but must
2164 * not be specified by the sender) so set it in the
2165 * copy that we store, ready to be returned to the
2166 * retriever.
2167 */
2168 if (vm_id_is_current_world(receiver_id)) {
Karl Meakin84710f32023-10-12 15:14:49 +01002169 permissions.instruction_access =
2170 FFA_INSTRUCTION_ACCESS_NX;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002171 receiver_permissions.permissions = permissions;
2172 }
J-Alves363f5722022-04-25 17:37:37 +01002173 }
J-Alves95fbb312024-03-20 15:19:16 +00002174 if ((share_func == FFA_MEM_LEND_32 ||
2175 share_func == FFA_MEM_LEND_64) &&
J-Alves363f5722022-04-25 17:37:37 +01002176 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
2177 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002178 "Invalid data access permissions %s for "
2179 "lending memory, expected %s.\n",
2180 ffa_data_access_name(data_access),
2181 ffa_data_access_name(
2182 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002183 return ffa_error(FFA_INVALID_PARAMETERS);
2184 }
2185
J-Alves95fbb312024-03-20 15:19:16 +00002186 if ((share_func == FFA_MEM_DONATE_32 ||
2187 share_func == FFA_MEM_DONATE_64) &&
J-Alves363f5722022-04-25 17:37:37 +01002188 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
2189 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002190 "Invalid data access permissions %s for "
2191 "donating memory, expected %s.\n",
2192 ffa_data_access_name(data_access),
2193 ffa_data_access_name(
2194 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002195 return ffa_error(FFA_INVALID_PARAMETERS);
2196 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002197 }
2198
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002199 /* Memory region attributes NS-Bit MBZ for FFA_MEM_SHARE/LEND/DONATE. */
Karl Meakin84710f32023-10-12 15:14:49 +01002200 security_state = memory_region->attributes.security;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002201 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
2202 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002203 "Invalid security state %s for memory share operation, "
2204 "expected %s.\n",
2205 ffa_memory_security_name(security_state),
2206 ffa_memory_security_name(
2207 FFA_MEMORY_SECURITY_UNSPECIFIED));
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002208 return ffa_error(FFA_INVALID_PARAMETERS);
2209 }
2210
Federico Recanatid937f5e2021-12-20 17:38:23 +01002211 /*
J-Alves807794e2022-06-16 13:42:47 +01002212 * If a memory donate or lend with single borrower, the memory type
2213 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01002214 */
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002215 type = memory_region->attributes.type;
J-Alves807794e2022-06-16 13:42:47 +01002216 if (share_func == FFA_MEM_DONATE_32 ||
J-Alves95fbb312024-03-20 15:19:16 +00002217 share_func == FFA_MEM_DONATE_64 ||
2218 ((share_func == FFA_MEM_LEND_32 || share_func == FFA_MEM_LEND_64) &&
J-Alves807794e2022-06-16 13:42:47 +01002219 memory_region->receiver_count == 1)) {
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002220 if (type != FFA_MEMORY_NOT_SPECIFIED_MEM) {
J-Alves807794e2022-06-16 13:42:47 +01002221 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002222 "Invalid memory type %s for memory share "
2223 "operation, expected %s.\n",
2224 ffa_memory_type_name(type),
2225 ffa_memory_type_name(
2226 FFA_MEMORY_NOT_SPECIFIED_MEM));
J-Alves807794e2022-06-16 13:42:47 +01002227 return ffa_error(FFA_INVALID_PARAMETERS);
2228 }
2229 } else {
2230 /*
2231 * Check that sender's memory attributes match Hafnium
2232 * expectations: Normal Memory, Inner shareable, Write-Back
2233 * Read-Allocate Write-Allocate Cacheable.
2234 */
2235 ret = ffa_memory_attributes_validate(memory_region->attributes);
2236 if (ret.func != FFA_SUCCESS_32) {
2237 return ret;
2238 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01002239 }
2240
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002241 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01002242}
2243
2244/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002245 * Gets the share state for continuing an operation to donate, lend or share
2246 * memory, and checks that it is a valid request.
2247 *
2248 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
2249 * not.
2250 */
J-Alvesfdd29272022-07-19 13:16:31 +01002251struct ffa_value ffa_memory_send_continue_validate(
Andrew Walbranca808b12020-05-15 17:22:28 +01002252 struct share_states_locked share_states, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01002253 struct ffa_memory_share_state **share_state_ret, ffa_id_t from_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01002254 struct mpool *page_pool)
2255{
2256 struct ffa_memory_share_state *share_state;
2257 struct ffa_memory_region *memory_region;
2258
Daniel Boulbya2f8c662021-11-26 17:52:53 +00002259 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01002260
2261 /*
2262 * Look up the share state by handle and make sure that the VM ID
2263 * matches.
2264 */
Karl Meakin4a2854a2023-06-30 16:26:52 +01002265 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00002266 if (share_state == NULL) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002267 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00002268 "Invalid handle %#lx for memory send continuation.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01002269 handle);
2270 return ffa_error(FFA_INVALID_PARAMETERS);
2271 }
2272 memory_region = share_state->memory_region;
2273
J-Alvesfdd29272022-07-19 13:16:31 +01002274 if (vm_id_is_current_world(from_vm_id) &&
2275 memory_region->sender != from_vm_id) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002276 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
2277 return ffa_error(FFA_INVALID_PARAMETERS);
2278 }
2279
2280 if (share_state->sending_complete) {
2281 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00002282 "Sending of memory handle %#lx is already complete.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01002283 handle);
2284 return ffa_error(FFA_INVALID_PARAMETERS);
2285 }
2286
2287 if (share_state->fragment_count == MAX_FRAGMENTS) {
2288 /*
2289 * Log a warning as this is a sign that MAX_FRAGMENTS should
2290 * probably be increased.
2291 */
2292 dlog_warning(
Karl Meakine8937d92024-03-19 16:04:25 +00002293 "Too many fragments for memory share with handle %#lx; "
Andrew Walbranca808b12020-05-15 17:22:28 +01002294 "only %d supported.\n",
2295 handle, MAX_FRAGMENTS);
2296 /* Free share state, as it's not possible to complete it. */
2297 share_state_free(share_states, share_state, page_pool);
2298 return ffa_error(FFA_NO_MEMORY);
2299 }
2300
2301 *share_state_ret = share_state;
2302
2303 return (struct ffa_value){.func = FFA_SUCCESS_32};
2304}
2305
2306/**
J-Alves95df0ef2022-12-07 10:09:48 +00002307 * Checks if there is at least one receiver from the other world.
2308 */
J-Alvesfdd29272022-07-19 13:16:31 +01002309bool memory_region_receivers_from_other_world(
J-Alves95df0ef2022-12-07 10:09:48 +00002310 struct ffa_memory_region *memory_region)
2311{
2312 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002313 struct ffa_memory_access *receiver =
2314 ffa_memory_region_get_receiver(memory_region, i);
2315 assert(receiver != NULL);
2316 ffa_id_t receiver_id = receiver->receiver_permissions.receiver;
2317
2318 if (!vm_id_is_current_world(receiver_id)) {
J-Alves95df0ef2022-12-07 10:09:48 +00002319 return true;
2320 }
2321 }
2322 return false;
2323}
2324
2325/**
J-Alves9da280b2022-12-21 14:55:39 +00002326 * Validates a call to donate, lend or share memory in which Hafnium is the
2327 * designated allocator of the memory handle. In practice, this also means
2328 * Hafnium is responsible for managing the state structures for the transaction.
2329 * If Hafnium is the SPMC, it should allocate the memory handle when either the
2330 * sender is an SP or there is at least one borrower that is an SP.
2331 * If Hafnium is the hypervisor, it should allocate the memory handle when
2332 * operation involves only NWd VMs.
2333 *
2334 * If validation goes well, Hafnium updates the stage-2 page tables of the
2335 * sender. Validation consists of checking if the message length and number of
2336 * memory region constituents match, and if the transition is valid for the
2337 * type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00002338 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002339 * Assumes that the caller has already found and locked the sender VM and copied
2340 * the memory region descriptor from the sender's TX buffer to a freshly
2341 * allocated page from Hafnium's internal pool. The caller must have also
2342 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002343 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002344 * This function takes ownership of the `memory_region` passed in and will free
2345 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01002346 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002347struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002348 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002349 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002350 uint32_t fragment_length, uint32_t share_func,
2351 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01002352{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002353 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002354 struct share_states_locked share_states;
2355 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01002356
2357 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01002358 * If there is an error validating the `memory_region` then we need to
2359 * free it because we own it but we won't be storing it in a share state
2360 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01002361 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002362 ret = ffa_memory_send_validate(from_locked, memory_region,
2363 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01002364 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002365 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002366 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002367 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002368 }
2369
Andrew Walbrana65a1322020-04-06 19:32:32 +01002370 /* Set flag for share function, ready to be retrieved later. */
2371 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00002372 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002373 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002374 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002375 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002376 break;
J-Alves95fbb312024-03-20 15:19:16 +00002377 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002378 case FFA_MEM_LEND_32:
2379 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002380 break;
J-Alves95fbb312024-03-20 15:19:16 +00002381 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002382 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002383 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002384 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002385 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01002386 }
2387
Andrew Walbranca808b12020-05-15 17:22:28 +01002388 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002389 /*
2390 * Allocate a share state before updating the page table. Otherwise if
2391 * updating the page table succeeded but allocating the share state
2392 * failed then it would leave the memory in a state where nobody could
2393 * get it back.
2394 */
Karl Meakin52cdfe72023-06-30 14:49:10 +01002395 share_state = allocate_share_state(share_states, share_func,
2396 memory_region, fragment_length,
2397 FFA_MEMORY_HANDLE_INVALID);
J-Alvesb56aac82023-11-10 09:44:43 +00002398 if (share_state == NULL) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002399 dlog_verbose("Failed to allocate share state.\n");
2400 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01002401 ret = ffa_error(FFA_NO_MEMORY);
2402 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002403 }
2404
Andrew Walbranca808b12020-05-15 17:22:28 +01002405 if (fragment_length == memory_share_length) {
2406 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00002407 ret = ffa_memory_send_complete(
2408 from_locked, share_states, share_state, page_pool,
2409 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002410 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01002411 /*
2412 * Use sender ID from 'memory_region' assuming
2413 * that at this point it has been validated:
2414 * - MBZ at virtual FF-A instance.
2415 */
J-Alves19e20cf2023-08-02 12:48:55 +01002416 ffa_id_t sender_to_ret =
J-Alvesfdd29272022-07-19 13:16:31 +01002417 (from_locked.vm->id == HF_OTHER_WORLD_ID)
2418 ? memory_region->sender
2419 : 0;
Andrew Walbranca808b12020-05-15 17:22:28 +01002420 ret = (struct ffa_value){
2421 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00002422 .arg1 = (uint32_t)memory_region->handle,
2423 .arg2 = (uint32_t)(memory_region->handle >> 32),
J-Alvesfdd29272022-07-19 13:16:31 +01002424 .arg3 = fragment_length,
2425 .arg4 = (uint32_t)(sender_to_ret & 0xffff) << 16};
Andrew Walbranca808b12020-05-15 17:22:28 +01002426 }
2427
2428out:
2429 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002430 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01002431 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002432}
2433
2434/**
J-Alves8505a8a2022-06-15 18:10:18 +01002435 * Continues an operation to donate, lend or share memory to a VM from current
2436 * world. If this is the last fragment then checks that the transition is valid
2437 * for the type of memory sending operation and updates the stage-2 page tables
2438 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01002439 *
2440 * Assumes that the caller has already found and locked the sender VM and copied
2441 * the memory region descriptor from the sender's TX buffer to a freshly
2442 * allocated page from Hafnium's internal pool.
2443 *
2444 * This function takes ownership of the `fragment` passed in; it must not be
2445 * freed by the caller.
2446 */
2447struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
2448 void *fragment,
2449 uint32_t fragment_length,
2450 ffa_memory_handle_t handle,
2451 struct mpool *page_pool)
2452{
2453 struct share_states_locked share_states = share_states_lock();
2454 struct ffa_memory_share_state *share_state;
2455 struct ffa_value ret;
2456 struct ffa_memory_region *memory_region;
2457
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05002458 CHECK(is_aligned(fragment,
2459 alignof(struct ffa_memory_region_constituent)));
2460 if (fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2461 0) {
2462 dlog_verbose("Fragment length %u misaligned.\n",
2463 fragment_length);
2464 ret = ffa_error(FFA_INVALID_PARAMETERS);
2465 goto out_free_fragment;
2466 }
2467
Andrew Walbranca808b12020-05-15 17:22:28 +01002468 ret = ffa_memory_send_continue_validate(share_states, handle,
2469 &share_state,
2470 from_locked.vm->id, page_pool);
2471 if (ret.func != FFA_SUCCESS_32) {
2472 goto out_free_fragment;
2473 }
2474 memory_region = share_state->memory_region;
2475
J-Alves95df0ef2022-12-07 10:09:48 +00002476 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002477 dlog_error(
2478 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01002479 "other world. This should never happen, and indicates "
2480 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01002481 "EL3 code.\n");
2482 ret = ffa_error(FFA_INVALID_PARAMETERS);
2483 goto out_free_fragment;
2484 }
2485
2486 /* Add this fragment. */
2487 share_state->fragments[share_state->fragment_count] = fragment;
2488 share_state->fragment_constituent_counts[share_state->fragment_count] =
2489 fragment_length / sizeof(struct ffa_memory_region_constituent);
2490 share_state->fragment_count++;
2491
2492 /* Check whether the memory send operation is now ready to complete. */
2493 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00002494 ret = ffa_memory_send_complete(
2495 from_locked, share_states, share_state, page_pool,
2496 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002497 } else {
2498 ret = (struct ffa_value){
2499 .func = FFA_MEM_FRAG_RX_32,
2500 .arg1 = (uint32_t)handle,
2501 .arg2 = (uint32_t)(handle >> 32),
2502 .arg3 = share_state_next_fragment_offset(share_states,
2503 share_state)};
2504 }
2505 goto out;
2506
2507out_free_fragment:
2508 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002509
2510out:
Andrew Walbranca808b12020-05-15 17:22:28 +01002511 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002512 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002513}
2514
Andrew Walbranca808b12020-05-15 17:22:28 +01002515/** Clean up after the receiver has finished retrieving a memory region. */
2516static void ffa_memory_retrieve_complete(
2517 struct share_states_locked share_states,
2518 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2519{
J-Alves95fbb312024-03-20 15:19:16 +00002520 if (share_state->share_func == FFA_MEM_DONATE_32 ||
2521 share_state->share_func == FFA_MEM_DONATE_64) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002522 /*
2523 * Memory that has been donated can't be relinquished,
2524 * so no need to keep the share state around.
2525 */
2526 share_state_free(share_states, share_state, page_pool);
2527 dlog_verbose("Freed share state for donate.\n");
2528 }
2529}
2530
J-Alves2d8457f2022-10-05 11:06:41 +01002531/**
2532 * Initialises the given memory region descriptor to be used for an
2533 * `FFA_MEM_RETRIEVE_RESP`, including the given constituents for the first
2534 * fragment.
2535 * The memory region descriptor is initialized according to retriever's
2536 * FF-A version.
2537 *
2538 * Returns true on success, or false if the given constituents won't all fit in
2539 * the first fragment.
2540 */
2541static bool ffa_retrieved_memory_region_init(
Karl Meakin0e617d92024-04-05 12:55:22 +01002542 void *response, enum ffa_version ffa_version, size_t response_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01002543 ffa_id_t sender, ffa_memory_attributes_t attributes,
J-Alves2d8457f2022-10-05 11:06:41 +01002544 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002545 ffa_memory_access_permissions_t permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002546 struct ffa_memory_access *receivers, size_t receiver_count,
2547 uint32_t memory_access_desc_size, uint32_t page_count,
2548 uint32_t total_constituent_count,
J-Alves2d8457f2022-10-05 11:06:41 +01002549 const struct ffa_memory_region_constituent constituents[],
2550 uint32_t fragment_constituent_count, uint32_t *total_length,
2551 uint32_t *fragment_length)
2552{
2553 struct ffa_composite_memory_region *composite_memory_region;
J-Alves2d8457f2022-10-05 11:06:41 +01002554 uint32_t i;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002555 uint32_t composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002556 uint32_t constituents_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002557
2558 assert(response != NULL);
2559
Karl Meakin0e617d92024-04-05 12:55:22 +01002560 if (ffa_version == FFA_VERSION_1_0) {
J-Alves2d8457f2022-10-05 11:06:41 +01002561 struct ffa_memory_region_v1_0 *retrieve_response =
2562 (struct ffa_memory_region_v1_0 *)response;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002563 struct ffa_memory_access_v1_0 *receiver;
J-Alves2d8457f2022-10-05 11:06:41 +01002564
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002565 ffa_memory_region_init_header_v1_0(retrieve_response, sender,
2566 attributes, flags, handle, 0,
2567 receiver_count);
J-Alves2d8457f2022-10-05 11:06:41 +01002568
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002569 receiver = (struct ffa_memory_access_v1_0 *)
2570 retrieve_response->receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002571 receiver_count = retrieve_response->receiver_count;
2572
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002573 for (uint32_t i = 0; i < receiver_count; i++) {
2574 ffa_id_t receiver_id =
2575 receivers[i].receiver_permissions.receiver;
2576 ffa_memory_receiver_flags_t recv_flags =
2577 receivers[i].receiver_permissions.flags;
2578
2579 /*
2580 * Initialized here as in memory retrieve responses we
2581 * currently expect one borrower to be specified.
2582 */
2583 ffa_memory_access_init_v1_0(
Karl Meakin84710f32023-10-12 15:14:49 +01002584 receiver, receiver_id, permissions.data_access,
2585 permissions.instruction_access, recv_flags);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002586 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002587
2588 composite_offset =
J-Alves2d8457f2022-10-05 11:06:41 +01002589 sizeof(struct ffa_memory_region_v1_0) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002590 receiver_count * sizeof(struct ffa_memory_access_v1_0);
2591 receiver->composite_memory_region_offset = composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002592
2593 composite_memory_region = ffa_memory_region_get_composite_v1_0(
2594 retrieve_response, 0);
2595 } else {
J-Alves2d8457f2022-10-05 11:06:41 +01002596 struct ffa_memory_region *retrieve_response =
2597 (struct ffa_memory_region *)response;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002598 struct ffa_memory_access *retrieve_response_receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002599
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002600 ffa_memory_region_init_header(
2601 retrieve_response, sender, attributes, flags, handle, 0,
2602 receiver_count, memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002603
2604 /*
2605 * Note that `sizeof(struct_ffa_memory_region)` and
2606 * `sizeof(struct ffa_memory_access)` must both be multiples of
2607 * 16 (as verified by the asserts in `ffa_memory.c`, so it is
2608 * guaranteed that the offset we calculate here is aligned to a
2609 * 64-bit boundary and so 64-bit values can be copied without
2610 * alignment faults.
2611 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002612 composite_offset =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01002613 retrieve_response->receivers_offset +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002614 (uint32_t)(receiver_count *
2615 retrieve_response->memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002616
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002617 retrieve_response_receivers =
2618 ffa_memory_region_get_receiver(retrieve_response, 0);
2619 assert(retrieve_response_receivers != NULL);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002620
2621 /*
2622 * Initialized here as in memory retrieve responses we currently
2623 * expect one borrower to be specified.
2624 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002625 memcpy_s(retrieve_response_receivers,
2626 sizeof(struct ffa_memory_access) * receiver_count,
2627 receivers,
2628 sizeof(struct ffa_memory_access) * receiver_count);
2629
2630 retrieve_response_receivers->composite_memory_region_offset =
2631 composite_offset;
2632
J-Alves2d8457f2022-10-05 11:06:41 +01002633 composite_memory_region =
2634 ffa_memory_region_get_composite(retrieve_response, 0);
2635 }
2636
J-Alves2d8457f2022-10-05 11:06:41 +01002637 assert(composite_memory_region != NULL);
2638
J-Alves2d8457f2022-10-05 11:06:41 +01002639 composite_memory_region->page_count = page_count;
2640 composite_memory_region->constituent_count = total_constituent_count;
2641 composite_memory_region->reserved_0 = 0;
2642
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002643 constituents_offset =
2644 composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alves2d8457f2022-10-05 11:06:41 +01002645 if (constituents_offset +
2646 fragment_constituent_count *
2647 sizeof(struct ffa_memory_region_constituent) >
2648 response_max_size) {
2649 return false;
2650 }
2651
2652 for (i = 0; i < fragment_constituent_count; ++i) {
2653 composite_memory_region->constituents[i] = constituents[i];
2654 }
2655
2656 if (total_length != NULL) {
2657 *total_length =
2658 constituents_offset +
2659 composite_memory_region->constituent_count *
2660 sizeof(struct ffa_memory_region_constituent);
2661 }
2662 if (fragment_length != NULL) {
2663 *fragment_length =
2664 constituents_offset +
2665 fragment_constituent_count *
2666 sizeof(struct ffa_memory_region_constituent);
2667 }
2668
2669 return true;
2670}
2671
J-Alves96de29f2022-04-26 16:05:24 +01002672/**
2673 * Validates the retrieved permissions against those specified by the lender
2674 * of memory share operation. Optionally can help set the permissions to be used
2675 * for the S2 mapping, through the `permissions` argument.
J-Alvesdcad8992023-09-15 14:10:35 +01002676 * Returns FFA_SUCCESS if all the fields are valid. FFA_ERROR, with error code:
2677 * - FFA_INVALID_PARAMETERS -> if the fields have invalid values as per the
2678 * specification for each ABI.
2679 * - FFA_DENIED -> if the permissions specified by the retriever are not
2680 * less permissive than those provided by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002681 */
J-Alvesdcad8992023-09-15 14:10:35 +01002682static struct ffa_value ffa_memory_retrieve_is_memory_access_valid(
2683 uint32_t share_func, enum ffa_data_access sent_data_access,
J-Alves96de29f2022-04-26 16:05:24 +01002684 enum ffa_data_access requested_data_access,
2685 enum ffa_instruction_access sent_instruction_access,
2686 enum ffa_instruction_access requested_instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002687 ffa_memory_access_permissions_t *permissions, bool multiple_borrowers)
J-Alves96de29f2022-04-26 16:05:24 +01002688{
2689 switch (sent_data_access) {
2690 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2691 case FFA_DATA_ACCESS_RW:
2692 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2693 requested_data_access == FFA_DATA_ACCESS_RW) {
2694 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002695 permissions->data_access = FFA_DATA_ACCESS_RW;
J-Alves96de29f2022-04-26 16:05:24 +01002696 }
2697 break;
2698 }
2699 /* Intentional fall-through. */
2700 case FFA_DATA_ACCESS_RO:
2701 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2702 requested_data_access == FFA_DATA_ACCESS_RO) {
2703 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002704 permissions->data_access = FFA_DATA_ACCESS_RO;
J-Alves96de29f2022-04-26 16:05:24 +01002705 }
2706 break;
2707 }
2708 dlog_verbose(
2709 "Invalid data access requested; sender specified "
2710 "permissions %#x but receiver requested %#x.\n",
2711 sent_data_access, requested_data_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002712 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002713 case FFA_DATA_ACCESS_RESERVED:
2714 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
2715 "checked before this point.");
2716 }
2717
J-Alvesdcad8992023-09-15 14:10:35 +01002718 /*
2719 * For operations with a single borrower, If it is an FFA_MEMORY_LEND
2720 * or FFA_MEMORY_DONATE the retriever should have specifed the
2721 * instruction permissions it wishes to receive.
2722 */
2723 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00002724 case FFA_MEM_SHARE_64:
J-Alvesdcad8992023-09-15 14:10:35 +01002725 case FFA_MEM_SHARE_32:
2726 if (requested_instruction_access !=
2727 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2728 dlog_verbose(
2729 "%s: for share instruction permissions must "
2730 "NOT be specified.\n",
2731 __func__);
2732 return ffa_error(FFA_INVALID_PARAMETERS);
2733 }
2734 break;
J-Alves95fbb312024-03-20 15:19:16 +00002735 case FFA_MEM_LEND_64:
J-Alvesdcad8992023-09-15 14:10:35 +01002736 case FFA_MEM_LEND_32:
2737 /*
2738 * For operations with multiple borrowers only permit XN
2739 * permissions, and both Sender and borrower should have used
2740 * FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED.
2741 */
2742 if (multiple_borrowers) {
2743 if (requested_instruction_access !=
2744 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2745 dlog_verbose(
2746 "%s: lend/share/donate with multiple "
2747 "borrowers "
2748 "instruction permissions must NOT be "
2749 "specified.\n",
2750 __func__);
2751 return ffa_error(FFA_INVALID_PARAMETERS);
2752 }
2753 break;
2754 }
2755 /* Fall through if the operation targets a single borrower. */
J-Alves95fbb312024-03-20 15:19:16 +00002756 case FFA_MEM_DONATE_64:
J-Alvesdcad8992023-09-15 14:10:35 +01002757 case FFA_MEM_DONATE_32:
2758 if (!multiple_borrowers &&
2759 requested_instruction_access ==
2760 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2761 dlog_verbose(
2762 "%s: for lend/donate with single borrower "
2763 "instruction permissions must be speficified "
2764 "by borrower\n",
2765 __func__);
2766 return ffa_error(FFA_INVALID_PARAMETERS);
2767 }
2768 break;
2769 default:
2770 panic("%s: Wrong func id provided.\n", __func__);
2771 }
2772
J-Alves96de29f2022-04-26 16:05:24 +01002773 switch (sent_instruction_access) {
2774 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2775 case FFA_INSTRUCTION_ACCESS_X:
J-Alvesdcad8992023-09-15 14:10:35 +01002776 if (requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
J-Alves96de29f2022-04-26 16:05:24 +01002777 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002778 permissions->instruction_access =
2779 FFA_INSTRUCTION_ACCESS_X;
J-Alves96de29f2022-04-26 16:05:24 +01002780 }
2781 break;
2782 }
J-Alvesdcad8992023-09-15 14:10:35 +01002783 /*
2784 * Fall through if requested permissions are less
2785 * permissive than those provided by the sender.
2786 */
J-Alves96de29f2022-04-26 16:05:24 +01002787 case FFA_INSTRUCTION_ACCESS_NX:
2788 if (requested_instruction_access ==
2789 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2790 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2791 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002792 permissions->instruction_access =
2793 FFA_INSTRUCTION_ACCESS_NX;
J-Alves96de29f2022-04-26 16:05:24 +01002794 }
2795 break;
2796 }
2797 dlog_verbose(
2798 "Invalid instruction access requested; sender "
2799 "specified permissions %#x but receiver requested "
2800 "%#x.\n",
2801 sent_instruction_access, requested_instruction_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002802 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002803 case FFA_INSTRUCTION_ACCESS_RESERVED:
2804 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
2805 "be checked before this point.");
2806 }
2807
J-Alvesdcad8992023-09-15 14:10:35 +01002808 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves96de29f2022-04-26 16:05:24 +01002809}
2810
2811/**
2812 * Validate the receivers' permissions in the retrieve request against those
2813 * specified by the lender.
2814 * In the `permissions` argument returns the permissions to set at S2 for the
2815 * caller to the FFA_MEMORY_RETRIEVE_REQ.
J-Alves3456e032023-07-20 12:20:05 +01002816 * The function looks into the flag to bypass multiple borrower checks:
2817 * - If not set returns FFA_SUCCESS if all specified permissions are valid.
2818 * - If set returns FFA_SUCCESS if the descriptor contains the permissions
2819 * to the caller of FFA_MEM_RETRIEVE_REQ and they are valid. Other permissions
2820 * are ignored, if provided.
J-Alves96de29f2022-04-26 16:05:24 +01002821 */
2822static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
2823 struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01002824 struct ffa_memory_region *retrieve_request, ffa_id_t to_vm_id,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002825 ffa_memory_access_permissions_t *permissions,
2826 struct ffa_memory_access **receiver_ret, uint32_t func_id)
J-Alves96de29f2022-04-26 16:05:24 +01002827{
2828 uint32_t retrieve_receiver_index;
J-Alves3456e032023-07-20 12:20:05 +01002829 bool bypass_multi_receiver_check =
2830 (retrieve_request->flags &
2831 FFA_MEMORY_REGION_FLAG_BYPASS_BORROWERS_CHECK) != 0U;
J-Alvesdcad8992023-09-15 14:10:35 +01002832 const uint32_t region_receiver_count = memory_region->receiver_count;
2833 struct ffa_value ret;
J-Alves96de29f2022-04-26 16:05:24 +01002834
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002835 assert(receiver_ret != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002836 assert(permissions != NULL);
2837
Karl Meakin84710f32023-10-12 15:14:49 +01002838 *permissions = (ffa_memory_access_permissions_t){0};
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002839
J-Alves3456e032023-07-20 12:20:05 +01002840 if (!bypass_multi_receiver_check) {
J-Alvesdcad8992023-09-15 14:10:35 +01002841 if (retrieve_request->receiver_count != region_receiver_count) {
J-Alves3456e032023-07-20 12:20:05 +01002842 dlog_verbose(
2843 "Retrieve request should contain same list of "
2844 "borrowers, as specified by the lender.\n");
2845 return ffa_error(FFA_INVALID_PARAMETERS);
2846 }
2847 } else {
2848 if (retrieve_request->receiver_count != 1) {
2849 dlog_verbose(
2850 "Set bypass multiple borrower check, receiver "
2851 "list must be sized 1 (%x)\n",
2852 memory_region->receiver_count);
2853 return ffa_error(FFA_INVALID_PARAMETERS);
2854 }
J-Alves96de29f2022-04-26 16:05:24 +01002855 }
2856
2857 retrieve_receiver_index = retrieve_request->receiver_count;
2858
J-Alves96de29f2022-04-26 16:05:24 +01002859 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
2860 ffa_memory_access_permissions_t sent_permissions;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002861 struct ffa_memory_access *retrieve_request_receiver =
2862 ffa_memory_region_get_receiver(retrieve_request, i);
2863 assert(retrieve_request_receiver != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002864 ffa_memory_access_permissions_t requested_permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002865 retrieve_request_receiver->receiver_permissions
2866 .permissions;
J-Alves19e20cf2023-08-02 12:48:55 +01002867 ffa_id_t current_receiver_id =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002868 retrieve_request_receiver->receiver_permissions
2869 .receiver;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002870 struct ffa_memory_access *receiver;
2871 uint32_t mem_region_receiver_index;
2872 bool permissions_RO;
2873 bool clear_memory_flags;
J-Alvesf220d572024-04-24 22:15:14 +01002874 /*
2875 * If the call is at the virtual FF-A instance the caller's
2876 * ID must match an entry in the memory access list.
2877 * In the SPMC, one of the specified receivers could be from
2878 * the NWd.
2879 */
2880 bool found_to_id = vm_id_is_current_world(to_vm_id)
2881 ? (current_receiver_id == to_vm_id)
2882 : (!vm_id_is_current_world(
2883 current_receiver_id));
J-Alves96de29f2022-04-26 16:05:24 +01002884
J-Alves3456e032023-07-20 12:20:05 +01002885 if (bypass_multi_receiver_check && !found_to_id) {
2886 dlog_verbose(
2887 "Bypass multiple borrower check for id %x.\n",
2888 current_receiver_id);
2889 continue;
2890 }
2891
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002892 if (retrieve_request_receiver->composite_memory_region_offset !=
2893 0U) {
2894 dlog_verbose(
2895 "Retriever specified address ranges not "
2896 "supported (got offset %d).\n",
2897 retrieve_request_receiver
2898 ->composite_memory_region_offset);
2899 return ffa_error(FFA_INVALID_PARAMETERS);
2900 }
2901
J-Alves96de29f2022-04-26 16:05:24 +01002902 /*
2903 * Find the current receiver in the transaction descriptor from
2904 * sender.
2905 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002906 mem_region_receiver_index =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002907 ffa_memory_region_get_receiver_index(
2908 memory_region, current_receiver_id);
J-Alves96de29f2022-04-26 16:05:24 +01002909
2910 if (mem_region_receiver_index ==
2911 memory_region->receiver_count) {
2912 dlog_verbose("%s: receiver %x not found\n", __func__,
2913 current_receiver_id);
2914 return ffa_error(FFA_DENIED);
2915 }
2916
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002917 receiver = ffa_memory_region_get_receiver(
2918 memory_region, mem_region_receiver_index);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002919 assert(receiver != NULL);
2920
2921 sent_permissions = receiver->receiver_permissions.permissions;
J-Alves96de29f2022-04-26 16:05:24 +01002922
2923 if (found_to_id) {
2924 retrieve_receiver_index = i;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002925
2926 *receiver_ret = receiver;
J-Alves96de29f2022-04-26 16:05:24 +01002927 }
2928
2929 /*
J-Alvesdcad8992023-09-15 14:10:35 +01002930 * Check if retrieve request memory access list is valid:
2931 * - The retrieve request complies with the specification.
2932 * - Permissions are within those specified by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002933 */
J-Alvesdcad8992023-09-15 14:10:35 +01002934 ret = ffa_memory_retrieve_is_memory_access_valid(
Karl Meakin84710f32023-10-12 15:14:49 +01002935 func_id, sent_permissions.data_access,
2936 requested_permissions.data_access,
2937 sent_permissions.instruction_access,
2938 requested_permissions.instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002939 found_to_id ? permissions : NULL,
2940 region_receiver_count > 1);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002941
J-Alvesdcad8992023-09-15 14:10:35 +01002942 if (ret.func != FFA_SUCCESS_32) {
2943 return ret;
J-Alves96de29f2022-04-26 16:05:24 +01002944 }
2945
Karl Meakin84710f32023-10-12 15:14:49 +01002946 permissions_RO =
2947 (permissions->data_access == FFA_DATA_ACCESS_RO);
J-Alvese5262372024-03-27 11:02:03 +00002948 clear_memory_flags =
2949 (retrieve_request->flags &
2950 (FFA_MEMORY_REGION_FLAG_CLEAR |
2951 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002952
J-Alves96de29f2022-04-26 16:05:24 +01002953 /*
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002954 * Can't request PM to clear memory if only provided
2955 * with RO permissions.
J-Alves96de29f2022-04-26 16:05:24 +01002956 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002957 if (found_to_id && permissions_RO && clear_memory_flags) {
J-Alves96de29f2022-04-26 16:05:24 +01002958 dlog_verbose(
2959 "Receiver has RO permissions can not request "
2960 "clear.\n");
2961 return ffa_error(FFA_DENIED);
2962 }
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002963
2964 /*
2965 * Check the impdef in the retrieve_request matches the value in
2966 * the original memory send.
2967 */
2968 if (ffa_version_from_memory_access_desc_size(
2969 memory_region->memory_access_desc_size) >=
Karl Meakin0e617d92024-04-05 12:55:22 +01002970 FFA_VERSION_1_2 &&
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002971 ffa_version_from_memory_access_desc_size(
2972 retrieve_request->memory_access_desc_size) >=
Karl Meakin0e617d92024-04-05 12:55:22 +01002973 FFA_VERSION_1_2) {
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002974 if (receiver->impdef.val[0] !=
2975 retrieve_request_receiver->impdef.val[0] ||
2976 receiver->impdef.val[1] !=
2977 retrieve_request_receiver->impdef.val[1]) {
2978 dlog_verbose(
2979 "Impdef value in memory send does not "
J-Alves0a824e92024-04-26 16:20:12 +01002980 "match retrieve request value send "
2981 "value %#lx %#lx retrieve request "
Karl Meakine8937d92024-03-19 16:04:25 +00002982 "value %#lx %#lx\n",
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002983 receiver->impdef.val[0],
2984 receiver->impdef.val[1],
2985 retrieve_request_receiver->impdef
2986 .val[0],
2987 retrieve_request_receiver->impdef
2988 .val[1]);
2989 return ffa_error(FFA_INVALID_PARAMETERS);
2990 }
2991 }
J-Alves96de29f2022-04-26 16:05:24 +01002992 }
2993
2994 if (retrieve_receiver_index == retrieve_request->receiver_count) {
2995 dlog_verbose(
2996 "Retrieve request does not contain caller's (%x) "
2997 "permissions\n",
2998 to_vm_id);
2999 return ffa_error(FFA_INVALID_PARAMETERS);
3000 }
3001
3002 return (struct ffa_value){.func = FFA_SUCCESS_32};
3003}
3004
J-Alvesa9cd7e32022-07-01 13:49:33 +01003005/*
3006 * According to section 16.4.3 of FF-A v1.1 EAC0 specification, the hypervisor
3007 * may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region description
3008 * of a pending memory sharing operation whose allocator is the SPM, for
3009 * validation purposes before forwarding an FFA_MEM_RECLAIM call. In doing so
3010 * the memory region descriptor of the retrieve request must be zeroed with the
3011 * exception of the sender ID and handle.
3012 */
J-Alves4f0d9c12024-01-17 17:23:11 +00003013bool is_ffa_hypervisor_retrieve_request(struct ffa_memory_region *request,
3014 struct vm_locked to_locked)
J-Alvesa9cd7e32022-07-01 13:49:33 +01003015{
3016 return to_locked.vm->id == HF_HYPERVISOR_VM_ID &&
Karl Meakin84710f32023-10-12 15:14:49 +01003017 request->attributes.shareability == 0U &&
3018 request->attributes.cacheability == 0U &&
3019 request->attributes.type == 0U &&
3020 request->attributes.security == 0U && request->flags == 0U &&
J-Alvesa9cd7e32022-07-01 13:49:33 +01003021 request->tag == 0U && request->receiver_count == 0U &&
3022 plat_ffa_memory_handle_allocated_by_current_world(
3023 request->handle);
3024}
3025
3026/*
3027 * Helper to reset count of fragments retrieved by the hypervisor.
3028 */
3029static void ffa_memory_retrieve_complete_from_hyp(
3030 struct ffa_memory_share_state *share_state)
3031{
3032 if (share_state->hypervisor_fragment_count ==
3033 share_state->fragment_count) {
3034 share_state->hypervisor_fragment_count = 0;
3035 }
3036}
3037
J-Alves089004f2022-07-13 14:25:44 +01003038/**
J-Alves4f0d9c12024-01-17 17:23:11 +00003039 * Prepares the return of the ffa_value for the memory retrieve response.
3040 */
3041static struct ffa_value ffa_memory_retrieve_resp(uint32_t total_length,
3042 uint32_t fragment_length)
3043{
3044 return (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
3045 .arg1 = total_length,
3046 .arg2 = fragment_length};
3047}
3048
3049/**
J-Alves089004f2022-07-13 14:25:44 +01003050 * Validate that the memory region descriptor provided by the borrower on
3051 * FFA_MEM_RETRIEVE_REQ, against saved memory region provided by lender at the
3052 * memory sharing call.
3053 */
3054static struct ffa_value ffa_memory_retrieve_validate(
J-Alves4f0d9c12024-01-17 17:23:11 +00003055 ffa_id_t to_id, struct ffa_memory_region *retrieve_request,
3056 uint32_t retrieve_request_length,
J-Alves089004f2022-07-13 14:25:44 +01003057 struct ffa_memory_region *memory_region, uint32_t *receiver_index,
3058 uint32_t share_func)
3059{
3060 ffa_memory_region_flags_t transaction_type =
3061 retrieve_request->flags &
3062 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003063 enum ffa_memory_security security_state;
J-Alves4f0d9c12024-01-17 17:23:11 +00003064 const uint64_t memory_access_desc_size =
3065 retrieve_request->memory_access_desc_size;
3066 const uint32_t expected_retrieve_request_length =
3067 retrieve_request->receivers_offset +
3068 (uint32_t)(retrieve_request->receiver_count *
3069 memory_access_desc_size);
J-Alves089004f2022-07-13 14:25:44 +01003070
3071 assert(retrieve_request != NULL);
3072 assert(memory_region != NULL);
3073 assert(receiver_index != NULL);
J-Alves089004f2022-07-13 14:25:44 +01003074
J-Alves4f0d9c12024-01-17 17:23:11 +00003075 if (retrieve_request_length != expected_retrieve_request_length) {
3076 dlog_verbose(
3077 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
3078 "but was %d.\n",
3079 expected_retrieve_request_length,
3080 retrieve_request_length);
3081 return ffa_error(FFA_INVALID_PARAMETERS);
3082 }
3083
3084 if (retrieve_request->sender != memory_region->sender) {
3085 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003086 "Memory with handle %#lx not fully sent, can't "
J-Alves4f0d9c12024-01-17 17:23:11 +00003087 "retrieve.\n",
3088 memory_region->handle);
3089 return ffa_error(FFA_DENIED);
3090 }
3091
3092 /*
3093 * The SPMC can only process retrieve requests to memory share
3094 * operations with one borrower from the other world. It can't
3095 * determine the ID of the NWd VM that invoked the retrieve
3096 * request interface call. It relies on the hypervisor to
3097 * validate the caller's ID against that provided in the
3098 * `receivers` list of the retrieve response.
3099 * In case there is only one borrower from the NWd in the
3100 * transaction descriptor, record that in the `receiver_id` for
3101 * later use, and validate in the retrieve request message.
3102 * This limitation is due to the fact SPMC can't determine the
3103 * index in the memory share structures state to update.
3104 */
3105 if (to_id == HF_HYPERVISOR_VM_ID) {
3106 uint32_t other_world_count = 0;
3107
3108 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3109 struct ffa_memory_access *receiver =
3110 ffa_memory_region_get_receiver(retrieve_request,
J-Alvesf220d572024-04-24 22:15:14 +01003111 i);
J-Alves4f0d9c12024-01-17 17:23:11 +00003112 assert(receiver != NULL);
3113
J-Alvesf220d572024-04-24 22:15:14 +01003114 if (!vm_id_is_current_world(
3115 receiver->receiver_permissions.receiver)) {
J-Alves4f0d9c12024-01-17 17:23:11 +00003116 other_world_count++;
J-Alvesf220d572024-04-24 22:15:14 +01003117 /* Set it to be used later. */
3118 to_id = receiver->receiver_permissions.receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003119 }
3120 }
3121
3122 if (other_world_count > 1) {
3123 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003124 "Support one receiver from the other world.\n");
J-Alves4f0d9c12024-01-17 17:23:11 +00003125 return ffa_error(FFA_NOT_SUPPORTED);
3126 }
3127 }
J-Alves089004f2022-07-13 14:25:44 +01003128 /*
3129 * Check that the transaction type expected by the receiver is
3130 * correct, if it has been specified.
3131 */
3132 if (transaction_type !=
3133 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
3134 transaction_type != (memory_region->flags &
3135 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
3136 dlog_verbose(
3137 "Incorrect transaction type %#x for "
Karl Meakine8937d92024-03-19 16:04:25 +00003138 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#lx.\n",
J-Alves089004f2022-07-13 14:25:44 +01003139 transaction_type,
3140 memory_region->flags &
3141 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
3142 retrieve_request->handle);
3143 return ffa_error(FFA_INVALID_PARAMETERS);
3144 }
3145
3146 if (retrieve_request->tag != memory_region->tag) {
3147 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003148 "Incorrect tag %lu for FFA_MEM_RETRIEVE_REQ, expected "
3149 "%lu for handle %#lx.\n",
J-Alves089004f2022-07-13 14:25:44 +01003150 retrieve_request->tag, memory_region->tag,
3151 retrieve_request->handle);
3152 return ffa_error(FFA_INVALID_PARAMETERS);
3153 }
3154
J-Alves4f0d9c12024-01-17 17:23:11 +00003155 *receiver_index =
3156 ffa_memory_region_get_receiver_index(memory_region, to_id);
J-Alves089004f2022-07-13 14:25:44 +01003157
3158 if (*receiver_index == memory_region->receiver_count) {
3159 dlog_verbose(
3160 "Incorrect receiver VM ID %d for "
Karl Meakine8937d92024-03-19 16:04:25 +00003161 "FFA_MEM_RETRIEVE_REQ, for handle %#lx.\n",
J-Alves4f0d9c12024-01-17 17:23:11 +00003162 to_id, memory_region->handle);
J-Alves089004f2022-07-13 14:25:44 +01003163 return ffa_error(FFA_INVALID_PARAMETERS);
3164 }
3165
3166 if ((retrieve_request->flags &
3167 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
3168 dlog_verbose(
3169 "Retriever specified 'address range alignment 'hint' "
3170 "not supported.\n");
3171 return ffa_error(FFA_INVALID_PARAMETERS);
3172 }
3173 if ((retrieve_request->flags &
3174 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
3175 dlog_verbose(
3176 "Bits 8-5 must be zero in memory region's flags "
3177 "(address range alignment hint not supported).\n");
3178 return ffa_error(FFA_INVALID_PARAMETERS);
3179 }
3180
3181 if ((retrieve_request->flags & ~0x7FF) != 0U) {
3182 dlog_verbose(
3183 "Bits 31-10 must be zero in memory region's flags.\n");
3184 return ffa_error(FFA_INVALID_PARAMETERS);
3185 }
3186
J-Alves95fbb312024-03-20 15:19:16 +00003187 if ((share_func == FFA_MEM_SHARE_32 ||
3188 share_func == FFA_MEM_SHARE_64) &&
J-Alves089004f2022-07-13 14:25:44 +01003189 (retrieve_request->flags &
3190 (FFA_MEMORY_REGION_FLAG_CLEAR |
3191 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
3192 dlog_verbose(
3193 "Memory Share operation can't clean after relinquish "
3194 "memory region.\n");
3195 return ffa_error(FFA_INVALID_PARAMETERS);
3196 }
3197
3198 /*
3199 * If the borrower needs the memory to be cleared before mapping
3200 * to its address space, the sender should have set the flag
3201 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
3202 * FFA_DENIED.
3203 */
3204 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
3205 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
3206 dlog_verbose(
3207 "Borrower needs memory cleared. Sender needs to set "
3208 "flag for clearing memory.\n");
3209 return ffa_error(FFA_DENIED);
3210 }
3211
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003212 /* Memory region attributes NS-Bit MBZ for FFA_MEM_RETRIEVE_REQ. */
Karl Meakin84710f32023-10-12 15:14:49 +01003213 security_state = retrieve_request->attributes.security;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003214 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
3215 dlog_verbose(
3216 "Invalid security state for memory retrieve request "
3217 "operation.\n");
3218 return ffa_error(FFA_INVALID_PARAMETERS);
3219 }
3220
J-Alves089004f2022-07-13 14:25:44 +01003221 /*
3222 * If memory type is not specified, bypass validation of memory
3223 * attributes in the retrieve request. The retriever is expecting to
3224 * obtain this information from the SPMC.
3225 */
Karl Meakin84710f32023-10-12 15:14:49 +01003226 if (retrieve_request->attributes.type == FFA_MEMORY_NOT_SPECIFIED_MEM) {
J-Alves089004f2022-07-13 14:25:44 +01003227 return (struct ffa_value){.func = FFA_SUCCESS_32};
3228 }
3229
3230 /*
3231 * Ensure receiver's attributes are compatible with how
3232 * Hafnium maps memory: Normal Memory, Inner shareable,
3233 * Write-Back Read-Allocate Write-Allocate Cacheable.
3234 */
3235 return ffa_memory_attributes_validate(retrieve_request->attributes);
3236}
3237
J-Alves3f6527c2024-04-25 17:10:57 +01003238/**
3239 * Whilst processing the retrieve request, the operation could be aborted, and
3240 * changes to page tables and the share state structures need to be reverted.
3241 */
3242static void ffa_partition_memory_retrieve_request_undo(
3243 struct vm_locked from_locked,
3244 struct ffa_memory_share_state *share_state, uint32_t receiver_index)
3245{
3246 /*
3247 * Currently this operation is expected for operations involving the
3248 * 'other_world' vm.
3249 */
3250 assert(from_locked.vm->id == HF_OTHER_WORLD_ID);
3251 assert(share_state->retrieved_fragment_count[receiver_index] > 0);
3252
3253 /* Decrement the retrieved fragment count for the given receiver. */
3254 share_state->retrieved_fragment_count[receiver_index]--;
3255}
3256
3257/**
3258 * Whilst processing an hypervisor retrieve request the operation could be
3259 * aborted. There were no updates to PTs in this case, so decrementing the
3260 * fragment count retrieved by the hypervisor should be enough.
3261 */
3262static void ffa_hypervisor_memory_retrieve_request_undo(
3263 struct ffa_memory_share_state *share_state)
3264{
3265 assert(share_state->hypervisor_fragment_count > 0);
3266 share_state->hypervisor_fragment_count--;
3267}
3268
J-Alves4f0d9c12024-01-17 17:23:11 +00003269static struct ffa_value ffa_partition_retrieve_request(
3270 struct share_states_locked share_states,
3271 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3272 struct ffa_memory_region *retrieve_request,
3273 uint32_t retrieve_request_length, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003274{
Karl Meakin84710f32023-10-12 15:14:49 +01003275 ffa_memory_access_permissions_t permissions = {0};
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003276 uint32_t memory_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003277 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01003278 struct ffa_composite_memory_region *composite;
3279 uint32_t total_length;
3280 uint32_t fragment_length;
J-Alves19e20cf2023-08-02 12:48:55 +01003281 ffa_id_t receiver_id = to_locked.vm->id;
J-Alves4f0d9c12024-01-17 17:23:11 +00003282 bool is_retrieve_complete = false;
J-Alves4f0d9c12024-01-17 17:23:11 +00003283 const uint64_t memory_access_desc_size =
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003284 retrieve_request->memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003285 uint32_t receiver_index;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003286 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003287 ffa_memory_handle_t handle = retrieve_request->handle;
Karl Meakin84710f32023-10-12 15:14:49 +01003288 ffa_memory_attributes_t attributes = {0};
J-Alves460d36c2023-10-12 17:02:15 +01003289 uint32_t retrieve_mode = 0;
J-Alves4f0d9c12024-01-17 17:23:11 +00003290 struct ffa_memory_region *memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003291
J-Alves96de29f2022-04-26 16:05:24 +01003292 if (!share_state->sending_complete) {
3293 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003294 "Memory with handle %#lx not fully sent, can't "
J-Alves96de29f2022-04-26 16:05:24 +01003295 "retrieve.\n",
3296 handle);
J-Alves4f0d9c12024-01-17 17:23:11 +00003297 return ffa_error(FFA_INVALID_PARAMETERS);
J-Alves96de29f2022-04-26 16:05:24 +01003298 }
3299
J-Alves4f0d9c12024-01-17 17:23:11 +00003300 /*
3301 * Validate retrieve request, according to what was sent by the
3302 * sender. Function will output the `receiver_index` from the
3303 * provided memory region.
3304 */
3305 ret = ffa_memory_retrieve_validate(
3306 receiver_id, retrieve_request, retrieve_request_length,
3307 memory_region, &receiver_index, share_state->share_func);
J-Alves089004f2022-07-13 14:25:44 +01003308
J-Alves4f0d9c12024-01-17 17:23:11 +00003309 if (ret.func != FFA_SUCCESS_32) {
3310 return ret;
J-Alves089004f2022-07-13 14:25:44 +01003311 }
J-Alves96de29f2022-04-26 16:05:24 +01003312
J-Alves4f0d9c12024-01-17 17:23:11 +00003313 /*
3314 * Validate the requested permissions against the sent
3315 * permissions.
3316 * Outputs the permissions to give to retriever at S2
3317 * PTs.
3318 */
3319 ret = ffa_memory_retrieve_validate_memory_access_list(
3320 memory_region, retrieve_request, receiver_id, &permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003321 &receiver, share_state->share_func);
J-Alves4f0d9c12024-01-17 17:23:11 +00003322 if (ret.func != FFA_SUCCESS_32) {
3323 return ret;
3324 }
3325
3326 memory_to_mode = ffa_memory_permissions_to_mode(
3327 permissions, share_state->sender_orig_mode);
3328
Daniel Boulby6e261362024-06-13 16:53:00 +01003329 /*
3330 * Check requested memory type is valid with the memory type of the
3331 * owner. E.g. they follow the memory type precedence where Normal
3332 * memory is more permissive than device and therefore device memory
3333 * can only be shared as device memory.
3334 */
3335 if (retrieve_request->attributes.type == FFA_MEMORY_NORMAL_MEM &&
3336 ((share_state->sender_orig_mode & MM_MODE_D) != 0U ||
3337 memory_region->attributes.type == FFA_MEMORY_DEVICE_MEM)) {
3338 dlog_verbose(
3339 "Retrieving device memory as Normal memory is not "
3340 "allowed\n");
3341 return ffa_error(FFA_DENIED);
3342 }
3343
J-Alves4f0d9c12024-01-17 17:23:11 +00003344 ret = ffa_retrieve_check_update(
3345 to_locked, share_state->fragments,
3346 share_state->fragment_constituent_counts,
3347 share_state->fragment_count, memory_to_mode,
J-Alves460d36c2023-10-12 17:02:15 +01003348 share_state->share_func, false, page_pool, &retrieve_mode,
3349 share_state->memory_protected);
J-Alves4f0d9c12024-01-17 17:23:11 +00003350
3351 if (ret.func != FFA_SUCCESS_32) {
3352 return ret;
3353 }
3354
3355 share_state->retrieved_fragment_count[receiver_index] = 1;
3356
3357 is_retrieve_complete =
3358 share_state->retrieved_fragment_count[receiver_index] ==
3359 share_state->fragment_count;
3360
J-Alvesb5084cf2022-07-06 14:20:12 +01003361 /* VMs acquire the RX buffer from SPMC. */
3362 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3363
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003364 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003365 * Copy response to RX buffer of caller and deliver the message.
3366 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003367 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003368 composite = ffa_memory_region_get_composite(memory_region, 0);
J-Alves4f0d9c12024-01-17 17:23:11 +00003369
Andrew Walbranca808b12020-05-15 17:22:28 +01003370 /*
J-Alves460d36c2023-10-12 17:02:15 +01003371 * Set the security state in the memory retrieve response attributes
3372 * if specified by the target mode.
3373 */
3374 attributes = plat_ffa_memory_security_mode(memory_region->attributes,
3375 retrieve_mode);
3376
3377 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003378 * Constituents which we received in the first fragment should
3379 * always fit in the first fragment we are sending, because the
3380 * header is the same size in both cases and we have a fixed
3381 * message buffer size. So `ffa_retrieved_memory_region_init`
3382 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01003383 */
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003384
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003385 /* Provide the permissions that had been provided. */
3386 receiver->receiver_permissions.permissions = permissions;
3387
3388 /*
3389 * Prepare the memory region descriptor for the retrieve response.
3390 * Provide the pointer to the receiver tracked in the share state
J-Alves7b9cc432024-04-04 10:57:17 +01003391 * structures.
3392 * At this point the retrieve request descriptor from the partition
3393 * has been processed. The `retrieve_request` is expected to be in
3394 * a region that is handled by the SPMC/Hyp. Reuse the same buffer to
3395 * prepare the retrieve response before copying it to the RX buffer of
3396 * the caller.
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003397 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003398 CHECK(ffa_retrieved_memory_region_init(
J-Alves7b9cc432024-04-04 10:57:17 +01003399 retrieve_request, to_locked.vm->ffa_version, HF_MAILBOX_SIZE,
3400 memory_region->sender, attributes, memory_region->flags, handle,
3401 permissions, receiver, 1, memory_access_desc_size,
3402 composite->page_count, composite->constituent_count,
3403 share_state->fragments[0],
Andrew Walbranca808b12020-05-15 17:22:28 +01003404 share_state->fragment_constituent_counts[0], &total_length,
3405 &fragment_length));
J-Alvesb5084cf2022-07-06 14:20:12 +01003406
J-Alves7b9cc432024-04-04 10:57:17 +01003407 /*
3408 * Copy the message from the buffer into the partition's mailbox.
3409 * The operation might fail unexpectedly due to change in PAS address
3410 * space, or improper values to the sizes of the structures.
3411 */
3412 if (!memcpy_trapped(to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3413 retrieve_request, fragment_length)) {
3414 dlog_error(
3415 "%s: aborted the copy of response to RX buffer of "
3416 "%x.\n",
3417 __func__, to_locked.vm->id);
J-Alves3f6527c2024-04-25 17:10:57 +01003418
3419 ffa_partition_memory_retrieve_request_undo(
3420 to_locked, share_state, receiver_index);
3421
J-Alves7b9cc432024-04-04 10:57:17 +01003422 return ffa_error(FFA_ABORTED);
3423 }
3424
J-Alves4f0d9c12024-01-17 17:23:11 +00003425 if (is_retrieve_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003426 ffa_memory_retrieve_complete(share_states, share_state,
3427 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003428 }
J-Alves4f0d9c12024-01-17 17:23:11 +00003429
3430 return ffa_memory_retrieve_resp(total_length, fragment_length);
3431}
3432
3433static struct ffa_value ffa_hypervisor_retrieve_request(
3434 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3435 struct ffa_memory_region *retrieve_request)
3436{
3437 struct ffa_value ret;
3438 struct ffa_composite_memory_region *composite;
3439 uint32_t total_length;
3440 uint32_t fragment_length;
J-Alves4f0d9c12024-01-17 17:23:11 +00003441 ffa_memory_attributes_t attributes;
J-Alves7b6ab612024-01-24 09:54:54 +00003442 uint64_t memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003443 struct ffa_memory_region *memory_region;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003444 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003445 ffa_memory_handle_t handle = retrieve_request->handle;
3446
J-Alves4f0d9c12024-01-17 17:23:11 +00003447 memory_region = share_state->memory_region;
3448
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003449 assert(to_locked.vm->id == HF_HYPERVISOR_VM_ID);
3450
J-Alves7b6ab612024-01-24 09:54:54 +00003451 switch (to_locked.vm->ffa_version) {
Karl Meakin0e617d92024-04-05 12:55:22 +01003452 case FFA_VERSION_1_2:
J-Alves7b6ab612024-01-24 09:54:54 +00003453 memory_access_desc_size = sizeof(struct ffa_memory_access);
3454 break;
Karl Meakin0e617d92024-04-05 12:55:22 +01003455 case FFA_VERSION_1_0:
3456 case FFA_VERSION_1_1:
J-Alves7b6ab612024-01-24 09:54:54 +00003457 memory_access_desc_size = sizeof(struct ffa_memory_access_v1_0);
3458 break;
3459 default:
3460 panic("version not supported: %x\n", to_locked.vm->ffa_version);
3461 }
3462
J-Alves4f0d9c12024-01-17 17:23:11 +00003463 if (share_state->hypervisor_fragment_count != 0U) {
3464 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003465 "Memory with handle %#lx already retrieved by "
J-Alves4f0d9c12024-01-17 17:23:11 +00003466 "the hypervisor.\n",
3467 handle);
3468 return ffa_error(FFA_DENIED);
3469 }
3470
3471 share_state->hypervisor_fragment_count = 1;
3472
J-Alves4f0d9c12024-01-17 17:23:11 +00003473 /* VMs acquire the RX buffer from SPMC. */
3474 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3475
3476 /*
3477 * Copy response to RX buffer of caller and deliver the message.
3478 * This must be done before the share_state is (possibly) freed.
3479 */
3480 composite = ffa_memory_region_get_composite(memory_region, 0);
3481
3482 /*
3483 * Constituents which we received in the first fragment should
3484 * always fit in the first fragment we are sending, because the
3485 * header is the same size in both cases and we have a fixed
3486 * message buffer size. So `ffa_retrieved_memory_region_init`
3487 * should never fail.
3488 */
3489
3490 /*
3491 * Set the security state in the memory retrieve response attributes
3492 * if specified by the target mode.
3493 */
3494 attributes = plat_ffa_memory_security_mode(
3495 memory_region->attributes, share_state->sender_orig_mode);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003496
3497 receiver = ffa_memory_region_get_receiver(memory_region, 0);
3498
J-Alves7b9cc432024-04-04 10:57:17 +01003499 /*
3500 * At this point the `retrieve_request` is expected to be in a section
3501 * managed by the hypervisor.
3502 */
J-Alves4f0d9c12024-01-17 17:23:11 +00003503 CHECK(ffa_retrieved_memory_region_init(
J-Alves7b9cc432024-04-04 10:57:17 +01003504 retrieve_request, to_locked.vm->ffa_version, HF_MAILBOX_SIZE,
3505 memory_region->sender, attributes, memory_region->flags, handle,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003506 receiver->receiver_permissions.permissions, receiver,
3507 memory_region->receiver_count, memory_access_desc_size,
J-Alves4f0d9c12024-01-17 17:23:11 +00003508 composite->page_count, composite->constituent_count,
3509 share_state->fragments[0],
3510 share_state->fragment_constituent_counts[0], &total_length,
3511 &fragment_length));
3512
J-Alves7b9cc432024-04-04 10:57:17 +01003513 /*
3514 * Copy the message from the buffer into the hypervisor's mailbox.
3515 * The operation might fail unexpectedly due to change in PAS, or
3516 * improper values for the sizes of the structures.
3517 */
3518 if (!memcpy_trapped(to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3519 retrieve_request, fragment_length)) {
3520 dlog_error(
3521 "%s: aborted the copy of response to RX buffer of "
3522 "%x.\n",
3523 __func__, to_locked.vm->id);
J-Alves3f6527c2024-04-25 17:10:57 +01003524
3525 ffa_hypervisor_memory_retrieve_request_undo(share_state);
3526
J-Alves7b9cc432024-04-04 10:57:17 +01003527 return ffa_error(FFA_ABORTED);
3528 }
3529
J-Alves3f6527c2024-04-25 17:10:57 +01003530 ffa_memory_retrieve_complete_from_hyp(share_state);
3531
J-Alves4f0d9c12024-01-17 17:23:11 +00003532 return ffa_memory_retrieve_resp(total_length, fragment_length);
3533}
3534
3535struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
3536 struct ffa_memory_region *retrieve_request,
3537 uint32_t retrieve_request_length,
3538 struct mpool *page_pool)
3539{
3540 ffa_memory_handle_t handle = retrieve_request->handle;
3541 struct share_states_locked share_states;
3542 struct ffa_memory_share_state *share_state;
3543 struct ffa_value ret;
3544
3545 dump_share_states();
3546
3547 share_states = share_states_lock();
3548 share_state = get_share_state(share_states, handle);
3549 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003550 dlog_verbose("Invalid handle %#lx for FFA_MEM_RETRIEVE_REQ.\n",
J-Alves4f0d9c12024-01-17 17:23:11 +00003551 handle);
3552 ret = ffa_error(FFA_INVALID_PARAMETERS);
3553 goto out;
3554 }
3555
3556 if (is_ffa_hypervisor_retrieve_request(retrieve_request, to_locked)) {
3557 ret = ffa_hypervisor_retrieve_request(share_state, to_locked,
3558 retrieve_request);
3559 } else {
3560 ret = ffa_partition_retrieve_request(
3561 share_states, share_state, to_locked, retrieve_request,
3562 retrieve_request_length, page_pool);
3563 }
3564
3565 /* Track use of the RX buffer if the handling has succeeded. */
3566 if (ret.func == FFA_MEM_RETRIEVE_RESP_32) {
3567 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
3568 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
3569 }
3570
Andrew Walbranca808b12020-05-15 17:22:28 +01003571out:
3572 share_states_unlock(&share_states);
3573 dump_share_states();
3574 return ret;
3575}
3576
J-Alves5da37d92022-10-24 16:33:48 +01003577/**
3578 * Determine expected fragment offset according to the FF-A version of
3579 * the caller.
3580 */
3581static uint32_t ffa_memory_retrieve_expected_offset_per_ffa_version(
3582 struct ffa_memory_region *memory_region,
Karl Meakin0e617d92024-04-05 12:55:22 +01003583 uint32_t retrieved_constituents_count, enum ffa_version ffa_version)
J-Alves5da37d92022-10-24 16:33:48 +01003584{
3585 uint32_t expected_fragment_offset;
3586 uint32_t composite_constituents_offset;
3587
Karl Meakin0e617d92024-04-05 12:55:22 +01003588 if (ffa_version >= FFA_VERSION_1_1) {
J-Alves5da37d92022-10-24 16:33:48 +01003589 /*
3590 * Hafnium operates memory regions in FF-A v1.1 format, so we
3591 * can retrieve the constituents offset from descriptor.
3592 */
3593 composite_constituents_offset =
3594 ffa_composite_constituent_offset(memory_region, 0);
Karl Meakin0e617d92024-04-05 12:55:22 +01003595 } else if (ffa_version == FFA_VERSION_1_0) {
J-Alves5da37d92022-10-24 16:33:48 +01003596 /*
3597 * If retriever is FF-A v1.0, determine the composite offset
3598 * as it is expected to have been configured in the
3599 * retrieve response.
3600 */
3601 composite_constituents_offset =
3602 sizeof(struct ffa_memory_region_v1_0) +
3603 RECEIVERS_COUNT_IN_RETRIEVE_RESP *
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003604 sizeof(struct ffa_memory_access_v1_0) +
J-Alves5da37d92022-10-24 16:33:48 +01003605 sizeof(struct ffa_composite_memory_region);
3606 } else {
3607 panic("%s received an invalid FF-A version.\n", __func__);
3608 }
3609
3610 expected_fragment_offset =
3611 composite_constituents_offset +
3612 retrieved_constituents_count *
3613 sizeof(struct ffa_memory_region_constituent) -
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003614 (uint32_t)(memory_region->memory_access_desc_size *
3615 (memory_region->receiver_count - 1));
J-Alves5da37d92022-10-24 16:33:48 +01003616
3617 return expected_fragment_offset;
3618}
3619
Andrew Walbranca808b12020-05-15 17:22:28 +01003620struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
3621 ffa_memory_handle_t handle,
3622 uint32_t fragment_offset,
J-Alves19e20cf2023-08-02 12:48:55 +01003623 ffa_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01003624 struct mpool *page_pool)
3625{
3626 struct ffa_memory_region *memory_region;
3627 struct share_states_locked share_states;
3628 struct ffa_memory_share_state *share_state;
3629 struct ffa_value ret;
3630 uint32_t fragment_index;
3631 uint32_t retrieved_constituents_count;
3632 uint32_t i;
3633 uint32_t expected_fragment_offset;
3634 uint32_t remaining_constituent_count;
3635 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01003636 uint32_t receiver_index;
J-Alves59ed0042022-07-28 18:26:41 +01003637 bool continue_ffa_hyp_mem_retrieve_req;
Andrew Walbranca808b12020-05-15 17:22:28 +01003638
3639 dump_share_states();
3640
3641 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003642 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003643 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003644 dlog_verbose("Invalid handle %#lx for FFA_MEM_FRAG_RX.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01003645 handle);
3646 ret = ffa_error(FFA_INVALID_PARAMETERS);
3647 goto out;
3648 }
3649
3650 memory_region = share_state->memory_region;
3651 CHECK(memory_region != NULL);
3652
Andrew Walbranca808b12020-05-15 17:22:28 +01003653 if (!share_state->sending_complete) {
3654 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003655 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01003656 "retrieve.\n",
3657 handle);
3658 ret = ffa_error(FFA_INVALID_PARAMETERS);
3659 goto out;
3660 }
3661
J-Alves59ed0042022-07-28 18:26:41 +01003662 /*
3663 * If retrieve request from the hypervisor has been initiated in the
3664 * given share_state, continue it, else assume it is a continuation of
3665 * retrieve request from a NWd VM.
3666 */
3667 continue_ffa_hyp_mem_retrieve_req =
3668 (to_locked.vm->id == HF_HYPERVISOR_VM_ID) &&
3669 (share_state->hypervisor_fragment_count != 0U) &&
J-Alves661e1b72023-08-02 13:39:40 +01003670 ffa_is_vm_id(sender_vm_id);
Andrew Walbranca808b12020-05-15 17:22:28 +01003671
J-Alves59ed0042022-07-28 18:26:41 +01003672 if (!continue_ffa_hyp_mem_retrieve_req) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003673 receiver_index = ffa_memory_region_get_receiver_index(
J-Alves59ed0042022-07-28 18:26:41 +01003674 memory_region, to_locked.vm->id);
3675
3676 if (receiver_index == memory_region->receiver_count) {
3677 dlog_verbose(
3678 "Caller of FFA_MEM_FRAG_RX (%x) is not a "
Karl Meakine8937d92024-03-19 16:04:25 +00003679 "borrower to memory sharing transaction "
3680 "(%lx)\n",
J-Alves59ed0042022-07-28 18:26:41 +01003681 to_locked.vm->id, handle);
3682 ret = ffa_error(FFA_INVALID_PARAMETERS);
3683 goto out;
3684 }
3685
3686 if (share_state->retrieved_fragment_count[receiver_index] ==
3687 0 ||
3688 share_state->retrieved_fragment_count[receiver_index] >=
3689 share_state->fragment_count) {
3690 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003691 "Retrieval of memory with handle %#lx not yet "
J-Alves59ed0042022-07-28 18:26:41 +01003692 "started or already completed (%d/%d fragments "
3693 "retrieved).\n",
3694 handle,
3695 share_state->retrieved_fragment_count
3696 [receiver_index],
3697 share_state->fragment_count);
3698 ret = ffa_error(FFA_INVALID_PARAMETERS);
3699 goto out;
3700 }
3701
3702 fragment_index =
3703 share_state->retrieved_fragment_count[receiver_index];
3704 } else {
3705 if (share_state->hypervisor_fragment_count == 0 ||
3706 share_state->hypervisor_fragment_count >=
3707 share_state->fragment_count) {
3708 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003709 "Retrieve of memory with handle %lx not "
J-Alves59ed0042022-07-28 18:26:41 +01003710 "started from hypervisor.\n",
3711 handle);
3712 ret = ffa_error(FFA_INVALID_PARAMETERS);
3713 goto out;
3714 }
3715
3716 if (memory_region->sender != sender_vm_id) {
3717 dlog_verbose(
3718 "Sender ID (%x) is not as expected for memory "
Karl Meakine8937d92024-03-19 16:04:25 +00003719 "handle %lx\n",
J-Alves59ed0042022-07-28 18:26:41 +01003720 sender_vm_id, handle);
3721 ret = ffa_error(FFA_INVALID_PARAMETERS);
3722 goto out;
3723 }
3724
3725 fragment_index = share_state->hypervisor_fragment_count;
3726
3727 receiver_index = 0;
3728 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003729
3730 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003731 * Check that the given fragment offset is correct by counting
3732 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01003733 */
3734 retrieved_constituents_count = 0;
3735 for (i = 0; i < fragment_index; ++i) {
3736 retrieved_constituents_count +=
3737 share_state->fragment_constituent_counts[i];
3738 }
J-Alvesc7484f12022-05-13 12:41:14 +01003739
3740 CHECK(memory_region->receiver_count > 0);
3741
Andrew Walbranca808b12020-05-15 17:22:28 +01003742 expected_fragment_offset =
J-Alves5da37d92022-10-24 16:33:48 +01003743 ffa_memory_retrieve_expected_offset_per_ffa_version(
3744 memory_region, retrieved_constituents_count,
3745 to_locked.vm->ffa_version);
3746
Andrew Walbranca808b12020-05-15 17:22:28 +01003747 if (fragment_offset != expected_fragment_offset) {
3748 dlog_verbose("Fragment offset was %d but expected %d.\n",
3749 fragment_offset, expected_fragment_offset);
3750 ret = ffa_error(FFA_INVALID_PARAMETERS);
3751 goto out;
3752 }
3753
J-Alves4f0d9c12024-01-17 17:23:11 +00003754 /*
3755 * When hafnium is the hypervisor, acquire the RX buffer of a VM, that
3756 * is currently ownder by the SPMC.
3757 */
3758 assert(plat_ffa_acquire_receiver_rx(to_locked, &ret));
J-Alves59ed0042022-07-28 18:26:41 +01003759
Andrew Walbranca808b12020-05-15 17:22:28 +01003760 remaining_constituent_count = ffa_memory_fragment_init(
3761 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3762 share_state->fragments[fragment_index],
3763 share_state->fragment_constituent_counts[fragment_index],
3764 &fragment_length);
3765 CHECK(remaining_constituent_count == 0);
J-Alves674e4de2024-01-17 16:20:32 +00003766
Andrew Walbranca808b12020-05-15 17:22:28 +01003767 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00003768 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbranca808b12020-05-15 17:22:28 +01003769
J-Alves59ed0042022-07-28 18:26:41 +01003770 if (!continue_ffa_hyp_mem_retrieve_req) {
3771 share_state->retrieved_fragment_count[receiver_index]++;
3772 if (share_state->retrieved_fragment_count[receiver_index] ==
3773 share_state->fragment_count) {
3774 ffa_memory_retrieve_complete(share_states, share_state,
3775 page_pool);
3776 }
3777 } else {
3778 share_state->hypervisor_fragment_count++;
3779
3780 ffa_memory_retrieve_complete_from_hyp(share_state);
3781 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003782 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
3783 .arg1 = (uint32_t)handle,
3784 .arg2 = (uint32_t)(handle >> 32),
3785 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003786
3787out:
3788 share_states_unlock(&share_states);
3789 dump_share_states();
3790 return ret;
3791}
3792
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003793struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003794 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003795 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003796{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003797 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003798 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003799 struct ffa_memory_share_state *share_state;
3800 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003801 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003802 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01003803 uint32_t receiver_index;
J-Alves3c5b2072022-11-21 12:45:40 +00003804 bool receivers_relinquished_memory;
Karl Meakin84710f32023-10-12 15:14:49 +01003805 ffa_memory_access_permissions_t receiver_permissions = {0};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003806
Andrew Walbrana65a1322020-04-06 19:32:32 +01003807 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003808 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003809 "Stream endpoints not supported (got %d endpoints on "
3810 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003811 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003812 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003813 }
3814
J-Alvesbd060342024-04-26 18:44:31 +01003815 if (vm_id_is_current_world(from_locked.vm->id) &&
3816 relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003817 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003818 "VM ID %d in relinquish message doesn't match calling "
3819 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01003820 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003821 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003822 }
3823
3824 dump_share_states();
3825
3826 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003827 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003828 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003829 dlog_verbose("Invalid handle %#lx for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003830 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003831 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003832 goto out;
3833 }
3834
Andrew Walbranca808b12020-05-15 17:22:28 +01003835 if (!share_state->sending_complete) {
3836 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003837 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01003838 "relinquish.\n",
3839 handle);
3840 ret = ffa_error(FFA_INVALID_PARAMETERS);
3841 goto out;
3842 }
3843
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003844 memory_region = share_state->memory_region;
3845 CHECK(memory_region != NULL);
3846
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003847 receiver_index = ffa_memory_region_get_receiver_index(
J-Alvesbd060342024-04-26 18:44:31 +01003848 memory_region, relinquish_request->endpoints[0]);
J-Alves8eb19162022-04-28 10:56:48 +01003849
3850 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003851 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003852 "VM ID %d tried to relinquish memory region "
Karl Meakine8937d92024-03-19 16:04:25 +00003853 "with handle %#lx and it is not a valid borrower.\n",
J-Alves8eb19162022-04-28 10:56:48 +01003854 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003855 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003856 goto out;
3857 }
3858
J-Alves8eb19162022-04-28 10:56:48 +01003859 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01003860 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003861 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003862 "Memory with handle %#lx not yet fully retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01003863 "receiver %x can't relinquish.\n",
3864 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003865 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003866 goto out;
3867 }
3868
J-Alves3c5b2072022-11-21 12:45:40 +00003869 /*
3870 * Either clear if requested in relinquish call, or in a retrieve
3871 * request from one of the borrowers.
3872 */
3873 receivers_relinquished_memory = true;
3874
3875 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3876 struct ffa_memory_access *receiver =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003877 ffa_memory_region_get_receiver(memory_region, i);
3878 assert(receiver != NULL);
J-Alves3c5b2072022-11-21 12:45:40 +00003879 if (receiver->receiver_permissions.receiver ==
3880 from_locked.vm->id) {
J-Alves639ddfc2023-11-21 14:17:26 +00003881 receiver_permissions =
3882 receiver->receiver_permissions.permissions;
J-Alves3c5b2072022-11-21 12:45:40 +00003883 continue;
3884 }
3885
3886 if (share_state->retrieved_fragment_count[i] != 0U) {
3887 receivers_relinquished_memory = false;
3888 break;
3889 }
3890 }
3891
3892 clear = receivers_relinquished_memory &&
Daniel Boulby2e14ebe2024-01-15 16:21:44 +00003893 ((relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
3894 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003895
3896 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003897 * Clear is not allowed for memory that was shared, as the
3898 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003899 */
J-Alves95fbb312024-03-20 15:19:16 +00003900 if (clear && (share_state->share_func == FFA_MEM_SHARE_32 ||
3901 share_state->share_func == FFA_MEM_SHARE_64)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003902 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003903 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003904 goto out;
3905 }
3906
J-Alvesb886d492024-04-15 10:55:29 +01003907 if (clear && receiver_permissions.data_access == FFA_DATA_ACCESS_RO) {
J-Alves639ddfc2023-11-21 14:17:26 +00003908 dlog_verbose("%s: RO memory can't use clear memory flag.\n",
3909 __func__);
3910 ret = ffa_error(FFA_DENIED);
3911 goto out;
3912 }
3913
Andrew Walbranca808b12020-05-15 17:22:28 +01003914 ret = ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01003915 from_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003916 share_state->fragment_constituent_counts,
J-Alves69cdfd92024-04-26 11:40:59 +01003917 share_state->fragment_count, share_state->sender_orig_mode,
3918 page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003919
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003920 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003921 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003922 * Mark memory handle as not retrieved, so it can be
3923 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003924 */
J-Alves8eb19162022-04-28 10:56:48 +01003925 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003926 }
3927
3928out:
3929 share_states_unlock(&share_states);
3930 dump_share_states();
3931 return ret;
3932}
3933
3934/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01003935 * Validates that the reclaim transition is allowed for the given
3936 * handle, updates the page table of the reclaiming VM, and frees the
3937 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003938 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003939struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01003940 ffa_memory_handle_t handle,
3941 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003942 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003943{
3944 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003945 struct ffa_memory_share_state *share_state;
3946 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003947 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003948
3949 dump_share_states();
3950
3951 share_states = share_states_lock();
Karl Meakin52cdfe72023-06-30 14:49:10 +01003952
Karl Meakin4a2854a2023-06-30 16:26:52 +01003953 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003954 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003955 dlog_verbose("Invalid handle %#lx for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003956 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003957 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003958 goto out;
3959 }
Karl Meakin4a2854a2023-06-30 16:26:52 +01003960 memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003961
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003962 CHECK(memory_region != NULL);
3963
J-Alvesa9cd7e32022-07-01 13:49:33 +01003964 if (vm_id_is_current_world(to_locked.vm->id) &&
3965 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003966 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003967 "VM %#x attempted to reclaim memory handle %#lx "
Olivier Deprezf92e5d42020-11-13 16:00:54 +01003968 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003969 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003970 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003971 goto out;
3972 }
3973
Andrew Walbranca808b12020-05-15 17:22:28 +01003974 if (!share_state->sending_complete) {
3975 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003976 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01003977 "reclaim.\n",
3978 handle);
3979 ret = ffa_error(FFA_INVALID_PARAMETERS);
3980 goto out;
3981 }
3982
J-Alves752236c2022-04-28 11:07:47 +01003983 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3984 if (share_state->retrieved_fragment_count[i] != 0) {
J-Alves9bbcb872024-04-25 17:19:00 +01003985 struct ffa_memory_access *receiver =
3986 ffa_memory_region_get_receiver(memory_region,
3987 i);
3988
3989 assert(receiver != NULL);
3990 (void)receiver;
J-Alves752236c2022-04-28 11:07:47 +01003991 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003992 "Tried to reclaim memory handle %#lx that has "
3993 "not been relinquished by all borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01003994 handle,
J-Alves9bbcb872024-04-25 17:19:00 +01003995 receiver->receiver_permissions.receiver);
J-Alves752236c2022-04-28 11:07:47 +01003996 ret = ffa_error(FFA_DENIED);
3997 goto out;
3998 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003999 }
4000
Andrew Walbranca808b12020-05-15 17:22:28 +01004001 ret = ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01004002 to_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01004003 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00004004 share_state->fragment_count, share_state->sender_orig_mode,
J-Alves460d36c2023-10-12 17:02:15 +01004005 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool,
J-Alvesfd206052023-05-22 16:45:00 +01004006 NULL, share_state->memory_protected);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004007
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01004008 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004009 share_state_free(share_states, share_state, page_pool);
J-Alves3c5b2072022-11-21 12:45:40 +00004010 dlog_verbose("Freed share state after successful reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004011 }
4012
4013out:
4014 share_states_unlock(&share_states);
4015 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01004016}