blob: 74e0f822ecc842c9a4365767b05f08b94107b859 [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"
Jose Marinho75509b42019-04-09 09:34:59 +010028
J-Alves2d8457f2022-10-05 11:06:41 +010029#include "vmapi/hf/ffa_v1_0.h"
30
J-Alves5da37d92022-10-24 16:33:48 +010031#define RECEIVERS_COUNT_IN_RETRIEVE_RESP 1
32
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000033/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010034 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000035 * by this lock.
36 */
37static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010038static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000039
40/**
J-Alvesed508c82023-05-04 16:09:48 +010041 * Return the offset to the first constituent within the
42 * `ffa_composite_memory_region` for the given receiver from an
43 * `ffa_memory_region`. The caller must check that the receiver_index is within
44 * bounds, and that it has a composite memory region offset.
45 */
46static uint32_t ffa_composite_constituent_offset(
47 struct ffa_memory_region *memory_region, uint32_t receiver_index)
48{
49 CHECK(receiver_index < memory_region->receiver_count);
50 CHECK(memory_region->receivers[receiver_index]
51 .composite_memory_region_offset != 0);
52
53 return memory_region->receivers[receiver_index]
54 .composite_memory_region_offset +
55 sizeof(struct ffa_composite_memory_region);
56}
57
58/**
J-Alves917d2f22020-10-30 18:39:30 +000059 * Extracts the index from a memory handle allocated by Hafnium's current world.
60 */
61uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle)
62{
63 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
64}
65
66/**
Karl Meakin52cdfe72023-06-30 14:49:10 +010067 * Initialises the next available `struct ffa_memory_share_state`. If `handle`
68 * is `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle,
69 * otherwise uses the provided handle which is assumed to be globally unique.
Andrew Walbranca808b12020-05-15 17:22:28 +010070 *
Karl Meakin52cdfe72023-06-30 14:49:10 +010071 * Returns a pointer to the allocated `ffa_memory_share_state` on success or
72 * `NULL` if none are available.
Andrew Walbranca808b12020-05-15 17:22:28 +010073 */
Karl Meakin52cdfe72023-06-30 14:49:10 +010074struct ffa_memory_share_state *allocate_share_state(
75 struct share_states_locked share_states, uint32_t share_func,
76 struct ffa_memory_region *memory_region, uint32_t fragment_length,
77 ffa_memory_handle_t handle)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000078{
Daniel Boulbya2f8c662021-11-26 17:52:53 +000079 assert(share_states.share_states != NULL);
80 assert(memory_region != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000081
Karl Meakin52cdfe72023-06-30 14:49:10 +010082 for (uint64_t i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +010083 if (share_states.share_states[i].share_func == 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010084 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +010085 &share_states.share_states[i];
86 struct ffa_composite_memory_region *composite =
87 ffa_memory_region_get_composite(memory_region,
88 0);
89
90 if (handle == FFA_MEMORY_HANDLE_INVALID) {
J-Alvesee68c542020-10-29 17:48:20 +000091 memory_region->handle =
Olivier Deprez55a189e2021-06-09 15:45:27 +020092 plat_ffa_memory_handle_make(i);
Andrew Walbranca808b12020-05-15 17:22:28 +010093 } else {
J-Alvesee68c542020-10-29 17:48:20 +000094 memory_region->handle = handle;
Andrew Walbranca808b12020-05-15 17:22:28 +010095 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000096 allocated_state->share_func = share_func;
97 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +010098 allocated_state->fragment_count = 1;
99 allocated_state->fragments[0] = composite->constituents;
100 allocated_state->fragment_constituent_counts[0] =
101 (fragment_length -
102 ffa_composite_constituent_offset(memory_region,
103 0)) /
104 sizeof(struct ffa_memory_region_constituent);
105 allocated_state->sending_complete = false;
Karl Meakin52cdfe72023-06-30 14:49:10 +0100106 for (uint32_t j = 0; j < MAX_MEM_SHARE_RECIPIENTS;
107 ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100108 allocated_state->retrieved_fragment_count[j] =
109 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000110 }
Karl Meakin52cdfe72023-06-30 14:49:10 +0100111 return allocated_state;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000112 }
113 }
114
Karl Meakin52cdfe72023-06-30 14:49:10 +0100115 return NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000116}
117
118/** Locks the share states lock. */
119struct share_states_locked share_states_lock(void)
120{
121 sl_lock(&share_states_lock_instance);
122
123 return (struct share_states_locked){.share_states = share_states};
124}
125
126/** Unlocks the share states lock. */
J-Alves66652252022-07-06 09:49:51 +0100127void share_states_unlock(struct share_states_locked *share_states)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000128{
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000129 assert(share_states->share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000130 share_states->share_states = NULL;
131 sl_unlock(&share_states_lock_instance);
132}
133
134/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100135 * If the given handle is a valid handle for an allocated share state then
Karl Meakin4a2854a2023-06-30 16:26:52 +0100136 * returns a pointer to the share state. Otherwise returns NULL.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000137 */
Karl Meakin4a2854a2023-06-30 16:26:52 +0100138struct ffa_memory_share_state *get_share_state(
139 struct share_states_locked share_states, ffa_memory_handle_t handle)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000140{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100141 struct ffa_memory_share_state *share_state;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000142
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000143 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100144
145 /*
146 * First look for a share_state allocated by us, in which case the
147 * handle is based on the index.
148 */
Olivier Deprez55a189e2021-06-09 15:45:27 +0200149 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100150 uint64_t index = ffa_memory_handle_get_index(handle);
151
Andrew Walbranca808b12020-05-15 17:22:28 +0100152 if (index < MAX_MEM_SHARES) {
153 share_state = &share_states.share_states[index];
154 if (share_state->share_func != 0) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100155 return share_state;
Andrew Walbranca808b12020-05-15 17:22:28 +0100156 }
157 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000158 }
159
Andrew Walbranca808b12020-05-15 17:22:28 +0100160 /* Fall back to a linear scan. */
Karl Meakin4a2854a2023-06-30 16:26:52 +0100161 for (uint64_t index = 0; index < MAX_MEM_SHARES; ++index) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100162 share_state = &share_states.share_states[index];
J-Alvesee68c542020-10-29 17:48:20 +0000163 if (share_state->memory_region != NULL &&
164 share_state->memory_region->handle == handle &&
Andrew Walbranca808b12020-05-15 17:22:28 +0100165 share_state->share_func != 0) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100166 return share_state;
Andrew Walbranca808b12020-05-15 17:22:28 +0100167 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000168 }
169
Karl Meakin4a2854a2023-06-30 16:26:52 +0100170 return NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000171}
172
173/** Marks a share state as unallocated. */
J-Alvesfdd29272022-07-19 13:16:31 +0100174void share_state_free(struct share_states_locked share_states,
175 struct ffa_memory_share_state *share_state,
176 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000177{
Andrew Walbranca808b12020-05-15 17:22:28 +0100178 uint32_t i;
179
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000180 assert(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000181 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100182 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000183 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100184 /*
185 * First fragment is part of the same page as the `memory_region`, so it
186 * doesn't need to be freed separately.
187 */
188 share_state->fragments[0] = NULL;
189 share_state->fragment_constituent_counts[0] = 0;
190 for (i = 1; i < share_state->fragment_count; ++i) {
191 mpool_free(page_pool, share_state->fragments[i]);
192 share_state->fragments[i] = NULL;
193 share_state->fragment_constituent_counts[i] = 0;
194 }
195 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000196 share_state->memory_region = NULL;
J-Alvesa9cd7e32022-07-01 13:49:33 +0100197 share_state->hypervisor_fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000198}
199
Andrew Walbranca808b12020-05-15 17:22:28 +0100200/** Checks whether the given share state has been fully sent. */
J-Alvesfdd29272022-07-19 13:16:31 +0100201bool share_state_sending_complete(struct share_states_locked share_states,
202 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000203{
Andrew Walbranca808b12020-05-15 17:22:28 +0100204 struct ffa_composite_memory_region *composite;
205 uint32_t expected_constituent_count;
206 uint32_t fragment_constituent_count_total = 0;
207 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000208
Andrew Walbranca808b12020-05-15 17:22:28 +0100209 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000210 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100211
212 /*
213 * Share state must already be valid, or it's not possible to get hold
214 * of it.
215 */
216 CHECK(share_state->memory_region != NULL &&
217 share_state->share_func != 0);
218
219 composite =
220 ffa_memory_region_get_composite(share_state->memory_region, 0);
221 expected_constituent_count = composite->constituent_count;
222 for (i = 0; i < share_state->fragment_count; ++i) {
223 fragment_constituent_count_total +=
224 share_state->fragment_constituent_counts[i];
225 }
226 dlog_verbose(
227 "Checking completion: constituent count %d/%d from %d "
228 "fragments.\n",
229 fragment_constituent_count_total, expected_constituent_count,
230 share_state->fragment_count);
231
232 return fragment_constituent_count_total == expected_constituent_count;
233}
234
235/**
236 * Calculates the offset of the next fragment expected for the given share
237 * state.
238 */
J-Alvesfdd29272022-07-19 13:16:31 +0100239uint32_t share_state_next_fragment_offset(
Andrew Walbranca808b12020-05-15 17:22:28 +0100240 struct share_states_locked share_states,
241 struct ffa_memory_share_state *share_state)
242{
243 uint32_t next_fragment_offset;
244 uint32_t i;
245
246 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000247 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100248
249 next_fragment_offset =
250 ffa_composite_constituent_offset(share_state->memory_region, 0);
251 for (i = 0; i < share_state->fragment_count; ++i) {
252 next_fragment_offset +=
253 share_state->fragment_constituent_counts[i] *
254 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000255 }
256
Andrew Walbranca808b12020-05-15 17:22:28 +0100257 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000258}
259
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100260static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000261{
262 uint32_t i;
263
264 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
265 return;
266 }
267
Olivier Deprez935e1b12020-12-22 18:01:29 +0100268 dlog("from VM %#x, attributes %#x, flags %#x, tag %u, to "
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100269 "%u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100270 "recipients [",
271 memory_region->sender, memory_region->attributes,
Olivier Deprez935e1b12020-12-22 18:01:29 +0100272 memory_region->flags, memory_region->tag,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100273 memory_region->receiver_count);
274 for (i = 0; i < memory_region->receiver_count; ++i) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000275 if (i != 0) {
276 dlog(", ");
277 }
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100278 dlog("VM %#x: %#x (offset %u)",
Andrew Walbrana65a1322020-04-06 19:32:32 +0100279 memory_region->receivers[i].receiver_permissions.receiver,
280 memory_region->receivers[i]
281 .receiver_permissions.permissions,
282 memory_region->receivers[i]
283 .composite_memory_region_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000284 }
285 dlog("]");
286}
287
J-Alves66652252022-07-06 09:49:51 +0100288void dump_share_states(void)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000289{
290 uint32_t i;
291
292 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
293 return;
294 }
295
296 dlog("Current share states:\n");
297 sl_lock(&share_states_lock_instance);
298 for (i = 0; i < MAX_MEM_SHARES; ++i) {
299 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000300 switch (share_states[i].share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100301 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000302 dlog("SHARE");
303 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100304 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000305 dlog("LEND");
306 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100307 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000308 dlog("DONATE");
309 break;
310 default:
311 dlog("invalid share_func %#x",
312 share_states[i].share_func);
313 }
Olivier Deprez935e1b12020-12-22 18:01:29 +0100314 dlog(" %#x (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000315 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100316 if (share_states[i].sending_complete) {
317 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000318 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100319 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000320 }
J-Alves2a0d2882020-10-29 14:49:50 +0000321 dlog(" with %d fragments, %d retrieved, "
322 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100323 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000324 share_states[i].retrieved_fragment_count[0],
325 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000326 }
327 }
328 sl_unlock(&share_states_lock_instance);
329}
330
Andrew Walbran475c1452020-02-07 13:22:22 +0000331/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100332static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100333 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000334{
335 uint32_t mode = 0;
336
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100337 switch (ffa_get_data_access_attr(permissions)) {
338 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000339 mode = MM_MODE_R;
340 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100341 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000342 mode = MM_MODE_R | MM_MODE_W;
343 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100344 case FFA_DATA_ACCESS_NOT_SPECIFIED:
345 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
346 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100347 case FFA_DATA_ACCESS_RESERVED:
348 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100349 }
350
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100351 switch (ffa_get_instruction_access_attr(permissions)) {
352 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000353 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100354 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100355 mode |= MM_MODE_X;
356 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100357 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
358 mode |= (default_mode & MM_MODE_X);
359 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100360 case FFA_INSTRUCTION_ACCESS_RESERVED:
361 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000362 }
363
Olivier Deprez878bd5b2021-04-15 19:05:10 +0200364 /* Set the security state bit if necessary. */
365 if ((default_mode & plat_ffa_other_world_mode()) != 0) {
366 mode |= plat_ffa_other_world_mode();
367 }
368
Andrew Walbran475c1452020-02-07 13:22:22 +0000369 return mode;
370}
371
Jose Marinho75509b42019-04-09 09:34:59 +0100372/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000373 * Get the current mode in the stage-2 page table of the given vm of all the
374 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100375 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100376 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100377static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000378 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100379 struct ffa_memory_region_constituent **fragments,
380 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100381{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100382 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100383 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100384
Andrew Walbranca808b12020-05-15 17:22:28 +0100385 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100386 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000387 * Fail if there are no constituents. Otherwise we would get an
388 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100389 */
Karl Meakin5df422c2023-07-11 17:31:38 +0100390 dlog_verbose("%s: no constituents\n", __func__);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100391 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100392 }
393
Andrew Walbranca808b12020-05-15 17:22:28 +0100394 for (i = 0; i < fragment_count; ++i) {
395 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
396 ipaddr_t begin = ipa_init(fragments[i][j].address);
397 size_t size = fragments[i][j].page_count * PAGE_SIZE;
398 ipaddr_t end = ipa_add(begin, size);
399 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100400
Andrew Walbranca808b12020-05-15 17:22:28 +0100401 /* Fail if addresses are not page-aligned. */
402 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
403 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100404 dlog_verbose("%s: addresses not page-aligned\n",
405 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +0100406 return ffa_error(FFA_INVALID_PARAMETERS);
407 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100408
Andrew Walbranca808b12020-05-15 17:22:28 +0100409 /*
410 * Ensure that this constituent memory range is all
411 * mapped with the same mode.
412 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800413 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100414 dlog_verbose(
415 "%s: constituent memory range %#x..%#x "
416 "not mapped with the same mode\n",
417 __func__, begin, end);
Andrew Walbranca808b12020-05-15 17:22:28 +0100418 return ffa_error(FFA_DENIED);
419 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100420
Andrew Walbranca808b12020-05-15 17:22:28 +0100421 /*
422 * Ensure that all constituents are mapped with the same
423 * mode.
424 */
425 if (i == 0) {
426 *orig_mode = current_mode;
427 } else if (current_mode != *orig_mode) {
428 dlog_verbose(
Karl Meakin5df422c2023-07-11 17:31:38 +0100429 "%s: expected mode %#x but was %#x for "
430 "%d pages at %#x.\n",
431 __func__, *orig_mode, current_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100432 fragments[i][j].page_count,
433 ipa_addr(begin));
434 return ffa_error(FFA_DENIED);
435 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100436 }
Jose Marinho75509b42019-04-09 09:34:59 +0100437 }
438
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100439 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000440}
441
442/**
443 * Verify that all pages have the same mode, that the starting mode
444 * constitutes a valid state and obtain the next mode to apply
445 * to the sending VM.
446 *
447 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100448 * 1) FFA_DENIED if a state transition was not found;
449 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100450 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100451 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100452 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100453 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
454 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000455 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100456static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100457 struct vm_locked from, uint32_t share_func,
J-Alves363f5722022-04-25 17:37:37 +0100458 struct ffa_memory_access *receivers, uint32_t receivers_count,
459 uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100460 struct ffa_memory_region_constituent **fragments,
461 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
462 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000463{
464 const uint32_t state_mask =
465 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100466 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000467
Andrew Walbranca808b12020-05-15 17:22:28 +0100468 ret = constituents_get_mode(from, orig_from_mode, fragments,
469 fragment_constituent_counts,
470 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100471 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100472 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100473 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100474 }
475
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000476 /* Ensure the address range is normal memory and not a device. */
J-Alves788b4492023-04-18 14:01:23 +0100477 if ((*orig_from_mode & MM_MODE_D) != 0U) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000478 dlog_verbose("Can't share device memory (mode is %#x).\n",
479 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100480 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000481 }
482
483 /*
484 * Ensure the sender is the owner and has exclusive access to the
485 * memory.
486 */
487 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100488 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100489 }
490
J-Alves363f5722022-04-25 17:37:37 +0100491 assert(receivers != NULL && receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100492
J-Alves363f5722022-04-25 17:37:37 +0100493 for (uint32_t i = 0U; i < receivers_count; i++) {
494 ffa_memory_access_permissions_t permissions =
495 receivers[i].receiver_permissions.permissions;
496 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
497 permissions, *orig_from_mode);
498
J-Alves788b4492023-04-18 14:01:23 +0100499 /*
500 * The assumption is that at this point, the operation from
501 * SP to a receiver VM, should have returned an FFA_ERROR
502 * already.
503 */
504 if (!ffa_is_vm_id(from.vm->id)) {
505 assert(!ffa_is_vm_id(
506 receivers[i].receiver_permissions.receiver));
507 }
508
J-Alves363f5722022-04-25 17:37:37 +0100509 if ((*orig_from_mode & required_from_mode) !=
510 required_from_mode) {
511 dlog_verbose(
512 "Sender tried to send memory with permissions "
J-Alves788b4492023-04-18 14:01:23 +0100513 "which required mode %#x but only had %#x "
514 "itself.\n",
J-Alves363f5722022-04-25 17:37:37 +0100515 required_from_mode, *orig_from_mode);
516 return ffa_error(FFA_DENIED);
517 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000518 }
519
520 /* Find the appropriate new mode. */
521 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000522 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100523 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000524 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100525 break;
526
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100527 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000528 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100529 break;
530
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100531 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000532 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100533 break;
534
Jose Marinho75509b42019-04-09 09:34:59 +0100535 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100536 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100537 }
538
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100539 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000540}
541
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100542static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000543 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100544 struct ffa_memory_region_constituent **fragments,
545 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
546 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000547{
548 const uint32_t state_mask =
549 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
550 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100551 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000552
Andrew Walbranca808b12020-05-15 17:22:28 +0100553 ret = constituents_get_mode(from, orig_from_mode, fragments,
554 fragment_constituent_counts,
555 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100556 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100557 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000558 }
559
560 /* Ensure the address range is normal memory and not a device. */
561 if (*orig_from_mode & MM_MODE_D) {
562 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
563 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100564 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000565 }
566
567 /*
568 * Ensure the relinquishing VM is not the owner but has access to the
569 * memory.
570 */
571 orig_from_state = *orig_from_mode & state_mask;
572 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
573 dlog_verbose(
574 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100575 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000576 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100577 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000578 }
579
580 /* Find the appropriate new mode. */
581 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
582
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100583 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000584}
585
586/**
587 * Verify that all pages have the same mode, that the starting mode
588 * constitutes a valid state and obtain the next mode to apply
589 * to the retrieving VM.
590 *
591 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100592 * 1) FFA_DENIED if a state transition was not found;
593 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100594 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100595 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100596 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100597 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
598 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000599 */
J-Alvesfc19b372022-07-06 12:17:35 +0100600struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000601 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100602 struct ffa_memory_region_constituent **fragments,
603 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
604 uint32_t memory_to_attributes, uint32_t *to_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000605{
606 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100607 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000608
Andrew Walbranca808b12020-05-15 17:22:28 +0100609 ret = constituents_get_mode(to, &orig_to_mode, fragments,
610 fragment_constituent_counts,
611 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100612 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100613 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100614 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000615 }
616
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100617 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000618 /*
619 * If the original ffa memory send call has been processed
620 * successfully, it is expected the orig_to_mode would overlay
621 * with `state_mask`, as a result of the function
622 * `ffa_send_check_transition`.
623 */
J-Alves59ed0042022-07-28 18:26:41 +0100624 if (vm_id_is_current_world(to.vm->id)) {
625 assert((orig_to_mode &
626 (MM_MODE_INVALID | MM_MODE_UNOWNED |
627 MM_MODE_SHARED)) != 0U);
628 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000629 } else {
630 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +0100631 * If the retriever is from virtual FF-A instance:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000632 * Ensure the retriever has the expected state. We don't care
633 * about the MM_MODE_SHARED bit; either with or without it set
634 * are both valid representations of the !O-NA state.
635 */
J-Alvesa9cd7e32022-07-01 13:49:33 +0100636 if (vm_id_is_current_world(to.vm->id) &&
637 to.vm->id != HF_PRIMARY_VM_ID &&
638 (orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
639 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100640 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000641 }
642 }
643
644 /* Find the appropriate new mode. */
645 *to_mode = memory_to_attributes;
646 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100647 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000648 *to_mode |= 0;
649 break;
650
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100651 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000652 *to_mode |= MM_MODE_UNOWNED;
653 break;
654
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100655 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000656 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
657 break;
658
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100659 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000660 *to_mode |= 0;
661 break;
662
663 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100664 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100665 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000666 }
667
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100668 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100669}
Jose Marinho09b1db82019-08-08 09:16:59 +0100670
671/**
672 * Updates a VM's page table such that the given set of physical address ranges
673 * are mapped in the address space at the corresponding address ranges, in the
674 * mode provided.
675 *
676 * If commit is false, the page tables will be allocated from the mpool but no
677 * mappings will actually be updated. This function must always be called first
678 * with commit false to check that it will succeed before calling with commit
679 * true, to avoid leaving the page table in a half-updated state. To make a
680 * series of changes atomically you can call them all with commit false before
681 * calling them all with commit true.
682 *
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700683 * vm_ptable_defrag should always be called after a series of page table
684 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +0100685 *
686 * Returns true on success, or false if the update failed and no changes were
687 * made to memory mappings.
688 */
J-Alves66652252022-07-06 09:49:51 +0100689bool ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000690 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100691 struct ffa_memory_region_constituent **fragments,
692 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby4dd3f532021-09-21 09:57:08 +0100693 uint32_t mode, struct mpool *ppool, bool commit)
Jose Marinho09b1db82019-08-08 09:16:59 +0100694{
Andrew Walbranca808b12020-05-15 17:22:28 +0100695 uint32_t i;
696 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100697
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700698 if (vm_locked.vm->el0_partition) {
699 mode |= MM_MODE_USER | MM_MODE_NG;
700 }
701
Andrew Walbranca808b12020-05-15 17:22:28 +0100702 /* Iterate over the memory region constituents within each fragment. */
703 for (i = 0; i < fragment_count; ++i) {
704 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
705 size_t size = fragments[i][j].page_count * PAGE_SIZE;
706 paddr_t pa_begin =
707 pa_from_ipa(ipa_init(fragments[i][j].address));
708 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +0200709 uint32_t pa_bits =
710 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +0100711
712 /*
713 * Ensure the requested region falls into system's PA
714 * range.
715 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +0200716 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
717 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +0100718 dlog_error("Region is outside of PA Range\n");
719 return false;
720 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100721
722 if (commit) {
723 vm_identity_commit(vm_locked, pa_begin, pa_end,
724 mode, ppool, NULL);
725 } else if (!vm_identity_prepare(vm_locked, pa_begin,
726 pa_end, mode, ppool)) {
727 return false;
728 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100729 }
730 }
731
732 return true;
733}
734
735/**
736 * Clears a region of physical memory by overwriting it with zeros. The data is
737 * flushed from the cache so the memory has been cleared across the system.
738 */
J-Alves7db32002021-12-14 14:44:50 +0000739static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
740 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +0100741{
742 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000743 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100744 * global mapping of the whole range. Such an approach will limit
745 * the changes to stage-1 tables and will allow only local
746 * invalidation.
747 */
748 bool ret;
749 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +0000750 void *ptr = mm_identity_map(stage1_locked, begin, end,
751 MM_MODE_W | (extra_mode_attributes &
752 plat_ffa_other_world_mode()),
753 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100754 size_t size = pa_difference(begin, end);
755
756 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100757 goto fail;
758 }
759
760 memset_s(ptr, size, 0, size);
761 arch_mm_flush_dcache(ptr, size);
762 mm_unmap(stage1_locked, begin, end, ppool);
763
764 ret = true;
765 goto out;
766
767fail:
768 ret = false;
769
770out:
771 mm_unlock_stage1(&stage1_locked);
772
773 return ret;
774}
775
776/**
777 * Clears a region of physical memory by overwriting it with zeros. The data is
778 * flushed from the cache so the memory has been cleared across the system.
779 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100780static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +0000781 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100782 struct ffa_memory_region_constituent **fragments,
783 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
784 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100785{
786 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +0100787 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100788 bool ret = false;
789
790 /*
791 * Create a local pool so any freed memory can't be used by another
792 * thread. This is to ensure each constituent that is mapped can be
793 * unmapped again afterwards.
794 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000795 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100796
Andrew Walbranca808b12020-05-15 17:22:28 +0100797 /* Iterate over the memory region constituents within each fragment. */
798 for (i = 0; i < fragment_count; ++i) {
799 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100800
J-Alves8457f932023-10-11 16:41:45 +0100801 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100802 size_t size = fragments[i][j].page_count * PAGE_SIZE;
803 paddr_t begin =
804 pa_from_ipa(ipa_init(fragments[i][j].address));
805 paddr_t end = pa_add(begin, size);
806
J-Alves7db32002021-12-14 14:44:50 +0000807 if (!clear_memory(begin, end, &local_page_pool,
808 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100809 /*
810 * api_clear_memory will defrag on failure, so
811 * no need to do it here.
812 */
813 goto out;
814 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100815 }
816 }
817
Jose Marinho09b1db82019-08-08 09:16:59 +0100818 ret = true;
819
820out:
821 mpool_fini(&local_page_pool);
822 return ret;
823}
824
J-Alves5952d942022-12-22 16:03:00 +0000825static bool is_memory_range_within(ipaddr_t begin, ipaddr_t end,
826 ipaddr_t in_begin, ipaddr_t in_end)
827{
828 return (ipa_addr(begin) >= ipa_addr(in_begin) &&
829 ipa_addr(begin) < ipa_addr(in_end)) ||
830 (ipa_addr(end) <= ipa_addr(in_end) &&
831 ipa_addr(end) > ipa_addr(in_begin));
832}
833
834/**
835 * Receives a memory range and looks for overlaps with the remainder
836 * constituents of the memory share/lend/donate operation. Assumes they are
837 * passed in order to avoid having to loop over all the elements at each call.
838 * The function only compares the received memory ranges with those that follow
839 * within the same fragment, and subsequent fragments from the same operation.
840 */
841static bool ffa_memory_check_overlap(
842 struct ffa_memory_region_constituent **fragments,
843 const uint32_t *fragment_constituent_counts,
844 const uint32_t fragment_count, const uint32_t current_fragment,
845 const uint32_t current_constituent)
846{
847 uint32_t i = current_fragment;
848 uint32_t j = current_constituent;
849 ipaddr_t current_begin = ipa_init(fragments[i][j].address);
850 const uint32_t current_page_count = fragments[i][j].page_count;
851 size_t current_size = current_page_count * PAGE_SIZE;
852 ipaddr_t current_end = ipa_add(current_begin, current_size - 1);
853
854 if (current_size == 0 ||
855 current_size > UINT64_MAX - ipa_addr(current_begin)) {
856 dlog_verbose("Invalid page count. Addr: %x page_count: %x\n",
857 current_begin, current_page_count);
858 return false;
859 }
860
861 for (; i < fragment_count; i++) {
862 j = (i == current_fragment) ? j + 1 : 0;
863
864 for (; j < fragment_constituent_counts[i]; j++) {
865 ipaddr_t begin = ipa_init(fragments[i][j].address);
866 const uint32_t page_count = fragments[i][j].page_count;
867 size_t size = page_count * PAGE_SIZE;
868 ipaddr_t end = ipa_add(begin, size - 1);
869
870 if (size == 0 || size > UINT64_MAX - ipa_addr(begin)) {
871 dlog_verbose(
872 "Invalid page count. Addr: %x "
873 "page_count: %x\n",
874 begin, page_count);
875 return false;
876 }
877
878 /*
879 * Check if current ranges is within begin and end, as
880 * well as the reverse. This should help optimize the
881 * loop, and reduce the number of iterations.
882 */
883 if (is_memory_range_within(begin, end, current_begin,
884 current_end) ||
885 is_memory_range_within(current_begin, current_end,
886 begin, end)) {
887 dlog_verbose(
888 "Overlapping memory ranges: %#x - %#x "
889 "with %#x - %#x\n",
890 ipa_addr(begin), ipa_addr(end),
891 ipa_addr(current_begin),
892 ipa_addr(current_end));
893 return true;
894 }
895 }
896 }
897
898 return false;
899}
900
Jose Marinho09b1db82019-08-08 09:16:59 +0100901/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000902 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +0100903 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000904 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +0100905 *
906 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000907 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100908 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +0100909 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100910 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
911 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100912 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +0100913 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100914 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +0100915 */
J-Alves66652252022-07-06 09:49:51 +0100916struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000917 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100918 struct ffa_memory_region_constituent **fragments,
919 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves8f11cde2022-12-21 16:18:22 +0000920 uint32_t composite_total_page_count, uint32_t share_func,
921 struct ffa_memory_access *receivers, uint32_t receivers_count,
922 struct mpool *page_pool, bool clear, uint32_t *orig_from_mode_ret)
Jose Marinho09b1db82019-08-08 09:16:59 +0100923{
Andrew Walbranca808b12020-05-15 17:22:28 +0100924 uint32_t i;
J-Alves8f11cde2022-12-21 16:18:22 +0000925 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100926 uint32_t orig_from_mode;
927 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +0100928 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100929 struct ffa_value ret;
J-Alves8f11cde2022-12-21 16:18:22 +0000930 uint32_t constituents_total_page_count = 0;
Jose Marinho09b1db82019-08-08 09:16:59 +0100931
932 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +0100933 * Make sure constituents are properly aligned to a 64-bit boundary. If
934 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100935 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100936 for (i = 0; i < fragment_count; ++i) {
937 if (!is_aligned(fragments[i], 8)) {
938 dlog_verbose("Constituents not aligned.\n");
939 return ffa_error(FFA_INVALID_PARAMETERS);
940 }
J-Alves8f11cde2022-12-21 16:18:22 +0000941 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
942 constituents_total_page_count +=
943 fragments[i][j].page_count;
J-Alves5952d942022-12-22 16:03:00 +0000944 if (ffa_memory_check_overlap(
945 fragments, fragment_constituent_counts,
946 fragment_count, i, j)) {
947 return ffa_error(FFA_INVALID_PARAMETERS);
948 }
J-Alves8f11cde2022-12-21 16:18:22 +0000949 }
950 }
951
952 if (constituents_total_page_count != composite_total_page_count) {
953 dlog_verbose(
954 "Composite page count differs from calculated page "
955 "count from constituents.\n");
956 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +0100957 }
958
959 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000960 * Check if the state transition is lawful for the sender, ensure that
961 * all constituents of a memory region being shared are at the same
962 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +0100963 */
J-Alves363f5722022-04-25 17:37:37 +0100964 ret = ffa_send_check_transition(from_locked, share_func, receivers,
965 receivers_count, &orig_from_mode,
966 fragments, fragment_constituent_counts,
Andrew Walbranca808b12020-05-15 17:22:28 +0100967 fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100968 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100969 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100970 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100971 }
972
Andrew Walbran37c574e2020-06-03 11:45:46 +0100973 if (orig_from_mode_ret != NULL) {
974 *orig_from_mode_ret = orig_from_mode;
975 }
976
Jose Marinho09b1db82019-08-08 09:16:59 +0100977 /*
978 * Create a local pool so any freed memory can't be used by another
979 * thread. This is to ensure the original mapping can be restored if the
980 * clear fails.
981 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000982 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100983
984 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000985 * First reserve all required memory for the new page table entries
986 * without committing, to make sure the entire operation will succeed
987 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +0100988 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100989 if (!ffa_region_group_identity_map(
990 from_locked, fragments, fragment_constituent_counts,
991 fragment_count, from_mode, page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100992 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100993 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100994 goto out;
995 }
996
997 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000998 * Update the mapping for the sender. This won't allocate because the
999 * transaction was already prepared above, but may free pages in the
1000 * case that a whole block is being unmapped that was previously
1001 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +01001002 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001003 CHECK(ffa_region_group_identity_map(
1004 from_locked, fragments, fragment_constituent_counts,
1005 fragment_count, from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001006
1007 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001008 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001009 !ffa_clear_memory_constituents(orig_from_mode, fragments,
1010 fragment_constituent_counts,
1011 fragment_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001012 /*
1013 * On failure, roll back by returning memory to the sender. This
1014 * may allocate pages which were previously freed into
1015 * `local_page_pool` by the call above, but will never allocate
1016 * more pages than that so can never fail.
1017 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001018 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001019 from_locked, fragments, fragment_constituent_counts,
1020 fragment_count, orig_from_mode, &local_page_pool,
1021 true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001022
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001023 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +01001024 goto out;
1025 }
1026
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001027 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001028
1029out:
1030 mpool_fini(&local_page_pool);
1031
1032 /*
1033 * Tidy up the page table by reclaiming failed mappings (if there was an
1034 * error) or merging entries into blocks where possible (on success).
1035 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001036 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001037
1038 return ret;
1039}
1040
1041/**
1042 * Validates and maps memory shared from one VM to another.
1043 *
1044 * This function requires the calling context to hold the <to> lock.
1045 *
1046 * Returns:
1047 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001048 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001049 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001050 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001051 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001052 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001053 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001054struct ffa_value ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01001055 struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001056 struct ffa_memory_region_constituent **fragments,
1057 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves26483382023-04-20 12:01:49 +01001058 uint32_t sender_orig_mode, uint32_t share_func, bool clear,
Andrew Walbranca808b12020-05-15 17:22:28 +01001059 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001060{
Andrew Walbranca808b12020-05-15 17:22:28 +01001061 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001062 uint32_t to_mode;
1063 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001064 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001065
1066 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001067 * Make sure constituents are properly aligned to a 64-bit boundary. If
1068 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001069 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001070 for (i = 0; i < fragment_count; ++i) {
1071 if (!is_aligned(fragments[i], 8)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001072 dlog_verbose("Fragment not properly aligned.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +01001073 return ffa_error(FFA_INVALID_PARAMETERS);
1074 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001075 }
1076
1077 /*
1078 * Check if the state transition is lawful for the recipient, and ensure
1079 * that all constituents of the memory region being retrieved are at the
1080 * same state.
1081 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001082 ret = ffa_retrieve_check_transition(
1083 to_locked, share_func, fragments, fragment_constituent_counts,
J-Alves26483382023-04-20 12:01:49 +01001084 fragment_count, sender_orig_mode, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001085 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001086 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001087 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001088 }
1089
1090 /*
1091 * Create a local pool so any freed memory can't be used by another
1092 * thread. This is to ensure the original mapping can be restored if the
1093 * clear fails.
1094 */
1095 mpool_init_with_fallback(&local_page_pool, page_pool);
1096
1097 /*
1098 * First reserve all required memory for the new page table entries in
1099 * the recipient page tables without committing, to make sure the entire
1100 * operation will succeed without exhausting the page pool.
1101 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001102 if (!ffa_region_group_identity_map(
1103 to_locked, fragments, fragment_constituent_counts,
1104 fragment_count, to_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001105 /* TODO: partial defrag of failed range. */
1106 dlog_verbose(
1107 "Insufficient memory to update recipient page "
1108 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001109 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001110 goto out;
1111 }
1112
1113 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001114 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001115 !ffa_clear_memory_constituents(sender_orig_mode, fragments,
1116 fragment_constituent_counts,
1117 fragment_count, page_pool)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001118 dlog_verbose("Couldn't clear constituents.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001119 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001120 goto out;
1121 }
1122
Jose Marinho09b1db82019-08-08 09:16:59 +01001123 /*
1124 * Complete the transfer by mapping the memory into the recipient. This
1125 * won't allocate because the transaction was already prepared above, so
1126 * it doesn't need to use the `local_page_pool`.
1127 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001128 CHECK(ffa_region_group_identity_map(
1129 to_locked, fragments, fragment_constituent_counts,
1130 fragment_count, to_mode, page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001131
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001132 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001133
1134out:
1135 mpool_fini(&local_page_pool);
1136
1137 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001138 * Tidy up the page table by reclaiming failed mappings (if there was an
1139 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001140 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001141 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001142
1143 return ret;
1144}
1145
Andrew Walbran996d1d12020-05-27 14:08:43 +01001146static struct ffa_value ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01001147 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001148 struct ffa_memory_region_constituent **fragments,
1149 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1150 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001151{
1152 uint32_t orig_from_mode;
1153 uint32_t from_mode;
1154 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001155 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001156
Andrew Walbranca808b12020-05-15 17:22:28 +01001157 ret = ffa_relinquish_check_transition(
1158 from_locked, &orig_from_mode, fragments,
1159 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001160 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001161 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001162 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001163 }
1164
1165 /*
1166 * Create a local pool so any freed memory can't be used by another
1167 * thread. This is to ensure the original mapping can be restored if the
1168 * clear fails.
1169 */
1170 mpool_init_with_fallback(&local_page_pool, page_pool);
1171
1172 /*
1173 * First reserve all required memory for the new page table entries
1174 * without committing, to make sure the entire operation will succeed
1175 * without exhausting the page pool.
1176 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001177 if (!ffa_region_group_identity_map(
1178 from_locked, fragments, fragment_constituent_counts,
1179 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001180 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001181 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001182 goto out;
1183 }
1184
1185 /*
1186 * Update the mapping for the sender. This won't allocate because the
1187 * transaction was already prepared above, but may free pages in the
1188 * case that a whole block is being unmapped that was previously
1189 * partially mapped.
1190 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001191 CHECK(ffa_region_group_identity_map(
1192 from_locked, fragments, fragment_constituent_counts,
1193 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001194
1195 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001196 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001197 !ffa_clear_memory_constituents(orig_from_mode, fragments,
1198 fragment_constituent_counts,
1199 fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001200 /*
1201 * On failure, roll back by returning memory to the sender. This
1202 * may allocate pages which were previously freed into
1203 * `local_page_pool` by the call above, but will never allocate
1204 * more pages than that so can never fail.
1205 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001206 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001207 from_locked, fragments, fragment_constituent_counts,
1208 fragment_count, orig_from_mode, &local_page_pool,
1209 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001210
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001211 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001212 goto out;
1213 }
1214
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001215 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001216
1217out:
1218 mpool_fini(&local_page_pool);
1219
1220 /*
1221 * Tidy up the page table by reclaiming failed mappings (if there was an
1222 * error) or merging entries into blocks where possible (on success).
1223 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001224 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001225
1226 return ret;
1227}
1228
1229/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001230 * Complete a memory sending operation by checking that it is valid, updating
1231 * the sender page table, and then either marking the share state as having
1232 * completed sending (on success) or freeing it (on failure).
1233 *
1234 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1235 */
J-Alvesfdd29272022-07-19 13:16:31 +01001236struct ffa_value ffa_memory_send_complete(
Andrew Walbranca808b12020-05-15 17:22:28 +01001237 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001238 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1239 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001240{
1241 struct ffa_memory_region *memory_region = share_state->memory_region;
J-Alves8f11cde2022-12-21 16:18:22 +00001242 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01001243 struct ffa_value ret;
1244
1245 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001246 assert(share_states.share_states != NULL);
J-Alves8f11cde2022-12-21 16:18:22 +00001247 assert(memory_region != NULL);
1248 composite = ffa_memory_region_get_composite(memory_region, 0);
1249 assert(composite != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001250
1251 /* Check that state is valid in sender page table and update. */
1252 ret = ffa_send_check_update(
1253 from_locked, share_state->fragments,
1254 share_state->fragment_constituent_counts,
J-Alves8f11cde2022-12-21 16:18:22 +00001255 share_state->fragment_count, composite->page_count,
1256 share_state->share_func, memory_region->receivers,
1257 memory_region->receiver_count, page_pool,
1258 memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001259 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001260 if (ret.func != FFA_SUCCESS_32) {
1261 /*
1262 * Free share state, it failed to send so it can't be retrieved.
1263 */
Karl Meakin4cec5e82023-06-30 16:30:22 +01001264 dlog_verbose("%s: failed to send check update: %s(%s)\n",
1265 __func__, ffa_func_name(ret.func),
1266 ffa_error_name(ffa_error_code(ret)));
Andrew Walbranca808b12020-05-15 17:22:28 +01001267 share_state_free(share_states, share_state, page_pool);
1268 return ret;
1269 }
1270
1271 share_state->sending_complete = true;
Karl Meakin4cec5e82023-06-30 16:30:22 +01001272 dlog_verbose("%s: marked sending complete.\n", __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001273
J-Alvesee68c542020-10-29 17:48:20 +00001274 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001275}
1276
1277/**
Federico Recanatia98603a2021-12-20 18:04:03 +01001278 * Check that the memory attributes match Hafnium expectations:
1279 * Normal Memory, Inner shareable, Write-Back Read-Allocate
1280 * Write-Allocate Cacheable.
1281 */
1282static struct ffa_value ffa_memory_attributes_validate(
J-Alves7a99d0d2023-02-08 13:49:48 +00001283 ffa_memory_attributes_t attributes)
Federico Recanatia98603a2021-12-20 18:04:03 +01001284{
1285 enum ffa_memory_type memory_type;
1286 enum ffa_memory_cacheability cacheability;
1287 enum ffa_memory_shareability shareability;
1288
1289 memory_type = ffa_get_memory_type_attr(attributes);
1290 if (memory_type != FFA_MEMORY_NORMAL_MEM) {
1291 dlog_verbose("Invalid memory type %#x, expected %#x.\n",
1292 memory_type, FFA_MEMORY_NORMAL_MEM);
Federico Recanati3d953f32022-02-17 09:31:29 +01001293 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001294 }
1295
1296 cacheability = ffa_get_memory_cacheability_attr(attributes);
1297 if (cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1298 dlog_verbose("Invalid cacheability %#x, expected %#x.\n",
1299 cacheability, FFA_MEMORY_CACHE_WRITE_BACK);
Federico Recanati3d953f32022-02-17 09:31:29 +01001300 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001301 }
1302
1303 shareability = ffa_get_memory_shareability_attr(attributes);
1304 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
1305 dlog_verbose("Invalid shareability %#x, expected #%x.\n",
1306 shareability, FFA_MEMORY_INNER_SHAREABLE);
Federico Recanati3d953f32022-02-17 09:31:29 +01001307 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001308 }
1309
1310 return (struct ffa_value){.func = FFA_SUCCESS_32};
1311}
1312
1313/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001314 * Check that the given `memory_region` represents a valid memory send request
1315 * of the given `share_func` type, return the clear flag and permissions via the
1316 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001317 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001318 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001319 * not.
1320 */
J-Alves66652252022-07-06 09:49:51 +01001321struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001322 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1323 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001324 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001325{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001326 struct ffa_composite_memory_region *composite;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001327 uint64_t receivers_end;
1328 uint64_t min_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001329 uint32_t composite_memory_region_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001330 uint32_t constituents_start;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001331 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001332 enum ffa_data_access data_access;
1333 enum ffa_instruction_access instruction_access;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001334 enum ffa_memory_security security_state;
Federico Recanatia98603a2021-12-20 18:04:03 +01001335 struct ffa_value ret;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001336 const size_t minimum_first_fragment_length =
1337 (sizeof(struct ffa_memory_region) +
1338 sizeof(struct ffa_memory_access) +
1339 sizeof(struct ffa_composite_memory_region));
1340
1341 if (fragment_length < minimum_first_fragment_length) {
1342 dlog_verbose("Fragment length %u too short (min %u).\n",
1343 (size_t)fragment_length,
1344 minimum_first_fragment_length);
1345 return ffa_error(FFA_INVALID_PARAMETERS);
1346 }
1347
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05001348 static_assert(sizeof(struct ffa_memory_region_constituent) == 16,
1349 "struct ffa_memory_region_constituent must be 16 bytes");
1350 if (!is_aligned(fragment_length,
1351 sizeof(struct ffa_memory_region_constituent)) ||
1352 !is_aligned(memory_share_length,
1353 sizeof(struct ffa_memory_region_constituent))) {
1354 dlog_verbose(
1355 "Fragment length %u or total length %u"
1356 " is not 16-byte aligned.\n",
1357 fragment_length, memory_share_length);
1358 return ffa_error(FFA_INVALID_PARAMETERS);
1359 }
1360
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001361 if (fragment_length > memory_share_length) {
1362 dlog_verbose(
1363 "Fragment length %u greater than total length %u.\n",
1364 (size_t)fragment_length, (size_t)memory_share_length);
1365 return ffa_error(FFA_INVALID_PARAMETERS);
1366 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001367
J-Alves0b6653d2022-04-22 13:17:38 +01001368 assert(memory_region->receivers_offset ==
1369 offsetof(struct ffa_memory_region, receivers));
1370 assert(memory_region->memory_access_desc_size ==
1371 sizeof(struct ffa_memory_access));
1372
J-Alves95df0ef2022-12-07 10:09:48 +00001373 /* The sender must match the caller. */
1374 if ((!vm_id_is_current_world(from_locked.vm->id) &&
1375 vm_id_is_current_world(memory_region->sender)) ||
1376 (vm_id_is_current_world(from_locked.vm->id) &&
1377 memory_region->sender != from_locked.vm->id)) {
1378 dlog_verbose("Invalid memory sender ID.\n");
1379 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001380 }
1381
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001382 if (memory_region->receiver_count <= 0) {
1383 dlog_verbose("No receivers!\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001384 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001385 }
1386
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001387 /*
1388 * Ensure that the composite header is within the memory bounds and
1389 * doesn't overlap the first part of the message. Cast to uint64_t
1390 * to prevent overflow.
1391 */
1392 receivers_end = ((uint64_t)sizeof(struct ffa_memory_access) *
1393 (uint64_t)memory_region->receiver_count) +
1394 sizeof(struct ffa_memory_region);
1395 min_length = receivers_end +
1396 sizeof(struct ffa_composite_memory_region) +
1397 sizeof(struct ffa_memory_region_constituent);
1398 if (min_length > memory_share_length) {
1399 dlog_verbose("Share too short: got %u but minimum is %u.\n",
1400 (size_t)memory_share_length, (size_t)min_length);
1401 return ffa_error(FFA_INVALID_PARAMETERS);
1402 }
1403
1404 composite_memory_region_offset =
1405 memory_region->receivers[0].composite_memory_region_offset;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001406
1407 /*
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001408 * Check that the composite memory region descriptor is after the access
1409 * descriptors, is at least 16-byte aligned, and fits in the first
1410 * fragment.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001411 */
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001412 if ((composite_memory_region_offset < receivers_end) ||
1413 (composite_memory_region_offset % 16 != 0) ||
1414 (composite_memory_region_offset >
1415 fragment_length - sizeof(struct ffa_composite_memory_region))) {
1416 dlog_verbose(
1417 "Invalid composite memory region descriptor offset "
1418 "%u.\n",
1419 (size_t)composite_memory_region_offset);
1420 return ffa_error(FFA_INVALID_PARAMETERS);
1421 }
1422
1423 /*
1424 * Compute the start of the constituent regions. Already checked
1425 * to be not more than fragment_length and thus not more than
1426 * memory_share_length.
1427 */
1428 constituents_start = composite_memory_region_offset +
1429 sizeof(struct ffa_composite_memory_region);
1430 constituents_length = memory_share_length - constituents_start;
1431
1432 /*
1433 * Check that the number of constituents is consistent with the length
1434 * of the constituent region.
1435 */
1436 composite = ffa_memory_region_get_composite(memory_region, 0);
1437 if ((constituents_length %
1438 sizeof(struct ffa_memory_region_constituent) !=
1439 0) ||
1440 ((constituents_length /
1441 sizeof(struct ffa_memory_region_constituent)) !=
1442 composite->constituent_count)) {
1443 dlog_verbose("Invalid length %u or composite offset %u.\n",
1444 (size_t)memory_share_length,
1445 (size_t)composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001446 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001447 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001448 if (fragment_length < memory_share_length &&
1449 fragment_length < HF_MAILBOX_SIZE) {
1450 dlog_warning(
1451 "Initial fragment length %d smaller than mailbox "
1452 "size.\n",
1453 fragment_length);
1454 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001455
Andrew Walbrana65a1322020-04-06 19:32:32 +01001456 /*
1457 * Clear is not allowed for memory sharing, as the sender still has
1458 * access to the memory.
1459 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001460 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1461 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001462 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001463 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001464 }
1465
1466 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001467 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001468 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001469 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001470 }
1471
J-Alves363f5722022-04-25 17:37:37 +01001472 /* Check that the permissions are valid, for each specified receiver. */
1473 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
1474 ffa_memory_access_permissions_t permissions =
1475 memory_region->receivers[i]
1476 .receiver_permissions.permissions;
J-Alves19e20cf2023-08-02 12:48:55 +01001477 ffa_id_t receiver_id = memory_region->receivers[i]
1478 .receiver_permissions.receiver;
J-Alves363f5722022-04-25 17:37:37 +01001479
1480 if (memory_region->sender == receiver_id) {
1481 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001482 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001483 }
Federico Recanati85090c42021-12-15 13:17:54 +01001484
J-Alves363f5722022-04-25 17:37:37 +01001485 for (uint32_t j = i + 1; j < memory_region->receiver_count;
1486 j++) {
1487 if (receiver_id ==
1488 memory_region->receivers[j]
1489 .receiver_permissions.receiver) {
1490 dlog_verbose(
1491 "Repeated receiver(%x) in memory send "
1492 "operation.\n",
1493 memory_region->receivers[j]
1494 .receiver_permissions.receiver);
1495 return ffa_error(FFA_INVALID_PARAMETERS);
1496 }
1497 }
1498
1499 if (composite_memory_region_offset !=
1500 memory_region->receivers[i]
1501 .composite_memory_region_offset) {
1502 dlog_verbose(
1503 "All ffa_memory_access should point to the "
1504 "same composite memory region offset.\n");
1505 return ffa_error(FFA_INVALID_PARAMETERS);
1506 }
1507
1508 data_access = ffa_get_data_access_attr(permissions);
1509 instruction_access =
1510 ffa_get_instruction_access_attr(permissions);
1511 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1512 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
1513 dlog_verbose(
1514 "Reserved value for receiver permissions "
1515 "%#x.\n",
1516 permissions);
1517 return ffa_error(FFA_INVALID_PARAMETERS);
1518 }
1519 if (instruction_access !=
1520 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
1521 dlog_verbose(
1522 "Invalid instruction access permissions %#x "
1523 "for sending memory.\n",
1524 permissions);
1525 return ffa_error(FFA_INVALID_PARAMETERS);
1526 }
1527 if (share_func == FFA_MEM_SHARE_32) {
1528 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1529 dlog_verbose(
1530 "Invalid data access permissions %#x "
1531 "for sharing memory.\n",
1532 permissions);
1533 return ffa_error(FFA_INVALID_PARAMETERS);
1534 }
J-Alves363f5722022-04-25 17:37:37 +01001535 }
1536 if (share_func == FFA_MEM_LEND_32 &&
1537 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1538 dlog_verbose(
1539 "Invalid data access permissions %#x for "
1540 "lending memory.\n",
1541 permissions);
1542 return ffa_error(FFA_INVALID_PARAMETERS);
1543 }
1544
1545 if (share_func == FFA_MEM_DONATE_32 &&
1546 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
1547 dlog_verbose(
1548 "Invalid data access permissions %#x for "
1549 "donating memory.\n",
1550 permissions);
1551 return ffa_error(FFA_INVALID_PARAMETERS);
1552 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001553 }
1554
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001555 /* Memory region attributes NS-Bit MBZ for FFA_MEM_SHARE/LEND/DONATE. */
1556 security_state =
1557 ffa_get_memory_security_attr(memory_region->attributes);
1558 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
1559 dlog_verbose(
1560 "Invalid security state for memory share operation.\n");
1561 return ffa_error(FFA_INVALID_PARAMETERS);
1562 }
1563
Federico Recanatid937f5e2021-12-20 17:38:23 +01001564 /*
J-Alves807794e2022-06-16 13:42:47 +01001565 * If a memory donate or lend with single borrower, the memory type
1566 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01001567 */
J-Alves807794e2022-06-16 13:42:47 +01001568 if (share_func == FFA_MEM_DONATE_32 ||
1569 (share_func == FFA_MEM_LEND_32 &&
1570 memory_region->receiver_count == 1)) {
1571 if (ffa_get_memory_type_attr(memory_region->attributes) !=
1572 FFA_MEMORY_NOT_SPECIFIED_MEM) {
1573 dlog_verbose(
1574 "Memory type shall not be specified by "
1575 "sender.\n");
1576 return ffa_error(FFA_INVALID_PARAMETERS);
1577 }
1578 } else {
1579 /*
1580 * Check that sender's memory attributes match Hafnium
1581 * expectations: Normal Memory, Inner shareable, Write-Back
1582 * Read-Allocate Write-Allocate Cacheable.
1583 */
1584 ret = ffa_memory_attributes_validate(memory_region->attributes);
1585 if (ret.func != FFA_SUCCESS_32) {
1586 return ret;
1587 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01001588 }
1589
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001590 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001591}
1592
1593/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001594 * Gets the share state for continuing an operation to donate, lend or share
1595 * memory, and checks that it is a valid request.
1596 *
1597 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1598 * not.
1599 */
J-Alvesfdd29272022-07-19 13:16:31 +01001600struct ffa_value ffa_memory_send_continue_validate(
Andrew Walbranca808b12020-05-15 17:22:28 +01001601 struct share_states_locked share_states, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001602 struct ffa_memory_share_state **share_state_ret, ffa_id_t from_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01001603 struct mpool *page_pool)
1604{
1605 struct ffa_memory_share_state *share_state;
1606 struct ffa_memory_region *memory_region;
1607
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001608 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001609
1610 /*
1611 * Look up the share state by handle and make sure that the VM ID
1612 * matches.
1613 */
Karl Meakin4a2854a2023-06-30 16:26:52 +01001614 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00001615 if (share_state == NULL) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001616 dlog_verbose(
1617 "Invalid handle %#x for memory send continuation.\n",
1618 handle);
1619 return ffa_error(FFA_INVALID_PARAMETERS);
1620 }
1621 memory_region = share_state->memory_region;
1622
J-Alvesfdd29272022-07-19 13:16:31 +01001623 if (vm_id_is_current_world(from_vm_id) &&
1624 memory_region->sender != from_vm_id) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001625 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1626 return ffa_error(FFA_INVALID_PARAMETERS);
1627 }
1628
1629 if (share_state->sending_complete) {
1630 dlog_verbose(
1631 "Sending of memory handle %#x is already complete.\n",
1632 handle);
1633 return ffa_error(FFA_INVALID_PARAMETERS);
1634 }
1635
1636 if (share_state->fragment_count == MAX_FRAGMENTS) {
1637 /*
1638 * Log a warning as this is a sign that MAX_FRAGMENTS should
1639 * probably be increased.
1640 */
1641 dlog_warning(
1642 "Too many fragments for memory share with handle %#x; "
1643 "only %d supported.\n",
1644 handle, MAX_FRAGMENTS);
1645 /* Free share state, as it's not possible to complete it. */
1646 share_state_free(share_states, share_state, page_pool);
1647 return ffa_error(FFA_NO_MEMORY);
1648 }
1649
1650 *share_state_ret = share_state;
1651
1652 return (struct ffa_value){.func = FFA_SUCCESS_32};
1653}
1654
1655/**
J-Alves95df0ef2022-12-07 10:09:48 +00001656 * Checks if there is at least one receiver from the other world.
1657 */
J-Alvesfdd29272022-07-19 13:16:31 +01001658bool memory_region_receivers_from_other_world(
J-Alves95df0ef2022-12-07 10:09:48 +00001659 struct ffa_memory_region *memory_region)
1660{
1661 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
J-Alves19e20cf2023-08-02 12:48:55 +01001662 ffa_id_t receiver = memory_region->receivers[i]
1663 .receiver_permissions.receiver;
J-Alves95df0ef2022-12-07 10:09:48 +00001664 if (!vm_id_is_current_world(receiver)) {
1665 return true;
1666 }
1667 }
1668 return false;
1669}
1670
1671/**
J-Alves9da280b2022-12-21 14:55:39 +00001672 * Validates a call to donate, lend or share memory in which Hafnium is the
1673 * designated allocator of the memory handle. In practice, this also means
1674 * Hafnium is responsible for managing the state structures for the transaction.
1675 * If Hafnium is the SPMC, it should allocate the memory handle when either the
1676 * sender is an SP or there is at least one borrower that is an SP.
1677 * If Hafnium is the hypervisor, it should allocate the memory handle when
1678 * operation involves only NWd VMs.
1679 *
1680 * If validation goes well, Hafnium updates the stage-2 page tables of the
1681 * sender. Validation consists of checking if the message length and number of
1682 * memory region constituents match, and if the transition is valid for the
1683 * type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001684 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001685 * Assumes that the caller has already found and locked the sender VM and copied
1686 * the memory region descriptor from the sender's TX buffer to a freshly
1687 * allocated page from Hafnium's internal pool. The caller must have also
1688 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001689 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001690 * This function takes ownership of the `memory_region` passed in and will free
1691 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001692 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001693struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001694 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001695 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001696 uint32_t fragment_length, uint32_t share_func,
1697 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001698{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001699 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001700 struct share_states_locked share_states;
1701 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001702
1703 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001704 * If there is an error validating the `memory_region` then we need to
1705 * free it because we own it but we won't be storing it in a share state
1706 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001707 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001708 ret = ffa_memory_send_validate(from_locked, memory_region,
1709 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001710 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001711 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001712 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001713 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001714 }
1715
Andrew Walbrana65a1322020-04-06 19:32:32 +01001716 /* Set flag for share function, ready to be retrieved later. */
1717 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001718 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001719 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001720 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001721 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001722 case FFA_MEM_LEND_32:
1723 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001724 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001725 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001726 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001727 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001728 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001729 }
1730
Andrew Walbranca808b12020-05-15 17:22:28 +01001731 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001732 /*
1733 * Allocate a share state before updating the page table. Otherwise if
1734 * updating the page table succeeded but allocating the share state
1735 * failed then it would leave the memory in a state where nobody could
1736 * get it back.
1737 */
Karl Meakin52cdfe72023-06-30 14:49:10 +01001738 share_state = allocate_share_state(share_states, share_func,
1739 memory_region, fragment_length,
1740 FFA_MEMORY_HANDLE_INVALID);
J-Alvesb56aac82023-11-10 09:44:43 +00001741 if (share_state == NULL) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001742 dlog_verbose("Failed to allocate share state.\n");
1743 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001744 ret = ffa_error(FFA_NO_MEMORY);
1745 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001746 }
1747
Andrew Walbranca808b12020-05-15 17:22:28 +01001748 if (fragment_length == memory_share_length) {
1749 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00001750 ret = ffa_memory_send_complete(
1751 from_locked, share_states, share_state, page_pool,
1752 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001753 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01001754 /*
1755 * Use sender ID from 'memory_region' assuming
1756 * that at this point it has been validated:
1757 * - MBZ at virtual FF-A instance.
1758 */
J-Alves19e20cf2023-08-02 12:48:55 +01001759 ffa_id_t sender_to_ret =
J-Alvesfdd29272022-07-19 13:16:31 +01001760 (from_locked.vm->id == HF_OTHER_WORLD_ID)
1761 ? memory_region->sender
1762 : 0;
Andrew Walbranca808b12020-05-15 17:22:28 +01001763 ret = (struct ffa_value){
1764 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00001765 .arg1 = (uint32_t)memory_region->handle,
1766 .arg2 = (uint32_t)(memory_region->handle >> 32),
J-Alvesfdd29272022-07-19 13:16:31 +01001767 .arg3 = fragment_length,
1768 .arg4 = (uint32_t)(sender_to_ret & 0xffff) << 16};
Andrew Walbranca808b12020-05-15 17:22:28 +01001769 }
1770
1771out:
1772 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001773 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001774 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001775}
1776
1777/**
J-Alves8505a8a2022-06-15 18:10:18 +01001778 * Continues an operation to donate, lend or share memory to a VM from current
1779 * world. If this is the last fragment then checks that the transition is valid
1780 * for the type of memory sending operation and updates the stage-2 page tables
1781 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01001782 *
1783 * Assumes that the caller has already found and locked the sender VM and copied
1784 * the memory region descriptor from the sender's TX buffer to a freshly
1785 * allocated page from Hafnium's internal pool.
1786 *
1787 * This function takes ownership of the `fragment` passed in; it must not be
1788 * freed by the caller.
1789 */
1790struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1791 void *fragment,
1792 uint32_t fragment_length,
1793 ffa_memory_handle_t handle,
1794 struct mpool *page_pool)
1795{
1796 struct share_states_locked share_states = share_states_lock();
1797 struct ffa_memory_share_state *share_state;
1798 struct ffa_value ret;
1799 struct ffa_memory_region *memory_region;
1800
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05001801 CHECK(is_aligned(fragment,
1802 alignof(struct ffa_memory_region_constituent)));
1803 if (fragment_length % sizeof(struct ffa_memory_region_constituent) !=
1804 0) {
1805 dlog_verbose("Fragment length %u misaligned.\n",
1806 fragment_length);
1807 ret = ffa_error(FFA_INVALID_PARAMETERS);
1808 goto out_free_fragment;
1809 }
1810
Andrew Walbranca808b12020-05-15 17:22:28 +01001811 ret = ffa_memory_send_continue_validate(share_states, handle,
1812 &share_state,
1813 from_locked.vm->id, page_pool);
1814 if (ret.func != FFA_SUCCESS_32) {
1815 goto out_free_fragment;
1816 }
1817 memory_region = share_state->memory_region;
1818
J-Alves95df0ef2022-12-07 10:09:48 +00001819 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001820 dlog_error(
1821 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01001822 "other world. This should never happen, and indicates "
1823 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01001824 "EL3 code.\n");
1825 ret = ffa_error(FFA_INVALID_PARAMETERS);
1826 goto out_free_fragment;
1827 }
1828
1829 /* Add this fragment. */
1830 share_state->fragments[share_state->fragment_count] = fragment;
1831 share_state->fragment_constituent_counts[share_state->fragment_count] =
1832 fragment_length / sizeof(struct ffa_memory_region_constituent);
1833 share_state->fragment_count++;
1834
1835 /* Check whether the memory send operation is now ready to complete. */
1836 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00001837 ret = ffa_memory_send_complete(
1838 from_locked, share_states, share_state, page_pool,
1839 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001840 } else {
1841 ret = (struct ffa_value){
1842 .func = FFA_MEM_FRAG_RX_32,
1843 .arg1 = (uint32_t)handle,
1844 .arg2 = (uint32_t)(handle >> 32),
1845 .arg3 = share_state_next_fragment_offset(share_states,
1846 share_state)};
1847 }
1848 goto out;
1849
1850out_free_fragment:
1851 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001852
1853out:
Andrew Walbranca808b12020-05-15 17:22:28 +01001854 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001855 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001856}
1857
Andrew Walbranca808b12020-05-15 17:22:28 +01001858/** Clean up after the receiver has finished retrieving a memory region. */
1859static void ffa_memory_retrieve_complete(
1860 struct share_states_locked share_states,
1861 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
1862{
1863 if (share_state->share_func == FFA_MEM_DONATE_32) {
1864 /*
1865 * Memory that has been donated can't be relinquished,
1866 * so no need to keep the share state around.
1867 */
1868 share_state_free(share_states, share_state, page_pool);
1869 dlog_verbose("Freed share state for donate.\n");
1870 }
1871}
1872
J-Alves2d8457f2022-10-05 11:06:41 +01001873/**
1874 * Initialises the given memory region descriptor to be used for an
1875 * `FFA_MEM_RETRIEVE_RESP`, including the given constituents for the first
1876 * fragment.
1877 * The memory region descriptor is initialized according to retriever's
1878 * FF-A version.
1879 *
1880 * Returns true on success, or false if the given constituents won't all fit in
1881 * the first fragment.
1882 */
1883static bool ffa_retrieved_memory_region_init(
1884 void *response, uint32_t ffa_version, size_t response_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01001885 ffa_id_t sender, ffa_memory_attributes_t attributes,
J-Alves2d8457f2022-10-05 11:06:41 +01001886 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001887 ffa_id_t receiver_id, ffa_memory_access_permissions_t permissions,
J-Alves2d8457f2022-10-05 11:06:41 +01001888 uint32_t page_count, uint32_t total_constituent_count,
1889 const struct ffa_memory_region_constituent constituents[],
1890 uint32_t fragment_constituent_count, uint32_t *total_length,
1891 uint32_t *fragment_length)
1892{
1893 struct ffa_composite_memory_region *composite_memory_region;
1894 struct ffa_memory_access *receiver;
1895 uint32_t i;
1896 uint32_t constituents_offset;
1897 uint32_t receiver_count;
1898
1899 assert(response != NULL);
1900
1901 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1902 struct ffa_memory_region_v1_0 *retrieve_response =
1903 (struct ffa_memory_region_v1_0 *)response;
1904
J-Alves5da37d92022-10-24 16:33:48 +01001905 ffa_memory_region_init_header_v1_0(
1906 retrieve_response, sender, attributes, flags, handle, 0,
1907 RECEIVERS_COUNT_IN_RETRIEVE_RESP);
J-Alves2d8457f2022-10-05 11:06:41 +01001908
1909 receiver = &retrieve_response->receivers[0];
1910 receiver_count = retrieve_response->receiver_count;
1911
1912 receiver->composite_memory_region_offset =
1913 sizeof(struct ffa_memory_region_v1_0) +
1914 receiver_count * sizeof(struct ffa_memory_access);
1915
1916 composite_memory_region = ffa_memory_region_get_composite_v1_0(
1917 retrieve_response, 0);
1918 } else {
1919 /* Default to FF-A v1.1 version. */
1920 struct ffa_memory_region *retrieve_response =
1921 (struct ffa_memory_region *)response;
1922
1923 ffa_memory_region_init_header(retrieve_response, sender,
1924 attributes, flags, handle, 0, 1);
1925
1926 receiver = &retrieve_response->receivers[0];
1927 receiver_count = retrieve_response->receiver_count;
1928
1929 /*
1930 * Note that `sizeof(struct_ffa_memory_region)` and
1931 * `sizeof(struct ffa_memory_access)` must both be multiples of
1932 * 16 (as verified by the asserts in `ffa_memory.c`, so it is
1933 * guaranteed that the offset we calculate here is aligned to a
1934 * 64-bit boundary and so 64-bit values can be copied without
1935 * alignment faults.
1936 */
1937 receiver->composite_memory_region_offset =
1938 sizeof(struct ffa_memory_region) +
1939 receiver_count * sizeof(struct ffa_memory_access);
1940
1941 composite_memory_region =
1942 ffa_memory_region_get_composite(retrieve_response, 0);
1943 }
1944
1945 assert(receiver != NULL);
1946 assert(composite_memory_region != NULL);
1947
1948 /*
1949 * Initialized here as in memory retrieve responses we currently expect
1950 * one borrower to be specified.
1951 */
1952 ffa_memory_access_init_permissions(receiver, receiver_id, 0, 0, flags);
1953 receiver->receiver_permissions.permissions = permissions;
1954
1955 composite_memory_region->page_count = page_count;
1956 composite_memory_region->constituent_count = total_constituent_count;
1957 composite_memory_region->reserved_0 = 0;
1958
1959 constituents_offset = receiver->composite_memory_region_offset +
1960 sizeof(struct ffa_composite_memory_region);
1961 if (constituents_offset +
1962 fragment_constituent_count *
1963 sizeof(struct ffa_memory_region_constituent) >
1964 response_max_size) {
1965 return false;
1966 }
1967
1968 for (i = 0; i < fragment_constituent_count; ++i) {
1969 composite_memory_region->constituents[i] = constituents[i];
1970 }
1971
1972 if (total_length != NULL) {
1973 *total_length =
1974 constituents_offset +
1975 composite_memory_region->constituent_count *
1976 sizeof(struct ffa_memory_region_constituent);
1977 }
1978 if (fragment_length != NULL) {
1979 *fragment_length =
1980 constituents_offset +
1981 fragment_constituent_count *
1982 sizeof(struct ffa_memory_region_constituent);
1983 }
1984
1985 return true;
1986}
1987
J-Alves96de29f2022-04-26 16:05:24 +01001988/*
1989 * Gets the receiver's access permissions from 'struct ffa_memory_region' and
1990 * returns its index in the receiver's array. If receiver's ID doesn't exist
1991 * in the array, return the region's 'receiver_count'.
1992 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001993uint32_t ffa_memory_region_get_receiver(struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01001994 ffa_id_t receiver)
J-Alves96de29f2022-04-26 16:05:24 +01001995{
1996 struct ffa_memory_access *receivers;
1997 uint32_t i;
1998
1999 assert(memory_region != NULL);
2000
2001 receivers = memory_region->receivers;
2002
2003 for (i = 0U; i < memory_region->receiver_count; i++) {
2004 if (receivers[i].receiver_permissions.receiver == receiver) {
2005 break;
2006 }
2007 }
2008
2009 return i;
2010}
2011
2012/**
2013 * Validates the retrieved permissions against those specified by the lender
2014 * of memory share operation. Optionally can help set the permissions to be used
2015 * for the S2 mapping, through the `permissions` argument.
J-Alvesdcad8992023-09-15 14:10:35 +01002016 * Returns FFA_SUCCESS if all the fields are valid. FFA_ERROR, with error code:
2017 * - FFA_INVALID_PARAMETERS -> if the fields have invalid values as per the
2018 * specification for each ABI.
2019 * - FFA_DENIED -> if the permissions specified by the retriever are not
2020 * less permissive than those provided by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002021 */
J-Alvesdcad8992023-09-15 14:10:35 +01002022static struct ffa_value ffa_memory_retrieve_is_memory_access_valid(
2023 uint32_t share_func, enum ffa_data_access sent_data_access,
J-Alves96de29f2022-04-26 16:05:24 +01002024 enum ffa_data_access requested_data_access,
2025 enum ffa_instruction_access sent_instruction_access,
2026 enum ffa_instruction_access requested_instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002027 ffa_memory_access_permissions_t *permissions, bool multiple_borrowers)
J-Alves96de29f2022-04-26 16:05:24 +01002028{
2029 switch (sent_data_access) {
2030 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2031 case FFA_DATA_ACCESS_RW:
2032 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2033 requested_data_access == FFA_DATA_ACCESS_RW) {
2034 if (permissions != NULL) {
2035 ffa_set_data_access_attr(permissions,
2036 FFA_DATA_ACCESS_RW);
2037 }
2038 break;
2039 }
2040 /* Intentional fall-through. */
2041 case FFA_DATA_ACCESS_RO:
2042 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2043 requested_data_access == FFA_DATA_ACCESS_RO) {
2044 if (permissions != NULL) {
2045 ffa_set_data_access_attr(permissions,
2046 FFA_DATA_ACCESS_RO);
2047 }
2048 break;
2049 }
2050 dlog_verbose(
2051 "Invalid data access requested; sender specified "
2052 "permissions %#x but receiver requested %#x.\n",
2053 sent_data_access, requested_data_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002054 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002055 case FFA_DATA_ACCESS_RESERVED:
2056 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
2057 "checked before this point.");
2058 }
2059
J-Alvesdcad8992023-09-15 14:10:35 +01002060 /*
2061 * For operations with a single borrower, If it is an FFA_MEMORY_LEND
2062 * or FFA_MEMORY_DONATE the retriever should have specifed the
2063 * instruction permissions it wishes to receive.
2064 */
2065 switch (share_func) {
2066 case FFA_MEM_SHARE_32:
2067 if (requested_instruction_access !=
2068 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2069 dlog_verbose(
2070 "%s: for share instruction permissions must "
2071 "NOT be specified.\n",
2072 __func__);
2073 return ffa_error(FFA_INVALID_PARAMETERS);
2074 }
2075 break;
2076 case FFA_MEM_LEND_32:
2077 /*
2078 * For operations with multiple borrowers only permit XN
2079 * permissions, and both Sender and borrower should have used
2080 * FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED.
2081 */
2082 if (multiple_borrowers) {
2083 if (requested_instruction_access !=
2084 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2085 dlog_verbose(
2086 "%s: lend/share/donate with multiple "
2087 "borrowers "
2088 "instruction permissions must NOT be "
2089 "specified.\n",
2090 __func__);
2091 return ffa_error(FFA_INVALID_PARAMETERS);
2092 }
2093 break;
2094 }
2095 /* Fall through if the operation targets a single borrower. */
2096 case FFA_MEM_DONATE_32:
2097 if (!multiple_borrowers &&
2098 requested_instruction_access ==
2099 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2100 dlog_verbose(
2101 "%s: for lend/donate with single borrower "
2102 "instruction permissions must be speficified "
2103 "by borrower\n",
2104 __func__);
2105 return ffa_error(FFA_INVALID_PARAMETERS);
2106 }
2107 break;
2108 default:
2109 panic("%s: Wrong func id provided.\n", __func__);
2110 }
2111
J-Alves96de29f2022-04-26 16:05:24 +01002112 switch (sent_instruction_access) {
2113 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2114 case FFA_INSTRUCTION_ACCESS_X:
J-Alvesdcad8992023-09-15 14:10:35 +01002115 if (requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
J-Alves96de29f2022-04-26 16:05:24 +01002116 if (permissions != NULL) {
2117 ffa_set_instruction_access_attr(
2118 permissions, FFA_INSTRUCTION_ACCESS_X);
2119 }
2120 break;
2121 }
J-Alvesdcad8992023-09-15 14:10:35 +01002122 /*
2123 * Fall through if requested permissions are less
2124 * permissive than those provided by the sender.
2125 */
J-Alves96de29f2022-04-26 16:05:24 +01002126 case FFA_INSTRUCTION_ACCESS_NX:
2127 if (requested_instruction_access ==
2128 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2129 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2130 if (permissions != NULL) {
2131 ffa_set_instruction_access_attr(
2132 permissions, FFA_INSTRUCTION_ACCESS_NX);
2133 }
2134 break;
2135 }
2136 dlog_verbose(
2137 "Invalid instruction access requested; sender "
2138 "specified permissions %#x but receiver requested "
2139 "%#x.\n",
2140 sent_instruction_access, requested_instruction_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002141 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002142 case FFA_INSTRUCTION_ACCESS_RESERVED:
2143 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
2144 "be checked before this point.");
2145 }
2146
J-Alvesdcad8992023-09-15 14:10:35 +01002147 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves96de29f2022-04-26 16:05:24 +01002148}
2149
2150/**
2151 * Validate the receivers' permissions in the retrieve request against those
2152 * specified by the lender.
2153 * In the `permissions` argument returns the permissions to set at S2 for the
2154 * caller to the FFA_MEMORY_RETRIEVE_REQ.
J-Alves3456e032023-07-20 12:20:05 +01002155 * The function looks into the flag to bypass multiple borrower checks:
2156 * - If not set returns FFA_SUCCESS if all specified permissions are valid.
2157 * - If set returns FFA_SUCCESS if the descriptor contains the permissions
2158 * to the caller of FFA_MEM_RETRIEVE_REQ and they are valid. Other permissions
2159 * are ignored, if provided.
J-Alves96de29f2022-04-26 16:05:24 +01002160 */
2161static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
2162 struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01002163 struct ffa_memory_region *retrieve_request, ffa_id_t to_vm_id,
J-Alvesdcad8992023-09-15 14:10:35 +01002164 ffa_memory_access_permissions_t *permissions, uint32_t func_id)
J-Alves96de29f2022-04-26 16:05:24 +01002165{
2166 uint32_t retrieve_receiver_index;
J-Alves3456e032023-07-20 12:20:05 +01002167 bool bypass_multi_receiver_check =
2168 (retrieve_request->flags &
2169 FFA_MEMORY_REGION_FLAG_BYPASS_BORROWERS_CHECK) != 0U;
J-Alvesdcad8992023-09-15 14:10:35 +01002170 const uint32_t region_receiver_count = memory_region->receiver_count;
2171 struct ffa_value ret;
J-Alves96de29f2022-04-26 16:05:24 +01002172
2173 assert(permissions != NULL);
2174
J-Alves3456e032023-07-20 12:20:05 +01002175 if (!bypass_multi_receiver_check) {
J-Alvesdcad8992023-09-15 14:10:35 +01002176 if (retrieve_request->receiver_count != region_receiver_count) {
J-Alves3456e032023-07-20 12:20:05 +01002177 dlog_verbose(
2178 "Retrieve request should contain same list of "
2179 "borrowers, as specified by the lender.\n");
2180 return ffa_error(FFA_INVALID_PARAMETERS);
2181 }
2182 } else {
2183 if (retrieve_request->receiver_count != 1) {
2184 dlog_verbose(
2185 "Set bypass multiple borrower check, receiver "
2186 "list must be sized 1 (%x)\n",
2187 memory_region->receiver_count);
2188 return ffa_error(FFA_INVALID_PARAMETERS);
2189 }
J-Alves96de29f2022-04-26 16:05:24 +01002190 }
2191
2192 retrieve_receiver_index = retrieve_request->receiver_count;
2193
2194 /* Should be populated with the permissions of the retriever. */
2195 *permissions = 0;
2196
2197 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
2198 ffa_memory_access_permissions_t sent_permissions;
2199 struct ffa_memory_access *current_receiver =
2200 &retrieve_request->receivers[i];
2201 ffa_memory_access_permissions_t requested_permissions =
2202 current_receiver->receiver_permissions.permissions;
J-Alves19e20cf2023-08-02 12:48:55 +01002203 ffa_id_t current_receiver_id =
J-Alves96de29f2022-04-26 16:05:24 +01002204 current_receiver->receiver_permissions.receiver;
2205 bool found_to_id = current_receiver_id == to_vm_id;
2206
J-Alves3456e032023-07-20 12:20:05 +01002207 if (bypass_multi_receiver_check && !found_to_id) {
2208 dlog_verbose(
2209 "Bypass multiple borrower check for id %x.\n",
2210 current_receiver_id);
2211 continue;
2212 }
2213
J-Alves96de29f2022-04-26 16:05:24 +01002214 /*
2215 * Find the current receiver in the transaction descriptor from
2216 * sender.
2217 */
2218 uint32_t mem_region_receiver_index =
2219 ffa_memory_region_get_receiver(memory_region,
2220 current_receiver_id);
2221
2222 if (mem_region_receiver_index ==
2223 memory_region->receiver_count) {
2224 dlog_verbose("%s: receiver %x not found\n", __func__,
2225 current_receiver_id);
2226 return ffa_error(FFA_DENIED);
2227 }
2228
2229 sent_permissions =
2230 memory_region->receivers[mem_region_receiver_index]
2231 .receiver_permissions.permissions;
2232
2233 if (found_to_id) {
2234 retrieve_receiver_index = i;
2235 }
2236
2237 /*
2238 * Since we are traversing the list of receivers, save the index
2239 * of the caller. As it needs to be there.
2240 */
2241
2242 if (current_receiver->composite_memory_region_offset != 0U) {
2243 dlog_verbose(
2244 "Retriever specified address ranges not "
2245 "supported (got offset %d).\n",
2246 current_receiver
2247 ->composite_memory_region_offset);
2248 return ffa_error(FFA_INVALID_PARAMETERS);
2249 }
2250
2251 /*
J-Alvesdcad8992023-09-15 14:10:35 +01002252 * Check if retrieve request memory access list is valid:
2253 * - The retrieve request complies with the specification.
2254 * - Permissions are within those specified by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002255 */
J-Alvesdcad8992023-09-15 14:10:35 +01002256 ret = ffa_memory_retrieve_is_memory_access_valid(
2257 func_id, ffa_get_data_access_attr(sent_permissions),
2258 ffa_get_data_access_attr(requested_permissions),
2259 ffa_get_instruction_access_attr(sent_permissions),
2260 ffa_get_instruction_access_attr(requested_permissions),
2261 found_to_id ? permissions : NULL,
2262 region_receiver_count > 1);
2263 if (ret.func != FFA_SUCCESS_32) {
2264 return ret;
J-Alves96de29f2022-04-26 16:05:24 +01002265 }
2266
2267 /*
2268 * Can't request PM to clear memory if only provided with RO
2269 * permissions.
2270 */
2271 if (found_to_id &&
2272 (ffa_get_data_access_attr(*permissions) ==
2273 FFA_DATA_ACCESS_RO) &&
2274 (retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2275 0U) {
2276 dlog_verbose(
2277 "Receiver has RO permissions can not request "
2278 "clear.\n");
2279 return ffa_error(FFA_DENIED);
2280 }
2281 }
2282
2283 if (retrieve_receiver_index == retrieve_request->receiver_count) {
2284 dlog_verbose(
2285 "Retrieve request does not contain caller's (%x) "
2286 "permissions\n",
2287 to_vm_id);
2288 return ffa_error(FFA_INVALID_PARAMETERS);
2289 }
2290
2291 return (struct ffa_value){.func = FFA_SUCCESS_32};
2292}
2293
J-Alvesa9cd7e32022-07-01 13:49:33 +01002294/*
2295 * According to section 16.4.3 of FF-A v1.1 EAC0 specification, the hypervisor
2296 * may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region description
2297 * of a pending memory sharing operation whose allocator is the SPM, for
2298 * validation purposes before forwarding an FFA_MEM_RECLAIM call. In doing so
2299 * the memory region descriptor of the retrieve request must be zeroed with the
2300 * exception of the sender ID and handle.
2301 */
2302bool is_ffa_memory_retrieve_borrower_request(struct ffa_memory_region *request,
2303 struct vm_locked to_locked)
2304{
2305 return to_locked.vm->id == HF_HYPERVISOR_VM_ID &&
2306 request->attributes == 0U && request->flags == 0U &&
2307 request->tag == 0U && request->receiver_count == 0U &&
2308 plat_ffa_memory_handle_allocated_by_current_world(
2309 request->handle);
2310}
2311
2312/*
2313 * Helper to reset count of fragments retrieved by the hypervisor.
2314 */
2315static void ffa_memory_retrieve_complete_from_hyp(
2316 struct ffa_memory_share_state *share_state)
2317{
2318 if (share_state->hypervisor_fragment_count ==
2319 share_state->fragment_count) {
2320 share_state->hypervisor_fragment_count = 0;
2321 }
2322}
2323
J-Alves089004f2022-07-13 14:25:44 +01002324/**
2325 * Validate that the memory region descriptor provided by the borrower on
2326 * FFA_MEM_RETRIEVE_REQ, against saved memory region provided by lender at the
2327 * memory sharing call.
2328 */
2329static struct ffa_value ffa_memory_retrieve_validate(
J-Alves19e20cf2023-08-02 12:48:55 +01002330 ffa_id_t receiver_id, struct ffa_memory_region *retrieve_request,
J-Alves089004f2022-07-13 14:25:44 +01002331 struct ffa_memory_region *memory_region, uint32_t *receiver_index,
2332 uint32_t share_func)
2333{
2334 ffa_memory_region_flags_t transaction_type =
2335 retrieve_request->flags &
2336 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002337 enum ffa_memory_security security_state;
J-Alves089004f2022-07-13 14:25:44 +01002338
2339 assert(retrieve_request != NULL);
2340 assert(memory_region != NULL);
2341 assert(receiver_index != NULL);
2342 assert(retrieve_request->sender == memory_region->sender);
2343
2344 /*
2345 * Check that the transaction type expected by the receiver is
2346 * correct, if it has been specified.
2347 */
2348 if (transaction_type !=
2349 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
2350 transaction_type != (memory_region->flags &
2351 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
2352 dlog_verbose(
2353 "Incorrect transaction type %#x for "
2354 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
2355 transaction_type,
2356 memory_region->flags &
2357 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
2358 retrieve_request->handle);
2359 return ffa_error(FFA_INVALID_PARAMETERS);
2360 }
2361
2362 if (retrieve_request->tag != memory_region->tag) {
2363 dlog_verbose(
2364 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
2365 "%d for handle %#x.\n",
2366 retrieve_request->tag, memory_region->tag,
2367 retrieve_request->handle);
2368 return ffa_error(FFA_INVALID_PARAMETERS);
2369 }
2370
2371 *receiver_index =
2372 ffa_memory_region_get_receiver(memory_region, receiver_id);
2373
2374 if (*receiver_index == memory_region->receiver_count) {
2375 dlog_verbose(
2376 "Incorrect receiver VM ID %d for "
2377 "FFA_MEM_RETRIEVE_REQ, for handle %#x.\n",
J-Alves59ed0042022-07-28 18:26:41 +01002378 receiver_id, memory_region->handle);
J-Alves089004f2022-07-13 14:25:44 +01002379 return ffa_error(FFA_INVALID_PARAMETERS);
2380 }
2381
2382 if ((retrieve_request->flags &
2383 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
2384 dlog_verbose(
2385 "Retriever specified 'address range alignment 'hint' "
2386 "not supported.\n");
2387 return ffa_error(FFA_INVALID_PARAMETERS);
2388 }
2389 if ((retrieve_request->flags &
2390 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
2391 dlog_verbose(
2392 "Bits 8-5 must be zero in memory region's flags "
2393 "(address range alignment hint not supported).\n");
2394 return ffa_error(FFA_INVALID_PARAMETERS);
2395 }
2396
2397 if ((retrieve_request->flags & ~0x7FF) != 0U) {
2398 dlog_verbose(
2399 "Bits 31-10 must be zero in memory region's flags.\n");
2400 return ffa_error(FFA_INVALID_PARAMETERS);
2401 }
2402
2403 if (share_func == FFA_MEM_SHARE_32 &&
2404 (retrieve_request->flags &
2405 (FFA_MEMORY_REGION_FLAG_CLEAR |
2406 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
2407 dlog_verbose(
2408 "Memory Share operation can't clean after relinquish "
2409 "memory region.\n");
2410 return ffa_error(FFA_INVALID_PARAMETERS);
2411 }
2412
2413 /*
2414 * If the borrower needs the memory to be cleared before mapping
2415 * to its address space, the sender should have set the flag
2416 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
2417 * FFA_DENIED.
2418 */
2419 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
2420 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
2421 dlog_verbose(
2422 "Borrower needs memory cleared. Sender needs to set "
2423 "flag for clearing memory.\n");
2424 return ffa_error(FFA_DENIED);
2425 }
2426
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002427 /* Memory region attributes NS-Bit MBZ for FFA_MEM_RETRIEVE_REQ. */
2428 security_state =
2429 ffa_get_memory_security_attr(retrieve_request->attributes);
2430 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
2431 dlog_verbose(
2432 "Invalid security state for memory retrieve request "
2433 "operation.\n");
2434 return ffa_error(FFA_INVALID_PARAMETERS);
2435 }
2436
J-Alves089004f2022-07-13 14:25:44 +01002437 /*
2438 * If memory type is not specified, bypass validation of memory
2439 * attributes in the retrieve request. The retriever is expecting to
2440 * obtain this information from the SPMC.
2441 */
2442 if (ffa_get_memory_type_attr(retrieve_request->attributes) ==
2443 FFA_MEMORY_NOT_SPECIFIED_MEM) {
2444 return (struct ffa_value){.func = FFA_SUCCESS_32};
2445 }
2446
2447 /*
2448 * Ensure receiver's attributes are compatible with how
2449 * Hafnium maps memory: Normal Memory, Inner shareable,
2450 * Write-Back Read-Allocate Write-Allocate Cacheable.
2451 */
2452 return ffa_memory_attributes_validate(retrieve_request->attributes);
2453}
2454
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002455struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2456 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002457 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002458 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002459{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002460 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002461 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002462 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002463 sizeof(struct ffa_memory_access);
2464 ffa_memory_handle_t handle = retrieve_request->handle;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002465 struct ffa_memory_region *memory_region;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002466 ffa_memory_access_permissions_t permissions = 0;
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002467 uint32_t memory_to_mode;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002468 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002469 struct ffa_memory_share_state *share_state;
2470 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002471 struct ffa_composite_memory_region *composite;
2472 uint32_t total_length;
2473 uint32_t fragment_length;
J-Alves19e20cf2023-08-02 12:48:55 +01002474 ffa_id_t receiver_id = to_locked.vm->id;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002475 bool is_send_complete = false;
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002476 ffa_memory_attributes_t attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002477
2478 dump_share_states();
2479
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002480 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002481 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002482 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002483 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002484 expected_retrieve_request_length,
2485 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002486 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002487 }
2488
2489 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01002490 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00002491 if (share_state == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002492 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002493 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002494 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002495 goto out;
2496 }
2497
J-Alves96de29f2022-04-26 16:05:24 +01002498 if (!share_state->sending_complete) {
2499 dlog_verbose(
2500 "Memory with handle %#x not fully sent, can't "
2501 "retrieve.\n",
2502 handle);
2503 ret = ffa_error(FFA_INVALID_PARAMETERS);
2504 goto out;
2505 }
2506
Andrew Walbrana65a1322020-04-06 19:32:32 +01002507 memory_region = share_state->memory_region;
J-Alves089004f2022-07-13 14:25:44 +01002508
Andrew Walbrana65a1322020-04-06 19:32:32 +01002509 CHECK(memory_region != NULL);
2510
J-Alves089004f2022-07-13 14:25:44 +01002511 if (retrieve_request->sender != memory_region->sender) {
2512 dlog_verbose(
2513 "Memory with handle %#x not fully sent, can't "
2514 "retrieve.\n",
2515 handle);
J-Alves41d4fef2023-11-16 16:20:09 +00002516 ret = ffa_error(FFA_DENIED);
J-Alves089004f2022-07-13 14:25:44 +01002517 goto out;
2518 }
J-Alves96de29f2022-04-26 16:05:24 +01002519
J-Alvesa9cd7e32022-07-01 13:49:33 +01002520 if (!is_ffa_memory_retrieve_borrower_request(retrieve_request,
2521 to_locked)) {
2522 uint32_t receiver_index;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002523
J-Alvesb5084cf2022-07-06 14:20:12 +01002524 /*
2525 * The SPMC can only process retrieve requests to memory share
2526 * operations with one borrower from the other world. It can't
2527 * determine the ID of the NWd VM that invoked the retrieve
2528 * request interface call. It relies on the hypervisor to
2529 * validate the caller's ID against that provided in the
2530 * `receivers` list of the retrieve response.
2531 * In case there is only one borrower from the NWd in the
2532 * transaction descriptor, record that in the `receiver_id` for
2533 * later use, and validate in the retrieve request message.
J-Alves3fa82aa2023-09-20 18:19:21 +01002534 * This limitation is due to the fact SPMC can't determine the
2535 * index in the memory share structures state to update.
J-Alvesb5084cf2022-07-06 14:20:12 +01002536 */
2537 if (to_locked.vm->id == HF_HYPERVISOR_VM_ID) {
2538 uint32_t other_world_count = 0;
2539
2540 for (uint32_t i = 0; i < memory_region->receiver_count;
2541 i++) {
2542 receiver_id =
2543 retrieve_request->receivers[0]
2544 .receiver_permissions.receiver;
2545 if (!vm_id_is_current_world(receiver_id)) {
2546 other_world_count++;
2547 }
2548 }
2549 if (other_world_count > 1) {
2550 dlog_verbose(
2551 "Support one receiver from the other "
2552 "world.\n");
2553 return ffa_error(FFA_NOT_SUPPORTED);
2554 }
2555 }
2556
2557 /*
2558 * Validate retrieve request, according to what was sent by the
2559 * sender. Function will output the `receiver_index` from the
J-Alves3fa82aa2023-09-20 18:19:21 +01002560 * provided memory region.
J-Alvesb5084cf2022-07-06 14:20:12 +01002561 */
J-Alves089004f2022-07-13 14:25:44 +01002562 ret = ffa_memory_retrieve_validate(
2563 receiver_id, retrieve_request, memory_region,
2564 &receiver_index, share_state->share_func);
2565 if (ret.func != FFA_SUCCESS_32) {
J-Alvesa9cd7e32022-07-01 13:49:33 +01002566 goto out;
2567 }
2568
2569 if (share_state->retrieved_fragment_count[receiver_index] !=
2570 0U) {
2571 dlog_verbose(
2572 "Memory with handle %#x already retrieved.\n",
2573 handle);
2574 ret = ffa_error(FFA_DENIED);
2575 goto out;
2576 }
2577
J-Alves3fa82aa2023-09-20 18:19:21 +01002578 /*
2579 * Validate the requested permissions against the sent
2580 * permissions.
2581 * Outputs the permissions to give to retriever at S2
2582 * PTs.
2583 */
J-Alvesa9cd7e32022-07-01 13:49:33 +01002584 ret = ffa_memory_retrieve_validate_memory_access_list(
2585 memory_region, retrieve_request, receiver_id,
J-Alvesdcad8992023-09-15 14:10:35 +01002586 &permissions, share_state->share_func);
J-Alves614d9f42022-06-28 14:03:10 +01002587 if (ret.func != FFA_SUCCESS_32) {
2588 goto out;
2589 }
Federico Recanatia98603a2021-12-20 18:04:03 +01002590
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002591 memory_to_mode = ffa_memory_permissions_to_mode(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002592 permissions, share_state->sender_orig_mode);
J-Alves40e260e2022-09-22 17:52:43 +01002593
J-Alvesa9cd7e32022-07-01 13:49:33 +01002594 ret = ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01002595 to_locked, share_state->fragments,
J-Alvesa9cd7e32022-07-01 13:49:33 +01002596 share_state->fragment_constituent_counts,
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002597 share_state->fragment_count, memory_to_mode,
J-Alvesa9cd7e32022-07-01 13:49:33 +01002598 share_state->share_func, false, page_pool);
2599
2600 if (ret.func != FFA_SUCCESS_32) {
2601 goto out;
2602 }
2603
2604 share_state->retrieved_fragment_count[receiver_index] = 1;
2605 is_send_complete =
2606 share_state->retrieved_fragment_count[receiver_index] ==
2607 share_state->fragment_count;
J-Alves3c5b2072022-11-21 12:45:40 +00002608
2609 share_state->clear_after_relinquish =
2610 (retrieve_request->flags &
2611 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH) != 0U;
2612
J-Alvesa9cd7e32022-07-01 13:49:33 +01002613 } else {
2614 if (share_state->hypervisor_fragment_count != 0U) {
2615 dlog_verbose(
J-Alvesb5084cf2022-07-06 14:20:12 +01002616 "Memory with handle %#x already retrieved by "
J-Alvesa9cd7e32022-07-01 13:49:33 +01002617 "the hypervisor.\n",
2618 handle);
2619 ret = ffa_error(FFA_DENIED);
2620 goto out;
2621 }
2622
2623 share_state->hypervisor_fragment_count = 1;
2624
2625 ffa_memory_retrieve_complete_from_hyp(share_state);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002626 }
2627
J-Alvesb5084cf2022-07-06 14:20:12 +01002628 /* VMs acquire the RX buffer from SPMC. */
2629 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
2630
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002631 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002632 * Copy response to RX buffer of caller and deliver the message.
2633 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002634 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002635 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002636 composite = ffa_memory_region_get_composite(memory_region, 0);
2637 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002638 * Constituents which we received in the first fragment should
2639 * always fit in the first fragment we are sending, because the
2640 * header is the same size in both cases and we have a fixed
2641 * message buffer size. So `ffa_retrieved_memory_region_init`
2642 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01002643 */
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002644
2645 /*
2646 * Set the security state in the memory retrieve response attributes
2647 * if specified by the target mode.
2648 */
2649 attributes = plat_ffa_memory_security_mode(
2650 memory_region->attributes, share_state->sender_orig_mode);
2651
Andrew Walbranca808b12020-05-15 17:22:28 +01002652 CHECK(ffa_retrieved_memory_region_init(
J-Alves2d8457f2022-10-05 11:06:41 +01002653 to_locked.vm->mailbox.recv, to_locked.vm->ffa_version,
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002654 HF_MAILBOX_SIZE, memory_region->sender, attributes,
2655 memory_region->flags, handle, receiver_id, permissions,
2656 composite->page_count, composite->constituent_count,
2657 share_state->fragments[0],
Andrew Walbranca808b12020-05-15 17:22:28 +01002658 share_state->fragment_constituent_counts[0], &total_length,
2659 &fragment_length));
J-Alvesb5084cf2022-07-06 14:20:12 +01002660
Andrew Walbranca808b12020-05-15 17:22:28 +01002661 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002662 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002663 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00002664 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002665
J-Alvesa9cd7e32022-07-01 13:49:33 +01002666 if (is_send_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002667 ffa_memory_retrieve_complete(share_states, share_state,
2668 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002669 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002670 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002671 .arg1 = total_length,
2672 .arg2 = fragment_length};
Andrew Walbranca808b12020-05-15 17:22:28 +01002673out:
2674 share_states_unlock(&share_states);
2675 dump_share_states();
2676 return ret;
2677}
2678
J-Alves5da37d92022-10-24 16:33:48 +01002679/**
2680 * Determine expected fragment offset according to the FF-A version of
2681 * the caller.
2682 */
2683static uint32_t ffa_memory_retrieve_expected_offset_per_ffa_version(
2684 struct ffa_memory_region *memory_region,
2685 uint32_t retrieved_constituents_count, uint32_t ffa_version)
2686{
2687 uint32_t expected_fragment_offset;
2688 uint32_t composite_constituents_offset;
2689
2690 if (ffa_version == MAKE_FFA_VERSION(1, 1)) {
2691 /*
2692 * Hafnium operates memory regions in FF-A v1.1 format, so we
2693 * can retrieve the constituents offset from descriptor.
2694 */
2695 composite_constituents_offset =
2696 ffa_composite_constituent_offset(memory_region, 0);
2697 } else if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
2698 /*
2699 * If retriever is FF-A v1.0, determine the composite offset
2700 * as it is expected to have been configured in the
2701 * retrieve response.
2702 */
2703 composite_constituents_offset =
2704 sizeof(struct ffa_memory_region_v1_0) +
2705 RECEIVERS_COUNT_IN_RETRIEVE_RESP *
2706 sizeof(struct ffa_memory_access) +
2707 sizeof(struct ffa_composite_memory_region);
2708 } else {
2709 panic("%s received an invalid FF-A version.\n", __func__);
2710 }
2711
2712 expected_fragment_offset =
2713 composite_constituents_offset +
2714 retrieved_constituents_count *
2715 sizeof(struct ffa_memory_region_constituent) -
2716 sizeof(struct ffa_memory_access) *
2717 (memory_region->receiver_count - 1);
2718
2719 return expected_fragment_offset;
2720}
2721
Andrew Walbranca808b12020-05-15 17:22:28 +01002722struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2723 ffa_memory_handle_t handle,
2724 uint32_t fragment_offset,
J-Alves19e20cf2023-08-02 12:48:55 +01002725 ffa_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01002726 struct mpool *page_pool)
2727{
2728 struct ffa_memory_region *memory_region;
2729 struct share_states_locked share_states;
2730 struct ffa_memory_share_state *share_state;
2731 struct ffa_value ret;
2732 uint32_t fragment_index;
2733 uint32_t retrieved_constituents_count;
2734 uint32_t i;
2735 uint32_t expected_fragment_offset;
2736 uint32_t remaining_constituent_count;
2737 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01002738 uint32_t receiver_index;
J-Alves59ed0042022-07-28 18:26:41 +01002739 bool continue_ffa_hyp_mem_retrieve_req;
Andrew Walbranca808b12020-05-15 17:22:28 +01002740
2741 dump_share_states();
2742
2743 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01002744 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00002745 if (share_state == NULL) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002746 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2747 handle);
2748 ret = ffa_error(FFA_INVALID_PARAMETERS);
2749 goto out;
2750 }
2751
2752 memory_region = share_state->memory_region;
2753 CHECK(memory_region != NULL);
2754
Andrew Walbranca808b12020-05-15 17:22:28 +01002755 if (!share_state->sending_complete) {
2756 dlog_verbose(
2757 "Memory with handle %#x not fully sent, can't "
2758 "retrieve.\n",
2759 handle);
2760 ret = ffa_error(FFA_INVALID_PARAMETERS);
2761 goto out;
2762 }
2763
J-Alves59ed0042022-07-28 18:26:41 +01002764 /*
2765 * If retrieve request from the hypervisor has been initiated in the
2766 * given share_state, continue it, else assume it is a continuation of
2767 * retrieve request from a NWd VM.
2768 */
2769 continue_ffa_hyp_mem_retrieve_req =
2770 (to_locked.vm->id == HF_HYPERVISOR_VM_ID) &&
2771 (share_state->hypervisor_fragment_count != 0U) &&
J-Alves661e1b72023-08-02 13:39:40 +01002772 ffa_is_vm_id(sender_vm_id);
Andrew Walbranca808b12020-05-15 17:22:28 +01002773
J-Alves59ed0042022-07-28 18:26:41 +01002774 if (!continue_ffa_hyp_mem_retrieve_req) {
2775 receiver_index = ffa_memory_region_get_receiver(
2776 memory_region, to_locked.vm->id);
2777
2778 if (receiver_index == memory_region->receiver_count) {
2779 dlog_verbose(
2780 "Caller of FFA_MEM_FRAG_RX (%x) is not a "
2781 "borrower to memory sharing transaction (%x)\n",
2782 to_locked.vm->id, handle);
2783 ret = ffa_error(FFA_INVALID_PARAMETERS);
2784 goto out;
2785 }
2786
2787 if (share_state->retrieved_fragment_count[receiver_index] ==
2788 0 ||
2789 share_state->retrieved_fragment_count[receiver_index] >=
2790 share_state->fragment_count) {
2791 dlog_verbose(
2792 "Retrieval of memory with handle %#x not yet "
2793 "started or already completed (%d/%d fragments "
2794 "retrieved).\n",
2795 handle,
2796 share_state->retrieved_fragment_count
2797 [receiver_index],
2798 share_state->fragment_count);
2799 ret = ffa_error(FFA_INVALID_PARAMETERS);
2800 goto out;
2801 }
2802
2803 fragment_index =
2804 share_state->retrieved_fragment_count[receiver_index];
2805 } else {
2806 if (share_state->hypervisor_fragment_count == 0 ||
2807 share_state->hypervisor_fragment_count >=
2808 share_state->fragment_count) {
2809 dlog_verbose(
2810 "Retrieve of memory with handle %x not "
2811 "started from hypervisor.\n",
2812 handle);
2813 ret = ffa_error(FFA_INVALID_PARAMETERS);
2814 goto out;
2815 }
2816
2817 if (memory_region->sender != sender_vm_id) {
2818 dlog_verbose(
2819 "Sender ID (%x) is not as expected for memory "
2820 "handle %x\n",
2821 sender_vm_id, handle);
2822 ret = ffa_error(FFA_INVALID_PARAMETERS);
2823 goto out;
2824 }
2825
2826 fragment_index = share_state->hypervisor_fragment_count;
2827
2828 receiver_index = 0;
2829 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002830
2831 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002832 * Check that the given fragment offset is correct by counting
2833 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01002834 */
2835 retrieved_constituents_count = 0;
2836 for (i = 0; i < fragment_index; ++i) {
2837 retrieved_constituents_count +=
2838 share_state->fragment_constituent_counts[i];
2839 }
J-Alvesc7484f12022-05-13 12:41:14 +01002840
2841 CHECK(memory_region->receiver_count > 0);
2842
Andrew Walbranca808b12020-05-15 17:22:28 +01002843 expected_fragment_offset =
J-Alves5da37d92022-10-24 16:33:48 +01002844 ffa_memory_retrieve_expected_offset_per_ffa_version(
2845 memory_region, retrieved_constituents_count,
2846 to_locked.vm->ffa_version);
2847
Andrew Walbranca808b12020-05-15 17:22:28 +01002848 if (fragment_offset != expected_fragment_offset) {
2849 dlog_verbose("Fragment offset was %d but expected %d.\n",
2850 fragment_offset, expected_fragment_offset);
2851 ret = ffa_error(FFA_INVALID_PARAMETERS);
2852 goto out;
2853 }
2854
J-Alves59ed0042022-07-28 18:26:41 +01002855 /* VMs acquire the RX buffer from SPMC. */
2856 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
2857
Andrew Walbranca808b12020-05-15 17:22:28 +01002858 remaining_constituent_count = ffa_memory_fragment_init(
2859 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2860 share_state->fragments[fragment_index],
2861 share_state->fragment_constituent_counts[fragment_index],
2862 &fragment_length);
2863 CHECK(remaining_constituent_count == 0);
2864 to_locked.vm->mailbox.recv_size = fragment_length;
2865 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2866 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00002867 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbranca808b12020-05-15 17:22:28 +01002868
J-Alves59ed0042022-07-28 18:26:41 +01002869 if (!continue_ffa_hyp_mem_retrieve_req) {
2870 share_state->retrieved_fragment_count[receiver_index]++;
2871 if (share_state->retrieved_fragment_count[receiver_index] ==
2872 share_state->fragment_count) {
2873 ffa_memory_retrieve_complete(share_states, share_state,
2874 page_pool);
2875 }
2876 } else {
2877 share_state->hypervisor_fragment_count++;
2878
2879 ffa_memory_retrieve_complete_from_hyp(share_state);
2880 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002881 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2882 .arg1 = (uint32_t)handle,
2883 .arg2 = (uint32_t)(handle >> 32),
2884 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002885
2886out:
2887 share_states_unlock(&share_states);
2888 dump_share_states();
2889 return ret;
2890}
2891
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002892struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002893 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002894 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002895{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002896 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002897 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002898 struct ffa_memory_share_state *share_state;
2899 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002900 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002901 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01002902 uint32_t receiver_index;
J-Alves3c5b2072022-11-21 12:45:40 +00002903 bool receivers_relinquished_memory;
J-Alves639ddfc2023-11-21 14:17:26 +00002904 ffa_memory_access_permissions_t receiver_permissions = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002905
Andrew Walbrana65a1322020-04-06 19:32:32 +01002906 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002907 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002908 "Stream endpoints not supported (got %d "
J-Alves668a86e2023-05-10 11:53:25 +01002909 "endpoints on FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002910 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002911 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002912 }
2913
Andrew Walbrana65a1322020-04-06 19:32:32 +01002914 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002915 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002916 "VM ID %d in relinquish message doesn't match "
J-Alves668a86e2023-05-10 11:53:25 +01002917 "calling VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002918 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002919 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002920 }
2921
2922 dump_share_states();
2923
2924 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01002925 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00002926 if (share_state == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002927 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002928 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002929 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002930 goto out;
2931 }
2932
Andrew Walbranca808b12020-05-15 17:22:28 +01002933 if (!share_state->sending_complete) {
2934 dlog_verbose(
2935 "Memory with handle %#x not fully sent, can't "
2936 "relinquish.\n",
2937 handle);
2938 ret = ffa_error(FFA_INVALID_PARAMETERS);
2939 goto out;
2940 }
2941
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002942 memory_region = share_state->memory_region;
2943 CHECK(memory_region != NULL);
2944
J-Alves8eb19162022-04-28 10:56:48 +01002945 receiver_index = ffa_memory_region_get_receiver(memory_region,
2946 from_locked.vm->id);
2947
2948 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002949 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002950 "VM ID %d tried to relinquish memory region "
J-Alves668a86e2023-05-10 11:53:25 +01002951 "with handle %#x and it is not a valid borrower.\n",
J-Alves8eb19162022-04-28 10:56:48 +01002952 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002953 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002954 goto out;
2955 }
2956
J-Alves8eb19162022-04-28 10:56:48 +01002957 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01002958 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002959 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002960 "Memory with handle %#x not yet fully "
2961 "retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01002962 "receiver %x can't relinquish.\n",
2963 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002964 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002965 goto out;
2966 }
2967
J-Alves3c5b2072022-11-21 12:45:40 +00002968 /*
2969 * Either clear if requested in relinquish call, or in a retrieve
2970 * request from one of the borrowers.
2971 */
2972 receivers_relinquished_memory = true;
2973
2974 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
2975 struct ffa_memory_access *receiver =
2976 &memory_region->receivers[i];
2977
2978 if (receiver->receiver_permissions.receiver ==
2979 from_locked.vm->id) {
J-Alves639ddfc2023-11-21 14:17:26 +00002980 receiver_permissions =
2981 receiver->receiver_permissions.permissions;
J-Alves3c5b2072022-11-21 12:45:40 +00002982 continue;
2983 }
2984
2985 if (share_state->retrieved_fragment_count[i] != 0U) {
2986 receivers_relinquished_memory = false;
2987 break;
2988 }
2989 }
2990
2991 clear = receivers_relinquished_memory &&
2992 (share_state->clear_after_relinquish ||
2993 (relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2994 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002995
2996 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002997 * Clear is not allowed for memory that was shared, as the
2998 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002999 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003000 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003001 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003002 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003003 goto out;
3004 }
3005
J-Alves639ddfc2023-11-21 14:17:26 +00003006 if (clear && receiver_permissions == FFA_DATA_ACCESS_RO) {
3007 dlog_verbose("%s: RO memory can't use clear memory flag.\n",
3008 __func__);
3009 ret = ffa_error(FFA_DENIED);
3010 goto out;
3011 }
3012
Andrew Walbranca808b12020-05-15 17:22:28 +01003013 ret = ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01003014 from_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003015 share_state->fragment_constituent_counts,
3016 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003017
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003018 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003019 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003020 * Mark memory handle as not retrieved, so it can be
3021 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003022 */
J-Alves8eb19162022-04-28 10:56:48 +01003023 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003024 }
3025
3026out:
3027 share_states_unlock(&share_states);
3028 dump_share_states();
3029 return ret;
3030}
3031
3032/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01003033 * Validates that the reclaim transition is allowed for the given
3034 * handle, updates the page table of the reclaiming VM, and frees the
3035 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003036 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003037struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01003038 ffa_memory_handle_t handle,
3039 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003040 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003041{
3042 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003043 struct ffa_memory_share_state *share_state;
3044 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003045 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003046
3047 dump_share_states();
3048
3049 share_states = share_states_lock();
Karl Meakin52cdfe72023-06-30 14:49:10 +01003050
Karl Meakin4a2854a2023-06-30 16:26:52 +01003051 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003052 if (share_state == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003053 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003054 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003055 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003056 goto out;
3057 }
Karl Meakin4a2854a2023-06-30 16:26:52 +01003058 memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003059
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003060 CHECK(memory_region != NULL);
3061
J-Alvesa9cd7e32022-07-01 13:49:33 +01003062 if (vm_id_is_current_world(to_locked.vm->id) &&
3063 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003064 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01003065 "VM %#x attempted to reclaim memory handle %#x "
3066 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003067 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003068 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003069 goto out;
3070 }
3071
Andrew Walbranca808b12020-05-15 17:22:28 +01003072 if (!share_state->sending_complete) {
3073 dlog_verbose(
3074 "Memory with handle %#x not fully sent, can't "
3075 "reclaim.\n",
3076 handle);
3077 ret = ffa_error(FFA_INVALID_PARAMETERS);
3078 goto out;
3079 }
3080
J-Alves752236c2022-04-28 11:07:47 +01003081 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3082 if (share_state->retrieved_fragment_count[i] != 0) {
3083 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003084 "Tried to reclaim memory handle %#x "
J-Alves3c5b2072022-11-21 12:45:40 +00003085 "that has not been relinquished by all "
J-Alvesa9cd7e32022-07-01 13:49:33 +01003086 "borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01003087 handle,
3088 memory_region->receivers[i]
3089 .receiver_permissions.receiver);
3090 ret = ffa_error(FFA_DENIED);
3091 goto out;
3092 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003093 }
3094
Andrew Walbranca808b12020-05-15 17:22:28 +01003095 ret = ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01003096 to_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003097 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00003098 share_state->fragment_count, share_state->sender_orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01003099 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003100
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003101 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003102 share_state_free(share_states, share_state, page_pool);
J-Alves3c5b2072022-11-21 12:45:40 +00003103 dlog_verbose("Freed share state after successful reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003104 }
3105
3106out:
3107 share_states_unlock(&share_states);
3108 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01003109}