blob: e6d167b5d14183231aae1f537b599ba9976cb8b0 [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
Daniel Boulbyfd374b82024-07-31 14:31:16 +0100666 if (ffa_version == FFA_VERSION_1_1) {
667 /*
668 * Since the reserved field is at the end of the
669 * Endpoint Memory Access Descriptor we must
670 * cast to ffa_memory_access_v1_0 as they match.
671 * Since all fields except reserved in the
672 * Endpoint Memory Access Descriptor have the
673 * same offsets across all versions this cast is
674 * not required when accessing other fields in
675 * the future.
676 */
677 struct ffa_memory_access_v1_0 *receiver_v1_0 =
678 (struct ffa_memory_access_v1_0 *)
679 receiver;
680 if (receiver_v1_0->reserved_0 != 0) {
681 dlog_verbose(
682 "Reserved field in the memory "
683 "access descriptor must be "
684 "zero. Currently reciever %zu "
685 "has a reserved field with a "
686 "value of %lu\n",
687 i, receiver_v1_0->reserved_0);
688 return false;
689 }
690
691 } else {
692 if (receiver->reserved_0 != 0) {
693 dlog_verbose(
694 "Reserved field in the memory "
695 "access descriptor must be "
696 "zero. Currently reciever %zu "
697 "has a reserved field with a "
698 "value of %lu\n",
699 i, receiver->reserved_0);
700 return false;
701 }
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100702 }
703 }
704
705 /* Check composite offset values are equal for all receivers. */
706 composite_offset = receiver->composite_memory_region_offset;
707 if (composite_offset != composite_offset_0) {
708 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +0000709 "Composite offset %x differs from %x in "
710 "index\n",
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100711 composite_offset, composite_offset_0);
712 return false;
713 }
714 }
715 return true;
716}
717
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000718/**
J-Alves460d36c2023-10-12 17:02:15 +0100719 * If the receivers for the memory management operation are all from the
720 * secure world and this isn't a FFA_MEM_SHARE, then request memory security
721 * state update by returning MAP_ACTION_CHECK_PROTECT.
722 */
723static enum ffa_map_action ffa_mem_send_get_map_action(
724 bool all_receivers_from_current_world, ffa_id_t sender_id,
725 uint32_t mem_func_id)
726{
J-Alves95fbb312024-03-20 15:19:16 +0000727 const bool is_memory_share_abi = mem_func_id == FFA_MEM_SHARE_32 ||
728 mem_func_id == FFA_MEM_SHARE_64;
729 const bool protect_memory =
730 (!is_memory_share_abi && all_receivers_from_current_world &&
731 ffa_is_vm_id(sender_id));
J-Alves460d36c2023-10-12 17:02:15 +0100732
733 return protect_memory ? MAP_ACTION_CHECK_PROTECT : MAP_ACTION_CHECK;
734}
735
736/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000737 * Verify that all pages have the same mode, that the starting mode
738 * constitutes a valid state and obtain the next mode to apply
J-Alves460d36c2023-10-12 17:02:15 +0100739 * to the sending VM. It outputs the mapping action that needs to be
740 * invoked for the given memory range. On memory lend/donate there
741 * could be a need to protect the memory from the normal world.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000742 *
743 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100744 * 1) FFA_DENIED if a state transition was not found;
745 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100746 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100747 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100748 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100749 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
750 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000751 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100752static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100753 struct vm_locked from, uint32_t share_func,
Daniel Boulbya76fd912024-02-22 14:22:15 +0000754 struct ffa_memory_region *memory_region, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100755 struct ffa_memory_region_constituent **fragments,
756 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby4b846eb2024-05-23 17:32:23 +0100757 uint32_t *from_mode, enum ffa_map_action *map_action, bool zero)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000758{
759 const uint32_t state_mask =
760 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100761 struct ffa_value ret;
J-Alves460d36c2023-10-12 17:02:15 +0100762 bool all_receivers_from_current_world = true;
Daniel Boulbya76fd912024-02-22 14:22:15 +0000763 uint32_t receivers_count = memory_region->receiver_count;
J-Alves95fbb312024-03-20 15:19:16 +0000764 const bool is_memory_lend = (share_func == FFA_MEM_LEND_32) ||
765 (share_func == FFA_MEM_LEND_64);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000766
Andrew Walbranca808b12020-05-15 17:22:28 +0100767 ret = constituents_get_mode(from, orig_from_mode, fragments,
768 fragment_constituent_counts,
769 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100770 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100771 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100772 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100773 }
774
Daniel Boulby6e261362024-06-13 16:53:00 +0100775 /*
776 * Check requested memory type is valid with the memory type of the
777 * owner. E.g. they follow the memory type precedence where Normal
778 * memory is more permissive than device and therefore device memory
779 * can only be shared as device memory.
780 */
781 if (memory_region->attributes.type == FFA_MEMORY_NORMAL_MEM &&
782 (*orig_from_mode & MM_MODE_D) != 0U) {
783 dlog_verbose(
784 "Send device memory as Normal memory is not allowed\n");
785 return ffa_error(FFA_DENIED);
786 }
787
Daniel Boulby63af1fa2024-03-18 14:17:31 +0000788 /* Device memory regions can only be lent a single borrower. */
Daniel Boulby9764ff62024-01-30 17:47:39 +0000789 if ((*orig_from_mode & MM_MODE_D) != 0U &&
J-Alves95fbb312024-03-20 15:19:16 +0000790 !(is_memory_lend && receivers_count == 1)) {
Daniel Boulby9764ff62024-01-30 17:47:39 +0000791 dlog_verbose(
Daniel Boulby63af1fa2024-03-18 14:17:31 +0000792 "Device memory can only be lent to a single borrower "
793 "(mode is %#x).\n",
Daniel Boulby9764ff62024-01-30 17:47:39 +0000794 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100795 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000796 }
797
798 /*
799 * Ensure the sender is the owner and has exclusive access to the
800 * memory.
801 */
802 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100803 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100804 }
805
Daniel Boulby4b846eb2024-05-23 17:32:23 +0100806 /*
807 * Memory cannot be zeroed during the lend/donate operation if the
808 * sender only has RO access.
809 */
810 if ((*orig_from_mode & MM_MODE_W) == 0 && zero == true) {
811 dlog_verbose(
812 "Cannot zero memory when the sender doesn't have "
813 "write access\n");
814 return ffa_error(FFA_DENIED);
815 }
816
Daniel Boulbya76fd912024-02-22 14:22:15 +0000817 assert(receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100818
J-Alves363f5722022-04-25 17:37:37 +0100819 for (uint32_t i = 0U; i < receivers_count; i++) {
Daniel Boulbya76fd912024-02-22 14:22:15 +0000820 struct ffa_memory_access *receiver =
821 ffa_memory_region_get_receiver(memory_region, i);
822 assert(receiver != NULL);
J-Alves363f5722022-04-25 17:37:37 +0100823 ffa_memory_access_permissions_t permissions =
Daniel Boulbya76fd912024-02-22 14:22:15 +0000824 receiver->receiver_permissions.permissions;
J-Alves363f5722022-04-25 17:37:37 +0100825 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
826 permissions, *orig_from_mode);
827
J-Alves788b4492023-04-18 14:01:23 +0100828 /*
829 * The assumption is that at this point, the operation from
830 * SP to a receiver VM, should have returned an FFA_ERROR
831 * already.
832 */
833 if (!ffa_is_vm_id(from.vm->id)) {
834 assert(!ffa_is_vm_id(
Daniel Boulbya76fd912024-02-22 14:22:15 +0000835 receiver->receiver_permissions.receiver));
J-Alves788b4492023-04-18 14:01:23 +0100836 }
837
J-Alves460d36c2023-10-12 17:02:15 +0100838 /* Track if all senders are from current world. */
839 all_receivers_from_current_world =
840 all_receivers_from_current_world &&
841 vm_id_is_current_world(
Daniel Boulbya76fd912024-02-22 14:22:15 +0000842 receiver->receiver_permissions.receiver);
J-Alves460d36c2023-10-12 17:02:15 +0100843
J-Alves363f5722022-04-25 17:37:37 +0100844 if ((*orig_from_mode & required_from_mode) !=
845 required_from_mode) {
846 dlog_verbose(
847 "Sender tried to send memory with permissions "
J-Alves788b4492023-04-18 14:01:23 +0100848 "which required mode %#x but only had %#x "
849 "itself.\n",
J-Alves363f5722022-04-25 17:37:37 +0100850 required_from_mode, *orig_from_mode);
851 return ffa_error(FFA_DENIED);
852 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000853 }
854
J-Alves460d36c2023-10-12 17:02:15 +0100855 *map_action = ffa_mem_send_get_map_action(
856 all_receivers_from_current_world, from.vm->id, share_func);
857
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000858 /* Find the appropriate new mode. */
859 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000860 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +0000861 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100862 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000863 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100864 break;
J-Alves95fbb312024-03-20 15:19:16 +0000865 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100866 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000867 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100868 break;
J-Alves95fbb312024-03-20 15:19:16 +0000869 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100870 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000871 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100872 break;
873
Jose Marinho75509b42019-04-09 09:34:59 +0100874 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100875 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100876 }
877
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100878 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000879}
880
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100881static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000882 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100883 struct ffa_memory_region_constituent **fragments,
884 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves69cdfd92024-04-26 11:40:59 +0100885 uint32_t *from_mode, enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000886{
887 const uint32_t state_mask =
888 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
889 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100890 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000891
J-Alves69cdfd92024-04-26 11:40:59 +0100892 assert(map_action != NULL);
893 if (vm_id_is_current_world(from.vm->id)) {
894 *map_action = MAP_ACTION_COMMIT;
895 } else {
896 /*
897 * No need to check the attributes of caller.
898 * The assumption is that the retrieve request of the receiver
899 * also used the MAP_ACTION_NONE, and no update was done to the
900 * page tables. When the receiver is not at the secure virtual
901 * instance SPMC doesn't manage its S2 translation (i.e. when
902 * the receiver is a VM).
903 */
904 *map_action = MAP_ACTION_NONE;
905
906 return (struct ffa_value){.func = FFA_SUCCESS_32};
907 }
908
Andrew Walbranca808b12020-05-15 17:22:28 +0100909 ret = constituents_get_mode(from, orig_from_mode, fragments,
910 fragment_constituent_counts,
911 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100912 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100913 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000914 }
915
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000916 /*
917 * Ensure the relinquishing VM is not the owner but has access to the
918 * memory.
919 */
920 orig_from_state = *orig_from_mode & state_mask;
921 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
922 dlog_verbose(
923 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100924 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000925 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100926 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000927 }
928
929 /* Find the appropriate new mode. */
930 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
931
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100932 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000933}
934
935/**
936 * Verify that all pages have the same mode, that the starting mode
937 * constitutes a valid state and obtain the next mode to apply
938 * to the retrieving VM.
939 *
940 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100941 * 1) FFA_DENIED if a state transition was not found;
942 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100943 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100944 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100945 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100946 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
947 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000948 */
J-Alvesfc19b372022-07-06 12:17:35 +0100949struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000950 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100951 struct ffa_memory_region_constituent **fragments,
952 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby71d887b2024-06-28 16:38:06 +0100953 uint32_t sender_orig_mode, uint32_t *to_mode, bool memory_protected,
J-Alvesfd206052023-05-22 16:45:00 +0100954 enum ffa_map_action *map_action)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000955{
956 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100957 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000958
Andrew Walbranca808b12020-05-15 17:22:28 +0100959 ret = constituents_get_mode(to, &orig_to_mode, fragments,
960 fragment_constituent_counts,
961 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100962 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100963 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100964 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000965 }
966
J-Alves460d36c2023-10-12 17:02:15 +0100967 /* Find the appropriate new mode. */
Daniel Boulby71d887b2024-06-28 16:38:06 +0100968 *to_mode = sender_orig_mode;
J-Alves460d36c2023-10-12 17:02:15 +0100969
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100970 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000971 /*
972 * If the original ffa memory send call has been processed
973 * successfully, it is expected the orig_to_mode would overlay
974 * with `state_mask`, as a result of the function
975 * `ffa_send_check_transition`.
J-Alvesfd206052023-05-22 16:45:00 +0100976 *
977 * If Hafnium is the SPMC:
978 * - Caller of the reclaim interface is an SP, the memory shall
979 * have been protected throughout the flow.
980 * - Caller of the reclaim is from the NWd, the memory may have
981 * been protected at the time of lending/donating the memory.
982 * In such case, set action to unprotect memory in the
983 * handling of reclaim operation.
984 * - If Hafnium is the hypervisor memory shall never have been
985 * protected in memory lend/share/donate.
986 *
987 * More details in the doc comment of the function
988 * `ffa_region_group_identity_map`.
J-Alves9256f162021-12-09 13:18:43 +0000989 */
J-Alves59ed0042022-07-28 18:26:41 +0100990 if (vm_id_is_current_world(to.vm->id)) {
991 assert((orig_to_mode &
992 (MM_MODE_INVALID | MM_MODE_UNOWNED |
993 MM_MODE_SHARED)) != 0U);
J-Alvesfd206052023-05-22 16:45:00 +0100994 assert(!memory_protected);
995 } else if (to.vm->id == HF_OTHER_WORLD_ID &&
996 map_action != NULL && memory_protected) {
997 *map_action = MAP_ACTION_COMMIT_UNPROTECT;
J-Alves59ed0042022-07-28 18:26:41 +0100998 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000999 } else {
J-Alves69cdfd92024-04-26 11:40:59 +01001000 if (!vm_id_is_current_world(to.vm->id)) {
1001 assert(map_action != NULL);
1002 *map_action = MAP_ACTION_NONE;
1003 return (struct ffa_value){.func = FFA_SUCCESS_32};
1004 }
1005
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001006 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01001007 * If the retriever is from virtual FF-A instance:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001008 * Ensure the retriever has the expected state. We don't care
1009 * about the MM_MODE_SHARED bit; either with or without it set
1010 * are both valid representations of the !O-NA state.
1011 */
J-Alvesa9cd7e32022-07-01 13:49:33 +01001012 if (vm_id_is_current_world(to.vm->id) &&
Karl Meakin5e996992024-05-20 11:27:07 +01001013 !vm_is_primary(to.vm) &&
J-Alvesa9cd7e32022-07-01 13:49:33 +01001014 (orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
1015 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001016 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001017 }
J-Alves460d36c2023-10-12 17:02:15 +01001018
1019 /*
1020 * If memory has been protected before, clear the NS bit to
1021 * allow the secure access from the SP.
1022 */
1023 if (memory_protected) {
1024 *to_mode &= ~plat_ffa_other_world_mode();
1025 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001026 }
1027
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001028 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00001029 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001030 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001031 *to_mode |= 0;
1032 break;
J-Alves95fbb312024-03-20 15:19:16 +00001033 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001034 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001035 *to_mode |= MM_MODE_UNOWNED;
1036 break;
J-Alves95fbb312024-03-20 15:19:16 +00001037 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001038 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001039 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
1040 break;
1041
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001042 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001043 *to_mode |= 0;
1044 break;
1045
1046 default:
Andrew Walbranca808b12020-05-15 17:22:28 +01001047 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001048 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001049 }
1050
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001051 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +01001052}
Jose Marinho09b1db82019-08-08 09:16:59 +01001053
J-Alvescf6253e2024-01-03 13:48:48 +00001054/*
1055 * Performs the operations related to the `action` MAP_ACTION_CHECK*.
1056 * Returns:
1057 * - FFA_SUCCESS_32: if all goes well.
1058 * - FFA_ERROR_32: with FFA_NO_MEMORY, if there is no memory to manage
1059 * the page table update. Or error code provided by the function
1060 * `arch_memory_protect`.
1061 */
1062static struct ffa_value ffa_region_group_check_actions(
1063 struct vm_locked vm_locked, paddr_t pa_begin, paddr_t pa_end,
1064 struct mpool *ppool, uint32_t mode, enum ffa_map_action action,
1065 bool *memory_protected)
1066{
1067 struct ffa_value ret;
1068 bool is_memory_protected;
1069
1070 if (!vm_identity_prepare(vm_locked, pa_begin, pa_end, mode, ppool)) {
1071 dlog_verbose(
1072 "%s: memory can't be mapped to %x due to lack of "
Karl Meakine8937d92024-03-19 16:04:25 +00001073 "memory. Base: %lx end: %lx\n",
J-Alvescf6253e2024-01-03 13:48:48 +00001074 __func__, vm_locked.vm->id, pa_addr(pa_begin),
1075 pa_addr(pa_end));
1076 return ffa_error(FFA_NO_MEMORY);
1077 }
1078
1079 switch (action) {
1080 case MAP_ACTION_CHECK:
1081 /* No protect requested. */
1082 is_memory_protected = false;
1083 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
1084 break;
1085 case MAP_ACTION_CHECK_PROTECT: {
1086 paddr_t last_protected_pa = pa_init(0);
1087
1088 ret = arch_memory_protect(pa_begin, pa_end, &last_protected_pa);
1089
1090 is_memory_protected = (ret.func == FFA_SUCCESS_32);
1091
1092 /*
1093 * - If protect memory has failed with FFA_DENIED, means some
1094 * range of memory was in the wrong state. In such case, SPM
1095 * reverts the state of the pages that were successfully
1096 * updated.
1097 * - If protect memory has failed with FFA_NOT_SUPPORTED, it
1098 * means the platform doesn't support the protection mechanism.
1099 * That said, it still permits the page table update to go
1100 * through. The variable
1101 * `is_memory_protected` will be equal to false.
1102 * - If protect memory has failed with FFA_INVALID_PARAMETERS,
1103 * break from switch and return the error.
1104 */
1105 if (ret.func == FFA_ERROR_32) {
1106 assert(!is_memory_protected);
1107 if (ffa_error_code(ret) == FFA_DENIED &&
1108 pa_addr(last_protected_pa) != (uintptr_t)0) {
1109 CHECK(arch_memory_unprotect(
1110 pa_begin,
1111 pa_add(last_protected_pa, PAGE_SIZE)));
1112 } else if (ffa_error_code(ret) == FFA_NOT_SUPPORTED) {
1113 ret = (struct ffa_value){
1114 .func = FFA_SUCCESS_32,
1115 };
1116 }
1117 }
1118 } break;
1119 default:
1120 panic("%s: invalid action to process %x\n", __func__, action);
1121 }
1122
1123 if (memory_protected != NULL) {
1124 *memory_protected = is_memory_protected;
1125 }
1126
1127 return ret;
1128}
1129
1130static void ffa_region_group_commit_actions(struct vm_locked vm_locked,
1131 paddr_t pa_begin, paddr_t pa_end,
1132 struct mpool *ppool, uint32_t mode,
1133 enum ffa_map_action action)
1134{
1135 switch (action) {
1136 case MAP_ACTION_COMMIT_UNPROTECT:
1137 /*
1138 * Checking that it should succeed because SPM should be
1139 * unprotecting memory that it had protected before.
1140 */
1141 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1142 case MAP_ACTION_COMMIT:
1143 vm_identity_commit(vm_locked, pa_begin, pa_end, mode, ppool,
1144 NULL);
1145 break;
1146 default:
1147 panic("%s: invalid action to process %x\n", __func__, action);
1148 }
1149}
1150
Jose Marinho09b1db82019-08-08 09:16:59 +01001151/**
J-Alves063ad832023-10-03 18:05:40 +01001152 * Helper function to revert a failed "Protect" action from the SPMC:
1153 * - `fragment_count`: should specify the number of fragments to traverse from
1154 * `fragments`. This may not be the full amount of fragments that are part of
1155 * the share_state structure.
1156 * - `fragment_constituent_counts`: array holding the amount of constituents
1157 * per fragment.
1158 * - `end`: pointer to the constituent that failed the "protect" action. It
1159 * shall be part of the last fragment, and it shall make the loop below break.
1160 */
1161static void ffa_region_group_fragments_revert_protect(
1162 struct ffa_memory_region_constituent **fragments,
1163 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1164 const struct ffa_memory_region_constituent *end)
1165{
1166 for (uint32_t i = 0; i < fragment_count; ++i) {
1167 for (uint32_t j = 0; j < fragment_constituent_counts[i]; ++j) {
1168 struct ffa_memory_region_constituent *constituent =
1169 &fragments[i][j];
1170 size_t size = constituent->page_count * PAGE_SIZE;
1171 paddr_t pa_begin =
1172 pa_from_ipa(ipa_init(constituent->address));
1173 paddr_t pa_end = pa_add(pa_begin, size);
1174
Karl Meakine8937d92024-03-19 16:04:25 +00001175 dlog_verbose("%s: reverting fragment %lx size %zx\n",
J-Alves063ad832023-10-03 18:05:40 +01001176 __func__, pa_addr(pa_begin), size);
1177
1178 if (constituent == end) {
1179 /*
1180 * The last constituent is expected to be in the
1181 * last fragment.
1182 */
1183 assert(i == fragment_count - 1);
1184 break;
1185 }
1186
1187 CHECK(arch_memory_unprotect(pa_begin, pa_end));
1188 }
1189 }
1190}
1191
1192/**
Jose Marinho09b1db82019-08-08 09:16:59 +01001193 * Updates a VM's page table such that the given set of physical address ranges
1194 * are mapped in the address space at the corresponding address ranges, in the
1195 * mode provided.
1196 *
J-Alves0a83dc22023-05-05 09:50:37 +01001197 * The enum ffa_map_action determines the action taken from a call to the
1198 * function below:
1199 * - If action is MAP_ACTION_CHECK, the page tables will be allocated from the
1200 * mpool but no mappings will actually be updated. This function must always
1201 * be called first with action set to MAP_ACTION_CHECK to check that it will
1202 * succeed before calling ffa_region_group_identity_map with whichever one of
1203 * the remaining actions, to avoid leaving the page table in a half-updated
1204 * state.
1205 * - The action MAP_ACTION_COMMIT allocates the page tables from the mpool, and
1206 * changes the memory mappings.
J-Alvescf6253e2024-01-03 13:48:48 +00001207 * - The action MAP_ACTION_CHECK_PROTECT extends the MAP_ACTION_CHECK with an
1208 * invocation to the monitor to update the security state of the memory,
1209 * to that of the SPMC.
1210 * - The action MAP_ACTION_COMMIT_UNPROTECT extends the MAP_ACTION_COMMIT
1211 * with a call into the monitor, to reset the security state of memory
1212 * that has priorly been mapped with the MAP_ACTION_CHECK_PROTECT action.
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001213 * vm_ptable_defrag should always be called after a series of page table
1214 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +01001215 *
J-Alvescf6253e2024-01-03 13:48:48 +00001216 * If all goes well, returns FFA_SUCCESS_32; or FFA_ERROR, with following
1217 * error codes:
1218 * - FFA_INVALID_PARAMETERS: invalid range of memory.
1219 * - FFA_DENIED:
1220 *
Jose Marinho09b1db82019-08-08 09:16:59 +01001221 * made to memory mappings.
1222 */
J-Alvescf6253e2024-01-03 13:48:48 +00001223struct ffa_value ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +00001224 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001225 struct ffa_memory_region_constituent **fragments,
1226 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alvescf6253e2024-01-03 13:48:48 +00001227 uint32_t mode, struct mpool *ppool, enum ffa_map_action action,
1228 bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001229{
Andrew Walbranca808b12020-05-15 17:22:28 +01001230 uint32_t i;
1231 uint32_t j;
J-Alvescf6253e2024-01-03 13:48:48 +00001232 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001233
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001234 if (vm_locked.vm->el0_partition) {
1235 mode |= MM_MODE_USER | MM_MODE_NG;
1236 }
1237
Andrew Walbranca808b12020-05-15 17:22:28 +01001238 /* Iterate over the memory region constituents within each fragment. */
1239 for (i = 0; i < fragment_count; ++i) {
1240 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
J-Alves063ad832023-10-03 18:05:40 +01001241 struct ffa_memory_region_constituent *constituent =
1242 &fragments[i][j];
1243 size_t size = constituent->page_count * PAGE_SIZE;
Andrew Walbranca808b12020-05-15 17:22:28 +01001244 paddr_t pa_begin =
J-Alves063ad832023-10-03 18:05:40 +01001245 pa_from_ipa(ipa_init(constituent->address));
Andrew Walbranca808b12020-05-15 17:22:28 +01001246 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001247 uint32_t pa_bits =
1248 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +01001249
1250 /*
1251 * Ensure the requested region falls into system's PA
1252 * range.
1253 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +02001254 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
1255 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +01001256 dlog_error("Region is outside of PA Range\n");
J-Alvescf6253e2024-01-03 13:48:48 +00001257 return ffa_error(FFA_INVALID_PARAMETERS);
Federico Recanati4fd065d2021-12-13 20:06:23 +01001258 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001259
J-Alvescf6253e2024-01-03 13:48:48 +00001260 if (action <= MAP_ACTION_CHECK_PROTECT) {
1261 ret = ffa_region_group_check_actions(
1262 vm_locked, pa_begin, pa_end, ppool,
1263 mode, action, memory_protected);
J-Alves063ad832023-10-03 18:05:40 +01001264
1265 if (ret.func == FFA_ERROR_32 &&
1266 ffa_error_code(ret) == FFA_DENIED) {
1267 if (memory_protected != NULL) {
1268 assert(!*memory_protected);
1269 }
1270
1271 ffa_region_group_fragments_revert_protect(
1272 fragments,
1273 fragment_constituent_counts,
1274 i + 1, constituent);
1275 break;
1276 }
J-Alvescf6253e2024-01-03 13:48:48 +00001277 } else if (action >= MAP_ACTION_COMMIT &&
1278 action < MAP_ACTION_MAX) {
1279 ffa_region_group_commit_actions(
1280 vm_locked, pa_begin, pa_end, ppool,
1281 mode, action);
1282 ret = (struct ffa_value){
1283 .func = FFA_SUCCESS_32};
1284 } else {
1285 panic("%s: Unknown ffa_map_action.\n",
1286 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001287 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001288 }
1289 }
1290
J-Alvescf6253e2024-01-03 13:48:48 +00001291 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001292}
1293
1294/**
1295 * Clears a region of physical memory by overwriting it with zeros. The data is
1296 * flushed from the cache so the memory has been cleared across the system.
1297 */
J-Alves7db32002021-12-14 14:44:50 +00001298static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
1299 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +01001300{
1301 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +00001302 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +01001303 * global mapping of the whole range. Such an approach will limit
1304 * the changes to stage-1 tables and will allow only local
1305 * invalidation.
1306 */
1307 bool ret;
1308 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +00001309 void *ptr = mm_identity_map(stage1_locked, begin, end,
1310 MM_MODE_W | (extra_mode_attributes &
1311 plat_ffa_other_world_mode()),
1312 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001313 size_t size = pa_difference(begin, end);
1314
1315 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001316 goto fail;
1317 }
1318
1319 memset_s(ptr, size, 0, size);
1320 arch_mm_flush_dcache(ptr, size);
1321 mm_unmap(stage1_locked, begin, end, ppool);
1322
1323 ret = true;
1324 goto out;
1325
1326fail:
1327 ret = false;
1328
1329out:
1330 mm_unlock_stage1(&stage1_locked);
1331
1332 return ret;
1333}
1334
1335/**
1336 * Clears a region of physical memory by overwriting it with zeros. The data is
1337 * flushed from the cache so the memory has been cleared across the system.
1338 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001339static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +00001340 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01001341 struct ffa_memory_region_constituent **fragments,
1342 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1343 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001344{
1345 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +01001346 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +01001347 bool ret = false;
1348
1349 /*
1350 * Create a local pool so any freed memory can't be used by another
1351 * thread. This is to ensure each constituent that is mapped can be
1352 * unmapped again afterwards.
1353 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001354 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001355
Andrew Walbranca808b12020-05-15 17:22:28 +01001356 /* Iterate over the memory region constituents within each fragment. */
1357 for (i = 0; i < fragment_count; ++i) {
1358 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001359
J-Alves8457f932023-10-11 16:41:45 +01001360 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001361 size_t size = fragments[i][j].page_count * PAGE_SIZE;
1362 paddr_t begin =
1363 pa_from_ipa(ipa_init(fragments[i][j].address));
1364 paddr_t end = pa_add(begin, size);
1365
J-Alves7db32002021-12-14 14:44:50 +00001366 if (!clear_memory(begin, end, &local_page_pool,
1367 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001368 /*
1369 * api_clear_memory will defrag on failure, so
1370 * no need to do it here.
1371 */
1372 goto out;
1373 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001374 }
1375 }
1376
Jose Marinho09b1db82019-08-08 09:16:59 +01001377 ret = true;
1378
1379out:
1380 mpool_fini(&local_page_pool);
1381 return ret;
1382}
1383
J-Alves5952d942022-12-22 16:03:00 +00001384static bool is_memory_range_within(ipaddr_t begin, ipaddr_t end,
1385 ipaddr_t in_begin, ipaddr_t in_end)
1386{
1387 return (ipa_addr(begin) >= ipa_addr(in_begin) &&
1388 ipa_addr(begin) < ipa_addr(in_end)) ||
1389 (ipa_addr(end) <= ipa_addr(in_end) &&
1390 ipa_addr(end) > ipa_addr(in_begin));
1391}
1392
1393/**
1394 * Receives a memory range and looks for overlaps with the remainder
1395 * constituents of the memory share/lend/donate operation. Assumes they are
1396 * passed in order to avoid having to loop over all the elements at each call.
1397 * The function only compares the received memory ranges with those that follow
1398 * within the same fragment, and subsequent fragments from the same operation.
1399 */
1400static bool ffa_memory_check_overlap(
1401 struct ffa_memory_region_constituent **fragments,
1402 const uint32_t *fragment_constituent_counts,
1403 const uint32_t fragment_count, const uint32_t current_fragment,
1404 const uint32_t current_constituent)
1405{
1406 uint32_t i = current_fragment;
1407 uint32_t j = current_constituent;
1408 ipaddr_t current_begin = ipa_init(fragments[i][j].address);
1409 const uint32_t current_page_count = fragments[i][j].page_count;
1410 size_t current_size = current_page_count * PAGE_SIZE;
1411 ipaddr_t current_end = ipa_add(current_begin, current_size - 1);
1412
1413 if (current_size == 0 ||
1414 current_size > UINT64_MAX - ipa_addr(current_begin)) {
Karl Meakine8937d92024-03-19 16:04:25 +00001415 dlog_verbose("Invalid page count. Addr: %zx page_count: %x\n",
1416 current_begin.ipa, current_page_count);
J-Alves5952d942022-12-22 16:03:00 +00001417 return false;
1418 }
1419
1420 for (; i < fragment_count; i++) {
1421 j = (i == current_fragment) ? j + 1 : 0;
1422
1423 for (; j < fragment_constituent_counts[i]; j++) {
1424 ipaddr_t begin = ipa_init(fragments[i][j].address);
1425 const uint32_t page_count = fragments[i][j].page_count;
1426 size_t size = page_count * PAGE_SIZE;
1427 ipaddr_t end = ipa_add(begin, size - 1);
1428
1429 if (size == 0 || size > UINT64_MAX - ipa_addr(begin)) {
1430 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00001431 "Invalid page count. Addr: %lx "
J-Alves5952d942022-12-22 16:03:00 +00001432 "page_count: %x\n",
Karl Meakine8937d92024-03-19 16:04:25 +00001433 begin.ipa, page_count);
J-Alves5952d942022-12-22 16:03:00 +00001434 return false;
1435 }
1436
1437 /*
1438 * Check if current ranges is within begin and end, as
1439 * well as the reverse. This should help optimize the
1440 * loop, and reduce the number of iterations.
1441 */
1442 if (is_memory_range_within(begin, end, current_begin,
1443 current_end) ||
1444 is_memory_range_within(current_begin, current_end,
1445 begin, end)) {
1446 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00001447 "Overlapping memory ranges: %#lx - "
1448 "%#lx with %#lx - %#lx\n",
J-Alves5952d942022-12-22 16:03:00 +00001449 ipa_addr(begin), ipa_addr(end),
1450 ipa_addr(current_begin),
1451 ipa_addr(current_end));
1452 return true;
1453 }
1454 }
1455 }
1456
1457 return false;
1458}
1459
Jose Marinho09b1db82019-08-08 09:16:59 +01001460/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001461 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +01001462 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001463 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +01001464 *
1465 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001466 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001467 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +01001468 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001469 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
1470 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001471 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +01001472 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001473 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +01001474 */
Daniel Boulbya76fd912024-02-22 14:22:15 +00001475static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001476 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001477 struct ffa_memory_region_constituent **fragments,
1478 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves8f11cde2022-12-21 16:18:22 +00001479 uint32_t composite_total_page_count, uint32_t share_func,
Daniel Boulbya76fd912024-02-22 14:22:15 +00001480 struct ffa_memory_region *memory_region, struct mpool *page_pool,
1481 uint32_t *orig_from_mode_ret, bool *memory_protected)
Jose Marinho09b1db82019-08-08 09:16:59 +01001482{
Andrew Walbranca808b12020-05-15 17:22:28 +01001483 uint32_t i;
J-Alves8f11cde2022-12-21 16:18:22 +00001484 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001485 uint32_t orig_from_mode;
J-Alves460d36c2023-10-12 17:02:15 +01001486 uint32_t clean_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001487 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001488 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001489 struct ffa_value ret;
J-Alves8f11cde2022-12-21 16:18:22 +00001490 uint32_t constituents_total_page_count = 0;
J-Alves460d36c2023-10-12 17:02:15 +01001491 enum ffa_map_action map_action = MAP_ACTION_CHECK;
Daniel Boulbya76fd912024-02-22 14:22:15 +00001492 bool clear = memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Jose Marinho09b1db82019-08-08 09:16:59 +01001493
1494 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001495 * Make sure constituents are properly aligned to a 64-bit boundary. If
1496 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +01001497 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001498 for (i = 0; i < fragment_count; ++i) {
1499 if (!is_aligned(fragments[i], 8)) {
1500 dlog_verbose("Constituents not aligned.\n");
1501 return ffa_error(FFA_INVALID_PARAMETERS);
1502 }
J-Alves8f11cde2022-12-21 16:18:22 +00001503 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
1504 constituents_total_page_count +=
1505 fragments[i][j].page_count;
J-Alves5952d942022-12-22 16:03:00 +00001506 if (ffa_memory_check_overlap(
1507 fragments, fragment_constituent_counts,
1508 fragment_count, i, j)) {
1509 return ffa_error(FFA_INVALID_PARAMETERS);
1510 }
J-Alves8f11cde2022-12-21 16:18:22 +00001511 }
1512 }
1513
1514 if (constituents_total_page_count != composite_total_page_count) {
1515 dlog_verbose(
1516 "Composite page count differs from calculated page "
1517 "count from constituents.\n");
1518 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +01001519 }
1520
1521 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001522 * Check if the state transition is lawful for the sender, ensure that
1523 * all constituents of a memory region being shared are at the same
1524 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +01001525 */
J-Alves460d36c2023-10-12 17:02:15 +01001526 ret = ffa_send_check_transition(
Daniel Boulbya76fd912024-02-22 14:22:15 +00001527 from_locked, share_func, memory_region, &orig_from_mode,
1528 fragments, fragment_constituent_counts, fragment_count,
Daniel Boulby4b846eb2024-05-23 17:32:23 +01001529 &from_mode, &map_action, clear);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001530 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001531 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001532 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001533 }
1534
Andrew Walbran37c574e2020-06-03 11:45:46 +01001535 if (orig_from_mode_ret != NULL) {
1536 *orig_from_mode_ret = orig_from_mode;
1537 }
1538
Jose Marinho09b1db82019-08-08 09:16:59 +01001539 /*
1540 * Create a local pool so any freed memory can't be used by another
1541 * thread. This is to ensure the original mapping can be restored if the
1542 * clear fails.
1543 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001544 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001545
1546 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001547 * First reserve all required memory for the new page table entries
1548 * without committing, to make sure the entire operation will succeed
1549 * without exhausting the page pool.
J-Alves460d36c2023-10-12 17:02:15 +01001550 * Provide the map_action as populated by 'ffa_send_check_transition'.
1551 * It may request memory to be protected.
Jose Marinho09b1db82019-08-08 09:16:59 +01001552 */
J-Alvescf6253e2024-01-03 13:48:48 +00001553 ret = ffa_region_group_identity_map(
1554 from_locked, fragments, fragment_constituent_counts,
J-Alves460d36c2023-10-12 17:02:15 +01001555 fragment_count, from_mode, page_pool, map_action,
1556 memory_protected);
J-Alvescf6253e2024-01-03 13:48:48 +00001557 if (ret.func == FFA_ERROR_32) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001558 goto out;
1559 }
1560
1561 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001562 * Update the mapping for the sender. This won't allocate because the
1563 * transaction was already prepared above, but may free pages in the
1564 * case that a whole block is being unmapped that was previously
1565 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +01001566 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001567 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001568 from_locked, fragments, fragment_constituent_counts,
1569 fragment_count, from_mode, &local_page_pool,
1570 MAP_ACTION_COMMIT, NULL)
1571 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001572
J-Alves460d36c2023-10-12 17:02:15 +01001573 /*
1574 * If memory has been protected, it is now part of the secure PAS
1575 * (happens for lend/donate from NWd to SWd), and the `orig_from_mode`
1576 * should have the MM_MODE_NS set, as such mask it in `clean_mode` for
1577 * SPM's S1 translation.
1578 * In case memory hasn't been protected, and it is in the non-secure
1579 * PAS (e.g. memory share from NWd to SWd), as such the SPM needs to
1580 * perform a non-secure memory access. In such case `clean_mode` takes
1581 * the same mode as `orig_from_mode`.
1582 */
1583 clean_mode = (memory_protected != NULL && *memory_protected)
1584 ? orig_from_mode & ~plat_ffa_other_world_mode()
1585 : orig_from_mode;
1586
Jose Marinho09b1db82019-08-08 09:16:59 +01001587 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves460d36c2023-10-12 17:02:15 +01001588 if (clear && !ffa_clear_memory_constituents(
1589 clean_mode, fragments, fragment_constituent_counts,
1590 fragment_count, page_pool)) {
1591 map_action = (memory_protected != NULL && *memory_protected)
1592 ? MAP_ACTION_COMMIT_UNPROTECT
1593 : MAP_ACTION_COMMIT;
1594
Jose Marinho09b1db82019-08-08 09:16:59 +01001595 /*
1596 * On failure, roll back by returning memory to the sender. This
1597 * may allocate pages which were previously freed into
1598 * `local_page_pool` by the call above, but will never allocate
1599 * more pages than that so can never fail.
1600 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001601 CHECK(ffa_region_group_identity_map(
J-Alvescf6253e2024-01-03 13:48:48 +00001602 from_locked, fragments,
1603 fragment_constituent_counts, fragment_count,
1604 orig_from_mode, &local_page_pool,
1605 MAP_ACTION_COMMIT, NULL)
1606 .func == FFA_SUCCESS_32);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001607 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +01001608 goto out;
1609 }
1610
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001611 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001612
1613out:
1614 mpool_fini(&local_page_pool);
1615
1616 /*
1617 * Tidy up the page table by reclaiming failed mappings (if there was an
1618 * error) or merging entries into blocks where possible (on success).
1619 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001620 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001621
1622 return ret;
1623}
1624
1625/**
1626 * Validates and maps memory shared from one VM to another.
1627 *
1628 * This function requires the calling context to hold the <to> lock.
1629 *
1630 * Returns:
1631 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001632 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001633 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001634 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001635 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001636 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001637 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001638struct ffa_value ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01001639 struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001640 struct ffa_memory_region_constituent **fragments,
1641 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves26483382023-04-20 12:01:49 +01001642 uint32_t sender_orig_mode, uint32_t share_func, bool clear,
J-Alves460d36c2023-10-12 17:02:15 +01001643 struct mpool *page_pool, uint32_t *response_mode, bool memory_protected)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001644{
Andrew Walbranca808b12020-05-15 17:22:28 +01001645 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001646 uint32_t to_mode;
1647 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001648 struct ffa_value ret;
J-Alvesfd206052023-05-22 16:45:00 +01001649 enum ffa_map_action map_action = MAP_ACTION_COMMIT;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001650
1651 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001652 * Make sure constituents are properly aligned to a 64-bit boundary. If
1653 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001654 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001655 for (i = 0; i < fragment_count; ++i) {
1656 if (!is_aligned(fragments[i], 8)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001657 dlog_verbose("Fragment not properly aligned.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +01001658 return ffa_error(FFA_INVALID_PARAMETERS);
1659 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001660 }
1661
1662 /*
Daniel Boulby4b846eb2024-05-23 17:32:23 +01001663 * Ensure the sender has write permissions if the memory needs to be
1664 * cleared.
1665 */
1666 if ((sender_orig_mode & MM_MODE_W) == 0 && clear == true) {
1667 dlog_verbose(
1668 "Cannot zero memory when the sender does not have "
1669 "write access\n");
1670 return ffa_error(FFA_DENIED);
1671 }
1672
1673 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001674 * Check if the state transition is lawful for the recipient, and ensure
1675 * that all constituents of the memory region being retrieved are at the
1676 * same state.
1677 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001678 ret = ffa_retrieve_check_transition(
1679 to_locked, share_func, fragments, fragment_constituent_counts,
J-Alvesfd206052023-05-22 16:45:00 +01001680 fragment_count, sender_orig_mode, &to_mode, memory_protected,
1681 &map_action);
J-Alves460d36c2023-10-12 17:02:15 +01001682
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001683 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001684 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001685 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001686 }
1687
1688 /*
J-Alves69cdfd92024-04-26 11:40:59 +01001689 * Create a local pool so any freed memory can't be used by
1690 * another thread. This is to ensure the original mapping can be
1691 * restored if the clear fails.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001692 */
1693 mpool_init_with_fallback(&local_page_pool, page_pool);
1694
1695 /*
J-Alves69cdfd92024-04-26 11:40:59 +01001696 * Memory retrieves from the NWd VMs don't require update to S2 PTs on
1697 * retrieve request.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001698 */
J-Alves69cdfd92024-04-26 11:40:59 +01001699 if (map_action != MAP_ACTION_NONE) {
1700 /*
1701 * First reserve all required memory for the new page table
1702 * entries in the recipient page tables without committing, to
1703 * make sure the entire operation will succeed without
1704 * exhausting the page pool.
1705 */
1706 ret = ffa_region_group_identity_map(
1707 to_locked, fragments, fragment_constituent_counts,
1708 fragment_count, to_mode, page_pool, MAP_ACTION_CHECK,
1709 NULL);
1710 if (ret.func == FFA_ERROR_32) {
1711 /* TODO: partial defrag of failed range. */
1712 goto out;
1713 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001714 }
1715
1716 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001717 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001718 !ffa_clear_memory_constituents(sender_orig_mode, fragments,
1719 fragment_constituent_counts,
1720 fragment_count, page_pool)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001721 dlog_verbose("Couldn't clear constituents.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001722 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001723 goto out;
1724 }
1725
J-Alves69cdfd92024-04-26 11:40:59 +01001726 if (map_action != MAP_ACTION_NONE) {
1727 /*
1728 * Complete the transfer by mapping the memory into the
1729 * recipient. This won't allocate because the transaction was
1730 * already prepared above, so it doesn't need to use the
1731 * `local_page_pool`.
1732 */
1733 CHECK(ffa_region_group_identity_map(to_locked, fragments,
1734 fragment_constituent_counts,
1735 fragment_count, to_mode,
1736 page_pool, map_action, NULL)
1737 .func == FFA_SUCCESS_32);
Jose Marinho09b1db82019-08-08 09:16:59 +01001738
J-Alves69cdfd92024-04-26 11:40:59 +01001739 /*
1740 * Return the mode used in mapping the memory in retriever's PT.
1741 */
1742 if (response_mode != NULL) {
1743 *response_mode = to_mode;
1744 }
J-Alves460d36c2023-10-12 17:02:15 +01001745 }
1746
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001747 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001748
1749out:
1750 mpool_fini(&local_page_pool);
1751
1752 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001753 * Tidy up the page table by reclaiming failed mappings (if there was an
1754 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001755 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001756 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001757
1758 return ret;
1759}
1760
Andrew Walbran996d1d12020-05-27 14:08:43 +01001761static struct ffa_value ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01001762 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001763 struct ffa_memory_region_constituent **fragments,
1764 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves69cdfd92024-04-26 11:40:59 +01001765 uint32_t sender_orig_mode, struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001766{
1767 uint32_t orig_from_mode;
J-Alves69cdfd92024-04-26 11:40:59 +01001768 uint32_t clearing_mode;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001769 uint32_t from_mode;
1770 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001771 struct ffa_value ret;
J-Alves69cdfd92024-04-26 11:40:59 +01001772 enum ffa_map_action map_action;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001773
Andrew Walbranca808b12020-05-15 17:22:28 +01001774 ret = ffa_relinquish_check_transition(
1775 from_locked, &orig_from_mode, fragments,
J-Alves69cdfd92024-04-26 11:40:59 +01001776 fragment_constituent_counts, fragment_count, &from_mode,
1777 &map_action);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001778 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001779 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001780 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001781 }
1782
1783 /*
1784 * Create a local pool so any freed memory can't be used by another
1785 * thread. This is to ensure the original mapping can be restored if the
1786 * clear fails.
1787 */
1788 mpool_init_with_fallback(&local_page_pool, page_pool);
1789
J-Alves69cdfd92024-04-26 11:40:59 +01001790 if (map_action != MAP_ACTION_NONE) {
1791 clearing_mode = orig_from_mode;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001792
J-Alves69cdfd92024-04-26 11:40:59 +01001793 /*
1794 * First reserve all required memory for the new page table
1795 * entries without committing, to make sure the entire operation
1796 * will succeed without exhausting the page pool.
1797 */
1798 ret = ffa_region_group_identity_map(
1799 from_locked, fragments, fragment_constituent_counts,
1800 fragment_count, from_mode, page_pool, MAP_ACTION_CHECK,
1801 NULL);
1802 if (ret.func == FFA_ERROR_32) {
1803 goto out;
1804 }
1805
1806 /*
1807 * Update the mapping for the sender. This won't allocate
1808 * because the transaction was already prepared above, but may
1809 * free pages in the case that a whole block is being unmapped
1810 * that was previously partially mapped.
1811 */
1812 CHECK(ffa_region_group_identity_map(from_locked, fragments,
1813 fragment_constituent_counts,
1814 fragment_count, from_mode,
1815 &local_page_pool,
1816 MAP_ACTION_COMMIT, NULL)
1817 .func == FFA_SUCCESS_32);
1818 } else {
1819 /*
1820 * If the `map_action` is set to `MAP_ACTION_NONE`, S2 PTs
1821 * were not updated on retrieve/relinquish. These were updating
1822 * only the `share_state` structures. As such, use the sender's
1823 * original mode.
1824 */
1825 clearing_mode = sender_orig_mode;
1826 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001827
1828 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001829 if (clear &&
J-Alves69cdfd92024-04-26 11:40:59 +01001830 !ffa_clear_memory_constituents(clearing_mode, fragments,
J-Alves26483382023-04-20 12:01:49 +01001831 fragment_constituent_counts,
1832 fragment_count, page_pool)) {
J-Alves69cdfd92024-04-26 11:40:59 +01001833 if (map_action != MAP_ACTION_NONE) {
1834 /*
1835 * On failure, roll back by returning memory to the
1836 * sender. This may allocate pages which were previously
1837 * freed into `local_page_pool` by the call above, but
1838 * will never allocate more pages than that so can never
1839 * fail.
1840 */
1841 CHECK(ffa_region_group_identity_map(
1842 from_locked, fragments,
1843 fragment_constituent_counts,
1844 fragment_count, orig_from_mode,
1845 &local_page_pool, MAP_ACTION_COMMIT, NULL)
1846 .func == FFA_SUCCESS_32);
1847 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001848 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001849 goto out;
1850 }
1851
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001852 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001853
1854out:
1855 mpool_fini(&local_page_pool);
1856
1857 /*
1858 * Tidy up the page table by reclaiming failed mappings (if there was an
1859 * error) or merging entries into blocks where possible (on success).
1860 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001861 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001862
1863 return ret;
1864}
1865
1866/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001867 * Complete a memory sending operation by checking that it is valid, updating
1868 * the sender page table, and then either marking the share state as having
1869 * completed sending (on success) or freeing it (on failure).
1870 *
1871 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1872 */
J-Alvesfdd29272022-07-19 13:16:31 +01001873struct ffa_value ffa_memory_send_complete(
Andrew Walbranca808b12020-05-15 17:22:28 +01001874 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001875 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1876 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001877{
1878 struct ffa_memory_region *memory_region = share_state->memory_region;
J-Alves8f11cde2022-12-21 16:18:22 +00001879 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01001880 struct ffa_value ret;
1881
1882 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001883 assert(share_states.share_states != NULL);
J-Alves8f11cde2022-12-21 16:18:22 +00001884 assert(memory_region != NULL);
1885 composite = ffa_memory_region_get_composite(memory_region, 0);
1886 assert(composite != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001887
1888 /* Check that state is valid in sender page table and update. */
1889 ret = ffa_send_check_update(
1890 from_locked, share_state->fragments,
1891 share_state->fragment_constituent_counts,
J-Alves8f11cde2022-12-21 16:18:22 +00001892 share_state->fragment_count, composite->page_count,
Daniel Boulbya76fd912024-02-22 14:22:15 +00001893 share_state->share_func, memory_region, page_pool,
J-Alves460d36c2023-10-12 17:02:15 +01001894 orig_from_mode_ret, &share_state->memory_protected);
Andrew Walbranca808b12020-05-15 17:22:28 +01001895 if (ret.func != FFA_SUCCESS_32) {
1896 /*
1897 * Free share state, it failed to send so it can't be retrieved.
1898 */
Karl Meakin4cec5e82023-06-30 16:30:22 +01001899 dlog_verbose("%s: failed to send check update: %s(%s)\n",
1900 __func__, ffa_func_name(ret.func),
1901 ffa_error_name(ffa_error_code(ret)));
Andrew Walbranca808b12020-05-15 17:22:28 +01001902 share_state_free(share_states, share_state, page_pool);
1903 return ret;
1904 }
1905
1906 share_state->sending_complete = true;
Karl Meakin4cec5e82023-06-30 16:30:22 +01001907 dlog_verbose("%s: marked sending complete.\n", __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001908
J-Alvesee68c542020-10-29 17:48:20 +00001909 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001910}
1911
1912/**
Daniel Boulby9764ff62024-01-30 17:47:39 +00001913 * Check that the memory attributes match Hafnium expectations.
1914 * Cacheability:
1915 * - Normal Memory as `FFA_MEMORY_CACHE_WRITE_BACK`.
1916 * - Device memory as `FFA_MEMORY_DEV_NGNRNE`.
1917 *
1918 * Shareability:
1919 * - Inner Shareable.
Federico Recanatia98603a2021-12-20 18:04:03 +01001920 */
1921static struct ffa_value ffa_memory_attributes_validate(
J-Alves7a99d0d2023-02-08 13:49:48 +00001922 ffa_memory_attributes_t attributes)
Federico Recanatia98603a2021-12-20 18:04:03 +01001923{
1924 enum ffa_memory_type memory_type;
1925 enum ffa_memory_cacheability cacheability;
1926 enum ffa_memory_shareability shareability;
1927
Karl Meakin84710f32023-10-12 15:14:49 +01001928 memory_type = attributes.type;
Daniel Boulby9764ff62024-01-30 17:47:39 +00001929 cacheability = attributes.cacheability;
1930 if (memory_type == FFA_MEMORY_NORMAL_MEM &&
1931 cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1932 dlog_verbose(
1933 "Normal Memory: Invalid cacheability %s, "
1934 "expected %s.\n",
1935 ffa_memory_cacheability_name(cacheability),
1936 ffa_memory_cacheability_name(
1937 FFA_MEMORY_CACHE_WRITE_BACK));
Federico Recanati3d953f32022-02-17 09:31:29 +01001938 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001939 }
Daniel Boulby9764ff62024-01-30 17:47:39 +00001940 if (memory_type == FFA_MEMORY_DEVICE_MEM &&
1941 cacheability != FFA_MEMORY_DEV_NGNRNE) {
1942 dlog_verbose(
1943 "Device Memory: Invalid cacheability %s, "
1944 "expected %s.\n",
1945 ffa_device_memory_cacheability_name(cacheability),
1946 ffa_device_memory_cacheability_name(
1947 FFA_MEMORY_DEV_NGNRNE));
Federico Recanati3d953f32022-02-17 09:31:29 +01001948 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001949 }
1950
Karl Meakin84710f32023-10-12 15:14:49 +01001951 shareability = attributes.shareability;
Federico Recanatia98603a2021-12-20 18:04:03 +01001952 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
Karl Meakinf98b2aa2023-10-12 16:09:59 +01001953 dlog_verbose("Invalid shareability %s, expected %s.\n",
1954 ffa_memory_shareability_name(shareability),
1955 ffa_memory_shareability_name(
1956 FFA_MEMORY_INNER_SHAREABLE));
Federico Recanati3d953f32022-02-17 09:31:29 +01001957 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001958 }
1959
1960 return (struct ffa_value){.func = FFA_SUCCESS_32};
1961}
1962
1963/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001964 * Check that the given `memory_region` represents a valid memory send request
1965 * of the given `share_func` type, return the clear flag and permissions via the
1966 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001967 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001968 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001969 * not.
1970 */
J-Alves66652252022-07-06 09:49:51 +01001971struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001972 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1973 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001974 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001975{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001976 struct ffa_composite_memory_region *composite;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001977 struct ffa_memory_access *receiver =
1978 ffa_memory_region_get_receiver(memory_region, 0);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001979 uint64_t receivers_end;
1980 uint64_t min_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001981 uint32_t composite_memory_region_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001982 uint32_t constituents_start;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001983 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001984 enum ffa_data_access data_access;
1985 enum ffa_instruction_access instruction_access;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001986 enum ffa_memory_security security_state;
Karl Meakinf98b2aa2023-10-12 16:09:59 +01001987 enum ffa_memory_type type;
Federico Recanatia98603a2021-12-20 18:04:03 +01001988 struct ffa_value ret;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001989 const size_t minimum_first_fragment_length =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001990 memory_region->receivers_offset +
1991 memory_region->memory_access_desc_size +
1992 sizeof(struct ffa_composite_memory_region);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001993
1994 if (fragment_length < minimum_first_fragment_length) {
Karl Meakine8937d92024-03-19 16:04:25 +00001995 dlog_verbose("Fragment length %u too short (min %zu).\n",
1996 fragment_length, minimum_first_fragment_length);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001997 return ffa_error(FFA_INVALID_PARAMETERS);
1998 }
1999
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05002000 static_assert(sizeof(struct ffa_memory_region_constituent) == 16,
2001 "struct ffa_memory_region_constituent must be 16 bytes");
2002 if (!is_aligned(fragment_length,
2003 sizeof(struct ffa_memory_region_constituent)) ||
2004 !is_aligned(memory_share_length,
2005 sizeof(struct ffa_memory_region_constituent))) {
2006 dlog_verbose(
2007 "Fragment length %u or total length %u"
2008 " is not 16-byte aligned.\n",
2009 fragment_length, memory_share_length);
2010 return ffa_error(FFA_INVALID_PARAMETERS);
2011 }
2012
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002013 if (fragment_length > memory_share_length) {
2014 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00002015 "Fragment length %zu greater than total length %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002016 (size_t)fragment_length, (size_t)memory_share_length);
2017 return ffa_error(FFA_INVALID_PARAMETERS);
2018 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002019
J-Alves95df0ef2022-12-07 10:09:48 +00002020 /* The sender must match the caller. */
2021 if ((!vm_id_is_current_world(from_locked.vm->id) &&
2022 vm_id_is_current_world(memory_region->sender)) ||
2023 (vm_id_is_current_world(from_locked.vm->id) &&
2024 memory_region->sender != from_locked.vm->id)) {
2025 dlog_verbose("Invalid memory sender ID.\n");
2026 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002027 }
2028
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002029 if (memory_region->receiver_count <= 0) {
2030 dlog_verbose("No receivers!\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002031 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002032 }
2033
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002034 /*
2035 * Ensure that the composite header is within the memory bounds and
2036 * doesn't overlap the first part of the message. Cast to uint64_t
2037 * to prevent overflow.
2038 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002039 receivers_end = ((uint64_t)memory_region->memory_access_desc_size *
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002040 (uint64_t)memory_region->receiver_count) +
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01002041 memory_region->receivers_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002042 min_length = receivers_end +
2043 sizeof(struct ffa_composite_memory_region) +
2044 sizeof(struct ffa_memory_region_constituent);
2045 if (min_length > memory_share_length) {
Karl Meakine8937d92024-03-19 16:04:25 +00002046 dlog_verbose("Share too short: got %zu but minimum is %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002047 (size_t)memory_share_length, (size_t)min_length);
2048 return ffa_error(FFA_INVALID_PARAMETERS);
2049 }
2050
2051 composite_memory_region_offset =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002052 receiver->composite_memory_region_offset;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002053
2054 /*
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002055 * Check that the composite memory region descriptor is after the access
2056 * descriptors, is at least 16-byte aligned, and fits in the first
2057 * fragment.
Andrew Walbrana65a1322020-04-06 19:32:32 +01002058 */
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002059 if ((composite_memory_region_offset < receivers_end) ||
2060 (composite_memory_region_offset % 16 != 0) ||
2061 (composite_memory_region_offset >
2062 fragment_length - sizeof(struct ffa_composite_memory_region))) {
2063 dlog_verbose(
2064 "Invalid composite memory region descriptor offset "
Karl Meakine8937d92024-03-19 16:04:25 +00002065 "%zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002066 (size_t)composite_memory_region_offset);
2067 return ffa_error(FFA_INVALID_PARAMETERS);
2068 }
2069
2070 /*
2071 * Compute the start of the constituent regions. Already checked
2072 * to be not more than fragment_length and thus not more than
2073 * memory_share_length.
2074 */
2075 constituents_start = composite_memory_region_offset +
2076 sizeof(struct ffa_composite_memory_region);
2077 constituents_length = memory_share_length - constituents_start;
2078
2079 /*
2080 * Check that the number of constituents is consistent with the length
2081 * of the constituent region.
2082 */
2083 composite = ffa_memory_region_get_composite(memory_region, 0);
2084 if ((constituents_length %
2085 sizeof(struct ffa_memory_region_constituent) !=
2086 0) ||
2087 ((constituents_length /
2088 sizeof(struct ffa_memory_region_constituent)) !=
2089 composite->constituent_count)) {
Karl Meakine8937d92024-03-19 16:04:25 +00002090 dlog_verbose("Invalid length %zu or composite offset %zu.\n",
Demi Marie Obenourd4677412023-02-03 20:35:12 -05002091 (size_t)memory_share_length,
2092 (size_t)composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002093 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002094 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002095 if (fragment_length < memory_share_length &&
2096 fragment_length < HF_MAILBOX_SIZE) {
2097 dlog_warning(
2098 "Initial fragment length %d smaller than mailbox "
2099 "size.\n",
2100 fragment_length);
2101 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002102
Andrew Walbrana65a1322020-04-06 19:32:32 +01002103 /*
2104 * Clear is not allowed for memory sharing, as the sender still has
2105 * access to the memory.
2106 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002107 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
J-Alves95fbb312024-03-20 15:19:16 +00002108 (share_func == FFA_MEM_SHARE_32 ||
2109 share_func == FFA_MEM_SHARE_64)) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01002110 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002111 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002112 }
2113
2114 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002115 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01002116 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002117 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002118 }
2119
J-Alves363f5722022-04-25 17:37:37 +01002120 /* Check that the permissions are valid, for each specified receiver. */
2121 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002122 struct ffa_memory_region_attributes receiver_permissions;
2123
2124 receiver = ffa_memory_region_get_receiver(memory_region, i);
2125 assert(receiver != NULL);
2126 receiver_permissions = receiver->receiver_permissions;
J-Alves363f5722022-04-25 17:37:37 +01002127 ffa_memory_access_permissions_t permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002128 receiver_permissions.permissions;
2129 ffa_id_t receiver_id = receiver_permissions.receiver;
J-Alves363f5722022-04-25 17:37:37 +01002130
2131 if (memory_region->sender == receiver_id) {
2132 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002133 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002134 }
Federico Recanati85090c42021-12-15 13:17:54 +01002135
J-Alves363f5722022-04-25 17:37:37 +01002136 for (uint32_t j = i + 1; j < memory_region->receiver_count;
2137 j++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002138 struct ffa_memory_access *other_receiver =
2139 ffa_memory_region_get_receiver(memory_region,
2140 j);
2141 assert(other_receiver != NULL);
2142
J-Alves363f5722022-04-25 17:37:37 +01002143 if (receiver_id ==
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002144 other_receiver->receiver_permissions.receiver) {
J-Alves363f5722022-04-25 17:37:37 +01002145 dlog_verbose(
2146 "Repeated receiver(%x) in memory send "
2147 "operation.\n",
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002148 other_receiver->receiver_permissions
2149 .receiver);
J-Alves363f5722022-04-25 17:37:37 +01002150 return ffa_error(FFA_INVALID_PARAMETERS);
2151 }
2152 }
2153
2154 if (composite_memory_region_offset !=
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002155 receiver->composite_memory_region_offset) {
J-Alves363f5722022-04-25 17:37:37 +01002156 dlog_verbose(
2157 "All ffa_memory_access should point to the "
2158 "same composite memory region offset.\n");
2159 return ffa_error(FFA_INVALID_PARAMETERS);
2160 }
2161
Karl Meakin84710f32023-10-12 15:14:49 +01002162 data_access = permissions.data_access;
2163 instruction_access = permissions.instruction_access;
J-Alves363f5722022-04-25 17:37:37 +01002164 if (data_access == FFA_DATA_ACCESS_RESERVED ||
2165 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
2166 dlog_verbose(
2167 "Reserved value for receiver permissions "
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002168 "(data_access = %s, instruction_access = %s)\n",
2169 ffa_data_access_name(data_access),
2170 ffa_instruction_access_name(
2171 instruction_access));
J-Alves363f5722022-04-25 17:37:37 +01002172 return ffa_error(FFA_INVALID_PARAMETERS);
2173 }
2174 if (instruction_access !=
2175 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2176 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002177 "Invalid instruction access permissions %s "
2178 "for sending memory, expected %s.\n",
2179 ffa_instruction_access_name(instruction_access),
2180 ffa_instruction_access_name(
Daniel Boulby91052c32024-05-21 14:09:48 +01002181 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002182 return ffa_error(FFA_INVALID_PARAMETERS);
2183 }
J-Alves95fbb312024-03-20 15:19:16 +00002184 if (share_func == FFA_MEM_SHARE_32 ||
2185 share_func == FFA_MEM_SHARE_64) {
J-Alves363f5722022-04-25 17:37:37 +01002186 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
2187 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002188 "Invalid data access permissions %s "
2189 "for sharing memory, expected %s.\n",
2190 ffa_data_access_name(data_access),
2191 ffa_data_access_name(
2192 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002193 return ffa_error(FFA_INVALID_PARAMETERS);
2194 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002195 /*
2196 * According to section 10.10.3 of the FF-A v1.1 EAC0
2197 * spec, NX is required for share operations (but must
2198 * not be specified by the sender) so set it in the
2199 * copy that we store, ready to be returned to the
2200 * retriever.
2201 */
2202 if (vm_id_is_current_world(receiver_id)) {
Karl Meakin84710f32023-10-12 15:14:49 +01002203 permissions.instruction_access =
2204 FFA_INSTRUCTION_ACCESS_NX;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002205 receiver_permissions.permissions = permissions;
2206 }
J-Alves363f5722022-04-25 17:37:37 +01002207 }
J-Alves95fbb312024-03-20 15:19:16 +00002208 if ((share_func == FFA_MEM_LEND_32 ||
2209 share_func == FFA_MEM_LEND_64) &&
J-Alves363f5722022-04-25 17:37:37 +01002210 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
2211 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002212 "Invalid data access permissions %s for "
2213 "lending memory, expected %s.\n",
2214 ffa_data_access_name(data_access),
2215 ffa_data_access_name(
2216 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002217 return ffa_error(FFA_INVALID_PARAMETERS);
2218 }
2219
J-Alves95fbb312024-03-20 15:19:16 +00002220 if ((share_func == FFA_MEM_DONATE_32 ||
2221 share_func == FFA_MEM_DONATE_64) &&
J-Alves363f5722022-04-25 17:37:37 +01002222 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
2223 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002224 "Invalid data access permissions %s for "
2225 "donating memory, expected %s.\n",
2226 ffa_data_access_name(data_access),
2227 ffa_data_access_name(
2228 FFA_DATA_ACCESS_NOT_SPECIFIED));
J-Alves363f5722022-04-25 17:37:37 +01002229 return ffa_error(FFA_INVALID_PARAMETERS);
2230 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01002231 }
2232
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002233 /* Memory region attributes NS-Bit MBZ for FFA_MEM_SHARE/LEND/DONATE. */
Karl Meakin84710f32023-10-12 15:14:49 +01002234 security_state = memory_region->attributes.security;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002235 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
2236 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002237 "Invalid security state %s for memory share operation, "
2238 "expected %s.\n",
2239 ffa_memory_security_name(security_state),
2240 ffa_memory_security_name(
2241 FFA_MEMORY_SECURITY_UNSPECIFIED));
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002242 return ffa_error(FFA_INVALID_PARAMETERS);
2243 }
2244
Federico Recanatid937f5e2021-12-20 17:38:23 +01002245 /*
J-Alves807794e2022-06-16 13:42:47 +01002246 * If a memory donate or lend with single borrower, the memory type
2247 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01002248 */
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002249 type = memory_region->attributes.type;
J-Alves807794e2022-06-16 13:42:47 +01002250 if (share_func == FFA_MEM_DONATE_32 ||
J-Alves95fbb312024-03-20 15:19:16 +00002251 share_func == FFA_MEM_DONATE_64 ||
2252 ((share_func == FFA_MEM_LEND_32 || share_func == FFA_MEM_LEND_64) &&
J-Alves807794e2022-06-16 13:42:47 +01002253 memory_region->receiver_count == 1)) {
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002254 if (type != FFA_MEMORY_NOT_SPECIFIED_MEM) {
J-Alves807794e2022-06-16 13:42:47 +01002255 dlog_verbose(
Karl Meakinf98b2aa2023-10-12 16:09:59 +01002256 "Invalid memory type %s for memory share "
2257 "operation, expected %s.\n",
2258 ffa_memory_type_name(type),
2259 ffa_memory_type_name(
2260 FFA_MEMORY_NOT_SPECIFIED_MEM));
J-Alves807794e2022-06-16 13:42:47 +01002261 return ffa_error(FFA_INVALID_PARAMETERS);
2262 }
2263 } else {
2264 /*
2265 * Check that sender's memory attributes match Hafnium
2266 * expectations: Normal Memory, Inner shareable, Write-Back
2267 * Read-Allocate Write-Allocate Cacheable.
2268 */
2269 ret = ffa_memory_attributes_validate(memory_region->attributes);
2270 if (ret.func != FFA_SUCCESS_32) {
2271 return ret;
2272 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01002273 }
2274
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002275 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01002276}
2277
2278/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002279 * Gets the share state for continuing an operation to donate, lend or share
2280 * memory, and checks that it is a valid request.
2281 *
2282 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
2283 * not.
2284 */
J-Alvesfdd29272022-07-19 13:16:31 +01002285struct ffa_value ffa_memory_send_continue_validate(
Andrew Walbranca808b12020-05-15 17:22:28 +01002286 struct share_states_locked share_states, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01002287 struct ffa_memory_share_state **share_state_ret, ffa_id_t from_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01002288 struct mpool *page_pool)
2289{
2290 struct ffa_memory_share_state *share_state;
2291 struct ffa_memory_region *memory_region;
2292
Daniel Boulbya2f8c662021-11-26 17:52:53 +00002293 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01002294
2295 /*
2296 * Look up the share state by handle and make sure that the VM ID
2297 * matches.
2298 */
Karl Meakin4a2854a2023-06-30 16:26:52 +01002299 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00002300 if (share_state == NULL) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002301 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00002302 "Invalid handle %#lx for memory send continuation.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01002303 handle);
2304 return ffa_error(FFA_INVALID_PARAMETERS);
2305 }
2306 memory_region = share_state->memory_region;
2307
J-Alvesfdd29272022-07-19 13:16:31 +01002308 if (vm_id_is_current_world(from_vm_id) &&
2309 memory_region->sender != from_vm_id) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002310 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
2311 return ffa_error(FFA_INVALID_PARAMETERS);
2312 }
2313
2314 if (share_state->sending_complete) {
2315 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00002316 "Sending of memory handle %#lx is already complete.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01002317 handle);
2318 return ffa_error(FFA_INVALID_PARAMETERS);
2319 }
2320
2321 if (share_state->fragment_count == MAX_FRAGMENTS) {
2322 /*
2323 * Log a warning as this is a sign that MAX_FRAGMENTS should
2324 * probably be increased.
2325 */
2326 dlog_warning(
Karl Meakine8937d92024-03-19 16:04:25 +00002327 "Too many fragments for memory share with handle %#lx; "
Andrew Walbranca808b12020-05-15 17:22:28 +01002328 "only %d supported.\n",
2329 handle, MAX_FRAGMENTS);
2330 /* Free share state, as it's not possible to complete it. */
2331 share_state_free(share_states, share_state, page_pool);
2332 return ffa_error(FFA_NO_MEMORY);
2333 }
2334
2335 *share_state_ret = share_state;
2336
2337 return (struct ffa_value){.func = FFA_SUCCESS_32};
2338}
2339
2340/**
J-Alves95df0ef2022-12-07 10:09:48 +00002341 * Checks if there is at least one receiver from the other world.
2342 */
J-Alvesfdd29272022-07-19 13:16:31 +01002343bool memory_region_receivers_from_other_world(
J-Alves95df0ef2022-12-07 10:09:48 +00002344 struct ffa_memory_region *memory_region)
2345{
2346 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002347 struct ffa_memory_access *receiver =
2348 ffa_memory_region_get_receiver(memory_region, i);
2349 assert(receiver != NULL);
2350 ffa_id_t receiver_id = receiver->receiver_permissions.receiver;
2351
2352 if (!vm_id_is_current_world(receiver_id)) {
J-Alves95df0ef2022-12-07 10:09:48 +00002353 return true;
2354 }
2355 }
2356 return false;
2357}
2358
2359/**
J-Alves9da280b2022-12-21 14:55:39 +00002360 * Validates a call to donate, lend or share memory in which Hafnium is the
2361 * designated allocator of the memory handle. In practice, this also means
2362 * Hafnium is responsible for managing the state structures for the transaction.
2363 * If Hafnium is the SPMC, it should allocate the memory handle when either the
2364 * sender is an SP or there is at least one borrower that is an SP.
2365 * If Hafnium is the hypervisor, it should allocate the memory handle when
2366 * operation involves only NWd VMs.
2367 *
2368 * If validation goes well, Hafnium updates the stage-2 page tables of the
2369 * sender. Validation consists of checking if the message length and number of
2370 * memory region constituents match, and if the transition is valid for the
2371 * type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00002372 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002373 * Assumes that the caller has already found and locked the sender VM and copied
2374 * the memory region descriptor from the sender's TX buffer to a freshly
2375 * allocated page from Hafnium's internal pool. The caller must have also
2376 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002377 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002378 * This function takes ownership of the `memory_region` passed in and will free
2379 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01002380 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002381struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002382 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002383 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002384 uint32_t fragment_length, uint32_t share_func,
2385 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01002386{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002387 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002388 struct share_states_locked share_states;
2389 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01002390
2391 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01002392 * If there is an error validating the `memory_region` then we need to
2393 * free it because we own it but we won't be storing it in a share state
2394 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01002395 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002396 ret = ffa_memory_send_validate(from_locked, memory_region,
2397 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01002398 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002399 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002400 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002401 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002402 }
2403
Andrew Walbrana65a1322020-04-06 19:32:32 +01002404 /* Set flag for share function, ready to be retrieved later. */
2405 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00002406 case FFA_MEM_SHARE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002407 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002408 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002409 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002410 break;
J-Alves95fbb312024-03-20 15:19:16 +00002411 case FFA_MEM_LEND_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002412 case FFA_MEM_LEND_32:
2413 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002414 break;
J-Alves95fbb312024-03-20 15:19:16 +00002415 case FFA_MEM_DONATE_64:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002416 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002417 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002418 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01002419 break;
Karl Meakina5ea9092024-05-28 15:40:33 +01002420 default:
2421 dlog_verbose("Unknown share func %#x (%s)\n", share_func,
2422 ffa_func_name(share_func));
2423 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +01002424 }
2425
Andrew Walbranca808b12020-05-15 17:22:28 +01002426 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002427 /*
2428 * Allocate a share state before updating the page table. Otherwise if
2429 * updating the page table succeeded but allocating the share state
2430 * failed then it would leave the memory in a state where nobody could
2431 * get it back.
2432 */
Karl Meakin52cdfe72023-06-30 14:49:10 +01002433 share_state = allocate_share_state(share_states, share_func,
2434 memory_region, fragment_length,
2435 FFA_MEMORY_HANDLE_INVALID);
J-Alvesb56aac82023-11-10 09:44:43 +00002436 if (share_state == NULL) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002437 dlog_verbose("Failed to allocate share state.\n");
2438 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01002439 ret = ffa_error(FFA_NO_MEMORY);
2440 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002441 }
2442
Andrew Walbranca808b12020-05-15 17:22:28 +01002443 if (fragment_length == memory_share_length) {
2444 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00002445 ret = ffa_memory_send_complete(
2446 from_locked, share_states, share_state, page_pool,
2447 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002448 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01002449 /*
2450 * Use sender ID from 'memory_region' assuming
2451 * that at this point it has been validated:
2452 * - MBZ at virtual FF-A instance.
2453 */
J-Alves19e20cf2023-08-02 12:48:55 +01002454 ffa_id_t sender_to_ret =
J-Alvesfdd29272022-07-19 13:16:31 +01002455 (from_locked.vm->id == HF_OTHER_WORLD_ID)
2456 ? memory_region->sender
2457 : 0;
Andrew Walbranca808b12020-05-15 17:22:28 +01002458 ret = (struct ffa_value){
2459 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00002460 .arg1 = (uint32_t)memory_region->handle,
2461 .arg2 = (uint32_t)(memory_region->handle >> 32),
J-Alvesfdd29272022-07-19 13:16:31 +01002462 .arg3 = fragment_length,
2463 .arg4 = (uint32_t)(sender_to_ret & 0xffff) << 16};
Andrew Walbranca808b12020-05-15 17:22:28 +01002464 }
2465
2466out:
2467 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002468 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01002469 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002470}
2471
2472/**
J-Alves8505a8a2022-06-15 18:10:18 +01002473 * Continues an operation to donate, lend or share memory to a VM from current
2474 * world. If this is the last fragment then checks that the transition is valid
2475 * for the type of memory sending operation and updates the stage-2 page tables
2476 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01002477 *
2478 * Assumes that the caller has already found and locked the sender VM and copied
2479 * the memory region descriptor from the sender's TX buffer to a freshly
2480 * allocated page from Hafnium's internal pool.
2481 *
2482 * This function takes ownership of the `fragment` passed in; it must not be
2483 * freed by the caller.
2484 */
2485struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
2486 void *fragment,
2487 uint32_t fragment_length,
2488 ffa_memory_handle_t handle,
2489 struct mpool *page_pool)
2490{
2491 struct share_states_locked share_states = share_states_lock();
2492 struct ffa_memory_share_state *share_state;
2493 struct ffa_value ret;
2494 struct ffa_memory_region *memory_region;
2495
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05002496 CHECK(is_aligned(fragment,
2497 alignof(struct ffa_memory_region_constituent)));
2498 if (fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2499 0) {
2500 dlog_verbose("Fragment length %u misaligned.\n",
2501 fragment_length);
2502 ret = ffa_error(FFA_INVALID_PARAMETERS);
2503 goto out_free_fragment;
2504 }
2505
Andrew Walbranca808b12020-05-15 17:22:28 +01002506 ret = ffa_memory_send_continue_validate(share_states, handle,
2507 &share_state,
2508 from_locked.vm->id, page_pool);
2509 if (ret.func != FFA_SUCCESS_32) {
2510 goto out_free_fragment;
2511 }
2512 memory_region = share_state->memory_region;
2513
J-Alves95df0ef2022-12-07 10:09:48 +00002514 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002515 dlog_error(
2516 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01002517 "other world. This should never happen, and indicates "
2518 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01002519 "EL3 code.\n");
2520 ret = ffa_error(FFA_INVALID_PARAMETERS);
2521 goto out_free_fragment;
2522 }
2523
2524 /* Add this fragment. */
2525 share_state->fragments[share_state->fragment_count] = fragment;
2526 share_state->fragment_constituent_counts[share_state->fragment_count] =
2527 fragment_length / sizeof(struct ffa_memory_region_constituent);
2528 share_state->fragment_count++;
2529
2530 /* Check whether the memory send operation is now ready to complete. */
2531 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00002532 ret = ffa_memory_send_complete(
2533 from_locked, share_states, share_state, page_pool,
2534 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002535 } else {
2536 ret = (struct ffa_value){
2537 .func = FFA_MEM_FRAG_RX_32,
2538 .arg1 = (uint32_t)handle,
2539 .arg2 = (uint32_t)(handle >> 32),
2540 .arg3 = share_state_next_fragment_offset(share_states,
2541 share_state)};
2542 }
2543 goto out;
2544
2545out_free_fragment:
2546 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002547
2548out:
Andrew Walbranca808b12020-05-15 17:22:28 +01002549 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002550 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002551}
2552
Andrew Walbranca808b12020-05-15 17:22:28 +01002553/** Clean up after the receiver has finished retrieving a memory region. */
2554static void ffa_memory_retrieve_complete(
2555 struct share_states_locked share_states,
2556 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2557{
J-Alves95fbb312024-03-20 15:19:16 +00002558 if (share_state->share_func == FFA_MEM_DONATE_32 ||
2559 share_state->share_func == FFA_MEM_DONATE_64) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002560 /*
2561 * Memory that has been donated can't be relinquished,
2562 * so no need to keep the share state around.
2563 */
2564 share_state_free(share_states, share_state, page_pool);
2565 dlog_verbose("Freed share state for donate.\n");
2566 }
2567}
2568
J-Alves2d8457f2022-10-05 11:06:41 +01002569/**
2570 * Initialises the given memory region descriptor to be used for an
2571 * `FFA_MEM_RETRIEVE_RESP`, including the given constituents for the first
2572 * fragment.
2573 * The memory region descriptor is initialized according to retriever's
2574 * FF-A version.
2575 *
2576 * Returns true on success, or false if the given constituents won't all fit in
2577 * the first fragment.
2578 */
2579static bool ffa_retrieved_memory_region_init(
Karl Meakin0e617d92024-04-05 12:55:22 +01002580 void *response, enum ffa_version ffa_version, size_t response_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01002581 ffa_id_t sender, ffa_memory_attributes_t attributes,
J-Alves2d8457f2022-10-05 11:06:41 +01002582 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002583 ffa_memory_access_permissions_t permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002584 struct ffa_memory_access *receivers, size_t receiver_count,
2585 uint32_t memory_access_desc_size, uint32_t page_count,
2586 uint32_t total_constituent_count,
J-Alves2d8457f2022-10-05 11:06:41 +01002587 const struct ffa_memory_region_constituent constituents[],
2588 uint32_t fragment_constituent_count, uint32_t *total_length,
2589 uint32_t *fragment_length)
2590{
2591 struct ffa_composite_memory_region *composite_memory_region;
J-Alves2d8457f2022-10-05 11:06:41 +01002592 uint32_t i;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002593 uint32_t composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002594 uint32_t constituents_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002595
2596 assert(response != NULL);
2597
Karl Meakin0e617d92024-04-05 12:55:22 +01002598 if (ffa_version == FFA_VERSION_1_0) {
J-Alves2d8457f2022-10-05 11:06:41 +01002599 struct ffa_memory_region_v1_0 *retrieve_response =
2600 (struct ffa_memory_region_v1_0 *)response;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002601 struct ffa_memory_access_v1_0 *receiver;
J-Alves2d8457f2022-10-05 11:06:41 +01002602
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002603 ffa_memory_region_init_header_v1_0(retrieve_response, sender,
2604 attributes, flags, handle, 0,
2605 receiver_count);
J-Alves2d8457f2022-10-05 11:06:41 +01002606
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002607 receiver = (struct ffa_memory_access_v1_0 *)
2608 retrieve_response->receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002609 receiver_count = retrieve_response->receiver_count;
2610
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002611 for (uint32_t i = 0; i < receiver_count; i++) {
2612 ffa_id_t receiver_id =
2613 receivers[i].receiver_permissions.receiver;
2614 ffa_memory_receiver_flags_t recv_flags =
2615 receivers[i].receiver_permissions.flags;
2616
2617 /*
2618 * Initialized here as in memory retrieve responses we
2619 * currently expect one borrower to be specified.
2620 */
2621 ffa_memory_access_init_v1_0(
Karl Meakin84710f32023-10-12 15:14:49 +01002622 receiver, receiver_id, permissions.data_access,
2623 permissions.instruction_access, recv_flags);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002624 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002625
2626 composite_offset =
J-Alves2d8457f2022-10-05 11:06:41 +01002627 sizeof(struct ffa_memory_region_v1_0) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002628 receiver_count * sizeof(struct ffa_memory_access_v1_0);
2629 receiver->composite_memory_region_offset = composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002630
2631 composite_memory_region = ffa_memory_region_get_composite_v1_0(
2632 retrieve_response, 0);
2633 } else {
J-Alves2d8457f2022-10-05 11:06:41 +01002634 struct ffa_memory_region *retrieve_response =
2635 (struct ffa_memory_region *)response;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002636 struct ffa_memory_access *retrieve_response_receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002637
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002638 ffa_memory_region_init_header(
2639 retrieve_response, sender, attributes, flags, handle, 0,
2640 receiver_count, memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002641
2642 /*
2643 * Note that `sizeof(struct_ffa_memory_region)` and
2644 * `sizeof(struct ffa_memory_access)` must both be multiples of
2645 * 16 (as verified by the asserts in `ffa_memory.c`, so it is
2646 * guaranteed that the offset we calculate here is aligned to a
2647 * 64-bit boundary and so 64-bit values can be copied without
2648 * alignment faults.
2649 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002650 composite_offset =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01002651 retrieve_response->receivers_offset +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002652 (uint32_t)(receiver_count *
2653 retrieve_response->memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002654
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002655 retrieve_response_receivers =
2656 ffa_memory_region_get_receiver(retrieve_response, 0);
2657 assert(retrieve_response_receivers != NULL);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002658
2659 /*
2660 * Initialized here as in memory retrieve responses we currently
2661 * expect one borrower to be specified.
2662 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002663 memcpy_s(retrieve_response_receivers,
2664 sizeof(struct ffa_memory_access) * receiver_count,
2665 receivers,
2666 sizeof(struct ffa_memory_access) * receiver_count);
2667
2668 retrieve_response_receivers->composite_memory_region_offset =
2669 composite_offset;
2670
J-Alves2d8457f2022-10-05 11:06:41 +01002671 composite_memory_region =
2672 ffa_memory_region_get_composite(retrieve_response, 0);
2673 }
2674
J-Alves2d8457f2022-10-05 11:06:41 +01002675 assert(composite_memory_region != NULL);
2676
J-Alves2d8457f2022-10-05 11:06:41 +01002677 composite_memory_region->page_count = page_count;
2678 composite_memory_region->constituent_count = total_constituent_count;
2679 composite_memory_region->reserved_0 = 0;
2680
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002681 constituents_offset =
2682 composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alves2d8457f2022-10-05 11:06:41 +01002683 if (constituents_offset +
2684 fragment_constituent_count *
2685 sizeof(struct ffa_memory_region_constituent) >
2686 response_max_size) {
2687 return false;
2688 }
2689
2690 for (i = 0; i < fragment_constituent_count; ++i) {
2691 composite_memory_region->constituents[i] = constituents[i];
2692 }
2693
2694 if (total_length != NULL) {
2695 *total_length =
2696 constituents_offset +
2697 composite_memory_region->constituent_count *
2698 sizeof(struct ffa_memory_region_constituent);
2699 }
2700 if (fragment_length != NULL) {
2701 *fragment_length =
2702 constituents_offset +
2703 fragment_constituent_count *
2704 sizeof(struct ffa_memory_region_constituent);
2705 }
2706
2707 return true;
2708}
2709
J-Alves96de29f2022-04-26 16:05:24 +01002710/**
2711 * Validates the retrieved permissions against those specified by the lender
2712 * of memory share operation. Optionally can help set the permissions to be used
2713 * for the S2 mapping, through the `permissions` argument.
J-Alvesdcad8992023-09-15 14:10:35 +01002714 * Returns FFA_SUCCESS if all the fields are valid. FFA_ERROR, with error code:
2715 * - FFA_INVALID_PARAMETERS -> if the fields have invalid values as per the
2716 * specification for each ABI.
2717 * - FFA_DENIED -> if the permissions specified by the retriever are not
2718 * less permissive than those provided by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002719 */
J-Alvesdcad8992023-09-15 14:10:35 +01002720static struct ffa_value ffa_memory_retrieve_is_memory_access_valid(
2721 uint32_t share_func, enum ffa_data_access sent_data_access,
J-Alves96de29f2022-04-26 16:05:24 +01002722 enum ffa_data_access requested_data_access,
2723 enum ffa_instruction_access sent_instruction_access,
2724 enum ffa_instruction_access requested_instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002725 ffa_memory_access_permissions_t *permissions, bool multiple_borrowers)
J-Alves96de29f2022-04-26 16:05:24 +01002726{
2727 switch (sent_data_access) {
2728 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2729 case FFA_DATA_ACCESS_RW:
2730 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2731 requested_data_access == FFA_DATA_ACCESS_RW) {
2732 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002733 permissions->data_access = FFA_DATA_ACCESS_RW;
J-Alves96de29f2022-04-26 16:05:24 +01002734 }
2735 break;
2736 }
2737 /* Intentional fall-through. */
2738 case FFA_DATA_ACCESS_RO:
2739 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2740 requested_data_access == FFA_DATA_ACCESS_RO) {
2741 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002742 permissions->data_access = FFA_DATA_ACCESS_RO;
J-Alves96de29f2022-04-26 16:05:24 +01002743 }
2744 break;
2745 }
2746 dlog_verbose(
2747 "Invalid data access requested; sender specified "
2748 "permissions %#x but receiver requested %#x.\n",
2749 sent_data_access, requested_data_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002750 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002751 case FFA_DATA_ACCESS_RESERVED:
2752 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
2753 "checked before this point.");
2754 }
2755
J-Alvesdcad8992023-09-15 14:10:35 +01002756 /*
2757 * For operations with a single borrower, If it is an FFA_MEMORY_LEND
2758 * or FFA_MEMORY_DONATE the retriever should have specifed the
2759 * instruction permissions it wishes to receive.
2760 */
2761 switch (share_func) {
J-Alves95fbb312024-03-20 15:19:16 +00002762 case FFA_MEM_SHARE_64:
J-Alvesdcad8992023-09-15 14:10:35 +01002763 case FFA_MEM_SHARE_32:
2764 if (requested_instruction_access !=
2765 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2766 dlog_verbose(
2767 "%s: for share instruction permissions must "
2768 "NOT be specified.\n",
2769 __func__);
2770 return ffa_error(FFA_INVALID_PARAMETERS);
2771 }
2772 break;
J-Alves95fbb312024-03-20 15:19:16 +00002773 case FFA_MEM_LEND_64:
J-Alvesdcad8992023-09-15 14:10:35 +01002774 case FFA_MEM_LEND_32:
2775 /*
2776 * For operations with multiple borrowers only permit XN
2777 * permissions, and both Sender and borrower should have used
2778 * FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED.
2779 */
2780 if (multiple_borrowers) {
2781 if (requested_instruction_access !=
2782 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2783 dlog_verbose(
2784 "%s: lend/share/donate with multiple "
2785 "borrowers "
2786 "instruction permissions must NOT be "
2787 "specified.\n",
2788 __func__);
2789 return ffa_error(FFA_INVALID_PARAMETERS);
2790 }
2791 break;
2792 }
2793 /* Fall through if the operation targets a single borrower. */
J-Alves95fbb312024-03-20 15:19:16 +00002794 case FFA_MEM_DONATE_64:
J-Alvesdcad8992023-09-15 14:10:35 +01002795 case FFA_MEM_DONATE_32:
2796 if (!multiple_borrowers &&
2797 requested_instruction_access ==
2798 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2799 dlog_verbose(
2800 "%s: for lend/donate with single borrower "
2801 "instruction permissions must be speficified "
2802 "by borrower\n",
2803 __func__);
2804 return ffa_error(FFA_INVALID_PARAMETERS);
2805 }
2806 break;
2807 default:
2808 panic("%s: Wrong func id provided.\n", __func__);
2809 }
2810
J-Alves96de29f2022-04-26 16:05:24 +01002811 switch (sent_instruction_access) {
2812 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2813 case FFA_INSTRUCTION_ACCESS_X:
J-Alvesdcad8992023-09-15 14:10:35 +01002814 if (requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
J-Alves96de29f2022-04-26 16:05:24 +01002815 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002816 permissions->instruction_access =
2817 FFA_INSTRUCTION_ACCESS_X;
J-Alves96de29f2022-04-26 16:05:24 +01002818 }
2819 break;
2820 }
J-Alvesdcad8992023-09-15 14:10:35 +01002821 /*
2822 * Fall through if requested permissions are less
2823 * permissive than those provided by the sender.
2824 */
J-Alves96de29f2022-04-26 16:05:24 +01002825 case FFA_INSTRUCTION_ACCESS_NX:
2826 if (requested_instruction_access ==
2827 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2828 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2829 if (permissions != NULL) {
Karl Meakin84710f32023-10-12 15:14:49 +01002830 permissions->instruction_access =
2831 FFA_INSTRUCTION_ACCESS_NX;
J-Alves96de29f2022-04-26 16:05:24 +01002832 }
2833 break;
2834 }
2835 dlog_verbose(
2836 "Invalid instruction access requested; sender "
2837 "specified permissions %#x but receiver requested "
2838 "%#x.\n",
2839 sent_instruction_access, requested_instruction_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002840 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002841 case FFA_INSTRUCTION_ACCESS_RESERVED:
2842 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
2843 "be checked before this point.");
2844 }
2845
J-Alvesdcad8992023-09-15 14:10:35 +01002846 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves96de29f2022-04-26 16:05:24 +01002847}
2848
2849/**
2850 * Validate the receivers' permissions in the retrieve request against those
2851 * specified by the lender.
2852 * In the `permissions` argument returns the permissions to set at S2 for the
2853 * caller to the FFA_MEMORY_RETRIEVE_REQ.
J-Alves3456e032023-07-20 12:20:05 +01002854 * The function looks into the flag to bypass multiple borrower checks:
2855 * - If not set returns FFA_SUCCESS if all specified permissions are valid.
2856 * - If set returns FFA_SUCCESS if the descriptor contains the permissions
2857 * to the caller of FFA_MEM_RETRIEVE_REQ and they are valid. Other permissions
2858 * are ignored, if provided.
J-Alves96de29f2022-04-26 16:05:24 +01002859 */
2860static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
2861 struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01002862 struct ffa_memory_region *retrieve_request, ffa_id_t to_vm_id,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002863 ffa_memory_access_permissions_t *permissions,
2864 struct ffa_memory_access **receiver_ret, uint32_t func_id)
J-Alves96de29f2022-04-26 16:05:24 +01002865{
2866 uint32_t retrieve_receiver_index;
J-Alves3456e032023-07-20 12:20:05 +01002867 bool bypass_multi_receiver_check =
2868 (retrieve_request->flags &
2869 FFA_MEMORY_REGION_FLAG_BYPASS_BORROWERS_CHECK) != 0U;
J-Alvesdcad8992023-09-15 14:10:35 +01002870 const uint32_t region_receiver_count = memory_region->receiver_count;
2871 struct ffa_value ret;
J-Alves96de29f2022-04-26 16:05:24 +01002872
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002873 assert(receiver_ret != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002874 assert(permissions != NULL);
2875
Karl Meakin84710f32023-10-12 15:14:49 +01002876 *permissions = (ffa_memory_access_permissions_t){0};
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002877
J-Alves3456e032023-07-20 12:20:05 +01002878 if (!bypass_multi_receiver_check) {
J-Alvesdcad8992023-09-15 14:10:35 +01002879 if (retrieve_request->receiver_count != region_receiver_count) {
J-Alves3456e032023-07-20 12:20:05 +01002880 dlog_verbose(
2881 "Retrieve request should contain same list of "
2882 "borrowers, as specified by the lender.\n");
2883 return ffa_error(FFA_INVALID_PARAMETERS);
2884 }
2885 } else {
2886 if (retrieve_request->receiver_count != 1) {
2887 dlog_verbose(
2888 "Set bypass multiple borrower check, receiver "
2889 "list must be sized 1 (%x)\n",
2890 memory_region->receiver_count);
2891 return ffa_error(FFA_INVALID_PARAMETERS);
2892 }
J-Alves96de29f2022-04-26 16:05:24 +01002893 }
2894
2895 retrieve_receiver_index = retrieve_request->receiver_count;
2896
J-Alves96de29f2022-04-26 16:05:24 +01002897 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
2898 ffa_memory_access_permissions_t sent_permissions;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002899 struct ffa_memory_access *retrieve_request_receiver =
2900 ffa_memory_region_get_receiver(retrieve_request, i);
2901 assert(retrieve_request_receiver != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002902 ffa_memory_access_permissions_t requested_permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002903 retrieve_request_receiver->receiver_permissions
2904 .permissions;
J-Alves19e20cf2023-08-02 12:48:55 +01002905 ffa_id_t current_receiver_id =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002906 retrieve_request_receiver->receiver_permissions
2907 .receiver;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002908 struct ffa_memory_access *receiver;
2909 uint32_t mem_region_receiver_index;
2910 bool permissions_RO;
2911 bool clear_memory_flags;
J-Alvesf220d572024-04-24 22:15:14 +01002912 /*
2913 * If the call is at the virtual FF-A instance the caller's
2914 * ID must match an entry in the memory access list.
2915 * In the SPMC, one of the specified receivers could be from
2916 * the NWd.
2917 */
2918 bool found_to_id = vm_id_is_current_world(to_vm_id)
2919 ? (current_receiver_id == to_vm_id)
2920 : (!vm_id_is_current_world(
2921 current_receiver_id));
J-Alves96de29f2022-04-26 16:05:24 +01002922
J-Alves3456e032023-07-20 12:20:05 +01002923 if (bypass_multi_receiver_check && !found_to_id) {
2924 dlog_verbose(
2925 "Bypass multiple borrower check for id %x.\n",
2926 current_receiver_id);
2927 continue;
2928 }
2929
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002930 if (retrieve_request_receiver->composite_memory_region_offset !=
2931 0U) {
2932 dlog_verbose(
2933 "Retriever specified address ranges not "
2934 "supported (got offset %d).\n",
2935 retrieve_request_receiver
2936 ->composite_memory_region_offset);
2937 return ffa_error(FFA_INVALID_PARAMETERS);
2938 }
2939
J-Alves96de29f2022-04-26 16:05:24 +01002940 /*
2941 * Find the current receiver in the transaction descriptor from
2942 * sender.
2943 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002944 mem_region_receiver_index =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002945 ffa_memory_region_get_receiver_index(
2946 memory_region, current_receiver_id);
J-Alves96de29f2022-04-26 16:05:24 +01002947
2948 if (mem_region_receiver_index ==
2949 memory_region->receiver_count) {
2950 dlog_verbose("%s: receiver %x not found\n", __func__,
2951 current_receiver_id);
2952 return ffa_error(FFA_DENIED);
2953 }
2954
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002955 receiver = ffa_memory_region_get_receiver(
2956 memory_region, mem_region_receiver_index);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002957 assert(receiver != NULL);
2958
2959 sent_permissions = receiver->receiver_permissions.permissions;
J-Alves96de29f2022-04-26 16:05:24 +01002960
2961 if (found_to_id) {
2962 retrieve_receiver_index = i;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002963
2964 *receiver_ret = receiver;
J-Alves96de29f2022-04-26 16:05:24 +01002965 }
2966
2967 /*
J-Alvesdcad8992023-09-15 14:10:35 +01002968 * Check if retrieve request memory access list is valid:
2969 * - The retrieve request complies with the specification.
2970 * - Permissions are within those specified by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002971 */
J-Alvesdcad8992023-09-15 14:10:35 +01002972 ret = ffa_memory_retrieve_is_memory_access_valid(
Karl Meakin84710f32023-10-12 15:14:49 +01002973 func_id, sent_permissions.data_access,
2974 requested_permissions.data_access,
2975 sent_permissions.instruction_access,
2976 requested_permissions.instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002977 found_to_id ? permissions : NULL,
2978 region_receiver_count > 1);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002979
J-Alvesdcad8992023-09-15 14:10:35 +01002980 if (ret.func != FFA_SUCCESS_32) {
2981 return ret;
J-Alves96de29f2022-04-26 16:05:24 +01002982 }
2983
Karl Meakin84710f32023-10-12 15:14:49 +01002984 permissions_RO =
2985 (permissions->data_access == FFA_DATA_ACCESS_RO);
J-Alvese5262372024-03-27 11:02:03 +00002986 clear_memory_flags =
2987 (retrieve_request->flags &
2988 (FFA_MEMORY_REGION_FLAG_CLEAR |
2989 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002990
J-Alves96de29f2022-04-26 16:05:24 +01002991 /*
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002992 * Can't request PM to clear memory if only provided
2993 * with RO permissions.
J-Alves96de29f2022-04-26 16:05:24 +01002994 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002995 if (found_to_id && permissions_RO && clear_memory_flags) {
J-Alves96de29f2022-04-26 16:05:24 +01002996 dlog_verbose(
2997 "Receiver has RO permissions can not request "
2998 "clear.\n");
2999 return ffa_error(FFA_DENIED);
3000 }
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003001
3002 /*
3003 * Check the impdef in the retrieve_request matches the value in
3004 * the original memory send.
3005 */
3006 if (ffa_version_from_memory_access_desc_size(
3007 memory_region->memory_access_desc_size) >=
Karl Meakin0e617d92024-04-05 12:55:22 +01003008 FFA_VERSION_1_2 &&
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003009 ffa_version_from_memory_access_desc_size(
3010 retrieve_request->memory_access_desc_size) >=
Karl Meakin0e617d92024-04-05 12:55:22 +01003011 FFA_VERSION_1_2) {
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003012 if (receiver->impdef.val[0] !=
3013 retrieve_request_receiver->impdef.val[0] ||
3014 receiver->impdef.val[1] !=
3015 retrieve_request_receiver->impdef.val[1]) {
3016 dlog_verbose(
3017 "Impdef value in memory send does not "
J-Alves0a824e92024-04-26 16:20:12 +01003018 "match retrieve request value send "
3019 "value %#lx %#lx retrieve request "
Karl Meakine8937d92024-03-19 16:04:25 +00003020 "value %#lx %#lx\n",
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003021 receiver->impdef.val[0],
3022 receiver->impdef.val[1],
3023 retrieve_request_receiver->impdef
3024 .val[0],
3025 retrieve_request_receiver->impdef
3026 .val[1]);
3027 return ffa_error(FFA_INVALID_PARAMETERS);
3028 }
3029 }
J-Alves96de29f2022-04-26 16:05:24 +01003030 }
3031
3032 if (retrieve_receiver_index == retrieve_request->receiver_count) {
3033 dlog_verbose(
3034 "Retrieve request does not contain caller's (%x) "
3035 "permissions\n",
3036 to_vm_id);
3037 return ffa_error(FFA_INVALID_PARAMETERS);
3038 }
3039
3040 return (struct ffa_value){.func = FFA_SUCCESS_32};
3041}
3042
J-Alvesa9cd7e32022-07-01 13:49:33 +01003043/*
3044 * According to section 16.4.3 of FF-A v1.1 EAC0 specification, the hypervisor
3045 * may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region description
3046 * of a pending memory sharing operation whose allocator is the SPM, for
3047 * validation purposes before forwarding an FFA_MEM_RECLAIM call. In doing so
3048 * the memory region descriptor of the retrieve request must be zeroed with the
3049 * exception of the sender ID and handle.
3050 */
J-Alves4f0d9c12024-01-17 17:23:11 +00003051bool is_ffa_hypervisor_retrieve_request(struct ffa_memory_region *request,
3052 struct vm_locked to_locked)
J-Alvesa9cd7e32022-07-01 13:49:33 +01003053{
3054 return to_locked.vm->id == HF_HYPERVISOR_VM_ID &&
Karl Meakin84710f32023-10-12 15:14:49 +01003055 request->attributes.shareability == 0U &&
3056 request->attributes.cacheability == 0U &&
3057 request->attributes.type == 0U &&
3058 request->attributes.security == 0U && request->flags == 0U &&
J-Alvesa9cd7e32022-07-01 13:49:33 +01003059 request->tag == 0U && request->receiver_count == 0U &&
3060 plat_ffa_memory_handle_allocated_by_current_world(
3061 request->handle);
3062}
3063
3064/*
3065 * Helper to reset count of fragments retrieved by the hypervisor.
3066 */
3067static void ffa_memory_retrieve_complete_from_hyp(
3068 struct ffa_memory_share_state *share_state)
3069{
3070 if (share_state->hypervisor_fragment_count ==
3071 share_state->fragment_count) {
3072 share_state->hypervisor_fragment_count = 0;
3073 }
3074}
3075
J-Alves089004f2022-07-13 14:25:44 +01003076/**
J-Alves4f0d9c12024-01-17 17:23:11 +00003077 * Prepares the return of the ffa_value for the memory retrieve response.
3078 */
3079static struct ffa_value ffa_memory_retrieve_resp(uint32_t total_length,
3080 uint32_t fragment_length)
3081{
3082 return (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
3083 .arg1 = total_length,
3084 .arg2 = fragment_length};
3085}
3086
3087/**
J-Alves089004f2022-07-13 14:25:44 +01003088 * Validate that the memory region descriptor provided by the borrower on
3089 * FFA_MEM_RETRIEVE_REQ, against saved memory region provided by lender at the
3090 * memory sharing call.
3091 */
3092static struct ffa_value ffa_memory_retrieve_validate(
J-Alves4f0d9c12024-01-17 17:23:11 +00003093 ffa_id_t to_id, struct ffa_memory_region *retrieve_request,
3094 uint32_t retrieve_request_length,
J-Alves089004f2022-07-13 14:25:44 +01003095 struct ffa_memory_region *memory_region, uint32_t *receiver_index,
3096 uint32_t share_func)
3097{
3098 ffa_memory_region_flags_t transaction_type =
3099 retrieve_request->flags &
3100 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003101 enum ffa_memory_security security_state;
J-Alves4f0d9c12024-01-17 17:23:11 +00003102 const uint64_t memory_access_desc_size =
3103 retrieve_request->memory_access_desc_size;
3104 const uint32_t expected_retrieve_request_length =
3105 retrieve_request->receivers_offset +
3106 (uint32_t)(retrieve_request->receiver_count *
3107 memory_access_desc_size);
J-Alves089004f2022-07-13 14:25:44 +01003108
3109 assert(retrieve_request != NULL);
3110 assert(memory_region != NULL);
3111 assert(receiver_index != NULL);
J-Alves089004f2022-07-13 14:25:44 +01003112
J-Alves4f0d9c12024-01-17 17:23:11 +00003113 if (retrieve_request_length != expected_retrieve_request_length) {
3114 dlog_verbose(
3115 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
3116 "but was %d.\n",
3117 expected_retrieve_request_length,
3118 retrieve_request_length);
3119 return ffa_error(FFA_INVALID_PARAMETERS);
3120 }
3121
3122 if (retrieve_request->sender != memory_region->sender) {
3123 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003124 "Memory with handle %#lx not fully sent, can't "
J-Alves4f0d9c12024-01-17 17:23:11 +00003125 "retrieve.\n",
3126 memory_region->handle);
3127 return ffa_error(FFA_DENIED);
3128 }
3129
3130 /*
3131 * The SPMC can only process retrieve requests to memory share
3132 * operations with one borrower from the other world. It can't
3133 * determine the ID of the NWd VM that invoked the retrieve
3134 * request interface call. It relies on the hypervisor to
3135 * validate the caller's ID against that provided in the
3136 * `receivers` list of the retrieve response.
3137 * In case there is only one borrower from the NWd in the
3138 * transaction descriptor, record that in the `receiver_id` for
3139 * later use, and validate in the retrieve request message.
3140 * This limitation is due to the fact SPMC can't determine the
3141 * index in the memory share structures state to update.
3142 */
3143 if (to_id == HF_HYPERVISOR_VM_ID) {
3144 uint32_t other_world_count = 0;
3145
3146 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3147 struct ffa_memory_access *receiver =
3148 ffa_memory_region_get_receiver(retrieve_request,
J-Alvesf220d572024-04-24 22:15:14 +01003149 i);
J-Alves4f0d9c12024-01-17 17:23:11 +00003150 assert(receiver != NULL);
3151
J-Alvesf220d572024-04-24 22:15:14 +01003152 if (!vm_id_is_current_world(
3153 receiver->receiver_permissions.receiver)) {
J-Alves4f0d9c12024-01-17 17:23:11 +00003154 other_world_count++;
J-Alvesf220d572024-04-24 22:15:14 +01003155 /* Set it to be used later. */
3156 to_id = receiver->receiver_permissions.receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003157 }
3158 }
3159
3160 if (other_world_count > 1) {
3161 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003162 "Support one receiver from the other world.\n");
J-Alves4f0d9c12024-01-17 17:23:11 +00003163 return ffa_error(FFA_NOT_SUPPORTED);
3164 }
3165 }
J-Alves089004f2022-07-13 14:25:44 +01003166 /*
3167 * Check that the transaction type expected by the receiver is
3168 * correct, if it has been specified.
3169 */
3170 if (transaction_type !=
3171 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
3172 transaction_type != (memory_region->flags &
3173 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
3174 dlog_verbose(
3175 "Incorrect transaction type %#x for "
Karl Meakine8937d92024-03-19 16:04:25 +00003176 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#lx.\n",
J-Alves089004f2022-07-13 14:25:44 +01003177 transaction_type,
3178 memory_region->flags &
3179 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
3180 retrieve_request->handle);
3181 return ffa_error(FFA_INVALID_PARAMETERS);
3182 }
3183
3184 if (retrieve_request->tag != memory_region->tag) {
3185 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003186 "Incorrect tag %lu for FFA_MEM_RETRIEVE_REQ, expected "
3187 "%lu for handle %#lx.\n",
J-Alves089004f2022-07-13 14:25:44 +01003188 retrieve_request->tag, memory_region->tag,
3189 retrieve_request->handle);
3190 return ffa_error(FFA_INVALID_PARAMETERS);
3191 }
3192
J-Alves4f0d9c12024-01-17 17:23:11 +00003193 *receiver_index =
3194 ffa_memory_region_get_receiver_index(memory_region, to_id);
J-Alves089004f2022-07-13 14:25:44 +01003195
3196 if (*receiver_index == memory_region->receiver_count) {
3197 dlog_verbose(
3198 "Incorrect receiver VM ID %d for "
Karl Meakine8937d92024-03-19 16:04:25 +00003199 "FFA_MEM_RETRIEVE_REQ, for handle %#lx.\n",
J-Alves4f0d9c12024-01-17 17:23:11 +00003200 to_id, memory_region->handle);
J-Alves089004f2022-07-13 14:25:44 +01003201 return ffa_error(FFA_INVALID_PARAMETERS);
3202 }
3203
3204 if ((retrieve_request->flags &
3205 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
3206 dlog_verbose(
3207 "Retriever specified 'address range alignment 'hint' "
3208 "not supported.\n");
3209 return ffa_error(FFA_INVALID_PARAMETERS);
3210 }
3211 if ((retrieve_request->flags &
3212 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
3213 dlog_verbose(
3214 "Bits 8-5 must be zero in memory region's flags "
3215 "(address range alignment hint not supported).\n");
3216 return ffa_error(FFA_INVALID_PARAMETERS);
3217 }
3218
3219 if ((retrieve_request->flags & ~0x7FF) != 0U) {
3220 dlog_verbose(
3221 "Bits 31-10 must be zero in memory region's flags.\n");
3222 return ffa_error(FFA_INVALID_PARAMETERS);
3223 }
3224
J-Alves95fbb312024-03-20 15:19:16 +00003225 if ((share_func == FFA_MEM_SHARE_32 ||
3226 share_func == FFA_MEM_SHARE_64) &&
J-Alves089004f2022-07-13 14:25:44 +01003227 (retrieve_request->flags &
3228 (FFA_MEMORY_REGION_FLAG_CLEAR |
3229 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
3230 dlog_verbose(
3231 "Memory Share operation can't clean after relinquish "
3232 "memory region.\n");
3233 return ffa_error(FFA_INVALID_PARAMETERS);
3234 }
3235
3236 /*
3237 * If the borrower needs the memory to be cleared before mapping
3238 * to its address space, the sender should have set the flag
3239 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
3240 * FFA_DENIED.
3241 */
3242 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
3243 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
3244 dlog_verbose(
3245 "Borrower needs memory cleared. Sender needs to set "
3246 "flag for clearing memory.\n");
3247 return ffa_error(FFA_DENIED);
3248 }
3249
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003250 /* Memory region attributes NS-Bit MBZ for FFA_MEM_RETRIEVE_REQ. */
Karl Meakin84710f32023-10-12 15:14:49 +01003251 security_state = retrieve_request->attributes.security;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01003252 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
3253 dlog_verbose(
3254 "Invalid security state for memory retrieve request "
3255 "operation.\n");
3256 return ffa_error(FFA_INVALID_PARAMETERS);
3257 }
3258
J-Alves089004f2022-07-13 14:25:44 +01003259 /*
3260 * If memory type is not specified, bypass validation of memory
3261 * attributes in the retrieve request. The retriever is expecting to
3262 * obtain this information from the SPMC.
3263 */
Karl Meakin84710f32023-10-12 15:14:49 +01003264 if (retrieve_request->attributes.type == FFA_MEMORY_NOT_SPECIFIED_MEM) {
J-Alves089004f2022-07-13 14:25:44 +01003265 return (struct ffa_value){.func = FFA_SUCCESS_32};
3266 }
3267
3268 /*
3269 * Ensure receiver's attributes are compatible with how
3270 * Hafnium maps memory: Normal Memory, Inner shareable,
3271 * Write-Back Read-Allocate Write-Allocate Cacheable.
3272 */
3273 return ffa_memory_attributes_validate(retrieve_request->attributes);
3274}
3275
J-Alves3f6527c2024-04-25 17:10:57 +01003276/**
3277 * Whilst processing the retrieve request, the operation could be aborted, and
3278 * changes to page tables and the share state structures need to be reverted.
3279 */
3280static void ffa_partition_memory_retrieve_request_undo(
3281 struct vm_locked from_locked,
3282 struct ffa_memory_share_state *share_state, uint32_t receiver_index)
3283{
3284 /*
3285 * Currently this operation is expected for operations involving the
3286 * 'other_world' vm.
3287 */
3288 assert(from_locked.vm->id == HF_OTHER_WORLD_ID);
3289 assert(share_state->retrieved_fragment_count[receiver_index] > 0);
3290
3291 /* Decrement the retrieved fragment count for the given receiver. */
3292 share_state->retrieved_fragment_count[receiver_index]--;
3293}
3294
3295/**
3296 * Whilst processing an hypervisor retrieve request the operation could be
3297 * aborted. There were no updates to PTs in this case, so decrementing the
3298 * fragment count retrieved by the hypervisor should be enough.
3299 */
3300static void ffa_hypervisor_memory_retrieve_request_undo(
3301 struct ffa_memory_share_state *share_state)
3302{
3303 assert(share_state->hypervisor_fragment_count > 0);
3304 share_state->hypervisor_fragment_count--;
3305}
3306
J-Alves4f0d9c12024-01-17 17:23:11 +00003307static struct ffa_value ffa_partition_retrieve_request(
3308 struct share_states_locked share_states,
3309 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3310 struct ffa_memory_region *retrieve_request,
3311 uint32_t retrieve_request_length, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003312{
Karl Meakin84710f32023-10-12 15:14:49 +01003313 ffa_memory_access_permissions_t permissions = {0};
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003314 uint32_t memory_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003315 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01003316 struct ffa_composite_memory_region *composite;
3317 uint32_t total_length;
3318 uint32_t fragment_length;
J-Alves19e20cf2023-08-02 12:48:55 +01003319 ffa_id_t receiver_id = to_locked.vm->id;
J-Alves4f0d9c12024-01-17 17:23:11 +00003320 bool is_retrieve_complete = false;
J-Alves4f0d9c12024-01-17 17:23:11 +00003321 const uint64_t memory_access_desc_size =
Daniel Boulbyde974ca2023-12-12 13:53:31 +00003322 retrieve_request->memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003323 uint32_t receiver_index;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003324 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003325 ffa_memory_handle_t handle = retrieve_request->handle;
Karl Meakin84710f32023-10-12 15:14:49 +01003326 ffa_memory_attributes_t attributes = {0};
J-Alves460d36c2023-10-12 17:02:15 +01003327 uint32_t retrieve_mode = 0;
J-Alves4f0d9c12024-01-17 17:23:11 +00003328 struct ffa_memory_region *memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003329
J-Alves96de29f2022-04-26 16:05:24 +01003330 if (!share_state->sending_complete) {
3331 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003332 "Memory with handle %#lx not fully sent, can't "
J-Alves96de29f2022-04-26 16:05:24 +01003333 "retrieve.\n",
3334 handle);
J-Alves4f0d9c12024-01-17 17:23:11 +00003335 return ffa_error(FFA_INVALID_PARAMETERS);
J-Alves96de29f2022-04-26 16:05:24 +01003336 }
3337
J-Alves4f0d9c12024-01-17 17:23:11 +00003338 /*
3339 * Validate retrieve request, according to what was sent by the
3340 * sender. Function will output the `receiver_index` from the
3341 * provided memory region.
3342 */
3343 ret = ffa_memory_retrieve_validate(
3344 receiver_id, retrieve_request, retrieve_request_length,
3345 memory_region, &receiver_index, share_state->share_func);
J-Alves089004f2022-07-13 14:25:44 +01003346
J-Alves4f0d9c12024-01-17 17:23:11 +00003347 if (ret.func != FFA_SUCCESS_32) {
3348 return ret;
J-Alves089004f2022-07-13 14:25:44 +01003349 }
J-Alves96de29f2022-04-26 16:05:24 +01003350
J-Alves4f0d9c12024-01-17 17:23:11 +00003351 /*
3352 * Validate the requested permissions against the sent
3353 * permissions.
3354 * Outputs the permissions to give to retriever at S2
3355 * PTs.
3356 */
3357 ret = ffa_memory_retrieve_validate_memory_access_list(
3358 memory_region, retrieve_request, receiver_id, &permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003359 &receiver, share_state->share_func);
J-Alves4f0d9c12024-01-17 17:23:11 +00003360 if (ret.func != FFA_SUCCESS_32) {
3361 return ret;
3362 }
3363
3364 memory_to_mode = ffa_memory_permissions_to_mode(
3365 permissions, share_state->sender_orig_mode);
3366
Daniel Boulby6e261362024-06-13 16:53:00 +01003367 /*
3368 * Check requested memory type is valid with the memory type of the
3369 * owner. E.g. they follow the memory type precedence where Normal
3370 * memory is more permissive than device and therefore device memory
3371 * can only be shared as device memory.
3372 */
3373 if (retrieve_request->attributes.type == FFA_MEMORY_NORMAL_MEM &&
3374 ((share_state->sender_orig_mode & MM_MODE_D) != 0U ||
3375 memory_region->attributes.type == FFA_MEMORY_DEVICE_MEM)) {
3376 dlog_verbose(
3377 "Retrieving device memory as Normal memory is not "
3378 "allowed\n");
3379 return ffa_error(FFA_DENIED);
3380 }
3381
J-Alves4f0d9c12024-01-17 17:23:11 +00003382 ret = ffa_retrieve_check_update(
3383 to_locked, share_state->fragments,
3384 share_state->fragment_constituent_counts,
3385 share_state->fragment_count, memory_to_mode,
J-Alves460d36c2023-10-12 17:02:15 +01003386 share_state->share_func, false, page_pool, &retrieve_mode,
3387 share_state->memory_protected);
J-Alves4f0d9c12024-01-17 17:23:11 +00003388
3389 if (ret.func != FFA_SUCCESS_32) {
3390 return ret;
3391 }
3392
3393 share_state->retrieved_fragment_count[receiver_index] = 1;
3394
3395 is_retrieve_complete =
3396 share_state->retrieved_fragment_count[receiver_index] ==
3397 share_state->fragment_count;
3398
J-Alvesb5084cf2022-07-06 14:20:12 +01003399 /* VMs acquire the RX buffer from SPMC. */
3400 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3401
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003402 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003403 * Copy response to RX buffer of caller and deliver the message.
3404 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003405 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003406 composite = ffa_memory_region_get_composite(memory_region, 0);
J-Alves4f0d9c12024-01-17 17:23:11 +00003407
Andrew Walbranca808b12020-05-15 17:22:28 +01003408 /*
J-Alves460d36c2023-10-12 17:02:15 +01003409 * Set the security state in the memory retrieve response attributes
3410 * if specified by the target mode.
3411 */
3412 attributes = plat_ffa_memory_security_mode(memory_region->attributes,
3413 retrieve_mode);
3414
3415 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003416 * Constituents which we received in the first fragment should
3417 * always fit in the first fragment we are sending, because the
3418 * header is the same size in both cases and we have a fixed
3419 * message buffer size. So `ffa_retrieved_memory_region_init`
3420 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01003421 */
Olivier Deprez878bd5b2021-04-15 19:05:10 +02003422
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003423 /* Provide the permissions that had been provided. */
3424 receiver->receiver_permissions.permissions = permissions;
3425
3426 /*
3427 * Prepare the memory region descriptor for the retrieve response.
3428 * Provide the pointer to the receiver tracked in the share state
J-Alves7b9cc432024-04-04 10:57:17 +01003429 * structures.
3430 * At this point the retrieve request descriptor from the partition
3431 * has been processed. The `retrieve_request` is expected to be in
3432 * a region that is handled by the SPMC/Hyp. Reuse the same buffer to
3433 * prepare the retrieve response before copying it to the RX buffer of
3434 * the caller.
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003435 */
Andrew Walbranca808b12020-05-15 17:22:28 +01003436 CHECK(ffa_retrieved_memory_region_init(
J-Alves7b9cc432024-04-04 10:57:17 +01003437 retrieve_request, to_locked.vm->ffa_version, HF_MAILBOX_SIZE,
3438 memory_region->sender, attributes, memory_region->flags, handle,
3439 permissions, receiver, 1, memory_access_desc_size,
3440 composite->page_count, composite->constituent_count,
3441 share_state->fragments[0],
Andrew Walbranca808b12020-05-15 17:22:28 +01003442 share_state->fragment_constituent_counts[0], &total_length,
3443 &fragment_length));
J-Alvesb5084cf2022-07-06 14:20:12 +01003444
J-Alves7b9cc432024-04-04 10:57:17 +01003445 /*
3446 * Copy the message from the buffer into the partition's mailbox.
3447 * The operation might fail unexpectedly due to change in PAS address
3448 * space, or improper values to the sizes of the structures.
3449 */
3450 if (!memcpy_trapped(to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3451 retrieve_request, fragment_length)) {
3452 dlog_error(
3453 "%s: aborted the copy of response to RX buffer of "
3454 "%x.\n",
3455 __func__, to_locked.vm->id);
J-Alves3f6527c2024-04-25 17:10:57 +01003456
3457 ffa_partition_memory_retrieve_request_undo(
3458 to_locked, share_state, receiver_index);
3459
J-Alves7b9cc432024-04-04 10:57:17 +01003460 return ffa_error(FFA_ABORTED);
3461 }
3462
J-Alves4f0d9c12024-01-17 17:23:11 +00003463 if (is_retrieve_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003464 ffa_memory_retrieve_complete(share_states, share_state,
3465 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003466 }
J-Alves4f0d9c12024-01-17 17:23:11 +00003467
3468 return ffa_memory_retrieve_resp(total_length, fragment_length);
3469}
3470
3471static struct ffa_value ffa_hypervisor_retrieve_request(
3472 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
3473 struct ffa_memory_region *retrieve_request)
3474{
3475 struct ffa_value ret;
3476 struct ffa_composite_memory_region *composite;
3477 uint32_t total_length;
3478 uint32_t fragment_length;
J-Alves4f0d9c12024-01-17 17:23:11 +00003479 ffa_memory_attributes_t attributes;
J-Alves7b6ab612024-01-24 09:54:54 +00003480 uint64_t memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00003481 struct ffa_memory_region *memory_region;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003482 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00003483 ffa_memory_handle_t handle = retrieve_request->handle;
3484
J-Alves4f0d9c12024-01-17 17:23:11 +00003485 memory_region = share_state->memory_region;
3486
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003487 assert(to_locked.vm->id == HF_HYPERVISOR_VM_ID);
3488
J-Alves7b6ab612024-01-24 09:54:54 +00003489 switch (to_locked.vm->ffa_version) {
Karl Meakin0e617d92024-04-05 12:55:22 +01003490 case FFA_VERSION_1_2:
J-Alves7b6ab612024-01-24 09:54:54 +00003491 memory_access_desc_size = sizeof(struct ffa_memory_access);
3492 break;
Karl Meakin0e617d92024-04-05 12:55:22 +01003493 case FFA_VERSION_1_0:
3494 case FFA_VERSION_1_1:
J-Alves7b6ab612024-01-24 09:54:54 +00003495 memory_access_desc_size = sizeof(struct ffa_memory_access_v1_0);
3496 break;
3497 default:
3498 panic("version not supported: %x\n", to_locked.vm->ffa_version);
3499 }
3500
J-Alves4f0d9c12024-01-17 17:23:11 +00003501 if (share_state->hypervisor_fragment_count != 0U) {
3502 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003503 "Memory with handle %#lx already retrieved by "
J-Alves4f0d9c12024-01-17 17:23:11 +00003504 "the hypervisor.\n",
3505 handle);
3506 return ffa_error(FFA_DENIED);
3507 }
3508
3509 share_state->hypervisor_fragment_count = 1;
3510
J-Alves4f0d9c12024-01-17 17:23:11 +00003511 /* VMs acquire the RX buffer from SPMC. */
3512 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3513
3514 /*
3515 * Copy response to RX buffer of caller and deliver the message.
3516 * This must be done before the share_state is (possibly) freed.
3517 */
3518 composite = ffa_memory_region_get_composite(memory_region, 0);
3519
3520 /*
3521 * Constituents which we received in the first fragment should
3522 * always fit in the first fragment we are sending, because the
3523 * header is the same size in both cases and we have a fixed
3524 * message buffer size. So `ffa_retrieved_memory_region_init`
3525 * should never fail.
3526 */
3527
3528 /*
3529 * Set the security state in the memory retrieve response attributes
3530 * if specified by the target mode.
3531 */
3532 attributes = plat_ffa_memory_security_mode(
3533 memory_region->attributes, share_state->sender_orig_mode);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003534
3535 receiver = ffa_memory_region_get_receiver(memory_region, 0);
3536
J-Alves7b9cc432024-04-04 10:57:17 +01003537 /*
3538 * At this point the `retrieve_request` is expected to be in a section
3539 * managed by the hypervisor.
3540 */
J-Alves4f0d9c12024-01-17 17:23:11 +00003541 CHECK(ffa_retrieved_memory_region_init(
J-Alves7b9cc432024-04-04 10:57:17 +01003542 retrieve_request, to_locked.vm->ffa_version, HF_MAILBOX_SIZE,
3543 memory_region->sender, attributes, memory_region->flags, handle,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003544 receiver->receiver_permissions.permissions, receiver,
3545 memory_region->receiver_count, memory_access_desc_size,
J-Alves4f0d9c12024-01-17 17:23:11 +00003546 composite->page_count, composite->constituent_count,
3547 share_state->fragments[0],
3548 share_state->fragment_constituent_counts[0], &total_length,
3549 &fragment_length));
3550
J-Alves7b9cc432024-04-04 10:57:17 +01003551 /*
3552 * Copy the message from the buffer into the hypervisor's mailbox.
3553 * The operation might fail unexpectedly due to change in PAS, or
3554 * improper values for the sizes of the structures.
3555 */
3556 if (!memcpy_trapped(to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3557 retrieve_request, fragment_length)) {
3558 dlog_error(
3559 "%s: aborted the copy of response to RX buffer of "
3560 "%x.\n",
3561 __func__, to_locked.vm->id);
J-Alves3f6527c2024-04-25 17:10:57 +01003562
3563 ffa_hypervisor_memory_retrieve_request_undo(share_state);
3564
J-Alves7b9cc432024-04-04 10:57:17 +01003565 return ffa_error(FFA_ABORTED);
3566 }
3567
J-Alves3f6527c2024-04-25 17:10:57 +01003568 ffa_memory_retrieve_complete_from_hyp(share_state);
3569
J-Alves4f0d9c12024-01-17 17:23:11 +00003570 return ffa_memory_retrieve_resp(total_length, fragment_length);
3571}
3572
3573struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
3574 struct ffa_memory_region *retrieve_request,
3575 uint32_t retrieve_request_length,
3576 struct mpool *page_pool)
3577{
3578 ffa_memory_handle_t handle = retrieve_request->handle;
3579 struct share_states_locked share_states;
3580 struct ffa_memory_share_state *share_state;
3581 struct ffa_value ret;
3582
3583 dump_share_states();
3584
3585 share_states = share_states_lock();
3586 share_state = get_share_state(share_states, handle);
3587 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003588 dlog_verbose("Invalid handle %#lx for FFA_MEM_RETRIEVE_REQ.\n",
J-Alves4f0d9c12024-01-17 17:23:11 +00003589 handle);
3590 ret = ffa_error(FFA_INVALID_PARAMETERS);
3591 goto out;
3592 }
3593
3594 if (is_ffa_hypervisor_retrieve_request(retrieve_request, to_locked)) {
3595 ret = ffa_hypervisor_retrieve_request(share_state, to_locked,
3596 retrieve_request);
3597 } else {
3598 ret = ffa_partition_retrieve_request(
3599 share_states, share_state, to_locked, retrieve_request,
3600 retrieve_request_length, page_pool);
3601 }
3602
3603 /* Track use of the RX buffer if the handling has succeeded. */
3604 if (ret.func == FFA_MEM_RETRIEVE_RESP_32) {
3605 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
3606 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
3607 }
3608
Andrew Walbranca808b12020-05-15 17:22:28 +01003609out:
3610 share_states_unlock(&share_states);
3611 dump_share_states();
3612 return ret;
3613}
3614
J-Alves5da37d92022-10-24 16:33:48 +01003615/**
3616 * Determine expected fragment offset according to the FF-A version of
3617 * the caller.
3618 */
3619static uint32_t ffa_memory_retrieve_expected_offset_per_ffa_version(
3620 struct ffa_memory_region *memory_region,
Karl Meakin0e617d92024-04-05 12:55:22 +01003621 uint32_t retrieved_constituents_count, enum ffa_version ffa_version)
J-Alves5da37d92022-10-24 16:33:48 +01003622{
3623 uint32_t expected_fragment_offset;
3624 uint32_t composite_constituents_offset;
3625
Karl Meakin0e617d92024-04-05 12:55:22 +01003626 if (ffa_version >= FFA_VERSION_1_1) {
J-Alves5da37d92022-10-24 16:33:48 +01003627 /*
3628 * Hafnium operates memory regions in FF-A v1.1 format, so we
3629 * can retrieve the constituents offset from descriptor.
3630 */
3631 composite_constituents_offset =
3632 ffa_composite_constituent_offset(memory_region, 0);
Karl Meakin0e617d92024-04-05 12:55:22 +01003633 } else if (ffa_version == FFA_VERSION_1_0) {
J-Alves5da37d92022-10-24 16:33:48 +01003634 /*
3635 * If retriever is FF-A v1.0, determine the composite offset
3636 * as it is expected to have been configured in the
3637 * retrieve response.
3638 */
3639 composite_constituents_offset =
3640 sizeof(struct ffa_memory_region_v1_0) +
3641 RECEIVERS_COUNT_IN_RETRIEVE_RESP *
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003642 sizeof(struct ffa_memory_access_v1_0) +
J-Alves5da37d92022-10-24 16:33:48 +01003643 sizeof(struct ffa_composite_memory_region);
3644 } else {
3645 panic("%s received an invalid FF-A version.\n", __func__);
3646 }
3647
3648 expected_fragment_offset =
3649 composite_constituents_offset +
3650 retrieved_constituents_count *
3651 sizeof(struct ffa_memory_region_constituent) -
Karl Meakin66a38bd2024-05-28 16:00:56 +01003652 (size_t)(memory_region->memory_access_desc_size *
3653 (memory_region->receiver_count - 1));
J-Alves5da37d92022-10-24 16:33:48 +01003654
3655 return expected_fragment_offset;
3656}
3657
Andrew Walbranca808b12020-05-15 17:22:28 +01003658struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
3659 ffa_memory_handle_t handle,
3660 uint32_t fragment_offset,
J-Alves19e20cf2023-08-02 12:48:55 +01003661 ffa_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01003662 struct mpool *page_pool)
3663{
3664 struct ffa_memory_region *memory_region;
3665 struct share_states_locked share_states;
3666 struct ffa_memory_share_state *share_state;
3667 struct ffa_value ret;
3668 uint32_t fragment_index;
3669 uint32_t retrieved_constituents_count;
3670 uint32_t i;
3671 uint32_t expected_fragment_offset;
3672 uint32_t remaining_constituent_count;
3673 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01003674 uint32_t receiver_index;
J-Alves59ed0042022-07-28 18:26:41 +01003675 bool continue_ffa_hyp_mem_retrieve_req;
Andrew Walbranca808b12020-05-15 17:22:28 +01003676
3677 dump_share_states();
3678
3679 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003680 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003681 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003682 dlog_verbose("Invalid handle %#lx for FFA_MEM_FRAG_RX.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01003683 handle);
3684 ret = ffa_error(FFA_INVALID_PARAMETERS);
3685 goto out;
3686 }
3687
3688 memory_region = share_state->memory_region;
3689 CHECK(memory_region != NULL);
3690
Andrew Walbranca808b12020-05-15 17:22:28 +01003691 if (!share_state->sending_complete) {
3692 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003693 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01003694 "retrieve.\n",
3695 handle);
3696 ret = ffa_error(FFA_INVALID_PARAMETERS);
3697 goto out;
3698 }
3699
J-Alves59ed0042022-07-28 18:26:41 +01003700 /*
3701 * If retrieve request from the hypervisor has been initiated in the
3702 * given share_state, continue it, else assume it is a continuation of
3703 * retrieve request from a NWd VM.
3704 */
3705 continue_ffa_hyp_mem_retrieve_req =
3706 (to_locked.vm->id == HF_HYPERVISOR_VM_ID) &&
3707 (share_state->hypervisor_fragment_count != 0U) &&
J-Alves661e1b72023-08-02 13:39:40 +01003708 ffa_is_vm_id(sender_vm_id);
Andrew Walbranca808b12020-05-15 17:22:28 +01003709
J-Alves59ed0042022-07-28 18:26:41 +01003710 if (!continue_ffa_hyp_mem_retrieve_req) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003711 receiver_index = ffa_memory_region_get_receiver_index(
J-Alves59ed0042022-07-28 18:26:41 +01003712 memory_region, to_locked.vm->id);
3713
3714 if (receiver_index == memory_region->receiver_count) {
3715 dlog_verbose(
3716 "Caller of FFA_MEM_FRAG_RX (%x) is not a "
Karl Meakine8937d92024-03-19 16:04:25 +00003717 "borrower to memory sharing transaction "
3718 "(%lx)\n",
J-Alves59ed0042022-07-28 18:26:41 +01003719 to_locked.vm->id, handle);
3720 ret = ffa_error(FFA_INVALID_PARAMETERS);
3721 goto out;
3722 }
3723
3724 if (share_state->retrieved_fragment_count[receiver_index] ==
3725 0 ||
3726 share_state->retrieved_fragment_count[receiver_index] >=
3727 share_state->fragment_count) {
3728 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003729 "Retrieval of memory with handle %#lx not yet "
J-Alves59ed0042022-07-28 18:26:41 +01003730 "started or already completed (%d/%d fragments "
3731 "retrieved).\n",
3732 handle,
3733 share_state->retrieved_fragment_count
3734 [receiver_index],
3735 share_state->fragment_count);
3736 ret = ffa_error(FFA_INVALID_PARAMETERS);
3737 goto out;
3738 }
3739
3740 fragment_index =
3741 share_state->retrieved_fragment_count[receiver_index];
3742 } else {
3743 if (share_state->hypervisor_fragment_count == 0 ||
3744 share_state->hypervisor_fragment_count >=
3745 share_state->fragment_count) {
3746 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003747 "Retrieve of memory with handle %lx not "
J-Alves59ed0042022-07-28 18:26:41 +01003748 "started from hypervisor.\n",
3749 handle);
3750 ret = ffa_error(FFA_INVALID_PARAMETERS);
3751 goto out;
3752 }
3753
3754 if (memory_region->sender != sender_vm_id) {
3755 dlog_verbose(
3756 "Sender ID (%x) is not as expected for memory "
Karl Meakine8937d92024-03-19 16:04:25 +00003757 "handle %lx\n",
J-Alves59ed0042022-07-28 18:26:41 +01003758 sender_vm_id, handle);
3759 ret = ffa_error(FFA_INVALID_PARAMETERS);
3760 goto out;
3761 }
3762
3763 fragment_index = share_state->hypervisor_fragment_count;
3764
3765 receiver_index = 0;
3766 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003767
3768 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003769 * Check that the given fragment offset is correct by counting
3770 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01003771 */
3772 retrieved_constituents_count = 0;
3773 for (i = 0; i < fragment_index; ++i) {
3774 retrieved_constituents_count +=
3775 share_state->fragment_constituent_counts[i];
3776 }
J-Alvesc7484f12022-05-13 12:41:14 +01003777
3778 CHECK(memory_region->receiver_count > 0);
3779
Andrew Walbranca808b12020-05-15 17:22:28 +01003780 expected_fragment_offset =
J-Alves5da37d92022-10-24 16:33:48 +01003781 ffa_memory_retrieve_expected_offset_per_ffa_version(
3782 memory_region, retrieved_constituents_count,
3783 to_locked.vm->ffa_version);
3784
Andrew Walbranca808b12020-05-15 17:22:28 +01003785 if (fragment_offset != expected_fragment_offset) {
3786 dlog_verbose("Fragment offset was %d but expected %d.\n",
3787 fragment_offset, expected_fragment_offset);
3788 ret = ffa_error(FFA_INVALID_PARAMETERS);
3789 goto out;
3790 }
3791
J-Alves4f0d9c12024-01-17 17:23:11 +00003792 /*
3793 * When hafnium is the hypervisor, acquire the RX buffer of a VM, that
3794 * is currently ownder by the SPMC.
3795 */
3796 assert(plat_ffa_acquire_receiver_rx(to_locked, &ret));
J-Alves59ed0042022-07-28 18:26:41 +01003797
Andrew Walbranca808b12020-05-15 17:22:28 +01003798 remaining_constituent_count = ffa_memory_fragment_init(
3799 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3800 share_state->fragments[fragment_index],
3801 share_state->fragment_constituent_counts[fragment_index],
3802 &fragment_length);
3803 CHECK(remaining_constituent_count == 0);
J-Alves674e4de2024-01-17 16:20:32 +00003804
Andrew Walbranca808b12020-05-15 17:22:28 +01003805 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00003806 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbranca808b12020-05-15 17:22:28 +01003807
J-Alves59ed0042022-07-28 18:26:41 +01003808 if (!continue_ffa_hyp_mem_retrieve_req) {
3809 share_state->retrieved_fragment_count[receiver_index]++;
3810 if (share_state->retrieved_fragment_count[receiver_index] ==
3811 share_state->fragment_count) {
3812 ffa_memory_retrieve_complete(share_states, share_state,
3813 page_pool);
3814 }
3815 } else {
3816 share_state->hypervisor_fragment_count++;
3817
3818 ffa_memory_retrieve_complete_from_hyp(share_state);
3819 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003820 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
3821 .arg1 = (uint32_t)handle,
3822 .arg2 = (uint32_t)(handle >> 32),
3823 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003824
3825out:
3826 share_states_unlock(&share_states);
3827 dump_share_states();
3828 return ret;
3829}
3830
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003831struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003832 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003833 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003834{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003835 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003836 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003837 struct ffa_memory_share_state *share_state;
3838 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003839 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003840 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01003841 uint32_t receiver_index;
J-Alves3c5b2072022-11-21 12:45:40 +00003842 bool receivers_relinquished_memory;
Karl Meakin84710f32023-10-12 15:14:49 +01003843 ffa_memory_access_permissions_t receiver_permissions = {0};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003844
Andrew Walbrana65a1322020-04-06 19:32:32 +01003845 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003846 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003847 "Stream endpoints not supported (got %d endpoints on "
3848 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003849 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003850 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003851 }
3852
J-Alvesbd060342024-04-26 18:44:31 +01003853 if (vm_id_is_current_world(from_locked.vm->id) &&
3854 relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003855 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003856 "VM ID %d in relinquish message doesn't match calling "
3857 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01003858 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003859 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003860 }
3861
3862 dump_share_states();
3863
3864 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003865 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003866 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003867 dlog_verbose("Invalid handle %#lx for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003868 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003869 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003870 goto out;
3871 }
3872
Andrew Walbranca808b12020-05-15 17:22:28 +01003873 if (!share_state->sending_complete) {
3874 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00003875 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01003876 "relinquish.\n",
3877 handle);
3878 ret = ffa_error(FFA_INVALID_PARAMETERS);
3879 goto out;
3880 }
3881
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003882 memory_region = share_state->memory_region;
3883 CHECK(memory_region != NULL);
3884
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003885 receiver_index = ffa_memory_region_get_receiver_index(
J-Alvesbd060342024-04-26 18:44:31 +01003886 memory_region, relinquish_request->endpoints[0]);
J-Alves8eb19162022-04-28 10:56:48 +01003887
3888 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003889 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003890 "VM ID %d tried to relinquish memory region "
Karl Meakine8937d92024-03-19 16:04:25 +00003891 "with handle %#lx and it is not a valid borrower.\n",
J-Alves8eb19162022-04-28 10:56:48 +01003892 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003893 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003894 goto out;
3895 }
3896
J-Alves8eb19162022-04-28 10:56:48 +01003897 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01003898 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003899 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01003900 "Memory with handle %#lx not yet fully retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01003901 "receiver %x can't relinquish.\n",
3902 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003903 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003904 goto out;
3905 }
3906
J-Alves3c5b2072022-11-21 12:45:40 +00003907 /*
3908 * Either clear if requested in relinquish call, or in a retrieve
3909 * request from one of the borrowers.
3910 */
3911 receivers_relinquished_memory = true;
3912
3913 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3914 struct ffa_memory_access *receiver =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003915 ffa_memory_region_get_receiver(memory_region, i);
3916 assert(receiver != NULL);
J-Alves3c5b2072022-11-21 12:45:40 +00003917 if (receiver->receiver_permissions.receiver ==
3918 from_locked.vm->id) {
J-Alves639ddfc2023-11-21 14:17:26 +00003919 receiver_permissions =
3920 receiver->receiver_permissions.permissions;
J-Alves3c5b2072022-11-21 12:45:40 +00003921 continue;
3922 }
3923
3924 if (share_state->retrieved_fragment_count[i] != 0U) {
3925 receivers_relinquished_memory = false;
3926 break;
3927 }
3928 }
3929
3930 clear = receivers_relinquished_memory &&
Daniel Boulby2e14ebe2024-01-15 16:21:44 +00003931 ((relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
3932 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003933
3934 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003935 * Clear is not allowed for memory that was shared, as the
3936 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003937 */
J-Alves95fbb312024-03-20 15:19:16 +00003938 if (clear && (share_state->share_func == FFA_MEM_SHARE_32 ||
3939 share_state->share_func == FFA_MEM_SHARE_64)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003940 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003941 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003942 goto out;
3943 }
3944
J-Alvesb886d492024-04-15 10:55:29 +01003945 if (clear && receiver_permissions.data_access == FFA_DATA_ACCESS_RO) {
J-Alves639ddfc2023-11-21 14:17:26 +00003946 dlog_verbose("%s: RO memory can't use clear memory flag.\n",
3947 __func__);
3948 ret = ffa_error(FFA_DENIED);
3949 goto out;
3950 }
3951
Andrew Walbranca808b12020-05-15 17:22:28 +01003952 ret = ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01003953 from_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003954 share_state->fragment_constituent_counts,
J-Alves69cdfd92024-04-26 11:40:59 +01003955 share_state->fragment_count, share_state->sender_orig_mode,
3956 page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003957
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003958 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003959 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003960 * Mark memory handle as not retrieved, so it can be
3961 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003962 */
J-Alves8eb19162022-04-28 10:56:48 +01003963 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003964 }
3965
3966out:
3967 share_states_unlock(&share_states);
3968 dump_share_states();
3969 return ret;
3970}
3971
3972/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01003973 * Validates that the reclaim transition is allowed for the given
3974 * handle, updates the page table of the reclaiming VM, and frees the
3975 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003976 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003977struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01003978 ffa_memory_handle_t handle,
3979 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003980 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003981{
3982 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003983 struct ffa_memory_share_state *share_state;
3984 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003985 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003986
3987 dump_share_states();
3988
3989 share_states = share_states_lock();
Karl Meakin52cdfe72023-06-30 14:49:10 +01003990
Karl Meakin4a2854a2023-06-30 16:26:52 +01003991 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003992 if (share_state == NULL) {
Karl Meakine8937d92024-03-19 16:04:25 +00003993 dlog_verbose("Invalid handle %#lx for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003994 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003995 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003996 goto out;
3997 }
Karl Meakin4a2854a2023-06-30 16:26:52 +01003998 memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003999
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004000 CHECK(memory_region != NULL);
4001
J-Alvesa9cd7e32022-07-01 13:49:33 +01004002 if (vm_id_is_current_world(to_locked.vm->id) &&
4003 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004004 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004005 "VM %#x attempted to reclaim memory handle %#lx "
Olivier Deprezf92e5d42020-11-13 16:00:54 +01004006 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004007 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01004008 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004009 goto out;
4010 }
4011
Andrew Walbranca808b12020-05-15 17:22:28 +01004012 if (!share_state->sending_complete) {
4013 dlog_verbose(
Karl Meakine8937d92024-03-19 16:04:25 +00004014 "Memory with handle %#lx not fully sent, can't "
Andrew Walbranca808b12020-05-15 17:22:28 +01004015 "reclaim.\n",
4016 handle);
4017 ret = ffa_error(FFA_INVALID_PARAMETERS);
4018 goto out;
4019 }
4020
J-Alves752236c2022-04-28 11:07:47 +01004021 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
4022 if (share_state->retrieved_fragment_count[i] != 0) {
J-Alves9bbcb872024-04-25 17:19:00 +01004023 struct ffa_memory_access *receiver =
4024 ffa_memory_region_get_receiver(memory_region,
4025 i);
4026
4027 assert(receiver != NULL);
4028 (void)receiver;
J-Alves752236c2022-04-28 11:07:47 +01004029 dlog_verbose(
J-Alves0a824e92024-04-26 16:20:12 +01004030 "Tried to reclaim memory handle %#lx that has "
4031 "not been relinquished by all borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01004032 handle,
J-Alves9bbcb872024-04-25 17:19:00 +01004033 receiver->receiver_permissions.receiver);
J-Alves752236c2022-04-28 11:07:47 +01004034 ret = ffa_error(FFA_DENIED);
4035 goto out;
4036 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004037 }
4038
Andrew Walbranca808b12020-05-15 17:22:28 +01004039 ret = ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01004040 to_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01004041 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00004042 share_state->fragment_count, share_state->sender_orig_mode,
J-Alves460d36c2023-10-12 17:02:15 +01004043 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool,
J-Alvesfd206052023-05-22 16:45:00 +01004044 NULL, share_state->memory_protected);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004045
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01004046 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004047 share_state_free(share_states, share_state, page_pool);
J-Alves3c5b2072022-11-21 12:45:40 +00004048 dlog_verbose("Freed share state after successful reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00004049 }
4050
4051out:
4052 share_states_unlock(&share_states);
4053 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01004054}