blob: 354c80c3aaade54a24c62440c24da08633af6d90 [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
Federico Recanati4fd065d2021-12-13 20:06:23 +010011#include "hf/arch/mm.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020012#include "hf/arch/other_world.h"
Olivier Deprez55a189e2021-06-09 15:45:27 +020013#include "hf/arch/plat/ffa.h"
Andrew Walbran290b0c92020-02-03 16:37:14 +000014
J-Alves5952d942022-12-22 16:03:00 +000015#include "hf/addr.h"
Jose Marinho75509b42019-04-09 09:34:59 +010016#include "hf/api.h"
Daniel Boulbya2f8c662021-11-26 17:52:53 +000017#include "hf/assert.h"
Jose Marinho09b1db82019-08-08 09:16:59 +010018#include "hf/check.h"
Jose Marinho75509b42019-04-09 09:34:59 +010019#include "hf/dlog.h"
J-Alves3456e032023-07-20 12:20:05 +010020#include "hf/ffa.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010021#include "hf/ffa_internal.h"
J-Alves66652252022-07-06 09:49:51 +010022#include "hf/ffa_memory_internal.h"
J-Alves3456e032023-07-20 12:20:05 +010023#include "hf/ffa_partition_manifest.h"
J-Alves5952d942022-12-22 16:03:00 +000024#include "hf/mm.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000025#include "hf/mpool.h"
Jose Marinho75509b42019-04-09 09:34:59 +010026#include "hf/std.h"
Andrew Scull3c257452019-11-26 13:32:50 +000027#include "hf/vm.h"
Daniel Boulby44e9b3b2024-01-17 12:21:44 +000028#include "hf/vm_ids.h"
Jose Marinho75509b42019-04-09 09:34:59 +010029
J-Alves2d8457f2022-10-05 11:06:41 +010030#include "vmapi/hf/ffa_v1_0.h"
31
J-Alves5da37d92022-10-24 16:33:48 +010032#define RECEIVERS_COUNT_IN_RETRIEVE_RESP 1
33
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000034/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010035 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000036 * by this lock.
37 */
38static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010039static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000040
41/**
J-Alvesed508c82023-05-04 16:09:48 +010042 * Return the offset to the first constituent within the
43 * `ffa_composite_memory_region` for the given receiver from an
44 * `ffa_memory_region`. The caller must check that the receiver_index is within
45 * bounds, and that it has a composite memory region offset.
46 */
47static uint32_t ffa_composite_constituent_offset(
48 struct ffa_memory_region *memory_region, uint32_t receiver_index)
49{
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +000050 struct ffa_memory_access *receiver;
51 uint32_t composite_offset;
J-Alvesed508c82023-05-04 16:09:48 +010052
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +000053 CHECK(receiver_index < memory_region->receiver_count);
54
55 receiver =
56 ffa_memory_region_get_receiver(memory_region, receiver_index);
57 CHECK(receiver != NULL);
58
59 composite_offset = receiver->composite_memory_region_offset;
60
61 CHECK(composite_offset != 0);
62
63 return composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alvesed508c82023-05-04 16:09:48 +010064}
65
66/**
J-Alves917d2f22020-10-30 18:39:30 +000067 * Extracts the index from a memory handle allocated by Hafnium's current world.
68 */
69uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle)
70{
71 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
72}
73
74/**
Karl Meakin52cdfe72023-06-30 14:49:10 +010075 * Initialises the next available `struct ffa_memory_share_state`. If `handle`
76 * is `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle,
77 * otherwise uses the provided handle which is assumed to be globally unique.
Andrew Walbranca808b12020-05-15 17:22:28 +010078 *
Karl Meakin52cdfe72023-06-30 14:49:10 +010079 * Returns a pointer to the allocated `ffa_memory_share_state` on success or
80 * `NULL` if none are available.
Andrew Walbranca808b12020-05-15 17:22:28 +010081 */
Karl Meakin52cdfe72023-06-30 14:49:10 +010082struct ffa_memory_share_state *allocate_share_state(
83 struct share_states_locked share_states, uint32_t share_func,
84 struct ffa_memory_region *memory_region, uint32_t fragment_length,
85 ffa_memory_handle_t handle)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000086{
Daniel Boulbya2f8c662021-11-26 17:52:53 +000087 assert(share_states.share_states != NULL);
88 assert(memory_region != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000089
Karl Meakin52cdfe72023-06-30 14:49:10 +010090 for (uint64_t i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +010091 if (share_states.share_states[i].share_func == 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010092 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +010093 &share_states.share_states[i];
94 struct ffa_composite_memory_region *composite =
95 ffa_memory_region_get_composite(memory_region,
96 0);
97
98 if (handle == FFA_MEMORY_HANDLE_INVALID) {
J-Alvesee68c542020-10-29 17:48:20 +000099 memory_region->handle =
Olivier Deprez55a189e2021-06-09 15:45:27 +0200100 plat_ffa_memory_handle_make(i);
Andrew Walbranca808b12020-05-15 17:22:28 +0100101 } else {
J-Alvesee68c542020-10-29 17:48:20 +0000102 memory_region->handle = handle;
Andrew Walbranca808b12020-05-15 17:22:28 +0100103 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000104 allocated_state->share_func = share_func;
105 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +0100106 allocated_state->fragment_count = 1;
107 allocated_state->fragments[0] = composite->constituents;
108 allocated_state->fragment_constituent_counts[0] =
109 (fragment_length -
110 ffa_composite_constituent_offset(memory_region,
111 0)) /
112 sizeof(struct ffa_memory_region_constituent);
113 allocated_state->sending_complete = false;
Karl Meakin52cdfe72023-06-30 14:49:10 +0100114 for (uint32_t j = 0; j < MAX_MEM_SHARE_RECIPIENTS;
115 ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100116 allocated_state->retrieved_fragment_count[j] =
117 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000118 }
Karl Meakin52cdfe72023-06-30 14:49:10 +0100119 return allocated_state;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000120 }
121 }
122
Karl Meakin52cdfe72023-06-30 14:49:10 +0100123 return NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000124}
125
126/** Locks the share states lock. */
127struct share_states_locked share_states_lock(void)
128{
129 sl_lock(&share_states_lock_instance);
130
131 return (struct share_states_locked){.share_states = share_states};
132}
133
134/** Unlocks the share states lock. */
J-Alves66652252022-07-06 09:49:51 +0100135void share_states_unlock(struct share_states_locked *share_states)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000136{
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000137 assert(share_states->share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000138 share_states->share_states = NULL;
139 sl_unlock(&share_states_lock_instance);
140}
141
142/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100143 * If the given handle is a valid handle for an allocated share state then
Karl Meakin4a2854a2023-06-30 16:26:52 +0100144 * returns a pointer to the share state. Otherwise returns NULL.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000145 */
Karl Meakin4a2854a2023-06-30 16:26:52 +0100146struct ffa_memory_share_state *get_share_state(
147 struct share_states_locked share_states, ffa_memory_handle_t handle)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000148{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100149 struct ffa_memory_share_state *share_state;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000150
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000151 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100152
153 /*
154 * First look for a share_state allocated by us, in which case the
155 * handle is based on the index.
156 */
Olivier Deprez55a189e2021-06-09 15:45:27 +0200157 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100158 uint64_t index = ffa_memory_handle_get_index(handle);
159
Andrew Walbranca808b12020-05-15 17:22:28 +0100160 if (index < MAX_MEM_SHARES) {
161 share_state = &share_states.share_states[index];
162 if (share_state->share_func != 0) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100163 return share_state;
Andrew Walbranca808b12020-05-15 17:22:28 +0100164 }
165 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000166 }
167
Andrew Walbranca808b12020-05-15 17:22:28 +0100168 /* Fall back to a linear scan. */
Karl Meakin4a2854a2023-06-30 16:26:52 +0100169 for (uint64_t index = 0; index < MAX_MEM_SHARES; ++index) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100170 share_state = &share_states.share_states[index];
J-Alvesee68c542020-10-29 17:48:20 +0000171 if (share_state->memory_region != NULL &&
172 share_state->memory_region->handle == handle &&
Andrew Walbranca808b12020-05-15 17:22:28 +0100173 share_state->share_func != 0) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100174 return share_state;
Andrew Walbranca808b12020-05-15 17:22:28 +0100175 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000176 }
177
Karl Meakin4a2854a2023-06-30 16:26:52 +0100178 return NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000179}
180
181/** Marks a share state as unallocated. */
J-Alvesfdd29272022-07-19 13:16:31 +0100182void share_state_free(struct share_states_locked share_states,
183 struct ffa_memory_share_state *share_state,
184 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000185{
Andrew Walbranca808b12020-05-15 17:22:28 +0100186 uint32_t i;
187
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000188 assert(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000189 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100190 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000191 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100192 /*
193 * First fragment is part of the same page as the `memory_region`, so it
194 * doesn't need to be freed separately.
195 */
196 share_state->fragments[0] = NULL;
197 share_state->fragment_constituent_counts[0] = 0;
198 for (i = 1; i < share_state->fragment_count; ++i) {
199 mpool_free(page_pool, share_state->fragments[i]);
200 share_state->fragments[i] = NULL;
201 share_state->fragment_constituent_counts[i] = 0;
202 }
203 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000204 share_state->memory_region = NULL;
J-Alvesa9cd7e32022-07-01 13:49:33 +0100205 share_state->hypervisor_fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000206}
207
Andrew Walbranca808b12020-05-15 17:22:28 +0100208/** Checks whether the given share state has been fully sent. */
J-Alvesfdd29272022-07-19 13:16:31 +0100209bool share_state_sending_complete(struct share_states_locked share_states,
210 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000211{
Andrew Walbranca808b12020-05-15 17:22:28 +0100212 struct ffa_composite_memory_region *composite;
213 uint32_t expected_constituent_count;
214 uint32_t fragment_constituent_count_total = 0;
215 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000216
Andrew Walbranca808b12020-05-15 17:22:28 +0100217 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000218 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100219
220 /*
221 * Share state must already be valid, or it's not possible to get hold
222 * of it.
223 */
224 CHECK(share_state->memory_region != NULL &&
225 share_state->share_func != 0);
226
227 composite =
228 ffa_memory_region_get_composite(share_state->memory_region, 0);
229 expected_constituent_count = composite->constituent_count;
230 for (i = 0; i < share_state->fragment_count; ++i) {
231 fragment_constituent_count_total +=
232 share_state->fragment_constituent_counts[i];
233 }
234 dlog_verbose(
235 "Checking completion: constituent count %d/%d from %d "
236 "fragments.\n",
237 fragment_constituent_count_total, expected_constituent_count,
238 share_state->fragment_count);
239
240 return fragment_constituent_count_total == expected_constituent_count;
241}
242
243/**
244 * Calculates the offset of the next fragment expected for the given share
245 * state.
246 */
J-Alvesfdd29272022-07-19 13:16:31 +0100247uint32_t share_state_next_fragment_offset(
Andrew Walbranca808b12020-05-15 17:22:28 +0100248 struct share_states_locked share_states,
249 struct ffa_memory_share_state *share_state)
250{
251 uint32_t next_fragment_offset;
252 uint32_t i;
253
254 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000255 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100256
257 next_fragment_offset =
258 ffa_composite_constituent_offset(share_state->memory_region, 0);
259 for (i = 0; i < share_state->fragment_count; ++i) {
260 next_fragment_offset +=
261 share_state->fragment_constituent_counts[i] *
262 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000263 }
264
Andrew Walbranca808b12020-05-15 17:22:28 +0100265 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000266}
267
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100268static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000269{
270 uint32_t i;
271
272 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
273 return;
274 }
275
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000276 dlog("from VM %#x, attributes %#x, flags %#x, handle %#x "
277 "tag %u, memory access descriptor size %u, to %u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100278 "recipients [",
279 memory_region->sender, memory_region->attributes,
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000280 memory_region->flags, memory_region->handle, memory_region->tag,
281 memory_region->memory_access_desc_size,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100282 memory_region->receiver_count);
283 for (i = 0; i < memory_region->receiver_count; ++i) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000284 struct ffa_memory_access *receiver =
285 ffa_memory_region_get_receiver(memory_region, i);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000286 if (i != 0) {
287 dlog(", ");
288 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000289 dlog("Receiver %#x: %#x (offset %u)",
290 receiver->receiver_permissions.receiver,
291 receiver->receiver_permissions.permissions,
292 receiver->composite_memory_region_offset);
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000293 /* The impdef field is only present from v1.2 and later */
294 if (ffa_version_from_memory_access_desc_size(
295 memory_region->memory_access_desc_size) >=
296 MAKE_FFA_VERSION(1, 2)) {
297 dlog(", impdef: %#x %#x", receiver->impdef.val[0],
298 receiver->impdef.val[1]);
299 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000300 }
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000301 dlog("] at offset %u", memory_region->receivers_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000302}
303
J-Alves66652252022-07-06 09:49:51 +0100304void dump_share_states(void)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000305{
306 uint32_t i;
307
308 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
309 return;
310 }
311
312 dlog("Current share states:\n");
313 sl_lock(&share_states_lock_instance);
314 for (i = 0; i < MAX_MEM_SHARES; ++i) {
315 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000316 switch (share_states[i].share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100317 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000318 dlog("SHARE");
319 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100320 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000321 dlog("LEND");
322 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100323 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000324 dlog("DONATE");
325 break;
326 default:
327 dlog("invalid share_func %#x",
328 share_states[i].share_func);
329 }
Olivier Deprez935e1b12020-12-22 18:01:29 +0100330 dlog(" %#x (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000331 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100332 if (share_states[i].sending_complete) {
333 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000334 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100335 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000336 }
J-Alves2a0d2882020-10-29 14:49:50 +0000337 dlog(" with %d fragments, %d retrieved, "
338 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100339 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000340 share_states[i].retrieved_fragment_count[0],
341 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000342 }
343 }
344 sl_unlock(&share_states_lock_instance);
345}
346
Andrew Walbran475c1452020-02-07 13:22:22 +0000347/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100348static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100349 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000350{
351 uint32_t mode = 0;
352
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100353 switch (ffa_get_data_access_attr(permissions)) {
354 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000355 mode = MM_MODE_R;
356 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100357 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000358 mode = MM_MODE_R | MM_MODE_W;
359 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100360 case FFA_DATA_ACCESS_NOT_SPECIFIED:
361 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
362 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100363 case FFA_DATA_ACCESS_RESERVED:
364 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100365 }
366
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100367 switch (ffa_get_instruction_access_attr(permissions)) {
368 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000369 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100370 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100371 mode |= MM_MODE_X;
372 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100373 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
374 mode |= (default_mode & MM_MODE_X);
375 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100376 case FFA_INSTRUCTION_ACCESS_RESERVED:
377 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000378 }
379
Olivier Deprez878bd5b2021-04-15 19:05:10 +0200380 /* Set the security state bit if necessary. */
381 if ((default_mode & plat_ffa_other_world_mode()) != 0) {
382 mode |= plat_ffa_other_world_mode();
383 }
384
Andrew Walbran475c1452020-02-07 13:22:22 +0000385 return mode;
386}
387
Jose Marinho75509b42019-04-09 09:34:59 +0100388/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000389 * Get the current mode in the stage-2 page table of the given vm of all the
390 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100391 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100392 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100393static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000394 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100395 struct ffa_memory_region_constituent **fragments,
396 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100397{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100398 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100399 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100400
Andrew Walbranca808b12020-05-15 17:22:28 +0100401 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100402 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000403 * Fail if there are no constituents. Otherwise we would get an
404 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100405 */
Karl Meakin5df422c2023-07-11 17:31:38 +0100406 dlog_verbose("%s: no constituents\n", __func__);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100407 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100408 }
409
Andrew Walbranca808b12020-05-15 17:22:28 +0100410 for (i = 0; i < fragment_count; ++i) {
411 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
412 ipaddr_t begin = ipa_init(fragments[i][j].address);
413 size_t size = fragments[i][j].page_count * PAGE_SIZE;
414 ipaddr_t end = ipa_add(begin, size);
415 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100416
Andrew Walbranca808b12020-05-15 17:22:28 +0100417 /* Fail if addresses are not page-aligned. */
418 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
419 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100420 dlog_verbose("%s: addresses not page-aligned\n",
421 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +0100422 return ffa_error(FFA_INVALID_PARAMETERS);
423 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100424
Andrew Walbranca808b12020-05-15 17:22:28 +0100425 /*
426 * Ensure that this constituent memory range is all
427 * mapped with the same mode.
428 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800429 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100430 dlog_verbose(
431 "%s: constituent memory range %#x..%#x "
432 "not mapped with the same mode\n",
433 __func__, begin, end);
Andrew Walbranca808b12020-05-15 17:22:28 +0100434 return ffa_error(FFA_DENIED);
435 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100436
Andrew Walbranca808b12020-05-15 17:22:28 +0100437 /*
438 * Ensure that all constituents are mapped with the same
439 * mode.
440 */
441 if (i == 0) {
442 *orig_mode = current_mode;
443 } else if (current_mode != *orig_mode) {
444 dlog_verbose(
Karl Meakin5df422c2023-07-11 17:31:38 +0100445 "%s: expected mode %#x but was %#x for "
446 "%d pages at %#x.\n",
447 __func__, *orig_mode, current_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100448 fragments[i][j].page_count,
449 ipa_addr(begin));
450 return ffa_error(FFA_DENIED);
451 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100452 }
Jose Marinho75509b42019-04-09 09:34:59 +0100453 }
454
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100455 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000456}
457
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100458uint32_t ffa_version_from_memory_access_desc_size(
459 uint32_t memory_access_desc_size)
460{
461 switch (memory_access_desc_size) {
462 /*
463 * v1.0 and v1.1 memory access descriptors are the same size however
464 * v1.1 is the first version to include the memory access descriptor
465 * size field so return v1.1.
466 */
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000467 case sizeof(struct ffa_memory_access_v1_0):
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100468 return MAKE_FFA_VERSION(1, 1);
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000469 case sizeof(struct ffa_memory_access):
470 return MAKE_FFA_VERSION(1, 2);
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100471 }
472 return 0;
473}
474
475/**
476 * Check if the receivers size and offset given is valid for the senders
477 * FF-A version.
478 */
479static bool receiver_size_and_offset_valid_for_version(
480 uint32_t receivers_size, uint32_t receivers_offset,
481 uint32_t ffa_version)
482{
483 /*
484 * Check that the version that the memory access descriptor size belongs
485 * to is compatible with the FF-A version we believe the sender to be.
486 */
487 uint32_t expected_ffa_version =
488 ffa_version_from_memory_access_desc_size(receivers_size);
489 if (!FFA_VERSIONS_ARE_COMPATIBLE(expected_ffa_version, ffa_version)) {
490 return false;
491 }
492
493 /*
494 * Check the receivers_offset matches the version we found from
495 * memory access descriptor size.
496 */
497 switch (expected_ffa_version) {
498 case MAKE_FFA_VERSION(1, 1):
Daniel Boulbyde974ca2023-12-12 13:53:31 +0000499 case MAKE_FFA_VERSION(1, 2):
Daniel Boulbyc7dc9322023-10-27 15:12:07 +0100500 return receivers_offset == sizeof(struct ffa_memory_region);
501 default:
502 return false;
503 }
504}
505
506/**
507 * Check the values set for fields in the memory region are valid and safe.
508 * Offset values are within safe bounds, receiver count will not cause overflows
509 * and reserved fields are 0.
510 */
511bool ffa_memory_region_sanity_check(struct ffa_memory_region *memory_region,
512 uint32_t ffa_version,
513 uint32_t fragment_length,
514 bool send_transaction)
515{
516 uint32_t receiver_count;
517 struct ffa_memory_access *receiver;
518 uint32_t composite_offset_0;
519
520 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
521 struct ffa_memory_region_v1_0 *memory_region_v1_0 =
522 (struct ffa_memory_region_v1_0 *)memory_region;
523 /* Check the reserved fields are 0. */
524 if (memory_region_v1_0->reserved_0 != 0 ||
525 memory_region_v1_0->reserved_1 != 0) {
526 dlog_verbose("Reserved fields must be 0.\n");
527 return false;
528 }
529
530 receiver_count = memory_region_v1_0->receiver_count;
531 } else {
532 uint32_t receivers_size =
533 memory_region->memory_access_desc_size;
534 uint32_t receivers_offset = memory_region->receivers_offset;
535
536 /* Check the reserved field is 0. */
537 if (memory_region->reserved[0] != 0 ||
538 memory_region->reserved[1] != 0 ||
539 memory_region->reserved[2] != 0) {
540 dlog_verbose("Reserved fields must be 0.\n");
541 return false;
542 }
543
544 /*
545 * Check memory_access_desc_size matches the size of the struct
546 * for the senders FF-A version.
547 */
548 if (!receiver_size_and_offset_valid_for_version(
549 receivers_size, receivers_offset, ffa_version)) {
550 dlog_verbose(
551 "Invalid memory access descriptor size %d, "
552 " or receiver offset %d, "
553 "for FF-A version %#x\n",
554 receivers_size, receivers_offset, ffa_version);
555 return false;
556 }
557
558 receiver_count = memory_region->receiver_count;
559 }
560
561 /* Check receiver count is not too large. */
562 if (receiver_count > MAX_MEM_SHARE_RECIPIENTS) {
563 dlog_verbose(
564 "Max number of recipients supported is %u "
565 "specified %u\n",
566 MAX_MEM_SHARE_RECIPIENTS, receiver_count);
567 return false;
568 }
569
570 /* Check values in the memory access descriptors. */
571 /*
572 * The composite offset values must be the same for all recievers so
573 * check the first one is valid and then they are all the same.
574 */
575 receiver = ffa_version == MAKE_FFA_VERSION(1, 0)
576 ? (struct ffa_memory_access *)&(
577 (struct ffa_memory_region_v1_0 *)
578 memory_region)
579 ->receivers[0]
580 : ffa_memory_region_get_receiver(memory_region, 0);
581 assert(receiver != NULL);
582 composite_offset_0 = receiver->composite_memory_region_offset;
583
584 if (!send_transaction) {
585 if (composite_offset_0 != 0) {
586 dlog_verbose(
587 "Composite offset memory region descriptor "
588 "offset must be 0 for retrieve requests. "
589 "Currently %d",
590 composite_offset_0);
591 return false;
592 }
593 } else {
594 bool comp_offset_is_zero = composite_offset_0 == 0U;
595 bool comp_offset_lt_transaction_descriptor_size =
596 composite_offset_0 <
597 (sizeof(struct ffa_memory_region) +
598 (uint32_t)(memory_region->memory_access_desc_size *
599 memory_region->receiver_count));
600 bool comp_offset_with_comp_gt_fragment_length =
601 composite_offset_0 +
602 sizeof(struct ffa_composite_memory_region) >
603 fragment_length;
604 if (comp_offset_is_zero ||
605 comp_offset_lt_transaction_descriptor_size ||
606 comp_offset_with_comp_gt_fragment_length) {
607 dlog_verbose(
608 "Invalid composite memory region descriptor "
609 "offset for send transaction %u\n",
610 composite_offset_0);
611 return false;
612 }
613 }
614
615 for (int i = 0; i < memory_region->receiver_count; i++) {
616 uint32_t composite_offset;
617
618 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
619 struct ffa_memory_region_v1_0 *memory_region_v1_0 =
620 (struct ffa_memory_region_v1_0 *)memory_region;
621
622 struct ffa_memory_access_v1_0 *receiver_v1_0 =
623 &memory_region_v1_0->receivers[i];
624 /* Check reserved fields are 0 */
625 if (receiver_v1_0->reserved_0 != 0) {
626 dlog_verbose(
627 "Reserved field in the memory access "
628 " descriptor must be zero "
629 " Currently reciever %d has a reserved "
630 " field with a value of %d\n",
631 i, receiver_v1_0->reserved_0);
632 return false;
633 }
634 /*
635 * We can cast to the current version receiver as the
636 * remaining fields we are checking have the same
637 * offsets for all versions since memory access
638 * descriptors are forwards compatible.
639 */
640 receiver = (struct ffa_memory_access *)receiver_v1_0;
641 } else {
642 receiver = ffa_memory_region_get_receiver(memory_region,
643 i);
644 assert(receiver != NULL);
645
646 if (receiver->reserved_0 != 0) {
647 dlog_verbose(
648 "Reserved field in the memory access "
649 " descriptor must be zero "
650 " Currently reciever %d has a reserved "
651 " field with a value of %d\n",
652 i, receiver->reserved_0);
653 return false;
654 }
655 }
656
657 /* Check composite offset values are equal for all receivers. */
658 composite_offset = receiver->composite_memory_region_offset;
659 if (composite_offset != composite_offset_0) {
660 dlog_verbose(
661 "Composite offset %x differs from %x in index "
662 "%u\n",
663 composite_offset, composite_offset_0);
664 return false;
665 }
666 }
667 return true;
668}
669
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000670/**
671 * Verify that all pages have the same mode, that the starting mode
672 * constitutes a valid state and obtain the next mode to apply
673 * to the sending VM.
674 *
675 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100676 * 1) FFA_DENIED if a state transition was not found;
677 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100678 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100679 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100680 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100681 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
682 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000683 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100684static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100685 struct vm_locked from, uint32_t share_func,
J-Alves363f5722022-04-25 17:37:37 +0100686 struct ffa_memory_access *receivers, uint32_t receivers_count,
687 uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100688 struct ffa_memory_region_constituent **fragments,
689 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
690 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000691{
692 const uint32_t state_mask =
693 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100694 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000695
Andrew Walbranca808b12020-05-15 17:22:28 +0100696 ret = constituents_get_mode(from, orig_from_mode, fragments,
697 fragment_constituent_counts,
698 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100699 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100700 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100701 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100702 }
703
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000704 /* Ensure the address range is normal memory and not a device. */
J-Alves788b4492023-04-18 14:01:23 +0100705 if ((*orig_from_mode & MM_MODE_D) != 0U) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000706 dlog_verbose("Can't share device memory (mode is %#x).\n",
707 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100708 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000709 }
710
711 /*
712 * Ensure the sender is the owner and has exclusive access to the
713 * memory.
714 */
715 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100716 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100717 }
718
J-Alves363f5722022-04-25 17:37:37 +0100719 assert(receivers != NULL && receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100720
J-Alves363f5722022-04-25 17:37:37 +0100721 for (uint32_t i = 0U; i < receivers_count; i++) {
722 ffa_memory_access_permissions_t permissions =
723 receivers[i].receiver_permissions.permissions;
724 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
725 permissions, *orig_from_mode);
726
J-Alves788b4492023-04-18 14:01:23 +0100727 /*
728 * The assumption is that at this point, the operation from
729 * SP to a receiver VM, should have returned an FFA_ERROR
730 * already.
731 */
732 if (!ffa_is_vm_id(from.vm->id)) {
733 assert(!ffa_is_vm_id(
734 receivers[i].receiver_permissions.receiver));
735 }
736
J-Alves363f5722022-04-25 17:37:37 +0100737 if ((*orig_from_mode & required_from_mode) !=
738 required_from_mode) {
739 dlog_verbose(
740 "Sender tried to send memory with permissions "
J-Alves788b4492023-04-18 14:01:23 +0100741 "which required mode %#x but only had %#x "
742 "itself.\n",
J-Alves363f5722022-04-25 17:37:37 +0100743 required_from_mode, *orig_from_mode);
744 return ffa_error(FFA_DENIED);
745 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000746 }
747
748 /* Find the appropriate new mode. */
749 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000750 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100751 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000752 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100753 break;
754
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100755 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000756 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100757 break;
758
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100759 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000760 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100761 break;
762
Jose Marinho75509b42019-04-09 09:34:59 +0100763 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100764 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100765 }
766
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100767 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000768}
769
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100770static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000771 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100772 struct ffa_memory_region_constituent **fragments,
773 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
774 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000775{
776 const uint32_t state_mask =
777 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
778 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100779 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000780
Andrew Walbranca808b12020-05-15 17:22:28 +0100781 ret = constituents_get_mode(from, orig_from_mode, fragments,
782 fragment_constituent_counts,
783 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100784 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100785 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000786 }
787
788 /* Ensure the address range is normal memory and not a device. */
789 if (*orig_from_mode & MM_MODE_D) {
790 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
791 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100792 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000793 }
794
795 /*
796 * Ensure the relinquishing VM is not the owner but has access to the
797 * memory.
798 */
799 orig_from_state = *orig_from_mode & state_mask;
800 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
801 dlog_verbose(
802 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100803 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000804 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100805 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000806 }
807
808 /* Find the appropriate new mode. */
809 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
810
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100811 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000812}
813
814/**
815 * Verify that all pages have the same mode, that the starting mode
816 * constitutes a valid state and obtain the next mode to apply
817 * to the retrieving VM.
818 *
819 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100820 * 1) FFA_DENIED if a state transition was not found;
821 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100822 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100823 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100824 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100825 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
826 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000827 */
J-Alvesfc19b372022-07-06 12:17:35 +0100828struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000829 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100830 struct ffa_memory_region_constituent **fragments,
831 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
832 uint32_t memory_to_attributes, uint32_t *to_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000833{
834 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100835 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000836
Andrew Walbranca808b12020-05-15 17:22:28 +0100837 ret = constituents_get_mode(to, &orig_to_mode, fragments,
838 fragment_constituent_counts,
839 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100840 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100841 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100842 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000843 }
844
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100845 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000846 /*
847 * If the original ffa memory send call has been processed
848 * successfully, it is expected the orig_to_mode would overlay
849 * with `state_mask`, as a result of the function
850 * `ffa_send_check_transition`.
851 */
J-Alves59ed0042022-07-28 18:26:41 +0100852 if (vm_id_is_current_world(to.vm->id)) {
853 assert((orig_to_mode &
854 (MM_MODE_INVALID | MM_MODE_UNOWNED |
855 MM_MODE_SHARED)) != 0U);
856 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000857 } else {
858 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +0100859 * If the retriever is from virtual FF-A instance:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000860 * Ensure the retriever has the expected state. We don't care
861 * about the MM_MODE_SHARED bit; either with or without it set
862 * are both valid representations of the !O-NA state.
863 */
J-Alvesa9cd7e32022-07-01 13:49:33 +0100864 if (vm_id_is_current_world(to.vm->id) &&
865 to.vm->id != HF_PRIMARY_VM_ID &&
866 (orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
867 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100868 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000869 }
870 }
871
872 /* Find the appropriate new mode. */
873 *to_mode = memory_to_attributes;
874 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100875 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000876 *to_mode |= 0;
877 break;
878
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100879 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000880 *to_mode |= MM_MODE_UNOWNED;
881 break;
882
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100883 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000884 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
885 break;
886
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100887 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000888 *to_mode |= 0;
889 break;
890
891 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100892 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100893 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000894 }
895
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100896 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100897}
Jose Marinho09b1db82019-08-08 09:16:59 +0100898
899/**
900 * Updates a VM's page table such that the given set of physical address ranges
901 * are mapped in the address space at the corresponding address ranges, in the
902 * mode provided.
903 *
J-Alves0a83dc22023-05-05 09:50:37 +0100904 * The enum ffa_map_action determines the action taken from a call to the
905 * function below:
906 * - If action is MAP_ACTION_CHECK, the page tables will be allocated from the
907 * mpool but no mappings will actually be updated. This function must always
908 * be called first with action set to MAP_ACTION_CHECK to check that it will
909 * succeed before calling ffa_region_group_identity_map with whichever one of
910 * the remaining actions, to avoid leaving the page table in a half-updated
911 * state.
912 * - The action MAP_ACTION_COMMIT allocates the page tables from the mpool, and
913 * changes the memory mappings.
Jose Marinho09b1db82019-08-08 09:16:59 +0100914 *
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700915 * vm_ptable_defrag should always be called after a series of page table
916 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +0100917 *
918 * Returns true on success, or false if the update failed and no changes were
919 * made to memory mappings.
920 */
J-Alves66652252022-07-06 09:49:51 +0100921bool ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000922 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100923 struct ffa_memory_region_constituent **fragments,
924 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves0a83dc22023-05-05 09:50:37 +0100925 uint32_t mode, struct mpool *ppool, enum ffa_map_action action)
Jose Marinho09b1db82019-08-08 09:16:59 +0100926{
Andrew Walbranca808b12020-05-15 17:22:28 +0100927 uint32_t i;
928 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100929
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700930 if (vm_locked.vm->el0_partition) {
931 mode |= MM_MODE_USER | MM_MODE_NG;
932 }
933
Andrew Walbranca808b12020-05-15 17:22:28 +0100934 /* Iterate over the memory region constituents within each fragment. */
935 for (i = 0; i < fragment_count; ++i) {
936 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
937 size_t size = fragments[i][j].page_count * PAGE_SIZE;
938 paddr_t pa_begin =
939 pa_from_ipa(ipa_init(fragments[i][j].address));
940 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +0200941 uint32_t pa_bits =
942 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +0100943
944 /*
945 * Ensure the requested region falls into system's PA
946 * range.
947 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +0200948 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
949 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +0100950 dlog_error("Region is outside of PA Range\n");
951 return false;
952 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100953
J-Alves0a83dc22023-05-05 09:50:37 +0100954 switch (action) {
955 case MAP_ACTION_COMMIT:
Andrew Walbranca808b12020-05-15 17:22:28 +0100956 vm_identity_commit(vm_locked, pa_begin, pa_end,
957 mode, ppool, NULL);
J-Alves0a83dc22023-05-05 09:50:37 +0100958 break;
959 case MAP_ACTION_CHECK:
960 if (!vm_identity_prepare(vm_locked, pa_begin,
961 pa_end, mode, ppool)) {
962 return false;
963 }
964
965 break;
966 default:
967 panic("%s: invalid action state %x\n", __func__,
968 action);
Andrew Walbranca808b12020-05-15 17:22:28 +0100969 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100970 }
971 }
972
973 return true;
974}
975
976/**
977 * Clears a region of physical memory by overwriting it with zeros. The data is
978 * flushed from the cache so the memory has been cleared across the system.
979 */
J-Alves7db32002021-12-14 14:44:50 +0000980static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
981 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +0100982{
983 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000984 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100985 * global mapping of the whole range. Such an approach will limit
986 * the changes to stage-1 tables and will allow only local
987 * invalidation.
988 */
989 bool ret;
990 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +0000991 void *ptr = mm_identity_map(stage1_locked, begin, end,
992 MM_MODE_W | (extra_mode_attributes &
993 plat_ffa_other_world_mode()),
994 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100995 size_t size = pa_difference(begin, end);
996
997 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100998 goto fail;
999 }
1000
1001 memset_s(ptr, size, 0, size);
1002 arch_mm_flush_dcache(ptr, size);
1003 mm_unmap(stage1_locked, begin, end, ppool);
1004
1005 ret = true;
1006 goto out;
1007
1008fail:
1009 ret = false;
1010
1011out:
1012 mm_unlock_stage1(&stage1_locked);
1013
1014 return ret;
1015}
1016
1017/**
1018 * Clears a region of physical memory by overwriting it with zeros. The data is
1019 * flushed from the cache so the memory has been cleared across the system.
1020 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001021static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +00001022 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01001023 struct ffa_memory_region_constituent **fragments,
1024 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1025 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001026{
1027 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +01001028 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +01001029 bool ret = false;
1030
1031 /*
1032 * Create a local pool so any freed memory can't be used by another
1033 * thread. This is to ensure each constituent that is mapped can be
1034 * unmapped again afterwards.
1035 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001036 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001037
Andrew Walbranca808b12020-05-15 17:22:28 +01001038 /* Iterate over the memory region constituents within each fragment. */
1039 for (i = 0; i < fragment_count; ++i) {
1040 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001041
J-Alves8457f932023-10-11 16:41:45 +01001042 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001043 size_t size = fragments[i][j].page_count * PAGE_SIZE;
1044 paddr_t begin =
1045 pa_from_ipa(ipa_init(fragments[i][j].address));
1046 paddr_t end = pa_add(begin, size);
1047
J-Alves7db32002021-12-14 14:44:50 +00001048 if (!clear_memory(begin, end, &local_page_pool,
1049 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001050 /*
1051 * api_clear_memory will defrag on failure, so
1052 * no need to do it here.
1053 */
1054 goto out;
1055 }
Jose Marinho09b1db82019-08-08 09:16:59 +01001056 }
1057 }
1058
Jose Marinho09b1db82019-08-08 09:16:59 +01001059 ret = true;
1060
1061out:
1062 mpool_fini(&local_page_pool);
1063 return ret;
1064}
1065
J-Alves5952d942022-12-22 16:03:00 +00001066static bool is_memory_range_within(ipaddr_t begin, ipaddr_t end,
1067 ipaddr_t in_begin, ipaddr_t in_end)
1068{
1069 return (ipa_addr(begin) >= ipa_addr(in_begin) &&
1070 ipa_addr(begin) < ipa_addr(in_end)) ||
1071 (ipa_addr(end) <= ipa_addr(in_end) &&
1072 ipa_addr(end) > ipa_addr(in_begin));
1073}
1074
1075/**
1076 * Receives a memory range and looks for overlaps with the remainder
1077 * constituents of the memory share/lend/donate operation. Assumes they are
1078 * passed in order to avoid having to loop over all the elements at each call.
1079 * The function only compares the received memory ranges with those that follow
1080 * within the same fragment, and subsequent fragments from the same operation.
1081 */
1082static bool ffa_memory_check_overlap(
1083 struct ffa_memory_region_constituent **fragments,
1084 const uint32_t *fragment_constituent_counts,
1085 const uint32_t fragment_count, const uint32_t current_fragment,
1086 const uint32_t current_constituent)
1087{
1088 uint32_t i = current_fragment;
1089 uint32_t j = current_constituent;
1090 ipaddr_t current_begin = ipa_init(fragments[i][j].address);
1091 const uint32_t current_page_count = fragments[i][j].page_count;
1092 size_t current_size = current_page_count * PAGE_SIZE;
1093 ipaddr_t current_end = ipa_add(current_begin, current_size - 1);
1094
1095 if (current_size == 0 ||
1096 current_size > UINT64_MAX - ipa_addr(current_begin)) {
1097 dlog_verbose("Invalid page count. Addr: %x page_count: %x\n",
1098 current_begin, current_page_count);
1099 return false;
1100 }
1101
1102 for (; i < fragment_count; i++) {
1103 j = (i == current_fragment) ? j + 1 : 0;
1104
1105 for (; j < fragment_constituent_counts[i]; j++) {
1106 ipaddr_t begin = ipa_init(fragments[i][j].address);
1107 const uint32_t page_count = fragments[i][j].page_count;
1108 size_t size = page_count * PAGE_SIZE;
1109 ipaddr_t end = ipa_add(begin, size - 1);
1110
1111 if (size == 0 || size > UINT64_MAX - ipa_addr(begin)) {
1112 dlog_verbose(
1113 "Invalid page count. Addr: %x "
1114 "page_count: %x\n",
1115 begin, page_count);
1116 return false;
1117 }
1118
1119 /*
1120 * Check if current ranges is within begin and end, as
1121 * well as the reverse. This should help optimize the
1122 * loop, and reduce the number of iterations.
1123 */
1124 if (is_memory_range_within(begin, end, current_begin,
1125 current_end) ||
1126 is_memory_range_within(current_begin, current_end,
1127 begin, end)) {
1128 dlog_verbose(
1129 "Overlapping memory ranges: %#x - %#x "
1130 "with %#x - %#x\n",
1131 ipa_addr(begin), ipa_addr(end),
1132 ipa_addr(current_begin),
1133 ipa_addr(current_end));
1134 return true;
1135 }
1136 }
1137 }
1138
1139 return false;
1140}
1141
Jose Marinho09b1db82019-08-08 09:16:59 +01001142/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001143 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +01001144 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001145 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +01001146 *
1147 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001148 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001149 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +01001150 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001151 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
1152 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001153 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +01001154 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001155 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +01001156 */
J-Alves66652252022-07-06 09:49:51 +01001157struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001158 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001159 struct ffa_memory_region_constituent **fragments,
1160 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves8f11cde2022-12-21 16:18:22 +00001161 uint32_t composite_total_page_count, uint32_t share_func,
1162 struct ffa_memory_access *receivers, uint32_t receivers_count,
1163 struct mpool *page_pool, bool clear, uint32_t *orig_from_mode_ret)
Jose Marinho09b1db82019-08-08 09:16:59 +01001164{
Andrew Walbranca808b12020-05-15 17:22:28 +01001165 uint32_t i;
J-Alves8f11cde2022-12-21 16:18:22 +00001166 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +01001167 uint32_t orig_from_mode;
1168 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +01001169 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001170 struct ffa_value ret;
J-Alves8f11cde2022-12-21 16:18:22 +00001171 uint32_t constituents_total_page_count = 0;
Jose Marinho09b1db82019-08-08 09:16:59 +01001172
1173 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001174 * Make sure constituents are properly aligned to a 64-bit boundary. If
1175 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +01001176 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001177 for (i = 0; i < fragment_count; ++i) {
1178 if (!is_aligned(fragments[i], 8)) {
1179 dlog_verbose("Constituents not aligned.\n");
1180 return ffa_error(FFA_INVALID_PARAMETERS);
1181 }
J-Alves8f11cde2022-12-21 16:18:22 +00001182 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
1183 constituents_total_page_count +=
1184 fragments[i][j].page_count;
J-Alves5952d942022-12-22 16:03:00 +00001185 if (ffa_memory_check_overlap(
1186 fragments, fragment_constituent_counts,
1187 fragment_count, i, j)) {
1188 return ffa_error(FFA_INVALID_PARAMETERS);
1189 }
J-Alves8f11cde2022-12-21 16:18:22 +00001190 }
1191 }
1192
1193 if (constituents_total_page_count != composite_total_page_count) {
1194 dlog_verbose(
1195 "Composite page count differs from calculated page "
1196 "count from constituents.\n");
1197 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +01001198 }
1199
1200 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001201 * Check if the state transition is lawful for the sender, ensure that
1202 * all constituents of a memory region being shared are at the same
1203 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +01001204 */
J-Alves363f5722022-04-25 17:37:37 +01001205 ret = ffa_send_check_transition(from_locked, share_func, receivers,
1206 receivers_count, &orig_from_mode,
1207 fragments, fragment_constituent_counts,
Andrew Walbranca808b12020-05-15 17:22:28 +01001208 fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001209 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001210 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001211 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001212 }
1213
Andrew Walbran37c574e2020-06-03 11:45:46 +01001214 if (orig_from_mode_ret != NULL) {
1215 *orig_from_mode_ret = orig_from_mode;
1216 }
1217
Jose Marinho09b1db82019-08-08 09:16:59 +01001218 /*
1219 * Create a local pool so any freed memory can't be used by another
1220 * thread. This is to ensure the original mapping can be restored if the
1221 * clear fails.
1222 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001223 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001224
1225 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001226 * First reserve all required memory for the new page table entries
1227 * without committing, to make sure the entire operation will succeed
1228 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +01001229 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001230 if (!ffa_region_group_identity_map(
1231 from_locked, fragments, fragment_constituent_counts,
J-Alves0a83dc22023-05-05 09:50:37 +01001232 fragment_count, from_mode, page_pool, MAP_ACTION_CHECK)) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001233 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001234 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +01001235 goto out;
1236 }
1237
1238 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001239 * Update the mapping for the sender. This won't allocate because the
1240 * transaction was already prepared above, but may free pages in the
1241 * case that a whole block is being unmapped that was previously
1242 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +01001243 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001244 CHECK(ffa_region_group_identity_map(
1245 from_locked, fragments, fragment_constituent_counts,
J-Alves0a83dc22023-05-05 09:50:37 +01001246 fragment_count, from_mode, &local_page_pool,
1247 MAP_ACTION_COMMIT));
Jose Marinho09b1db82019-08-08 09:16:59 +01001248
1249 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001250 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001251 !ffa_clear_memory_constituents(orig_from_mode, fragments,
1252 fragment_constituent_counts,
1253 fragment_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001254 /*
1255 * On failure, roll back by returning memory to the sender. This
1256 * may allocate pages which were previously freed into
1257 * `local_page_pool` by the call above, but will never allocate
1258 * more pages than that so can never fail.
1259 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001260 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001261 from_locked, fragments, fragment_constituent_counts,
1262 fragment_count, orig_from_mode, &local_page_pool,
J-Alves0a83dc22023-05-05 09:50:37 +01001263 MAP_ACTION_COMMIT));
Jose Marinho09b1db82019-08-08 09:16:59 +01001264
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001265 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +01001266 goto out;
1267 }
1268
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001269 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001270
1271out:
1272 mpool_fini(&local_page_pool);
1273
1274 /*
1275 * Tidy up the page table by reclaiming failed mappings (if there was an
1276 * error) or merging entries into blocks where possible (on success).
1277 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001278 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001279
1280 return ret;
1281}
1282
1283/**
1284 * Validates and maps memory shared from one VM to another.
1285 *
1286 * This function requires the calling context to hold the <to> lock.
1287 *
1288 * Returns:
1289 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001290 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001291 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001292 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001293 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001294 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001295 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001296struct ffa_value ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01001297 struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001298 struct ffa_memory_region_constituent **fragments,
1299 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves26483382023-04-20 12:01:49 +01001300 uint32_t sender_orig_mode, uint32_t share_func, bool clear,
Andrew Walbranca808b12020-05-15 17:22:28 +01001301 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001302{
Andrew Walbranca808b12020-05-15 17:22:28 +01001303 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001304 uint32_t to_mode;
1305 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001306 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001307
1308 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001309 * Make sure constituents are properly aligned to a 64-bit boundary. If
1310 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001311 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001312 for (i = 0; i < fragment_count; ++i) {
1313 if (!is_aligned(fragments[i], 8)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001314 dlog_verbose("Fragment not properly aligned.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +01001315 return ffa_error(FFA_INVALID_PARAMETERS);
1316 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001317 }
1318
1319 /*
1320 * Check if the state transition is lawful for the recipient, and ensure
1321 * that all constituents of the memory region being retrieved are at the
1322 * same state.
1323 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001324 ret = ffa_retrieve_check_transition(
1325 to_locked, share_func, fragments, fragment_constituent_counts,
J-Alves26483382023-04-20 12:01:49 +01001326 fragment_count, sender_orig_mode, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001327 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001328 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001329 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001330 }
1331
1332 /*
1333 * Create a local pool so any freed memory can't be used by another
1334 * thread. This is to ensure the original mapping can be restored if the
1335 * clear fails.
1336 */
1337 mpool_init_with_fallback(&local_page_pool, page_pool);
1338
1339 /*
1340 * First reserve all required memory for the new page table entries in
1341 * the recipient page tables without committing, to make sure the entire
1342 * operation will succeed without exhausting the page pool.
1343 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001344 if (!ffa_region_group_identity_map(
1345 to_locked, fragments, fragment_constituent_counts,
J-Alves0a83dc22023-05-05 09:50:37 +01001346 fragment_count, to_mode, page_pool, MAP_ACTION_CHECK)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001347 /* TODO: partial defrag of failed range. */
1348 dlog_verbose(
1349 "Insufficient memory to update recipient page "
1350 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001351 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001352 goto out;
1353 }
1354
1355 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001356 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001357 !ffa_clear_memory_constituents(sender_orig_mode, fragments,
1358 fragment_constituent_counts,
1359 fragment_count, page_pool)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001360 dlog_verbose("Couldn't clear constituents.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001361 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001362 goto out;
1363 }
1364
Jose Marinho09b1db82019-08-08 09:16:59 +01001365 /*
1366 * Complete the transfer by mapping the memory into the recipient. This
1367 * won't allocate because the transaction was already prepared above, so
1368 * it doesn't need to use the `local_page_pool`.
1369 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001370 CHECK(ffa_region_group_identity_map(
1371 to_locked, fragments, fragment_constituent_counts,
J-Alves0a83dc22023-05-05 09:50:37 +01001372 fragment_count, to_mode, page_pool, MAP_ACTION_COMMIT));
Jose Marinho09b1db82019-08-08 09:16:59 +01001373
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001374 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001375
1376out:
1377 mpool_fini(&local_page_pool);
1378
1379 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001380 * Tidy up the page table by reclaiming failed mappings (if there was an
1381 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001382 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001383 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001384
1385 return ret;
1386}
1387
Andrew Walbran996d1d12020-05-27 14:08:43 +01001388static struct ffa_value ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01001389 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001390 struct ffa_memory_region_constituent **fragments,
1391 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1392 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001393{
1394 uint32_t orig_from_mode;
1395 uint32_t from_mode;
1396 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001397 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001398
Andrew Walbranca808b12020-05-15 17:22:28 +01001399 ret = ffa_relinquish_check_transition(
1400 from_locked, &orig_from_mode, fragments,
1401 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001402 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001403 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001404 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001405 }
1406
1407 /*
1408 * Create a local pool so any freed memory can't be used by another
1409 * thread. This is to ensure the original mapping can be restored if the
1410 * clear fails.
1411 */
1412 mpool_init_with_fallback(&local_page_pool, page_pool);
1413
1414 /*
1415 * First reserve all required memory for the new page table entries
1416 * without committing, to make sure the entire operation will succeed
1417 * without exhausting the page pool.
1418 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001419 if (!ffa_region_group_identity_map(
1420 from_locked, fragments, fragment_constituent_counts,
J-Alves0a83dc22023-05-05 09:50:37 +01001421 fragment_count, from_mode, page_pool, MAP_ACTION_CHECK)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001422 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001423 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001424 goto out;
1425 }
1426
1427 /*
1428 * Update the mapping for the sender. This won't allocate because the
1429 * transaction was already prepared above, but may free pages in the
1430 * case that a whole block is being unmapped that was previously
1431 * partially mapped.
1432 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001433 CHECK(ffa_region_group_identity_map(
1434 from_locked, fragments, fragment_constituent_counts,
J-Alves0a83dc22023-05-05 09:50:37 +01001435 fragment_count, from_mode, &local_page_pool,
1436 MAP_ACTION_COMMIT));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001437
1438 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001439 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001440 !ffa_clear_memory_constituents(orig_from_mode, fragments,
1441 fragment_constituent_counts,
1442 fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001443 /*
1444 * On failure, roll back by returning memory to the sender. This
1445 * may allocate pages which were previously freed into
1446 * `local_page_pool` by the call above, but will never allocate
1447 * more pages than that so can never fail.
1448 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001449 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001450 from_locked, fragments, fragment_constituent_counts,
1451 fragment_count, orig_from_mode, &local_page_pool,
J-Alves0a83dc22023-05-05 09:50:37 +01001452 MAP_ACTION_COMMIT));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001453
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001454 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001455 goto out;
1456 }
1457
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001458 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001459
1460out:
1461 mpool_fini(&local_page_pool);
1462
1463 /*
1464 * Tidy up the page table by reclaiming failed mappings (if there was an
1465 * error) or merging entries into blocks where possible (on success).
1466 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001467 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001468
1469 return ret;
1470}
1471
1472/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001473 * Complete a memory sending operation by checking that it is valid, updating
1474 * the sender page table, and then either marking the share state as having
1475 * completed sending (on success) or freeing it (on failure).
1476 *
1477 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1478 */
J-Alvesfdd29272022-07-19 13:16:31 +01001479struct ffa_value ffa_memory_send_complete(
Andrew Walbranca808b12020-05-15 17:22:28 +01001480 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001481 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1482 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001483{
1484 struct ffa_memory_region *memory_region = share_state->memory_region;
J-Alves8f11cde2022-12-21 16:18:22 +00001485 struct ffa_composite_memory_region *composite;
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001486 struct ffa_memory_access *receiver;
Andrew Walbranca808b12020-05-15 17:22:28 +01001487 struct ffa_value ret;
1488
1489 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001490 assert(share_states.share_states != NULL);
J-Alves8f11cde2022-12-21 16:18:22 +00001491 assert(memory_region != NULL);
1492 composite = ffa_memory_region_get_composite(memory_region, 0);
1493 assert(composite != NULL);
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001494 receiver = ffa_memory_region_get_receiver(memory_region, 0);
1495 assert(receiver != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001496
1497 /* Check that state is valid in sender page table and update. */
1498 ret = ffa_send_check_update(
1499 from_locked, share_state->fragments,
1500 share_state->fragment_constituent_counts,
J-Alves8f11cde2022-12-21 16:18:22 +00001501 share_state->fragment_count, composite->page_count,
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001502 share_state->share_func, receiver,
J-Alves8f11cde2022-12-21 16:18:22 +00001503 memory_region->receiver_count, page_pool,
1504 memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001505 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001506 if (ret.func != FFA_SUCCESS_32) {
1507 /*
1508 * Free share state, it failed to send so it can't be retrieved.
1509 */
Karl Meakin4cec5e82023-06-30 16:30:22 +01001510 dlog_verbose("%s: failed to send check update: %s(%s)\n",
1511 __func__, ffa_func_name(ret.func),
1512 ffa_error_name(ffa_error_code(ret)));
Andrew Walbranca808b12020-05-15 17:22:28 +01001513 share_state_free(share_states, share_state, page_pool);
1514 return ret;
1515 }
1516
1517 share_state->sending_complete = true;
Karl Meakin4cec5e82023-06-30 16:30:22 +01001518 dlog_verbose("%s: marked sending complete.\n", __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001519
J-Alvesee68c542020-10-29 17:48:20 +00001520 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001521}
1522
1523/**
Federico Recanatia98603a2021-12-20 18:04:03 +01001524 * Check that the memory attributes match Hafnium expectations:
1525 * Normal Memory, Inner shareable, Write-Back Read-Allocate
1526 * Write-Allocate Cacheable.
1527 */
1528static struct ffa_value ffa_memory_attributes_validate(
J-Alves7a99d0d2023-02-08 13:49:48 +00001529 ffa_memory_attributes_t attributes)
Federico Recanatia98603a2021-12-20 18:04:03 +01001530{
1531 enum ffa_memory_type memory_type;
1532 enum ffa_memory_cacheability cacheability;
1533 enum ffa_memory_shareability shareability;
1534
1535 memory_type = ffa_get_memory_type_attr(attributes);
1536 if (memory_type != FFA_MEMORY_NORMAL_MEM) {
1537 dlog_verbose("Invalid memory type %#x, expected %#x.\n",
1538 memory_type, FFA_MEMORY_NORMAL_MEM);
Federico Recanati3d953f32022-02-17 09:31:29 +01001539 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001540 }
1541
1542 cacheability = ffa_get_memory_cacheability_attr(attributes);
1543 if (cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1544 dlog_verbose("Invalid cacheability %#x, expected %#x.\n",
1545 cacheability, FFA_MEMORY_CACHE_WRITE_BACK);
Federico Recanati3d953f32022-02-17 09:31:29 +01001546 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001547 }
1548
1549 shareability = ffa_get_memory_shareability_attr(attributes);
1550 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
1551 dlog_verbose("Invalid shareability %#x, expected #%x.\n",
1552 shareability, FFA_MEMORY_INNER_SHAREABLE);
Federico Recanati3d953f32022-02-17 09:31:29 +01001553 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001554 }
1555
1556 return (struct ffa_value){.func = FFA_SUCCESS_32};
1557}
1558
1559/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001560 * Check that the given `memory_region` represents a valid memory send request
1561 * of the given `share_func` type, return the clear flag and permissions via the
1562 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001563 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001564 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001565 * not.
1566 */
J-Alves66652252022-07-06 09:49:51 +01001567struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001568 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1569 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001570 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001571{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001572 struct ffa_composite_memory_region *composite;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001573 struct ffa_memory_access *receiver =
1574 ffa_memory_region_get_receiver(memory_region, 0);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001575 uint64_t receivers_end;
1576 uint64_t min_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001577 uint32_t composite_memory_region_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001578 uint32_t constituents_start;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001579 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001580 enum ffa_data_access data_access;
1581 enum ffa_instruction_access instruction_access;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001582 enum ffa_memory_security security_state;
Federico Recanatia98603a2021-12-20 18:04:03 +01001583 struct ffa_value ret;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001584 const size_t minimum_first_fragment_length =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001585 memory_region->receivers_offset +
1586 memory_region->memory_access_desc_size +
1587 sizeof(struct ffa_composite_memory_region);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001588
1589 if (fragment_length < minimum_first_fragment_length) {
1590 dlog_verbose("Fragment length %u too short (min %u).\n",
1591 (size_t)fragment_length,
1592 minimum_first_fragment_length);
1593 return ffa_error(FFA_INVALID_PARAMETERS);
1594 }
1595
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05001596 static_assert(sizeof(struct ffa_memory_region_constituent) == 16,
1597 "struct ffa_memory_region_constituent must be 16 bytes");
1598 if (!is_aligned(fragment_length,
1599 sizeof(struct ffa_memory_region_constituent)) ||
1600 !is_aligned(memory_share_length,
1601 sizeof(struct ffa_memory_region_constituent))) {
1602 dlog_verbose(
1603 "Fragment length %u or total length %u"
1604 " is not 16-byte aligned.\n",
1605 fragment_length, memory_share_length);
1606 return ffa_error(FFA_INVALID_PARAMETERS);
1607 }
1608
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001609 if (fragment_length > memory_share_length) {
1610 dlog_verbose(
1611 "Fragment length %u greater than total length %u.\n",
1612 (size_t)fragment_length, (size_t)memory_share_length);
1613 return ffa_error(FFA_INVALID_PARAMETERS);
1614 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001615
J-Alves95df0ef2022-12-07 10:09:48 +00001616 /* The sender must match the caller. */
1617 if ((!vm_id_is_current_world(from_locked.vm->id) &&
1618 vm_id_is_current_world(memory_region->sender)) ||
1619 (vm_id_is_current_world(from_locked.vm->id) &&
1620 memory_region->sender != from_locked.vm->id)) {
1621 dlog_verbose("Invalid memory sender ID.\n");
1622 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001623 }
1624
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001625 if (memory_region->receiver_count <= 0) {
1626 dlog_verbose("No receivers!\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001627 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001628 }
1629
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001630 /*
1631 * Ensure that the composite header is within the memory bounds and
1632 * doesn't overlap the first part of the message. Cast to uint64_t
1633 * to prevent overflow.
1634 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001635 receivers_end = ((uint64_t)memory_region->memory_access_desc_size *
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001636 (uint64_t)memory_region->receiver_count) +
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001637 memory_region->receivers_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001638 min_length = receivers_end +
1639 sizeof(struct ffa_composite_memory_region) +
1640 sizeof(struct ffa_memory_region_constituent);
1641 if (min_length > memory_share_length) {
1642 dlog_verbose("Share too short: got %u but minimum is %u.\n",
1643 (size_t)memory_share_length, (size_t)min_length);
1644 return ffa_error(FFA_INVALID_PARAMETERS);
1645 }
1646
1647 composite_memory_region_offset =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001648 receiver->composite_memory_region_offset;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001649
1650 /*
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001651 * Check that the composite memory region descriptor is after the access
1652 * descriptors, is at least 16-byte aligned, and fits in the first
1653 * fragment.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001654 */
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001655 if ((composite_memory_region_offset < receivers_end) ||
1656 (composite_memory_region_offset % 16 != 0) ||
1657 (composite_memory_region_offset >
1658 fragment_length - sizeof(struct ffa_composite_memory_region))) {
1659 dlog_verbose(
1660 "Invalid composite memory region descriptor offset "
1661 "%u.\n",
1662 (size_t)composite_memory_region_offset);
1663 return ffa_error(FFA_INVALID_PARAMETERS);
1664 }
1665
1666 /*
1667 * Compute the start of the constituent regions. Already checked
1668 * to be not more than fragment_length and thus not more than
1669 * memory_share_length.
1670 */
1671 constituents_start = composite_memory_region_offset +
1672 sizeof(struct ffa_composite_memory_region);
1673 constituents_length = memory_share_length - constituents_start;
1674
1675 /*
1676 * Check that the number of constituents is consistent with the length
1677 * of the constituent region.
1678 */
1679 composite = ffa_memory_region_get_composite(memory_region, 0);
1680 if ((constituents_length %
1681 sizeof(struct ffa_memory_region_constituent) !=
1682 0) ||
1683 ((constituents_length /
1684 sizeof(struct ffa_memory_region_constituent)) !=
1685 composite->constituent_count)) {
1686 dlog_verbose("Invalid length %u or composite offset %u.\n",
1687 (size_t)memory_share_length,
1688 (size_t)composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001689 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001690 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001691 if (fragment_length < memory_share_length &&
1692 fragment_length < HF_MAILBOX_SIZE) {
1693 dlog_warning(
1694 "Initial fragment length %d smaller than mailbox "
1695 "size.\n",
1696 fragment_length);
1697 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001698
Andrew Walbrana65a1322020-04-06 19:32:32 +01001699 /*
1700 * Clear is not allowed for memory sharing, as the sender still has
1701 * access to the memory.
1702 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001703 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1704 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001705 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001706 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001707 }
1708
1709 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001710 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001711 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001712 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001713 }
1714
J-Alves363f5722022-04-25 17:37:37 +01001715 /* Check that the permissions are valid, for each specified receiver. */
1716 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001717 struct ffa_memory_region_attributes receiver_permissions;
1718
1719 receiver = ffa_memory_region_get_receiver(memory_region, i);
1720 assert(receiver != NULL);
1721 receiver_permissions = receiver->receiver_permissions;
J-Alves363f5722022-04-25 17:37:37 +01001722 ffa_memory_access_permissions_t permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001723 receiver_permissions.permissions;
1724 ffa_id_t receiver_id = receiver_permissions.receiver;
J-Alves363f5722022-04-25 17:37:37 +01001725
1726 if (memory_region->sender == receiver_id) {
1727 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001728 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001729 }
Federico Recanati85090c42021-12-15 13:17:54 +01001730
J-Alves363f5722022-04-25 17:37:37 +01001731 for (uint32_t j = i + 1; j < memory_region->receiver_count;
1732 j++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001733 struct ffa_memory_access *other_receiver =
1734 ffa_memory_region_get_receiver(memory_region,
1735 j);
1736 assert(other_receiver != NULL);
1737
J-Alves363f5722022-04-25 17:37:37 +01001738 if (receiver_id ==
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001739 other_receiver->receiver_permissions.receiver) {
J-Alves363f5722022-04-25 17:37:37 +01001740 dlog_verbose(
1741 "Repeated receiver(%x) in memory send "
1742 "operation.\n",
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001743 other_receiver->receiver_permissions
1744 .receiver);
J-Alves363f5722022-04-25 17:37:37 +01001745 return ffa_error(FFA_INVALID_PARAMETERS);
1746 }
1747 }
1748
1749 if (composite_memory_region_offset !=
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001750 receiver->composite_memory_region_offset) {
J-Alves363f5722022-04-25 17:37:37 +01001751 dlog_verbose(
1752 "All ffa_memory_access should point to the "
1753 "same composite memory region offset.\n");
1754 return ffa_error(FFA_INVALID_PARAMETERS);
1755 }
1756
1757 data_access = ffa_get_data_access_attr(permissions);
1758 instruction_access =
1759 ffa_get_instruction_access_attr(permissions);
1760 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1761 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
1762 dlog_verbose(
1763 "Reserved value for receiver permissions "
1764 "%#x.\n",
1765 permissions);
1766 return ffa_error(FFA_INVALID_PARAMETERS);
1767 }
1768 if (instruction_access !=
1769 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
1770 dlog_verbose(
1771 "Invalid instruction access permissions %#x "
1772 "for sending memory.\n",
1773 permissions);
1774 return ffa_error(FFA_INVALID_PARAMETERS);
1775 }
1776 if (share_func == FFA_MEM_SHARE_32) {
1777 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1778 dlog_verbose(
1779 "Invalid data access permissions %#x "
1780 "for sharing memory.\n",
1781 permissions);
1782 return ffa_error(FFA_INVALID_PARAMETERS);
1783 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001784 /*
1785 * According to section 10.10.3 of the FF-A v1.1 EAC0
1786 * spec, NX is required for share operations (but must
1787 * not be specified by the sender) so set it in the
1788 * copy that we store, ready to be returned to the
1789 * retriever.
1790 */
1791 if (vm_id_is_current_world(receiver_id)) {
1792 ffa_set_instruction_access_attr(
1793 &permissions,
1794 FFA_INSTRUCTION_ACCESS_NX);
1795 receiver_permissions.permissions = permissions;
1796 }
J-Alves363f5722022-04-25 17:37:37 +01001797 }
1798 if (share_func == FFA_MEM_LEND_32 &&
1799 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1800 dlog_verbose(
1801 "Invalid data access permissions %#x for "
1802 "lending memory.\n",
1803 permissions);
1804 return ffa_error(FFA_INVALID_PARAMETERS);
1805 }
1806
1807 if (share_func == FFA_MEM_DONATE_32 &&
1808 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
1809 dlog_verbose(
1810 "Invalid data access permissions %#x for "
1811 "donating memory.\n",
1812 permissions);
1813 return ffa_error(FFA_INVALID_PARAMETERS);
1814 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001815 }
1816
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001817 /* Memory region attributes NS-Bit MBZ for FFA_MEM_SHARE/LEND/DONATE. */
1818 security_state =
1819 ffa_get_memory_security_attr(memory_region->attributes);
1820 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
1821 dlog_verbose(
1822 "Invalid security state for memory share operation.\n");
1823 return ffa_error(FFA_INVALID_PARAMETERS);
1824 }
1825
Federico Recanatid937f5e2021-12-20 17:38:23 +01001826 /*
J-Alves807794e2022-06-16 13:42:47 +01001827 * If a memory donate or lend with single borrower, the memory type
1828 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01001829 */
J-Alves807794e2022-06-16 13:42:47 +01001830 if (share_func == FFA_MEM_DONATE_32 ||
1831 (share_func == FFA_MEM_LEND_32 &&
1832 memory_region->receiver_count == 1)) {
1833 if (ffa_get_memory_type_attr(memory_region->attributes) !=
1834 FFA_MEMORY_NOT_SPECIFIED_MEM) {
1835 dlog_verbose(
1836 "Memory type shall not be specified by "
1837 "sender.\n");
1838 return ffa_error(FFA_INVALID_PARAMETERS);
1839 }
1840 } else {
1841 /*
1842 * Check that sender's memory attributes match Hafnium
1843 * expectations: Normal Memory, Inner shareable, Write-Back
1844 * Read-Allocate Write-Allocate Cacheable.
1845 */
1846 ret = ffa_memory_attributes_validate(memory_region->attributes);
1847 if (ret.func != FFA_SUCCESS_32) {
1848 return ret;
1849 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01001850 }
1851
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001852 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001853}
1854
1855/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001856 * Gets the share state for continuing an operation to donate, lend or share
1857 * memory, and checks that it is a valid request.
1858 *
1859 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1860 * not.
1861 */
J-Alvesfdd29272022-07-19 13:16:31 +01001862struct ffa_value ffa_memory_send_continue_validate(
Andrew Walbranca808b12020-05-15 17:22:28 +01001863 struct share_states_locked share_states, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001864 struct ffa_memory_share_state **share_state_ret, ffa_id_t from_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01001865 struct mpool *page_pool)
1866{
1867 struct ffa_memory_share_state *share_state;
1868 struct ffa_memory_region *memory_region;
1869
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001870 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001871
1872 /*
1873 * Look up the share state by handle and make sure that the VM ID
1874 * matches.
1875 */
Karl Meakin4a2854a2023-06-30 16:26:52 +01001876 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00001877 if (share_state == NULL) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001878 dlog_verbose(
1879 "Invalid handle %#x for memory send continuation.\n",
1880 handle);
1881 return ffa_error(FFA_INVALID_PARAMETERS);
1882 }
1883 memory_region = share_state->memory_region;
1884
J-Alvesfdd29272022-07-19 13:16:31 +01001885 if (vm_id_is_current_world(from_vm_id) &&
1886 memory_region->sender != from_vm_id) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001887 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1888 return ffa_error(FFA_INVALID_PARAMETERS);
1889 }
1890
1891 if (share_state->sending_complete) {
1892 dlog_verbose(
1893 "Sending of memory handle %#x is already complete.\n",
1894 handle);
1895 return ffa_error(FFA_INVALID_PARAMETERS);
1896 }
1897
1898 if (share_state->fragment_count == MAX_FRAGMENTS) {
1899 /*
1900 * Log a warning as this is a sign that MAX_FRAGMENTS should
1901 * probably be increased.
1902 */
1903 dlog_warning(
1904 "Too many fragments for memory share with handle %#x; "
1905 "only %d supported.\n",
1906 handle, MAX_FRAGMENTS);
1907 /* Free share state, as it's not possible to complete it. */
1908 share_state_free(share_states, share_state, page_pool);
1909 return ffa_error(FFA_NO_MEMORY);
1910 }
1911
1912 *share_state_ret = share_state;
1913
1914 return (struct ffa_value){.func = FFA_SUCCESS_32};
1915}
1916
1917/**
J-Alves95df0ef2022-12-07 10:09:48 +00001918 * Checks if there is at least one receiver from the other world.
1919 */
J-Alvesfdd29272022-07-19 13:16:31 +01001920bool memory_region_receivers_from_other_world(
J-Alves95df0ef2022-12-07 10:09:48 +00001921 struct ffa_memory_region *memory_region)
1922{
1923 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001924 struct ffa_memory_access *receiver =
1925 ffa_memory_region_get_receiver(memory_region, i);
1926 assert(receiver != NULL);
1927 ffa_id_t receiver_id = receiver->receiver_permissions.receiver;
1928
1929 if (!vm_id_is_current_world(receiver_id)) {
J-Alves95df0ef2022-12-07 10:09:48 +00001930 return true;
1931 }
1932 }
1933 return false;
1934}
1935
1936/**
J-Alves9da280b2022-12-21 14:55:39 +00001937 * Validates a call to donate, lend or share memory in which Hafnium is the
1938 * designated allocator of the memory handle. In practice, this also means
1939 * Hafnium is responsible for managing the state structures for the transaction.
1940 * If Hafnium is the SPMC, it should allocate the memory handle when either the
1941 * sender is an SP or there is at least one borrower that is an SP.
1942 * If Hafnium is the hypervisor, it should allocate the memory handle when
1943 * operation involves only NWd VMs.
1944 *
1945 * If validation goes well, Hafnium updates the stage-2 page tables of the
1946 * sender. Validation consists of checking if the message length and number of
1947 * memory region constituents match, and if the transition is valid for the
1948 * type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001949 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001950 * Assumes that the caller has already found and locked the sender VM and copied
1951 * the memory region descriptor from the sender's TX buffer to a freshly
1952 * allocated page from Hafnium's internal pool. The caller must have also
1953 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001954 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001955 * This function takes ownership of the `memory_region` passed in and will free
1956 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001957 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001958struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001959 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001960 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001961 uint32_t fragment_length, uint32_t share_func,
1962 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001963{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001964 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001965 struct share_states_locked share_states;
1966 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001967
1968 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001969 * If there is an error validating the `memory_region` then we need to
1970 * free it because we own it but we won't be storing it in a share state
1971 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001972 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001973 ret = ffa_memory_send_validate(from_locked, memory_region,
1974 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001975 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001976 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001977 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001978 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001979 }
1980
Andrew Walbrana65a1322020-04-06 19:32:32 +01001981 /* Set flag for share function, ready to be retrieved later. */
1982 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001983 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001984 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001985 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001986 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001987 case FFA_MEM_LEND_32:
1988 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001989 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001990 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001991 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001992 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001993 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001994 }
1995
Andrew Walbranca808b12020-05-15 17:22:28 +01001996 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001997 /*
1998 * Allocate a share state before updating the page table. Otherwise if
1999 * updating the page table succeeded but allocating the share state
2000 * failed then it would leave the memory in a state where nobody could
2001 * get it back.
2002 */
Karl Meakin52cdfe72023-06-30 14:49:10 +01002003 share_state = allocate_share_state(share_states, share_func,
2004 memory_region, fragment_length,
2005 FFA_MEMORY_HANDLE_INVALID);
J-Alvesb56aac82023-11-10 09:44:43 +00002006 if (share_state == NULL) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002007 dlog_verbose("Failed to allocate share state.\n");
2008 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01002009 ret = ffa_error(FFA_NO_MEMORY);
2010 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002011 }
2012
Andrew Walbranca808b12020-05-15 17:22:28 +01002013 if (fragment_length == memory_share_length) {
2014 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00002015 ret = ffa_memory_send_complete(
2016 from_locked, share_states, share_state, page_pool,
2017 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002018 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01002019 /*
2020 * Use sender ID from 'memory_region' assuming
2021 * that at this point it has been validated:
2022 * - MBZ at virtual FF-A instance.
2023 */
J-Alves19e20cf2023-08-02 12:48:55 +01002024 ffa_id_t sender_to_ret =
J-Alvesfdd29272022-07-19 13:16:31 +01002025 (from_locked.vm->id == HF_OTHER_WORLD_ID)
2026 ? memory_region->sender
2027 : 0;
Andrew Walbranca808b12020-05-15 17:22:28 +01002028 ret = (struct ffa_value){
2029 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00002030 .arg1 = (uint32_t)memory_region->handle,
2031 .arg2 = (uint32_t)(memory_region->handle >> 32),
J-Alvesfdd29272022-07-19 13:16:31 +01002032 .arg3 = fragment_length,
2033 .arg4 = (uint32_t)(sender_to_ret & 0xffff) << 16};
Andrew Walbranca808b12020-05-15 17:22:28 +01002034 }
2035
2036out:
2037 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002038 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01002039 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002040}
2041
2042/**
J-Alves8505a8a2022-06-15 18:10:18 +01002043 * Continues an operation to donate, lend or share memory to a VM from current
2044 * world. If this is the last fragment then checks that the transition is valid
2045 * for the type of memory sending operation and updates the stage-2 page tables
2046 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01002047 *
2048 * Assumes that the caller has already found and locked the sender VM and copied
2049 * the memory region descriptor from the sender's TX buffer to a freshly
2050 * allocated page from Hafnium's internal pool.
2051 *
2052 * This function takes ownership of the `fragment` passed in; it must not be
2053 * freed by the caller.
2054 */
2055struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
2056 void *fragment,
2057 uint32_t fragment_length,
2058 ffa_memory_handle_t handle,
2059 struct mpool *page_pool)
2060{
2061 struct share_states_locked share_states = share_states_lock();
2062 struct ffa_memory_share_state *share_state;
2063 struct ffa_value ret;
2064 struct ffa_memory_region *memory_region;
2065
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05002066 CHECK(is_aligned(fragment,
2067 alignof(struct ffa_memory_region_constituent)));
2068 if (fragment_length % sizeof(struct ffa_memory_region_constituent) !=
2069 0) {
2070 dlog_verbose("Fragment length %u misaligned.\n",
2071 fragment_length);
2072 ret = ffa_error(FFA_INVALID_PARAMETERS);
2073 goto out_free_fragment;
2074 }
2075
Andrew Walbranca808b12020-05-15 17:22:28 +01002076 ret = ffa_memory_send_continue_validate(share_states, handle,
2077 &share_state,
2078 from_locked.vm->id, page_pool);
2079 if (ret.func != FFA_SUCCESS_32) {
2080 goto out_free_fragment;
2081 }
2082 memory_region = share_state->memory_region;
2083
J-Alves95df0ef2022-12-07 10:09:48 +00002084 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002085 dlog_error(
2086 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01002087 "other world. This should never happen, and indicates "
2088 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01002089 "EL3 code.\n");
2090 ret = ffa_error(FFA_INVALID_PARAMETERS);
2091 goto out_free_fragment;
2092 }
2093
2094 /* Add this fragment. */
2095 share_state->fragments[share_state->fragment_count] = fragment;
2096 share_state->fragment_constituent_counts[share_state->fragment_count] =
2097 fragment_length / sizeof(struct ffa_memory_region_constituent);
2098 share_state->fragment_count++;
2099
2100 /* Check whether the memory send operation is now ready to complete. */
2101 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00002102 ret = ffa_memory_send_complete(
2103 from_locked, share_states, share_state, page_pool,
2104 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002105 } else {
2106 ret = (struct ffa_value){
2107 .func = FFA_MEM_FRAG_RX_32,
2108 .arg1 = (uint32_t)handle,
2109 .arg2 = (uint32_t)(handle >> 32),
2110 .arg3 = share_state_next_fragment_offset(share_states,
2111 share_state)};
2112 }
2113 goto out;
2114
2115out_free_fragment:
2116 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002117
2118out:
Andrew Walbranca808b12020-05-15 17:22:28 +01002119 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002120 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002121}
2122
Andrew Walbranca808b12020-05-15 17:22:28 +01002123/** Clean up after the receiver has finished retrieving a memory region. */
2124static void ffa_memory_retrieve_complete(
2125 struct share_states_locked share_states,
2126 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2127{
2128 if (share_state->share_func == FFA_MEM_DONATE_32) {
2129 /*
2130 * Memory that has been donated can't be relinquished,
2131 * so no need to keep the share state around.
2132 */
2133 share_state_free(share_states, share_state, page_pool);
2134 dlog_verbose("Freed share state for donate.\n");
2135 }
2136}
2137
J-Alves2d8457f2022-10-05 11:06:41 +01002138/**
2139 * Initialises the given memory region descriptor to be used for an
2140 * `FFA_MEM_RETRIEVE_RESP`, including the given constituents for the first
2141 * fragment.
2142 * The memory region descriptor is initialized according to retriever's
2143 * FF-A version.
2144 *
2145 * Returns true on success, or false if the given constituents won't all fit in
2146 * the first fragment.
2147 */
2148static bool ffa_retrieved_memory_region_init(
2149 void *response, uint32_t ffa_version, size_t response_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01002150 ffa_id_t sender, ffa_memory_attributes_t attributes,
J-Alves2d8457f2022-10-05 11:06:41 +01002151 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002152 ffa_memory_access_permissions_t permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002153 struct ffa_memory_access *receivers, size_t receiver_count,
2154 uint32_t memory_access_desc_size, uint32_t page_count,
2155 uint32_t total_constituent_count,
J-Alves2d8457f2022-10-05 11:06:41 +01002156 const struct ffa_memory_region_constituent constituents[],
2157 uint32_t fragment_constituent_count, uint32_t *total_length,
2158 uint32_t *fragment_length)
2159{
2160 struct ffa_composite_memory_region *composite_memory_region;
J-Alves2d8457f2022-10-05 11:06:41 +01002161 uint32_t i;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002162 uint32_t composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002163 uint32_t constituents_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002164
2165 assert(response != NULL);
2166
2167 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
2168 struct ffa_memory_region_v1_0 *retrieve_response =
2169 (struct ffa_memory_region_v1_0 *)response;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002170 struct ffa_memory_access_v1_0 *receiver;
J-Alves2d8457f2022-10-05 11:06:41 +01002171
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002172 ffa_memory_region_init_header_v1_0(retrieve_response, sender,
2173 attributes, flags, handle, 0,
2174 receiver_count);
J-Alves2d8457f2022-10-05 11:06:41 +01002175
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002176 receiver = (struct ffa_memory_access_v1_0 *)
2177 retrieve_response->receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002178 receiver_count = retrieve_response->receiver_count;
2179
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002180 for (uint32_t i = 0; i < receiver_count; i++) {
2181 ffa_id_t receiver_id =
2182 receivers[i].receiver_permissions.receiver;
2183 ffa_memory_receiver_flags_t recv_flags =
2184 receivers[i].receiver_permissions.flags;
2185
2186 /*
2187 * Initialized here as in memory retrieve responses we
2188 * currently expect one borrower to be specified.
2189 */
2190 ffa_memory_access_init_v1_0(
2191 receiver, receiver_id,
2192 ffa_get_data_access_attr(permissions),
2193 ffa_get_instruction_access_attr(permissions),
2194 recv_flags);
2195 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002196
2197 composite_offset =
J-Alves2d8457f2022-10-05 11:06:41 +01002198 sizeof(struct ffa_memory_region_v1_0) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002199 receiver_count * sizeof(struct ffa_memory_access_v1_0);
2200 receiver->composite_memory_region_offset = composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01002201
2202 composite_memory_region = ffa_memory_region_get_composite_v1_0(
2203 retrieve_response, 0);
2204 } else {
J-Alves2d8457f2022-10-05 11:06:41 +01002205 struct ffa_memory_region *retrieve_response =
2206 (struct ffa_memory_region *)response;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002207 struct ffa_memory_access *retrieve_response_receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01002208
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002209 ffa_memory_region_init_header(
2210 retrieve_response, sender, attributes, flags, handle, 0,
2211 receiver_count, memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002212
2213 /*
2214 * Note that `sizeof(struct_ffa_memory_region)` and
2215 * `sizeof(struct ffa_memory_access)` must both be multiples of
2216 * 16 (as verified by the asserts in `ffa_memory.c`, so it is
2217 * guaranteed that the offset we calculate here is aligned to a
2218 * 64-bit boundary and so 64-bit values can be copied without
2219 * alignment faults.
2220 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002221 composite_offset =
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01002222 retrieve_response->receivers_offset +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002223 (uint32_t)(receiver_count *
2224 retrieve_response->memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01002225
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002226 retrieve_response_receivers =
2227 ffa_memory_region_get_receiver(retrieve_response, 0);
2228 assert(retrieve_response_receivers != NULL);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002229
2230 /*
2231 * Initialized here as in memory retrieve responses we currently
2232 * expect one borrower to be specified.
2233 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002234 memcpy_s(retrieve_response_receivers,
2235 sizeof(struct ffa_memory_access) * receiver_count,
2236 receivers,
2237 sizeof(struct ffa_memory_access) * receiver_count);
2238
2239 retrieve_response_receivers->composite_memory_region_offset =
2240 composite_offset;
2241
J-Alves2d8457f2022-10-05 11:06:41 +01002242 composite_memory_region =
2243 ffa_memory_region_get_composite(retrieve_response, 0);
2244 }
2245
J-Alves2d8457f2022-10-05 11:06:41 +01002246 assert(composite_memory_region != NULL);
2247
J-Alves2d8457f2022-10-05 11:06:41 +01002248 composite_memory_region->page_count = page_count;
2249 composite_memory_region->constituent_count = total_constituent_count;
2250 composite_memory_region->reserved_0 = 0;
2251
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002252 constituents_offset =
2253 composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alves2d8457f2022-10-05 11:06:41 +01002254 if (constituents_offset +
2255 fragment_constituent_count *
2256 sizeof(struct ffa_memory_region_constituent) >
2257 response_max_size) {
2258 return false;
2259 }
2260
2261 for (i = 0; i < fragment_constituent_count; ++i) {
2262 composite_memory_region->constituents[i] = constituents[i];
2263 }
2264
2265 if (total_length != NULL) {
2266 *total_length =
2267 constituents_offset +
2268 composite_memory_region->constituent_count *
2269 sizeof(struct ffa_memory_region_constituent);
2270 }
2271 if (fragment_length != NULL) {
2272 *fragment_length =
2273 constituents_offset +
2274 fragment_constituent_count *
2275 sizeof(struct ffa_memory_region_constituent);
2276 }
2277
2278 return true;
2279}
2280
J-Alves96de29f2022-04-26 16:05:24 +01002281/**
2282 * Validates the retrieved permissions against those specified by the lender
2283 * of memory share operation. Optionally can help set the permissions to be used
2284 * for the S2 mapping, through the `permissions` argument.
J-Alvesdcad8992023-09-15 14:10:35 +01002285 * Returns FFA_SUCCESS if all the fields are valid. FFA_ERROR, with error code:
2286 * - FFA_INVALID_PARAMETERS -> if the fields have invalid values as per the
2287 * specification for each ABI.
2288 * - FFA_DENIED -> if the permissions specified by the retriever are not
2289 * less permissive than those provided by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002290 */
J-Alvesdcad8992023-09-15 14:10:35 +01002291static struct ffa_value ffa_memory_retrieve_is_memory_access_valid(
2292 uint32_t share_func, enum ffa_data_access sent_data_access,
J-Alves96de29f2022-04-26 16:05:24 +01002293 enum ffa_data_access requested_data_access,
2294 enum ffa_instruction_access sent_instruction_access,
2295 enum ffa_instruction_access requested_instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002296 ffa_memory_access_permissions_t *permissions, bool multiple_borrowers)
J-Alves96de29f2022-04-26 16:05:24 +01002297{
2298 switch (sent_data_access) {
2299 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2300 case FFA_DATA_ACCESS_RW:
2301 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2302 requested_data_access == FFA_DATA_ACCESS_RW) {
2303 if (permissions != NULL) {
2304 ffa_set_data_access_attr(permissions,
2305 FFA_DATA_ACCESS_RW);
2306 }
2307 break;
2308 }
2309 /* Intentional fall-through. */
2310 case FFA_DATA_ACCESS_RO:
2311 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2312 requested_data_access == FFA_DATA_ACCESS_RO) {
2313 if (permissions != NULL) {
2314 ffa_set_data_access_attr(permissions,
2315 FFA_DATA_ACCESS_RO);
2316 }
2317 break;
2318 }
2319 dlog_verbose(
2320 "Invalid data access requested; sender specified "
2321 "permissions %#x but receiver requested %#x.\n",
2322 sent_data_access, requested_data_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002323 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002324 case FFA_DATA_ACCESS_RESERVED:
2325 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
2326 "checked before this point.");
2327 }
2328
J-Alvesdcad8992023-09-15 14:10:35 +01002329 /*
2330 * For operations with a single borrower, If it is an FFA_MEMORY_LEND
2331 * or FFA_MEMORY_DONATE the retriever should have specifed the
2332 * instruction permissions it wishes to receive.
2333 */
2334 switch (share_func) {
2335 case FFA_MEM_SHARE_32:
2336 if (requested_instruction_access !=
2337 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2338 dlog_verbose(
2339 "%s: for share instruction permissions must "
2340 "NOT be specified.\n",
2341 __func__);
2342 return ffa_error(FFA_INVALID_PARAMETERS);
2343 }
2344 break;
2345 case FFA_MEM_LEND_32:
2346 /*
2347 * For operations with multiple borrowers only permit XN
2348 * permissions, and both Sender and borrower should have used
2349 * FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED.
2350 */
2351 if (multiple_borrowers) {
2352 if (requested_instruction_access !=
2353 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2354 dlog_verbose(
2355 "%s: lend/share/donate with multiple "
2356 "borrowers "
2357 "instruction permissions must NOT be "
2358 "specified.\n",
2359 __func__);
2360 return ffa_error(FFA_INVALID_PARAMETERS);
2361 }
2362 break;
2363 }
2364 /* Fall through if the operation targets a single borrower. */
2365 case FFA_MEM_DONATE_32:
2366 if (!multiple_borrowers &&
2367 requested_instruction_access ==
2368 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2369 dlog_verbose(
2370 "%s: for lend/donate with single borrower "
2371 "instruction permissions must be speficified "
2372 "by borrower\n",
2373 __func__);
2374 return ffa_error(FFA_INVALID_PARAMETERS);
2375 }
2376 break;
2377 default:
2378 panic("%s: Wrong func id provided.\n", __func__);
2379 }
2380
J-Alves96de29f2022-04-26 16:05:24 +01002381 switch (sent_instruction_access) {
2382 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2383 case FFA_INSTRUCTION_ACCESS_X:
J-Alvesdcad8992023-09-15 14:10:35 +01002384 if (requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
J-Alves96de29f2022-04-26 16:05:24 +01002385 if (permissions != NULL) {
2386 ffa_set_instruction_access_attr(
2387 permissions, FFA_INSTRUCTION_ACCESS_X);
2388 }
2389 break;
2390 }
J-Alvesdcad8992023-09-15 14:10:35 +01002391 /*
2392 * Fall through if requested permissions are less
2393 * permissive than those provided by the sender.
2394 */
J-Alves96de29f2022-04-26 16:05:24 +01002395 case FFA_INSTRUCTION_ACCESS_NX:
2396 if (requested_instruction_access ==
2397 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2398 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2399 if (permissions != NULL) {
2400 ffa_set_instruction_access_attr(
2401 permissions, FFA_INSTRUCTION_ACCESS_NX);
2402 }
2403 break;
2404 }
2405 dlog_verbose(
2406 "Invalid instruction access requested; sender "
2407 "specified permissions %#x but receiver requested "
2408 "%#x.\n",
2409 sent_instruction_access, requested_instruction_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002410 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002411 case FFA_INSTRUCTION_ACCESS_RESERVED:
2412 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
2413 "be checked before this point.");
2414 }
2415
J-Alvesdcad8992023-09-15 14:10:35 +01002416 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves96de29f2022-04-26 16:05:24 +01002417}
2418
2419/**
2420 * Validate the receivers' permissions in the retrieve request against those
2421 * specified by the lender.
2422 * In the `permissions` argument returns the permissions to set at S2 for the
2423 * caller to the FFA_MEMORY_RETRIEVE_REQ.
J-Alves3456e032023-07-20 12:20:05 +01002424 * The function looks into the flag to bypass multiple borrower checks:
2425 * - If not set returns FFA_SUCCESS if all specified permissions are valid.
2426 * - If set returns FFA_SUCCESS if the descriptor contains the permissions
2427 * to the caller of FFA_MEM_RETRIEVE_REQ and they are valid. Other permissions
2428 * are ignored, if provided.
J-Alves96de29f2022-04-26 16:05:24 +01002429 */
2430static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
2431 struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01002432 struct ffa_memory_region *retrieve_request, ffa_id_t to_vm_id,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002433 ffa_memory_access_permissions_t *permissions,
2434 struct ffa_memory_access **receiver_ret, uint32_t func_id)
J-Alves96de29f2022-04-26 16:05:24 +01002435{
2436 uint32_t retrieve_receiver_index;
J-Alves3456e032023-07-20 12:20:05 +01002437 bool bypass_multi_receiver_check =
2438 (retrieve_request->flags &
2439 FFA_MEMORY_REGION_FLAG_BYPASS_BORROWERS_CHECK) != 0U;
J-Alvesdcad8992023-09-15 14:10:35 +01002440 const uint32_t region_receiver_count = memory_region->receiver_count;
2441 struct ffa_value ret;
J-Alves96de29f2022-04-26 16:05:24 +01002442
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002443 assert(receiver_ret != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002444 assert(permissions != NULL);
2445
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002446 *permissions = 0;
2447
J-Alves3456e032023-07-20 12:20:05 +01002448 if (!bypass_multi_receiver_check) {
J-Alvesdcad8992023-09-15 14:10:35 +01002449 if (retrieve_request->receiver_count != region_receiver_count) {
J-Alves3456e032023-07-20 12:20:05 +01002450 dlog_verbose(
2451 "Retrieve request should contain same list of "
2452 "borrowers, as specified by the lender.\n");
2453 return ffa_error(FFA_INVALID_PARAMETERS);
2454 }
2455 } else {
2456 if (retrieve_request->receiver_count != 1) {
2457 dlog_verbose(
2458 "Set bypass multiple borrower check, receiver "
2459 "list must be sized 1 (%x)\n",
2460 memory_region->receiver_count);
2461 return ffa_error(FFA_INVALID_PARAMETERS);
2462 }
J-Alves96de29f2022-04-26 16:05:24 +01002463 }
2464
2465 retrieve_receiver_index = retrieve_request->receiver_count;
2466
J-Alves96de29f2022-04-26 16:05:24 +01002467 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
2468 ffa_memory_access_permissions_t sent_permissions;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002469 struct ffa_memory_access *retrieve_request_receiver =
2470 ffa_memory_region_get_receiver(retrieve_request, i);
2471 assert(retrieve_request_receiver != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002472 ffa_memory_access_permissions_t requested_permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002473 retrieve_request_receiver->receiver_permissions
2474 .permissions;
J-Alves19e20cf2023-08-02 12:48:55 +01002475 ffa_id_t current_receiver_id =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002476 retrieve_request_receiver->receiver_permissions
2477 .receiver;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002478 struct ffa_memory_access *receiver;
2479 uint32_t mem_region_receiver_index;
2480 bool permissions_RO;
2481 bool clear_memory_flags;
J-Alves96de29f2022-04-26 16:05:24 +01002482 bool found_to_id = current_receiver_id == to_vm_id;
2483
J-Alves3456e032023-07-20 12:20:05 +01002484 if (bypass_multi_receiver_check && !found_to_id) {
2485 dlog_verbose(
2486 "Bypass multiple borrower check for id %x.\n",
2487 current_receiver_id);
2488 continue;
2489 }
2490
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002491 if (retrieve_request_receiver->composite_memory_region_offset !=
2492 0U) {
2493 dlog_verbose(
2494 "Retriever specified address ranges not "
2495 "supported (got offset %d).\n",
2496 retrieve_request_receiver
2497 ->composite_memory_region_offset);
2498 return ffa_error(FFA_INVALID_PARAMETERS);
2499 }
2500
J-Alves96de29f2022-04-26 16:05:24 +01002501 /*
2502 * Find the current receiver in the transaction descriptor from
2503 * sender.
2504 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002505 mem_region_receiver_index =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002506 ffa_memory_region_get_receiver_index(
2507 memory_region, current_receiver_id);
J-Alves96de29f2022-04-26 16:05:24 +01002508
2509 if (mem_region_receiver_index ==
2510 memory_region->receiver_count) {
2511 dlog_verbose("%s: receiver %x not found\n", __func__,
2512 current_receiver_id);
2513 return ffa_error(FFA_DENIED);
2514 }
2515
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002516 receiver = ffa_memory_region_get_receiver(
2517 memory_region, mem_region_receiver_index);
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002518 assert(receiver != NULL);
2519
2520 sent_permissions = receiver->receiver_permissions.permissions;
J-Alves96de29f2022-04-26 16:05:24 +01002521
2522 if (found_to_id) {
2523 retrieve_receiver_index = i;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002524
2525 *receiver_ret = receiver;
J-Alves96de29f2022-04-26 16:05:24 +01002526 }
2527
2528 /*
J-Alvesdcad8992023-09-15 14:10:35 +01002529 * Check if retrieve request memory access list is valid:
2530 * - The retrieve request complies with the specification.
2531 * - Permissions are within those specified by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002532 */
J-Alvesdcad8992023-09-15 14:10:35 +01002533 ret = ffa_memory_retrieve_is_memory_access_valid(
2534 func_id, ffa_get_data_access_attr(sent_permissions),
2535 ffa_get_data_access_attr(requested_permissions),
2536 ffa_get_instruction_access_attr(sent_permissions),
2537 ffa_get_instruction_access_attr(requested_permissions),
2538 found_to_id ? permissions : NULL,
2539 region_receiver_count > 1);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002540
J-Alvesdcad8992023-09-15 14:10:35 +01002541 if (ret.func != FFA_SUCCESS_32) {
2542 return ret;
J-Alves96de29f2022-04-26 16:05:24 +01002543 }
2544
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002545 permissions_RO = (ffa_get_data_access_attr(*permissions) ==
2546 FFA_DATA_ACCESS_RO);
2547 clear_memory_flags = (retrieve_request->flags &
2548 FFA_MEMORY_REGION_FLAG_CLEAR) != 0U;
2549
J-Alves96de29f2022-04-26 16:05:24 +01002550 /*
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002551 * Can't request PM to clear memory if only provided
2552 * with RO permissions.
J-Alves96de29f2022-04-26 16:05:24 +01002553 */
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002554 if (found_to_id && permissions_RO && clear_memory_flags) {
J-Alves96de29f2022-04-26 16:05:24 +01002555 dlog_verbose(
2556 "Receiver has RO permissions can not request "
2557 "clear.\n");
2558 return ffa_error(FFA_DENIED);
2559 }
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002560
2561 /*
2562 * Check the impdef in the retrieve_request matches the value in
2563 * the original memory send.
2564 */
2565 if (ffa_version_from_memory_access_desc_size(
2566 memory_region->memory_access_desc_size) >=
2567 MAKE_FFA_VERSION(1, 2) &&
2568 ffa_version_from_memory_access_desc_size(
2569 retrieve_request->memory_access_desc_size) >=
2570 MAKE_FFA_VERSION(1, 2)) {
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002571 if (receiver->impdef.val[0] !=
2572 retrieve_request_receiver->impdef.val[0] ||
2573 receiver->impdef.val[1] !=
2574 retrieve_request_receiver->impdef.val[1]) {
2575 dlog_verbose(
2576 "Impdef value in memory send does not "
2577 "match retrieve request value "
2578 "send value %#x %#x retrieve request "
2579 "value %#x %#x\n",
2580 receiver->impdef.val[0],
2581 receiver->impdef.val[1],
2582 retrieve_request_receiver->impdef
2583 .val[0],
2584 retrieve_request_receiver->impdef
2585 .val[1]);
2586 return ffa_error(FFA_INVALID_PARAMETERS);
2587 }
2588 }
J-Alves96de29f2022-04-26 16:05:24 +01002589 }
2590
2591 if (retrieve_receiver_index == retrieve_request->receiver_count) {
2592 dlog_verbose(
2593 "Retrieve request does not contain caller's (%x) "
2594 "permissions\n",
2595 to_vm_id);
2596 return ffa_error(FFA_INVALID_PARAMETERS);
2597 }
2598
2599 return (struct ffa_value){.func = FFA_SUCCESS_32};
2600}
2601
J-Alvesa9cd7e32022-07-01 13:49:33 +01002602/*
2603 * According to section 16.4.3 of FF-A v1.1 EAC0 specification, the hypervisor
2604 * may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region description
2605 * of a pending memory sharing operation whose allocator is the SPM, for
2606 * validation purposes before forwarding an FFA_MEM_RECLAIM call. In doing so
2607 * the memory region descriptor of the retrieve request must be zeroed with the
2608 * exception of the sender ID and handle.
2609 */
J-Alves4f0d9c12024-01-17 17:23:11 +00002610bool is_ffa_hypervisor_retrieve_request(struct ffa_memory_region *request,
2611 struct vm_locked to_locked)
J-Alvesa9cd7e32022-07-01 13:49:33 +01002612{
2613 return to_locked.vm->id == HF_HYPERVISOR_VM_ID &&
2614 request->attributes == 0U && request->flags == 0U &&
2615 request->tag == 0U && request->receiver_count == 0U &&
2616 plat_ffa_memory_handle_allocated_by_current_world(
2617 request->handle);
2618}
2619
2620/*
2621 * Helper to reset count of fragments retrieved by the hypervisor.
2622 */
2623static void ffa_memory_retrieve_complete_from_hyp(
2624 struct ffa_memory_share_state *share_state)
2625{
2626 if (share_state->hypervisor_fragment_count ==
2627 share_state->fragment_count) {
2628 share_state->hypervisor_fragment_count = 0;
2629 }
2630}
2631
J-Alves089004f2022-07-13 14:25:44 +01002632/**
J-Alves4f0d9c12024-01-17 17:23:11 +00002633 * Prepares the return of the ffa_value for the memory retrieve response.
2634 */
2635static struct ffa_value ffa_memory_retrieve_resp(uint32_t total_length,
2636 uint32_t fragment_length)
2637{
2638 return (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
2639 .arg1 = total_length,
2640 .arg2 = fragment_length};
2641}
2642
2643/**
J-Alves089004f2022-07-13 14:25:44 +01002644 * Validate that the memory region descriptor provided by the borrower on
2645 * FFA_MEM_RETRIEVE_REQ, against saved memory region provided by lender at the
2646 * memory sharing call.
2647 */
2648static struct ffa_value ffa_memory_retrieve_validate(
J-Alves4f0d9c12024-01-17 17:23:11 +00002649 ffa_id_t to_id, struct ffa_memory_region *retrieve_request,
2650 uint32_t retrieve_request_length,
J-Alves089004f2022-07-13 14:25:44 +01002651 struct ffa_memory_region *memory_region, uint32_t *receiver_index,
2652 uint32_t share_func)
2653{
2654 ffa_memory_region_flags_t transaction_type =
2655 retrieve_request->flags &
2656 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002657 enum ffa_memory_security security_state;
J-Alves4f0d9c12024-01-17 17:23:11 +00002658 const uint64_t memory_access_desc_size =
2659 retrieve_request->memory_access_desc_size;
2660 const uint32_t expected_retrieve_request_length =
2661 retrieve_request->receivers_offset +
2662 (uint32_t)(retrieve_request->receiver_count *
2663 memory_access_desc_size);
J-Alves089004f2022-07-13 14:25:44 +01002664
2665 assert(retrieve_request != NULL);
2666 assert(memory_region != NULL);
2667 assert(receiver_index != NULL);
2668 assert(retrieve_request->sender == memory_region->sender);
2669
J-Alves4f0d9c12024-01-17 17:23:11 +00002670 if (retrieve_request_length != expected_retrieve_request_length) {
2671 dlog_verbose(
2672 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
2673 "but was %d.\n",
2674 expected_retrieve_request_length,
2675 retrieve_request_length);
2676 return ffa_error(FFA_INVALID_PARAMETERS);
2677 }
2678
2679 if (retrieve_request->sender != memory_region->sender) {
2680 dlog_verbose(
2681 "Memory with handle %#x not fully sent, can't "
2682 "retrieve.\n",
2683 memory_region->handle);
2684 return ffa_error(FFA_DENIED);
2685 }
2686
2687 /*
2688 * The SPMC can only process retrieve requests to memory share
2689 * operations with one borrower from the other world. It can't
2690 * determine the ID of the NWd VM that invoked the retrieve
2691 * request interface call. It relies on the hypervisor to
2692 * validate the caller's ID against that provided in the
2693 * `receivers` list of the retrieve response.
2694 * In case there is only one borrower from the NWd in the
2695 * transaction descriptor, record that in the `receiver_id` for
2696 * later use, and validate in the retrieve request message.
2697 * This limitation is due to the fact SPMC can't determine the
2698 * index in the memory share structures state to update.
2699 */
2700 if (to_id == HF_HYPERVISOR_VM_ID) {
2701 uint32_t other_world_count = 0;
2702
2703 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
2704 struct ffa_memory_access *receiver =
2705 ffa_memory_region_get_receiver(retrieve_request,
2706 0);
2707 assert(receiver != NULL);
2708
2709 to_id = receiver->receiver_permissions.receiver;
2710
2711 if (!vm_id_is_current_world(to_id)) {
2712 other_world_count++;
2713 }
2714 }
2715
2716 if (other_world_count > 1) {
2717 dlog_verbose(
2718 "Support one receiver from the other "
2719 "world.\n");
2720 return ffa_error(FFA_NOT_SUPPORTED);
2721 }
2722 }
J-Alves089004f2022-07-13 14:25:44 +01002723 /*
2724 * Check that the transaction type expected by the receiver is
2725 * correct, if it has been specified.
2726 */
2727 if (transaction_type !=
2728 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
2729 transaction_type != (memory_region->flags &
2730 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
2731 dlog_verbose(
2732 "Incorrect transaction type %#x for "
2733 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
2734 transaction_type,
2735 memory_region->flags &
2736 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
2737 retrieve_request->handle);
2738 return ffa_error(FFA_INVALID_PARAMETERS);
2739 }
2740
2741 if (retrieve_request->tag != memory_region->tag) {
2742 dlog_verbose(
2743 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
2744 "%d for handle %#x.\n",
2745 retrieve_request->tag, memory_region->tag,
2746 retrieve_request->handle);
2747 return ffa_error(FFA_INVALID_PARAMETERS);
2748 }
2749
J-Alves4f0d9c12024-01-17 17:23:11 +00002750 *receiver_index =
2751 ffa_memory_region_get_receiver_index(memory_region, to_id);
J-Alves089004f2022-07-13 14:25:44 +01002752
2753 if (*receiver_index == memory_region->receiver_count) {
2754 dlog_verbose(
2755 "Incorrect receiver VM ID %d for "
2756 "FFA_MEM_RETRIEVE_REQ, for handle %#x.\n",
J-Alves4f0d9c12024-01-17 17:23:11 +00002757 to_id, memory_region->handle);
J-Alves089004f2022-07-13 14:25:44 +01002758 return ffa_error(FFA_INVALID_PARAMETERS);
2759 }
2760
2761 if ((retrieve_request->flags &
2762 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
2763 dlog_verbose(
2764 "Retriever specified 'address range alignment 'hint' "
2765 "not supported.\n");
2766 return ffa_error(FFA_INVALID_PARAMETERS);
2767 }
2768 if ((retrieve_request->flags &
2769 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
2770 dlog_verbose(
2771 "Bits 8-5 must be zero in memory region's flags "
2772 "(address range alignment hint not supported).\n");
2773 return ffa_error(FFA_INVALID_PARAMETERS);
2774 }
2775
2776 if ((retrieve_request->flags & ~0x7FF) != 0U) {
2777 dlog_verbose(
2778 "Bits 31-10 must be zero in memory region's flags.\n");
2779 return ffa_error(FFA_INVALID_PARAMETERS);
2780 }
2781
2782 if (share_func == FFA_MEM_SHARE_32 &&
2783 (retrieve_request->flags &
2784 (FFA_MEMORY_REGION_FLAG_CLEAR |
2785 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
2786 dlog_verbose(
2787 "Memory Share operation can't clean after relinquish "
2788 "memory region.\n");
2789 return ffa_error(FFA_INVALID_PARAMETERS);
2790 }
2791
2792 /*
2793 * If the borrower needs the memory to be cleared before mapping
2794 * to its address space, the sender should have set the flag
2795 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
2796 * FFA_DENIED.
2797 */
2798 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
2799 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
2800 dlog_verbose(
2801 "Borrower needs memory cleared. Sender needs to set "
2802 "flag for clearing memory.\n");
2803 return ffa_error(FFA_DENIED);
2804 }
2805
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002806 /* Memory region attributes NS-Bit MBZ for FFA_MEM_RETRIEVE_REQ. */
2807 security_state =
2808 ffa_get_memory_security_attr(retrieve_request->attributes);
2809 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
2810 dlog_verbose(
2811 "Invalid security state for memory retrieve request "
2812 "operation.\n");
2813 return ffa_error(FFA_INVALID_PARAMETERS);
2814 }
2815
J-Alves089004f2022-07-13 14:25:44 +01002816 /*
2817 * If memory type is not specified, bypass validation of memory
2818 * attributes in the retrieve request. The retriever is expecting to
2819 * obtain this information from the SPMC.
2820 */
2821 if (ffa_get_memory_type_attr(retrieve_request->attributes) ==
2822 FFA_MEMORY_NOT_SPECIFIED_MEM) {
2823 return (struct ffa_value){.func = FFA_SUCCESS_32};
2824 }
2825
2826 /*
2827 * Ensure receiver's attributes are compatible with how
2828 * Hafnium maps memory: Normal Memory, Inner shareable,
2829 * Write-Back Read-Allocate Write-Allocate Cacheable.
2830 */
2831 return ffa_memory_attributes_validate(retrieve_request->attributes);
2832}
2833
J-Alves4f0d9c12024-01-17 17:23:11 +00002834static struct ffa_value ffa_partition_retrieve_request(
2835 struct share_states_locked share_states,
2836 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
2837 struct ffa_memory_region *retrieve_request,
2838 uint32_t retrieve_request_length, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002839{
J-Alvesa9cd7e32022-07-01 13:49:33 +01002840 ffa_memory_access_permissions_t permissions = 0;
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002841 uint32_t memory_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002842 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002843 struct ffa_composite_memory_region *composite;
2844 uint32_t total_length;
2845 uint32_t fragment_length;
J-Alves19e20cf2023-08-02 12:48:55 +01002846 ffa_id_t receiver_id = to_locked.vm->id;
J-Alves4f0d9c12024-01-17 17:23:11 +00002847 bool is_retrieve_complete = false;
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002848 ffa_memory_attributes_t attributes;
J-Alves4f0d9c12024-01-17 17:23:11 +00002849 const uint64_t memory_access_desc_size =
Daniel Boulbyde974ca2023-12-12 13:53:31 +00002850 retrieve_request->memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00002851 uint32_t receiver_index;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002852 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00002853 ffa_memory_handle_t handle = retrieve_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002854
J-Alves4f0d9c12024-01-17 17:23:11 +00002855 struct ffa_memory_region *memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002856
J-Alves96de29f2022-04-26 16:05:24 +01002857 if (!share_state->sending_complete) {
2858 dlog_verbose(
2859 "Memory with handle %#x not fully sent, can't "
2860 "retrieve.\n",
2861 handle);
J-Alves4f0d9c12024-01-17 17:23:11 +00002862 return ffa_error(FFA_INVALID_PARAMETERS);
J-Alves96de29f2022-04-26 16:05:24 +01002863 }
2864
J-Alves4f0d9c12024-01-17 17:23:11 +00002865 /*
2866 * Validate retrieve request, according to what was sent by the
2867 * sender. Function will output the `receiver_index` from the
2868 * provided memory region.
2869 */
2870 ret = ffa_memory_retrieve_validate(
2871 receiver_id, retrieve_request, retrieve_request_length,
2872 memory_region, &receiver_index, share_state->share_func);
J-Alves089004f2022-07-13 14:25:44 +01002873
J-Alves4f0d9c12024-01-17 17:23:11 +00002874 if (ret.func != FFA_SUCCESS_32) {
2875 return ret;
J-Alves089004f2022-07-13 14:25:44 +01002876 }
J-Alves96de29f2022-04-26 16:05:24 +01002877
J-Alves4f0d9c12024-01-17 17:23:11 +00002878 if (share_state->retrieved_fragment_count[receiver_index] != 0U) {
2879 dlog_verbose("Memory with handle %#x already retrieved.\n",
2880 handle);
2881 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002882 }
2883
J-Alves4f0d9c12024-01-17 17:23:11 +00002884 /*
2885 * Validate the requested permissions against the sent
2886 * permissions.
2887 * Outputs the permissions to give to retriever at S2
2888 * PTs.
2889 */
2890 ret = ffa_memory_retrieve_validate_memory_access_list(
2891 memory_region, retrieve_request, receiver_id, &permissions,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002892 &receiver, share_state->share_func);
J-Alves4f0d9c12024-01-17 17:23:11 +00002893 if (ret.func != FFA_SUCCESS_32) {
2894 return ret;
2895 }
2896
2897 memory_to_mode = ffa_memory_permissions_to_mode(
2898 permissions, share_state->sender_orig_mode);
2899
2900 ret = ffa_retrieve_check_update(
2901 to_locked, share_state->fragments,
2902 share_state->fragment_constituent_counts,
2903 share_state->fragment_count, memory_to_mode,
2904 share_state->share_func, false, page_pool);
2905
2906 if (ret.func != FFA_SUCCESS_32) {
2907 return ret;
2908 }
2909
2910 share_state->retrieved_fragment_count[receiver_index] = 1;
2911
2912 is_retrieve_complete =
2913 share_state->retrieved_fragment_count[receiver_index] ==
2914 share_state->fragment_count;
2915
J-Alvesb5084cf2022-07-06 14:20:12 +01002916 /* VMs acquire the RX buffer from SPMC. */
2917 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
2918
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002919 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002920 * Copy response to RX buffer of caller and deliver the message.
2921 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002922 */
Andrew Walbranca808b12020-05-15 17:22:28 +01002923 composite = ffa_memory_region_get_composite(memory_region, 0);
J-Alves4f0d9c12024-01-17 17:23:11 +00002924
Andrew Walbranca808b12020-05-15 17:22:28 +01002925 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002926 * Constituents which we received in the first fragment should
2927 * always fit in the first fragment we are sending, because the
2928 * header is the same size in both cases and we have a fixed
2929 * message buffer size. So `ffa_retrieved_memory_region_init`
2930 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01002931 */
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002932
2933 /*
2934 * Set the security state in the memory retrieve response attributes
2935 * if specified by the target mode.
2936 */
2937 attributes = plat_ffa_memory_security_mode(
2938 memory_region->attributes, share_state->sender_orig_mode);
2939
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002940 /* Provide the permissions that had been provided. */
2941 receiver->receiver_permissions.permissions = permissions;
2942
2943 /*
2944 * Prepare the memory region descriptor for the retrieve response.
2945 * Provide the pointer to the receiver tracked in the share state
2946 * strucutures.
2947 */
Andrew Walbranca808b12020-05-15 17:22:28 +01002948 CHECK(ffa_retrieved_memory_region_init(
J-Alves2d8457f2022-10-05 11:06:41 +01002949 to_locked.vm->mailbox.recv, to_locked.vm->ffa_version,
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002950 HF_MAILBOX_SIZE, memory_region->sender, attributes,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002951 memory_region->flags, handle, permissions, receiver, 1,
2952 memory_access_desc_size, composite->page_count,
2953 composite->constituent_count, share_state->fragments[0],
Andrew Walbranca808b12020-05-15 17:22:28 +01002954 share_state->fragment_constituent_counts[0], &total_length,
2955 &fragment_length));
J-Alvesb5084cf2022-07-06 14:20:12 +01002956
J-Alves4f0d9c12024-01-17 17:23:11 +00002957 if (is_retrieve_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002958 ffa_memory_retrieve_complete(share_states, share_state,
2959 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002960 }
J-Alves4f0d9c12024-01-17 17:23:11 +00002961
2962 return ffa_memory_retrieve_resp(total_length, fragment_length);
2963}
2964
2965static struct ffa_value ffa_hypervisor_retrieve_request(
2966 struct ffa_memory_share_state *share_state, struct vm_locked to_locked,
2967 struct ffa_memory_region *retrieve_request)
2968{
2969 struct ffa_value ret;
2970 struct ffa_composite_memory_region *composite;
2971 uint32_t total_length;
2972 uint32_t fragment_length;
J-Alves4f0d9c12024-01-17 17:23:11 +00002973 ffa_memory_attributes_t attributes;
J-Alves7b6ab612024-01-24 09:54:54 +00002974 uint64_t memory_access_desc_size;
J-Alves4f0d9c12024-01-17 17:23:11 +00002975 struct ffa_memory_region *memory_region;
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002976 struct ffa_memory_access *receiver;
J-Alves4f0d9c12024-01-17 17:23:11 +00002977 ffa_memory_handle_t handle = retrieve_request->handle;
2978
J-Alves4f0d9c12024-01-17 17:23:11 +00002979 memory_region = share_state->memory_region;
2980
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00002981 assert(to_locked.vm->id == HF_HYPERVISOR_VM_ID);
2982
J-Alves7b6ab612024-01-24 09:54:54 +00002983 switch (to_locked.vm->ffa_version) {
2984 case MAKE_FFA_VERSION(1, 2):
2985 memory_access_desc_size = sizeof(struct ffa_memory_access);
2986 break;
2987 case MAKE_FFA_VERSION(1, 0):
2988 case MAKE_FFA_VERSION(1, 1):
2989 memory_access_desc_size = sizeof(struct ffa_memory_access_v1_0);
2990 break;
2991 default:
2992 panic("version not supported: %x\n", to_locked.vm->ffa_version);
2993 }
2994
J-Alves4f0d9c12024-01-17 17:23:11 +00002995 if (share_state->hypervisor_fragment_count != 0U) {
2996 dlog_verbose(
2997 "Memory with handle %#x already retrieved by "
2998 "the hypervisor.\n",
2999 handle);
3000 return ffa_error(FFA_DENIED);
3001 }
3002
3003 share_state->hypervisor_fragment_count = 1;
3004
3005 ffa_memory_retrieve_complete_from_hyp(share_state);
3006
3007 /* VMs acquire the RX buffer from SPMC. */
3008 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
3009
3010 /*
3011 * Copy response to RX buffer of caller and deliver the message.
3012 * This must be done before the share_state is (possibly) freed.
3013 */
3014 composite = ffa_memory_region_get_composite(memory_region, 0);
3015
3016 /*
3017 * Constituents which we received in the first fragment should
3018 * always fit in the first fragment we are sending, because the
3019 * header is the same size in both cases and we have a fixed
3020 * message buffer size. So `ffa_retrieved_memory_region_init`
3021 * should never fail.
3022 */
3023
3024 /*
3025 * Set the security state in the memory retrieve response attributes
3026 * if specified by the target mode.
3027 */
3028 attributes = plat_ffa_memory_security_mode(
3029 memory_region->attributes, share_state->sender_orig_mode);
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003030
3031 receiver = ffa_memory_region_get_receiver(memory_region, 0);
3032
J-Alves4f0d9c12024-01-17 17:23:11 +00003033 CHECK(ffa_retrieved_memory_region_init(
3034 to_locked.vm->mailbox.recv, to_locked.vm->ffa_version,
3035 HF_MAILBOX_SIZE, memory_region->sender, attributes,
Daniel Boulby44e9b3b2024-01-17 12:21:44 +00003036 memory_region->flags, handle,
3037 receiver->receiver_permissions.permissions, receiver,
3038 memory_region->receiver_count, memory_access_desc_size,
J-Alves4f0d9c12024-01-17 17:23:11 +00003039 composite->page_count, composite->constituent_count,
3040 share_state->fragments[0],
3041 share_state->fragment_constituent_counts[0], &total_length,
3042 &fragment_length));
3043
3044 return ffa_memory_retrieve_resp(total_length, fragment_length);
3045}
3046
3047struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
3048 struct ffa_memory_region *retrieve_request,
3049 uint32_t retrieve_request_length,
3050 struct mpool *page_pool)
3051{
3052 ffa_memory_handle_t handle = retrieve_request->handle;
3053 struct share_states_locked share_states;
3054 struct ffa_memory_share_state *share_state;
3055 struct ffa_value ret;
3056
3057 dump_share_states();
3058
3059 share_states = share_states_lock();
3060 share_state = get_share_state(share_states, handle);
3061 if (share_state == NULL) {
3062 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
3063 handle);
3064 ret = ffa_error(FFA_INVALID_PARAMETERS);
3065 goto out;
3066 }
3067
3068 if (is_ffa_hypervisor_retrieve_request(retrieve_request, to_locked)) {
3069 ret = ffa_hypervisor_retrieve_request(share_state, to_locked,
3070 retrieve_request);
3071 } else {
3072 ret = ffa_partition_retrieve_request(
3073 share_states, share_state, to_locked, retrieve_request,
3074 retrieve_request_length, page_pool);
3075 }
3076
3077 /* Track use of the RX buffer if the handling has succeeded. */
3078 if (ret.func == FFA_MEM_RETRIEVE_RESP_32) {
3079 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
3080 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
3081 }
3082
Andrew Walbranca808b12020-05-15 17:22:28 +01003083out:
3084 share_states_unlock(&share_states);
3085 dump_share_states();
3086 return ret;
3087}
3088
J-Alves5da37d92022-10-24 16:33:48 +01003089/**
3090 * Determine expected fragment offset according to the FF-A version of
3091 * the caller.
3092 */
3093static uint32_t ffa_memory_retrieve_expected_offset_per_ffa_version(
3094 struct ffa_memory_region *memory_region,
3095 uint32_t retrieved_constituents_count, uint32_t ffa_version)
3096{
3097 uint32_t expected_fragment_offset;
3098 uint32_t composite_constituents_offset;
3099
Kathleen Capellae4fe2962023-09-01 17:08:47 -04003100 if (ffa_version >= MAKE_FFA_VERSION(1, 1)) {
J-Alves5da37d92022-10-24 16:33:48 +01003101 /*
3102 * Hafnium operates memory regions in FF-A v1.1 format, so we
3103 * can retrieve the constituents offset from descriptor.
3104 */
3105 composite_constituents_offset =
3106 ffa_composite_constituent_offset(memory_region, 0);
3107 } else if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
3108 /*
3109 * If retriever is FF-A v1.0, determine the composite offset
3110 * as it is expected to have been configured in the
3111 * retrieve response.
3112 */
3113 composite_constituents_offset =
3114 sizeof(struct ffa_memory_region_v1_0) +
3115 RECEIVERS_COUNT_IN_RETRIEVE_RESP *
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003116 sizeof(struct ffa_memory_access_v1_0) +
J-Alves5da37d92022-10-24 16:33:48 +01003117 sizeof(struct ffa_composite_memory_region);
3118 } else {
3119 panic("%s received an invalid FF-A version.\n", __func__);
3120 }
3121
3122 expected_fragment_offset =
3123 composite_constituents_offset +
3124 retrieved_constituents_count *
3125 sizeof(struct ffa_memory_region_constituent) -
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003126 (uint32_t)(memory_region->memory_access_desc_size *
3127 (memory_region->receiver_count - 1));
J-Alves5da37d92022-10-24 16:33:48 +01003128
3129 return expected_fragment_offset;
3130}
3131
Andrew Walbranca808b12020-05-15 17:22:28 +01003132struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
3133 ffa_memory_handle_t handle,
3134 uint32_t fragment_offset,
J-Alves19e20cf2023-08-02 12:48:55 +01003135 ffa_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01003136 struct mpool *page_pool)
3137{
3138 struct ffa_memory_region *memory_region;
3139 struct share_states_locked share_states;
3140 struct ffa_memory_share_state *share_state;
3141 struct ffa_value ret;
3142 uint32_t fragment_index;
3143 uint32_t retrieved_constituents_count;
3144 uint32_t i;
3145 uint32_t expected_fragment_offset;
3146 uint32_t remaining_constituent_count;
3147 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01003148 uint32_t receiver_index;
J-Alves59ed0042022-07-28 18:26:41 +01003149 bool continue_ffa_hyp_mem_retrieve_req;
Andrew Walbranca808b12020-05-15 17:22:28 +01003150
3151 dump_share_states();
3152
3153 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003154 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003155 if (share_state == NULL) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003156 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
3157 handle);
3158 ret = ffa_error(FFA_INVALID_PARAMETERS);
3159 goto out;
3160 }
3161
3162 memory_region = share_state->memory_region;
3163 CHECK(memory_region != NULL);
3164
Andrew Walbranca808b12020-05-15 17:22:28 +01003165 if (!share_state->sending_complete) {
3166 dlog_verbose(
3167 "Memory with handle %#x not fully sent, can't "
3168 "retrieve.\n",
3169 handle);
3170 ret = ffa_error(FFA_INVALID_PARAMETERS);
3171 goto out;
3172 }
3173
J-Alves59ed0042022-07-28 18:26:41 +01003174 /*
3175 * If retrieve request from the hypervisor has been initiated in the
3176 * given share_state, continue it, else assume it is a continuation of
3177 * retrieve request from a NWd VM.
3178 */
3179 continue_ffa_hyp_mem_retrieve_req =
3180 (to_locked.vm->id == HF_HYPERVISOR_VM_ID) &&
3181 (share_state->hypervisor_fragment_count != 0U) &&
J-Alves661e1b72023-08-02 13:39:40 +01003182 ffa_is_vm_id(sender_vm_id);
Andrew Walbranca808b12020-05-15 17:22:28 +01003183
J-Alves59ed0042022-07-28 18:26:41 +01003184 if (!continue_ffa_hyp_mem_retrieve_req) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003185 receiver_index = ffa_memory_region_get_receiver_index(
J-Alves59ed0042022-07-28 18:26:41 +01003186 memory_region, to_locked.vm->id);
3187
3188 if (receiver_index == memory_region->receiver_count) {
3189 dlog_verbose(
3190 "Caller of FFA_MEM_FRAG_RX (%x) is not a "
3191 "borrower to memory sharing transaction (%x)\n",
3192 to_locked.vm->id, handle);
3193 ret = ffa_error(FFA_INVALID_PARAMETERS);
3194 goto out;
3195 }
3196
3197 if (share_state->retrieved_fragment_count[receiver_index] ==
3198 0 ||
3199 share_state->retrieved_fragment_count[receiver_index] >=
3200 share_state->fragment_count) {
3201 dlog_verbose(
3202 "Retrieval of memory with handle %#x not yet "
3203 "started or already completed (%d/%d fragments "
3204 "retrieved).\n",
3205 handle,
3206 share_state->retrieved_fragment_count
3207 [receiver_index],
3208 share_state->fragment_count);
3209 ret = ffa_error(FFA_INVALID_PARAMETERS);
3210 goto out;
3211 }
3212
3213 fragment_index =
3214 share_state->retrieved_fragment_count[receiver_index];
3215 } else {
3216 if (share_state->hypervisor_fragment_count == 0 ||
3217 share_state->hypervisor_fragment_count >=
3218 share_state->fragment_count) {
3219 dlog_verbose(
3220 "Retrieve of memory with handle %x not "
3221 "started from hypervisor.\n",
3222 handle);
3223 ret = ffa_error(FFA_INVALID_PARAMETERS);
3224 goto out;
3225 }
3226
3227 if (memory_region->sender != sender_vm_id) {
3228 dlog_verbose(
3229 "Sender ID (%x) is not as expected for memory "
3230 "handle %x\n",
3231 sender_vm_id, handle);
3232 ret = ffa_error(FFA_INVALID_PARAMETERS);
3233 goto out;
3234 }
3235
3236 fragment_index = share_state->hypervisor_fragment_count;
3237
3238 receiver_index = 0;
3239 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003240
3241 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003242 * Check that the given fragment offset is correct by counting
3243 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01003244 */
3245 retrieved_constituents_count = 0;
3246 for (i = 0; i < fragment_index; ++i) {
3247 retrieved_constituents_count +=
3248 share_state->fragment_constituent_counts[i];
3249 }
J-Alvesc7484f12022-05-13 12:41:14 +01003250
3251 CHECK(memory_region->receiver_count > 0);
3252
Andrew Walbranca808b12020-05-15 17:22:28 +01003253 expected_fragment_offset =
J-Alves5da37d92022-10-24 16:33:48 +01003254 ffa_memory_retrieve_expected_offset_per_ffa_version(
3255 memory_region, retrieved_constituents_count,
3256 to_locked.vm->ffa_version);
3257
Andrew Walbranca808b12020-05-15 17:22:28 +01003258 if (fragment_offset != expected_fragment_offset) {
3259 dlog_verbose("Fragment offset was %d but expected %d.\n",
3260 fragment_offset, expected_fragment_offset);
3261 ret = ffa_error(FFA_INVALID_PARAMETERS);
3262 goto out;
3263 }
3264
J-Alves4f0d9c12024-01-17 17:23:11 +00003265 /*
3266 * When hafnium is the hypervisor, acquire the RX buffer of a VM, that
3267 * is currently ownder by the SPMC.
3268 */
3269 assert(plat_ffa_acquire_receiver_rx(to_locked, &ret));
J-Alves59ed0042022-07-28 18:26:41 +01003270
Andrew Walbranca808b12020-05-15 17:22:28 +01003271 remaining_constituent_count = ffa_memory_fragment_init(
3272 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
3273 share_state->fragments[fragment_index],
3274 share_state->fragment_constituent_counts[fragment_index],
3275 &fragment_length);
3276 CHECK(remaining_constituent_count == 0);
J-Alves674e4de2024-01-17 16:20:32 +00003277
Andrew Walbranca808b12020-05-15 17:22:28 +01003278 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00003279 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbranca808b12020-05-15 17:22:28 +01003280
J-Alves59ed0042022-07-28 18:26:41 +01003281 if (!continue_ffa_hyp_mem_retrieve_req) {
3282 share_state->retrieved_fragment_count[receiver_index]++;
3283 if (share_state->retrieved_fragment_count[receiver_index] ==
3284 share_state->fragment_count) {
3285 ffa_memory_retrieve_complete(share_states, share_state,
3286 page_pool);
3287 }
3288 } else {
3289 share_state->hypervisor_fragment_count++;
3290
3291 ffa_memory_retrieve_complete_from_hyp(share_state);
3292 }
Andrew Walbranca808b12020-05-15 17:22:28 +01003293 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
3294 .arg1 = (uint32_t)handle,
3295 .arg2 = (uint32_t)(handle >> 32),
3296 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003297
3298out:
3299 share_states_unlock(&share_states);
3300 dump_share_states();
3301 return ret;
3302}
3303
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003304struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003305 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003306 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003307{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003308 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003309 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003310 struct ffa_memory_share_state *share_state;
3311 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003312 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003313 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01003314 uint32_t receiver_index;
J-Alves3c5b2072022-11-21 12:45:40 +00003315 bool receivers_relinquished_memory;
J-Alves639ddfc2023-11-21 14:17:26 +00003316 ffa_memory_access_permissions_t receiver_permissions = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003317
Andrew Walbrana65a1322020-04-06 19:32:32 +01003318 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003319 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003320 "Stream endpoints not supported (got %d "
J-Alves668a86e2023-05-10 11:53:25 +01003321 "endpoints on FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003322 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003323 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003324 }
3325
Andrew Walbrana65a1322020-04-06 19:32:32 +01003326 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003327 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003328 "VM ID %d in relinquish message doesn't match "
J-Alves668a86e2023-05-10 11:53:25 +01003329 "calling VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01003330 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003331 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003332 }
3333
3334 dump_share_states();
3335
3336 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01003337 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003338 if (share_state == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003339 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003340 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003341 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003342 goto out;
3343 }
3344
Andrew Walbranca808b12020-05-15 17:22:28 +01003345 if (!share_state->sending_complete) {
3346 dlog_verbose(
3347 "Memory with handle %#x not fully sent, can't "
3348 "relinquish.\n",
3349 handle);
3350 ret = ffa_error(FFA_INVALID_PARAMETERS);
3351 goto out;
3352 }
3353
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003354 memory_region = share_state->memory_region;
3355 CHECK(memory_region != NULL);
3356
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003357 receiver_index = ffa_memory_region_get_receiver_index(
3358 memory_region, from_locked.vm->id);
J-Alves8eb19162022-04-28 10:56:48 +01003359
3360 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003361 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003362 "VM ID %d tried to relinquish memory region "
J-Alves668a86e2023-05-10 11:53:25 +01003363 "with handle %#x and it is not a valid borrower.\n",
J-Alves8eb19162022-04-28 10:56:48 +01003364 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003365 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003366 goto out;
3367 }
3368
J-Alves8eb19162022-04-28 10:56:48 +01003369 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01003370 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003371 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003372 "Memory with handle %#x not yet fully "
3373 "retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01003374 "receiver %x can't relinquish.\n",
3375 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003376 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003377 goto out;
3378 }
3379
J-Alves3c5b2072022-11-21 12:45:40 +00003380 /*
3381 * Either clear if requested in relinquish call, or in a retrieve
3382 * request from one of the borrowers.
3383 */
3384 receivers_relinquished_memory = true;
3385
3386 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3387 struct ffa_memory_access *receiver =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003388 ffa_memory_region_get_receiver(memory_region, i);
3389 assert(receiver != NULL);
J-Alves3c5b2072022-11-21 12:45:40 +00003390 if (receiver->receiver_permissions.receiver ==
3391 from_locked.vm->id) {
J-Alves639ddfc2023-11-21 14:17:26 +00003392 receiver_permissions =
3393 receiver->receiver_permissions.permissions;
J-Alves3c5b2072022-11-21 12:45:40 +00003394 continue;
3395 }
3396
3397 if (share_state->retrieved_fragment_count[i] != 0U) {
3398 receivers_relinquished_memory = false;
3399 break;
3400 }
3401 }
3402
3403 clear = receivers_relinquished_memory &&
Daniel Boulby2e14ebe2024-01-15 16:21:44 +00003404 ((relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
3405 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003406
3407 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003408 * Clear is not allowed for memory that was shared, as the
3409 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003410 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003411 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003412 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003413 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003414 goto out;
3415 }
3416
J-Alves639ddfc2023-11-21 14:17:26 +00003417 if (clear && receiver_permissions == FFA_DATA_ACCESS_RO) {
3418 dlog_verbose("%s: RO memory can't use clear memory flag.\n",
3419 __func__);
3420 ret = ffa_error(FFA_DENIED);
3421 goto out;
3422 }
3423
Andrew Walbranca808b12020-05-15 17:22:28 +01003424 ret = ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01003425 from_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003426 share_state->fragment_constituent_counts,
3427 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003428
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003429 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003430 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003431 * Mark memory handle as not retrieved, so it can be
3432 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003433 */
J-Alves8eb19162022-04-28 10:56:48 +01003434 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003435 }
3436
3437out:
3438 share_states_unlock(&share_states);
3439 dump_share_states();
3440 return ret;
3441}
3442
3443/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01003444 * Validates that the reclaim transition is allowed for the given
3445 * handle, updates the page table of the reclaiming VM, and frees the
3446 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003447 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003448struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01003449 ffa_memory_handle_t handle,
3450 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003451 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003452{
3453 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003454 struct ffa_memory_share_state *share_state;
3455 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003456 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003457
3458 dump_share_states();
3459
3460 share_states = share_states_lock();
Karl Meakin52cdfe72023-06-30 14:49:10 +01003461
Karl Meakin4a2854a2023-06-30 16:26:52 +01003462 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003463 if (share_state == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003464 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003465 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003466 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003467 goto out;
3468 }
Karl Meakin4a2854a2023-06-30 16:26:52 +01003469 memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003470
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003471 CHECK(memory_region != NULL);
3472
J-Alvesa9cd7e32022-07-01 13:49:33 +01003473 if (vm_id_is_current_world(to_locked.vm->id) &&
3474 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003475 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01003476 "VM %#x attempted to reclaim memory handle %#x "
3477 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003478 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003479 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003480 goto out;
3481 }
3482
Andrew Walbranca808b12020-05-15 17:22:28 +01003483 if (!share_state->sending_complete) {
3484 dlog_verbose(
3485 "Memory with handle %#x not fully sent, can't "
3486 "reclaim.\n",
3487 handle);
3488 ret = ffa_error(FFA_INVALID_PARAMETERS);
3489 goto out;
3490 }
3491
J-Alves752236c2022-04-28 11:07:47 +01003492 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3493 if (share_state->retrieved_fragment_count[i] != 0) {
3494 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003495 "Tried to reclaim memory handle %#x "
J-Alves3c5b2072022-11-21 12:45:40 +00003496 "that has not been relinquished by all "
J-Alvesa9cd7e32022-07-01 13:49:33 +01003497 "borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01003498 handle,
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003499 ffa_memory_region_get_receiver(memory_region, i)
3500 ->receiver_permissions.receiver);
J-Alves752236c2022-04-28 11:07:47 +01003501 ret = ffa_error(FFA_DENIED);
3502 goto out;
3503 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003504 }
3505
Andrew Walbranca808b12020-05-15 17:22:28 +01003506 ret = ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01003507 to_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003508 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00003509 share_state->fragment_count, share_state->sender_orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01003510 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003511
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003512 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003513 share_state_free(share_states, share_state, page_pool);
J-Alves3c5b2072022-11-21 12:45:40 +00003514 dlog_verbose("Freed share state after successful reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003515 }
3516
3517out:
3518 share_states_unlock(&share_states);
3519 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01003520}