blob: 53a605fdd7956e53ec1657a2b75dfe8cb9d11292 [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.");
Karl Meakina5ea9092024-05-28 15:40:33 +0100381 default:
382 panic("Unknown data access %#x\n", permissions.data_access);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100383 }
384
Karl Meakin84710f32023-10-12 15:14:49 +0100385 switch (permissions.instruction_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100386 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000387 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100388 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100389 mode |= MM_MODE_X;
390 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100391 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
392 mode |= (default_mode & MM_MODE_X);
393 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100394 case FFA_INSTRUCTION_ACCESS_RESERVED:
395 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Karl Meakina5ea9092024-05-28 15:40:33 +0100396 default:
397 panic("Unknown instruction access %#x\n",
398 permissions.instruction_access);
Andrew Walbran475c1452020-02-07 13:22:22 +0000399 }
400
Olivier Deprez878bd5b2021-04-15 19:05:10 +0200401 /* Set the security state bit if necessary. */
402 if ((default_mode & plat_ffa_other_world_mode()) != 0) {
403 mode |= plat_ffa_other_world_mode();
404 }
405
Daniel Boulby6e261362024-06-13 16:53:00 +0100406 mode |= default_mode & MM_MODE_D;
407
Andrew Walbran475c1452020-02-07 13:22:22 +0000408 return mode;
409}
410
Jose Marinho75509b42019-04-09 09:34:59 +0100411/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000412 * Get the current mode in the stage-2 page table of the given vm of all the
413 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100414 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100415 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100416static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000417 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100418 struct ffa_memory_region_constituent **fragments,
419 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100420{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100421 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100422 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100423
Andrew Walbranca808b12020-05-15 17:22:28 +0100424 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100425 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000426 * Fail if there are no constituents. Otherwise we would get an
427 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100428 */
Karl Meakin5df422c2023-07-11 17:31:38 +0100429 dlog_verbose("%s: no constituents\n", __func__);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100430 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100431 }
432
Andrew Walbranca808b12020-05-15 17:22:28 +0100433 for (i = 0; i < fragment_count; ++i) {
434 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
435 ipaddr_t begin = ipa_init(fragments[i][j].address);
436 size_t size = fragments[i][j].page_count * PAGE_SIZE;
437 ipaddr_t end = ipa_add(begin, size);
438 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100439
Andrew Walbranca808b12020-05-15 17:22:28 +0100440 /* Fail if addresses are not page-aligned. */
441 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
442 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100443 dlog_verbose("%s: addresses not page-aligned\n",
444 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +0100445 return ffa_error(FFA_INVALID_PARAMETERS);
446 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100447
Andrew Walbranca808b12020-05-15 17:22:28 +0100448 /*
449 * Ensure that this constituent memory range is all
450 * mapped with the same mode.
451 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800452 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100453 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +0000454 "%s: constituent memory range "
455 "%#lx..%#lx "
Karl Meakin5df422c2023-07-11 17:31:38 +0100456 "not mapped with the same mode\n",
Karl Meakine8937d92024-03-19 16:04:25 +0000457 __func__, begin.ipa, end.ipa);
Andrew Walbranca808b12020-05-15 17:22:28 +0100458 return ffa_error(FFA_DENIED);
459 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100460
Andrew Walbranca808b12020-05-15 17:22:28 +0100461 /*
462 * Ensure that all constituents are mapped with the same
463 * mode.
464 */
465 if (i == 0) {
466 *orig_mode = current_mode;
467 } else if (current_mode != *orig_mode) {
468 dlog_verbose(
Karl Meakin5df422c2023-07-11 17:31:38 +0100469 "%s: expected mode %#x but was %#x for "
Karl Meakine8937d92024-03-19 16:04:25 +0000470 "%d pages at %#lx.\n",
Karl Meakin5df422c2023-07-11 17:31:38 +0100471 __func__, *orig_mode, current_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100472 fragments[i][j].page_count,
473 ipa_addr(begin));
474 return ffa_error(FFA_DENIED);
475 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100476 }
Jose Marinho75509b42019-04-09 09:34:59 +0100477 }
478
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100479 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000480}
481
Karl Meakin0e617d92024-04-05 12:55:22 +0100482enum ffa_version ffa_version_from_memory_access_desc_size(
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100483 uint32_t memory_access_desc_size)
484{
485 switch (memory_access_desc_size) {
486 /*
487 * v1.0 and v1.1 memory access descriptors are the same size however
488 * v1.1 is the first version to include the memory access descriptor
489 * size field so return v1.1.
490 */
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000491 case sizeof(struct ffa_memory_access_v1_0):
Karl Meakin0e617d92024-04-05 12:55:22 +0100492 return FFA_VERSION_1_1;
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000493 case sizeof(struct ffa_memory_access):
Karl Meakin0e617d92024-04-05 12:55:22 +0100494 return FFA_VERSION_1_2;
Karl Meakina5ea9092024-05-28 15:40:33 +0100495 default:
496 return 0;
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100497 }
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100498}
499
500/**
501 * Check if the receivers size and offset given is valid for the senders
502 * FF-A version.
503 */
504static bool receiver_size_and_offset_valid_for_version(
505 uint32_t receivers_size, uint32_t receivers_offset,
Karl Meakin0e617d92024-04-05 12:55:22 +0100506 enum ffa_version ffa_version)
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100507{
508 /*
509 * Check that the version that the memory access descriptor size belongs
510 * to is compatible with the FF-A version we believe the sender to be.
511 */
Karl Meakin0e617d92024-04-05 12:55:22 +0100512 enum ffa_version expected_ffa_version =
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100513 ffa_version_from_memory_access_desc_size(receivers_size);
Karl Meakin0e617d92024-04-05 12:55:22 +0100514 if (!ffa_versions_are_compatible(expected_ffa_version, ffa_version)) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100515 return false;
516 }
517
518 /*
519 * Check the receivers_offset matches the version we found from
520 * memory access descriptor size.
521 */
522 switch (expected_ffa_version) {
Karl Meakin0e617d92024-04-05 12:55:22 +0100523 case FFA_VERSION_1_1:
524 case FFA_VERSION_1_2:
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100525 return receivers_offset == sizeof(struct ffa_memory_region);
526 default:
527 return false;
528 }
529}
530
531/**
532 * Check the values set for fields in the memory region are valid and safe.
533 * Offset values are within safe bounds, receiver count will not cause overflows
534 * and reserved fields are 0.
535 */
536bool ffa_memory_region_sanity_check(struct ffa_memory_region *memory_region,
Karl Meakin0e617d92024-04-05 12:55:22 +0100537 enum ffa_version ffa_version,
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100538 uint32_t fragment_length,
539 bool send_transaction)
540{
541 uint32_t receiver_count;
542 struct ffa_memory_access *receiver;
543 uint32_t composite_offset_0;
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000544 struct ffa_memory_region_v1_0 *memory_region_v1_0 =
545 (struct ffa_memory_region_v1_0 *)memory_region;
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100546
Karl Meakin0e617d92024-04-05 12:55:22 +0100547 if (ffa_version == FFA_VERSION_1_0) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100548 /* Check the reserved fields are 0. */
549 if (memory_region_v1_0->reserved_0 != 0 ||
550 memory_region_v1_0->reserved_1 != 0) {
551 dlog_verbose("Reserved fields must be 0.\n");
552 return false;
553 }
554
555 receiver_count = memory_region_v1_0->receiver_count;
556 } else {
557 uint32_t receivers_size =
558 memory_region->memory_access_desc_size;
559 uint32_t receivers_offset = memory_region->receivers_offset;
560
561 /* Check the reserved field is 0. */
562 if (memory_region->reserved[0] != 0 ||
563 memory_region->reserved[1] != 0 ||
564 memory_region->reserved[2] != 0) {
565 dlog_verbose("Reserved fields must be 0.\n");
566 return false;
567 }
568
569 /*
570 * Check memory_access_desc_size matches the size of the struct
571 * for the senders FF-A version.
572 */
573 if (!receiver_size_and_offset_valid_for_version(
574 receivers_size, receivers_offset, ffa_version)) {
575 dlog_verbose(
576 "Invalid memory access descriptor size %d, "
577 " or receiver offset %d, "
578 "for FF-A version %#x\n",
579 receivers_size, receivers_offset, ffa_version);
580 return false;
581 }
582
583 receiver_count = memory_region->receiver_count;
584 }
585
586 /* Check receiver count is not too large. */
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000587 if (receiver_count > MAX_MEM_SHARE_RECIPIENTS || receiver_count < 1) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100588 dlog_verbose(
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000589 "Receiver count must be 0 < receiver_count < %u "
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100590 "specified %u\n",
591 MAX_MEM_SHARE_RECIPIENTS, receiver_count);
592 return false;
593 }
594
595 /* Check values in the memory access descriptors. */
596 /*
597 * The composite offset values must be the same for all recievers so
598 * check the first one is valid and then they are all the same.
599 */
Karl Meakin0e617d92024-04-05 12:55:22 +0100600 receiver = ffa_version == FFA_VERSION_1_0
Daniel Boulbyf06b5232024-02-22 16:26:43 +0000601 ? (struct ffa_memory_access *)&memory_region_v1_0
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100602 ->receivers[0]
603 : ffa_memory_region_get_receiver(memory_region, 0);
604 assert(receiver != NULL);
605 composite_offset_0 = receiver->composite_memory_region_offset;
606
607 if (!send_transaction) {
608 if (composite_offset_0 != 0) {
609 dlog_verbose(
610 "Composite offset memory region descriptor "
611 "offset must be 0 for retrieve requests. "
612 "Currently %d",
613 composite_offset_0);
614 return false;
615 }
616 } else {
617 bool comp_offset_is_zero = composite_offset_0 == 0U;
618 bool comp_offset_lt_transaction_descriptor_size =
619 composite_offset_0 <
620 (sizeof(struct ffa_memory_region) +
Karl Meakin66a38bd2024-05-28 16:00:56 +0100621 (size_t)(memory_region->memory_access_desc_size *
622 memory_region->receiver_count));
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100623 bool comp_offset_with_comp_gt_fragment_length =
624 composite_offset_0 +
625 sizeof(struct ffa_composite_memory_region) >
626 fragment_length;
627 if (comp_offset_is_zero ||
628 comp_offset_lt_transaction_descriptor_size ||
629 comp_offset_with_comp_gt_fragment_length) {
630 dlog_verbose(
631 "Invalid composite memory region descriptor "
632 "offset for send transaction %u\n",
633 composite_offset_0);
634 return false;
635 }
636 }
637
Karl Meakin824b63d2024-06-03 19:04:53 +0100638 for (size_t i = 0; i < memory_region->receiver_count; i++) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100639 uint32_t composite_offset;
640
Karl Meakin0e617d92024-04-05 12:55:22 +0100641 if (ffa_version == FFA_VERSION_1_0) {
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100642 struct ffa_memory_access_v1_0 *receiver_v1_0 =
643 &memory_region_v1_0->receivers[i];
644 /* Check reserved fields are 0 */
645 if (receiver_v1_0->reserved_0 != 0) {
646 dlog_verbose(
647 "Reserved field in the memory access "
Karl Meakine8937d92024-03-19 16:04:25 +0000648 "descriptor must be zero. Currently "
649 "reciever %zu has a reserved field "
650 "with a value of %lu\n",
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100651 i, receiver_v1_0->reserved_0);
652 return false;
653 }
654 /*
655 * We can cast to the current version receiver as the
656 * remaining fields we are checking have the same
657 * offsets for all versions since memory access
658 * descriptors are forwards compatible.
659 */
660 receiver = (struct ffa_memory_access *)receiver_v1_0;
661 } else {
662 receiver = ffa_memory_region_get_receiver(memory_region,
663 i);
664 assert(receiver != NULL);
665
666 if (receiver->reserved_0 != 0) {
667 dlog_verbose(
668 "Reserved field in the memory access "
Karl Meakine8937d92024-03-19 16:04:25 +0000669 "descriptor must be zero. Currently "
670 "reciever %zu has a reserved field "
671 "with a value of %lu\n",
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100672 i, receiver->reserved_0);
673 return false;
674 }
675 }
676
677 /* Check composite offset values are equal for all receivers. */
678 composite_offset = receiver->composite_memory_region_offset;
679 if (composite_offset != composite_offset_0) {
680 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +0000681 "Composite offset %x differs from %x in "
682 "index\n",
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100683 composite_offset, composite_offset_0);
684 return false;
685 }
686 }
687 return true;
688}
689
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000690/**
J-Alves460d36c2023-10-12 17:02:15 +0100691 * If the receivers for the memory management operation are all from the
692 * secure world and this isn't a FFA_MEM_SHARE, then request memory security
693 * state update by returning MAP_ACTION_CHECK_PROTECT.
694 */
695static enum ffa_map_action ffa_mem_send_get_map_action(
696 bool all_receivers_from_current_world, ffa_id_t sender_id,
697 uint32_t mem_func_id)
698{
J-Alves95fbb312024-03-20 15:19:16 +0000699 const bool is_memory_share_abi = mem_func_id == FFA_MEM_SHARE_32 ||
700 mem_func_id == FFA_MEM_SHARE_64;
701 const bool protect_memory =
702 (!is_memory_share_abi && all_receivers_from_current_world &&
703 ffa_is_vm_id(sender_id));
J-Alves460d36c2023-10-12 17:02:15 +0100704
705 return protect_memory ? MAP_ACTION_CHECK_PROTECT : MAP_ACTION_CHECK;
706}
707
708/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000709 * Verify that all pages have the same mode, that the starting mode
710 * constitutes a valid state and obtain the next mode to apply
J-Alves460d36c2023-10-12 17:02:15 +0100711 * to the sending VM. It outputs the mapping action that needs to be
712 * invoked for the given memory range. On memory lend/donate there
713 * could be a need to protect the memory from the normal world.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000714 *
715 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100716 * 1) FFA_DENIED if a state transition was not found;
717 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100718 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100719 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100720 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100721 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
722 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000723 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100724static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100725 struct vm_locked from, uint32_t share_func,
Daniel Boulbya76fd912024-02-22 14:22:15 +0000726 struct ffa_memory_region *memory_region, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100727 struct ffa_memory_region_constituent **fragments,
728 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby4b846eb2024-05-23 17:32:23 +0100729 uint32_t *from_mode, enum ffa_map_action *map_action, bool zero)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000730{
731 const uint32_t state_mask =
732 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100733 struct ffa_value ret;
J-Alves460d36c2023-10-12 17:02:15 +0100734 bool all_receivers_from_current_world = true;
Daniel Boulbya76fd912024-02-22 14:22:15 +0000735 uint32_t receivers_count = memory_region->receiver_count;
J-Alves95fbb312024-03-20 15:19:16 +0000736 const bool is_memory_lend = (share_func == FFA_MEM_LEND_32) ||
737 (share_func == FFA_MEM_LEND_64);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000738
Andrew Walbranca808b12020-05-15 17:22:28 +0100739 ret = constituents_get_mode(from, orig_from_mode, fragments,
740 fragment_constituent_counts,
741 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100742 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100743 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100744 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100745 }
746
Daniel Boulby6e261362024-06-13 16:53:00 +0100747 /*
748 * Check requested memory type is valid with the memory type of the
749 * owner. E.g. they follow the memory type precedence where Normal
750 * memory is more permissive than device and therefore device memory
751 * can only be shared as device memory.
752 */
753 if (memory_region->attributes.type == FFA_MEMORY_NORMAL_MEM &&
754 (*orig_from_mode & MM_MODE_D) != 0U) {
755 dlog_verbose(
756 "Send device memory as Normal memory is not allowed\n");
757 return ffa_error(FFA_DENIED);
758 }
759
Daniel Boulby63af1fa2024-03-18 14:17:31 +0000760 /* Device memory regions can only be lent a single borrower. */
Daniel Boulby9764ff62024-01-30 17:47:39 +0000761 if ((*orig_from_mode & MM_MODE_D) != 0U &&
J-Alves95fbb312024-03-20 15:19:16 +0000762 !(is_memory_lend && receivers_count == 1)) {
Daniel Boulby9764ff62024-01-30 17:47:39 +0000763 dlog_verbose(
Daniel Boulby63af1fa2024-03-18 14:17:31 +0000764 "Device memory can only be lent to a single borrower "
765 "(mode is %#x).\n",
Daniel Boulby9764ff62024-01-30 17:47:39 +0000766 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100767 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000768 }
769
770 /*
771 * Ensure the sender is the owner and has exclusive access to the
772 * memory.
773 */
774 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100775 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100776 }
777
Daniel Boulby4b846eb2024-05-23 17:32:23 +0100778 /*
779 * Memory cannot be zeroed during the lend/donate operation if the
780 * sender only has RO access.
781 */
782 if ((*orig_from_mode & MM_MODE_W) == 0 && zero == true) {
783 dlog_verbose(
784 "Cannot zero memory when the sender doesn't have "
785 "write access\n");
786 return ffa_error(FFA_DENIED);
787 }
788
Daniel Boulbya76fd912024-02-22 14:22:15 +0000789 assert(receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100790
J-Alves363f5722022-04-25 17:37:37 +0100791 for (uint32_t i = 0U; i < receivers_count; i++) {
Daniel Boulbya76fd912024-02-22 14:22:15 +0000792 struct ffa_memory_access *receiver =
793 ffa_memory_region_get_receiver(memory_region, i);
794 assert(receiver != NULL);
J-Alves363f5722022-04-25 17:37:37 +0100795 ffa_memory_access_permissions_t permissions =
Daniel Boulbya76fd912024-02-22 14:22:15 +0000796 receiver->receiver_permissions.permissions;
J-Alves363f5722022-04-25 17:37:37 +0100797 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
798 permissions, *orig_from_mode);
799
J-Alves788b4492023-04-18 14:01:23 +0100800 /*
801 * The assumption is that at this point, the operation from
802 * SP to a receiver VM, should have returned an FFA_ERROR
803 * already.
804 */
805 if (!ffa_is_vm_id(from.vm->id)) {
806 assert(!ffa_is_vm_id(
Daniel Boulbya76fd912024-02-22 14:22:15 +0000807 receiver->receiver_permissions.receiver));
J-Alves788b4492023-04-18 14:01:23 +0100808 }
809
J-Alves460d36c2023-10-12 17:02:15 +0100810 /* Track if all senders are from current world. */
811 all_receivers_from_current_world =
812 all_receivers_from_current_world &&
813 vm_id_is_current_world(
Daniel Boulbya76fd912024-02-22 14:22:15 +0000814 receiver->receiver_permissions.receiver);
J-Alves460d36c2023-10-12 17:02:15 +0100815
J-Alves363f5722022-04-25 17:37:37 +0100816 if ((*orig_from_mode & required_from_mode) !=
817 required_from_mode) {
818 dlog_verbose(
819 "Sender tried to send memory with permissions "
J-Alves788b4492023-04-18 14:01:23 +0100820 "which required mode %#x but only had %#x "
821 "itself.\n",
J-Alves363f5722022-04-25 17:37:37 +0100822 required_from_mode, *orig_from_mode);
823 return ffa_error(FFA_DENIED);
824 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000825 }
826
J-Alves460d36c2023-10-12 17:02:15 +0100827 *map_action = ffa_mem_send_get_map_action(
828 all_receivers_from_current_world, from.vm->id, share_func);
829
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000830 /* Find the appropriate new mode. */
831 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000832 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +0000833 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100834 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000835 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100836 break;
J-Alves95fbb312024-03-20 15:19:16 +0000837 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100838 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000839 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100840 break;
J-Alves95fbb312024-03-20 15:19:16 +0000841 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100842 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000843 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100844 break;
845
Jose Marinho75509b42019-04-09 09:34:59 +0100846 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100847 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100848 }
849
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100850 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000851}
852
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100853static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000854 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100855 struct ffa_memory_region_constituent **fragments,
856 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves69cdfd92024-04-26 11:40:59 +0100857 uint32_t *from_mode, enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000858{
859 const uint32_t state_mask =
860 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
861 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100862 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000863
J-Alves69cdfd92024-04-26 11:40:59 +0100864 assert(map_action != NULL);
865 if (vm_id_is_current_world(from.vm->id)) {
866 *map_action = MAP_ACTION_COMMIT;
867 } else {
868 /*
869 * No need to check the attributes of caller.
870 * The assumption is that the retrieve request of the receiver
871 * also used the MAP_ACTION_NONE, and no update was done to the
872 * page tables. When the receiver is not at the secure virtual
873 * instance SPMC doesn't manage its S2 translation (i.e. when
874 * the receiver is a VM).
875 */
876 *map_action = MAP_ACTION_NONE;
877
878 return (struct ffa_value){.func = FFA_SUCCESS_32};
879 }
880
Andrew Walbranca808b12020-05-15 17:22:28 +0100881 ret = constituents_get_mode(from, orig_from_mode, fragments,
882 fragment_constituent_counts,
883 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100884 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100885 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000886 }
887
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000888 /*
889 * Ensure the relinquishing VM is not the owner but has access to the
890 * memory.
891 */
892 orig_from_state = *orig_from_mode & state_mask;
893 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
894 dlog_verbose(
895 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100896 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000897 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100898 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000899 }
900
901 /* Find the appropriate new mode. */
902 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
903
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100904 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000905}
906
907/**
908 * Verify that all pages have the same mode, that the starting mode
909 * constitutes a valid state and obtain the next mode to apply
910 * to the retrieving VM.
911 *
912 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100913 * 1) FFA_DENIED if a state transition was not found;
914 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100915 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100916 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100917 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100918 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
919 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000920 */
J-Alvesfc19b372022-07-06 12:17:35 +0100921struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000922 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100923 struct ffa_memory_region_constituent **fragments,
924 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby71d887b2024-06-28 16:38:06 +0100925 uint32_t sender_orig_mode, uint32_t *to_mode, bool memory_protected,
J-Alvesfd206052023-05-22 16:45:00 +0100926 enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000927{
928 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100929 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000930
Andrew Walbranca808b12020-05-15 17:22:28 +0100931 ret = constituents_get_mode(to, &orig_to_mode, fragments,
932 fragment_constituent_counts,
933 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100934 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100935 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100936 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000937 }
938
J-Alves460d36c2023-10-12 17:02:15 +0100939 /* Find the appropriate new mode. */
Daniel Boulby71d887b2024-06-28 16:38:06 +0100940 *to_mode = sender_orig_mode;
J-Alves460d36c2023-10-12 17:02:15 +0100941
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100942 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000943 /*
944 * If the original ffa memory send call has been processed
945 * successfully, it is expected the orig_to_mode would overlay
946 * with `state_mask`, as a result of the function
947 * `ffa_send_check_transition`.
J-Alvesfd206052023-05-22 16:45:00 +0100948 *
949 * If Hafnium is the SPMC:
950 * - Caller of the reclaim interface is an SP, the memory shall
951 * have been protected throughout the flow.
952 * - Caller of the reclaim is from the NWd, the memory may have
953 * been protected at the time of lending/donating the memory.
954 * In such case, set action to unprotect memory in the
955 * handling of reclaim operation.
956 * - If Hafnium is the hypervisor memory shall never have been
957 * protected in memory lend/share/donate.
958 *
959 * More details in the doc comment of the function
960 * `ffa_region_group_identity_map`.
J-Alves9256f162021-12-09 13:18:43 +0000961 */
J-Alves59ed0042022-07-28 18:26:41 +0100962 if (vm_id_is_current_world(to.vm->id)) {
963 assert((orig_to_mode &
964 (MM_MODE_INVALID | MM_MODE_UNOWNED |
965 MM_MODE_SHARED)) != 0U);
J-Alvesfd206052023-05-22 16:45:00 +0100966 assert(!memory_protected);
967 } else if (to.vm->id == HF_OTHER_WORLD_ID &&
968 map_action != NULL && memory_protected) {
969 *map_action = MAP_ACTION_COMMIT_UNPROTECT;
J-Alves59ed0042022-07-28 18:26:41 +0100970 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000971 } else {
J-Alves69cdfd92024-04-26 11:40:59 +0100972 if (!vm_id_is_current_world(to.vm->id)) {
973 assert(map_action != NULL);
974 *map_action = MAP_ACTION_NONE;
975 return (struct ffa_value){.func = FFA_SUCCESS_32};
976 }
977
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000978 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +0100979 * If the retriever is from virtual FF-A instance:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000980 * Ensure the retriever has the expected state. We don't care
981 * about the MM_MODE_SHARED bit; either with or without it set
982 * are both valid representations of the !O-NA state.
983 */
J-Alvesa9cd7e32022-07-01 13:49:33 +0100984 if (vm_id_is_current_world(to.vm->id) &&
Karl Meakin5e996992024-05-20 11:27:07 +0100985 !vm_is_primary(to.vm) &&
J-Alvesa9cd7e32022-07-01 13:49:33 +0100986 (orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
987 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100988 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000989 }
J-Alves460d36c2023-10-12 17:02:15 +0100990
991 /*
992 * If memory has been protected before, clear the NS bit to
993 * allow the secure access from the SP.
994 */
995 if (memory_protected) {
996 *to_mode &= ~plat_ffa_other_world_mode();
997 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000998 }
999
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001000 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00001001 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001002 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001003 *to_mode |= 0;
1004 break;
J-Alves95fbb312024-03-20 15:19:16 +00001005 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001006 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001007 *to_mode |= MM_MODE_UNOWNED;
1008 break;
J-Alves95fbb312024-03-20 15:19:16 +00001009 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001010 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001011 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
1012 break;
1013
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001014 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001015 *to_mode |= 0;
1016 break;
1017
1018 default:
Andrew Walbranca808b12020-05-15 17:22:28 +01001019 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001020 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001021 }
1022
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001023 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +01001024}
Jose Marinho09b1db82019-08-08 09:16:59 +01001025
J-Alvescf6253e2024-01-03 13:48:48 +00001026/*
1027 * Performs the operations related to the `action` MAP_ACTION_CHECK*.
1028 * Returns:
1029 * - FFA_SUCCESS_32: if all goes well.
1030 * - FFA_ERROR_32: with FFA_NO_MEMORY, if there is no memory to manage
1031 * the page table update. Or error code provided by the function
1032 * `arch_memory_protect`.
1033 */
1034static struct ffa_value ffa_region_group_check_actions(
1035 struct vm_locked vm_locked, paddr_t pa_begin, paddr_t pa_end,
1036 struct mpool *ppool, uint32_t mode, enum ffa_map_action action,
1037 bool *memory_protected)
1038{
1039 struct ffa_value ret;
1040 bool is_memory_protected;
1041
1042 if (!vm_identity_prepare(vm_locked, pa_begin, pa_end, mode, ppool)) {
1043 dlog_verbose(
1044 "%s: memory can't be mapped to %x due to lack of "
Karl Meakine8937d92024-03-19 16:04:25 +00001045 "memory. Base: %lx end: %lx\n",
J-Alvescf6253e2024-01-03 13:48:48 +00001046 __func__, vm_locked.vm->id, pa_addr(pa_begin),
1047 pa_addr(pa_end));
1048 return ffa_error(FFA_NO_MEMORY);
1049 }
1050
1051 switch (action) {
1052 case MAP_ACTION_CHECK:
1053 /* No protect requested. */
1054 is_memory_protected = false;
1055 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1056 break;
1057 case MAP_ACTION_CHECK_PROTECT: {
1058 paddr_t last_protected_pa = pa_init(0);
1059
1060 ret = arch_memory_protect(pa_begin, pa_end, &last_protected_pa);
1061
1062 is_memory_protected = (ret.func == FFA_SUCCESS_32);
1063
1064 /*
1065 * - If protect memory has failed with FFA_DENIED, means some
1066 * range of memory was in the wrong state. In such case, SPM
1067 * reverts the state of the pages that were successfully
1068 * updated.
1069 * - If protect memory has failed with FFA_NOT_SUPPORTED, it
1070 * means the platform doesn't support the protection mechanism.
1071 * That said, it still permits the page table update to go
1072 * through. The variable
1073 * `is_memory_protected` will be equal to false.
1074 * - If protect memory has failed with FFA_INVALID_PARAMETERS,
1075 * break from switch and return the error.
1076 */
1077 if (ret.func == FFA_ERROR_32) {
1078 assert(!is_memory_protected);
1079 if (ffa_error_code(ret) == FFA_DENIED &&
1080 pa_addr(last_protected_pa) != (uintptr_t)0) {
1081 CHECK(arch_memory_unprotect(
1082 pa_begin,
1083 pa_add(last_protected_pa, PAGE_SIZE)));
1084 } else if (ffa_error_code(ret) == FFA_NOT_SUPPORTED) {
1085 ret = (struct ffa_value){
1086 .func = FFA_SUCCESS_32,
1087 };
1088 }
1089 }
1090 } break;
1091 default:
1092 panic("%s: invalid action to process %x\n", __func__, action);
1093 }
1094
1095 if (memory_protected != NULL) {
1096 *memory_protected = is_memory_protected;
1097 }
1098
1099 return ret;
1100}
1101
1102static void ffa_region_group_commit_actions(struct vm_locked vm_locked,
1103 paddr_t pa_begin, paddr_t pa_end,
1104 struct mpool *ppool, uint32_t mode,
1105 enum ffa_map_action action)
1106{
1107 switch (action) {
1108 case MAP_ACTION_COMMIT_UNPROTECT:
1109 /*
1110 * Checking that it should succeed because SPM should be
1111 * unprotecting memory that it had protected before.
1112 */
1113 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1114 case MAP_ACTION_COMMIT:
1115 vm_identity_commit(vm_locked, pa_begin, pa_end, mode, ppool,
1116 NULL);
1117 break;
1118 default:
1119 panic("%s: invalid action to process %x\n", __func__, action);
1120 }
1121}
1122
Jose Marinho09b1db82019-08-08 09:16:59 +01001123/**
J-Alves063ad832023-10-03 18:05:40 +01001124 * Helper function to revert a failed "Protect" action from the SPMC:
1125 * - `fragment_count`: should specify the number of fragments to traverse from
1126 * `fragments`. This may not be the full amount of fragments that are part of
1127 * the share_state structure.
1128 * - `fragment_constituent_counts`: array holding the amount of constituents
1129 * per fragment.
1130 * - `end`: pointer to the constituent that failed the "protect" action. It
1131 * shall be part of the last fragment, and it shall make the loop below break.
1132 */
1133static void ffa_region_group_fragments_revert_protect(
1134 struct ffa_memory_region_constituent **fragments,
1135 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1136 const struct ffa_memory_region_constituent *end)
1137{
1138 for (uint32_t i = 0; i < fragment_count; ++i) {
1139 for (uint32_t j = 0; j < fragment_constituent_counts[i]; ++j) {
1140 struct ffa_memory_region_constituent *constituent =
1141 &fragments[i][j];
1142 size_t size = constituent->page_count * PAGE_SIZE;
1143 paddr_t pa_begin =
1144 pa_from_ipa(ipa_init(constituent->address));
1145 paddr_t pa_end = pa_add(pa_begin, size);
1146
Karl Meakine8937d92024-03-19 16:04:25 +00001147 dlog_verbose("%s: reverting fragment %lx size %zx\n",
J-Alves063ad832023-10-03 18:05:40 +01001148 __func__, pa_addr(pa_begin), size);
1149
1150 if (constituent == end) {
1151 /*
1152 * The last constituent is expected to be in the
1153 * last fragment.
1154 */
1155 assert(i == fragment_count - 1);
1156 break;
1157 }
1158
1159 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1160 }
1161 }
1162}
1163
1164/**
Jose Marinho09b1db82019-08-08 09:16:59 +01001165 * Updates a VM's page table such that the given set of physical address ranges
1166 * are mapped in the address space at the corresponding address ranges, in the
1167 * mode provided.
1168 *
J-Alves0a83dc22023-05-05 09:50:37 +01001169 * The enum ffa_map_action determines the action taken from a call to the
1170 * function below:
1171 * - If action is MAP_ACTION_CHECK, the page tables will be allocated from the
1172 * mpool but no mappings will actually be updated. This function must always
1173 * be called first with action set to MAP_ACTION_CHECK to check that it will
1174 * succeed before calling ffa_region_group_identity_map with whichever one of
1175 * the remaining actions, to avoid leaving the page table in a half-updated
1176 * state.
1177 * - The action MAP_ACTION_COMMIT allocates the page tables from the mpool, and
1178 * changes the memory mappings.
J-Alvescf6253e2024-01-03 13:48:48 +00001179 * - The action MAP_ACTION_CHECK_PROTECT extends the MAP_ACTION_CHECK with an
1180 * invocation to the monitor to update the security state of the memory,
1181 * to that of the SPMC.
1182 * - The action MAP_ACTION_COMMIT_UNPROTECT extends the MAP_ACTION_COMMIT
1183 * with a call into the monitor, to reset the security state of memory
1184 * that has priorly been mapped with the MAP_ACTION_CHECK_PROTECT action.
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001185 * vm_ptable_defrag should always be called after a series of page table
1186 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +01001187 *
J-Alvescf6253e2024-01-03 13:48:48 +00001188 * If all goes well, returns FFA_SUCCESS_32; or FFA_ERROR, with following
1189 * error codes:
1190 * - FFA_INVALID_PARAMETERS: invalid range of memory.
1191 * - FFA_DENIED:
1192 *
Jose Marinho09b1db82019-08-08 09:16:59 +01001193 * made to memory mappings.
1194 */
J-Alvescf6253e2024-01-03 13:48:48 +00001195struct ffa_value ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +00001196 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001197 struct ffa_memory_region_constituent **fragments,
1198 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alvescf6253e2024-01-03 13:48:48 +00001199 uint32_t mode, struct mpool *ppool, enum ffa_map_action action,
1200 bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001201{
Andrew Walbranca808b12020-05-15 17:22:28 +01001202 uint32_t i;
1203 uint32_t j;
J-Alvescf6253e2024-01-03 13:48:48 +00001204 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001205
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001206 if (vm_locked.vm->el0_partition) {
1207 mode |= MM_MODE_USER | MM_MODE_NG;
1208 }
1209
Andrew Walbranca808b12020-05-15 17:22:28 +01001210 /* Iterate over the memory region constituents within each fragment. */
1211 for (i = 0; i < fragment_count; ++i) {
1212 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
J-Alves063ad832023-10-03 18:05:40 +01001213 struct ffa_memory_region_constituent *constituent =
1214 &fragments[i][j];
1215 size_t size = constituent->page_count * PAGE_SIZE;
Andrew Walbranca808b12020-05-15 17:22:28 +01001216 paddr_t pa_begin =
J-Alves063ad832023-10-03 18:05:40 +01001217 pa_from_ipa(ipa_init(constituent->address));
Andrew Walbranca808b12020-05-15 17:22:28 +01001218 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001219 uint32_t pa_bits =
1220 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +01001221
1222 /*
1223 * Ensure the requested region falls into system's PA
1224 * range.
1225 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001226 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
1227 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +01001228 dlog_error("Region is outside of PA Range\n");
J-Alvescf6253e2024-01-03 13:48:48 +00001229 return ffa_error(FFA_INVALID_PARAMETERS);
Federico Recanati4fd065d2021-12-13 20:06:23 +01001230 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001231
J-Alvescf6253e2024-01-03 13:48:48 +00001232 if (action <= MAP_ACTION_CHECK_PROTECT) {
1233 ret = ffa_region_group_check_actions(
1234 vm_locked, pa_begin, pa_end, ppool,
1235 mode, action, memory_protected);
J-Alves063ad832023-10-03 18:05:40 +01001236
1237 if (ret.func == FFA_ERROR_32 &&
1238 ffa_error_code(ret) == FFA_DENIED) {
1239 if (memory_protected != NULL) {
1240 assert(!*memory_protected);
1241 }
1242
1243 ffa_region_group_fragments_revert_protect(
1244 fragments,
1245 fragment_constituent_counts,
1246 i + 1, constituent);
1247 break;
1248 }
J-Alvescf6253e2024-01-03 13:48:48 +00001249 } else if (action >= MAP_ACTION_COMMIT &&
1250 action < MAP_ACTION_MAX) {
1251 ffa_region_group_commit_actions(
1252 vm_locked, pa_begin, pa_end, ppool,
1253 mode, action);
1254 ret = (struct ffa_value){
1255 .func = FFA_SUCCESS_32};
1256 } else {
1257 panic("%s: Unknown ffa_map_action.\n",
1258 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001259 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001260 }
1261 }
1262
J-Alvescf6253e2024-01-03 13:48:48 +00001263 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001264}
1265
1266/**
1267 * Clears a region of physical memory by overwriting it with zeros. The data is
1268 * flushed from the cache so the memory has been cleared across the system.
1269 */
J-Alves7db32002021-12-14 14:44:50 +00001270static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
1271 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +01001272{
1273 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +00001274 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +01001275 * global mapping of the whole range. Such an approach will limit
1276 * the changes to stage-1 tables and will allow only local
1277 * invalidation.
1278 */
1279 bool ret;
1280 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +00001281 void *ptr = mm_identity_map(stage1_locked, begin, end,
1282 MM_MODE_W | (extra_mode_attributes &
1283 plat_ffa_other_world_mode()),
1284 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001285 size_t size = pa_difference(begin, end);
1286
1287 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001288 goto fail;
1289 }
1290
1291 memset_s(ptr, size, 0, size);
1292 arch_mm_flush_dcache(ptr, size);
1293 mm_unmap(stage1_locked, begin, end, ppool);
1294
1295 ret = true;
1296 goto out;
1297
1298fail:
1299 ret = false;
1300
1301out:
1302 mm_unlock_stage1(&stage1_locked);
1303
1304 return ret;
1305}
1306
1307/**
1308 * Clears a region of physical memory by overwriting it with zeros. The data is
1309 * flushed from the cache so the memory has been cleared across the system.
1310 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001311static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +00001312 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01001313 struct ffa_memory_region_constituent **fragments,
1314 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1315 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001316{
1317 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +01001318 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +01001319 bool ret = false;
1320
1321 /*
1322 * Create a local pool so any freed memory can't be used by another
1323 * thread. This is to ensure each constituent that is mapped can be
1324 * unmapped again afterwards.
1325 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001326 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001327
Andrew Walbranca808b12020-05-15 17:22:28 +01001328 /* Iterate over the memory region constituents within each fragment. */
1329 for (i = 0; i < fragment_count; ++i) {
1330 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001331
J-Alves8457f932023-10-11 16:41:45 +01001332 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001333 size_t size = fragments[i][j].page_count * PAGE_SIZE;
1334 paddr_t begin =
1335 pa_from_ipa(ipa_init(fragments[i][j].address));
1336 paddr_t end = pa_add(begin, size);
1337
J-Alves7db32002021-12-14 14:44:50 +00001338 if (!clear_memory(begin, end, &local_page_pool,
1339 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001340 /*
1341 * api_clear_memory will defrag on failure, so
1342 * no need to do it here.
1343 */
1344 goto out;
1345 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001346 }
1347 }
1348
Jose Marinho09b1db82019-08-08 09:16:59 +01001349 ret = true;
1350
1351out:
1352 mpool_fini(&local_page_pool);
1353 return ret;
1354}
1355
J-Alves5952d942022-12-22 16:03:00 +00001356static bool is_memory_range_within(ipaddr_t begin, ipaddr_t end,
1357 ipaddr_t in_begin, ipaddr_t in_end)
1358{
1359 return (ipa_addr(begin) >= ipa_addr(in_begin) &&
1360 ipa_addr(begin) < ipa_addr(in_end)) ||
1361 (ipa_addr(end) <= ipa_addr(in_end) &&
1362 ipa_addr(end) > ipa_addr(in_begin));
1363}
1364
1365/**
1366 * Receives a memory range and looks for overlaps with the remainder
1367 * constituents of the memory share/lend/donate operation. Assumes they are
1368 * passed in order to avoid having to loop over all the elements at each call.
1369 * The function only compares the received memory ranges with those that follow
1370 * within the same fragment, and subsequent fragments from the same operation.
1371 */
1372static bool ffa_memory_check_overlap(
1373 struct ffa_memory_region_constituent **fragments,
1374 const uint32_t *fragment_constituent_counts,
1375 const uint32_t fragment_count, const uint32_t current_fragment,
1376 const uint32_t current_constituent)
1377{
1378 uint32_t i = current_fragment;
1379 uint32_t j = current_constituent;
1380 ipaddr_t current_begin = ipa_init(fragments[i][j].address);
1381 const uint32_t current_page_count = fragments[i][j].page_count;
1382 size_t current_size = current_page_count * PAGE_SIZE;
1383 ipaddr_t current_end = ipa_add(current_begin, current_size - 1);
1384
1385 if (current_size == 0 ||
1386 current_size > UINT64_MAX - ipa_addr(current_begin)) {
Karl Meakine8937d92024-03-19 16:04:25 +00001387 dlog_verbose("Invalid page count. Addr: %zx page_count: %x\n",
1388 current_begin.ipa, current_page_count);
J-Alves5952d942022-12-22 16:03:00 +00001389 return false;
1390 }
1391
1392 for (; i < fragment_count; i++) {
1393 j = (i == current_fragment) ? j + 1 : 0;
1394
1395 for (; j < fragment_constituent_counts[i]; j++) {
1396 ipaddr_t begin = ipa_init(fragments[i][j].address);
1397 const uint32_t page_count = fragments[i][j].page_count;
1398 size_t size = page_count * PAGE_SIZE;
1399 ipaddr_t end = ipa_add(begin, size - 1);
1400
1401 if (size == 0 || size > UINT64_MAX - ipa_addr(begin)) {
1402 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00001403 "Invalid page count. Addr: %lx "
J-Alves5952d942022-12-22 16:03:00 +00001404 "page_count: %x\n",
Karl Meakine8937d92024-03-19 16:04:25 +00001405 begin.ipa, page_count);
J-Alves5952d942022-12-22 16:03:00 +00001406 return false;
1407 }
1408
1409 /*
1410 * Check if current ranges is within begin and end, as
1411 * well as the reverse. This should help optimize the
1412 * loop, and reduce the number of iterations.
1413 */
1414 if (is_memory_range_within(begin, end, current_begin,
1415 current_end) ||
1416 is_memory_range_within(current_begin, current_end,
1417 begin, end)) {
1418 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00001419 "Overlapping memory ranges: %#lx - "
1420 "%#lx with %#lx - %#lx\n",
J-Alves5952d942022-12-22 16:03:00 +00001421 ipa_addr(begin), ipa_addr(end),
1422 ipa_addr(current_begin),
1423 ipa_addr(current_end));
1424 return true;
1425 }
1426 }
1427 }
1428
1429 return false;
1430}
1431
Jose Marinho09b1db82019-08-08 09:16:59 +01001432/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001433 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +01001434 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001435 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +01001436 *
1437 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001438 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001439 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +01001440 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001441 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
1442 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001443 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +01001444 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001445 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +01001446 */
Daniel Boulbya76fd912024-02-22 14:22:15 +00001447static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001448 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001449 struct ffa_memory_region_constituent **fragments,
1450 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves8f11cde2022-12-21 16:18:22 +00001451 uint32_t composite_total_page_count, uint32_t share_func,
Daniel Boulbya76fd912024-02-22 14:22:15 +00001452 struct ffa_memory_region *memory_region, struct mpool *page_pool,
1453 uint32_t *orig_from_mode_ret, bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001454{
Andrew Walbranca808b12020-05-15 17:22:28 +01001455 uint32_t i;
J-Alves8f11cde2022-12-21 16:18:22 +00001456 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001457 uint32_t orig_from_mode;
J-Alves460d36c2023-10-12 17:02:15 +01001458 uint32_t clean_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001459 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001460 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001461 struct ffa_value ret;
J-Alves8f11cde2022-12-21 16:18:22 +00001462 uint32_t constituents_total_page_count = 0;
J-Alves460d36c2023-10-12 17:02:15 +01001463 enum ffa_map_action map_action = MAP_ACTION_CHECK;
Daniel Boulbya76fd912024-02-22 14:22:15 +00001464 bool clear = memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Jose Marinho09b1db82019-08-08 09:16:59 +01001465
1466 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001467 * Make sure constituents are properly aligned to a 64-bit boundary. If
1468 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +01001469 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001470 for (i = 0; i < fragment_count; ++i) {
1471 if (!is_aligned(fragments[i], 8)) {
1472 dlog_verbose("Constituents not aligned.\n");
1473 return ffa_error(FFA_INVALID_PARAMETERS);
1474 }
J-Alves8f11cde2022-12-21 16:18:22 +00001475 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
1476 constituents_total_page_count +=
1477 fragments[i][j].page_count;
J-Alves5952d942022-12-22 16:03:00 +00001478 if (ffa_memory_check_overlap(
1479 fragments, fragment_constituent_counts,
1480 fragment_count, i, j)) {
1481 return ffa_error(FFA_INVALID_PARAMETERS);
1482 }
J-Alves8f11cde2022-12-21 16:18:22 +00001483 }
1484 }
1485
1486 if (constituents_total_page_count != composite_total_page_count) {
1487 dlog_verbose(
1488 "Composite page count differs from calculated page "
1489 "count from constituents.\n");
1490 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +01001491 }
1492
1493 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001494 * Check if the state transition is lawful for the sender, ensure that
1495 * all constituents of a memory region being shared are at the same
1496 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +01001497 */
J-Alves460d36c2023-10-12 17:02:15 +01001498 ret = ffa_send_check_transition(
Daniel Boulbya76fd912024-02-22 14:22:15 +00001499 from_locked, share_func, memory_region, &orig_from_mode,
1500 fragments, fragment_constituent_counts, fragment_count,
Daniel Boulby4b846eb2024-05-23 17:32:23 +01001501 &from_mode, &map_action, clear);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001502 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001503 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001504 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001505 }
1506
Andrew Walbran37c574e2020-06-03 11:45:46 +01001507 if (orig_from_mode_ret != NULL) {
1508 *orig_from_mode_ret = orig_from_mode;
1509 }
1510
Jose Marinho09b1db82019-08-08 09:16:59 +01001511 /*
1512 * Create a local pool so any freed memory can't be used by another
1513 * thread. This is to ensure the original mapping can be restored if the
1514 * clear fails.
1515 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001516 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001517
1518 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001519 * First reserve all required memory for the new page table entries
1520 * without committing, to make sure the entire operation will succeed
1521 * without exhausting the page pool.
J-Alves460d36c2023-10-12 17:02:15 +01001522 * Provide the map_action as populated by 'ffa_send_check_transition'.
1523 * It may request memory to be protected.
Jose Marinho09b1db82019-08-08 09:16:59 +01001524 */
J-Alvescf6253e2024-01-03 13:48:48 +00001525 ret = ffa_region_group_identity_map(
1526 from_locked, fragments, fragment_constituent_counts,
J-Alves460d36c2023-10-12 17:02:15 +01001527 fragment_count, from_mode, page_pool, map_action,
1528 memory_protected);
J-Alvescf6253e2024-01-03 13:48:48 +00001529 if (ret.func == FFA_ERROR_32) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001530 goto out;
1531 }
1532
1533 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001534 * Update the mapping for the sender. This won't allocate because the
1535 * transaction was already prepared above, but may free pages in the
1536 * case that a whole block is being unmapped that was previously
1537 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +01001538 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001539 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001540 from_locked, fragments, fragment_constituent_counts,
1541 fragment_count, from_mode, &local_page_pool,
1542 MAP_ACTION_COMMIT, NULL)
1543 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001544
J-Alves460d36c2023-10-12 17:02:15 +01001545 /*
1546 * If memory has been protected, it is now part of the secure PAS
1547 * (happens for lend/donate from NWd to SWd), and the `orig_from_mode`
1548 * should have the MM_MODE_NS set, as such mask it in `clean_mode` for
1549 * SPM's S1 translation.
1550 * In case memory hasn't been protected, and it is in the non-secure
1551 * PAS (e.g. memory share from NWd to SWd), as such the SPM needs to
1552 * perform a non-secure memory access. In such case `clean_mode` takes
1553 * the same mode as `orig_from_mode`.
1554 */
1555 clean_mode = (memory_protected != NULL && *memory_protected)
1556 ? orig_from_mode & ~plat_ffa_other_world_mode()
1557 : orig_from_mode;
1558
Jose Marinho09b1db82019-08-08 09:16:59 +01001559 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves460d36c2023-10-12 17:02:15 +01001560 if (clear && !ffa_clear_memory_constituents(
1561 clean_mode, fragments, fragment_constituent_counts,
1562 fragment_count, page_pool)) {
1563 map_action = (memory_protected != NULL && *memory_protected)
1564 ? MAP_ACTION_COMMIT_UNPROTECT
1565 : MAP_ACTION_COMMIT;
1566
Jose Marinho09b1db82019-08-08 09:16:59 +01001567 /*
1568 * On failure, roll back by returning memory to the sender. This
1569 * may allocate pages which were previously freed into
1570 * `local_page_pool` by the call above, but will never allocate
1571 * more pages than that so can never fail.
1572 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001573 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001574 from_locked, fragments,
1575 fragment_constituent_counts, fragment_count,
1576 orig_from_mode, &local_page_pool,
1577 MAP_ACTION_COMMIT, NULL)
1578 .func == FFA_SUCCESS_32);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001579 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +01001580 goto out;
1581 }
1582
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001583 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001584
1585out:
1586 mpool_fini(&local_page_pool);
1587
1588 /*
1589 * Tidy up the page table by reclaiming failed mappings (if there was an
1590 * error) or merging entries into blocks where possible (on success).
1591 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001592 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001593
1594 return ret;
1595}
1596
1597/**
1598 * Validates and maps memory shared from one VM to another.
1599 *
1600 * This function requires the calling context to hold the <to> lock.
1601 *
1602 * Returns:
1603 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001604 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001605 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001606 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001607 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001608 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001609 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001610struct ffa_value ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01001611 struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001612 struct ffa_memory_region_constituent **fragments,
1613 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves26483382023-04-20 12:01:49 +01001614 uint32_t sender_orig_mode, uint32_t share_func, bool clear,
J-Alves460d36c2023-10-12 17:02:15 +01001615 struct mpool *page_pool, uint32_t *response_mode, bool memory_protected)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001616{
Andrew Walbranca808b12020-05-15 17:22:28 +01001617 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001618 uint32_t to_mode;
1619 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001620 struct ffa_value ret;
J-Alvesfd206052023-05-22 16:45:00 +01001621 enum ffa_map_action map_action = MAP_ACTION_COMMIT;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001622
1623 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001624 * Make sure constituents are properly aligned to a 64-bit boundary. If
1625 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001626 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001627 for (i = 0; i < fragment_count; ++i) {
1628 if (!is_aligned(fragments[i], 8)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001629 dlog_verbose("Fragment not properly aligned.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +01001630 return ffa_error(FFA_INVALID_PARAMETERS);
1631 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001632 }
1633
1634 /*
Daniel Boulby4b846eb2024-05-23 17:32:23 +01001635 * Ensure the sender has write permissions if the memory needs to be
1636 * cleared.
1637 */
1638 if ((sender_orig_mode & MM_MODE_W) == 0 && clear == true) {
1639 dlog_verbose(
1640 "Cannot zero memory when the sender does not have "
1641 "write access\n");
1642 return ffa_error(FFA_DENIED);
1643 }
1644
1645 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001646 * Check if the state transition is lawful for the recipient, and ensure
1647 * that all constituents of the memory region being retrieved are at the
1648 * same state.
1649 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001650 ret = ffa_retrieve_check_transition(
1651 to_locked, share_func, fragments, fragment_constituent_counts,
J-Alvesfd206052023-05-22 16:45:00 +01001652 fragment_count, sender_orig_mode, &to_mode, memory_protected,
1653 &map_action);
J-Alves460d36c2023-10-12 17:02:15 +01001654
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001655 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001656 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001657 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001658 }
1659
1660 /*
J-Alves69cdfd92024-04-26 11:40:59 +01001661 * Create a local pool so any freed memory can't be used by
1662 * another thread. This is to ensure the original mapping can be
1663 * restored if the clear fails.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001664 */
1665 mpool_init_with_fallback(&local_page_pool, page_pool);
1666
1667 /*
J-Alves69cdfd92024-04-26 11:40:59 +01001668 * Memory retrieves from the NWd VMs don't require update to S2 PTs on
1669 * retrieve request.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001670 */
J-Alves69cdfd92024-04-26 11:40:59 +01001671 if (map_action != MAP_ACTION_NONE) {
1672 /*
1673 * First reserve all required memory for the new page table
1674 * entries in the recipient page tables without committing, to
1675 * make sure the entire operation will succeed without
1676 * exhausting the page pool.
1677 */
1678 ret = ffa_region_group_identity_map(
1679 to_locked, fragments, fragment_constituent_counts,
1680 fragment_count, to_mode, page_pool, MAP_ACTION_CHECK,
1681 NULL);
1682 if (ret.func == FFA_ERROR_32) {
1683 /* TODO: partial defrag of failed range. */
1684 goto out;
1685 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001686 }
1687
1688 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001689 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001690 !ffa_clear_memory_constituents(sender_orig_mode, fragments,
1691 fragment_constituent_counts,
1692 fragment_count, page_pool)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001693 dlog_verbose("Couldn't clear constituents.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001694 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001695 goto out;
1696 }
1697
J-Alves69cdfd92024-04-26 11:40:59 +01001698 if (map_action != MAP_ACTION_NONE) {
1699 /*
1700 * Complete the transfer by mapping the memory into the
1701 * recipient. This won't allocate because the transaction was
1702 * already prepared above, so it doesn't need to use the
1703 * `local_page_pool`.
1704 */
1705 CHECK(ffa_region_group_identity_map(to_locked, fragments,
1706 fragment_constituent_counts,
1707 fragment_count, to_mode,
1708 page_pool, map_action, NULL)
1709 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001710
J-Alves69cdfd92024-04-26 11:40:59 +01001711 /*
1712 * Return the mode used in mapping the memory in retriever's PT.
1713 */
1714 if (response_mode != NULL) {
1715 *response_mode = to_mode;
1716 }
J-Alves460d36c2023-10-12 17:02:15 +01001717 }
1718
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001719 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001720
1721out:
1722 mpool_fini(&local_page_pool);
1723
1724 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001725 * Tidy up the page table by reclaiming failed mappings (if there was an
1726 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001727 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001728 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001729
1730 return ret;
1731}
1732
Andrew Walbran996d1d12020-05-27 14:08:43 +01001733static struct ffa_value ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01001734 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001735 struct ffa_memory_region_constituent **fragments,
1736 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves69cdfd92024-04-26 11:40:59 +01001737 uint32_t sender_orig_mode, struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001738{
1739 uint32_t orig_from_mode;
J-Alves69cdfd92024-04-26 11:40:59 +01001740 uint32_t clearing_mode;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001741 uint32_t from_mode;
1742 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001743 struct ffa_value ret;
J-Alves69cdfd92024-04-26 11:40:59 +01001744 enum ffa_map_action map_action;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001745
Andrew Walbranca808b12020-05-15 17:22:28 +01001746 ret = ffa_relinquish_check_transition(
1747 from_locked, &orig_from_mode, fragments,
J-Alves69cdfd92024-04-26 11:40:59 +01001748 fragment_constituent_counts, fragment_count, &from_mode,
1749 &map_action);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001750 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001751 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001752 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001753 }
1754
1755 /*
1756 * Create a local pool so any freed memory can't be used by another
1757 * thread. This is to ensure the original mapping can be restored if the
1758 * clear fails.
1759 */
1760 mpool_init_with_fallback(&local_page_pool, page_pool);
1761
J-Alves69cdfd92024-04-26 11:40:59 +01001762 if (map_action != MAP_ACTION_NONE) {
1763 clearing_mode = orig_from_mode;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001764
J-Alves69cdfd92024-04-26 11:40:59 +01001765 /*
1766 * First reserve all required memory for the new page table
1767 * entries without committing, to make sure the entire operation
1768 * will succeed without exhausting the page pool.
1769 */
1770 ret = ffa_region_group_identity_map(
1771 from_locked, fragments, fragment_constituent_counts,
1772 fragment_count, from_mode, page_pool, MAP_ACTION_CHECK,
1773 NULL);
1774 if (ret.func == FFA_ERROR_32) {
1775 goto out;
1776 }
1777
1778 /*
1779 * Update the mapping for the sender. This won't allocate
1780 * because the transaction was already prepared above, but may
1781 * free pages in the case that a whole block is being unmapped
1782 * that was previously partially mapped.
1783 */
1784 CHECK(ffa_region_group_identity_map(from_locked, fragments,
1785 fragment_constituent_counts,
1786 fragment_count, from_mode,
1787 &local_page_pool,
1788 MAP_ACTION_COMMIT, NULL)
1789 .func == FFA_SUCCESS_32);
1790 } else {
1791 /*
1792 * If the `map_action` is set to `MAP_ACTION_NONE`, S2 PTs
1793 * were not updated on retrieve/relinquish. These were updating
1794 * only the `share_state` structures. As such, use the sender's
1795 * original mode.
1796 */
1797 clearing_mode = sender_orig_mode;
1798 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001799
1800 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001801 if (clear &&
J-Alves69cdfd92024-04-26 11:40:59 +01001802 !ffa_clear_memory_constituents(clearing_mode, fragments,
J-Alves26483382023-04-20 12:01:49 +01001803 fragment_constituent_counts,
1804 fragment_count, page_pool)) {
J-Alves69cdfd92024-04-26 11:40:59 +01001805 if (map_action != MAP_ACTION_NONE) {
1806 /*
1807 * On failure, roll back by returning memory to the
1808 * sender. This may allocate pages which were previously
1809 * freed into `local_page_pool` by the call above, but
1810 * will never allocate more pages than that so can never
1811 * fail.
1812 */
1813 CHECK(ffa_region_group_identity_map(
1814 from_locked, fragments,
1815 fragment_constituent_counts,
1816 fragment_count, orig_from_mode,
1817 &local_page_pool, MAP_ACTION_COMMIT, NULL)
1818 .func == FFA_SUCCESS_32);
1819 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001820 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001821 goto out;
1822 }
1823
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001824 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001825
1826out:
1827 mpool_fini(&local_page_pool);
1828
1829 /*
1830 * Tidy up the page table by reclaiming failed mappings (if there was an
1831 * error) or merging entries into blocks where possible (on success).
1832 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001833 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001834
1835 return ret;
1836}
1837
1838/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001839 * Complete a memory sending operation by checking that it is valid, updating
1840 * the sender page table, and then either marking the share state as having
1841 * completed sending (on success) or freeing it (on failure).
1842 *
1843 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1844 */
J-Alvesfdd29272022-07-19 13:16:31 +01001845struct ffa_value ffa_memory_send_complete(
Andrew Walbranca808b12020-05-15 17:22:28 +01001846 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001847 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1848 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001849{
1850 struct ffa_memory_region *memory_region = share_state->memory_region;
J-Alves8f11cde2022-12-21 16:18:22 +00001851 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01001852 struct ffa_value ret;
1853
1854 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001855 assert(share_states.share_states != NULL);
J-Alves8f11cde2022-12-21 16:18:22 +00001856 assert(memory_region != NULL);
1857 composite = ffa_memory_region_get_composite(memory_region, 0);
1858 assert(composite != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001859
1860 /* Check that state is valid in sender page table and update. */
1861 ret = ffa_send_check_update(
1862 from_locked, share_state->fragments,
1863 share_state->fragment_constituent_counts,
J-Alves8f11cde2022-12-21 16:18:22 +00001864 share_state->fragment_count, composite->page_count,
Daniel Boulbya76fd912024-02-22 14:22:15 +00001865 share_state->share_func, memory_region, page_pool,
J-Alves460d36c2023-10-12 17:02:15 +01001866 orig_from_mode_ret, &share_state->memory_protected);
Andrew Walbranca808b12020-05-15 17:22:28 +01001867 if (ret.func != FFA_SUCCESS_32) {
1868 /*
1869 * Free share state, it failed to send so it can't be retrieved.
1870 */
Karl Meakin4cec5e82023-06-30 16:30:22 +01001871 dlog_verbose("%s: failed to send check update: %s(%s)\n",
1872 __func__, ffa_func_name(ret.func),
1873 ffa_error_name(ffa_error_code(ret)));
Andrew Walbranca808b12020-05-15 17:22:28 +01001874 share_state_free(share_states, share_state, page_pool);
1875 return ret;
1876 }
1877
1878 share_state->sending_complete = true;
Karl Meakin4cec5e82023-06-30 16:30:22 +01001879 dlog_verbose("%s: marked sending complete.\n", __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001880
J-Alvesee68c542020-10-29 17:48:20 +00001881 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001882}
1883
1884/**
Daniel Boulby9764ff62024-01-30 17:47:39 +00001885 * Check that the memory attributes match Hafnium expectations.
1886 * Cacheability:
1887 * - Normal Memory as `FFA_MEMORY_CACHE_WRITE_BACK`.
1888 * - Device memory as `FFA_MEMORY_DEV_NGNRNE`.
1889 *
1890 * Shareability:
1891 * - Inner Shareable.
Federico Recanatia98603a2021-12-20 18:04:03 +01001892 */
1893static struct ffa_value ffa_memory_attributes_validate(
J-Alves7a99d0d2023-02-08 13:49:48 +00001894 ffa_memory_attributes_t attributes)
Federico Recanatia98603a2021-12-20 18:04:03 +01001895{
1896 enum ffa_memory_type memory_type;
1897 enum ffa_memory_cacheability cacheability;
1898 enum ffa_memory_shareability shareability;
1899
Karl Meakin84710f32023-10-12 15:14:49 +01001900 memory_type = attributes.type;
Daniel Boulby9764ff62024-01-30 17:47:39 +00001901 cacheability = attributes.cacheability;
1902 if (memory_type == FFA_MEMORY_NORMAL_MEM &&
1903 cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1904 dlog_verbose(
1905 "Normal Memory: Invalid cacheability %s, "
1906 "expected %s.\n",
1907 ffa_memory_cacheability_name(cacheability),
1908 ffa_memory_cacheability_name(
1909 FFA_MEMORY_CACHE_WRITE_BACK));
Federico Recanati3d953f32022-02-17 09:31:29 +01001910 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001911 }
Daniel Boulby9764ff62024-01-30 17:47:39 +00001912 if (memory_type == FFA_MEMORY_DEVICE_MEM &&
1913 cacheability != FFA_MEMORY_DEV_NGNRNE) {
1914 dlog_verbose(
1915 "Device Memory: Invalid cacheability %s, "
1916 "expected %s.\n",
1917 ffa_device_memory_cacheability_name(cacheability),
1918 ffa_device_memory_cacheability_name(
1919 FFA_MEMORY_DEV_NGNRNE));
Federico Recanati3d953f32022-02-17 09:31:29 +01001920 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001921 }
1922
Karl Meakin84710f32023-10-12 15:14:49 +01001923 shareability = attributes.shareability;
Federico Recanatia98603a2021-12-20 18:04:03 +01001924 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
Karl Meakinf98b2aa2023-10-12 16:09:59 +01001925 dlog_verbose("Invalid shareability %s, expected %s.\n",
1926 ffa_memory_shareability_name(shareability),
1927 ffa_memory_shareability_name(
1928 FFA_MEMORY_INNER_SHAREABLE));
Federico Recanati3d953f32022-02-17 09:31:29 +01001929 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001930 }
1931
1932 return (struct ffa_value){.func = FFA_SUCCESS_32};
1933}
1934
1935/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001936 * Check that the given `memory_region` represents a valid memory send request
1937 * of the given `share_func` type, return the clear flag and permissions via the
1938 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001939 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001940 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001941 * not.
1942 */
J-Alves66652252022-07-06 09:49:51 +01001943struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001944 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1945 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001946 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001947{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001948 struct ffa_composite_memory_region *composite;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001949 struct ffa_memory_access *receiver =
1950 ffa_memory_region_get_receiver(memory_region, 0);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001951 uint64_t receivers_end;
1952 uint64_t min_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001953 uint32_t composite_memory_region_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001954 uint32_t constituents_start;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001955 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001956 enum ffa_data_access data_access;
1957 enum ffa_instruction_access instruction_access;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001958 enum ffa_memory_security security_state;
Karl Meakinf98b2aa2023-10-12 16:09:59 +01001959 enum ffa_memory_type type;
Federico Recanatia98603a2021-12-20 18:04:03 +01001960 struct ffa_value ret;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001961 const size_t minimum_first_fragment_length =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001962 memory_region->receivers_offset +
1963 memory_region->memory_access_desc_size +
1964 sizeof(struct ffa_composite_memory_region);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001965
1966 if (fragment_length < minimum_first_fragment_length) {
Karl Meakine8937d92024-03-19 16:04:25 +00001967 dlog_verbose("Fragment length %u too short (min %zu).\n",
1968 fragment_length, minimum_first_fragment_length);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001969 return ffa_error(FFA_INVALID_PARAMETERS);
1970 }
1971
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05001972 static_assert(sizeof(struct ffa_memory_region_constituent) == 16,
1973 "struct ffa_memory_region_constituent must be 16 bytes");
1974 if (!is_aligned(fragment_length,
1975 sizeof(struct ffa_memory_region_constituent)) ||
1976 !is_aligned(memory_share_length,
1977 sizeof(struct ffa_memory_region_constituent))) {
1978 dlog_verbose(
1979 "Fragment length %u or total length %u"
1980 " is not 16-byte aligned.\n",
1981 fragment_length, memory_share_length);
1982 return ffa_error(FFA_INVALID_PARAMETERS);
1983 }
1984
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001985 if (fragment_length > memory_share_length) {
1986 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00001987 "Fragment length %zu greater than total length %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001988 (size_t)fragment_length, (size_t)memory_share_length);
1989 return ffa_error(FFA_INVALID_PARAMETERS);
1990 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001991
J-Alves95df0ef2022-12-07 10:09:48 +00001992 /* The sender must match the caller. */
1993 if ((!vm_id_is_current_world(from_locked.vm->id) &&
1994 vm_id_is_current_world(memory_region->sender)) ||
1995 (vm_id_is_current_world(from_locked.vm->id) &&
1996 memory_region->sender != from_locked.vm->id)) {
1997 dlog_verbose("Invalid memory sender ID.\n");
1998 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001999 }
2000
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002001 if (memory_region->receiver_count <= 0) {
2002 dlog_verbose("No receivers!\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002003 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002004 }
2005
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002006 /*
2007 * Ensure that the composite header is within the memory bounds and
2008 * doesn't overlap the first part of the message. Cast to uint64_t
2009 * to prevent overflow.
2010 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002011 receivers_end = ((uint64_t)memory_region->memory_access_desc_size *
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002012 (uint64_t)memory_region->receiver_count) +
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01002013 memory_region->receivers_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002014 min_length = receivers_end +
2015 sizeof(struct ffa_composite_memory_region) +
2016 sizeof(struct ffa_memory_region_constituent);
2017 if (min_length > memory_share_length) {
Karl Meakine8937d92024-03-19 16:04:25 +00002018 dlog_verbose("Share too short: got %zu but minimum is %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002019 (size_t)memory_share_length, (size_t)min_length);
2020 return ffa_error(FFA_INVALID_PARAMETERS);
2021 }
2022
2023 composite_memory_region_offset =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002024 receiver->composite_memory_region_offset;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002025
2026 /*
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002027 * Check that the composite memory region descriptor is after the access
2028 * descriptors, is at least 16-byte aligned, and fits in the first
2029 * fragment.
Andrew Walbrana65a1322020-04-06 19:32:32 +01002030 */
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002031 if ((composite_memory_region_offset < receivers_end) ||
2032 (composite_memory_region_offset % 16 != 0) ||
2033 (composite_memory_region_offset >
2034 fragment_length - sizeof(struct ffa_composite_memory_region))) {
2035 dlog_verbose(
2036 "Invalid composite memory region descriptor offset "
Karl Meakine8937d92024-03-19 16:04:25 +00002037 "%zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002038 (size_t)composite_memory_region_offset);
2039 return ffa_error(FFA_INVALID_PARAMETERS);
2040 }
2041
2042 /*
2043 * Compute the start of the constituent regions. Already checked
2044 * to be not more than fragment_length and thus not more than
2045 * memory_share_length.
2046 */
2047 constituents_start = composite_memory_region_offset +
2048 sizeof(struct ffa_composite_memory_region);
2049 constituents_length = memory_share_length - constituents_start;
2050
2051 /*
2052 * Check that the number of constituents is consistent with the length
2053 * of the constituent region.
2054 */
2055 composite = ffa_memory_region_get_composite(memory_region, 0);
2056 if ((constituents_length %
2057 sizeof(struct ffa_memory_region_constituent) !=
2058 0) ||
2059 ((constituents_length /
2060 sizeof(struct ffa_memory_region_constituent)) !=
2061 composite->constituent_count)) {
Karl Meakine8937d92024-03-19 16:04:25 +00002062 dlog_verbose("Invalid length %zu or composite offset %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002063 (size_t)memory_share_length,
2064 (size_t)composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002065 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002066 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002067 if (fragment_length < memory_share_length &&
2068 fragment_length < HF_MAILBOX_SIZE) {
2069 dlog_warning(
2070 "Initial fragment length %d smaller than mailbox "
2071 "size.\n",
2072 fragment_length);
2073 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002074
Andrew Walbrana65a1322020-04-06 19:32:32 +01002075 /*
2076 * Clear is not allowed for memory sharing, as the sender still has
2077 * access to the memory.
2078 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002079 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
J-Alves95fbb312024-03-20 15:19:16 +00002080 (share_func == FFA_MEM_SHARE_32 ||
2081 share_func == FFA_MEM_SHARE_64)) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01002082 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002083 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002084 }
2085
2086 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002087 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01002088 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002089 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002090 }
2091
J-Alves363f5722022-04-25 17:37:37 +01002092 /* Check that the permissions are valid, for each specified receiver. */
2093 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002094 struct ffa_memory_region_attributes receiver_permissions;
2095
2096 receiver = ffa_memory_region_get_receiver(memory_region, i);
2097 assert(receiver != NULL);
2098 receiver_permissions = receiver->receiver_permissions;
J-Alves363f5722022-04-25 17:37:37 +01002099 ffa_memory_access_permissions_t permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002100 receiver_permissions.permissions;
2101 ffa_id_t receiver_id = receiver_permissions.receiver;
J-Alves363f5722022-04-25 17:37:37 +01002102
2103 if (memory_region->sender == receiver_id) {
2104 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002105 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002106 }
Federico Recanati85090c42021-12-15 13:17:54 +01002107
J-Alves363f5722022-04-25 17:37:37 +01002108 for (uint32_t j = i + 1; j < memory_region->receiver_count;
2109 j++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002110 struct ffa_memory_access *other_receiver =
2111 ffa_memory_region_get_receiver(memory_region,
2112 j);
2113 assert(other_receiver != NULL);
2114
J-Alves363f5722022-04-25 17:37:37 +01002115 if (receiver_id ==
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002116 other_receiver->receiver_permissions.receiver) {
J-Alves363f5722022-04-25 17:37:37 +01002117 dlog_verbose(
2118 "Repeated receiver(%x) in memory send "
2119 "operation.\n",
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002120 other_receiver->receiver_permissions
2121 .receiver);
J-Alves363f5722022-04-25 17:37:37 +01002122 return ffa_error(FFA_INVALID_PARAMETERS);
2123 }
2124 }
2125
2126 if (composite_memory_region_offset !=
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002127 receiver->composite_memory_region_offset) {
J-Alves363f5722022-04-25 17:37:37 +01002128 dlog_verbose(
2129 "All ffa_memory_access should point to the "
2130 "same composite memory region offset.\n");
2131 return ffa_error(FFA_INVALID_PARAMETERS);
2132 }
2133
Karl Meakin84710f32023-10-12 15:14:49 +01002134 data_access = permissions.data_access;
2135 instruction_access = permissions.instruction_access;
J-Alves363f5722022-04-25 17:37:37 +01002136 if (data_access == FFA_DATA_ACCESS_RESERVED ||
2137 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
2138 dlog_verbose(
2139 "Reserved value for receiver permissions "
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002140 "(data_access = %s, instruction_access = %s)\n",
2141 ffa_data_access_name(data_access),
2142 ffa_instruction_access_name(
2143 instruction_access));
J-Alves363f5722022-04-25 17:37:37 +01002144 return ffa_error(FFA_INVALID_PARAMETERS);
2145 }
2146 if (instruction_access !=
2147 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2148 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002149 "Invalid instruction access permissions %s "
2150 "for sending memory, expected %s.\n",
2151 ffa_instruction_access_name(instruction_access),
2152 ffa_instruction_access_name(
Daniel Boulby91052c32024-05-21 14:09:48 +01002153 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002154 return ffa_error(FFA_INVALID_PARAMETERS);
2155 }
J-Alves95fbb312024-03-20 15:19:16 +00002156 if (share_func == FFA_MEM_SHARE_32 ||
2157 share_func == FFA_MEM_SHARE_64) {
J-Alves363f5722022-04-25 17:37:37 +01002158 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
2159 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002160 "Invalid data access permissions %s "
2161 "for sharing memory, expected %s.\n",
2162 ffa_data_access_name(data_access),
2163 ffa_data_access_name(
2164 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002165 return ffa_error(FFA_INVALID_PARAMETERS);
2166 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002167 /*
2168 * According to section 10.10.3 of the FF-A v1.1 EAC0
2169 * spec, NX is required for share operations (but must
2170 * not be specified by the sender) so set it in the
2171 * copy that we store, ready to be returned to the
2172 * retriever.
2173 */
2174 if (vm_id_is_current_world(receiver_id)) {
Karl Meakin84710f32023-10-12 15:14:49 +01002175 permissions.instruction_access =
2176 FFA_INSTRUCTION_ACCESS_NX;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002177 receiver_permissions.permissions = permissions;
2178 }
J-Alves363f5722022-04-25 17:37:37 +01002179 }
J-Alves95fbb312024-03-20 15:19:16 +00002180 if ((share_func == FFA_MEM_LEND_32 ||
2181 share_func == FFA_MEM_LEND_64) &&
J-Alves363f5722022-04-25 17:37:37 +01002182 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
2183 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002184 "Invalid data access permissions %s for "
2185 "lending memory, expected %s.\n",
2186 ffa_data_access_name(data_access),
2187 ffa_data_access_name(
2188 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002189 return ffa_error(FFA_INVALID_PARAMETERS);
2190 }
2191
J-Alves95fbb312024-03-20 15:19:16 +00002192 if ((share_func == FFA_MEM_DONATE_32 ||
2193 share_func == FFA_MEM_DONATE_64) &&
J-Alves363f5722022-04-25 17:37:37 +01002194 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
2195 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002196 "Invalid data access permissions %s for "
2197 "donating memory, expected %s.\n",
2198 ffa_data_access_name(data_access),
2199 ffa_data_access_name(
2200 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002201 return ffa_error(FFA_INVALID_PARAMETERS);
2202 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002203 }
2204
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002205 /* Memory region attributes NS-Bit MBZ for FFA_MEM_SHARE/LEND/DONATE. */
Karl Meakin84710f32023-10-12 15:14:49 +01002206 security_state = memory_region->attributes.security;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002207 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
2208 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002209 "Invalid security state %s for memory share operation, "
2210 "expected %s.\n",
2211 ffa_memory_security_name(security_state),
2212 ffa_memory_security_name(
2213 FFA_MEMORY_SECURITY_UNSPECIFIED));
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002214 return ffa_error(FFA_INVALID_PARAMETERS);
2215 }
2216
Federico Recanatid937f5e2021-12-20 17:38:23 +01002217 /*
J-Alves807794e2022-06-16 13:42:47 +01002218 * If a memory donate or lend with single borrower, the memory type
2219 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01002220 */
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002221 type = memory_region->attributes.type;
J-Alves807794e2022-06-16 13:42:47 +01002222 if (share_func == FFA_MEM_DONATE_32 ||
J-Alves95fbb312024-03-20 15:19:16 +00002223 share_func == FFA_MEM_DONATE_64 ||
2224 ((share_func == FFA_MEM_LEND_32 || share_func == FFA_MEM_LEND_64) &&
J-Alves807794e2022-06-16 13:42:47 +01002225 memory_region->receiver_count == 1)) {
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002226 if (type != FFA_MEMORY_NOT_SPECIFIED_MEM) {
J-Alves807794e2022-06-16 13:42:47 +01002227 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002228 "Invalid memory type %s for memory share "
2229 "operation, expected %s.\n",
2230 ffa_memory_type_name(type),
2231 ffa_memory_type_name(
2232 FFA_MEMORY_NOT_SPECIFIED_MEM));
J-Alves807794e2022-06-16 13:42:47 +01002233 return ffa_error(FFA_INVALID_PARAMETERS);
2234 }
2235 } else {
2236 /*
2237 * Check that sender's memory attributes match Hafnium
2238 * expectations: Normal Memory, Inner shareable, Write-Back
2239 * Read-Allocate Write-Allocate Cacheable.
2240 */
2241 ret = ffa_memory_attributes_validate(memory_region->attributes);
2242 if (ret.func != FFA_SUCCESS_32) {
2243 return ret;
2244 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01002245 }
2246
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002247 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01002248}
2249
2250/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002251 * Gets the share state for continuing an operation to donate, lend or share
2252 * memory, and checks that it is a valid request.
2253 *
2254 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
2255 * not.
2256 */
J-Alvesfdd29272022-07-19 13:16:31 +01002257struct ffa_value ffa_memory_send_continue_validate(
Andrew Walbranca808b12020-05-15 17:22:28 +01002258 struct share_states_locked share_states, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01002259 struct ffa_memory_share_state **share_state_ret, ffa_id_t from_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01002260 struct mpool *page_pool)
2261{
2262 struct ffa_memory_share_state *share_state;
2263 struct ffa_memory_region *memory_region;
2264
Daniel Boulbya2f8c662021-11-26 17:52:53 +00002265 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01002266
2267 /*
2268 * Look up the share state by handle and make sure that the VM ID
2269 * matches.
2270 */
Karl Meakin4a2854a2023-06-30 16:26:52 +01002271 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00002272 if (share_state == NULL) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002273 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00002274 "Invalid handle %#lx for memory send continuation.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01002275 handle);
2276 return ffa_error(FFA_INVALID_PARAMETERS);
2277 }
2278 memory_region = share_state->memory_region;
2279
J-Alvesfdd29272022-07-19 13:16:31 +01002280 if (vm_id_is_current_world(from_vm_id) &&
2281 memory_region->sender != from_vm_id) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002282 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
2283 return ffa_error(FFA_INVALID_PARAMETERS);
2284 }
2285
2286 if (share_state->sending_complete) {
2287 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00002288 "Sending of memory handle %#lx is already complete.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01002289 handle);
2290 return ffa_error(FFA_INVALID_PARAMETERS);
2291 }
2292
2293 if (share_state->fragment_count == MAX_FRAGMENTS) {
2294 /*
2295 * Log a warning as this is a sign that MAX_FRAGMENTS should
2296 * probably be increased.
2297 */
2298 dlog_warning(
Karl Meakine8937d92024-03-19 16:04:25 +00002299 "Too many fragments for memory share with handle %#lx; "
Andrew Walbranca808b12020-05-15 17:22:28 +01002300 "only %d supported.\n",
2301 handle, MAX_FRAGMENTS);
2302 /* Free share state, as it's not possible to complete it. */
2303 share_state_free(share_states, share_state, page_pool);
2304 return ffa_error(FFA_NO_MEMORY);
2305 }
2306
2307 *share_state_ret = share_state;
2308
2309 return (struct ffa_value){.func = FFA_SUCCESS_32};
2310}
2311
2312/**
J-Alves95df0ef2022-12-07 10:09:48 +00002313 * Checks if there is at least one receiver from the other world.
2314 */
J-Alvesfdd29272022-07-19 13:16:31 +01002315bool memory_region_receivers_from_other_world(
J-Alves95df0ef2022-12-07 10:09:48 +00002316 struct ffa_memory_region *memory_region)
2317{
2318 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002319 struct ffa_memory_access *receiver =
2320 ffa_memory_region_get_receiver(memory_region, i);
2321 assert(receiver != NULL);
2322 ffa_id_t receiver_id = receiver->receiver_permissions.receiver;
2323
2324 if (!vm_id_is_current_world(receiver_id)) {
J-Alves95df0ef2022-12-07 10:09:48 +00002325 return true;
2326 }
2327 }
2328 return false;
2329}
2330
2331/**
J-Alves9da280b2022-12-21 14:55:39 +00002332 * Validates a call to donate, lend or share memory in which Hafnium is the
2333 * designated allocator of the memory handle. In practice, this also means
2334 * Hafnium is responsible for managing the state structures for the transaction.
2335 * If Hafnium is the SPMC, it should allocate the memory handle when either the
2336 * sender is an SP or there is at least one borrower that is an SP.
2337 * If Hafnium is the hypervisor, it should allocate the memory handle when
2338 * operation involves only NWd VMs.
2339 *
2340 * If validation goes well, Hafnium updates the stage-2 page tables of the
2341 * sender. Validation consists of checking if the message length and number of
2342 * memory region constituents match, and if the transition is valid for the
2343 * type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00002344 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002345 * Assumes that the caller has already found and locked the sender VM and copied
2346 * the memory region descriptor from the sender's TX buffer to a freshly
2347 * allocated page from Hafnium's internal pool. The caller must have also
2348 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002349 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002350 * This function takes ownership of the `memory_region` passed in and will free
2351 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01002352 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002353struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002354 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002355 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002356 uint32_t fragment_length, uint32_t share_func,
2357 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01002358{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002359 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002360 struct share_states_locked share_states;
2361 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01002362
2363 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01002364 * If there is an error validating the `memory_region` then we need to
2365 * free it because we own it but we won't be storing it in a share state
2366 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01002367 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002368 ret = ffa_memory_send_validate(from_locked, memory_region,
2369 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01002370 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002371 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002372 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002373 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002374 }
2375
Andrew Walbrana65a1322020-04-06 19:32:32 +01002376 /* Set flag for share function, ready to be retrieved later. */
2377 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00002378 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002379 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002380 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002381 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002382 break;
J-Alves95fbb312024-03-20 15:19:16 +00002383 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002384 case FFA_MEM_LEND_32:
2385 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002386 break;
J-Alves95fbb312024-03-20 15:19:16 +00002387 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002388 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002389 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002390 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002391 break;
Karl Meakina5ea9092024-05-28 15:40:33 +01002392 default:
2393 dlog_verbose("Unknown share func %#x (%s)\n", share_func,
2394 ffa_func_name(share_func));
2395 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +01002396 }
2397
Andrew Walbranca808b12020-05-15 17:22:28 +01002398 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002399 /*
2400 * Allocate a share state before updating the page table. Otherwise if
2401 * updating the page table succeeded but allocating the share state
2402 * failed then it would leave the memory in a state where nobody could
2403 * get it back.
2404 */
Karl Meakin52cdfe72023-06-30 14:49:10 +01002405 share_state = allocate_share_state(share_states, share_func,
2406 memory_region, fragment_length,
2407 FFA_MEMORY_HANDLE_INVALID);
J-Alvesb56aac82023-11-10 09:44:43 +00002408 if (share_state == NULL) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002409 dlog_verbose("Failed to allocate share state.\n");
2410 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01002411 ret = ffa_error(FFA_NO_MEMORY);
2412 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002413 }
2414
Andrew Walbranca808b12020-05-15 17:22:28 +01002415 if (fragment_length == memory_share_length) {
2416 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00002417 ret = ffa_memory_send_complete(
2418 from_locked, share_states, share_state, page_pool,
2419 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002420 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01002421 /*
2422 * Use sender ID from 'memory_region' assuming
2423 * that at this point it has been validated:
2424 * - MBZ at virtual FF-A instance.
2425 */
J-Alves19e20cf2023-08-02 12:48:55 +01002426 ffa_id_t sender_to_ret =
J-Alvesfdd29272022-07-19 13:16:31 +01002427 (from_locked.vm->id == HF_OTHER_WORLD_ID)
2428 ? memory_region->sender
2429 : 0;
Andrew Walbranca808b12020-05-15 17:22:28 +01002430 ret = (struct ffa_value){
2431 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00002432 .arg1 = (uint32_t)memory_region->handle,
2433 .arg2 = (uint32_t)(memory_region->handle >> 32),
J-Alvesfdd29272022-07-19 13:16:31 +01002434 .arg3 = fragment_length,
2435 .arg4 = (uint32_t)(sender_to_ret & 0xffff) << 16};
Andrew Walbranca808b12020-05-15 17:22:28 +01002436 }
2437
2438out:
2439 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002440 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01002441 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002442}
2443
2444/**
J-Alves8505a8a2022-06-15 18:10:18 +01002445 * Continues an operation to donate, lend or share memory to a VM from current
2446 * world. If this is the last fragment then checks that the transition is valid
2447 * for the type of memory sending operation and updates the stage-2 page tables
2448 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01002449 *
2450 * Assumes that the caller has already found and locked the sender VM and copied
2451 * the memory region descriptor from the sender's TX buffer to a freshly
2452 * allocated page from Hafnium's internal pool.
2453 *
2454 * This function takes ownership of the `fragment` passed in; it must not be
2455 * freed by the caller.
2456 */
2457struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
2458 void *fragment,
2459 uint32_t fragment_length,
2460 ffa_memory_handle_t handle,
2461 struct mpool *page_pool)
2462{
2463 struct share_states_locked share_states = share_states_lock();
2464 struct ffa_memory_share_state *share_state;
2465 struct ffa_value ret;
2466 struct ffa_memory_region *memory_region;
2467
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05002468 CHECK(is_aligned(fragment,
2469 alignof(struct ffa_memory_region_constituent)));
2470 if (fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2471 0) {
2472 dlog_verbose("Fragment length %u misaligned.\n",
2473 fragment_length);
2474 ret = ffa_error(FFA_INVALID_PARAMETERS);
2475 goto out_free_fragment;
2476 }
2477
Andrew Walbranca808b12020-05-15 17:22:28 +01002478 ret = ffa_memory_send_continue_validate(share_states, handle,
2479 &share_state,
2480 from_locked.vm->id, page_pool);
2481 if (ret.func != FFA_SUCCESS_32) {
2482 goto out_free_fragment;
2483 }
2484 memory_region = share_state->memory_region;
2485
J-Alves95df0ef2022-12-07 10:09:48 +00002486 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002487 dlog_error(
2488 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01002489 "other world. This should never happen, and indicates "
2490 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01002491 "EL3 code.\n");
2492 ret = ffa_error(FFA_INVALID_PARAMETERS);
2493 goto out_free_fragment;
2494 }
2495
2496 /* Add this fragment. */
2497 share_state->fragments[share_state->fragment_count] = fragment;
2498 share_state->fragment_constituent_counts[share_state->fragment_count] =
2499 fragment_length / sizeof(struct ffa_memory_region_constituent);
2500 share_state->fragment_count++;
2501
2502 /* Check whether the memory send operation is now ready to complete. */
2503 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00002504 ret = ffa_memory_send_complete(
2505 from_locked, share_states, share_state, page_pool,
2506 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002507 } else {
2508 ret = (struct ffa_value){
2509 .func = FFA_MEM_FRAG_RX_32,
2510 .arg1 = (uint32_t)handle,
2511 .arg2 = (uint32_t)(handle >> 32),
2512 .arg3 = share_state_next_fragment_offset(share_states,
2513 share_state)};
2514 }
2515 goto out;
2516
2517out_free_fragment:
2518 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002519
2520out:
Andrew Walbranca808b12020-05-15 17:22:28 +01002521 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002522 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002523}
2524
Andrew Walbranca808b12020-05-15 17:22:28 +01002525/** Clean up after the receiver has finished retrieving a memory region. */
2526static void ffa_memory_retrieve_complete(
2527 struct share_states_locked share_states,
2528 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2529{
J-Alves95fbb312024-03-20 15:19:16 +00002530 if (share_state->share_func == FFA_MEM_DONATE_32 ||
2531 share_state->share_func == FFA_MEM_DONATE_64) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002532 /*
2533 * Memory that has been donated can't be relinquished,
2534 * so no need to keep the share state around.
2535 */
2536 share_state_free(share_states, share_state, page_pool);
2537 dlog_verbose("Freed share state for donate.\n");
2538 }
2539}
2540
J-Alves2d8457f2022-10-05 11:06:41 +01002541/**
2542 * Initialises the given memory region descriptor to be used for an
2543 * `FFA_MEM_RETRIEVE_RESP`, including the given constituents for the first
2544 * fragment.
2545 * The memory region descriptor is initialized according to retriever's
2546 * FF-A version.
2547 *
2548 * Returns true on success, or false if the given constituents won't all fit in
2549 * the first fragment.
2550 */
2551static bool ffa_retrieved_memory_region_init(
Karl Meakin0e617d92024-04-05 12:55:22 +01002552 void *response, enum ffa_version ffa_version, size_t response_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01002553 ffa_id_t sender, ffa_memory_attributes_t attributes,
J-Alves2d8457f2022-10-05 11:06:41 +01002554 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002555 ffa_memory_access_permissions_t permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002556 struct ffa_memory_access *receivers, size_t receiver_count,
2557 uint32_t memory_access_desc_size, uint32_t page_count,
2558 uint32_t total_constituent_count,
J-Alves2d8457f2022-10-05 11:06:41 +01002559 const struct ffa_memory_region_constituent constituents[],
2560 uint32_t fragment_constituent_count, uint32_t *total_length,
2561 uint32_t *fragment_length)
2562{
2563 struct ffa_composite_memory_region *composite_memory_region;
J-Alves2d8457f2022-10-05 11:06:41 +01002564 uint32_t i;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002565 uint32_t composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002566 uint32_t constituents_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002567
2568 assert(response != NULL);
2569
Karl Meakin0e617d92024-04-05 12:55:22 +01002570 if (ffa_version == FFA_VERSION_1_0) {
J-Alves2d8457f2022-10-05 11:06:41 +01002571 struct ffa_memory_region_v1_0 *retrieve_response =
2572 (struct ffa_memory_region_v1_0 *)response;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002573 struct ffa_memory_access_v1_0 *receiver;
J-Alves2d8457f2022-10-05 11:06:41 +01002574
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002575 ffa_memory_region_init_header_v1_0(retrieve_response, sender,
2576 attributes, flags, handle, 0,
2577 receiver_count);
J-Alves2d8457f2022-10-05 11:06:41 +01002578
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002579 receiver = (struct ffa_memory_access_v1_0 *)
2580 retrieve_response->receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002581 receiver_count = retrieve_response->receiver_count;
2582
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002583 for (uint32_t i = 0; i < receiver_count; i++) {
2584 ffa_id_t receiver_id =
2585 receivers[i].receiver_permissions.receiver;
2586 ffa_memory_receiver_flags_t recv_flags =
2587 receivers[i].receiver_permissions.flags;
2588
2589 /*
2590 * Initialized here as in memory retrieve responses we
2591 * currently expect one borrower to be specified.
2592 */
2593 ffa_memory_access_init_v1_0(
Karl Meakin84710f32023-10-12 15:14:49 +01002594 receiver, receiver_id, permissions.data_access,
2595 permissions.instruction_access, recv_flags);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002596 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002597
2598 composite_offset =
J-Alves2d8457f2022-10-05 11:06:41 +01002599 sizeof(struct ffa_memory_region_v1_0) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002600 receiver_count * sizeof(struct ffa_memory_access_v1_0);
2601 receiver->composite_memory_region_offset = composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002602
2603 composite_memory_region = ffa_memory_region_get_composite_v1_0(
2604 retrieve_response, 0);
2605 } else {
J-Alves2d8457f2022-10-05 11:06:41 +01002606 struct ffa_memory_region *retrieve_response =
2607 (struct ffa_memory_region *)response;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002608 struct ffa_memory_access *retrieve_response_receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002609
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002610 ffa_memory_region_init_header(
2611 retrieve_response, sender, attributes, flags, handle, 0,
2612 receiver_count, memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002613
2614 /*
2615 * Note that `sizeof(struct_ffa_memory_region)` and
2616 * `sizeof(struct ffa_memory_access)` must both be multiples of
2617 * 16 (as verified by the asserts in `ffa_memory.c`, so it is
2618 * guaranteed that the offset we calculate here is aligned to a
2619 * 64-bit boundary and so 64-bit values can be copied without
2620 * alignment faults.
2621 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002622 composite_offset =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01002623 retrieve_response->receivers_offset +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002624 (uint32_t)(receiver_count *
2625 retrieve_response->memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002626
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002627 retrieve_response_receivers =
2628 ffa_memory_region_get_receiver(retrieve_response, 0);
2629 assert(retrieve_response_receivers != NULL);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002630
2631 /*
2632 * Initialized here as in memory retrieve responses we currently
2633 * expect one borrower to be specified.
2634 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002635 memcpy_s(retrieve_response_receivers,
2636 sizeof(struct ffa_memory_access) * receiver_count,
2637 receivers,
2638 sizeof(struct ffa_memory_access) * receiver_count);
2639
2640 retrieve_response_receivers->composite_memory_region_offset =
2641 composite_offset;
2642
J-Alves2d8457f2022-10-05 11:06:41 +01002643 composite_memory_region =
2644 ffa_memory_region_get_composite(retrieve_response, 0);
2645 }
2646
J-Alves2d8457f2022-10-05 11:06:41 +01002647 assert(composite_memory_region != NULL);
2648
J-Alves2d8457f2022-10-05 11:06:41 +01002649 composite_memory_region->page_count = page_count;
2650 composite_memory_region->constituent_count = total_constituent_count;
2651 composite_memory_region->reserved_0 = 0;
2652
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002653 constituents_offset =
2654 composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alves2d8457f2022-10-05 11:06:41 +01002655 if (constituents_offset +
2656 fragment_constituent_count *
2657 sizeof(struct ffa_memory_region_constituent) >
2658 response_max_size) {
2659 return false;
2660 }
2661
2662 for (i = 0; i < fragment_constituent_count; ++i) {
2663 composite_memory_region->constituents[i] = constituents[i];
2664 }
2665
2666 if (total_length != NULL) {
2667 *total_length =
2668 constituents_offset +
2669 composite_memory_region->constituent_count *
2670 sizeof(struct ffa_memory_region_constituent);
2671 }
2672 if (fragment_length != NULL) {
2673 *fragment_length =
2674 constituents_offset +
2675 fragment_constituent_count *
2676 sizeof(struct ffa_memory_region_constituent);
2677 }
2678
2679 return true;
2680}
2681
J-Alves96de29f2022-04-26 16:05:24 +01002682/**
2683 * Validates the retrieved permissions against those specified by the lender
2684 * of memory share operation. Optionally can help set the permissions to be used
2685 * for the S2 mapping, through the `permissions` argument.
J-Alvesdcad8992023-09-15 14:10:35 +01002686 * Returns FFA_SUCCESS if all the fields are valid. FFA_ERROR, with error code:
2687 * - FFA_INVALID_PARAMETERS -> if the fields have invalid values as per the
2688 * specification for each ABI.
2689 * - FFA_DENIED -> if the permissions specified by the retriever are not
2690 * less permissive than those provided by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002691 */
J-Alvesdcad8992023-09-15 14:10:35 +01002692static struct ffa_value ffa_memory_retrieve_is_memory_access_valid(
2693 uint32_t share_func, enum ffa_data_access sent_data_access,
J-Alves96de29f2022-04-26 16:05:24 +01002694 enum ffa_data_access requested_data_access,
2695 enum ffa_instruction_access sent_instruction_access,
2696 enum ffa_instruction_access requested_instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002697 ffa_memory_access_permissions_t *permissions, bool multiple_borrowers)
J-Alves96de29f2022-04-26 16:05:24 +01002698{
2699 switch (sent_data_access) {
2700 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2701 case FFA_DATA_ACCESS_RW:
2702 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2703 requested_data_access == FFA_DATA_ACCESS_RW) {
2704 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002705 permissions->data_access = FFA_DATA_ACCESS_RW;
J-Alves96de29f2022-04-26 16:05:24 +01002706 }
2707 break;
2708 }
2709 /* Intentional fall-through. */
2710 case FFA_DATA_ACCESS_RO:
2711 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2712 requested_data_access == FFA_DATA_ACCESS_RO) {
2713 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002714 permissions->data_access = FFA_DATA_ACCESS_RO;
J-Alves96de29f2022-04-26 16:05:24 +01002715 }
2716 break;
2717 }
2718 dlog_verbose(
2719 "Invalid data access requested; sender specified "
2720 "permissions %#x but receiver requested %#x.\n",
2721 sent_data_access, requested_data_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002722 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002723 case FFA_DATA_ACCESS_RESERVED:
2724 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
2725 "checked before this point.");
2726 }
2727
J-Alvesdcad8992023-09-15 14:10:35 +01002728 /*
2729 * For operations with a single borrower, If it is an FFA_MEMORY_LEND
2730 * or FFA_MEMORY_DONATE the retriever should have specifed the
2731 * instruction permissions it wishes to receive.
2732 */
2733 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00002734 case FFA_MEM_SHARE_64:
J-Alvesdcad8992023-09-15 14:10:35 +01002735 case FFA_MEM_SHARE_32:
2736 if (requested_instruction_access !=
2737 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2738 dlog_verbose(
2739 "%s: for share instruction permissions must "
2740 "NOT be specified.\n",
2741 __func__);
2742 return ffa_error(FFA_INVALID_PARAMETERS);
2743 }
2744 break;
J-Alves95fbb312024-03-20 15:19:16 +00002745 case FFA_MEM_LEND_64:
J-Alvesdcad8992023-09-15 14:10:35 +01002746 case FFA_MEM_LEND_32:
2747 /*
2748 * For operations with multiple borrowers only permit XN
2749 * permissions, and both Sender and borrower should have used
2750 * FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED.
2751 */
2752 if (multiple_borrowers) {
2753 if (requested_instruction_access !=
2754 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2755 dlog_verbose(
2756 "%s: lend/share/donate with multiple "
2757 "borrowers "
2758 "instruction permissions must NOT be "
2759 "specified.\n",
2760 __func__);
2761 return ffa_error(FFA_INVALID_PARAMETERS);
2762 }
2763 break;
2764 }
2765 /* Fall through if the operation targets a single borrower. */
J-Alves95fbb312024-03-20 15:19:16 +00002766 case FFA_MEM_DONATE_64:
J-Alvesdcad8992023-09-15 14:10:35 +01002767 case FFA_MEM_DONATE_32:
2768 if (!multiple_borrowers &&
2769 requested_instruction_access ==
2770 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2771 dlog_verbose(
2772 "%s: for lend/donate with single borrower "
2773 "instruction permissions must be speficified "
2774 "by borrower\n",
2775 __func__);
2776 return ffa_error(FFA_INVALID_PARAMETERS);
2777 }
2778 break;
2779 default:
2780 panic("%s: Wrong func id provided.\n", __func__);
2781 }
2782
J-Alves96de29f2022-04-26 16:05:24 +01002783 switch (sent_instruction_access) {
2784 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2785 case FFA_INSTRUCTION_ACCESS_X:
J-Alvesdcad8992023-09-15 14:10:35 +01002786 if (requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
J-Alves96de29f2022-04-26 16:05:24 +01002787 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002788 permissions->instruction_access =
2789 FFA_INSTRUCTION_ACCESS_X;
J-Alves96de29f2022-04-26 16:05:24 +01002790 }
2791 break;
2792 }
J-Alvesdcad8992023-09-15 14:10:35 +01002793 /*
2794 * Fall through if requested permissions are less
2795 * permissive than those provided by the sender.
2796 */
J-Alves96de29f2022-04-26 16:05:24 +01002797 case FFA_INSTRUCTION_ACCESS_NX:
2798 if (requested_instruction_access ==
2799 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2800 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2801 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002802 permissions->instruction_access =
2803 FFA_INSTRUCTION_ACCESS_NX;
J-Alves96de29f2022-04-26 16:05:24 +01002804 }
2805 break;
2806 }
2807 dlog_verbose(
2808 "Invalid instruction access requested; sender "
2809 "specified permissions %#x but receiver requested "
2810 "%#x.\n",
2811 sent_instruction_access, requested_instruction_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002812 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002813 case FFA_INSTRUCTION_ACCESS_RESERVED:
2814 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
2815 "be checked before this point.");
2816 }
2817
J-Alvesdcad8992023-09-15 14:10:35 +01002818 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves96de29f2022-04-26 16:05:24 +01002819}
2820
2821/**
2822 * Validate the receivers' permissions in the retrieve request against those
2823 * specified by the lender.
2824 * In the `permissions` argument returns the permissions to set at S2 for the
2825 * caller to the FFA_MEMORY_RETRIEVE_REQ.
J-Alves3456e032023-07-20 12:20:05 +01002826 * The function looks into the flag to bypass multiple borrower checks:
2827 * - If not set returns FFA_SUCCESS if all specified permissions are valid.
2828 * - If set returns FFA_SUCCESS if the descriptor contains the permissions
2829 * to the caller of FFA_MEM_RETRIEVE_REQ and they are valid. Other permissions
2830 * are ignored, if provided.
J-Alves96de29f2022-04-26 16:05:24 +01002831 */
2832static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
2833 struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01002834 struct ffa_memory_region *retrieve_request, ffa_id_t to_vm_id,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002835 ffa_memory_access_permissions_t *permissions,
2836 struct ffa_memory_access **receiver_ret, uint32_t func_id)
J-Alves96de29f2022-04-26 16:05:24 +01002837{
2838 uint32_t retrieve_receiver_index;
J-Alves3456e032023-07-20 12:20:05 +01002839 bool bypass_multi_receiver_check =
2840 (retrieve_request->flags &
2841 FFA_MEMORY_REGION_FLAG_BYPASS_BORROWERS_CHECK) != 0U;
J-Alvesdcad8992023-09-15 14:10:35 +01002842 const uint32_t region_receiver_count = memory_region->receiver_count;
2843 struct ffa_value ret;
J-Alves96de29f2022-04-26 16:05:24 +01002844
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002845 assert(receiver_ret != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002846 assert(permissions != NULL);
2847
Karl Meakin84710f32023-10-12 15:14:49 +01002848 *permissions = (ffa_memory_access_permissions_t){0};
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002849
J-Alves3456e032023-07-20 12:20:05 +01002850 if (!bypass_multi_receiver_check) {
J-Alvesdcad8992023-09-15 14:10:35 +01002851 if (retrieve_request->receiver_count != region_receiver_count) {
J-Alves3456e032023-07-20 12:20:05 +01002852 dlog_verbose(
2853 "Retrieve request should contain same list of "
2854 "borrowers, as specified by the lender.\n");
2855 return ffa_error(FFA_INVALID_PARAMETERS);
2856 }
2857 } else {
2858 if (retrieve_request->receiver_count != 1) {
2859 dlog_verbose(
2860 "Set bypass multiple borrower check, receiver "
2861 "list must be sized 1 (%x)\n",
2862 memory_region->receiver_count);
2863 return ffa_error(FFA_INVALID_PARAMETERS);
2864 }
J-Alves96de29f2022-04-26 16:05:24 +01002865 }
2866
2867 retrieve_receiver_index = retrieve_request->receiver_count;
2868
J-Alves96de29f2022-04-26 16:05:24 +01002869 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
2870 ffa_memory_access_permissions_t sent_permissions;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002871 struct ffa_memory_access *retrieve_request_receiver =
2872 ffa_memory_region_get_receiver(retrieve_request, i);
2873 assert(retrieve_request_receiver != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002874 ffa_memory_access_permissions_t requested_permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002875 retrieve_request_receiver->receiver_permissions
2876 .permissions;
J-Alves19e20cf2023-08-02 12:48:55 +01002877 ffa_id_t current_receiver_id =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002878 retrieve_request_receiver->receiver_permissions
2879 .receiver;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002880 struct ffa_memory_access *receiver;
2881 uint32_t mem_region_receiver_index;
2882 bool permissions_RO;
2883 bool clear_memory_flags;
J-Alvesf220d572024-04-24 22:15:14 +01002884 /*
2885 * If the call is at the virtual FF-A instance the caller's
2886 * ID must match an entry in the memory access list.
2887 * In the SPMC, one of the specified receivers could be from
2888 * the NWd.
2889 */
2890 bool found_to_id = vm_id_is_current_world(to_vm_id)
2891 ? (current_receiver_id == to_vm_id)
2892 : (!vm_id_is_current_world(
2893 current_receiver_id));
J-Alves96de29f2022-04-26 16:05:24 +01002894
J-Alves3456e032023-07-20 12:20:05 +01002895 if (bypass_multi_receiver_check && !found_to_id) {
2896 dlog_verbose(
2897 "Bypass multiple borrower check for id %x.\n",
2898 current_receiver_id);
2899 continue;
2900 }
2901
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002902 if (retrieve_request_receiver->composite_memory_region_offset !=
2903 0U) {
2904 dlog_verbose(
2905 "Retriever specified address ranges not "
2906 "supported (got offset %d).\n",
2907 retrieve_request_receiver
2908 ->composite_memory_region_offset);
2909 return ffa_error(FFA_INVALID_PARAMETERS);
2910 }
2911
J-Alves96de29f2022-04-26 16:05:24 +01002912 /*
2913 * Find the current receiver in the transaction descriptor from
2914 * sender.
2915 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002916 mem_region_receiver_index =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002917 ffa_memory_region_get_receiver_index(
2918 memory_region, current_receiver_id);
J-Alves96de29f2022-04-26 16:05:24 +01002919
2920 if (mem_region_receiver_index ==
2921 memory_region->receiver_count) {
2922 dlog_verbose("%s: receiver %x not found\n", __func__,
2923 current_receiver_id);
2924 return ffa_error(FFA_DENIED);
2925 }
2926
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002927 receiver = ffa_memory_region_get_receiver(
2928 memory_region, mem_region_receiver_index);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002929 assert(receiver != NULL);
2930
2931 sent_permissions = receiver->receiver_permissions.permissions;
J-Alves96de29f2022-04-26 16:05:24 +01002932
2933 if (found_to_id) {
2934 retrieve_receiver_index = i;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002935
2936 *receiver_ret = receiver;
J-Alves96de29f2022-04-26 16:05:24 +01002937 }
2938
2939 /*
J-Alvesdcad8992023-09-15 14:10:35 +01002940 * Check if retrieve request memory access list is valid:
2941 * - The retrieve request complies with the specification.
2942 * - Permissions are within those specified by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002943 */
J-Alvesdcad8992023-09-15 14:10:35 +01002944 ret = ffa_memory_retrieve_is_memory_access_valid(
Karl Meakin84710f32023-10-12 15:14:49 +01002945 func_id, sent_permissions.data_access,
2946 requested_permissions.data_access,
2947 sent_permissions.instruction_access,
2948 requested_permissions.instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002949 found_to_id ? permissions : NULL,
2950 region_receiver_count > 1);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002951
J-Alvesdcad8992023-09-15 14:10:35 +01002952 if (ret.func != FFA_SUCCESS_32) {
2953 return ret;
J-Alves96de29f2022-04-26 16:05:24 +01002954 }
2955
Karl Meakin84710f32023-10-12 15:14:49 +01002956 permissions_RO =
2957 (permissions->data_access == FFA_DATA_ACCESS_RO);
J-Alvese5262372024-03-27 11:02:03 +00002958 clear_memory_flags =
2959 (retrieve_request->flags &
2960 (FFA_MEMORY_REGION_FLAG_CLEAR |
2961 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002962
J-Alves96de29f2022-04-26 16:05:24 +01002963 /*
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002964 * Can't request PM to clear memory if only provided
2965 * with RO permissions.
J-Alves96de29f2022-04-26 16:05:24 +01002966 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002967 if (found_to_id && permissions_RO && clear_memory_flags) {
J-Alves96de29f2022-04-26 16:05:24 +01002968 dlog_verbose(
2969 "Receiver has RO permissions can not request "
2970 "clear.\n");
2971 return ffa_error(FFA_DENIED);
2972 }
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002973
2974 /*
2975 * Check the impdef in the retrieve_request matches the value in
2976 * the original memory send.
2977 */
2978 if (ffa_version_from_memory_access_desc_size(
2979 memory_region->memory_access_desc_size) >=
Karl Meakin0e617d92024-04-05 12:55:22 +01002980 FFA_VERSION_1_2 &&
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002981 ffa_version_from_memory_access_desc_size(
2982 retrieve_request->memory_access_desc_size) >=
Karl Meakin0e617d92024-04-05 12:55:22 +01002983 FFA_VERSION_1_2) {
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002984 if (receiver->impdef.val[0] !=
2985 retrieve_request_receiver->impdef.val[0] ||
2986 receiver->impdef.val[1] !=
2987 retrieve_request_receiver->impdef.val[1]) {
2988 dlog_verbose(
2989 "Impdef value in memory send does not "
J-Alves0a824e92024-04-26 16:20:12 +01002990 "match retrieve request value send "
2991 "value %#lx %#lx retrieve request "
Karl Meakine8937d92024-03-19 16:04:25 +00002992 "value %#lx %#lx\n",
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002993 receiver->impdef.val[0],
2994 receiver->impdef.val[1],
2995 retrieve_request_receiver->impdef
2996 .val[0],
2997 retrieve_request_receiver->impdef
2998 .val[1]);
2999 return ffa_error(FFA_INVALID_PARAMETERS);
3000 }
3001 }
J-Alves96de29f2022-04-26 16:05:24 +01003002 }
3003
3004 if (retrieve_receiver_index == retrieve_request->receiver_count) {
3005 dlog_verbose(
3006 "Retrieve request does not contain caller's (%x) "
3007 "permissions\n",
3008 to_vm_id);
3009 return ffa_error(FFA_INVALID_PARAMETERS);
3010 }
3011
3012 return (struct ffa_value){.func = FFA_SUCCESS_32};
3013}
3014
J-Alvesa9cd7e32022-07-01 13:49:33 +01003015/*
3016 * According to section 16.4.3 of FF-A v1.1 EAC0 specification, the hypervisor
3017 * may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region description
3018 * of a pending memory sharing operation whose allocator is the SPM, for
3019 * validation purposes before forwarding an FFA_MEM_RECLAIM call. In doing so
3020 * the memory region descriptor of the retrieve request must be zeroed with the
3021 * exception of the sender ID and handle.
3022 */
J-Alves4f0d9c12024-01-17 17:23:11 +00003023bool is_ffa_hypervisor_retrieve_request(struct ffa_memory_region *request,
3024 struct vm_locked to_locked)
J-Alvesa9cd7e32022-07-01 13:49:33 +01003025{
3026 return to_locked.vm->id == HF_HYPERVISOR_VM_ID &&
Karl Meakin84710f32023-10-12 15:14:49 +01003027 request->attributes.shareability == 0U &&
3028 request->attributes.cacheability == 0U &&
3029 request->attributes.type == 0U &&
3030 request->attributes.security == 0U && request->flags == 0U &&
J-Alvesa9cd7e32022-07-01 13:49:33 +01003031 request->tag == 0U && request->receiver_count == 0U &&
3032 plat_ffa_memory_handle_allocated_by_current_world(
3033 request->handle);
3034}
3035
3036/*
3037 * Helper to reset count of fragments retrieved by the hypervisor.
3038 */
3039static void ffa_memory_retrieve_complete_from_hyp(
3040 struct ffa_memory_share_state *share_state)
3041{
3042 if (share_state->hypervisor_fragment_count ==
3043 share_state->fragment_count) {
3044 share_state->hypervisor_fragment_count = 0;
3045 }
3046}
3047
J-Alves089004f2022-07-13 14:25:44 +01003048/**
J-Alves4f0d9c12024-01-17 17:23:11 +00003049 * Prepares the return of the ffa_value for the memory retrieve response.
3050 */
3051static struct ffa_value ffa_memory_retrieve_resp(uint32_t total_length,
3052 uint32_t fragment_length)
3053{
3054 return (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
3055 .arg1 = total_length,
3056 .arg2 = fragment_length};
3057}
3058
3059/**
J-Alves089004f2022-07-13 14:25:44 +01003060 * Validate that the memory region descriptor provided by the borrower on
3061 * FFA_MEM_RETRIEVE_REQ, against saved memory region provided by lender at the
3062 * memory sharing call.
3063 */
3064static struct ffa_value ffa_memory_retrieve_validate(
J-Alves4f0d9c12024-01-17 17:23:11 +00003065 ffa_id_t to_id, struct ffa_memory_region *retrieve_request,
3066 uint32_t retrieve_request_length,
J-Alves089004f2022-07-13 14:25:44 +01003067 struct ffa_memory_region *memory_region, uint32_t *receiver_index,
3068 uint32_t share_func)
3069{
3070 ffa_memory_region_flags_t transaction_type =
3071 retrieve_request->flags &
3072 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003073 enum ffa_memory_security security_state;
J-Alves4f0d9c12024-01-17 17:23:11 +00003074 const uint64_t memory_access_desc_size =
3075 retrieve_request->memory_access_desc_size;
3076 const uint32_t expected_retrieve_request_length =
3077 retrieve_request->receivers_offset +
3078 (uint32_t)(retrieve_request->receiver_count *
3079 memory_access_desc_size);
J-Alves089004f2022-07-13 14:25:44 +01003080
3081 assert(retrieve_request != NULL);
3082 assert(memory_region != NULL);
3083 assert(receiver_index != NULL);
J-Alves089004f2022-07-13 14:25:44 +01003084
J-Alves4f0d9c12024-01-17 17:23:11 +00003085 if (retrieve_request_length != expected_retrieve_request_length) {
3086 dlog_verbose(
3087 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
3088 "but was %d.\n",
3089 expected_retrieve_request_length,
3090 retrieve_request_length);
3091 return ffa_error(FFA_INVALID_PARAMETERS);
3092 }
3093
3094 if (retrieve_request->sender != memory_region->sender) {
3095 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003096 "Memory with handle %#lx not fully sent, can't "
J-Alves4f0d9c12024-01-17 17:23:11 +00003097 "retrieve.\n",
3098 memory_region->handle);
3099 return ffa_error(FFA_DENIED);
3100 }
3101
3102 /*
3103 * The SPMC can only process retrieve requests to memory share
3104 * operations with one borrower from the other world. It can't
3105 * determine the ID of the NWd VM that invoked the retrieve
3106 * request interface call. It relies on the hypervisor to
3107 * validate the caller's ID against that provided in the
3108 * `receivers` list of the retrieve response.
3109 * In case there is only one borrower from the NWd in the
3110 * transaction descriptor, record that in the `receiver_id` for
3111 * later use, and validate in the retrieve request message.
3112 * This limitation is due to the fact SPMC can't determine the
3113 * index in the memory share structures state to update.
3114 */
3115 if (to_id == HF_HYPERVISOR_VM_ID) {
3116 uint32_t other_world_count = 0;
3117
3118 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3119 struct ffa_memory_access *receiver =
3120 ffa_memory_region_get_receiver(retrieve_request,
J-Alvesf220d572024-04-24 22:15:14 +01003121 i);
J-Alves4f0d9c12024-01-17 17:23:11 +00003122 assert(receiver != NULL);
3123
J-Alvesf220d572024-04-24 22:15:14 +01003124 if (!vm_id_is_current_world(
3125 receiver->receiver_permissions.receiver)) {
J-Alves4f0d9c12024-01-17 17:23:11 +00003126 other_world_count++;
J-Alvesf220d572024-04-24 22:15:14 +01003127 /* Set it to be used later. */
3128 to_id = receiver->receiver_permissions.receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003129 }
3130 }
3131
3132 if (other_world_count > 1) {
3133 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003134 "Support one receiver from the other world.\n");
J-Alves4f0d9c12024-01-17 17:23:11 +00003135 return ffa_error(FFA_NOT_SUPPORTED);
3136 }
3137 }
J-Alves089004f2022-07-13 14:25:44 +01003138 /*
3139 * Check that the transaction type expected by the receiver is
3140 * correct, if it has been specified.
3141 */
3142 if (transaction_type !=
3143 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
3144 transaction_type != (memory_region->flags &
3145 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
3146 dlog_verbose(
3147 "Incorrect transaction type %#x for "
Karl Meakine8937d92024-03-19 16:04:25 +00003148 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#lx.\n",
J-Alves089004f2022-07-13 14:25:44 +01003149 transaction_type,
3150 memory_region->flags &
3151 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
3152 retrieve_request->handle);
3153 return ffa_error(FFA_INVALID_PARAMETERS);
3154 }
3155
3156 if (retrieve_request->tag != memory_region->tag) {
3157 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003158 "Incorrect tag %lu for FFA_MEM_RETRIEVE_REQ, expected "
3159 "%lu for handle %#lx.\n",
J-Alves089004f2022-07-13 14:25:44 +01003160 retrieve_request->tag, memory_region->tag,
3161 retrieve_request->handle);
3162 return ffa_error(FFA_INVALID_PARAMETERS);
3163 }
3164
J-Alves4f0d9c12024-01-17 17:23:11 +00003165 *receiver_index =
3166 ffa_memory_region_get_receiver_index(memory_region, to_id);
J-Alves089004f2022-07-13 14:25:44 +01003167
3168 if (*receiver_index == memory_region->receiver_count) {
3169 dlog_verbose(
3170 "Incorrect receiver VM ID %d for "
Karl Meakine8937d92024-03-19 16:04:25 +00003171 "FFA_MEM_RETRIEVE_REQ, for handle %#lx.\n",
J-Alves4f0d9c12024-01-17 17:23:11 +00003172 to_id, memory_region->handle);
J-Alves089004f2022-07-13 14:25:44 +01003173 return ffa_error(FFA_INVALID_PARAMETERS);
3174 }
3175
3176 if ((retrieve_request->flags &
3177 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
3178 dlog_verbose(
3179 "Retriever specified 'address range alignment 'hint' "
3180 "not supported.\n");
3181 return ffa_error(FFA_INVALID_PARAMETERS);
3182 }
3183 if ((retrieve_request->flags &
3184 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
3185 dlog_verbose(
3186 "Bits 8-5 must be zero in memory region's flags "
3187 "(address range alignment hint not supported).\n");
3188 return ffa_error(FFA_INVALID_PARAMETERS);
3189 }
3190
3191 if ((retrieve_request->flags & ~0x7FF) != 0U) {
3192 dlog_verbose(
3193 "Bits 31-10 must be zero in memory region's flags.\n");
3194 return ffa_error(FFA_INVALID_PARAMETERS);
3195 }
3196
J-Alves95fbb312024-03-20 15:19:16 +00003197 if ((share_func == FFA_MEM_SHARE_32 ||
3198 share_func == FFA_MEM_SHARE_64) &&
J-Alves089004f2022-07-13 14:25:44 +01003199 (retrieve_request->flags &
3200 (FFA_MEMORY_REGION_FLAG_CLEAR |
3201 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
3202 dlog_verbose(
3203 "Memory Share operation can't clean after relinquish "
3204 "memory region.\n");
3205 return ffa_error(FFA_INVALID_PARAMETERS);
3206 }
3207
3208 /*
3209 * If the borrower needs the memory to be cleared before mapping
3210 * to its address space, the sender should have set the flag
3211 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
3212 * FFA_DENIED.
3213 */
3214 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
3215 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
3216 dlog_verbose(
3217 "Borrower needs memory cleared. Sender needs to set "
3218 "flag for clearing memory.\n");
3219 return ffa_error(FFA_DENIED);
3220 }
3221
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003222 /* Memory region attributes NS-Bit MBZ for FFA_MEM_RETRIEVE_REQ. */
Karl Meakin84710f32023-10-12 15:14:49 +01003223 security_state = retrieve_request->attributes.security;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003224 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
3225 dlog_verbose(
3226 "Invalid security state for memory retrieve request "
3227 "operation.\n");
3228 return ffa_error(FFA_INVALID_PARAMETERS);
3229 }
3230
J-Alves089004f2022-07-13 14:25:44 +01003231 /*
3232 * If memory type is not specified, bypass validation of memory
3233 * attributes in the retrieve request. The retriever is expecting to
3234 * obtain this information from the SPMC.
3235 */
Karl Meakin84710f32023-10-12 15:14:49 +01003236 if (retrieve_request->attributes.type == FFA_MEMORY_NOT_SPECIFIED_MEM) {
J-Alves089004f2022-07-13 14:25:44 +01003237 return (struct ffa_value){.func = FFA_SUCCESS_32};
3238 }
3239
3240 /*
3241 * Ensure receiver's attributes are compatible with how
3242 * Hafnium maps memory: Normal Memory, Inner shareable,
3243 * Write-Back Read-Allocate Write-Allocate Cacheable.
3244 */
3245 return ffa_memory_attributes_validate(retrieve_request->attributes);
3246}
3247
J-Alves3f6527c2024-04-25 17:10:57 +01003248/**
3249 * Whilst processing the retrieve request, the operation could be aborted, and
3250 * changes to page tables and the share state structures need to be reverted.
3251 */
3252static void ffa_partition_memory_retrieve_request_undo(
3253 struct vm_locked from_locked,
3254 struct ffa_memory_share_state *share_state, uint32_t receiver_index)
3255{
3256 /*
3257 * Currently this operation is expected for operations involving the
3258 * 'other_world' vm.
3259 */
3260 assert(from_locked.vm->id == HF_OTHER_WORLD_ID);
3261 assert(share_state->retrieved_fragment_count[receiver_index] > 0);
3262
3263 /* Decrement the retrieved fragment count for the given receiver. */
3264 share_state->retrieved_fragment_count[receiver_index]--;
3265}
3266
3267/**
3268 * Whilst processing an hypervisor retrieve request the operation could be
3269 * aborted. There were no updates to PTs in this case, so decrementing the
3270 * fragment count retrieved by the hypervisor should be enough.
3271 */
3272static void ffa_hypervisor_memory_retrieve_request_undo(
3273 struct ffa_memory_share_state *share_state)
3274{
3275 assert(share_state->hypervisor_fragment_count > 0);
3276 share_state->hypervisor_fragment_count--;
3277}
3278
J-Alves4f0d9c12024-01-17 17:23:11 +00003279static struct ffa_value ffa_partition_retrieve_request(
3280 struct share_states_locked share_states,
3281 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3282 struct ffa_memory_region *retrieve_request,
3283 uint32_t retrieve_request_length, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003284{
Karl Meakin84710f32023-10-12 15:14:49 +01003285 ffa_memory_access_permissions_t permissions = {0};
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003286 uint32_t memory_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003287 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01003288 struct ffa_composite_memory_region *composite;
3289 uint32_t total_length;
3290 uint32_t fragment_length;
J-Alves19e20cf2023-08-02 12:48:55 +01003291 ffa_id_t receiver_id = to_locked.vm->id;
J-Alves4f0d9c12024-01-17 17:23:11 +00003292 bool is_retrieve_complete = false;
J-Alves4f0d9c12024-01-17 17:23:11 +00003293 const uint64_t memory_access_desc_size =
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003294 retrieve_request->memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003295 uint32_t receiver_index;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003296 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003297 ffa_memory_handle_t handle = retrieve_request->handle;
Karl Meakin84710f32023-10-12 15:14:49 +01003298 ffa_memory_attributes_t attributes = {0};
J-Alves460d36c2023-10-12 17:02:15 +01003299 uint32_t retrieve_mode = 0;
J-Alves4f0d9c12024-01-17 17:23:11 +00003300 struct ffa_memory_region *memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003301
J-Alves96de29f2022-04-26 16:05:24 +01003302 if (!share_state->sending_complete) {
3303 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003304 "Memory with handle %#lx not fully sent, can't "
J-Alves96de29f2022-04-26 16:05:24 +01003305 "retrieve.\n",
3306 handle);
J-Alves4f0d9c12024-01-17 17:23:11 +00003307 return ffa_error(FFA_INVALID_PARAMETERS);
J-Alves96de29f2022-04-26 16:05:24 +01003308 }
3309
J-Alves4f0d9c12024-01-17 17:23:11 +00003310 /*
3311 * Validate retrieve request, according to what was sent by the
3312 * sender. Function will output the `receiver_index` from the
3313 * provided memory region.
3314 */
3315 ret = ffa_memory_retrieve_validate(
3316 receiver_id, retrieve_request, retrieve_request_length,
3317 memory_region, &receiver_index, share_state->share_func);
J-Alves089004f2022-07-13 14:25:44 +01003318
J-Alves4f0d9c12024-01-17 17:23:11 +00003319 if (ret.func != FFA_SUCCESS_32) {
3320 return ret;
J-Alves089004f2022-07-13 14:25:44 +01003321 }
J-Alves96de29f2022-04-26 16:05:24 +01003322
J-Alves4f0d9c12024-01-17 17:23:11 +00003323 /*
3324 * Validate the requested permissions against the sent
3325 * permissions.
3326 * Outputs the permissions to give to retriever at S2
3327 * PTs.
3328 */
3329 ret = ffa_memory_retrieve_validate_memory_access_list(
3330 memory_region, retrieve_request, receiver_id, &permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003331 &receiver, share_state->share_func);
J-Alves4f0d9c12024-01-17 17:23:11 +00003332 if (ret.func != FFA_SUCCESS_32) {
3333 return ret;
3334 }
3335
3336 memory_to_mode = ffa_memory_permissions_to_mode(
3337 permissions, share_state->sender_orig_mode);
3338
Daniel Boulby6e261362024-06-13 16:53:00 +01003339 /*
3340 * Check requested memory type is valid with the memory type of the
3341 * owner. E.g. they follow the memory type precedence where Normal
3342 * memory is more permissive than device and therefore device memory
3343 * can only be shared as device memory.
3344 */
3345 if (retrieve_request->attributes.type == FFA_MEMORY_NORMAL_MEM &&
3346 ((share_state->sender_orig_mode & MM_MODE_D) != 0U ||
3347 memory_region->attributes.type == FFA_MEMORY_DEVICE_MEM)) {
3348 dlog_verbose(
3349 "Retrieving device memory as Normal memory is not "
3350 "allowed\n");
3351 return ffa_error(FFA_DENIED);
3352 }
3353
J-Alves4f0d9c12024-01-17 17:23:11 +00003354 ret = ffa_retrieve_check_update(
3355 to_locked, share_state->fragments,
3356 share_state->fragment_constituent_counts,
3357 share_state->fragment_count, memory_to_mode,
J-Alves460d36c2023-10-12 17:02:15 +01003358 share_state->share_func, false, page_pool, &retrieve_mode,
3359 share_state->memory_protected);
J-Alves4f0d9c12024-01-17 17:23:11 +00003360
3361 if (ret.func != FFA_SUCCESS_32) {
3362 return ret;
3363 }
3364
3365 share_state->retrieved_fragment_count[receiver_index] = 1;
3366
3367 is_retrieve_complete =
3368 share_state->retrieved_fragment_count[receiver_index] ==
3369 share_state->fragment_count;
3370
J-Alvesb5084cf2022-07-06 14:20:12 +01003371 /* VMs acquire the RX buffer from SPMC. */
3372 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3373
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003374 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003375 * Copy response to RX buffer of caller and deliver the message.
3376 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003377 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003378 composite = ffa_memory_region_get_composite(memory_region, 0);
J-Alves4f0d9c12024-01-17 17:23:11 +00003379
Andrew Walbranca808b12020-05-15 17:22:28 +01003380 /*
J-Alves460d36c2023-10-12 17:02:15 +01003381 * Set the security state in the memory retrieve response attributes
3382 * if specified by the target mode.
3383 */
3384 attributes = plat_ffa_memory_security_mode(memory_region->attributes,
3385 retrieve_mode);
3386
3387 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003388 * Constituents which we received in the first fragment should
3389 * always fit in the first fragment we are sending, because the
3390 * header is the same size in both cases and we have a fixed
3391 * message buffer size. So `ffa_retrieved_memory_region_init`
3392 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01003393 */
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003394
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003395 /* Provide the permissions that had been provided. */
3396 receiver->receiver_permissions.permissions = permissions;
3397
3398 /*
3399 * Prepare the memory region descriptor for the retrieve response.
3400 * Provide the pointer to the receiver tracked in the share state
J-Alves7b9cc432024-04-04 10:57:17 +01003401 * structures.
3402 * At this point the retrieve request descriptor from the partition
3403 * has been processed. The `retrieve_request` is expected to be in
3404 * a region that is handled by the SPMC/Hyp. Reuse the same buffer to
3405 * prepare the retrieve response before copying it to the RX buffer of
3406 * the caller.
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003407 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003408 CHECK(ffa_retrieved_memory_region_init(
J-Alves7b9cc432024-04-04 10:57:17 +01003409 retrieve_request, to_locked.vm->ffa_version, HF_MAILBOX_SIZE,
3410 memory_region->sender, attributes, memory_region->flags, handle,
3411 permissions, receiver, 1, memory_access_desc_size,
3412 composite->page_count, composite->constituent_count,
3413 share_state->fragments[0],
Andrew Walbranca808b12020-05-15 17:22:28 +01003414 share_state->fragment_constituent_counts[0], &total_length,
3415 &fragment_length));
J-Alvesb5084cf2022-07-06 14:20:12 +01003416
J-Alves7b9cc432024-04-04 10:57:17 +01003417 /*
3418 * Copy the message from the buffer into the partition's mailbox.
3419 * The operation might fail unexpectedly due to change in PAS address
3420 * space, or improper values to the sizes of the structures.
3421 */
3422 if (!memcpy_trapped(to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3423 retrieve_request, fragment_length)) {
3424 dlog_error(
3425 "%s: aborted the copy of response to RX buffer of "
3426 "%x.\n",
3427 __func__, to_locked.vm->id);
J-Alves3f6527c2024-04-25 17:10:57 +01003428
3429 ffa_partition_memory_retrieve_request_undo(
3430 to_locked, share_state, receiver_index);
3431
J-Alves7b9cc432024-04-04 10:57:17 +01003432 return ffa_error(FFA_ABORTED);
3433 }
3434
J-Alves4f0d9c12024-01-17 17:23:11 +00003435 if (is_retrieve_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003436 ffa_memory_retrieve_complete(share_states, share_state,
3437 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003438 }
J-Alves4f0d9c12024-01-17 17:23:11 +00003439
3440 return ffa_memory_retrieve_resp(total_length, fragment_length);
3441}
3442
3443static struct ffa_value ffa_hypervisor_retrieve_request(
3444 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3445 struct ffa_memory_region *retrieve_request)
3446{
3447 struct ffa_value ret;
3448 struct ffa_composite_memory_region *composite;
3449 uint32_t total_length;
3450 uint32_t fragment_length;
J-Alves4f0d9c12024-01-17 17:23:11 +00003451 ffa_memory_attributes_t attributes;
J-Alves7b6ab612024-01-24 09:54:54 +00003452 uint64_t memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003453 struct ffa_memory_region *memory_region;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003454 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003455 ffa_memory_handle_t handle = retrieve_request->handle;
3456
J-Alves4f0d9c12024-01-17 17:23:11 +00003457 memory_region = share_state->memory_region;
3458
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003459 assert(to_locked.vm->id == HF_HYPERVISOR_VM_ID);
3460
J-Alves7b6ab612024-01-24 09:54:54 +00003461 switch (to_locked.vm->ffa_version) {
Karl Meakin0e617d92024-04-05 12:55:22 +01003462 case FFA_VERSION_1_2:
J-Alves7b6ab612024-01-24 09:54:54 +00003463 memory_access_desc_size = sizeof(struct ffa_memory_access);
3464 break;
Karl Meakin0e617d92024-04-05 12:55:22 +01003465 case FFA_VERSION_1_0:
3466 case FFA_VERSION_1_1:
J-Alves7b6ab612024-01-24 09:54:54 +00003467 memory_access_desc_size = sizeof(struct ffa_memory_access_v1_0);
3468 break;
3469 default:
3470 panic("version not supported: %x\n", to_locked.vm->ffa_version);
3471 }
3472
J-Alves4f0d9c12024-01-17 17:23:11 +00003473 if (share_state->hypervisor_fragment_count != 0U) {
3474 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003475 "Memory with handle %#lx already retrieved by "
J-Alves4f0d9c12024-01-17 17:23:11 +00003476 "the hypervisor.\n",
3477 handle);
3478 return ffa_error(FFA_DENIED);
3479 }
3480
3481 share_state->hypervisor_fragment_count = 1;
3482
J-Alves4f0d9c12024-01-17 17:23:11 +00003483 /* VMs acquire the RX buffer from SPMC. */
3484 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3485
3486 /*
3487 * Copy response to RX buffer of caller and deliver the message.
3488 * This must be done before the share_state is (possibly) freed.
3489 */
3490 composite = ffa_memory_region_get_composite(memory_region, 0);
3491
3492 /*
3493 * Constituents which we received in the first fragment should
3494 * always fit in the first fragment we are sending, because the
3495 * header is the same size in both cases and we have a fixed
3496 * message buffer size. So `ffa_retrieved_memory_region_init`
3497 * should never fail.
3498 */
3499
3500 /*
3501 * Set the security state in the memory retrieve response attributes
3502 * if specified by the target mode.
3503 */
3504 attributes = plat_ffa_memory_security_mode(
3505 memory_region->attributes, share_state->sender_orig_mode);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003506
3507 receiver = ffa_memory_region_get_receiver(memory_region, 0);
3508
J-Alves7b9cc432024-04-04 10:57:17 +01003509 /*
3510 * At this point the `retrieve_request` is expected to be in a section
3511 * managed by the hypervisor.
3512 */
J-Alves4f0d9c12024-01-17 17:23:11 +00003513 CHECK(ffa_retrieved_memory_region_init(
J-Alves7b9cc432024-04-04 10:57:17 +01003514 retrieve_request, to_locked.vm->ffa_version, HF_MAILBOX_SIZE,
3515 memory_region->sender, attributes, memory_region->flags, handle,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003516 receiver->receiver_permissions.permissions, receiver,
3517 memory_region->receiver_count, memory_access_desc_size,
J-Alves4f0d9c12024-01-17 17:23:11 +00003518 composite->page_count, composite->constituent_count,
3519 share_state->fragments[0],
3520 share_state->fragment_constituent_counts[0], &total_length,
3521 &fragment_length));
3522
J-Alves7b9cc432024-04-04 10:57:17 +01003523 /*
3524 * Copy the message from the buffer into the hypervisor's mailbox.
3525 * The operation might fail unexpectedly due to change in PAS, or
3526 * improper values for the sizes of the structures.
3527 */
3528 if (!memcpy_trapped(to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3529 retrieve_request, fragment_length)) {
3530 dlog_error(
3531 "%s: aborted the copy of response to RX buffer of "
3532 "%x.\n",
3533 __func__, to_locked.vm->id);
J-Alves3f6527c2024-04-25 17:10:57 +01003534
3535 ffa_hypervisor_memory_retrieve_request_undo(share_state);
3536
J-Alves7b9cc432024-04-04 10:57:17 +01003537 return ffa_error(FFA_ABORTED);
3538 }
3539
J-Alves3f6527c2024-04-25 17:10:57 +01003540 ffa_memory_retrieve_complete_from_hyp(share_state);
3541
J-Alves4f0d9c12024-01-17 17:23:11 +00003542 return ffa_memory_retrieve_resp(total_length, fragment_length);
3543}
3544
3545struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
3546 struct ffa_memory_region *retrieve_request,
3547 uint32_t retrieve_request_length,
3548 struct mpool *page_pool)
3549{
3550 ffa_memory_handle_t handle = retrieve_request->handle;
3551 struct share_states_locked share_states;
3552 struct ffa_memory_share_state *share_state;
3553 struct ffa_value ret;
3554
3555 dump_share_states();
3556
3557 share_states = share_states_lock();
3558 share_state = get_share_state(share_states, handle);
3559 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003560 dlog_verbose("Invalid handle %#lx for FFA_MEM_RETRIEVE_REQ.\n",
J-Alves4f0d9c12024-01-17 17:23:11 +00003561 handle);
3562 ret = ffa_error(FFA_INVALID_PARAMETERS);
3563 goto out;
3564 }
3565
3566 if (is_ffa_hypervisor_retrieve_request(retrieve_request, to_locked)) {
3567 ret = ffa_hypervisor_retrieve_request(share_state, to_locked,
3568 retrieve_request);
3569 } else {
3570 ret = ffa_partition_retrieve_request(
3571 share_states, share_state, to_locked, retrieve_request,
3572 retrieve_request_length, page_pool);
3573 }
3574
3575 /* Track use of the RX buffer if the handling has succeeded. */
3576 if (ret.func == FFA_MEM_RETRIEVE_RESP_32) {
3577 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
3578 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
3579 }
3580
Andrew Walbranca808b12020-05-15 17:22:28 +01003581out:
3582 share_states_unlock(&share_states);
3583 dump_share_states();
3584 return ret;
3585}
3586
J-Alves5da37d92022-10-24 16:33:48 +01003587/**
3588 * Determine expected fragment offset according to the FF-A version of
3589 * the caller.
3590 */
3591static uint32_t ffa_memory_retrieve_expected_offset_per_ffa_version(
3592 struct ffa_memory_region *memory_region,
Karl Meakin0e617d92024-04-05 12:55:22 +01003593 uint32_t retrieved_constituents_count, enum ffa_version ffa_version)
J-Alves5da37d92022-10-24 16:33:48 +01003594{
3595 uint32_t expected_fragment_offset;
3596 uint32_t composite_constituents_offset;
3597
Karl Meakin0e617d92024-04-05 12:55:22 +01003598 if (ffa_version >= FFA_VERSION_1_1) {
J-Alves5da37d92022-10-24 16:33:48 +01003599 /*
3600 * Hafnium operates memory regions in FF-A v1.1 format, so we
3601 * can retrieve the constituents offset from descriptor.
3602 */
3603 composite_constituents_offset =
3604 ffa_composite_constituent_offset(memory_region, 0);
Karl Meakin0e617d92024-04-05 12:55:22 +01003605 } else if (ffa_version == FFA_VERSION_1_0) {
J-Alves5da37d92022-10-24 16:33:48 +01003606 /*
3607 * If retriever is FF-A v1.0, determine the composite offset
3608 * as it is expected to have been configured in the
3609 * retrieve response.
3610 */
3611 composite_constituents_offset =
3612 sizeof(struct ffa_memory_region_v1_0) +
3613 RECEIVERS_COUNT_IN_RETRIEVE_RESP *
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003614 sizeof(struct ffa_memory_access_v1_0) +
J-Alves5da37d92022-10-24 16:33:48 +01003615 sizeof(struct ffa_composite_memory_region);
3616 } else {
3617 panic("%s received an invalid FF-A version.\n", __func__);
3618 }
3619
3620 expected_fragment_offset =
3621 composite_constituents_offset +
3622 retrieved_constituents_count *
3623 sizeof(struct ffa_memory_region_constituent) -
Karl Meakin66a38bd2024-05-28 16:00:56 +01003624 (size_t)(memory_region->memory_access_desc_size *
3625 (memory_region->receiver_count - 1));
J-Alves5da37d92022-10-24 16:33:48 +01003626
3627 return expected_fragment_offset;
3628}
3629
Andrew Walbranca808b12020-05-15 17:22:28 +01003630struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
3631 ffa_memory_handle_t handle,
3632 uint32_t fragment_offset,
J-Alves19e20cf2023-08-02 12:48:55 +01003633 ffa_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01003634 struct mpool *page_pool)
3635{
3636 struct ffa_memory_region *memory_region;
3637 struct share_states_locked share_states;
3638 struct ffa_memory_share_state *share_state;
3639 struct ffa_value ret;
3640 uint32_t fragment_index;
3641 uint32_t retrieved_constituents_count;
3642 uint32_t i;
3643 uint32_t expected_fragment_offset;
3644 uint32_t remaining_constituent_count;
3645 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01003646 uint32_t receiver_index;
J-Alves59ed0042022-07-28 18:26:41 +01003647 bool continue_ffa_hyp_mem_retrieve_req;
Andrew Walbranca808b12020-05-15 17:22:28 +01003648
3649 dump_share_states();
3650
3651 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003652 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003653 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003654 dlog_verbose("Invalid handle %#lx for FFA_MEM_FRAG_RX.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01003655 handle);
3656 ret = ffa_error(FFA_INVALID_PARAMETERS);
3657 goto out;
3658 }
3659
3660 memory_region = share_state->memory_region;
3661 CHECK(memory_region != NULL);
3662
Andrew Walbranca808b12020-05-15 17:22:28 +01003663 if (!share_state->sending_complete) {
3664 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003665 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01003666 "retrieve.\n",
3667 handle);
3668 ret = ffa_error(FFA_INVALID_PARAMETERS);
3669 goto out;
3670 }
3671
J-Alves59ed0042022-07-28 18:26:41 +01003672 /*
3673 * If retrieve request from the hypervisor has been initiated in the
3674 * given share_state, continue it, else assume it is a continuation of
3675 * retrieve request from a NWd VM.
3676 */
3677 continue_ffa_hyp_mem_retrieve_req =
3678 (to_locked.vm->id == HF_HYPERVISOR_VM_ID) &&
3679 (share_state->hypervisor_fragment_count != 0U) &&
J-Alves661e1b72023-08-02 13:39:40 +01003680 ffa_is_vm_id(sender_vm_id);
Andrew Walbranca808b12020-05-15 17:22:28 +01003681
J-Alves59ed0042022-07-28 18:26:41 +01003682 if (!continue_ffa_hyp_mem_retrieve_req) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003683 receiver_index = ffa_memory_region_get_receiver_index(
J-Alves59ed0042022-07-28 18:26:41 +01003684 memory_region, to_locked.vm->id);
3685
3686 if (receiver_index == memory_region->receiver_count) {
3687 dlog_verbose(
3688 "Caller of FFA_MEM_FRAG_RX (%x) is not a "
Karl Meakine8937d92024-03-19 16:04:25 +00003689 "borrower to memory sharing transaction "
3690 "(%lx)\n",
J-Alves59ed0042022-07-28 18:26:41 +01003691 to_locked.vm->id, handle);
3692 ret = ffa_error(FFA_INVALID_PARAMETERS);
3693 goto out;
3694 }
3695
3696 if (share_state->retrieved_fragment_count[receiver_index] ==
3697 0 ||
3698 share_state->retrieved_fragment_count[receiver_index] >=
3699 share_state->fragment_count) {
3700 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003701 "Retrieval of memory with handle %#lx not yet "
J-Alves59ed0042022-07-28 18:26:41 +01003702 "started or already completed (%d/%d fragments "
3703 "retrieved).\n",
3704 handle,
3705 share_state->retrieved_fragment_count
3706 [receiver_index],
3707 share_state->fragment_count);
3708 ret = ffa_error(FFA_INVALID_PARAMETERS);
3709 goto out;
3710 }
3711
3712 fragment_index =
3713 share_state->retrieved_fragment_count[receiver_index];
3714 } else {
3715 if (share_state->hypervisor_fragment_count == 0 ||
3716 share_state->hypervisor_fragment_count >=
3717 share_state->fragment_count) {
3718 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003719 "Retrieve of memory with handle %lx not "
J-Alves59ed0042022-07-28 18:26:41 +01003720 "started from hypervisor.\n",
3721 handle);
3722 ret = ffa_error(FFA_INVALID_PARAMETERS);
3723 goto out;
3724 }
3725
3726 if (memory_region->sender != sender_vm_id) {
3727 dlog_verbose(
3728 "Sender ID (%x) is not as expected for memory "
Karl Meakine8937d92024-03-19 16:04:25 +00003729 "handle %lx\n",
J-Alves59ed0042022-07-28 18:26:41 +01003730 sender_vm_id, handle);
3731 ret = ffa_error(FFA_INVALID_PARAMETERS);
3732 goto out;
3733 }
3734
3735 fragment_index = share_state->hypervisor_fragment_count;
3736
3737 receiver_index = 0;
3738 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003739
3740 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003741 * Check that the given fragment offset is correct by counting
3742 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01003743 */
3744 retrieved_constituents_count = 0;
3745 for (i = 0; i < fragment_index; ++i) {
3746 retrieved_constituents_count +=
3747 share_state->fragment_constituent_counts[i];
3748 }
J-Alvesc7484f12022-05-13 12:41:14 +01003749
3750 CHECK(memory_region->receiver_count > 0);
3751
Andrew Walbranca808b12020-05-15 17:22:28 +01003752 expected_fragment_offset =
J-Alves5da37d92022-10-24 16:33:48 +01003753 ffa_memory_retrieve_expected_offset_per_ffa_version(
3754 memory_region, retrieved_constituents_count,
3755 to_locked.vm->ffa_version);
3756
Andrew Walbranca808b12020-05-15 17:22:28 +01003757 if (fragment_offset != expected_fragment_offset) {
3758 dlog_verbose("Fragment offset was %d but expected %d.\n",
3759 fragment_offset, expected_fragment_offset);
3760 ret = ffa_error(FFA_INVALID_PARAMETERS);
3761 goto out;
3762 }
3763
J-Alves4f0d9c12024-01-17 17:23:11 +00003764 /*
3765 * When hafnium is the hypervisor, acquire the RX buffer of a VM, that
3766 * is currently ownder by the SPMC.
3767 */
3768 assert(plat_ffa_acquire_receiver_rx(to_locked, &ret));
J-Alves59ed0042022-07-28 18:26:41 +01003769
Andrew Walbranca808b12020-05-15 17:22:28 +01003770 remaining_constituent_count = ffa_memory_fragment_init(
3771 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3772 share_state->fragments[fragment_index],
3773 share_state->fragment_constituent_counts[fragment_index],
3774 &fragment_length);
3775 CHECK(remaining_constituent_count == 0);
J-Alves674e4de2024-01-17 16:20:32 +00003776
Andrew Walbranca808b12020-05-15 17:22:28 +01003777 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00003778 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbranca808b12020-05-15 17:22:28 +01003779
J-Alves59ed0042022-07-28 18:26:41 +01003780 if (!continue_ffa_hyp_mem_retrieve_req) {
3781 share_state->retrieved_fragment_count[receiver_index]++;
3782 if (share_state->retrieved_fragment_count[receiver_index] ==
3783 share_state->fragment_count) {
3784 ffa_memory_retrieve_complete(share_states, share_state,
3785 page_pool);
3786 }
3787 } else {
3788 share_state->hypervisor_fragment_count++;
3789
3790 ffa_memory_retrieve_complete_from_hyp(share_state);
3791 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003792 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
3793 .arg1 = (uint32_t)handle,
3794 .arg2 = (uint32_t)(handle >> 32),
3795 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003796
3797out:
3798 share_states_unlock(&share_states);
3799 dump_share_states();
3800 return ret;
3801}
3802
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003803struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003804 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003805 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003806{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003807 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003808 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003809 struct ffa_memory_share_state *share_state;
3810 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003811 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003812 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01003813 uint32_t receiver_index;
J-Alves3c5b2072022-11-21 12:45:40 +00003814 bool receivers_relinquished_memory;
Karl Meakin84710f32023-10-12 15:14:49 +01003815 ffa_memory_access_permissions_t receiver_permissions = {0};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003816
Andrew Walbrana65a1322020-04-06 19:32:32 +01003817 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003818 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003819 "Stream endpoints not supported (got %d endpoints on "
3820 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003821 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003822 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003823 }
3824
J-Alvesbd060342024-04-26 18:44:31 +01003825 if (vm_id_is_current_world(from_locked.vm->id) &&
3826 relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003827 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003828 "VM ID %d in relinquish message doesn't match calling "
3829 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01003830 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003831 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003832 }
3833
3834 dump_share_states();
3835
3836 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003837 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003838 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003839 dlog_verbose("Invalid handle %#lx for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003840 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003841 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003842 goto out;
3843 }
3844
Andrew Walbranca808b12020-05-15 17:22:28 +01003845 if (!share_state->sending_complete) {
3846 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003847 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01003848 "relinquish.\n",
3849 handle);
3850 ret = ffa_error(FFA_INVALID_PARAMETERS);
3851 goto out;
3852 }
3853
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003854 memory_region = share_state->memory_region;
3855 CHECK(memory_region != NULL);
3856
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003857 receiver_index = ffa_memory_region_get_receiver_index(
J-Alvesbd060342024-04-26 18:44:31 +01003858 memory_region, relinquish_request->endpoints[0]);
J-Alves8eb19162022-04-28 10:56:48 +01003859
3860 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003861 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003862 "VM ID %d tried to relinquish memory region "
Karl Meakine8937d92024-03-19 16:04:25 +00003863 "with handle %#lx and it is not a valid borrower.\n",
J-Alves8eb19162022-04-28 10:56:48 +01003864 from_locked.vm->id, handle);
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-Alves8eb19162022-04-28 10:56:48 +01003869 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01003870 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003871 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003872 "Memory with handle %#lx not yet fully retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01003873 "receiver %x can't relinquish.\n",
3874 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003875 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003876 goto out;
3877 }
3878
J-Alves3c5b2072022-11-21 12:45:40 +00003879 /*
3880 * Either clear if requested in relinquish call, or in a retrieve
3881 * request from one of the borrowers.
3882 */
3883 receivers_relinquished_memory = true;
3884
3885 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3886 struct ffa_memory_access *receiver =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003887 ffa_memory_region_get_receiver(memory_region, i);
3888 assert(receiver != NULL);
J-Alves3c5b2072022-11-21 12:45:40 +00003889 if (receiver->receiver_permissions.receiver ==
3890 from_locked.vm->id) {
J-Alves639ddfc2023-11-21 14:17:26 +00003891 receiver_permissions =
3892 receiver->receiver_permissions.permissions;
J-Alves3c5b2072022-11-21 12:45:40 +00003893 continue;
3894 }
3895
3896 if (share_state->retrieved_fragment_count[i] != 0U) {
3897 receivers_relinquished_memory = false;
3898 break;
3899 }
3900 }
3901
3902 clear = receivers_relinquished_memory &&
Daniel Boulby2e14ebe2024-01-15 16:21:44 +00003903 ((relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
3904 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003905
3906 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003907 * Clear is not allowed for memory that was shared, as the
3908 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003909 */
J-Alves95fbb312024-03-20 15:19:16 +00003910 if (clear && (share_state->share_func == FFA_MEM_SHARE_32 ||
3911 share_state->share_func == FFA_MEM_SHARE_64)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003912 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003913 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003914 goto out;
3915 }
3916
J-Alvesb886d492024-04-15 10:55:29 +01003917 if (clear && receiver_permissions.data_access == FFA_DATA_ACCESS_RO) {
J-Alves639ddfc2023-11-21 14:17:26 +00003918 dlog_verbose("%s: RO memory can't use clear memory flag.\n",
3919 __func__);
3920 ret = ffa_error(FFA_DENIED);
3921 goto out;
3922 }
3923
Andrew Walbranca808b12020-05-15 17:22:28 +01003924 ret = ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01003925 from_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003926 share_state->fragment_constituent_counts,
J-Alves69cdfd92024-04-26 11:40:59 +01003927 share_state->fragment_count, share_state->sender_orig_mode,
3928 page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003929
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003930 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003931 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003932 * Mark memory handle as not retrieved, so it can be
3933 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003934 */
J-Alves8eb19162022-04-28 10:56:48 +01003935 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003936 }
3937
3938out:
3939 share_states_unlock(&share_states);
3940 dump_share_states();
3941 return ret;
3942}
3943
3944/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01003945 * Validates that the reclaim transition is allowed for the given
3946 * handle, updates the page table of the reclaiming VM, and frees the
3947 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003948 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003949struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01003950 ffa_memory_handle_t handle,
3951 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003952 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003953{
3954 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003955 struct ffa_memory_share_state *share_state;
3956 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003957 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003958
3959 dump_share_states();
3960
3961 share_states = share_states_lock();
Karl Meakin52cdfe72023-06-30 14:49:10 +01003962
Karl Meakin4a2854a2023-06-30 16:26:52 +01003963 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003964 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003965 dlog_verbose("Invalid handle %#lx for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003966 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003967 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003968 goto out;
3969 }
Karl Meakin4a2854a2023-06-30 16:26:52 +01003970 memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003971
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003972 CHECK(memory_region != NULL);
3973
J-Alvesa9cd7e32022-07-01 13:49:33 +01003974 if (vm_id_is_current_world(to_locked.vm->id) &&
3975 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003976 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003977 "VM %#x attempted to reclaim memory handle %#lx "
Olivier Deprezf92e5d42020-11-13 16:00:54 +01003978 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003979 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003980 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003981 goto out;
3982 }
3983
Andrew Walbranca808b12020-05-15 17:22:28 +01003984 if (!share_state->sending_complete) {
3985 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003986 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01003987 "reclaim.\n",
3988 handle);
3989 ret = ffa_error(FFA_INVALID_PARAMETERS);
3990 goto out;
3991 }
3992
J-Alves752236c2022-04-28 11:07:47 +01003993 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3994 if (share_state->retrieved_fragment_count[i] != 0) {
J-Alves9bbcb872024-04-25 17:19:00 +01003995 struct ffa_memory_access *receiver =
3996 ffa_memory_region_get_receiver(memory_region,
3997 i);
3998
3999 assert(receiver != NULL);
4000 (void)receiver;
J-Alves752236c2022-04-28 11:07:47 +01004001 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01004002 "Tried to reclaim memory handle %#lx that has "
4003 "not been relinquished by all borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01004004 handle,
J-Alves9bbcb872024-04-25 17:19:00 +01004005 receiver->receiver_permissions.receiver);
J-Alves752236c2022-04-28 11:07:47 +01004006 ret = ffa_error(FFA_DENIED);
4007 goto out;
4008 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004009 }
4010
Andrew Walbranca808b12020-05-15 17:22:28 +01004011 ret = ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01004012 to_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01004013 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00004014 share_state->fragment_count, share_state->sender_orig_mode,
J-Alves460d36c2023-10-12 17:02:15 +01004015 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool,
J-Alvesfd206052023-05-22 16:45:00 +01004016 NULL, share_state->memory_protected);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004017
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01004018 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004019 share_state_free(share_states, share_state, page_pool);
J-Alves3c5b2072022-11-21 12:45:40 +00004020 dlog_verbose("Freed share state after successful reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004021 }
4022
4023out:
4024 share_states_unlock(&share_states);
4025 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01004026}