blob: a17000d4d723bfb4d0378ebf0c2331944fe66f11 [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
Jose Marinho75509b42019-04-09 09:34:59 +010015#include "hf/api.h"
Daniel Boulbya2f8c662021-11-26 17:52:53 +000016#include "hf/assert.h"
Jose Marinho09b1db82019-08-08 09:16:59 +010017#include "hf/check.h"
Jose Marinho75509b42019-04-09 09:34:59 +010018#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010019#include "hf/ffa_internal.h"
J-Alves66652252022-07-06 09:49:51 +010020#include "hf/ffa_memory_internal.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000021#include "hf/mpool.h"
Jose Marinho75509b42019-04-09 09:34:59 +010022#include "hf/std.h"
Andrew Scull3c257452019-11-26 13:32:50 +000023#include "hf/vm.h"
Jose Marinho75509b42019-04-09 09:34:59 +010024
J-Alves2d8457f2022-10-05 11:06:41 +010025#include "vmapi/hf/ffa_v1_0.h"
26
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000027/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010028 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000029 * by this lock.
30 */
31static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010032static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000033
34/**
J-Alves917d2f22020-10-30 18:39:30 +000035 * Extracts the index from a memory handle allocated by Hafnium's current world.
36 */
37uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle)
38{
39 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
40}
41
42/**
Andrew Walbranca808b12020-05-15 17:22:28 +010043 * Initialises the next available `struct ffa_memory_share_state` and sets
44 * `share_state_ret` to a pointer to it. If `handle` is
45 * `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle, otherwise
46 * uses the provided handle which is assumed to be globally unique.
47 *
48 * Returns true on success or false if none are available.
49 */
J-Alves66652252022-07-06 09:49:51 +010050bool allocate_share_state(struct share_states_locked share_states,
51 uint32_t share_func,
52 struct ffa_memory_region *memory_region,
53 uint32_t fragment_length, ffa_memory_handle_t handle,
54 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000055{
Andrew Walbrana65a1322020-04-06 19:32:32 +010056 uint64_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000057
Daniel Boulbya2f8c662021-11-26 17:52:53 +000058 assert(share_states.share_states != NULL);
59 assert(memory_region != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000060
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000061 for (i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +010062 if (share_states.share_states[i].share_func == 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000063 uint32_t j;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010064 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +010065 &share_states.share_states[i];
66 struct ffa_composite_memory_region *composite =
67 ffa_memory_region_get_composite(memory_region,
68 0);
69
70 if (handle == FFA_MEMORY_HANDLE_INVALID) {
J-Alvesee68c542020-10-29 17:48:20 +000071 memory_region->handle =
Olivier Deprez55a189e2021-06-09 15:45:27 +020072 plat_ffa_memory_handle_make(i);
Andrew Walbranca808b12020-05-15 17:22:28 +010073 } else {
J-Alvesee68c542020-10-29 17:48:20 +000074 memory_region->handle = handle;
Andrew Walbranca808b12020-05-15 17:22:28 +010075 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000076 allocated_state->share_func = share_func;
77 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +010078 allocated_state->fragment_count = 1;
79 allocated_state->fragments[0] = composite->constituents;
80 allocated_state->fragment_constituent_counts[0] =
81 (fragment_length -
82 ffa_composite_constituent_offset(memory_region,
83 0)) /
84 sizeof(struct ffa_memory_region_constituent);
85 allocated_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000086 for (j = 0; j < MAX_MEM_SHARE_RECIPIENTS; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +010087 allocated_state->retrieved_fragment_count[j] =
88 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000089 }
Andrew Walbranca808b12020-05-15 17:22:28 +010090 if (share_state_ret != NULL) {
91 *share_state_ret = allocated_state;
92 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000093 return true;
94 }
95 }
96
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000097 return false;
98}
99
100/** Locks the share states lock. */
101struct share_states_locked share_states_lock(void)
102{
103 sl_lock(&share_states_lock_instance);
104
105 return (struct share_states_locked){.share_states = share_states};
106}
107
108/** Unlocks the share states lock. */
J-Alves66652252022-07-06 09:49:51 +0100109void share_states_unlock(struct share_states_locked *share_states)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000110{
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000111 assert(share_states->share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000112 share_states->share_states = NULL;
113 sl_unlock(&share_states_lock_instance);
114}
115
116/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100117 * If the given handle is a valid handle for an allocated share state then
118 * initialises `share_state_ret` to point to the share state and returns true.
119 * Otherwise returns false.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000120 */
J-Alvesfdd29272022-07-19 13:16:31 +0100121bool get_share_state(struct share_states_locked share_states,
122 ffa_memory_handle_t handle,
123 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000124{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100125 struct ffa_memory_share_state *share_state;
J-Alves917d2f22020-10-30 18:39:30 +0000126 uint64_t index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000127
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000128 assert(share_states.share_states != NULL);
129 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100130
131 /*
132 * First look for a share_state allocated by us, in which case the
133 * handle is based on the index.
134 */
Olivier Deprez55a189e2021-06-09 15:45:27 +0200135 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
J-Alves917d2f22020-10-30 18:39:30 +0000136 index = ffa_memory_handle_get_index(handle);
Andrew Walbranca808b12020-05-15 17:22:28 +0100137 if (index < MAX_MEM_SHARES) {
138 share_state = &share_states.share_states[index];
139 if (share_state->share_func != 0) {
140 *share_state_ret = share_state;
141 return true;
142 }
143 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000144 }
145
Andrew Walbranca808b12020-05-15 17:22:28 +0100146 /* Fall back to a linear scan. */
147 for (index = 0; index < MAX_MEM_SHARES; ++index) {
148 share_state = &share_states.share_states[index];
J-Alvesee68c542020-10-29 17:48:20 +0000149 if (share_state->memory_region != NULL &&
150 share_state->memory_region->handle == handle &&
Andrew Walbranca808b12020-05-15 17:22:28 +0100151 share_state->share_func != 0) {
152 *share_state_ret = share_state;
153 return true;
154 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000155 }
156
Andrew Walbranca808b12020-05-15 17:22:28 +0100157 return false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000158}
159
160/** Marks a share state as unallocated. */
J-Alvesfdd29272022-07-19 13:16:31 +0100161void share_state_free(struct share_states_locked share_states,
162 struct ffa_memory_share_state *share_state,
163 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000164{
Andrew Walbranca808b12020-05-15 17:22:28 +0100165 uint32_t i;
166
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000167 assert(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000168 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100169 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000170 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100171 /*
172 * First fragment is part of the same page as the `memory_region`, so it
173 * doesn't need to be freed separately.
174 */
175 share_state->fragments[0] = NULL;
176 share_state->fragment_constituent_counts[0] = 0;
177 for (i = 1; i < share_state->fragment_count; ++i) {
178 mpool_free(page_pool, share_state->fragments[i]);
179 share_state->fragments[i] = NULL;
180 share_state->fragment_constituent_counts[i] = 0;
181 }
182 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000183 share_state->memory_region = NULL;
J-Alvesa9cd7e32022-07-01 13:49:33 +0100184 share_state->hypervisor_fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000185}
186
Andrew Walbranca808b12020-05-15 17:22:28 +0100187/** Checks whether the given share state has been fully sent. */
J-Alvesfdd29272022-07-19 13:16:31 +0100188bool share_state_sending_complete(struct share_states_locked share_states,
189 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000190{
Andrew Walbranca808b12020-05-15 17:22:28 +0100191 struct ffa_composite_memory_region *composite;
192 uint32_t expected_constituent_count;
193 uint32_t fragment_constituent_count_total = 0;
194 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000195
Andrew Walbranca808b12020-05-15 17:22:28 +0100196 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000197 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100198
199 /*
200 * Share state must already be valid, or it's not possible to get hold
201 * of it.
202 */
203 CHECK(share_state->memory_region != NULL &&
204 share_state->share_func != 0);
205
206 composite =
207 ffa_memory_region_get_composite(share_state->memory_region, 0);
208 expected_constituent_count = composite->constituent_count;
209 for (i = 0; i < share_state->fragment_count; ++i) {
210 fragment_constituent_count_total +=
211 share_state->fragment_constituent_counts[i];
212 }
213 dlog_verbose(
214 "Checking completion: constituent count %d/%d from %d "
215 "fragments.\n",
216 fragment_constituent_count_total, expected_constituent_count,
217 share_state->fragment_count);
218
219 return fragment_constituent_count_total == expected_constituent_count;
220}
221
222/**
223 * Calculates the offset of the next fragment expected for the given share
224 * state.
225 */
J-Alvesfdd29272022-07-19 13:16:31 +0100226uint32_t share_state_next_fragment_offset(
Andrew Walbranca808b12020-05-15 17:22:28 +0100227 struct share_states_locked share_states,
228 struct ffa_memory_share_state *share_state)
229{
230 uint32_t next_fragment_offset;
231 uint32_t i;
232
233 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000234 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100235
236 next_fragment_offset =
237 ffa_composite_constituent_offset(share_state->memory_region, 0);
238 for (i = 0; i < share_state->fragment_count; ++i) {
239 next_fragment_offset +=
240 share_state->fragment_constituent_counts[i] *
241 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000242 }
243
Andrew Walbranca808b12020-05-15 17:22:28 +0100244 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000245}
246
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100247static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000248{
249 uint32_t i;
250
251 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
252 return;
253 }
254
Olivier Deprez935e1b12020-12-22 18:01:29 +0100255 dlog("from VM %#x, attributes %#x, flags %#x, tag %u, to "
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100256 "%u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100257 "recipients [",
258 memory_region->sender, memory_region->attributes,
Olivier Deprez935e1b12020-12-22 18:01:29 +0100259 memory_region->flags, memory_region->tag,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100260 memory_region->receiver_count);
261 for (i = 0; i < memory_region->receiver_count; ++i) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000262 if (i != 0) {
263 dlog(", ");
264 }
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100265 dlog("VM %#x: %#x (offset %u)",
Andrew Walbrana65a1322020-04-06 19:32:32 +0100266 memory_region->receivers[i].receiver_permissions.receiver,
267 memory_region->receivers[i]
268 .receiver_permissions.permissions,
269 memory_region->receivers[i]
270 .composite_memory_region_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000271 }
272 dlog("]");
273}
274
J-Alves66652252022-07-06 09:49:51 +0100275void dump_share_states(void)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000276{
277 uint32_t i;
278
279 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
280 return;
281 }
282
283 dlog("Current share states:\n");
284 sl_lock(&share_states_lock_instance);
285 for (i = 0; i < MAX_MEM_SHARES; ++i) {
286 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000287 switch (share_states[i].share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100288 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000289 dlog("SHARE");
290 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100291 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000292 dlog("LEND");
293 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100294 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000295 dlog("DONATE");
296 break;
297 default:
298 dlog("invalid share_func %#x",
299 share_states[i].share_func);
300 }
Olivier Deprez935e1b12020-12-22 18:01:29 +0100301 dlog(" %#x (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000302 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100303 if (share_states[i].sending_complete) {
304 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000305 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100306 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000307 }
J-Alves2a0d2882020-10-29 14:49:50 +0000308 dlog(" with %d fragments, %d retrieved, "
309 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100310 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000311 share_states[i].retrieved_fragment_count[0],
312 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000313 }
314 }
315 sl_unlock(&share_states_lock_instance);
316}
317
Andrew Walbran475c1452020-02-07 13:22:22 +0000318/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100319static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100320 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000321{
322 uint32_t mode = 0;
323
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100324 switch (ffa_get_data_access_attr(permissions)) {
325 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000326 mode = MM_MODE_R;
327 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100328 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000329 mode = MM_MODE_R | MM_MODE_W;
330 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100331 case FFA_DATA_ACCESS_NOT_SPECIFIED:
332 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
333 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100334 case FFA_DATA_ACCESS_RESERVED:
335 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100336 }
337
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100338 switch (ffa_get_instruction_access_attr(permissions)) {
339 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000340 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100341 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100342 mode |= MM_MODE_X;
343 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100344 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
345 mode |= (default_mode & MM_MODE_X);
346 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100347 case FFA_INSTRUCTION_ACCESS_RESERVED:
348 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000349 }
350
351 return mode;
352}
353
Jose Marinho75509b42019-04-09 09:34:59 +0100354/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000355 * Get the current mode in the stage-2 page table of the given vm of all the
356 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100357 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100358 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100359static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000360 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100361 struct ffa_memory_region_constituent **fragments,
362 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100363{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100364 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100365 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100366
Andrew Walbranca808b12020-05-15 17:22:28 +0100367 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100368 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000369 * Fail if there are no constituents. Otherwise we would get an
370 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100371 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100372 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100373 }
374
Andrew Walbranca808b12020-05-15 17:22:28 +0100375 for (i = 0; i < fragment_count; ++i) {
376 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
377 ipaddr_t begin = ipa_init(fragments[i][j].address);
378 size_t size = fragments[i][j].page_count * PAGE_SIZE;
379 ipaddr_t end = ipa_add(begin, size);
380 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100381
Andrew Walbranca808b12020-05-15 17:22:28 +0100382 /* Fail if addresses are not page-aligned. */
383 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
384 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
385 return ffa_error(FFA_INVALID_PARAMETERS);
386 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100387
Andrew Walbranca808b12020-05-15 17:22:28 +0100388 /*
389 * Ensure that this constituent memory range is all
390 * mapped with the same mode.
391 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800392 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100393 return ffa_error(FFA_DENIED);
394 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100395
Andrew Walbranca808b12020-05-15 17:22:28 +0100396 /*
397 * Ensure that all constituents are mapped with the same
398 * mode.
399 */
400 if (i == 0) {
401 *orig_mode = current_mode;
402 } else if (current_mode != *orig_mode) {
403 dlog_verbose(
404 "Expected mode %#x but was %#x for %d "
405 "pages at %#x.\n",
406 *orig_mode, current_mode,
407 fragments[i][j].page_count,
408 ipa_addr(begin));
409 return ffa_error(FFA_DENIED);
410 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100411 }
Jose Marinho75509b42019-04-09 09:34:59 +0100412 }
413
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100414 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000415}
416
417/**
418 * Verify that all pages have the same mode, that the starting mode
419 * constitutes a valid state and obtain the next mode to apply
420 * to the sending VM.
421 *
422 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100423 * 1) FFA_DENIED if a state transition was not found;
424 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100425 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100426 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100427 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100428 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
429 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000430 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100431static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100432 struct vm_locked from, uint32_t share_func,
J-Alves363f5722022-04-25 17:37:37 +0100433 struct ffa_memory_access *receivers, uint32_t receivers_count,
434 uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100435 struct ffa_memory_region_constituent **fragments,
436 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
437 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000438{
439 const uint32_t state_mask =
440 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100441 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000442
Andrew Walbranca808b12020-05-15 17:22:28 +0100443 ret = constituents_get_mode(from, orig_from_mode, fragments,
444 fragment_constituent_counts,
445 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100446 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100447 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100448 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100449 }
450
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000451 /* Ensure the address range is normal memory and not a device. */
452 if (*orig_from_mode & MM_MODE_D) {
453 dlog_verbose("Can't share device memory (mode is %#x).\n",
454 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100455 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000456 }
457
458 /*
459 * Ensure the sender is the owner and has exclusive access to the
460 * memory.
461 */
462 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100463 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100464 }
465
J-Alves363f5722022-04-25 17:37:37 +0100466 assert(receivers != NULL && receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100467
J-Alves363f5722022-04-25 17:37:37 +0100468 for (uint32_t i = 0U; i < receivers_count; i++) {
469 ffa_memory_access_permissions_t permissions =
470 receivers[i].receiver_permissions.permissions;
471 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
472 permissions, *orig_from_mode);
473
474 if ((*orig_from_mode & required_from_mode) !=
475 required_from_mode) {
476 dlog_verbose(
477 "Sender tried to send memory with permissions "
478 "which "
479 "required mode %#x but only had %#x itself.\n",
480 required_from_mode, *orig_from_mode);
481 return ffa_error(FFA_DENIED);
482 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000483 }
484
485 /* Find the appropriate new mode. */
486 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000487 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100488 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000489 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100490 break;
491
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100492 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000493 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100494 break;
495
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100496 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000497 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100498 break;
499
Jose Marinho75509b42019-04-09 09:34:59 +0100500 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100501 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100502 }
503
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100504 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000505}
506
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100507static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000508 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100509 struct ffa_memory_region_constituent **fragments,
510 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
511 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000512{
513 const uint32_t state_mask =
514 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
515 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100516 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000517
Andrew Walbranca808b12020-05-15 17:22:28 +0100518 ret = constituents_get_mode(from, orig_from_mode, fragments,
519 fragment_constituent_counts,
520 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100521 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100522 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000523 }
524
525 /* Ensure the address range is normal memory and not a device. */
526 if (*orig_from_mode & MM_MODE_D) {
527 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
528 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100529 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000530 }
531
532 /*
533 * Ensure the relinquishing VM is not the owner but has access to the
534 * memory.
535 */
536 orig_from_state = *orig_from_mode & state_mask;
537 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
538 dlog_verbose(
539 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100540 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000541 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100542 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000543 }
544
545 /* Find the appropriate new mode. */
546 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
547
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100548 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000549}
550
551/**
552 * Verify that all pages have the same mode, that the starting mode
553 * constitutes a valid state and obtain the next mode to apply
554 * to the retrieving VM.
555 *
556 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100557 * 1) FFA_DENIED if a state transition was not found;
558 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100559 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100560 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100561 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100562 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
563 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000564 */
J-Alvesfc19b372022-07-06 12:17:35 +0100565struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000566 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100567 struct ffa_memory_region_constituent **fragments,
568 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
569 uint32_t memory_to_attributes, uint32_t *to_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000570{
571 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100572 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000573
Andrew Walbranca808b12020-05-15 17:22:28 +0100574 ret = constituents_get_mode(to, &orig_to_mode, fragments,
575 fragment_constituent_counts,
576 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100577 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100578 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100579 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000580 }
581
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100582 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000583 /*
584 * If the original ffa memory send call has been processed
585 * successfully, it is expected the orig_to_mode would overlay
586 * with `state_mask`, as a result of the function
587 * `ffa_send_check_transition`.
588 */
J-Alves59ed0042022-07-28 18:26:41 +0100589 if (vm_id_is_current_world(to.vm->id)) {
590 assert((orig_to_mode &
591 (MM_MODE_INVALID | MM_MODE_UNOWNED |
592 MM_MODE_SHARED)) != 0U);
593 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000594 } else {
595 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +0100596 * If the retriever is from virtual FF-A instance:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000597 * Ensure the retriever has the expected state. We don't care
598 * about the MM_MODE_SHARED bit; either with or without it set
599 * are both valid representations of the !O-NA state.
600 */
J-Alvesa9cd7e32022-07-01 13:49:33 +0100601 if (vm_id_is_current_world(to.vm->id) &&
602 to.vm->id != HF_PRIMARY_VM_ID &&
603 (orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
604 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100605 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000606 }
607 }
608
609 /* Find the appropriate new mode. */
610 *to_mode = memory_to_attributes;
611 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100612 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000613 *to_mode |= 0;
614 break;
615
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100616 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000617 *to_mode |= MM_MODE_UNOWNED;
618 break;
619
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100620 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000621 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
622 break;
623
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100624 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000625 *to_mode |= 0;
626 break;
627
628 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100629 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100630 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000631 }
632
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100633 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100634}
Jose Marinho09b1db82019-08-08 09:16:59 +0100635
636/**
637 * Updates a VM's page table such that the given set of physical address ranges
638 * are mapped in the address space at the corresponding address ranges, in the
639 * mode provided.
640 *
641 * If commit is false, the page tables will be allocated from the mpool but no
642 * mappings will actually be updated. This function must always be called first
643 * with commit false to check that it will succeed before calling with commit
644 * true, to avoid leaving the page table in a half-updated state. To make a
645 * series of changes atomically you can call them all with commit false before
646 * calling them all with commit true.
647 *
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700648 * vm_ptable_defrag should always be called after a series of page table
649 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +0100650 *
651 * Returns true on success, or false if the update failed and no changes were
652 * made to memory mappings.
653 */
J-Alves66652252022-07-06 09:49:51 +0100654bool ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000655 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100656 struct ffa_memory_region_constituent **fragments,
657 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby4dd3f532021-09-21 09:57:08 +0100658 uint32_t mode, struct mpool *ppool, bool commit)
Jose Marinho09b1db82019-08-08 09:16:59 +0100659{
Andrew Walbranca808b12020-05-15 17:22:28 +0100660 uint32_t i;
661 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100662
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700663 if (vm_locked.vm->el0_partition) {
664 mode |= MM_MODE_USER | MM_MODE_NG;
665 }
666
Andrew Walbranca808b12020-05-15 17:22:28 +0100667 /* Iterate over the memory region constituents within each fragment. */
668 for (i = 0; i < fragment_count; ++i) {
669 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
670 size_t size = fragments[i][j].page_count * PAGE_SIZE;
671 paddr_t pa_begin =
672 pa_from_ipa(ipa_init(fragments[i][j].address));
673 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +0200674 uint32_t pa_bits =
675 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +0100676
677 /*
678 * Ensure the requested region falls into system's PA
679 * range.
680 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +0200681 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
682 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +0100683 dlog_error("Region is outside of PA Range\n");
684 return false;
685 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100686
687 if (commit) {
688 vm_identity_commit(vm_locked, pa_begin, pa_end,
689 mode, ppool, NULL);
690 } else if (!vm_identity_prepare(vm_locked, pa_begin,
691 pa_end, mode, ppool)) {
692 return false;
693 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100694 }
695 }
696
697 return true;
698}
699
700/**
701 * Clears a region of physical memory by overwriting it with zeros. The data is
702 * flushed from the cache so the memory has been cleared across the system.
703 */
J-Alves7db32002021-12-14 14:44:50 +0000704static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
705 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +0100706{
707 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000708 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100709 * global mapping of the whole range. Such an approach will limit
710 * the changes to stage-1 tables and will allow only local
711 * invalidation.
712 */
713 bool ret;
714 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +0000715 void *ptr = mm_identity_map(stage1_locked, begin, end,
716 MM_MODE_W | (extra_mode_attributes &
717 plat_ffa_other_world_mode()),
718 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100719 size_t size = pa_difference(begin, end);
720
721 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100722 goto fail;
723 }
724
725 memset_s(ptr, size, 0, size);
726 arch_mm_flush_dcache(ptr, size);
727 mm_unmap(stage1_locked, begin, end, ppool);
728
729 ret = true;
730 goto out;
731
732fail:
733 ret = false;
734
735out:
736 mm_unlock_stage1(&stage1_locked);
737
738 return ret;
739}
740
741/**
742 * Clears a region of physical memory by overwriting it with zeros. The data is
743 * flushed from the cache so the memory has been cleared across the system.
744 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100745static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +0000746 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100747 struct ffa_memory_region_constituent **fragments,
748 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
749 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100750{
751 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +0100752 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100753 bool ret = false;
754
755 /*
756 * Create a local pool so any freed memory can't be used by another
757 * thread. This is to ensure each constituent that is mapped can be
758 * unmapped again afterwards.
759 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000760 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100761
Andrew Walbranca808b12020-05-15 17:22:28 +0100762 /* Iterate over the memory region constituents within each fragment. */
763 for (i = 0; i < fragment_count; ++i) {
764 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100765
Andrew Walbranca808b12020-05-15 17:22:28 +0100766 for (j = 0; j < fragment_constituent_counts[j]; ++j) {
767 size_t size = fragments[i][j].page_count * PAGE_SIZE;
768 paddr_t begin =
769 pa_from_ipa(ipa_init(fragments[i][j].address));
770 paddr_t end = pa_add(begin, size);
771
J-Alves7db32002021-12-14 14:44:50 +0000772 if (!clear_memory(begin, end, &local_page_pool,
773 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100774 /*
775 * api_clear_memory will defrag on failure, so
776 * no need to do it here.
777 */
778 goto out;
779 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100780 }
781 }
782
Jose Marinho09b1db82019-08-08 09:16:59 +0100783 ret = true;
784
785out:
786 mpool_fini(&local_page_pool);
787 return ret;
788}
789
790/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000791 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +0100792 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000793 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +0100794 *
795 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000796 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100797 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +0100798 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100799 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
800 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100801 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +0100802 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100803 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +0100804 */
J-Alves66652252022-07-06 09:49:51 +0100805struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000806 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100807 struct ffa_memory_region_constituent **fragments,
808 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves363f5722022-04-25 17:37:37 +0100809 uint32_t share_func, struct ffa_memory_access *receivers,
810 uint32_t receivers_count, struct mpool *page_pool, bool clear,
811 uint32_t *orig_from_mode_ret)
Jose Marinho09b1db82019-08-08 09:16:59 +0100812{
Andrew Walbranca808b12020-05-15 17:22:28 +0100813 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100814 uint32_t orig_from_mode;
815 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +0100816 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100817 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100818
819 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +0100820 * Make sure constituents are properly aligned to a 64-bit boundary. If
821 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100822 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100823 for (i = 0; i < fragment_count; ++i) {
824 if (!is_aligned(fragments[i], 8)) {
825 dlog_verbose("Constituents not aligned.\n");
826 return ffa_error(FFA_INVALID_PARAMETERS);
827 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100828 }
829
830 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000831 * Check if the state transition is lawful for the sender, ensure that
832 * all constituents of a memory region being shared are at the same
833 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +0100834 */
J-Alves363f5722022-04-25 17:37:37 +0100835 ret = ffa_send_check_transition(from_locked, share_func, receivers,
836 receivers_count, &orig_from_mode,
837 fragments, fragment_constituent_counts,
Andrew Walbranca808b12020-05-15 17:22:28 +0100838 fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100839 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100840 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100841 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100842 }
843
Andrew Walbran37c574e2020-06-03 11:45:46 +0100844 if (orig_from_mode_ret != NULL) {
845 *orig_from_mode_ret = orig_from_mode;
846 }
847
Jose Marinho09b1db82019-08-08 09:16:59 +0100848 /*
849 * Create a local pool so any freed memory can't be used by another
850 * thread. This is to ensure the original mapping can be restored if the
851 * clear fails.
852 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000853 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100854
855 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000856 * First reserve all required memory for the new page table entries
857 * without committing, to make sure the entire operation will succeed
858 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +0100859 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100860 if (!ffa_region_group_identity_map(
861 from_locked, fragments, fragment_constituent_counts,
862 fragment_count, from_mode, page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100863 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100864 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100865 goto out;
866 }
867
868 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000869 * Update the mapping for the sender. This won't allocate because the
870 * transaction was already prepared above, but may free pages in the
871 * case that a whole block is being unmapped that was previously
872 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +0100873 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100874 CHECK(ffa_region_group_identity_map(
875 from_locked, fragments, fragment_constituent_counts,
876 fragment_count, from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100877
878 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +0000879 if (clear &&
880 !ffa_clear_memory_constituents(
881 plat_ffa_owner_world_mode(from_locked.vm->id), fragments,
882 fragment_constituent_counts, fragment_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100883 /*
884 * On failure, roll back by returning memory to the sender. This
885 * may allocate pages which were previously freed into
886 * `local_page_pool` by the call above, but will never allocate
887 * more pages than that so can never fail.
888 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100889 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +0100890 from_locked, fragments, fragment_constituent_counts,
891 fragment_count, orig_from_mode, &local_page_pool,
892 true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100893
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100894 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100895 goto out;
896 }
897
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100898 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000899
900out:
901 mpool_fini(&local_page_pool);
902
903 /*
904 * Tidy up the page table by reclaiming failed mappings (if there was an
905 * error) or merging entries into blocks where possible (on success).
906 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700907 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000908
909 return ret;
910}
911
912/**
913 * Validates and maps memory shared from one VM to another.
914 *
915 * This function requires the calling context to hold the <to> lock.
916 *
917 * Returns:
918 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100919 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000920 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100921 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000922 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100923 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000924 */
J-Alvesb5084cf2022-07-06 14:20:12 +0100925struct ffa_value ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +0000926 struct vm_locked to_locked, ffa_vm_id_t from_id,
Andrew Walbranca808b12020-05-15 17:22:28 +0100927 struct ffa_memory_region_constituent **fragments,
928 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
929 uint32_t memory_to_attributes, uint32_t share_func, bool clear,
930 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000931{
Andrew Walbranca808b12020-05-15 17:22:28 +0100932 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000933 uint32_t to_mode;
934 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100935 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000936
937 /*
Andrew Walbranca808b12020-05-15 17:22:28 +0100938 * Make sure constituents are properly aligned to a 64-bit boundary. If
939 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000940 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100941 for (i = 0; i < fragment_count; ++i) {
942 if (!is_aligned(fragments[i], 8)) {
J-Alvesb5084cf2022-07-06 14:20:12 +0100943 dlog_verbose("Fragment not properly aligned.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +0100944 return ffa_error(FFA_INVALID_PARAMETERS);
945 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000946 }
947
948 /*
949 * Check if the state transition is lawful for the recipient, and ensure
950 * that all constituents of the memory region being retrieved are at the
951 * same state.
952 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100953 ret = ffa_retrieve_check_transition(
954 to_locked, share_func, fragments, fragment_constituent_counts,
955 fragment_count, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100956 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100957 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100958 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000959 }
960
961 /*
962 * Create a local pool so any freed memory can't be used by another
963 * thread. This is to ensure the original mapping can be restored if the
964 * clear fails.
965 */
966 mpool_init_with_fallback(&local_page_pool, page_pool);
967
968 /*
969 * First reserve all required memory for the new page table entries in
970 * the recipient page tables without committing, to make sure the entire
971 * operation will succeed without exhausting the page pool.
972 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100973 if (!ffa_region_group_identity_map(
974 to_locked, fragments, fragment_constituent_counts,
975 fragment_count, to_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000976 /* TODO: partial defrag of failed range. */
977 dlog_verbose(
978 "Insufficient memory to update recipient page "
979 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100980 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000981 goto out;
982 }
983
984 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +0000985 if (clear &&
986 !ffa_clear_memory_constituents(
987 plat_ffa_owner_world_mode(from_id), fragments,
988 fragment_constituent_counts, fragment_count, page_pool)) {
J-Alvesb5084cf2022-07-06 14:20:12 +0100989 dlog_verbose("Couldn't clear constituents.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100990 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000991 goto out;
992 }
993
Jose Marinho09b1db82019-08-08 09:16:59 +0100994 /*
995 * Complete the transfer by mapping the memory into the recipient. This
996 * won't allocate because the transaction was already prepared above, so
997 * it doesn't need to use the `local_page_pool`.
998 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100999 CHECK(ffa_region_group_identity_map(
1000 to_locked, fragments, fragment_constituent_counts,
1001 fragment_count, to_mode, page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001002
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001003 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001004
1005out:
1006 mpool_fini(&local_page_pool);
1007
1008 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001009 * Tidy up the page table by reclaiming failed mappings (if there was an
1010 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001011 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001012 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001013
1014 return ret;
1015}
1016
Andrew Walbran996d1d12020-05-27 14:08:43 +01001017static struct ffa_value ffa_relinquish_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001018 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001019 struct ffa_memory_region_constituent **fragments,
1020 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1021 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001022{
1023 uint32_t orig_from_mode;
1024 uint32_t from_mode;
1025 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001026 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001027
Andrew Walbranca808b12020-05-15 17:22:28 +01001028 ret = ffa_relinquish_check_transition(
1029 from_locked, &orig_from_mode, fragments,
1030 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001031 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001032 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001033 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001034 }
1035
1036 /*
1037 * Create a local pool so any freed memory can't be used by another
1038 * thread. This is to ensure the original mapping can be restored if the
1039 * clear fails.
1040 */
1041 mpool_init_with_fallback(&local_page_pool, page_pool);
1042
1043 /*
1044 * First reserve all required memory for the new page table entries
1045 * without committing, to make sure the entire operation will succeed
1046 * without exhausting the page pool.
1047 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001048 if (!ffa_region_group_identity_map(
1049 from_locked, fragments, fragment_constituent_counts,
1050 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001051 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001052 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001053 goto out;
1054 }
1055
1056 /*
1057 * Update the mapping for the sender. This won't allocate because the
1058 * transaction was already prepared above, but may free pages in the
1059 * case that a whole block is being unmapped that was previously
1060 * partially mapped.
1061 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001062 CHECK(ffa_region_group_identity_map(
1063 from_locked, fragments, fragment_constituent_counts,
1064 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001065
1066 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001067 if (clear &&
1068 !ffa_clear_memory_constituents(
1069 plat_ffa_owner_world_mode(from_locked.vm->id), fragments,
1070 fragment_constituent_counts, fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001071 /*
1072 * On failure, roll back by returning memory to the sender. This
1073 * may allocate pages which were previously freed into
1074 * `local_page_pool` by the call above, but will never allocate
1075 * more pages than that so can never fail.
1076 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001077 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001078 from_locked, fragments, fragment_constituent_counts,
1079 fragment_count, orig_from_mode, &local_page_pool,
1080 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001081
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001082 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001083 goto out;
1084 }
1085
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001086 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001087
1088out:
1089 mpool_fini(&local_page_pool);
1090
1091 /*
1092 * Tidy up the page table by reclaiming failed mappings (if there was an
1093 * error) or merging entries into blocks where possible (on success).
1094 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001095 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001096
1097 return ret;
1098}
1099
1100/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001101 * Complete a memory sending operation by checking that it is valid, updating
1102 * the sender page table, and then either marking the share state as having
1103 * completed sending (on success) or freeing it (on failure).
1104 *
1105 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1106 */
J-Alvesfdd29272022-07-19 13:16:31 +01001107struct ffa_value ffa_memory_send_complete(
Andrew Walbranca808b12020-05-15 17:22:28 +01001108 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001109 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1110 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001111{
1112 struct ffa_memory_region *memory_region = share_state->memory_region;
1113 struct ffa_value ret;
1114
1115 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001116 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001117
1118 /* Check that state is valid in sender page table and update. */
1119 ret = ffa_send_check_update(
1120 from_locked, share_state->fragments,
1121 share_state->fragment_constituent_counts,
1122 share_state->fragment_count, share_state->share_func,
J-Alves363f5722022-04-25 17:37:37 +01001123 memory_region->receivers, memory_region->receiver_count,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001124 page_pool, memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1125 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001126 if (ret.func != FFA_SUCCESS_32) {
1127 /*
1128 * Free share state, it failed to send so it can't be retrieved.
1129 */
1130 dlog_verbose("Complete failed, freeing share state.\n");
1131 share_state_free(share_states, share_state, page_pool);
1132 return ret;
1133 }
1134
1135 share_state->sending_complete = true;
1136 dlog_verbose("Marked sending complete.\n");
1137
J-Alvesee68c542020-10-29 17:48:20 +00001138 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001139}
1140
1141/**
Federico Recanatia98603a2021-12-20 18:04:03 +01001142 * Check that the memory attributes match Hafnium expectations:
1143 * Normal Memory, Inner shareable, Write-Back Read-Allocate
1144 * Write-Allocate Cacheable.
1145 */
1146static struct ffa_value ffa_memory_attributes_validate(
J-Alves7a99d0d2023-02-08 13:49:48 +00001147 ffa_memory_attributes_t attributes)
Federico Recanatia98603a2021-12-20 18:04:03 +01001148{
1149 enum ffa_memory_type memory_type;
1150 enum ffa_memory_cacheability cacheability;
1151 enum ffa_memory_shareability shareability;
1152
1153 memory_type = ffa_get_memory_type_attr(attributes);
1154 if (memory_type != FFA_MEMORY_NORMAL_MEM) {
1155 dlog_verbose("Invalid memory type %#x, expected %#x.\n",
1156 memory_type, FFA_MEMORY_NORMAL_MEM);
Federico Recanati3d953f32022-02-17 09:31:29 +01001157 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001158 }
1159
1160 cacheability = ffa_get_memory_cacheability_attr(attributes);
1161 if (cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1162 dlog_verbose("Invalid cacheability %#x, expected %#x.\n",
1163 cacheability, FFA_MEMORY_CACHE_WRITE_BACK);
Federico Recanati3d953f32022-02-17 09:31:29 +01001164 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001165 }
1166
1167 shareability = ffa_get_memory_shareability_attr(attributes);
1168 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
1169 dlog_verbose("Invalid shareability %#x, expected #%x.\n",
1170 shareability, FFA_MEMORY_INNER_SHAREABLE);
Federico Recanati3d953f32022-02-17 09:31:29 +01001171 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001172 }
1173
1174 return (struct ffa_value){.func = FFA_SUCCESS_32};
1175}
1176
1177/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001178 * Check that the given `memory_region` represents a valid memory send request
1179 * of the given `share_func` type, return the clear flag and permissions via the
1180 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001181 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001182 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001183 * not.
1184 */
J-Alves66652252022-07-06 09:49:51 +01001185struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001186 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1187 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001188 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001189{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001190 struct ffa_composite_memory_region *composite;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001191 uint32_t receivers_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001192 uint32_t composite_memory_region_offset;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001193 uint32_t constituents_offset;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001194 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001195 enum ffa_data_access data_access;
1196 enum ffa_instruction_access instruction_access;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001197 enum ffa_memory_security security_state;
Federico Recanatia98603a2021-12-20 18:04:03 +01001198 struct ffa_value ret;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001199
J-Alves0b6653d2022-04-22 13:17:38 +01001200 assert(memory_region->receivers_offset ==
1201 offsetof(struct ffa_memory_region, receivers));
1202 assert(memory_region->memory_access_desc_size ==
1203 sizeof(struct ffa_memory_access));
1204
J-Alves95df0ef2022-12-07 10:09:48 +00001205 /* The sender must match the caller. */
1206 if ((!vm_id_is_current_world(from_locked.vm->id) &&
1207 vm_id_is_current_world(memory_region->sender)) ||
1208 (vm_id_is_current_world(from_locked.vm->id) &&
1209 memory_region->sender != from_locked.vm->id)) {
1210 dlog_verbose("Invalid memory sender ID.\n");
1211 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001212 }
1213
Andrew Walbrana65a1322020-04-06 19:32:32 +01001214 /*
1215 * Ensure that the composite header is within the memory bounds and
1216 * doesn't overlap the first part of the message.
1217 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001218 receivers_length = sizeof(struct ffa_memory_access) *
1219 memory_region->receiver_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001220 constituents_offset =
1221 ffa_composite_constituent_offset(memory_region, 0);
Federico Recanati872cd692022-01-05 13:10:10 +01001222 composite_memory_region_offset =
1223 memory_region->receivers[0].composite_memory_region_offset;
1224 if ((composite_memory_region_offset == 0) ||
1225 (composite_memory_region_offset <
1226 sizeof(struct ffa_memory_region) + receivers_length) ||
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001227 constituents_offset > fragment_length) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001228 dlog_verbose(
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001229 "Invalid composite memory region descriptor offset "
1230 "%d.\n",
1231 memory_region->receivers[0]
1232 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001233 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001234 }
1235
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001236 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001237
1238 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001239 * Ensure the number of constituents are within the memory bounds.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001240 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001241 constituents_length = sizeof(struct ffa_memory_region_constituent) *
1242 composite->constituent_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001243 if (memory_share_length != constituents_offset + constituents_length) {
1244 dlog_verbose("Invalid length %d or composite offset %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001245 memory_share_length,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001246 memory_region->receivers[0]
1247 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001248 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001249 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001250 if (fragment_length < memory_share_length &&
1251 fragment_length < HF_MAILBOX_SIZE) {
1252 dlog_warning(
1253 "Initial fragment length %d smaller than mailbox "
1254 "size.\n",
1255 fragment_length);
1256 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001257
Andrew Walbrana65a1322020-04-06 19:32:32 +01001258 /*
1259 * Clear is not allowed for memory sharing, as the sender still has
1260 * access to the memory.
1261 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001262 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1263 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001264 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001265 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001266 }
1267
1268 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001269 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001270 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001271 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001272 }
1273
J-Alves363f5722022-04-25 17:37:37 +01001274 /* Check that the permissions are valid, for each specified receiver. */
1275 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
1276 ffa_memory_access_permissions_t permissions =
1277 memory_region->receivers[i]
1278 .receiver_permissions.permissions;
1279 ffa_vm_id_t receiver_id =
1280 memory_region->receivers[i]
1281 .receiver_permissions.receiver;
1282
1283 if (memory_region->sender == receiver_id) {
1284 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001285 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001286 }
Federico Recanati85090c42021-12-15 13:17:54 +01001287
J-Alves363f5722022-04-25 17:37:37 +01001288 for (uint32_t j = i + 1; j < memory_region->receiver_count;
1289 j++) {
1290 if (receiver_id ==
1291 memory_region->receivers[j]
1292 .receiver_permissions.receiver) {
1293 dlog_verbose(
1294 "Repeated receiver(%x) in memory send "
1295 "operation.\n",
1296 memory_region->receivers[j]
1297 .receiver_permissions.receiver);
1298 return ffa_error(FFA_INVALID_PARAMETERS);
1299 }
1300 }
1301
1302 if (composite_memory_region_offset !=
1303 memory_region->receivers[i]
1304 .composite_memory_region_offset) {
1305 dlog_verbose(
1306 "All ffa_memory_access should point to the "
1307 "same composite memory region offset.\n");
1308 return ffa_error(FFA_INVALID_PARAMETERS);
1309 }
1310
1311 data_access = ffa_get_data_access_attr(permissions);
1312 instruction_access =
1313 ffa_get_instruction_access_attr(permissions);
1314 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1315 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
1316 dlog_verbose(
1317 "Reserved value for receiver permissions "
1318 "%#x.\n",
1319 permissions);
1320 return ffa_error(FFA_INVALID_PARAMETERS);
1321 }
1322 if (instruction_access !=
1323 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
1324 dlog_verbose(
1325 "Invalid instruction access permissions %#x "
1326 "for sending memory.\n",
1327 permissions);
1328 return ffa_error(FFA_INVALID_PARAMETERS);
1329 }
1330 if (share_func == FFA_MEM_SHARE_32) {
1331 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1332 dlog_verbose(
1333 "Invalid data access permissions %#x "
1334 "for sharing memory.\n",
1335 permissions);
1336 return ffa_error(FFA_INVALID_PARAMETERS);
1337 }
1338 /*
1339 * According to section 10.10.3 of the FF-A v1.1 EAC0
1340 * spec, NX is required for share operations (but must
1341 * not be specified by the sender) so set it in the
1342 * copy that we store, ready to be returned to the
1343 * retriever.
1344 */
J-Alvesb19731a2022-06-20 17:30:33 +01001345 if (vm_id_is_current_world(receiver_id)) {
1346 ffa_set_instruction_access_attr(
1347 &permissions,
1348 FFA_INSTRUCTION_ACCESS_NX);
1349 memory_region->receivers[i]
1350 .receiver_permissions.permissions =
1351 permissions;
1352 }
J-Alves363f5722022-04-25 17:37:37 +01001353 }
1354 if (share_func == FFA_MEM_LEND_32 &&
1355 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1356 dlog_verbose(
1357 "Invalid data access permissions %#x for "
1358 "lending memory.\n",
1359 permissions);
1360 return ffa_error(FFA_INVALID_PARAMETERS);
1361 }
1362
1363 if (share_func == FFA_MEM_DONATE_32 &&
1364 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
1365 dlog_verbose(
1366 "Invalid data access permissions %#x for "
1367 "donating memory.\n",
1368 permissions);
1369 return ffa_error(FFA_INVALID_PARAMETERS);
1370 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001371 }
1372
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001373 /* Memory region attributes NS-Bit MBZ for FFA_MEM_SHARE/LEND/DONATE. */
1374 security_state =
1375 ffa_get_memory_security_attr(memory_region->attributes);
1376 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
1377 dlog_verbose(
1378 "Invalid security state for memory share operation.\n");
1379 return ffa_error(FFA_INVALID_PARAMETERS);
1380 }
1381
Federico Recanatid937f5e2021-12-20 17:38:23 +01001382 /*
J-Alves807794e2022-06-16 13:42:47 +01001383 * If a memory donate or lend with single borrower, the memory type
1384 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01001385 */
J-Alves807794e2022-06-16 13:42:47 +01001386 if (share_func == FFA_MEM_DONATE_32 ||
1387 (share_func == FFA_MEM_LEND_32 &&
1388 memory_region->receiver_count == 1)) {
1389 if (ffa_get_memory_type_attr(memory_region->attributes) !=
1390 FFA_MEMORY_NOT_SPECIFIED_MEM) {
1391 dlog_verbose(
1392 "Memory type shall not be specified by "
1393 "sender.\n");
1394 return ffa_error(FFA_INVALID_PARAMETERS);
1395 }
1396 } else {
1397 /*
1398 * Check that sender's memory attributes match Hafnium
1399 * expectations: Normal Memory, Inner shareable, Write-Back
1400 * Read-Allocate Write-Allocate Cacheable.
1401 */
1402 ret = ffa_memory_attributes_validate(memory_region->attributes);
1403 if (ret.func != FFA_SUCCESS_32) {
1404 return ret;
1405 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01001406 }
1407
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001408 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001409}
1410
1411/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001412 * Gets the share state for continuing an operation to donate, lend or share
1413 * memory, and checks that it is a valid request.
1414 *
1415 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1416 * not.
1417 */
J-Alvesfdd29272022-07-19 13:16:31 +01001418struct ffa_value ffa_memory_send_continue_validate(
Andrew Walbranca808b12020-05-15 17:22:28 +01001419 struct share_states_locked share_states, ffa_memory_handle_t handle,
1420 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1421 struct mpool *page_pool)
1422{
1423 struct ffa_memory_share_state *share_state;
1424 struct ffa_memory_region *memory_region;
1425
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001426 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001427
1428 /*
1429 * Look up the share state by handle and make sure that the VM ID
1430 * matches.
1431 */
1432 if (!get_share_state(share_states, handle, &share_state)) {
1433 dlog_verbose(
1434 "Invalid handle %#x for memory send continuation.\n",
1435 handle);
1436 return ffa_error(FFA_INVALID_PARAMETERS);
1437 }
1438 memory_region = share_state->memory_region;
1439
J-Alvesfdd29272022-07-19 13:16:31 +01001440 if (vm_id_is_current_world(from_vm_id) &&
1441 memory_region->sender != from_vm_id) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001442 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1443 return ffa_error(FFA_INVALID_PARAMETERS);
1444 }
1445
1446 if (share_state->sending_complete) {
1447 dlog_verbose(
1448 "Sending of memory handle %#x is already complete.\n",
1449 handle);
1450 return ffa_error(FFA_INVALID_PARAMETERS);
1451 }
1452
1453 if (share_state->fragment_count == MAX_FRAGMENTS) {
1454 /*
1455 * Log a warning as this is a sign that MAX_FRAGMENTS should
1456 * probably be increased.
1457 */
1458 dlog_warning(
1459 "Too many fragments for memory share with handle %#x; "
1460 "only %d supported.\n",
1461 handle, MAX_FRAGMENTS);
1462 /* Free share state, as it's not possible to complete it. */
1463 share_state_free(share_states, share_state, page_pool);
1464 return ffa_error(FFA_NO_MEMORY);
1465 }
1466
1467 *share_state_ret = share_state;
1468
1469 return (struct ffa_value){.func = FFA_SUCCESS_32};
1470}
1471
1472/**
J-Alves95df0ef2022-12-07 10:09:48 +00001473 * Checks if there is at least one receiver from the other world.
1474 */
J-Alvesfdd29272022-07-19 13:16:31 +01001475bool memory_region_receivers_from_other_world(
J-Alves95df0ef2022-12-07 10:09:48 +00001476 struct ffa_memory_region *memory_region)
1477{
1478 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
1479 ffa_vm_id_t receiver = memory_region->receivers[i]
1480 .receiver_permissions.receiver;
1481 if (!vm_id_is_current_world(receiver)) {
1482 return true;
1483 }
1484 }
1485 return false;
1486}
1487
1488/**
J-Alves8505a8a2022-06-15 18:10:18 +01001489 * Validates a call to donate, lend or share memory to a non-other world VM and
1490 * then updates the stage-2 page tables. Specifically, check if the message
1491 * length and number of memory region constituents match, and if the transition
1492 * is valid for the type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001493 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001494 * Assumes that the caller has already found and locked the sender VM and copied
1495 * the memory region descriptor from the sender's TX buffer to a freshly
1496 * allocated page from Hafnium's internal pool. The caller must have also
1497 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001498 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001499 * This function takes ownership of the `memory_region` passed in and will free
1500 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001501 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001502struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001503 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001504 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001505 uint32_t fragment_length, uint32_t share_func,
1506 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001507{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001508 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001509 struct share_states_locked share_states;
1510 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001511
1512 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001513 * If there is an error validating the `memory_region` then we need to
1514 * free it because we own it but we won't be storing it in a share state
1515 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001516 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001517 ret = ffa_memory_send_validate(from_locked, memory_region,
1518 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001519 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001520 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001521 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001522 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001523 }
1524
Andrew Walbrana65a1322020-04-06 19:32:32 +01001525 /* Set flag for share function, ready to be retrieved later. */
1526 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001527 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001528 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001529 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001530 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001531 case FFA_MEM_LEND_32:
1532 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001533 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001534 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001535 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001536 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001537 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001538 }
1539
Andrew Walbranca808b12020-05-15 17:22:28 +01001540 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001541 /*
1542 * Allocate a share state before updating the page table. Otherwise if
1543 * updating the page table succeeded but allocating the share state
1544 * failed then it would leave the memory in a state where nobody could
1545 * get it back.
1546 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001547 if (!allocate_share_state(share_states, share_func, memory_region,
1548 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1549 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001550 dlog_verbose("Failed to allocate share state.\n");
1551 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001552 ret = ffa_error(FFA_NO_MEMORY);
1553 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001554 }
1555
Andrew Walbranca808b12020-05-15 17:22:28 +01001556 if (fragment_length == memory_share_length) {
1557 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00001558 ret = ffa_memory_send_complete(
1559 from_locked, share_states, share_state, page_pool,
1560 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001561 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01001562 /*
1563 * Use sender ID from 'memory_region' assuming
1564 * that at this point it has been validated:
1565 * - MBZ at virtual FF-A instance.
1566 */
1567 ffa_vm_id_t sender_to_ret =
1568 (from_locked.vm->id == HF_OTHER_WORLD_ID)
1569 ? memory_region->sender
1570 : 0;
Andrew Walbranca808b12020-05-15 17:22:28 +01001571 ret = (struct ffa_value){
1572 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00001573 .arg1 = (uint32_t)memory_region->handle,
1574 .arg2 = (uint32_t)(memory_region->handle >> 32),
J-Alvesfdd29272022-07-19 13:16:31 +01001575 .arg3 = fragment_length,
1576 .arg4 = (uint32_t)(sender_to_ret & 0xffff) << 16};
Andrew Walbranca808b12020-05-15 17:22:28 +01001577 }
1578
1579out:
1580 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001581 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001582 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001583}
1584
1585/**
J-Alves8505a8a2022-06-15 18:10:18 +01001586 * Continues an operation to donate, lend or share memory to a VM from current
1587 * world. If this is the last fragment then checks that the transition is valid
1588 * for the type of memory sending operation and updates the stage-2 page tables
1589 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01001590 *
1591 * Assumes that the caller has already found and locked the sender VM and copied
1592 * the memory region descriptor from the sender's TX buffer to a freshly
1593 * allocated page from Hafnium's internal pool.
1594 *
1595 * This function takes ownership of the `fragment` passed in; it must not be
1596 * freed by the caller.
1597 */
1598struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1599 void *fragment,
1600 uint32_t fragment_length,
1601 ffa_memory_handle_t handle,
1602 struct mpool *page_pool)
1603{
1604 struct share_states_locked share_states = share_states_lock();
1605 struct ffa_memory_share_state *share_state;
1606 struct ffa_value ret;
1607 struct ffa_memory_region *memory_region;
1608
1609 ret = ffa_memory_send_continue_validate(share_states, handle,
1610 &share_state,
1611 from_locked.vm->id, page_pool);
1612 if (ret.func != FFA_SUCCESS_32) {
1613 goto out_free_fragment;
1614 }
1615 memory_region = share_state->memory_region;
1616
J-Alves95df0ef2022-12-07 10:09:48 +00001617 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001618 dlog_error(
1619 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01001620 "other world. This should never happen, and indicates "
1621 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01001622 "EL3 code.\n");
1623 ret = ffa_error(FFA_INVALID_PARAMETERS);
1624 goto out_free_fragment;
1625 }
1626
1627 /* Add this fragment. */
1628 share_state->fragments[share_state->fragment_count] = fragment;
1629 share_state->fragment_constituent_counts[share_state->fragment_count] =
1630 fragment_length / sizeof(struct ffa_memory_region_constituent);
1631 share_state->fragment_count++;
1632
1633 /* Check whether the memory send operation is now ready to complete. */
1634 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00001635 ret = ffa_memory_send_complete(
1636 from_locked, share_states, share_state, page_pool,
1637 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001638 } else {
1639 ret = (struct ffa_value){
1640 .func = FFA_MEM_FRAG_RX_32,
1641 .arg1 = (uint32_t)handle,
1642 .arg2 = (uint32_t)(handle >> 32),
1643 .arg3 = share_state_next_fragment_offset(share_states,
1644 share_state)};
1645 }
1646 goto out;
1647
1648out_free_fragment:
1649 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001650
1651out:
Andrew Walbranca808b12020-05-15 17:22:28 +01001652 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001653 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001654}
1655
Andrew Walbranca808b12020-05-15 17:22:28 +01001656/** Clean up after the receiver has finished retrieving a memory region. */
1657static void ffa_memory_retrieve_complete(
1658 struct share_states_locked share_states,
1659 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
1660{
1661 if (share_state->share_func == FFA_MEM_DONATE_32) {
1662 /*
1663 * Memory that has been donated can't be relinquished,
1664 * so no need to keep the share state around.
1665 */
1666 share_state_free(share_states, share_state, page_pool);
1667 dlog_verbose("Freed share state for donate.\n");
1668 }
1669}
1670
J-Alves2d8457f2022-10-05 11:06:41 +01001671/**
1672 * Initialises the given memory region descriptor to be used for an
1673 * `FFA_MEM_RETRIEVE_RESP`, including the given constituents for the first
1674 * fragment.
1675 * The memory region descriptor is initialized according to retriever's
1676 * FF-A version.
1677 *
1678 * Returns true on success, or false if the given constituents won't all fit in
1679 * the first fragment.
1680 */
1681static bool ffa_retrieved_memory_region_init(
1682 void *response, uint32_t ffa_version, size_t response_max_size,
1683 ffa_vm_id_t sender, ffa_memory_attributes_t attributes,
1684 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
1685 ffa_vm_id_t receiver_id, ffa_memory_access_permissions_t permissions,
1686 uint32_t page_count, uint32_t total_constituent_count,
1687 const struct ffa_memory_region_constituent constituents[],
1688 uint32_t fragment_constituent_count, uint32_t *total_length,
1689 uint32_t *fragment_length)
1690{
1691 struct ffa_composite_memory_region *composite_memory_region;
1692 struct ffa_memory_access *receiver;
1693 uint32_t i;
1694 uint32_t constituents_offset;
1695 uint32_t receiver_count;
1696
1697 assert(response != NULL);
1698
1699 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1700 struct ffa_memory_region_v1_0 *retrieve_response =
1701 (struct ffa_memory_region_v1_0 *)response;
1702
1703 ffa_memory_region_init_header_v1_0(retrieve_response, sender,
1704 attributes, flags, handle, 0,
1705 1);
1706
1707 receiver = &retrieve_response->receivers[0];
1708 receiver_count = retrieve_response->receiver_count;
1709
1710 receiver->composite_memory_region_offset =
1711 sizeof(struct ffa_memory_region_v1_0) +
1712 receiver_count * sizeof(struct ffa_memory_access);
1713
1714 composite_memory_region = ffa_memory_region_get_composite_v1_0(
1715 retrieve_response, 0);
1716 } else {
1717 /* Default to FF-A v1.1 version. */
1718 struct ffa_memory_region *retrieve_response =
1719 (struct ffa_memory_region *)response;
1720
1721 ffa_memory_region_init_header(retrieve_response, sender,
1722 attributes, flags, handle, 0, 1);
1723
1724 receiver = &retrieve_response->receivers[0];
1725 receiver_count = retrieve_response->receiver_count;
1726
1727 /*
1728 * Note that `sizeof(struct_ffa_memory_region)` and
1729 * `sizeof(struct ffa_memory_access)` must both be multiples of
1730 * 16 (as verified by the asserts in `ffa_memory.c`, so it is
1731 * guaranteed that the offset we calculate here is aligned to a
1732 * 64-bit boundary and so 64-bit values can be copied without
1733 * alignment faults.
1734 */
1735 receiver->composite_memory_region_offset =
1736 sizeof(struct ffa_memory_region) +
1737 receiver_count * sizeof(struct ffa_memory_access);
1738
1739 composite_memory_region =
1740 ffa_memory_region_get_composite(retrieve_response, 0);
1741 }
1742
1743 assert(receiver != NULL);
1744 assert(composite_memory_region != NULL);
1745
1746 /*
1747 * Initialized here as in memory retrieve responses we currently expect
1748 * one borrower to be specified.
1749 */
1750 ffa_memory_access_init_permissions(receiver, receiver_id, 0, 0, flags);
1751 receiver->receiver_permissions.permissions = permissions;
1752
1753 composite_memory_region->page_count = page_count;
1754 composite_memory_region->constituent_count = total_constituent_count;
1755 composite_memory_region->reserved_0 = 0;
1756
1757 constituents_offset = receiver->composite_memory_region_offset +
1758 sizeof(struct ffa_composite_memory_region);
1759 if (constituents_offset +
1760 fragment_constituent_count *
1761 sizeof(struct ffa_memory_region_constituent) >
1762 response_max_size) {
1763 return false;
1764 }
1765
1766 for (i = 0; i < fragment_constituent_count; ++i) {
1767 composite_memory_region->constituents[i] = constituents[i];
1768 }
1769
1770 if (total_length != NULL) {
1771 *total_length =
1772 constituents_offset +
1773 composite_memory_region->constituent_count *
1774 sizeof(struct ffa_memory_region_constituent);
1775 }
1776 if (fragment_length != NULL) {
1777 *fragment_length =
1778 constituents_offset +
1779 fragment_constituent_count *
1780 sizeof(struct ffa_memory_region_constituent);
1781 }
1782
1783 return true;
1784}
1785
J-Alves96de29f2022-04-26 16:05:24 +01001786/*
1787 * Gets the receiver's access permissions from 'struct ffa_memory_region' and
1788 * returns its index in the receiver's array. If receiver's ID doesn't exist
1789 * in the array, return the region's 'receiver_count'.
1790 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001791uint32_t ffa_memory_region_get_receiver(struct ffa_memory_region *memory_region,
1792 ffa_vm_id_t receiver)
J-Alves96de29f2022-04-26 16:05:24 +01001793{
1794 struct ffa_memory_access *receivers;
1795 uint32_t i;
1796
1797 assert(memory_region != NULL);
1798
1799 receivers = memory_region->receivers;
1800
1801 for (i = 0U; i < memory_region->receiver_count; i++) {
1802 if (receivers[i].receiver_permissions.receiver == receiver) {
1803 break;
1804 }
1805 }
1806
1807 return i;
1808}
1809
1810/**
1811 * Validates the retrieved permissions against those specified by the lender
1812 * of memory share operation. Optionally can help set the permissions to be used
1813 * for the S2 mapping, through the `permissions` argument.
1814 * Returns true if permissions are valid, false otherwise.
1815 */
1816static bool ffa_memory_retrieve_is_memory_access_valid(
1817 enum ffa_data_access sent_data_access,
1818 enum ffa_data_access requested_data_access,
1819 enum ffa_instruction_access sent_instruction_access,
1820 enum ffa_instruction_access requested_instruction_access,
1821 ffa_memory_access_permissions_t *permissions)
1822{
1823 switch (sent_data_access) {
1824 case FFA_DATA_ACCESS_NOT_SPECIFIED:
1825 case FFA_DATA_ACCESS_RW:
1826 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
1827 requested_data_access == FFA_DATA_ACCESS_RW) {
1828 if (permissions != NULL) {
1829 ffa_set_data_access_attr(permissions,
1830 FFA_DATA_ACCESS_RW);
1831 }
1832 break;
1833 }
1834 /* Intentional fall-through. */
1835 case FFA_DATA_ACCESS_RO:
1836 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
1837 requested_data_access == FFA_DATA_ACCESS_RO) {
1838 if (permissions != NULL) {
1839 ffa_set_data_access_attr(permissions,
1840 FFA_DATA_ACCESS_RO);
1841 }
1842 break;
1843 }
1844 dlog_verbose(
1845 "Invalid data access requested; sender specified "
1846 "permissions %#x but receiver requested %#x.\n",
1847 sent_data_access, requested_data_access);
1848 return false;
1849 case FFA_DATA_ACCESS_RESERVED:
1850 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
1851 "checked before this point.");
1852 }
1853
1854 switch (sent_instruction_access) {
1855 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
1856 case FFA_INSTRUCTION_ACCESS_X:
1857 if (requested_instruction_access ==
1858 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
1859 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
1860 if (permissions != NULL) {
1861 ffa_set_instruction_access_attr(
1862 permissions, FFA_INSTRUCTION_ACCESS_X);
1863 }
1864 break;
1865 }
1866 case FFA_INSTRUCTION_ACCESS_NX:
1867 if (requested_instruction_access ==
1868 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
1869 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
1870 if (permissions != NULL) {
1871 ffa_set_instruction_access_attr(
1872 permissions, FFA_INSTRUCTION_ACCESS_NX);
1873 }
1874 break;
1875 }
1876 dlog_verbose(
1877 "Invalid instruction access requested; sender "
1878 "specified permissions %#x but receiver requested "
1879 "%#x.\n",
1880 sent_instruction_access, requested_instruction_access);
1881 return false;
1882 case FFA_INSTRUCTION_ACCESS_RESERVED:
1883 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
1884 "be checked before this point.");
1885 }
1886
1887 return true;
1888}
1889
1890/**
1891 * Validate the receivers' permissions in the retrieve request against those
1892 * specified by the lender.
1893 * In the `permissions` argument returns the permissions to set at S2 for the
1894 * caller to the FFA_MEMORY_RETRIEVE_REQ.
1895 * Returns FFA_SUCCESS if all specified permissions are valid.
1896 */
1897static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
1898 struct ffa_memory_region *memory_region,
1899 struct ffa_memory_region *retrieve_request, ffa_vm_id_t to_vm_id,
1900 ffa_memory_access_permissions_t *permissions)
1901{
1902 uint32_t retrieve_receiver_index;
1903
1904 assert(permissions != NULL);
1905
1906 if (retrieve_request->receiver_count != memory_region->receiver_count) {
1907 dlog_verbose(
1908 "Retrieve request should contain same list of "
1909 "borrowers, as specified by the lender.\n");
1910 return ffa_error(FFA_INVALID_PARAMETERS);
1911 }
1912
1913 retrieve_receiver_index = retrieve_request->receiver_count;
1914
1915 /* Should be populated with the permissions of the retriever. */
1916 *permissions = 0;
1917
1918 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
1919 ffa_memory_access_permissions_t sent_permissions;
1920 struct ffa_memory_access *current_receiver =
1921 &retrieve_request->receivers[i];
1922 ffa_memory_access_permissions_t requested_permissions =
1923 current_receiver->receiver_permissions.permissions;
1924 ffa_vm_id_t current_receiver_id =
1925 current_receiver->receiver_permissions.receiver;
1926 bool found_to_id = current_receiver_id == to_vm_id;
1927
1928 /*
1929 * Find the current receiver in the transaction descriptor from
1930 * sender.
1931 */
1932 uint32_t mem_region_receiver_index =
1933 ffa_memory_region_get_receiver(memory_region,
1934 current_receiver_id);
1935
1936 if (mem_region_receiver_index ==
1937 memory_region->receiver_count) {
1938 dlog_verbose("%s: receiver %x not found\n", __func__,
1939 current_receiver_id);
1940 return ffa_error(FFA_DENIED);
1941 }
1942
1943 sent_permissions =
1944 memory_region->receivers[mem_region_receiver_index]
1945 .receiver_permissions.permissions;
1946
1947 if (found_to_id) {
1948 retrieve_receiver_index = i;
1949 }
1950
1951 /*
1952 * Since we are traversing the list of receivers, save the index
1953 * of the caller. As it needs to be there.
1954 */
1955
1956 if (current_receiver->composite_memory_region_offset != 0U) {
1957 dlog_verbose(
1958 "Retriever specified address ranges not "
1959 "supported (got offset %d).\n",
1960 current_receiver
1961 ->composite_memory_region_offset);
1962 return ffa_error(FFA_INVALID_PARAMETERS);
1963 }
1964
1965 /*
1966 * Check permissions from sender against permissions requested
1967 * by receiver.
1968 */
1969 if (!ffa_memory_retrieve_is_memory_access_valid(
1970 ffa_get_data_access_attr(sent_permissions),
1971 ffa_get_data_access_attr(requested_permissions),
1972 ffa_get_instruction_access_attr(sent_permissions),
1973 ffa_get_instruction_access_attr(
1974 requested_permissions),
1975 found_to_id ? permissions : NULL)) {
1976 return ffa_error(FFA_DENIED);
1977 }
1978
1979 /*
1980 * Can't request PM to clear memory if only provided with RO
1981 * permissions.
1982 */
1983 if (found_to_id &&
1984 (ffa_get_data_access_attr(*permissions) ==
1985 FFA_DATA_ACCESS_RO) &&
1986 (retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
1987 0U) {
1988 dlog_verbose(
1989 "Receiver has RO permissions can not request "
1990 "clear.\n");
1991 return ffa_error(FFA_DENIED);
1992 }
1993 }
1994
1995 if (retrieve_receiver_index == retrieve_request->receiver_count) {
1996 dlog_verbose(
1997 "Retrieve request does not contain caller's (%x) "
1998 "permissions\n",
1999 to_vm_id);
2000 return ffa_error(FFA_INVALID_PARAMETERS);
2001 }
2002
2003 return (struct ffa_value){.func = FFA_SUCCESS_32};
2004}
2005
J-Alvesa9cd7e32022-07-01 13:49:33 +01002006/*
2007 * According to section 16.4.3 of FF-A v1.1 EAC0 specification, the hypervisor
2008 * may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region description
2009 * of a pending memory sharing operation whose allocator is the SPM, for
2010 * validation purposes before forwarding an FFA_MEM_RECLAIM call. In doing so
2011 * the memory region descriptor of the retrieve request must be zeroed with the
2012 * exception of the sender ID and handle.
2013 */
2014bool is_ffa_memory_retrieve_borrower_request(struct ffa_memory_region *request,
2015 struct vm_locked to_locked)
2016{
2017 return to_locked.vm->id == HF_HYPERVISOR_VM_ID &&
2018 request->attributes == 0U && request->flags == 0U &&
2019 request->tag == 0U && request->receiver_count == 0U &&
2020 plat_ffa_memory_handle_allocated_by_current_world(
2021 request->handle);
2022}
2023
2024/*
2025 * Helper to reset count of fragments retrieved by the hypervisor.
2026 */
2027static void ffa_memory_retrieve_complete_from_hyp(
2028 struct ffa_memory_share_state *share_state)
2029{
2030 if (share_state->hypervisor_fragment_count ==
2031 share_state->fragment_count) {
2032 share_state->hypervisor_fragment_count = 0;
2033 }
2034}
2035
J-Alves089004f2022-07-13 14:25:44 +01002036/**
2037 * Validate that the memory region descriptor provided by the borrower on
2038 * FFA_MEM_RETRIEVE_REQ, against saved memory region provided by lender at the
2039 * memory sharing call.
2040 */
2041static struct ffa_value ffa_memory_retrieve_validate(
2042 ffa_vm_id_t receiver_id, struct ffa_memory_region *retrieve_request,
2043 struct ffa_memory_region *memory_region, uint32_t *receiver_index,
2044 uint32_t share_func)
2045{
2046 ffa_memory_region_flags_t transaction_type =
2047 retrieve_request->flags &
2048 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002049 enum ffa_memory_security security_state;
J-Alves089004f2022-07-13 14:25:44 +01002050
2051 assert(retrieve_request != NULL);
2052 assert(memory_region != NULL);
2053 assert(receiver_index != NULL);
2054 assert(retrieve_request->sender == memory_region->sender);
2055
2056 /*
2057 * Check that the transaction type expected by the receiver is
2058 * correct, if it has been specified.
2059 */
2060 if (transaction_type !=
2061 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
2062 transaction_type != (memory_region->flags &
2063 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
2064 dlog_verbose(
2065 "Incorrect transaction type %#x for "
2066 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
2067 transaction_type,
2068 memory_region->flags &
2069 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
2070 retrieve_request->handle);
2071 return ffa_error(FFA_INVALID_PARAMETERS);
2072 }
2073
2074 if (retrieve_request->tag != memory_region->tag) {
2075 dlog_verbose(
2076 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
2077 "%d for handle %#x.\n",
2078 retrieve_request->tag, memory_region->tag,
2079 retrieve_request->handle);
2080 return ffa_error(FFA_INVALID_PARAMETERS);
2081 }
2082
2083 *receiver_index =
2084 ffa_memory_region_get_receiver(memory_region, receiver_id);
2085
2086 if (*receiver_index == memory_region->receiver_count) {
2087 dlog_verbose(
2088 "Incorrect receiver VM ID %d for "
2089 "FFA_MEM_RETRIEVE_REQ, for handle %#x.\n",
J-Alves59ed0042022-07-28 18:26:41 +01002090 receiver_id, memory_region->handle);
J-Alves089004f2022-07-13 14:25:44 +01002091 return ffa_error(FFA_INVALID_PARAMETERS);
2092 }
2093
2094 if ((retrieve_request->flags &
2095 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
2096 dlog_verbose(
2097 "Retriever specified 'address range alignment 'hint' "
2098 "not supported.\n");
2099 return ffa_error(FFA_INVALID_PARAMETERS);
2100 }
2101 if ((retrieve_request->flags &
2102 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
2103 dlog_verbose(
2104 "Bits 8-5 must be zero in memory region's flags "
2105 "(address range alignment hint not supported).\n");
2106 return ffa_error(FFA_INVALID_PARAMETERS);
2107 }
2108
2109 if ((retrieve_request->flags & ~0x7FF) != 0U) {
2110 dlog_verbose(
2111 "Bits 31-10 must be zero in memory region's flags.\n");
2112 return ffa_error(FFA_INVALID_PARAMETERS);
2113 }
2114
2115 if (share_func == FFA_MEM_SHARE_32 &&
2116 (retrieve_request->flags &
2117 (FFA_MEMORY_REGION_FLAG_CLEAR |
2118 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
2119 dlog_verbose(
2120 "Memory Share operation can't clean after relinquish "
2121 "memory region.\n");
2122 return ffa_error(FFA_INVALID_PARAMETERS);
2123 }
2124
2125 /*
2126 * If the borrower needs the memory to be cleared before mapping
2127 * to its address space, the sender should have set the flag
2128 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
2129 * FFA_DENIED.
2130 */
2131 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
2132 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
2133 dlog_verbose(
2134 "Borrower needs memory cleared. Sender needs to set "
2135 "flag for clearing memory.\n");
2136 return ffa_error(FFA_DENIED);
2137 }
2138
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002139 /* Memory region attributes NS-Bit MBZ for FFA_MEM_RETRIEVE_REQ. */
2140 security_state =
2141 ffa_get_memory_security_attr(retrieve_request->attributes);
2142 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
2143 dlog_verbose(
2144 "Invalid security state for memory retrieve request "
2145 "operation.\n");
2146 return ffa_error(FFA_INVALID_PARAMETERS);
2147 }
2148
J-Alves089004f2022-07-13 14:25:44 +01002149 /*
2150 * If memory type is not specified, bypass validation of memory
2151 * attributes in the retrieve request. The retriever is expecting to
2152 * obtain this information from the SPMC.
2153 */
2154 if (ffa_get_memory_type_attr(retrieve_request->attributes) ==
2155 FFA_MEMORY_NOT_SPECIFIED_MEM) {
2156 return (struct ffa_value){.func = FFA_SUCCESS_32};
2157 }
2158
2159 /*
2160 * Ensure receiver's attributes are compatible with how
2161 * Hafnium maps memory: Normal Memory, Inner shareable,
2162 * Write-Back Read-Allocate Write-Allocate Cacheable.
2163 */
2164 return ffa_memory_attributes_validate(retrieve_request->attributes);
2165}
2166
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002167struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2168 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002169 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002170 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002171{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002172 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002173 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002174 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002175 sizeof(struct ffa_memory_access);
2176 ffa_memory_handle_t handle = retrieve_request->handle;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002177 struct ffa_memory_region *memory_region;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002178 ffa_memory_access_permissions_t permissions = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002179 uint32_t memory_to_attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002180 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002181 struct ffa_memory_share_state *share_state;
2182 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002183 struct ffa_composite_memory_region *composite;
2184 uint32_t total_length;
2185 uint32_t fragment_length;
J-Alves089004f2022-07-13 14:25:44 +01002186 ffa_vm_id_t receiver_id = to_locked.vm->id;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002187 bool is_send_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002188
2189 dump_share_states();
2190
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002191 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002192 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002193 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002194 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002195 expected_retrieve_request_length,
2196 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002197 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002198 }
2199
2200 share_states = share_states_lock();
2201 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002202 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002203 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002204 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002205 goto out;
2206 }
2207
J-Alves96de29f2022-04-26 16:05:24 +01002208 if (!share_state->sending_complete) {
2209 dlog_verbose(
2210 "Memory with handle %#x not fully sent, can't "
2211 "retrieve.\n",
2212 handle);
2213 ret = ffa_error(FFA_INVALID_PARAMETERS);
2214 goto out;
2215 }
2216
Andrew Walbrana65a1322020-04-06 19:32:32 +01002217 memory_region = share_state->memory_region;
J-Alves089004f2022-07-13 14:25:44 +01002218
Andrew Walbrana65a1322020-04-06 19:32:32 +01002219 CHECK(memory_region != NULL);
2220
J-Alves089004f2022-07-13 14:25:44 +01002221 if (retrieve_request->sender != memory_region->sender) {
2222 dlog_verbose(
2223 "Memory with handle %#x not fully sent, can't "
2224 "retrieve.\n",
2225 handle);
2226 ret = ffa_error(FFA_INVALID_PARAMETERS);
2227 goto out;
2228 }
J-Alves96de29f2022-04-26 16:05:24 +01002229
J-Alvesa9cd7e32022-07-01 13:49:33 +01002230 if (!is_ffa_memory_retrieve_borrower_request(retrieve_request,
2231 to_locked)) {
2232 uint32_t receiver_index;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002233
J-Alvesb5084cf2022-07-06 14:20:12 +01002234 /*
2235 * The SPMC can only process retrieve requests to memory share
2236 * operations with one borrower from the other world. It can't
2237 * determine the ID of the NWd VM that invoked the retrieve
2238 * request interface call. It relies on the hypervisor to
2239 * validate the caller's ID against that provided in the
2240 * `receivers` list of the retrieve response.
2241 * In case there is only one borrower from the NWd in the
2242 * transaction descriptor, record that in the `receiver_id` for
2243 * later use, and validate in the retrieve request message.
2244 */
2245 if (to_locked.vm->id == HF_HYPERVISOR_VM_ID) {
2246 uint32_t other_world_count = 0;
2247
2248 for (uint32_t i = 0; i < memory_region->receiver_count;
2249 i++) {
2250 receiver_id =
2251 retrieve_request->receivers[0]
2252 .receiver_permissions.receiver;
2253 if (!vm_id_is_current_world(receiver_id)) {
2254 other_world_count++;
2255 }
2256 }
2257 if (other_world_count > 1) {
2258 dlog_verbose(
2259 "Support one receiver from the other "
2260 "world.\n");
2261 return ffa_error(FFA_NOT_SUPPORTED);
2262 }
2263 }
2264
2265 /*
2266 * Validate retrieve request, according to what was sent by the
2267 * sender. Function will output the `receiver_index` from the
2268 * provided memory region, and will output `permissions` from
2269 * the validated requested permissions.
2270 */
J-Alves089004f2022-07-13 14:25:44 +01002271 ret = ffa_memory_retrieve_validate(
2272 receiver_id, retrieve_request, memory_region,
2273 &receiver_index, share_state->share_func);
2274 if (ret.func != FFA_SUCCESS_32) {
J-Alvesa9cd7e32022-07-01 13:49:33 +01002275 goto out;
2276 }
2277
2278 if (share_state->retrieved_fragment_count[receiver_index] !=
2279 0U) {
2280 dlog_verbose(
2281 "Memory with handle %#x already retrieved.\n",
2282 handle);
2283 ret = ffa_error(FFA_DENIED);
2284 goto out;
2285 }
2286
J-Alvesa9cd7e32022-07-01 13:49:33 +01002287 ret = ffa_memory_retrieve_validate_memory_access_list(
2288 memory_region, retrieve_request, receiver_id,
2289 &permissions);
J-Alves614d9f42022-06-28 14:03:10 +01002290 if (ret.func != FFA_SUCCESS_32) {
2291 goto out;
2292 }
Federico Recanatia98603a2021-12-20 18:04:03 +01002293
J-Alvesa9cd7e32022-07-01 13:49:33 +01002294 memory_to_attributes = ffa_memory_permissions_to_mode(
2295 permissions, share_state->sender_orig_mode);
J-Alves40e260e2022-09-22 17:52:43 +01002296
2297 if (to_locked.vm->el0_partition) {
2298 /*
2299 * Get extra mapping attributes for the given VM ID.
2300 * If the memory is shared by a VM executing in non
2301 * secure world, attribute MM_MODE_NS has to be set
2302 * while mapping that in a SP executing in secure world.
2303 */
2304 memory_to_attributes |=
2305 arch_mm_extra_attributes_from_vm(
2306 retrieve_request->sender);
2307 }
2308
J-Alvesa9cd7e32022-07-01 13:49:33 +01002309 ret = ffa_retrieve_check_update(
2310 to_locked, memory_region->sender,
2311 share_state->fragments,
2312 share_state->fragment_constituent_counts,
2313 share_state->fragment_count, memory_to_attributes,
2314 share_state->share_func, false, page_pool);
2315
2316 if (ret.func != FFA_SUCCESS_32) {
2317 goto out;
2318 }
2319
2320 share_state->retrieved_fragment_count[receiver_index] = 1;
2321 is_send_complete =
2322 share_state->retrieved_fragment_count[receiver_index] ==
2323 share_state->fragment_count;
2324 } else {
2325 if (share_state->hypervisor_fragment_count != 0U) {
2326 dlog_verbose(
J-Alvesb5084cf2022-07-06 14:20:12 +01002327 "Memory with handle %#x already retrieved by "
J-Alvesa9cd7e32022-07-01 13:49:33 +01002328 "the hypervisor.\n",
2329 handle);
2330 ret = ffa_error(FFA_DENIED);
2331 goto out;
2332 }
2333
2334 share_state->hypervisor_fragment_count = 1;
2335
2336 ffa_memory_retrieve_complete_from_hyp(share_state);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002337 }
2338
J-Alvesb5084cf2022-07-06 14:20:12 +01002339 /* VMs acquire the RX buffer from SPMC. */
2340 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
2341
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002342 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002343 * Copy response to RX buffer of caller and deliver the message.
2344 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002345 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002346 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002347 composite = ffa_memory_region_get_composite(memory_region, 0);
2348 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002349 * Constituents which we received in the first fragment should
2350 * always fit in the first fragment we are sending, because the
2351 * header is the same size in both cases and we have a fixed
2352 * message buffer size. So `ffa_retrieved_memory_region_init`
2353 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01002354 */
2355 CHECK(ffa_retrieved_memory_region_init(
J-Alves2d8457f2022-10-05 11:06:41 +01002356 to_locked.vm->mailbox.recv, to_locked.vm->ffa_version,
2357 HF_MAILBOX_SIZE, memory_region->sender,
2358 memory_region->attributes, memory_region->flags, handle,
2359 receiver_id, permissions, composite->page_count,
2360 composite->constituent_count, share_state->fragments[0],
Andrew Walbranca808b12020-05-15 17:22:28 +01002361 share_state->fragment_constituent_counts[0], &total_length,
2362 &fragment_length));
J-Alvesb5084cf2022-07-06 14:20:12 +01002363
Andrew Walbranca808b12020-05-15 17:22:28 +01002364 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002365 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002366 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00002367 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002368
J-Alvesa9cd7e32022-07-01 13:49:33 +01002369 if (is_send_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002370 ffa_memory_retrieve_complete(share_states, share_state,
2371 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002372 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002373 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002374 .arg1 = total_length,
2375 .arg2 = fragment_length};
Andrew Walbranca808b12020-05-15 17:22:28 +01002376out:
2377 share_states_unlock(&share_states);
2378 dump_share_states();
2379 return ret;
2380}
2381
2382struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2383 ffa_memory_handle_t handle,
2384 uint32_t fragment_offset,
J-Alves59ed0042022-07-28 18:26:41 +01002385 ffa_vm_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01002386 struct mpool *page_pool)
2387{
2388 struct ffa_memory_region *memory_region;
2389 struct share_states_locked share_states;
2390 struct ffa_memory_share_state *share_state;
2391 struct ffa_value ret;
2392 uint32_t fragment_index;
2393 uint32_t retrieved_constituents_count;
2394 uint32_t i;
2395 uint32_t expected_fragment_offset;
2396 uint32_t remaining_constituent_count;
2397 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01002398 uint32_t receiver_index;
J-Alves59ed0042022-07-28 18:26:41 +01002399 bool continue_ffa_hyp_mem_retrieve_req;
Andrew Walbranca808b12020-05-15 17:22:28 +01002400
2401 dump_share_states();
2402
2403 share_states = share_states_lock();
2404 if (!get_share_state(share_states, handle, &share_state)) {
2405 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2406 handle);
2407 ret = ffa_error(FFA_INVALID_PARAMETERS);
2408 goto out;
2409 }
2410
2411 memory_region = share_state->memory_region;
2412 CHECK(memory_region != NULL);
2413
Andrew Walbranca808b12020-05-15 17:22:28 +01002414 if (!share_state->sending_complete) {
2415 dlog_verbose(
2416 "Memory with handle %#x not fully sent, can't "
2417 "retrieve.\n",
2418 handle);
2419 ret = ffa_error(FFA_INVALID_PARAMETERS);
2420 goto out;
2421 }
2422
J-Alves59ed0042022-07-28 18:26:41 +01002423 /*
2424 * If retrieve request from the hypervisor has been initiated in the
2425 * given share_state, continue it, else assume it is a continuation of
2426 * retrieve request from a NWd VM.
2427 */
2428 continue_ffa_hyp_mem_retrieve_req =
2429 (to_locked.vm->id == HF_HYPERVISOR_VM_ID) &&
2430 (share_state->hypervisor_fragment_count != 0U) &&
2431 plat_ffa_is_vm_id(sender_vm_id);
Andrew Walbranca808b12020-05-15 17:22:28 +01002432
J-Alves59ed0042022-07-28 18:26:41 +01002433 if (!continue_ffa_hyp_mem_retrieve_req) {
2434 receiver_index = ffa_memory_region_get_receiver(
2435 memory_region, to_locked.vm->id);
2436
2437 if (receiver_index == memory_region->receiver_count) {
2438 dlog_verbose(
2439 "Caller of FFA_MEM_FRAG_RX (%x) is not a "
2440 "borrower to memory sharing transaction (%x)\n",
2441 to_locked.vm->id, handle);
2442 ret = ffa_error(FFA_INVALID_PARAMETERS);
2443 goto out;
2444 }
2445
2446 if (share_state->retrieved_fragment_count[receiver_index] ==
2447 0 ||
2448 share_state->retrieved_fragment_count[receiver_index] >=
2449 share_state->fragment_count) {
2450 dlog_verbose(
2451 "Retrieval of memory with handle %#x not yet "
2452 "started or already completed (%d/%d fragments "
2453 "retrieved).\n",
2454 handle,
2455 share_state->retrieved_fragment_count
2456 [receiver_index],
2457 share_state->fragment_count);
2458 ret = ffa_error(FFA_INVALID_PARAMETERS);
2459 goto out;
2460 }
2461
2462 fragment_index =
2463 share_state->retrieved_fragment_count[receiver_index];
2464 } else {
2465 if (share_state->hypervisor_fragment_count == 0 ||
2466 share_state->hypervisor_fragment_count >=
2467 share_state->fragment_count) {
2468 dlog_verbose(
2469 "Retrieve of memory with handle %x not "
2470 "started from hypervisor.\n",
2471 handle);
2472 ret = ffa_error(FFA_INVALID_PARAMETERS);
2473 goto out;
2474 }
2475
2476 if (memory_region->sender != sender_vm_id) {
2477 dlog_verbose(
2478 "Sender ID (%x) is not as expected for memory "
2479 "handle %x\n",
2480 sender_vm_id, handle);
2481 ret = ffa_error(FFA_INVALID_PARAMETERS);
2482 goto out;
2483 }
2484
2485 fragment_index = share_state->hypervisor_fragment_count;
2486
2487 receiver_index = 0;
2488 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002489
2490 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002491 * Check that the given fragment offset is correct by counting
2492 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01002493 */
2494 retrieved_constituents_count = 0;
2495 for (i = 0; i < fragment_index; ++i) {
2496 retrieved_constituents_count +=
2497 share_state->fragment_constituent_counts[i];
2498 }
J-Alvesc7484f12022-05-13 12:41:14 +01002499
2500 CHECK(memory_region->receiver_count > 0);
2501
Andrew Walbranca808b12020-05-15 17:22:28 +01002502 expected_fragment_offset =
J-Alvesc7484f12022-05-13 12:41:14 +01002503 ffa_composite_constituent_offset(memory_region,
2504 receiver_index) +
Andrew Walbranca808b12020-05-15 17:22:28 +01002505 retrieved_constituents_count *
J-Alvesc7484f12022-05-13 12:41:14 +01002506 sizeof(struct ffa_memory_region_constituent) -
2507 sizeof(struct ffa_memory_access) *
2508 (memory_region->receiver_count - 1);
Andrew Walbranca808b12020-05-15 17:22:28 +01002509 if (fragment_offset != expected_fragment_offset) {
2510 dlog_verbose("Fragment offset was %d but expected %d.\n",
2511 fragment_offset, expected_fragment_offset);
2512 ret = ffa_error(FFA_INVALID_PARAMETERS);
2513 goto out;
2514 }
2515
J-Alves59ed0042022-07-28 18:26:41 +01002516 /* VMs acquire the RX buffer from SPMC. */
2517 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
2518
Andrew Walbranca808b12020-05-15 17:22:28 +01002519 remaining_constituent_count = ffa_memory_fragment_init(
2520 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2521 share_state->fragments[fragment_index],
2522 share_state->fragment_constituent_counts[fragment_index],
2523 &fragment_length);
2524 CHECK(remaining_constituent_count == 0);
2525 to_locked.vm->mailbox.recv_size = fragment_length;
2526 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2527 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00002528 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbranca808b12020-05-15 17:22:28 +01002529
J-Alves59ed0042022-07-28 18:26:41 +01002530 if (!continue_ffa_hyp_mem_retrieve_req) {
2531 share_state->retrieved_fragment_count[receiver_index]++;
2532 if (share_state->retrieved_fragment_count[receiver_index] ==
2533 share_state->fragment_count) {
2534 ffa_memory_retrieve_complete(share_states, share_state,
2535 page_pool);
2536 }
2537 } else {
2538 share_state->hypervisor_fragment_count++;
2539
2540 ffa_memory_retrieve_complete_from_hyp(share_state);
2541 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002542 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2543 .arg1 = (uint32_t)handle,
2544 .arg2 = (uint32_t)(handle >> 32),
2545 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002546
2547out:
2548 share_states_unlock(&share_states);
2549 dump_share_states();
2550 return ret;
2551}
2552
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002553struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002554 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002555 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002556{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002557 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002558 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002559 struct ffa_memory_share_state *share_state;
2560 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002561 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002562 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01002563 uint32_t receiver_index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002564
Andrew Walbrana65a1322020-04-06 19:32:32 +01002565 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002566 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002567 "Stream endpoints not supported (got %d "
2568 "endpoints on "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002569 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002570 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002571 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002572 }
2573
Andrew Walbrana65a1322020-04-06 19:32:32 +01002574 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002575 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002576 "VM ID %d in relinquish message doesn't match "
2577 "calling "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002578 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002579 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002580 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002581 }
2582
2583 dump_share_states();
2584
2585 share_states = share_states_lock();
2586 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002587 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002588 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002589 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002590 goto out;
2591 }
2592
Andrew Walbranca808b12020-05-15 17:22:28 +01002593 if (!share_state->sending_complete) {
2594 dlog_verbose(
2595 "Memory with handle %#x not fully sent, can't "
2596 "relinquish.\n",
2597 handle);
2598 ret = ffa_error(FFA_INVALID_PARAMETERS);
2599 goto out;
2600 }
2601
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002602 memory_region = share_state->memory_region;
2603 CHECK(memory_region != NULL);
2604
J-Alves8eb19162022-04-28 10:56:48 +01002605 receiver_index = ffa_memory_region_get_receiver(memory_region,
2606 from_locked.vm->id);
2607
2608 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002609 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002610 "VM ID %d tried to relinquish memory region "
2611 "with "
J-Alves8eb19162022-04-28 10:56:48 +01002612 "handle %#x and it is not a valid borrower.\n",
2613 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002614 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002615 goto out;
2616 }
2617
J-Alves8eb19162022-04-28 10:56:48 +01002618 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01002619 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002620 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002621 "Memory with handle %#x not yet fully "
2622 "retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01002623 "receiver %x can't relinquish.\n",
2624 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002625 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002626 goto out;
2627 }
2628
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002629 clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002630
2631 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002632 * Clear is not allowed for memory that was shared, as the
2633 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002634 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002635 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002636 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002637 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002638 goto out;
2639 }
2640
Andrew Walbranca808b12020-05-15 17:22:28 +01002641 ret = ffa_relinquish_check_update(
2642 from_locked, share_state->fragments,
2643 share_state->fragment_constituent_counts,
2644 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002645
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002646 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002647 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002648 * Mark memory handle as not retrieved, so it can be
2649 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002650 */
J-Alves8eb19162022-04-28 10:56:48 +01002651 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002652 }
2653
2654out:
2655 share_states_unlock(&share_states);
2656 dump_share_states();
2657 return ret;
2658}
2659
2660/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01002661 * Validates that the reclaim transition is allowed for the given
2662 * handle, updates the page table of the reclaiming VM, and frees the
2663 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002664 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002665struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002666 ffa_memory_handle_t handle,
2667 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002668 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002669{
2670 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002671 struct ffa_memory_share_state *share_state;
2672 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002673 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002674
2675 dump_share_states();
2676
2677 share_states = share_states_lock();
J-Alvesb5084cf2022-07-06 14:20:12 +01002678 if (get_share_state(share_states, handle, &share_state)) {
2679 memory_region = share_state->memory_region;
2680 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002681 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002682 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002683 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002684 goto out;
2685 }
2686
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002687 CHECK(memory_region != NULL);
2688
J-Alvesa9cd7e32022-07-01 13:49:33 +01002689 if (vm_id_is_current_world(to_locked.vm->id) &&
2690 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002691 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002692 "VM %#x attempted to reclaim memory handle %#x "
2693 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002694 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002695 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002696 goto out;
2697 }
2698
Andrew Walbranca808b12020-05-15 17:22:28 +01002699 if (!share_state->sending_complete) {
2700 dlog_verbose(
2701 "Memory with handle %#x not fully sent, can't "
2702 "reclaim.\n",
2703 handle);
2704 ret = ffa_error(FFA_INVALID_PARAMETERS);
2705 goto out;
2706 }
2707
J-Alves752236c2022-04-28 11:07:47 +01002708 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
2709 if (share_state->retrieved_fragment_count[i] != 0) {
2710 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002711 "Tried to reclaim memory handle %#x "
2712 "that has "
2713 "not been relinquished by all "
2714 "borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01002715 handle,
2716 memory_region->receivers[i]
2717 .receiver_permissions.receiver);
2718 ret = ffa_error(FFA_DENIED);
2719 goto out;
2720 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002721 }
2722
Andrew Walbranca808b12020-05-15 17:22:28 +01002723 ret = ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00002724 to_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002725 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00002726 share_state->fragment_count, share_state->sender_orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01002727 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002728
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002729 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002730 share_state_free(share_states, share_state, page_pool);
J-Alvesa9cd7e32022-07-01 13:49:33 +01002731 dlog_verbose(
2732 "Freed share state after successful "
2733 "reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002734 }
2735
2736out:
2737 share_states_unlock(&share_states);
2738 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002739}