blob: c3b6f1032c66270ca13138a14c9fa567736bbce0 [file] [log] [blame]
Jose Marinho75509b42019-04-09 09:34:59 +01001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Jose Marinho75509b42019-04-09 09:34:59 +01007 */
8
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01009#include "hf/ffa_memory.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000010
Federico Recanati4fd065d2021-12-13 20:06:23 +010011#include "hf/arch/mm.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020012#include "hf/arch/other_world.h"
Olivier Deprez55a189e2021-06-09 15:45:27 +020013#include "hf/arch/plat/ffa.h"
Andrew Walbran290b0c92020-02-03 16:37:14 +000014
J-Alves5952d942022-12-22 16:03:00 +000015#include "hf/addr.h"
Jose Marinho75509b42019-04-09 09:34:59 +010016#include "hf/api.h"
Daniel Boulbya2f8c662021-11-26 17:52:53 +000017#include "hf/assert.h"
Jose Marinho09b1db82019-08-08 09:16:59 +010018#include "hf/check.h"
Jose Marinho75509b42019-04-09 09:34:59 +010019#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010020#include "hf/ffa_internal.h"
J-Alves66652252022-07-06 09:49:51 +010021#include "hf/ffa_memory_internal.h"
J-Alves5952d942022-12-22 16:03:00 +000022#include "hf/mm.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000023#include "hf/mpool.h"
Jose Marinho75509b42019-04-09 09:34:59 +010024#include "hf/std.h"
Andrew Scull3c257452019-11-26 13:32:50 +000025#include "hf/vm.h"
Jose Marinho75509b42019-04-09 09:34:59 +010026
J-Alves2d8457f2022-10-05 11:06:41 +010027#include "vmapi/hf/ffa_v1_0.h"
28
J-Alves5da37d92022-10-24 16:33:48 +010029#define RECEIVERS_COUNT_IN_RETRIEVE_RESP 1
30
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000031/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010032 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000033 * by this lock.
34 */
35static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010036static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000037
38/**
J-Alves917d2f22020-10-30 18:39:30 +000039 * Extracts the index from a memory handle allocated by Hafnium's current world.
40 */
41uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle)
42{
43 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
44}
45
46/**
Andrew Walbranca808b12020-05-15 17:22:28 +010047 * Initialises the next available `struct ffa_memory_share_state` and sets
48 * `share_state_ret` to a pointer to it. If `handle` is
49 * `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle, otherwise
50 * uses the provided handle which is assumed to be globally unique.
51 *
52 * Returns true on success or false if none are available.
53 */
J-Alves66652252022-07-06 09:49:51 +010054bool allocate_share_state(struct share_states_locked share_states,
55 uint32_t share_func,
56 struct ffa_memory_region *memory_region,
57 uint32_t fragment_length, ffa_memory_handle_t handle,
58 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000059{
Andrew Walbrana65a1322020-04-06 19:32:32 +010060 uint64_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000061
Daniel Boulbya2f8c662021-11-26 17:52:53 +000062 assert(share_states.share_states != NULL);
63 assert(memory_region != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000064
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000065 for (i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +010066 if (share_states.share_states[i].share_func == 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000067 uint32_t j;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010068 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +010069 &share_states.share_states[i];
70 struct ffa_composite_memory_region *composite =
71 ffa_memory_region_get_composite(memory_region,
72 0);
73
74 if (handle == FFA_MEMORY_HANDLE_INVALID) {
J-Alvesee68c542020-10-29 17:48:20 +000075 memory_region->handle =
Olivier Deprez55a189e2021-06-09 15:45:27 +020076 plat_ffa_memory_handle_make(i);
Andrew Walbranca808b12020-05-15 17:22:28 +010077 } else {
J-Alvesee68c542020-10-29 17:48:20 +000078 memory_region->handle = handle;
Andrew Walbranca808b12020-05-15 17:22:28 +010079 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000080 allocated_state->share_func = share_func;
81 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +010082 allocated_state->fragment_count = 1;
83 allocated_state->fragments[0] = composite->constituents;
84 allocated_state->fragment_constituent_counts[0] =
85 (fragment_length -
86 ffa_composite_constituent_offset(memory_region,
87 0)) /
88 sizeof(struct ffa_memory_region_constituent);
89 allocated_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000090 for (j = 0; j < MAX_MEM_SHARE_RECIPIENTS; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +010091 allocated_state->retrieved_fragment_count[j] =
92 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000093 }
Andrew Walbranca808b12020-05-15 17:22:28 +010094 if (share_state_ret != NULL) {
95 *share_state_ret = allocated_state;
96 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000097 return true;
98 }
99 }
100
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000101 return false;
102}
103
104/** Locks the share states lock. */
105struct share_states_locked share_states_lock(void)
106{
107 sl_lock(&share_states_lock_instance);
108
109 return (struct share_states_locked){.share_states = share_states};
110}
111
112/** Unlocks the share states lock. */
J-Alves66652252022-07-06 09:49:51 +0100113void share_states_unlock(struct share_states_locked *share_states)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000114{
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000115 assert(share_states->share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000116 share_states->share_states = NULL;
117 sl_unlock(&share_states_lock_instance);
118}
119
120/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100121 * If the given handle is a valid handle for an allocated share state then
122 * initialises `share_state_ret` to point to the share state and returns true.
123 * Otherwise returns false.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000124 */
J-Alvesfdd29272022-07-19 13:16:31 +0100125bool get_share_state(struct share_states_locked share_states,
126 ffa_memory_handle_t handle,
127 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000128{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100129 struct ffa_memory_share_state *share_state;
J-Alves917d2f22020-10-30 18:39:30 +0000130 uint64_t index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000131
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000132 assert(share_states.share_states != NULL);
133 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100134
135 /*
136 * First look for a share_state allocated by us, in which case the
137 * handle is based on the index.
138 */
Olivier Deprez55a189e2021-06-09 15:45:27 +0200139 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
J-Alves917d2f22020-10-30 18:39:30 +0000140 index = ffa_memory_handle_get_index(handle);
Andrew Walbranca808b12020-05-15 17:22:28 +0100141 if (index < MAX_MEM_SHARES) {
142 share_state = &share_states.share_states[index];
143 if (share_state->share_func != 0) {
144 *share_state_ret = share_state;
145 return true;
146 }
147 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000148 }
149
Andrew Walbranca808b12020-05-15 17:22:28 +0100150 /* Fall back to a linear scan. */
151 for (index = 0; index < MAX_MEM_SHARES; ++index) {
152 share_state = &share_states.share_states[index];
J-Alvesee68c542020-10-29 17:48:20 +0000153 if (share_state->memory_region != NULL &&
154 share_state->memory_region->handle == handle &&
Andrew Walbranca808b12020-05-15 17:22:28 +0100155 share_state->share_func != 0) {
156 *share_state_ret = share_state;
157 return true;
158 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000159 }
160
Andrew Walbranca808b12020-05-15 17:22:28 +0100161 return false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000162}
163
164/** Marks a share state as unallocated. */
J-Alvesfdd29272022-07-19 13:16:31 +0100165void share_state_free(struct share_states_locked share_states,
166 struct ffa_memory_share_state *share_state,
167 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000168{
Andrew Walbranca808b12020-05-15 17:22:28 +0100169 uint32_t i;
170
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000171 assert(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000172 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100173 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000174 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100175 /*
176 * First fragment is part of the same page as the `memory_region`, so it
177 * doesn't need to be freed separately.
178 */
179 share_state->fragments[0] = NULL;
180 share_state->fragment_constituent_counts[0] = 0;
181 for (i = 1; i < share_state->fragment_count; ++i) {
182 mpool_free(page_pool, share_state->fragments[i]);
183 share_state->fragments[i] = NULL;
184 share_state->fragment_constituent_counts[i] = 0;
185 }
186 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000187 share_state->memory_region = NULL;
J-Alvesa9cd7e32022-07-01 13:49:33 +0100188 share_state->hypervisor_fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000189}
190
Andrew Walbranca808b12020-05-15 17:22:28 +0100191/** Checks whether the given share state has been fully sent. */
J-Alvesfdd29272022-07-19 13:16:31 +0100192bool share_state_sending_complete(struct share_states_locked share_states,
193 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000194{
Andrew Walbranca808b12020-05-15 17:22:28 +0100195 struct ffa_composite_memory_region *composite;
196 uint32_t expected_constituent_count;
197 uint32_t fragment_constituent_count_total = 0;
198 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000199
Andrew Walbranca808b12020-05-15 17:22:28 +0100200 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000201 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100202
203 /*
204 * Share state must already be valid, or it's not possible to get hold
205 * of it.
206 */
207 CHECK(share_state->memory_region != NULL &&
208 share_state->share_func != 0);
209
210 composite =
211 ffa_memory_region_get_composite(share_state->memory_region, 0);
212 expected_constituent_count = composite->constituent_count;
213 for (i = 0; i < share_state->fragment_count; ++i) {
214 fragment_constituent_count_total +=
215 share_state->fragment_constituent_counts[i];
216 }
217 dlog_verbose(
218 "Checking completion: constituent count %d/%d from %d "
219 "fragments.\n",
220 fragment_constituent_count_total, expected_constituent_count,
221 share_state->fragment_count);
222
223 return fragment_constituent_count_total == expected_constituent_count;
224}
225
226/**
227 * Calculates the offset of the next fragment expected for the given share
228 * state.
229 */
J-Alvesfdd29272022-07-19 13:16:31 +0100230uint32_t share_state_next_fragment_offset(
Andrew Walbranca808b12020-05-15 17:22:28 +0100231 struct share_states_locked share_states,
232 struct ffa_memory_share_state *share_state)
233{
234 uint32_t next_fragment_offset;
235 uint32_t i;
236
237 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000238 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100239
240 next_fragment_offset =
241 ffa_composite_constituent_offset(share_state->memory_region, 0);
242 for (i = 0; i < share_state->fragment_count; ++i) {
243 next_fragment_offset +=
244 share_state->fragment_constituent_counts[i] *
245 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000246 }
247
Andrew Walbranca808b12020-05-15 17:22:28 +0100248 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000249}
250
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100251static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000252{
253 uint32_t i;
254
255 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
256 return;
257 }
258
Olivier Deprez935e1b12020-12-22 18:01:29 +0100259 dlog("from VM %#x, attributes %#x, flags %#x, tag %u, to "
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100260 "%u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100261 "recipients [",
262 memory_region->sender, memory_region->attributes,
Olivier Deprez935e1b12020-12-22 18:01:29 +0100263 memory_region->flags, memory_region->tag,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100264 memory_region->receiver_count);
265 for (i = 0; i < memory_region->receiver_count; ++i) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000266 if (i != 0) {
267 dlog(", ");
268 }
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100269 dlog("VM %#x: %#x (offset %u)",
Andrew Walbrana65a1322020-04-06 19:32:32 +0100270 memory_region->receivers[i].receiver_permissions.receiver,
271 memory_region->receivers[i]
272 .receiver_permissions.permissions,
273 memory_region->receivers[i]
274 .composite_memory_region_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000275 }
276 dlog("]");
277}
278
J-Alves66652252022-07-06 09:49:51 +0100279void dump_share_states(void)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000280{
281 uint32_t i;
282
283 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
284 return;
285 }
286
287 dlog("Current share states:\n");
288 sl_lock(&share_states_lock_instance);
289 for (i = 0; i < MAX_MEM_SHARES; ++i) {
290 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000291 switch (share_states[i].share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100292 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000293 dlog("SHARE");
294 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100295 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000296 dlog("LEND");
297 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100298 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000299 dlog("DONATE");
300 break;
301 default:
302 dlog("invalid share_func %#x",
303 share_states[i].share_func);
304 }
Olivier Deprez935e1b12020-12-22 18:01:29 +0100305 dlog(" %#x (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000306 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100307 if (share_states[i].sending_complete) {
308 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000309 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100310 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000311 }
J-Alves2a0d2882020-10-29 14:49:50 +0000312 dlog(" with %d fragments, %d retrieved, "
313 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100314 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000315 share_states[i].retrieved_fragment_count[0],
316 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000317 }
318 }
319 sl_unlock(&share_states_lock_instance);
320}
321
Andrew Walbran475c1452020-02-07 13:22:22 +0000322/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100323static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100324 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000325{
326 uint32_t mode = 0;
327
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100328 switch (ffa_get_data_access_attr(permissions)) {
329 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000330 mode = MM_MODE_R;
331 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100332 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000333 mode = MM_MODE_R | MM_MODE_W;
334 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100335 case FFA_DATA_ACCESS_NOT_SPECIFIED:
336 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
337 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100338 case FFA_DATA_ACCESS_RESERVED:
339 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100340 }
341
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100342 switch (ffa_get_instruction_access_attr(permissions)) {
343 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000344 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100345 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100346 mode |= MM_MODE_X;
347 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100348 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
349 mode |= (default_mode & MM_MODE_X);
350 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100351 case FFA_INSTRUCTION_ACCESS_RESERVED:
352 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000353 }
354
Olivier Deprez878bd5b2021-04-15 19:05:10 +0200355 /* Set the security state bit if necessary. */
356 if ((default_mode & plat_ffa_other_world_mode()) != 0) {
357 mode |= plat_ffa_other_world_mode();
358 }
359
Andrew Walbran475c1452020-02-07 13:22:22 +0000360 return mode;
361}
362
Jose Marinho75509b42019-04-09 09:34:59 +0100363/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000364 * Get the current mode in the stage-2 page table of the given vm of all the
365 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100366 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100367 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100368static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000369 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100370 struct ffa_memory_region_constituent **fragments,
371 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100372{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100373 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100374 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100375
Andrew Walbranca808b12020-05-15 17:22:28 +0100376 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100377 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000378 * Fail if there are no constituents. Otherwise we would get an
379 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100380 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100381 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100382 }
383
Andrew Walbranca808b12020-05-15 17:22:28 +0100384 for (i = 0; i < fragment_count; ++i) {
385 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
386 ipaddr_t begin = ipa_init(fragments[i][j].address);
387 size_t size = fragments[i][j].page_count * PAGE_SIZE;
388 ipaddr_t end = ipa_add(begin, size);
389 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100390
Andrew Walbranca808b12020-05-15 17:22:28 +0100391 /* Fail if addresses are not page-aligned. */
392 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
393 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
394 return ffa_error(FFA_INVALID_PARAMETERS);
395 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100396
Andrew Walbranca808b12020-05-15 17:22:28 +0100397 /*
398 * Ensure that this constituent memory range is all
399 * mapped with the same mode.
400 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800401 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100402 return ffa_error(FFA_DENIED);
403 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100404
Andrew Walbranca808b12020-05-15 17:22:28 +0100405 /*
406 * Ensure that all constituents are mapped with the same
407 * mode.
408 */
409 if (i == 0) {
410 *orig_mode = current_mode;
411 } else if (current_mode != *orig_mode) {
412 dlog_verbose(
413 "Expected mode %#x but was %#x for %d "
414 "pages at %#x.\n",
415 *orig_mode, current_mode,
416 fragments[i][j].page_count,
417 ipa_addr(begin));
418 return ffa_error(FFA_DENIED);
419 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100420 }
Jose Marinho75509b42019-04-09 09:34:59 +0100421 }
422
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100423 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000424}
425
426/**
427 * Verify that all pages have the same mode, that the starting mode
428 * constitutes a valid state and obtain the next mode to apply
429 * to the sending VM.
430 *
431 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100432 * 1) FFA_DENIED if a state transition was not found;
433 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100434 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100435 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100436 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100437 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
438 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000439 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100440static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100441 struct vm_locked from, uint32_t share_func,
J-Alves363f5722022-04-25 17:37:37 +0100442 struct ffa_memory_access *receivers, uint32_t receivers_count,
443 uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100444 struct ffa_memory_region_constituent **fragments,
445 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
446 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000447{
448 const uint32_t state_mask =
449 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100450 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000451
Andrew Walbranca808b12020-05-15 17:22:28 +0100452 ret = constituents_get_mode(from, orig_from_mode, fragments,
453 fragment_constituent_counts,
454 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100455 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100456 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100457 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100458 }
459
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000460 /* Ensure the address range is normal memory and not a device. */
461 if (*orig_from_mode & MM_MODE_D) {
462 dlog_verbose("Can't share device memory (mode is %#x).\n",
463 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100464 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000465 }
466
467 /*
468 * Ensure the sender is the owner and has exclusive access to the
469 * memory.
470 */
471 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100472 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100473 }
474
J-Alves363f5722022-04-25 17:37:37 +0100475 assert(receivers != NULL && receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100476
J-Alves363f5722022-04-25 17:37:37 +0100477 for (uint32_t i = 0U; i < receivers_count; i++) {
478 ffa_memory_access_permissions_t permissions =
479 receivers[i].receiver_permissions.permissions;
480 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
481 permissions, *orig_from_mode);
482
483 if ((*orig_from_mode & required_from_mode) !=
484 required_from_mode) {
485 dlog_verbose(
486 "Sender tried to send memory with permissions "
487 "which "
488 "required mode %#x but only had %#x itself.\n",
489 required_from_mode, *orig_from_mode);
490 return ffa_error(FFA_DENIED);
491 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000492 }
493
494 /* Find the appropriate new mode. */
495 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000496 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100497 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000498 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100499 break;
500
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100501 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000502 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100503 break;
504
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100505 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000506 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100507 break;
508
Jose Marinho75509b42019-04-09 09:34:59 +0100509 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100510 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100511 }
512
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100513 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000514}
515
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100516static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000517 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100518 struct ffa_memory_region_constituent **fragments,
519 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
520 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000521{
522 const uint32_t state_mask =
523 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
524 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100525 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000526
Andrew Walbranca808b12020-05-15 17:22:28 +0100527 ret = constituents_get_mode(from, orig_from_mode, fragments,
528 fragment_constituent_counts,
529 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100530 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100531 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000532 }
533
534 /* Ensure the address range is normal memory and not a device. */
535 if (*orig_from_mode & MM_MODE_D) {
536 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
537 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100538 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000539 }
540
541 /*
542 * Ensure the relinquishing VM is not the owner but has access to the
543 * memory.
544 */
545 orig_from_state = *orig_from_mode & state_mask;
546 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
547 dlog_verbose(
548 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100549 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000550 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100551 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000552 }
553
554 /* Find the appropriate new mode. */
555 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
556
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100557 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000558}
559
560/**
561 * Verify that all pages have the same mode, that the starting mode
562 * constitutes a valid state and obtain the next mode to apply
563 * to the retrieving VM.
564 *
565 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100566 * 1) FFA_DENIED if a state transition was not found;
567 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100568 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100569 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100570 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100571 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
572 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000573 */
J-Alvesfc19b372022-07-06 12:17:35 +0100574struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000575 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100576 struct ffa_memory_region_constituent **fragments,
577 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
578 uint32_t memory_to_attributes, uint32_t *to_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000579{
580 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100581 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000582
Andrew Walbranca808b12020-05-15 17:22:28 +0100583 ret = constituents_get_mode(to, &orig_to_mode, fragments,
584 fragment_constituent_counts,
585 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100586 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100587 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100588 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000589 }
590
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100591 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000592 /*
593 * If the original ffa memory send call has been processed
594 * successfully, it is expected the orig_to_mode would overlay
595 * with `state_mask`, as a result of the function
596 * `ffa_send_check_transition`.
597 */
J-Alves59ed0042022-07-28 18:26:41 +0100598 if (vm_id_is_current_world(to.vm->id)) {
599 assert((orig_to_mode &
600 (MM_MODE_INVALID | MM_MODE_UNOWNED |
601 MM_MODE_SHARED)) != 0U);
602 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000603 } else {
604 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +0100605 * If the retriever is from virtual FF-A instance:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000606 * Ensure the retriever has the expected state. We don't care
607 * about the MM_MODE_SHARED bit; either with or without it set
608 * are both valid representations of the !O-NA state.
609 */
J-Alvesa9cd7e32022-07-01 13:49:33 +0100610 if (vm_id_is_current_world(to.vm->id) &&
611 to.vm->id != HF_PRIMARY_VM_ID &&
612 (orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
613 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100614 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000615 }
616 }
617
618 /* Find the appropriate new mode. */
619 *to_mode = memory_to_attributes;
620 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100621 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000622 *to_mode |= 0;
623 break;
624
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100625 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000626 *to_mode |= MM_MODE_UNOWNED;
627 break;
628
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100629 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000630 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
631 break;
632
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100633 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000634 *to_mode |= 0;
635 break;
636
637 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100638 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100639 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000640 }
641
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100642 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100643}
Jose Marinho09b1db82019-08-08 09:16:59 +0100644
645/**
646 * Updates a VM's page table such that the given set of physical address ranges
647 * are mapped in the address space at the corresponding address ranges, in the
648 * mode provided.
649 *
650 * If commit is false, the page tables will be allocated from the mpool but no
651 * mappings will actually be updated. This function must always be called first
652 * with commit false to check that it will succeed before calling with commit
653 * true, to avoid leaving the page table in a half-updated state. To make a
654 * series of changes atomically you can call them all with commit false before
655 * calling them all with commit true.
656 *
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700657 * vm_ptable_defrag should always be called after a series of page table
658 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +0100659 *
660 * Returns true on success, or false if the update failed and no changes were
661 * made to memory mappings.
662 */
J-Alves66652252022-07-06 09:49:51 +0100663bool ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000664 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100665 struct ffa_memory_region_constituent **fragments,
666 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby4dd3f532021-09-21 09:57:08 +0100667 uint32_t mode, struct mpool *ppool, bool commit)
Jose Marinho09b1db82019-08-08 09:16:59 +0100668{
Andrew Walbranca808b12020-05-15 17:22:28 +0100669 uint32_t i;
670 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100671
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700672 if (vm_locked.vm->el0_partition) {
673 mode |= MM_MODE_USER | MM_MODE_NG;
674 }
675
Andrew Walbranca808b12020-05-15 17:22:28 +0100676 /* Iterate over the memory region constituents within each fragment. */
677 for (i = 0; i < fragment_count; ++i) {
678 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
679 size_t size = fragments[i][j].page_count * PAGE_SIZE;
680 paddr_t pa_begin =
681 pa_from_ipa(ipa_init(fragments[i][j].address));
682 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +0200683 uint32_t pa_bits =
684 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +0100685
686 /*
687 * Ensure the requested region falls into system's PA
688 * range.
689 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +0200690 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
691 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +0100692 dlog_error("Region is outside of PA Range\n");
693 return false;
694 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100695
696 if (commit) {
697 vm_identity_commit(vm_locked, pa_begin, pa_end,
698 mode, ppool, NULL);
699 } else if (!vm_identity_prepare(vm_locked, pa_begin,
700 pa_end, mode, ppool)) {
701 return false;
702 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100703 }
704 }
705
706 return true;
707}
708
709/**
710 * Clears a region of physical memory by overwriting it with zeros. The data is
711 * flushed from the cache so the memory has been cleared across the system.
712 */
J-Alves7db32002021-12-14 14:44:50 +0000713static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
714 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +0100715{
716 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000717 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100718 * global mapping of the whole range. Such an approach will limit
719 * the changes to stage-1 tables and will allow only local
720 * invalidation.
721 */
722 bool ret;
723 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +0000724 void *ptr = mm_identity_map(stage1_locked, begin, end,
725 MM_MODE_W | (extra_mode_attributes &
726 plat_ffa_other_world_mode()),
727 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100728 size_t size = pa_difference(begin, end);
729
730 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100731 goto fail;
732 }
733
734 memset_s(ptr, size, 0, size);
735 arch_mm_flush_dcache(ptr, size);
736 mm_unmap(stage1_locked, begin, end, ppool);
737
738 ret = true;
739 goto out;
740
741fail:
742 ret = false;
743
744out:
745 mm_unlock_stage1(&stage1_locked);
746
747 return ret;
748}
749
750/**
751 * Clears a region of physical memory by overwriting it with zeros. The data is
752 * flushed from the cache so the memory has been cleared across the system.
753 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100754static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +0000755 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100756 struct ffa_memory_region_constituent **fragments,
757 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
758 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100759{
760 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +0100761 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100762 bool ret = false;
763
764 /*
765 * Create a local pool so any freed memory can't be used by another
766 * thread. This is to ensure each constituent that is mapped can be
767 * unmapped again afterwards.
768 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000769 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100770
Andrew Walbranca808b12020-05-15 17:22:28 +0100771 /* Iterate over the memory region constituents within each fragment. */
772 for (i = 0; i < fragment_count; ++i) {
773 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100774
Andrew Walbranca808b12020-05-15 17:22:28 +0100775 for (j = 0; j < fragment_constituent_counts[j]; ++j) {
776 size_t size = fragments[i][j].page_count * PAGE_SIZE;
777 paddr_t begin =
778 pa_from_ipa(ipa_init(fragments[i][j].address));
779 paddr_t end = pa_add(begin, size);
780
J-Alves7db32002021-12-14 14:44:50 +0000781 if (!clear_memory(begin, end, &local_page_pool,
782 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100783 /*
784 * api_clear_memory will defrag on failure, so
785 * no need to do it here.
786 */
787 goto out;
788 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100789 }
790 }
791
Jose Marinho09b1db82019-08-08 09:16:59 +0100792 ret = true;
793
794out:
795 mpool_fini(&local_page_pool);
796 return ret;
797}
798
J-Alves5952d942022-12-22 16:03:00 +0000799static bool is_memory_range_within(ipaddr_t begin, ipaddr_t end,
800 ipaddr_t in_begin, ipaddr_t in_end)
801{
802 return (ipa_addr(begin) >= ipa_addr(in_begin) &&
803 ipa_addr(begin) < ipa_addr(in_end)) ||
804 (ipa_addr(end) <= ipa_addr(in_end) &&
805 ipa_addr(end) > ipa_addr(in_begin));
806}
807
808/**
809 * Receives a memory range and looks for overlaps with the remainder
810 * constituents of the memory share/lend/donate operation. Assumes they are
811 * passed in order to avoid having to loop over all the elements at each call.
812 * The function only compares the received memory ranges with those that follow
813 * within the same fragment, and subsequent fragments from the same operation.
814 */
815static bool ffa_memory_check_overlap(
816 struct ffa_memory_region_constituent **fragments,
817 const uint32_t *fragment_constituent_counts,
818 const uint32_t fragment_count, const uint32_t current_fragment,
819 const uint32_t current_constituent)
820{
821 uint32_t i = current_fragment;
822 uint32_t j = current_constituent;
823 ipaddr_t current_begin = ipa_init(fragments[i][j].address);
824 const uint32_t current_page_count = fragments[i][j].page_count;
825 size_t current_size = current_page_count * PAGE_SIZE;
826 ipaddr_t current_end = ipa_add(current_begin, current_size - 1);
827
828 if (current_size == 0 ||
829 current_size > UINT64_MAX - ipa_addr(current_begin)) {
830 dlog_verbose("Invalid page count. Addr: %x page_count: %x\n",
831 current_begin, current_page_count);
832 return false;
833 }
834
835 for (; i < fragment_count; i++) {
836 j = (i == current_fragment) ? j + 1 : 0;
837
838 for (; j < fragment_constituent_counts[i]; j++) {
839 ipaddr_t begin = ipa_init(fragments[i][j].address);
840 const uint32_t page_count = fragments[i][j].page_count;
841 size_t size = page_count * PAGE_SIZE;
842 ipaddr_t end = ipa_add(begin, size - 1);
843
844 if (size == 0 || size > UINT64_MAX - ipa_addr(begin)) {
845 dlog_verbose(
846 "Invalid page count. Addr: %x "
847 "page_count: %x\n",
848 begin, page_count);
849 return false;
850 }
851
852 /*
853 * Check if current ranges is within begin and end, as
854 * well as the reverse. This should help optimize the
855 * loop, and reduce the number of iterations.
856 */
857 if (is_memory_range_within(begin, end, current_begin,
858 current_end) ||
859 is_memory_range_within(current_begin, current_end,
860 begin, end)) {
861 dlog_verbose(
862 "Overlapping memory ranges: %#x - %#x "
863 "with %#x - %#x\n",
864 ipa_addr(begin), ipa_addr(end),
865 ipa_addr(current_begin),
866 ipa_addr(current_end));
867 return true;
868 }
869 }
870 }
871
872 return false;
873}
874
Jose Marinho09b1db82019-08-08 09:16:59 +0100875/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000876 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +0100877 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000878 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +0100879 *
880 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000881 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100882 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +0100883 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100884 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
885 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100886 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +0100887 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100888 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +0100889 */
J-Alves66652252022-07-06 09:49:51 +0100890struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000891 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100892 struct ffa_memory_region_constituent **fragments,
893 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves8f11cde2022-12-21 16:18:22 +0000894 uint32_t composite_total_page_count, uint32_t share_func,
895 struct ffa_memory_access *receivers, uint32_t receivers_count,
896 struct mpool *page_pool, bool clear, uint32_t *orig_from_mode_ret)
Jose Marinho09b1db82019-08-08 09:16:59 +0100897{
Andrew Walbranca808b12020-05-15 17:22:28 +0100898 uint32_t i;
J-Alves8f11cde2022-12-21 16:18:22 +0000899 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100900 uint32_t orig_from_mode;
901 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +0100902 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100903 struct ffa_value ret;
J-Alves8f11cde2022-12-21 16:18:22 +0000904 uint32_t constituents_total_page_count = 0;
Jose Marinho09b1db82019-08-08 09:16:59 +0100905
906 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +0100907 * Make sure constituents are properly aligned to a 64-bit boundary. If
908 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100909 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100910 for (i = 0; i < fragment_count; ++i) {
911 if (!is_aligned(fragments[i], 8)) {
912 dlog_verbose("Constituents not aligned.\n");
913 return ffa_error(FFA_INVALID_PARAMETERS);
914 }
J-Alves8f11cde2022-12-21 16:18:22 +0000915 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
916 constituents_total_page_count +=
917 fragments[i][j].page_count;
J-Alves5952d942022-12-22 16:03:00 +0000918 if (ffa_memory_check_overlap(
919 fragments, fragment_constituent_counts,
920 fragment_count, i, j)) {
921 return ffa_error(FFA_INVALID_PARAMETERS);
922 }
J-Alves8f11cde2022-12-21 16:18:22 +0000923 }
924 }
925
926 if (constituents_total_page_count != composite_total_page_count) {
927 dlog_verbose(
928 "Composite page count differs from calculated page "
929 "count from constituents.\n");
930 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +0100931 }
932
933 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000934 * Check if the state transition is lawful for the sender, ensure that
935 * all constituents of a memory region being shared are at the same
936 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +0100937 */
J-Alves363f5722022-04-25 17:37:37 +0100938 ret = ffa_send_check_transition(from_locked, share_func, receivers,
939 receivers_count, &orig_from_mode,
940 fragments, fragment_constituent_counts,
Andrew Walbranca808b12020-05-15 17:22:28 +0100941 fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100942 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100943 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100944 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100945 }
946
Andrew Walbran37c574e2020-06-03 11:45:46 +0100947 if (orig_from_mode_ret != NULL) {
948 *orig_from_mode_ret = orig_from_mode;
949 }
950
Jose Marinho09b1db82019-08-08 09:16:59 +0100951 /*
952 * Create a local pool so any freed memory can't be used by another
953 * thread. This is to ensure the original mapping can be restored if the
954 * clear fails.
955 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000956 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100957
958 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000959 * First reserve all required memory for the new page table entries
960 * without committing, to make sure the entire operation will succeed
961 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +0100962 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100963 if (!ffa_region_group_identity_map(
964 from_locked, fragments, fragment_constituent_counts,
965 fragment_count, from_mode, page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100966 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100967 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100968 goto out;
969 }
970
971 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000972 * Update the mapping for the sender. This won't allocate because the
973 * transaction was already prepared above, but may free pages in the
974 * case that a whole block is being unmapped that was previously
975 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +0100976 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100977 CHECK(ffa_region_group_identity_map(
978 from_locked, fragments, fragment_constituent_counts,
979 fragment_count, from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100980
981 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +0000982 if (clear &&
983 !ffa_clear_memory_constituents(
984 plat_ffa_owner_world_mode(from_locked.vm->id), fragments,
985 fragment_constituent_counts, fragment_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100986 /*
987 * On failure, roll back by returning memory to the sender. This
988 * may allocate pages which were previously freed into
989 * `local_page_pool` by the call above, but will never allocate
990 * more pages than that so can never fail.
991 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100992 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +0100993 from_locked, fragments, fragment_constituent_counts,
994 fragment_count, orig_from_mode, &local_page_pool,
995 true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100996
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100997 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100998 goto out;
999 }
1000
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001001 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001002
1003out:
1004 mpool_fini(&local_page_pool);
1005
1006 /*
1007 * Tidy up the page table by reclaiming failed mappings (if there was an
1008 * error) or merging entries into blocks where possible (on success).
1009 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001010 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001011
1012 return ret;
1013}
1014
1015/**
1016 * Validates and maps memory shared from one VM to another.
1017 *
1018 * This function requires the calling context to hold the <to> lock.
1019 *
1020 * Returns:
1021 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001022 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001023 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001024 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001025 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001026 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001027 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001028struct ffa_value ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00001029 struct vm_locked to_locked, ffa_vm_id_t from_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01001030 struct ffa_memory_region_constituent **fragments,
1031 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1032 uint32_t memory_to_attributes, uint32_t share_func, bool clear,
1033 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001034{
Andrew Walbranca808b12020-05-15 17:22:28 +01001035 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001036 uint32_t to_mode;
1037 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001038 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001039
1040 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001041 * Make sure constituents are properly aligned to a 64-bit boundary. If
1042 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001043 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001044 for (i = 0; i < fragment_count; ++i) {
1045 if (!is_aligned(fragments[i], 8)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001046 dlog_verbose("Fragment not properly aligned.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +01001047 return ffa_error(FFA_INVALID_PARAMETERS);
1048 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001049 }
1050
1051 /*
1052 * Check if the state transition is lawful for the recipient, and ensure
1053 * that all constituents of the memory region being retrieved are at the
1054 * same state.
1055 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001056 ret = ffa_retrieve_check_transition(
1057 to_locked, share_func, fragments, fragment_constituent_counts,
1058 fragment_count, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001059 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001060 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001061 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001062 }
1063
1064 /*
1065 * Create a local pool so any freed memory can't be used by another
1066 * thread. This is to ensure the original mapping can be restored if the
1067 * clear fails.
1068 */
1069 mpool_init_with_fallback(&local_page_pool, page_pool);
1070
1071 /*
1072 * First reserve all required memory for the new page table entries in
1073 * the recipient page tables without committing, to make sure the entire
1074 * operation will succeed without exhausting the page pool.
1075 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001076 if (!ffa_region_group_identity_map(
1077 to_locked, fragments, fragment_constituent_counts,
1078 fragment_count, to_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001079 /* TODO: partial defrag of failed range. */
1080 dlog_verbose(
1081 "Insufficient memory to update recipient page "
1082 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001083 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001084 goto out;
1085 }
1086
1087 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001088 if (clear &&
1089 !ffa_clear_memory_constituents(
1090 plat_ffa_owner_world_mode(from_id), fragments,
1091 fragment_constituent_counts, fragment_count, page_pool)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001092 dlog_verbose("Couldn't clear constituents.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001093 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001094 goto out;
1095 }
1096
Jose Marinho09b1db82019-08-08 09:16:59 +01001097 /*
1098 * Complete the transfer by mapping the memory into the recipient. This
1099 * won't allocate because the transaction was already prepared above, so
1100 * it doesn't need to use the `local_page_pool`.
1101 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001102 CHECK(ffa_region_group_identity_map(
1103 to_locked, fragments, fragment_constituent_counts,
1104 fragment_count, to_mode, page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001105
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001106 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001107
1108out:
1109 mpool_fini(&local_page_pool);
1110
1111 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001112 * Tidy up the page table by reclaiming failed mappings (if there was an
1113 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001114 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001115 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001116
1117 return ret;
1118}
1119
Andrew Walbran996d1d12020-05-27 14:08:43 +01001120static struct ffa_value ffa_relinquish_check_update(
J-Alves3c5b2072022-11-21 12:45:40 +00001121 struct vm_locked from_locked, ffa_vm_id_t owner_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01001122 struct ffa_memory_region_constituent **fragments,
1123 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1124 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001125{
1126 uint32_t orig_from_mode;
1127 uint32_t from_mode;
1128 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001129 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001130
Andrew Walbranca808b12020-05-15 17:22:28 +01001131 ret = ffa_relinquish_check_transition(
1132 from_locked, &orig_from_mode, fragments,
1133 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001134 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001135 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001136 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001137 }
1138
1139 /*
1140 * Create a local pool so any freed memory can't be used by another
1141 * thread. This is to ensure the original mapping can be restored if the
1142 * clear fails.
1143 */
1144 mpool_init_with_fallback(&local_page_pool, page_pool);
1145
1146 /*
1147 * First reserve all required memory for the new page table entries
1148 * without committing, to make sure the entire operation will succeed
1149 * without exhausting the page pool.
1150 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001151 if (!ffa_region_group_identity_map(
1152 from_locked, fragments, fragment_constituent_counts,
1153 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001154 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001155 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001156 goto out;
1157 }
1158
1159 /*
1160 * Update the mapping for the sender. This won't allocate because the
1161 * transaction was already prepared above, but may free pages in the
1162 * case that a whole block is being unmapped that was previously
1163 * partially mapped.
1164 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001165 CHECK(ffa_region_group_identity_map(
1166 from_locked, fragments, fragment_constituent_counts,
1167 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001168
1169 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001170 if (clear &&
1171 !ffa_clear_memory_constituents(
J-Alves3c5b2072022-11-21 12:45:40 +00001172 plat_ffa_owner_world_mode(owner_id), fragments,
J-Alves7db32002021-12-14 14:44:50 +00001173 fragment_constituent_counts, fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001174 /*
1175 * On failure, roll back by returning memory to the sender. This
1176 * may allocate pages which were previously freed into
1177 * `local_page_pool` by the call above, but will never allocate
1178 * more pages than that so can never fail.
1179 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001180 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001181 from_locked, fragments, fragment_constituent_counts,
1182 fragment_count, orig_from_mode, &local_page_pool,
1183 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001184
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001185 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001186 goto out;
1187 }
1188
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001189 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001190
1191out:
1192 mpool_fini(&local_page_pool);
1193
1194 /*
1195 * Tidy up the page table by reclaiming failed mappings (if there was an
1196 * error) or merging entries into blocks where possible (on success).
1197 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001198 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001199
1200 return ret;
1201}
1202
1203/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001204 * Complete a memory sending operation by checking that it is valid, updating
1205 * the sender page table, and then either marking the share state as having
1206 * completed sending (on success) or freeing it (on failure).
1207 *
1208 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1209 */
J-Alvesfdd29272022-07-19 13:16:31 +01001210struct ffa_value ffa_memory_send_complete(
Andrew Walbranca808b12020-05-15 17:22:28 +01001211 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001212 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1213 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001214{
1215 struct ffa_memory_region *memory_region = share_state->memory_region;
J-Alves8f11cde2022-12-21 16:18:22 +00001216 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01001217 struct ffa_value ret;
1218
1219 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001220 assert(share_states.share_states != NULL);
J-Alves8f11cde2022-12-21 16:18:22 +00001221 assert(memory_region != NULL);
1222 composite = ffa_memory_region_get_composite(memory_region, 0);
1223 assert(composite != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001224
1225 /* Check that state is valid in sender page table and update. */
1226 ret = ffa_send_check_update(
1227 from_locked, share_state->fragments,
1228 share_state->fragment_constituent_counts,
J-Alves8f11cde2022-12-21 16:18:22 +00001229 share_state->fragment_count, composite->page_count,
1230 share_state->share_func, memory_region->receivers,
1231 memory_region->receiver_count, page_pool,
1232 memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001233 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001234 if (ret.func != FFA_SUCCESS_32) {
1235 /*
1236 * Free share state, it failed to send so it can't be retrieved.
1237 */
1238 dlog_verbose("Complete failed, freeing share state.\n");
1239 share_state_free(share_states, share_state, page_pool);
1240 return ret;
1241 }
1242
1243 share_state->sending_complete = true;
1244 dlog_verbose("Marked sending complete.\n");
1245
J-Alvesee68c542020-10-29 17:48:20 +00001246 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001247}
1248
1249/**
Federico Recanatia98603a2021-12-20 18:04:03 +01001250 * Check that the memory attributes match Hafnium expectations:
1251 * Normal Memory, Inner shareable, Write-Back Read-Allocate
1252 * Write-Allocate Cacheable.
1253 */
1254static struct ffa_value ffa_memory_attributes_validate(
J-Alves7a99d0d2023-02-08 13:49:48 +00001255 ffa_memory_attributes_t attributes)
Federico Recanatia98603a2021-12-20 18:04:03 +01001256{
1257 enum ffa_memory_type memory_type;
1258 enum ffa_memory_cacheability cacheability;
1259 enum ffa_memory_shareability shareability;
1260
1261 memory_type = ffa_get_memory_type_attr(attributes);
1262 if (memory_type != FFA_MEMORY_NORMAL_MEM) {
1263 dlog_verbose("Invalid memory type %#x, expected %#x.\n",
1264 memory_type, FFA_MEMORY_NORMAL_MEM);
Federico Recanati3d953f32022-02-17 09:31:29 +01001265 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001266 }
1267
1268 cacheability = ffa_get_memory_cacheability_attr(attributes);
1269 if (cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1270 dlog_verbose("Invalid cacheability %#x, expected %#x.\n",
1271 cacheability, FFA_MEMORY_CACHE_WRITE_BACK);
Federico Recanati3d953f32022-02-17 09:31:29 +01001272 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001273 }
1274
1275 shareability = ffa_get_memory_shareability_attr(attributes);
1276 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
1277 dlog_verbose("Invalid shareability %#x, expected #%x.\n",
1278 shareability, FFA_MEMORY_INNER_SHAREABLE);
Federico Recanati3d953f32022-02-17 09:31:29 +01001279 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001280 }
1281
1282 return (struct ffa_value){.func = FFA_SUCCESS_32};
1283}
1284
1285/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001286 * Check that the given `memory_region` represents a valid memory send request
1287 * of the given `share_func` type, return the clear flag and permissions via the
1288 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001289 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001290 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001291 * not.
1292 */
J-Alves66652252022-07-06 09:49:51 +01001293struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001294 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1295 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001296 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001297{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001298 struct ffa_composite_memory_region *composite;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001299 uint64_t receivers_end;
1300 uint64_t min_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001301 uint32_t composite_memory_region_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001302 uint32_t constituents_start;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001303 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001304 enum ffa_data_access data_access;
1305 enum ffa_instruction_access instruction_access;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001306 enum ffa_memory_security security_state;
Federico Recanatia98603a2021-12-20 18:04:03 +01001307 struct ffa_value ret;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001308 const size_t minimum_first_fragment_length =
1309 (sizeof(struct ffa_memory_region) +
1310 sizeof(struct ffa_memory_access) +
1311 sizeof(struct ffa_composite_memory_region));
1312
1313 if (fragment_length < minimum_first_fragment_length) {
1314 dlog_verbose("Fragment length %u too short (min %u).\n",
1315 (size_t)fragment_length,
1316 minimum_first_fragment_length);
1317 return ffa_error(FFA_INVALID_PARAMETERS);
1318 }
1319
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05001320 static_assert(sizeof(struct ffa_memory_region_constituent) == 16,
1321 "struct ffa_memory_region_constituent must be 16 bytes");
1322 if (!is_aligned(fragment_length,
1323 sizeof(struct ffa_memory_region_constituent)) ||
1324 !is_aligned(memory_share_length,
1325 sizeof(struct ffa_memory_region_constituent))) {
1326 dlog_verbose(
1327 "Fragment length %u or total length %u"
1328 " is not 16-byte aligned.\n",
1329 fragment_length, memory_share_length);
1330 return ffa_error(FFA_INVALID_PARAMETERS);
1331 }
1332
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001333 if (fragment_length > memory_share_length) {
1334 dlog_verbose(
1335 "Fragment length %u greater than total length %u.\n",
1336 (size_t)fragment_length, (size_t)memory_share_length);
1337 return ffa_error(FFA_INVALID_PARAMETERS);
1338 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001339
J-Alves0b6653d2022-04-22 13:17:38 +01001340 assert(memory_region->receivers_offset ==
1341 offsetof(struct ffa_memory_region, receivers));
1342 assert(memory_region->memory_access_desc_size ==
1343 sizeof(struct ffa_memory_access));
1344
J-Alves95df0ef2022-12-07 10:09:48 +00001345 /* The sender must match the caller. */
1346 if ((!vm_id_is_current_world(from_locked.vm->id) &&
1347 vm_id_is_current_world(memory_region->sender)) ||
1348 (vm_id_is_current_world(from_locked.vm->id) &&
1349 memory_region->sender != from_locked.vm->id)) {
1350 dlog_verbose("Invalid memory sender ID.\n");
1351 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001352 }
1353
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001354 if (memory_region->receiver_count <= 0) {
1355 dlog_verbose("No receivers!\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001356 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001357 }
1358
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001359 /*
1360 * Ensure that the composite header is within the memory bounds and
1361 * doesn't overlap the first part of the message. Cast to uint64_t
1362 * to prevent overflow.
1363 */
1364 receivers_end = ((uint64_t)sizeof(struct ffa_memory_access) *
1365 (uint64_t)memory_region->receiver_count) +
1366 sizeof(struct ffa_memory_region);
1367 min_length = receivers_end +
1368 sizeof(struct ffa_composite_memory_region) +
1369 sizeof(struct ffa_memory_region_constituent);
1370 if (min_length > memory_share_length) {
1371 dlog_verbose("Share too short: got %u but minimum is %u.\n",
1372 (size_t)memory_share_length, (size_t)min_length);
1373 return ffa_error(FFA_INVALID_PARAMETERS);
1374 }
1375
1376 composite_memory_region_offset =
1377 memory_region->receivers[0].composite_memory_region_offset;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001378
1379 /*
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001380 * Check that the composite memory region descriptor is after the access
1381 * descriptors, is at least 16-byte aligned, and fits in the first
1382 * fragment.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001383 */
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001384 if ((composite_memory_region_offset < receivers_end) ||
1385 (composite_memory_region_offset % 16 != 0) ||
1386 (composite_memory_region_offset >
1387 fragment_length - sizeof(struct ffa_composite_memory_region))) {
1388 dlog_verbose(
1389 "Invalid composite memory region descriptor offset "
1390 "%u.\n",
1391 (size_t)composite_memory_region_offset);
1392 return ffa_error(FFA_INVALID_PARAMETERS);
1393 }
1394
1395 /*
1396 * Compute the start of the constituent regions. Already checked
1397 * to be not more than fragment_length and thus not more than
1398 * memory_share_length.
1399 */
1400 constituents_start = composite_memory_region_offset +
1401 sizeof(struct ffa_composite_memory_region);
1402 constituents_length = memory_share_length - constituents_start;
1403
1404 /*
1405 * Check that the number of constituents is consistent with the length
1406 * of the constituent region.
1407 */
1408 composite = ffa_memory_region_get_composite(memory_region, 0);
1409 if ((constituents_length %
1410 sizeof(struct ffa_memory_region_constituent) !=
1411 0) ||
1412 ((constituents_length /
1413 sizeof(struct ffa_memory_region_constituent)) !=
1414 composite->constituent_count)) {
1415 dlog_verbose("Invalid length %u or composite offset %u.\n",
1416 (size_t)memory_share_length,
1417 (size_t)composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001418 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001419 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001420 if (fragment_length < memory_share_length &&
1421 fragment_length < HF_MAILBOX_SIZE) {
1422 dlog_warning(
1423 "Initial fragment length %d smaller than mailbox "
1424 "size.\n",
1425 fragment_length);
1426 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001427
Andrew Walbrana65a1322020-04-06 19:32:32 +01001428 /*
1429 * Clear is not allowed for memory sharing, as the sender still has
1430 * access to the memory.
1431 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001432 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1433 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001434 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001435 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001436 }
1437
1438 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001439 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001440 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001441 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001442 }
1443
J-Alves363f5722022-04-25 17:37:37 +01001444 /* Check that the permissions are valid, for each specified receiver. */
1445 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
1446 ffa_memory_access_permissions_t permissions =
1447 memory_region->receivers[i]
1448 .receiver_permissions.permissions;
1449 ffa_vm_id_t receiver_id =
1450 memory_region->receivers[i]
1451 .receiver_permissions.receiver;
1452
1453 if (memory_region->sender == receiver_id) {
1454 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001455 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001456 }
Federico Recanati85090c42021-12-15 13:17:54 +01001457
J-Alves363f5722022-04-25 17:37:37 +01001458 for (uint32_t j = i + 1; j < memory_region->receiver_count;
1459 j++) {
1460 if (receiver_id ==
1461 memory_region->receivers[j]
1462 .receiver_permissions.receiver) {
1463 dlog_verbose(
1464 "Repeated receiver(%x) in memory send "
1465 "operation.\n",
1466 memory_region->receivers[j]
1467 .receiver_permissions.receiver);
1468 return ffa_error(FFA_INVALID_PARAMETERS);
1469 }
1470 }
1471
1472 if (composite_memory_region_offset !=
1473 memory_region->receivers[i]
1474 .composite_memory_region_offset) {
1475 dlog_verbose(
1476 "All ffa_memory_access should point to the "
1477 "same composite memory region offset.\n");
1478 return ffa_error(FFA_INVALID_PARAMETERS);
1479 }
1480
1481 data_access = ffa_get_data_access_attr(permissions);
1482 instruction_access =
1483 ffa_get_instruction_access_attr(permissions);
1484 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1485 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
1486 dlog_verbose(
1487 "Reserved value for receiver permissions "
1488 "%#x.\n",
1489 permissions);
1490 return ffa_error(FFA_INVALID_PARAMETERS);
1491 }
1492 if (instruction_access !=
1493 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
1494 dlog_verbose(
1495 "Invalid instruction access permissions %#x "
1496 "for sending memory.\n",
1497 permissions);
1498 return ffa_error(FFA_INVALID_PARAMETERS);
1499 }
1500 if (share_func == FFA_MEM_SHARE_32) {
1501 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1502 dlog_verbose(
1503 "Invalid data access permissions %#x "
1504 "for sharing memory.\n",
1505 permissions);
1506 return ffa_error(FFA_INVALID_PARAMETERS);
1507 }
1508 /*
1509 * According to section 10.10.3 of the FF-A v1.1 EAC0
1510 * spec, NX is required for share operations (but must
1511 * not be specified by the sender) so set it in the
1512 * copy that we store, ready to be returned to the
1513 * retriever.
1514 */
J-Alvesb19731a2022-06-20 17:30:33 +01001515 if (vm_id_is_current_world(receiver_id)) {
1516 ffa_set_instruction_access_attr(
1517 &permissions,
1518 FFA_INSTRUCTION_ACCESS_NX);
1519 memory_region->receivers[i]
1520 .receiver_permissions.permissions =
1521 permissions;
1522 }
J-Alves363f5722022-04-25 17:37:37 +01001523 }
1524 if (share_func == FFA_MEM_LEND_32 &&
1525 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1526 dlog_verbose(
1527 "Invalid data access permissions %#x for "
1528 "lending memory.\n",
1529 permissions);
1530 return ffa_error(FFA_INVALID_PARAMETERS);
1531 }
1532
1533 if (share_func == FFA_MEM_DONATE_32 &&
1534 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
1535 dlog_verbose(
1536 "Invalid data access permissions %#x for "
1537 "donating memory.\n",
1538 permissions);
1539 return ffa_error(FFA_INVALID_PARAMETERS);
1540 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001541 }
1542
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001543 /* Memory region attributes NS-Bit MBZ for FFA_MEM_SHARE/LEND/DONATE. */
1544 security_state =
1545 ffa_get_memory_security_attr(memory_region->attributes);
1546 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
1547 dlog_verbose(
1548 "Invalid security state for memory share operation.\n");
1549 return ffa_error(FFA_INVALID_PARAMETERS);
1550 }
1551
Federico Recanatid937f5e2021-12-20 17:38:23 +01001552 /*
J-Alves807794e2022-06-16 13:42:47 +01001553 * If a memory donate or lend with single borrower, the memory type
1554 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01001555 */
J-Alves807794e2022-06-16 13:42:47 +01001556 if (share_func == FFA_MEM_DONATE_32 ||
1557 (share_func == FFA_MEM_LEND_32 &&
1558 memory_region->receiver_count == 1)) {
1559 if (ffa_get_memory_type_attr(memory_region->attributes) !=
1560 FFA_MEMORY_NOT_SPECIFIED_MEM) {
1561 dlog_verbose(
1562 "Memory type shall not be specified by "
1563 "sender.\n");
1564 return ffa_error(FFA_INVALID_PARAMETERS);
1565 }
1566 } else {
1567 /*
1568 * Check that sender's memory attributes match Hafnium
1569 * expectations: Normal Memory, Inner shareable, Write-Back
1570 * Read-Allocate Write-Allocate Cacheable.
1571 */
1572 ret = ffa_memory_attributes_validate(memory_region->attributes);
1573 if (ret.func != FFA_SUCCESS_32) {
1574 return ret;
1575 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01001576 }
1577
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001578 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001579}
1580
1581/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001582 * Gets the share state for continuing an operation to donate, lend or share
1583 * memory, and checks that it is a valid request.
1584 *
1585 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1586 * not.
1587 */
J-Alvesfdd29272022-07-19 13:16:31 +01001588struct ffa_value ffa_memory_send_continue_validate(
Andrew Walbranca808b12020-05-15 17:22:28 +01001589 struct share_states_locked share_states, ffa_memory_handle_t handle,
1590 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1591 struct mpool *page_pool)
1592{
1593 struct ffa_memory_share_state *share_state;
1594 struct ffa_memory_region *memory_region;
1595
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001596 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001597
1598 /*
1599 * Look up the share state by handle and make sure that the VM ID
1600 * matches.
1601 */
1602 if (!get_share_state(share_states, handle, &share_state)) {
1603 dlog_verbose(
1604 "Invalid handle %#x for memory send continuation.\n",
1605 handle);
1606 return ffa_error(FFA_INVALID_PARAMETERS);
1607 }
1608 memory_region = share_state->memory_region;
1609
J-Alvesfdd29272022-07-19 13:16:31 +01001610 if (vm_id_is_current_world(from_vm_id) &&
1611 memory_region->sender != from_vm_id) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001612 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1613 return ffa_error(FFA_INVALID_PARAMETERS);
1614 }
1615
1616 if (share_state->sending_complete) {
1617 dlog_verbose(
1618 "Sending of memory handle %#x is already complete.\n",
1619 handle);
1620 return ffa_error(FFA_INVALID_PARAMETERS);
1621 }
1622
1623 if (share_state->fragment_count == MAX_FRAGMENTS) {
1624 /*
1625 * Log a warning as this is a sign that MAX_FRAGMENTS should
1626 * probably be increased.
1627 */
1628 dlog_warning(
1629 "Too many fragments for memory share with handle %#x; "
1630 "only %d supported.\n",
1631 handle, MAX_FRAGMENTS);
1632 /* Free share state, as it's not possible to complete it. */
1633 share_state_free(share_states, share_state, page_pool);
1634 return ffa_error(FFA_NO_MEMORY);
1635 }
1636
1637 *share_state_ret = share_state;
1638
1639 return (struct ffa_value){.func = FFA_SUCCESS_32};
1640}
1641
1642/**
J-Alves95df0ef2022-12-07 10:09:48 +00001643 * Checks if there is at least one receiver from the other world.
1644 */
J-Alvesfdd29272022-07-19 13:16:31 +01001645bool memory_region_receivers_from_other_world(
J-Alves95df0ef2022-12-07 10:09:48 +00001646 struct ffa_memory_region *memory_region)
1647{
1648 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
1649 ffa_vm_id_t receiver = memory_region->receivers[i]
1650 .receiver_permissions.receiver;
1651 if (!vm_id_is_current_world(receiver)) {
1652 return true;
1653 }
1654 }
1655 return false;
1656}
1657
1658/**
J-Alves9da280b2022-12-21 14:55:39 +00001659 * Validates a call to donate, lend or share memory in which Hafnium is the
1660 * designated allocator of the memory handle. In practice, this also means
1661 * Hafnium is responsible for managing the state structures for the transaction.
1662 * If Hafnium is the SPMC, it should allocate the memory handle when either the
1663 * sender is an SP or there is at least one borrower that is an SP.
1664 * If Hafnium is the hypervisor, it should allocate the memory handle when
1665 * operation involves only NWd VMs.
1666 *
1667 * If validation goes well, Hafnium updates the stage-2 page tables of the
1668 * sender. Validation consists of checking if the message length and number of
1669 * memory region constituents match, and if the transition is valid for the
1670 * type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001671 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001672 * Assumes that the caller has already found and locked the sender VM and copied
1673 * the memory region descriptor from the sender's TX buffer to a freshly
1674 * allocated page from Hafnium's internal pool. The caller must have also
1675 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001676 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001677 * This function takes ownership of the `memory_region` passed in and will free
1678 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001679 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001680struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001681 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001682 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001683 uint32_t fragment_length, uint32_t share_func,
1684 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001685{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001686 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001687 struct share_states_locked share_states;
1688 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001689
1690 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001691 * If there is an error validating the `memory_region` then we need to
1692 * free it because we own it but we won't be storing it in a share state
1693 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001694 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001695 ret = ffa_memory_send_validate(from_locked, memory_region,
1696 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001697 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001698 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001699 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001700 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001701 }
1702
Andrew Walbrana65a1322020-04-06 19:32:32 +01001703 /* Set flag for share function, ready to be retrieved later. */
1704 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001705 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001706 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001707 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001708 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001709 case FFA_MEM_LEND_32:
1710 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001711 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001712 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001713 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001714 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001715 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001716 }
1717
Andrew Walbranca808b12020-05-15 17:22:28 +01001718 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001719 /*
1720 * Allocate a share state before updating the page table. Otherwise if
1721 * updating the page table succeeded but allocating the share state
1722 * failed then it would leave the memory in a state where nobody could
1723 * get it back.
1724 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001725 if (!allocate_share_state(share_states, share_func, memory_region,
1726 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1727 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001728 dlog_verbose("Failed to allocate share state.\n");
1729 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001730 ret = ffa_error(FFA_NO_MEMORY);
1731 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001732 }
1733
Andrew Walbranca808b12020-05-15 17:22:28 +01001734 if (fragment_length == memory_share_length) {
1735 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00001736 ret = ffa_memory_send_complete(
1737 from_locked, share_states, share_state, page_pool,
1738 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001739 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01001740 /*
1741 * Use sender ID from 'memory_region' assuming
1742 * that at this point it has been validated:
1743 * - MBZ at virtual FF-A instance.
1744 */
1745 ffa_vm_id_t sender_to_ret =
1746 (from_locked.vm->id == HF_OTHER_WORLD_ID)
1747 ? memory_region->sender
1748 : 0;
Andrew Walbranca808b12020-05-15 17:22:28 +01001749 ret = (struct ffa_value){
1750 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00001751 .arg1 = (uint32_t)memory_region->handle,
1752 .arg2 = (uint32_t)(memory_region->handle >> 32),
J-Alvesfdd29272022-07-19 13:16:31 +01001753 .arg3 = fragment_length,
1754 .arg4 = (uint32_t)(sender_to_ret & 0xffff) << 16};
Andrew Walbranca808b12020-05-15 17:22:28 +01001755 }
1756
1757out:
1758 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001759 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001760 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001761}
1762
1763/**
J-Alves8505a8a2022-06-15 18:10:18 +01001764 * Continues an operation to donate, lend or share memory to a VM from current
1765 * world. If this is the last fragment then checks that the transition is valid
1766 * for the type of memory sending operation and updates the stage-2 page tables
1767 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01001768 *
1769 * Assumes that the caller has already found and locked the sender VM and copied
1770 * the memory region descriptor from the sender's TX buffer to a freshly
1771 * allocated page from Hafnium's internal pool.
1772 *
1773 * This function takes ownership of the `fragment` passed in; it must not be
1774 * freed by the caller.
1775 */
1776struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1777 void *fragment,
1778 uint32_t fragment_length,
1779 ffa_memory_handle_t handle,
1780 struct mpool *page_pool)
1781{
1782 struct share_states_locked share_states = share_states_lock();
1783 struct ffa_memory_share_state *share_state;
1784 struct ffa_value ret;
1785 struct ffa_memory_region *memory_region;
1786
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05001787 CHECK(is_aligned(fragment,
1788 alignof(struct ffa_memory_region_constituent)));
1789 if (fragment_length % sizeof(struct ffa_memory_region_constituent) !=
1790 0) {
1791 dlog_verbose("Fragment length %u misaligned.\n",
1792 fragment_length);
1793 ret = ffa_error(FFA_INVALID_PARAMETERS);
1794 goto out_free_fragment;
1795 }
1796
Andrew Walbranca808b12020-05-15 17:22:28 +01001797 ret = ffa_memory_send_continue_validate(share_states, handle,
1798 &share_state,
1799 from_locked.vm->id, page_pool);
1800 if (ret.func != FFA_SUCCESS_32) {
1801 goto out_free_fragment;
1802 }
1803 memory_region = share_state->memory_region;
1804
J-Alves95df0ef2022-12-07 10:09:48 +00001805 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001806 dlog_error(
1807 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01001808 "other world. This should never happen, and indicates "
1809 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01001810 "EL3 code.\n");
1811 ret = ffa_error(FFA_INVALID_PARAMETERS);
1812 goto out_free_fragment;
1813 }
1814
1815 /* Add this fragment. */
1816 share_state->fragments[share_state->fragment_count] = fragment;
1817 share_state->fragment_constituent_counts[share_state->fragment_count] =
1818 fragment_length / sizeof(struct ffa_memory_region_constituent);
1819 share_state->fragment_count++;
1820
1821 /* Check whether the memory send operation is now ready to complete. */
1822 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00001823 ret = ffa_memory_send_complete(
1824 from_locked, share_states, share_state, page_pool,
1825 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001826 } else {
1827 ret = (struct ffa_value){
1828 .func = FFA_MEM_FRAG_RX_32,
1829 .arg1 = (uint32_t)handle,
1830 .arg2 = (uint32_t)(handle >> 32),
1831 .arg3 = share_state_next_fragment_offset(share_states,
1832 share_state)};
1833 }
1834 goto out;
1835
1836out_free_fragment:
1837 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001838
1839out:
Andrew Walbranca808b12020-05-15 17:22:28 +01001840 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001841 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001842}
1843
Andrew Walbranca808b12020-05-15 17:22:28 +01001844/** Clean up after the receiver has finished retrieving a memory region. */
1845static void ffa_memory_retrieve_complete(
1846 struct share_states_locked share_states,
1847 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
1848{
1849 if (share_state->share_func == FFA_MEM_DONATE_32) {
1850 /*
1851 * Memory that has been donated can't be relinquished,
1852 * so no need to keep the share state around.
1853 */
1854 share_state_free(share_states, share_state, page_pool);
1855 dlog_verbose("Freed share state for donate.\n");
1856 }
1857}
1858
J-Alves2d8457f2022-10-05 11:06:41 +01001859/**
1860 * Initialises the given memory region descriptor to be used for an
1861 * `FFA_MEM_RETRIEVE_RESP`, including the given constituents for the first
1862 * fragment.
1863 * The memory region descriptor is initialized according to retriever's
1864 * FF-A version.
1865 *
1866 * Returns true on success, or false if the given constituents won't all fit in
1867 * the first fragment.
1868 */
1869static bool ffa_retrieved_memory_region_init(
1870 void *response, uint32_t ffa_version, size_t response_max_size,
1871 ffa_vm_id_t sender, ffa_memory_attributes_t attributes,
1872 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
1873 ffa_vm_id_t receiver_id, ffa_memory_access_permissions_t permissions,
1874 uint32_t page_count, uint32_t total_constituent_count,
1875 const struct ffa_memory_region_constituent constituents[],
1876 uint32_t fragment_constituent_count, uint32_t *total_length,
1877 uint32_t *fragment_length)
1878{
1879 struct ffa_composite_memory_region *composite_memory_region;
1880 struct ffa_memory_access *receiver;
1881 uint32_t i;
1882 uint32_t constituents_offset;
1883 uint32_t receiver_count;
1884
1885 assert(response != NULL);
1886
1887 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1888 struct ffa_memory_region_v1_0 *retrieve_response =
1889 (struct ffa_memory_region_v1_0 *)response;
1890
J-Alves5da37d92022-10-24 16:33:48 +01001891 ffa_memory_region_init_header_v1_0(
1892 retrieve_response, sender, attributes, flags, handle, 0,
1893 RECEIVERS_COUNT_IN_RETRIEVE_RESP);
J-Alves2d8457f2022-10-05 11:06:41 +01001894
1895 receiver = &retrieve_response->receivers[0];
1896 receiver_count = retrieve_response->receiver_count;
1897
1898 receiver->composite_memory_region_offset =
1899 sizeof(struct ffa_memory_region_v1_0) +
1900 receiver_count * sizeof(struct ffa_memory_access);
1901
1902 composite_memory_region = ffa_memory_region_get_composite_v1_0(
1903 retrieve_response, 0);
1904 } else {
1905 /* Default to FF-A v1.1 version. */
1906 struct ffa_memory_region *retrieve_response =
1907 (struct ffa_memory_region *)response;
1908
1909 ffa_memory_region_init_header(retrieve_response, sender,
1910 attributes, flags, handle, 0, 1);
1911
1912 receiver = &retrieve_response->receivers[0];
1913 receiver_count = retrieve_response->receiver_count;
1914
1915 /*
1916 * Note that `sizeof(struct_ffa_memory_region)` and
1917 * `sizeof(struct ffa_memory_access)` must both be multiples of
1918 * 16 (as verified by the asserts in `ffa_memory.c`, so it is
1919 * guaranteed that the offset we calculate here is aligned to a
1920 * 64-bit boundary and so 64-bit values can be copied without
1921 * alignment faults.
1922 */
1923 receiver->composite_memory_region_offset =
1924 sizeof(struct ffa_memory_region) +
1925 receiver_count * sizeof(struct ffa_memory_access);
1926
1927 composite_memory_region =
1928 ffa_memory_region_get_composite(retrieve_response, 0);
1929 }
1930
1931 assert(receiver != NULL);
1932 assert(composite_memory_region != NULL);
1933
1934 /*
1935 * Initialized here as in memory retrieve responses we currently expect
1936 * one borrower to be specified.
1937 */
1938 ffa_memory_access_init_permissions(receiver, receiver_id, 0, 0, flags);
1939 receiver->receiver_permissions.permissions = permissions;
1940
1941 composite_memory_region->page_count = page_count;
1942 composite_memory_region->constituent_count = total_constituent_count;
1943 composite_memory_region->reserved_0 = 0;
1944
1945 constituents_offset = receiver->composite_memory_region_offset +
1946 sizeof(struct ffa_composite_memory_region);
1947 if (constituents_offset +
1948 fragment_constituent_count *
1949 sizeof(struct ffa_memory_region_constituent) >
1950 response_max_size) {
1951 return false;
1952 }
1953
1954 for (i = 0; i < fragment_constituent_count; ++i) {
1955 composite_memory_region->constituents[i] = constituents[i];
1956 }
1957
1958 if (total_length != NULL) {
1959 *total_length =
1960 constituents_offset +
1961 composite_memory_region->constituent_count *
1962 sizeof(struct ffa_memory_region_constituent);
1963 }
1964 if (fragment_length != NULL) {
1965 *fragment_length =
1966 constituents_offset +
1967 fragment_constituent_count *
1968 sizeof(struct ffa_memory_region_constituent);
1969 }
1970
1971 return true;
1972}
1973
J-Alves96de29f2022-04-26 16:05:24 +01001974/*
1975 * Gets the receiver's access permissions from 'struct ffa_memory_region' and
1976 * returns its index in the receiver's array. If receiver's ID doesn't exist
1977 * in the array, return the region's 'receiver_count'.
1978 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001979uint32_t ffa_memory_region_get_receiver(struct ffa_memory_region *memory_region,
1980 ffa_vm_id_t receiver)
J-Alves96de29f2022-04-26 16:05:24 +01001981{
1982 struct ffa_memory_access *receivers;
1983 uint32_t i;
1984
1985 assert(memory_region != NULL);
1986
1987 receivers = memory_region->receivers;
1988
1989 for (i = 0U; i < memory_region->receiver_count; i++) {
1990 if (receivers[i].receiver_permissions.receiver == receiver) {
1991 break;
1992 }
1993 }
1994
1995 return i;
1996}
1997
1998/**
1999 * Validates the retrieved permissions against those specified by the lender
2000 * of memory share operation. Optionally can help set the permissions to be used
2001 * for the S2 mapping, through the `permissions` argument.
2002 * Returns true if permissions are valid, false otherwise.
2003 */
2004static bool ffa_memory_retrieve_is_memory_access_valid(
2005 enum ffa_data_access sent_data_access,
2006 enum ffa_data_access requested_data_access,
2007 enum ffa_instruction_access sent_instruction_access,
2008 enum ffa_instruction_access requested_instruction_access,
2009 ffa_memory_access_permissions_t *permissions)
2010{
2011 switch (sent_data_access) {
2012 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2013 case FFA_DATA_ACCESS_RW:
2014 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2015 requested_data_access == FFA_DATA_ACCESS_RW) {
2016 if (permissions != NULL) {
2017 ffa_set_data_access_attr(permissions,
2018 FFA_DATA_ACCESS_RW);
2019 }
2020 break;
2021 }
2022 /* Intentional fall-through. */
2023 case FFA_DATA_ACCESS_RO:
2024 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2025 requested_data_access == FFA_DATA_ACCESS_RO) {
2026 if (permissions != NULL) {
2027 ffa_set_data_access_attr(permissions,
2028 FFA_DATA_ACCESS_RO);
2029 }
2030 break;
2031 }
2032 dlog_verbose(
2033 "Invalid data access requested; sender specified "
2034 "permissions %#x but receiver requested %#x.\n",
2035 sent_data_access, requested_data_access);
2036 return false;
2037 case FFA_DATA_ACCESS_RESERVED:
2038 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
2039 "checked before this point.");
2040 }
2041
2042 switch (sent_instruction_access) {
2043 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2044 case FFA_INSTRUCTION_ACCESS_X:
2045 if (requested_instruction_access ==
2046 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2047 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
2048 if (permissions != NULL) {
2049 ffa_set_instruction_access_attr(
2050 permissions, FFA_INSTRUCTION_ACCESS_X);
2051 }
2052 break;
2053 }
2054 case FFA_INSTRUCTION_ACCESS_NX:
2055 if (requested_instruction_access ==
2056 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2057 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2058 if (permissions != NULL) {
2059 ffa_set_instruction_access_attr(
2060 permissions, FFA_INSTRUCTION_ACCESS_NX);
2061 }
2062 break;
2063 }
2064 dlog_verbose(
2065 "Invalid instruction access requested; sender "
2066 "specified permissions %#x but receiver requested "
2067 "%#x.\n",
2068 sent_instruction_access, requested_instruction_access);
2069 return false;
2070 case FFA_INSTRUCTION_ACCESS_RESERVED:
2071 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
2072 "be checked before this point.");
2073 }
2074
2075 return true;
2076}
2077
2078/**
2079 * Validate the receivers' permissions in the retrieve request against those
2080 * specified by the lender.
2081 * In the `permissions` argument returns the permissions to set at S2 for the
2082 * caller to the FFA_MEMORY_RETRIEVE_REQ.
2083 * Returns FFA_SUCCESS if all specified permissions are valid.
2084 */
2085static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
2086 struct ffa_memory_region *memory_region,
2087 struct ffa_memory_region *retrieve_request, ffa_vm_id_t to_vm_id,
2088 ffa_memory_access_permissions_t *permissions)
2089{
2090 uint32_t retrieve_receiver_index;
2091
2092 assert(permissions != NULL);
2093
2094 if (retrieve_request->receiver_count != memory_region->receiver_count) {
2095 dlog_verbose(
2096 "Retrieve request should contain same list of "
2097 "borrowers, as specified by the lender.\n");
2098 return ffa_error(FFA_INVALID_PARAMETERS);
2099 }
2100
2101 retrieve_receiver_index = retrieve_request->receiver_count;
2102
2103 /* Should be populated with the permissions of the retriever. */
2104 *permissions = 0;
2105
2106 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
2107 ffa_memory_access_permissions_t sent_permissions;
2108 struct ffa_memory_access *current_receiver =
2109 &retrieve_request->receivers[i];
2110 ffa_memory_access_permissions_t requested_permissions =
2111 current_receiver->receiver_permissions.permissions;
2112 ffa_vm_id_t current_receiver_id =
2113 current_receiver->receiver_permissions.receiver;
2114 bool found_to_id = current_receiver_id == to_vm_id;
2115
2116 /*
2117 * Find the current receiver in the transaction descriptor from
2118 * sender.
2119 */
2120 uint32_t mem_region_receiver_index =
2121 ffa_memory_region_get_receiver(memory_region,
2122 current_receiver_id);
2123
2124 if (mem_region_receiver_index ==
2125 memory_region->receiver_count) {
2126 dlog_verbose("%s: receiver %x not found\n", __func__,
2127 current_receiver_id);
2128 return ffa_error(FFA_DENIED);
2129 }
2130
2131 sent_permissions =
2132 memory_region->receivers[mem_region_receiver_index]
2133 .receiver_permissions.permissions;
2134
2135 if (found_to_id) {
2136 retrieve_receiver_index = i;
2137 }
2138
2139 /*
2140 * Since we are traversing the list of receivers, save the index
2141 * of the caller. As it needs to be there.
2142 */
2143
2144 if (current_receiver->composite_memory_region_offset != 0U) {
2145 dlog_verbose(
2146 "Retriever specified address ranges not "
2147 "supported (got offset %d).\n",
2148 current_receiver
2149 ->composite_memory_region_offset);
2150 return ffa_error(FFA_INVALID_PARAMETERS);
2151 }
2152
2153 /*
2154 * Check permissions from sender against permissions requested
2155 * by receiver.
2156 */
2157 if (!ffa_memory_retrieve_is_memory_access_valid(
2158 ffa_get_data_access_attr(sent_permissions),
2159 ffa_get_data_access_attr(requested_permissions),
2160 ffa_get_instruction_access_attr(sent_permissions),
2161 ffa_get_instruction_access_attr(
2162 requested_permissions),
2163 found_to_id ? permissions : NULL)) {
2164 return ffa_error(FFA_DENIED);
2165 }
2166
2167 /*
2168 * Can't request PM to clear memory if only provided with RO
2169 * permissions.
2170 */
2171 if (found_to_id &&
2172 (ffa_get_data_access_attr(*permissions) ==
2173 FFA_DATA_ACCESS_RO) &&
2174 (retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2175 0U) {
2176 dlog_verbose(
2177 "Receiver has RO permissions can not request "
2178 "clear.\n");
2179 return ffa_error(FFA_DENIED);
2180 }
2181 }
2182
2183 if (retrieve_receiver_index == retrieve_request->receiver_count) {
2184 dlog_verbose(
2185 "Retrieve request does not contain caller's (%x) "
2186 "permissions\n",
2187 to_vm_id);
2188 return ffa_error(FFA_INVALID_PARAMETERS);
2189 }
2190
2191 return (struct ffa_value){.func = FFA_SUCCESS_32};
2192}
2193
J-Alvesa9cd7e32022-07-01 13:49:33 +01002194/*
2195 * According to section 16.4.3 of FF-A v1.1 EAC0 specification, the hypervisor
2196 * may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region description
2197 * of a pending memory sharing operation whose allocator is the SPM, for
2198 * validation purposes before forwarding an FFA_MEM_RECLAIM call. In doing so
2199 * the memory region descriptor of the retrieve request must be zeroed with the
2200 * exception of the sender ID and handle.
2201 */
2202bool is_ffa_memory_retrieve_borrower_request(struct ffa_memory_region *request,
2203 struct vm_locked to_locked)
2204{
2205 return to_locked.vm->id == HF_HYPERVISOR_VM_ID &&
2206 request->attributes == 0U && request->flags == 0U &&
2207 request->tag == 0U && request->receiver_count == 0U &&
2208 plat_ffa_memory_handle_allocated_by_current_world(
2209 request->handle);
2210}
2211
2212/*
2213 * Helper to reset count of fragments retrieved by the hypervisor.
2214 */
2215static void ffa_memory_retrieve_complete_from_hyp(
2216 struct ffa_memory_share_state *share_state)
2217{
2218 if (share_state->hypervisor_fragment_count ==
2219 share_state->fragment_count) {
2220 share_state->hypervisor_fragment_count = 0;
2221 }
2222}
2223
J-Alves089004f2022-07-13 14:25:44 +01002224/**
2225 * Validate that the memory region descriptor provided by the borrower on
2226 * FFA_MEM_RETRIEVE_REQ, against saved memory region provided by lender at the
2227 * memory sharing call.
2228 */
2229static struct ffa_value ffa_memory_retrieve_validate(
2230 ffa_vm_id_t receiver_id, struct ffa_memory_region *retrieve_request,
2231 struct ffa_memory_region *memory_region, uint32_t *receiver_index,
2232 uint32_t share_func)
2233{
2234 ffa_memory_region_flags_t transaction_type =
2235 retrieve_request->flags &
2236 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002237 enum ffa_memory_security security_state;
J-Alves089004f2022-07-13 14:25:44 +01002238
2239 assert(retrieve_request != NULL);
2240 assert(memory_region != NULL);
2241 assert(receiver_index != NULL);
2242 assert(retrieve_request->sender == memory_region->sender);
2243
2244 /*
2245 * Check that the transaction type expected by the receiver is
2246 * correct, if it has been specified.
2247 */
2248 if (transaction_type !=
2249 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
2250 transaction_type != (memory_region->flags &
2251 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
2252 dlog_verbose(
2253 "Incorrect transaction type %#x for "
2254 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
2255 transaction_type,
2256 memory_region->flags &
2257 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
2258 retrieve_request->handle);
2259 return ffa_error(FFA_INVALID_PARAMETERS);
2260 }
2261
2262 if (retrieve_request->tag != memory_region->tag) {
2263 dlog_verbose(
2264 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
2265 "%d for handle %#x.\n",
2266 retrieve_request->tag, memory_region->tag,
2267 retrieve_request->handle);
2268 return ffa_error(FFA_INVALID_PARAMETERS);
2269 }
2270
2271 *receiver_index =
2272 ffa_memory_region_get_receiver(memory_region, receiver_id);
2273
2274 if (*receiver_index == memory_region->receiver_count) {
2275 dlog_verbose(
2276 "Incorrect receiver VM ID %d for "
2277 "FFA_MEM_RETRIEVE_REQ, for handle %#x.\n",
J-Alves59ed0042022-07-28 18:26:41 +01002278 receiver_id, memory_region->handle);
J-Alves089004f2022-07-13 14:25:44 +01002279 return ffa_error(FFA_INVALID_PARAMETERS);
2280 }
2281
2282 if ((retrieve_request->flags &
2283 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
2284 dlog_verbose(
2285 "Retriever specified 'address range alignment 'hint' "
2286 "not supported.\n");
2287 return ffa_error(FFA_INVALID_PARAMETERS);
2288 }
2289 if ((retrieve_request->flags &
2290 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
2291 dlog_verbose(
2292 "Bits 8-5 must be zero in memory region's flags "
2293 "(address range alignment hint not supported).\n");
2294 return ffa_error(FFA_INVALID_PARAMETERS);
2295 }
2296
2297 if ((retrieve_request->flags & ~0x7FF) != 0U) {
2298 dlog_verbose(
2299 "Bits 31-10 must be zero in memory region's flags.\n");
2300 return ffa_error(FFA_INVALID_PARAMETERS);
2301 }
2302
2303 if (share_func == FFA_MEM_SHARE_32 &&
2304 (retrieve_request->flags &
2305 (FFA_MEMORY_REGION_FLAG_CLEAR |
2306 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
2307 dlog_verbose(
2308 "Memory Share operation can't clean after relinquish "
2309 "memory region.\n");
2310 return ffa_error(FFA_INVALID_PARAMETERS);
2311 }
2312
2313 /*
2314 * If the borrower needs the memory to be cleared before mapping
2315 * to its address space, the sender should have set the flag
2316 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
2317 * FFA_DENIED.
2318 */
2319 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
2320 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
2321 dlog_verbose(
2322 "Borrower needs memory cleared. Sender needs to set "
2323 "flag for clearing memory.\n");
2324 return ffa_error(FFA_DENIED);
2325 }
2326
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002327 /* Memory region attributes NS-Bit MBZ for FFA_MEM_RETRIEVE_REQ. */
2328 security_state =
2329 ffa_get_memory_security_attr(retrieve_request->attributes);
2330 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
2331 dlog_verbose(
2332 "Invalid security state for memory retrieve request "
2333 "operation.\n");
2334 return ffa_error(FFA_INVALID_PARAMETERS);
2335 }
2336
J-Alves089004f2022-07-13 14:25:44 +01002337 /*
2338 * If memory type is not specified, bypass validation of memory
2339 * attributes in the retrieve request. The retriever is expecting to
2340 * obtain this information from the SPMC.
2341 */
2342 if (ffa_get_memory_type_attr(retrieve_request->attributes) ==
2343 FFA_MEMORY_NOT_SPECIFIED_MEM) {
2344 return (struct ffa_value){.func = FFA_SUCCESS_32};
2345 }
2346
2347 /*
2348 * Ensure receiver's attributes are compatible with how
2349 * Hafnium maps memory: Normal Memory, Inner shareable,
2350 * Write-Back Read-Allocate Write-Allocate Cacheable.
2351 */
2352 return ffa_memory_attributes_validate(retrieve_request->attributes);
2353}
2354
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002355struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2356 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002357 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002358 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002359{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002360 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002361 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002362 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002363 sizeof(struct ffa_memory_access);
2364 ffa_memory_handle_t handle = retrieve_request->handle;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002365 struct ffa_memory_region *memory_region;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002366 ffa_memory_access_permissions_t permissions = 0;
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002367 uint32_t memory_to_mode;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002368 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002369 struct ffa_memory_share_state *share_state;
2370 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002371 struct ffa_composite_memory_region *composite;
2372 uint32_t total_length;
2373 uint32_t fragment_length;
J-Alves089004f2022-07-13 14:25:44 +01002374 ffa_vm_id_t receiver_id = to_locked.vm->id;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002375 bool is_send_complete = false;
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002376 ffa_memory_attributes_t attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002377
2378 dump_share_states();
2379
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002380 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002381 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002382 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002383 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002384 expected_retrieve_request_length,
2385 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002386 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002387 }
2388
2389 share_states = share_states_lock();
2390 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002391 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002392 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002393 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002394 goto out;
2395 }
2396
J-Alves96de29f2022-04-26 16:05:24 +01002397 if (!share_state->sending_complete) {
2398 dlog_verbose(
2399 "Memory with handle %#x not fully sent, can't "
2400 "retrieve.\n",
2401 handle);
2402 ret = ffa_error(FFA_INVALID_PARAMETERS);
2403 goto out;
2404 }
2405
Andrew Walbrana65a1322020-04-06 19:32:32 +01002406 memory_region = share_state->memory_region;
J-Alves089004f2022-07-13 14:25:44 +01002407
Andrew Walbrana65a1322020-04-06 19:32:32 +01002408 CHECK(memory_region != NULL);
2409
J-Alves089004f2022-07-13 14:25:44 +01002410 if (retrieve_request->sender != memory_region->sender) {
2411 dlog_verbose(
2412 "Memory with handle %#x not fully sent, can't "
2413 "retrieve.\n",
2414 handle);
2415 ret = ffa_error(FFA_INVALID_PARAMETERS);
2416 goto out;
2417 }
J-Alves96de29f2022-04-26 16:05:24 +01002418
J-Alvesa9cd7e32022-07-01 13:49:33 +01002419 if (!is_ffa_memory_retrieve_borrower_request(retrieve_request,
2420 to_locked)) {
2421 uint32_t receiver_index;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002422
J-Alvesb5084cf2022-07-06 14:20:12 +01002423 /*
2424 * The SPMC can only process retrieve requests to memory share
2425 * operations with one borrower from the other world. It can't
2426 * determine the ID of the NWd VM that invoked the retrieve
2427 * request interface call. It relies on the hypervisor to
2428 * validate the caller's ID against that provided in the
2429 * `receivers` list of the retrieve response.
2430 * In case there is only one borrower from the NWd in the
2431 * transaction descriptor, record that in the `receiver_id` for
2432 * later use, and validate in the retrieve request message.
2433 */
2434 if (to_locked.vm->id == HF_HYPERVISOR_VM_ID) {
2435 uint32_t other_world_count = 0;
2436
2437 for (uint32_t i = 0; i < memory_region->receiver_count;
2438 i++) {
2439 receiver_id =
2440 retrieve_request->receivers[0]
2441 .receiver_permissions.receiver;
2442 if (!vm_id_is_current_world(receiver_id)) {
2443 other_world_count++;
2444 }
2445 }
2446 if (other_world_count > 1) {
2447 dlog_verbose(
2448 "Support one receiver from the other "
2449 "world.\n");
2450 return ffa_error(FFA_NOT_SUPPORTED);
2451 }
2452 }
2453
2454 /*
2455 * Validate retrieve request, according to what was sent by the
2456 * sender. Function will output the `receiver_index` from the
2457 * provided memory region, and will output `permissions` from
2458 * the validated requested permissions.
2459 */
J-Alves089004f2022-07-13 14:25:44 +01002460 ret = ffa_memory_retrieve_validate(
2461 receiver_id, retrieve_request, memory_region,
2462 &receiver_index, share_state->share_func);
2463 if (ret.func != FFA_SUCCESS_32) {
J-Alvesa9cd7e32022-07-01 13:49:33 +01002464 goto out;
2465 }
2466
2467 if (share_state->retrieved_fragment_count[receiver_index] !=
2468 0U) {
2469 dlog_verbose(
2470 "Memory with handle %#x already retrieved.\n",
2471 handle);
2472 ret = ffa_error(FFA_DENIED);
2473 goto out;
2474 }
2475
J-Alvesa9cd7e32022-07-01 13:49:33 +01002476 ret = ffa_memory_retrieve_validate_memory_access_list(
2477 memory_region, retrieve_request, receiver_id,
2478 &permissions);
J-Alves614d9f42022-06-28 14:03:10 +01002479 if (ret.func != FFA_SUCCESS_32) {
2480 goto out;
2481 }
Federico Recanatia98603a2021-12-20 18:04:03 +01002482
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002483 memory_to_mode = ffa_memory_permissions_to_mode(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002484 permissions, share_state->sender_orig_mode);
J-Alves40e260e2022-09-22 17:52:43 +01002485
J-Alvesa9cd7e32022-07-01 13:49:33 +01002486 ret = ffa_retrieve_check_update(
2487 to_locked, memory_region->sender,
2488 share_state->fragments,
2489 share_state->fragment_constituent_counts,
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002490 share_state->fragment_count, memory_to_mode,
J-Alvesa9cd7e32022-07-01 13:49:33 +01002491 share_state->share_func, false, page_pool);
2492
2493 if (ret.func != FFA_SUCCESS_32) {
2494 goto out;
2495 }
2496
2497 share_state->retrieved_fragment_count[receiver_index] = 1;
2498 is_send_complete =
2499 share_state->retrieved_fragment_count[receiver_index] ==
2500 share_state->fragment_count;
J-Alves3c5b2072022-11-21 12:45:40 +00002501
2502 share_state->clear_after_relinquish =
2503 (retrieve_request->flags &
2504 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH) != 0U;
2505
J-Alvesa9cd7e32022-07-01 13:49:33 +01002506 } else {
2507 if (share_state->hypervisor_fragment_count != 0U) {
2508 dlog_verbose(
J-Alvesb5084cf2022-07-06 14:20:12 +01002509 "Memory with handle %#x already retrieved by "
J-Alvesa9cd7e32022-07-01 13:49:33 +01002510 "the hypervisor.\n",
2511 handle);
2512 ret = ffa_error(FFA_DENIED);
2513 goto out;
2514 }
2515
2516 share_state->hypervisor_fragment_count = 1;
2517
2518 ffa_memory_retrieve_complete_from_hyp(share_state);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002519 }
2520
J-Alvesb5084cf2022-07-06 14:20:12 +01002521 /* VMs acquire the RX buffer from SPMC. */
2522 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
2523
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002524 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002525 * Copy response to RX buffer of caller and deliver the message.
2526 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002527 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002528 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002529 composite = ffa_memory_region_get_composite(memory_region, 0);
2530 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002531 * Constituents which we received in the first fragment should
2532 * always fit in the first fragment we are sending, because the
2533 * header is the same size in both cases and we have a fixed
2534 * message buffer size. So `ffa_retrieved_memory_region_init`
2535 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01002536 */
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002537
2538 /*
2539 * Set the security state in the memory retrieve response attributes
2540 * if specified by the target mode.
2541 */
2542 attributes = plat_ffa_memory_security_mode(
2543 memory_region->attributes, share_state->sender_orig_mode);
2544
Andrew Walbranca808b12020-05-15 17:22:28 +01002545 CHECK(ffa_retrieved_memory_region_init(
J-Alves2d8457f2022-10-05 11:06:41 +01002546 to_locked.vm->mailbox.recv, to_locked.vm->ffa_version,
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002547 HF_MAILBOX_SIZE, memory_region->sender, attributes,
2548 memory_region->flags, handle, receiver_id, permissions,
2549 composite->page_count, composite->constituent_count,
2550 share_state->fragments[0],
Andrew Walbranca808b12020-05-15 17:22:28 +01002551 share_state->fragment_constituent_counts[0], &total_length,
2552 &fragment_length));
J-Alvesb5084cf2022-07-06 14:20:12 +01002553
Andrew Walbranca808b12020-05-15 17:22:28 +01002554 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002555 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002556 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00002557 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002558
J-Alvesa9cd7e32022-07-01 13:49:33 +01002559 if (is_send_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002560 ffa_memory_retrieve_complete(share_states, share_state,
2561 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002562 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002563 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002564 .arg1 = total_length,
2565 .arg2 = fragment_length};
Andrew Walbranca808b12020-05-15 17:22:28 +01002566out:
2567 share_states_unlock(&share_states);
2568 dump_share_states();
2569 return ret;
2570}
2571
J-Alves5da37d92022-10-24 16:33:48 +01002572/**
2573 * Determine expected fragment offset according to the FF-A version of
2574 * the caller.
2575 */
2576static uint32_t ffa_memory_retrieve_expected_offset_per_ffa_version(
2577 struct ffa_memory_region *memory_region,
2578 uint32_t retrieved_constituents_count, uint32_t ffa_version)
2579{
2580 uint32_t expected_fragment_offset;
2581 uint32_t composite_constituents_offset;
2582
2583 if (ffa_version == MAKE_FFA_VERSION(1, 1)) {
2584 /*
2585 * Hafnium operates memory regions in FF-A v1.1 format, so we
2586 * can retrieve the constituents offset from descriptor.
2587 */
2588 composite_constituents_offset =
2589 ffa_composite_constituent_offset(memory_region, 0);
2590 } else if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
2591 /*
2592 * If retriever is FF-A v1.0, determine the composite offset
2593 * as it is expected to have been configured in the
2594 * retrieve response.
2595 */
2596 composite_constituents_offset =
2597 sizeof(struct ffa_memory_region_v1_0) +
2598 RECEIVERS_COUNT_IN_RETRIEVE_RESP *
2599 sizeof(struct ffa_memory_access) +
2600 sizeof(struct ffa_composite_memory_region);
2601 } else {
2602 panic("%s received an invalid FF-A version.\n", __func__);
2603 }
2604
2605 expected_fragment_offset =
2606 composite_constituents_offset +
2607 retrieved_constituents_count *
2608 sizeof(struct ffa_memory_region_constituent) -
2609 sizeof(struct ffa_memory_access) *
2610 (memory_region->receiver_count - 1);
2611
2612 return expected_fragment_offset;
2613}
2614
Andrew Walbranca808b12020-05-15 17:22:28 +01002615struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2616 ffa_memory_handle_t handle,
2617 uint32_t fragment_offset,
J-Alves59ed0042022-07-28 18:26:41 +01002618 ffa_vm_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01002619 struct mpool *page_pool)
2620{
2621 struct ffa_memory_region *memory_region;
2622 struct share_states_locked share_states;
2623 struct ffa_memory_share_state *share_state;
2624 struct ffa_value ret;
2625 uint32_t fragment_index;
2626 uint32_t retrieved_constituents_count;
2627 uint32_t i;
2628 uint32_t expected_fragment_offset;
2629 uint32_t remaining_constituent_count;
2630 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01002631 uint32_t receiver_index;
J-Alves59ed0042022-07-28 18:26:41 +01002632 bool continue_ffa_hyp_mem_retrieve_req;
Andrew Walbranca808b12020-05-15 17:22:28 +01002633
2634 dump_share_states();
2635
2636 share_states = share_states_lock();
2637 if (!get_share_state(share_states, handle, &share_state)) {
2638 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2639 handle);
2640 ret = ffa_error(FFA_INVALID_PARAMETERS);
2641 goto out;
2642 }
2643
2644 memory_region = share_state->memory_region;
2645 CHECK(memory_region != NULL);
2646
Andrew Walbranca808b12020-05-15 17:22:28 +01002647 if (!share_state->sending_complete) {
2648 dlog_verbose(
2649 "Memory with handle %#x not fully sent, can't "
2650 "retrieve.\n",
2651 handle);
2652 ret = ffa_error(FFA_INVALID_PARAMETERS);
2653 goto out;
2654 }
2655
J-Alves59ed0042022-07-28 18:26:41 +01002656 /*
2657 * If retrieve request from the hypervisor has been initiated in the
2658 * given share_state, continue it, else assume it is a continuation of
2659 * retrieve request from a NWd VM.
2660 */
2661 continue_ffa_hyp_mem_retrieve_req =
2662 (to_locked.vm->id == HF_HYPERVISOR_VM_ID) &&
2663 (share_state->hypervisor_fragment_count != 0U) &&
2664 plat_ffa_is_vm_id(sender_vm_id);
Andrew Walbranca808b12020-05-15 17:22:28 +01002665
J-Alves59ed0042022-07-28 18:26:41 +01002666 if (!continue_ffa_hyp_mem_retrieve_req) {
2667 receiver_index = ffa_memory_region_get_receiver(
2668 memory_region, to_locked.vm->id);
2669
2670 if (receiver_index == memory_region->receiver_count) {
2671 dlog_verbose(
2672 "Caller of FFA_MEM_FRAG_RX (%x) is not a "
2673 "borrower to memory sharing transaction (%x)\n",
2674 to_locked.vm->id, handle);
2675 ret = ffa_error(FFA_INVALID_PARAMETERS);
2676 goto out;
2677 }
2678
2679 if (share_state->retrieved_fragment_count[receiver_index] ==
2680 0 ||
2681 share_state->retrieved_fragment_count[receiver_index] >=
2682 share_state->fragment_count) {
2683 dlog_verbose(
2684 "Retrieval of memory with handle %#x not yet "
2685 "started or already completed (%d/%d fragments "
2686 "retrieved).\n",
2687 handle,
2688 share_state->retrieved_fragment_count
2689 [receiver_index],
2690 share_state->fragment_count);
2691 ret = ffa_error(FFA_INVALID_PARAMETERS);
2692 goto out;
2693 }
2694
2695 fragment_index =
2696 share_state->retrieved_fragment_count[receiver_index];
2697 } else {
2698 if (share_state->hypervisor_fragment_count == 0 ||
2699 share_state->hypervisor_fragment_count >=
2700 share_state->fragment_count) {
2701 dlog_verbose(
2702 "Retrieve of memory with handle %x not "
2703 "started from hypervisor.\n",
2704 handle);
2705 ret = ffa_error(FFA_INVALID_PARAMETERS);
2706 goto out;
2707 }
2708
2709 if (memory_region->sender != sender_vm_id) {
2710 dlog_verbose(
2711 "Sender ID (%x) is not as expected for memory "
2712 "handle %x\n",
2713 sender_vm_id, handle);
2714 ret = ffa_error(FFA_INVALID_PARAMETERS);
2715 goto out;
2716 }
2717
2718 fragment_index = share_state->hypervisor_fragment_count;
2719
2720 receiver_index = 0;
2721 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002722
2723 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002724 * Check that the given fragment offset is correct by counting
2725 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01002726 */
2727 retrieved_constituents_count = 0;
2728 for (i = 0; i < fragment_index; ++i) {
2729 retrieved_constituents_count +=
2730 share_state->fragment_constituent_counts[i];
2731 }
J-Alvesc7484f12022-05-13 12:41:14 +01002732
2733 CHECK(memory_region->receiver_count > 0);
2734
Andrew Walbranca808b12020-05-15 17:22:28 +01002735 expected_fragment_offset =
J-Alves5da37d92022-10-24 16:33:48 +01002736 ffa_memory_retrieve_expected_offset_per_ffa_version(
2737 memory_region, retrieved_constituents_count,
2738 to_locked.vm->ffa_version);
2739
Andrew Walbranca808b12020-05-15 17:22:28 +01002740 if (fragment_offset != expected_fragment_offset) {
2741 dlog_verbose("Fragment offset was %d but expected %d.\n",
2742 fragment_offset, expected_fragment_offset);
2743 ret = ffa_error(FFA_INVALID_PARAMETERS);
2744 goto out;
2745 }
2746
J-Alves59ed0042022-07-28 18:26:41 +01002747 /* VMs acquire the RX buffer from SPMC. */
2748 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
2749
Andrew Walbranca808b12020-05-15 17:22:28 +01002750 remaining_constituent_count = ffa_memory_fragment_init(
2751 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2752 share_state->fragments[fragment_index],
2753 share_state->fragment_constituent_counts[fragment_index],
2754 &fragment_length);
2755 CHECK(remaining_constituent_count == 0);
2756 to_locked.vm->mailbox.recv_size = fragment_length;
2757 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2758 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00002759 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbranca808b12020-05-15 17:22:28 +01002760
J-Alves59ed0042022-07-28 18:26:41 +01002761 if (!continue_ffa_hyp_mem_retrieve_req) {
2762 share_state->retrieved_fragment_count[receiver_index]++;
2763 if (share_state->retrieved_fragment_count[receiver_index] ==
2764 share_state->fragment_count) {
2765 ffa_memory_retrieve_complete(share_states, share_state,
2766 page_pool);
2767 }
2768 } else {
2769 share_state->hypervisor_fragment_count++;
2770
2771 ffa_memory_retrieve_complete_from_hyp(share_state);
2772 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002773 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2774 .arg1 = (uint32_t)handle,
2775 .arg2 = (uint32_t)(handle >> 32),
2776 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002777
2778out:
2779 share_states_unlock(&share_states);
2780 dump_share_states();
2781 return ret;
2782}
2783
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002784struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002785 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002786 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002787{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002788 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002789 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002790 struct ffa_memory_share_state *share_state;
2791 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002792 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002793 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01002794 uint32_t receiver_index;
J-Alves3c5b2072022-11-21 12:45:40 +00002795 bool receivers_relinquished_memory;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002796
Andrew Walbrana65a1322020-04-06 19:32:32 +01002797 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002798 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002799 "Stream endpoints not supported (got %d "
J-Alves668a86e2023-05-10 11:53:25 +01002800 "endpoints on FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002801 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002802 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002803 }
2804
Andrew Walbrana65a1322020-04-06 19:32:32 +01002805 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002806 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002807 "VM ID %d in relinquish message doesn't match "
J-Alves668a86e2023-05-10 11:53:25 +01002808 "calling VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002809 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002810 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002811 }
2812
2813 dump_share_states();
2814
2815 share_states = share_states_lock();
2816 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002817 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002818 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002819 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002820 goto out;
2821 }
2822
Andrew Walbranca808b12020-05-15 17:22:28 +01002823 if (!share_state->sending_complete) {
2824 dlog_verbose(
2825 "Memory with handle %#x not fully sent, can't "
2826 "relinquish.\n",
2827 handle);
2828 ret = ffa_error(FFA_INVALID_PARAMETERS);
2829 goto out;
2830 }
2831
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002832 memory_region = share_state->memory_region;
2833 CHECK(memory_region != NULL);
2834
J-Alves8eb19162022-04-28 10:56:48 +01002835 receiver_index = ffa_memory_region_get_receiver(memory_region,
2836 from_locked.vm->id);
2837
2838 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002839 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002840 "VM ID %d tried to relinquish memory region "
J-Alves668a86e2023-05-10 11:53:25 +01002841 "with handle %#x and it is not a valid borrower.\n",
J-Alves8eb19162022-04-28 10:56:48 +01002842 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002843 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002844 goto out;
2845 }
2846
J-Alves8eb19162022-04-28 10:56:48 +01002847 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01002848 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002849 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002850 "Memory with handle %#x not yet fully "
2851 "retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01002852 "receiver %x can't relinquish.\n",
2853 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002854 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002855 goto out;
2856 }
2857
J-Alves3c5b2072022-11-21 12:45:40 +00002858 /*
2859 * Either clear if requested in relinquish call, or in a retrieve
2860 * request from one of the borrowers.
2861 */
2862 receivers_relinquished_memory = true;
2863
2864 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
2865 struct ffa_memory_access *receiver =
2866 &memory_region->receivers[i];
2867
2868 if (receiver->receiver_permissions.receiver ==
2869 from_locked.vm->id) {
2870 continue;
2871 }
2872
2873 if (share_state->retrieved_fragment_count[i] != 0U) {
2874 receivers_relinquished_memory = false;
2875 break;
2876 }
2877 }
2878
2879 clear = receivers_relinquished_memory &&
2880 (share_state->clear_after_relinquish ||
2881 (relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2882 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002883
2884 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002885 * Clear is not allowed for memory that was shared, as the
2886 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002887 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002888 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002889 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002890 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002891 goto out;
2892 }
2893
Andrew Walbranca808b12020-05-15 17:22:28 +01002894 ret = ffa_relinquish_check_update(
J-Alves3c5b2072022-11-21 12:45:40 +00002895 from_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002896 share_state->fragment_constituent_counts,
2897 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002898
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002899 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002900 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002901 * Mark memory handle as not retrieved, so it can be
2902 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002903 */
J-Alves8eb19162022-04-28 10:56:48 +01002904 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002905 }
2906
2907out:
2908 share_states_unlock(&share_states);
2909 dump_share_states();
2910 return ret;
2911}
2912
2913/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01002914 * Validates that the reclaim transition is allowed for the given
2915 * handle, updates the page table of the reclaiming VM, and frees the
2916 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002917 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002918struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002919 ffa_memory_handle_t handle,
2920 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002921 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002922{
2923 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002924 struct ffa_memory_share_state *share_state;
2925 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002926 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002927
2928 dump_share_states();
2929
2930 share_states = share_states_lock();
J-Alvesb5084cf2022-07-06 14:20:12 +01002931 if (get_share_state(share_states, handle, &share_state)) {
2932 memory_region = share_state->memory_region;
2933 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002934 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002935 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002936 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002937 goto out;
2938 }
2939
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002940 CHECK(memory_region != NULL);
2941
J-Alvesa9cd7e32022-07-01 13:49:33 +01002942 if (vm_id_is_current_world(to_locked.vm->id) &&
2943 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002944 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002945 "VM %#x attempted to reclaim memory handle %#x "
2946 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002947 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002948 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002949 goto out;
2950 }
2951
Andrew Walbranca808b12020-05-15 17:22:28 +01002952 if (!share_state->sending_complete) {
2953 dlog_verbose(
2954 "Memory with handle %#x not fully sent, can't "
2955 "reclaim.\n",
2956 handle);
2957 ret = ffa_error(FFA_INVALID_PARAMETERS);
2958 goto out;
2959 }
2960
J-Alves752236c2022-04-28 11:07:47 +01002961 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
2962 if (share_state->retrieved_fragment_count[i] != 0) {
2963 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002964 "Tried to reclaim memory handle %#x "
J-Alves3c5b2072022-11-21 12:45:40 +00002965 "that has not been relinquished by all "
J-Alvesa9cd7e32022-07-01 13:49:33 +01002966 "borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01002967 handle,
2968 memory_region->receivers[i]
2969 .receiver_permissions.receiver);
2970 ret = ffa_error(FFA_DENIED);
2971 goto out;
2972 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002973 }
2974
Andrew Walbranca808b12020-05-15 17:22:28 +01002975 ret = ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00002976 to_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002977 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00002978 share_state->fragment_count, share_state->sender_orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01002979 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002980
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002981 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002982 share_state_free(share_states, share_state, page_pool);
J-Alves3c5b2072022-11-21 12:45:40 +00002983 dlog_verbose("Freed share state after successful reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002984 }
2985
2986out:
2987 share_states_unlock(&share_states);
2988 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002989}