blob: 958e6bd2ee7768a794001cf6e7ebf001b66f9f87 [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
355 return mode;
356}
357
Jose Marinho75509b42019-04-09 09:34:59 +0100358/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000359 * Get the current mode in the stage-2 page table of the given vm of all the
360 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100361 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100362 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100363static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000364 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100365 struct ffa_memory_region_constituent **fragments,
366 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100367{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100368 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100369 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100370
Andrew Walbranca808b12020-05-15 17:22:28 +0100371 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100372 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000373 * Fail if there are no constituents. Otherwise we would get an
374 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100375 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100376 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100377 }
378
Andrew Walbranca808b12020-05-15 17:22:28 +0100379 for (i = 0; i < fragment_count; ++i) {
380 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
381 ipaddr_t begin = ipa_init(fragments[i][j].address);
382 size_t size = fragments[i][j].page_count * PAGE_SIZE;
383 ipaddr_t end = ipa_add(begin, size);
384 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100385
Andrew Walbranca808b12020-05-15 17:22:28 +0100386 /* Fail if addresses are not page-aligned. */
387 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
388 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
389 return ffa_error(FFA_INVALID_PARAMETERS);
390 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100391
Andrew Walbranca808b12020-05-15 17:22:28 +0100392 /*
393 * Ensure that this constituent memory range is all
394 * mapped with the same mode.
395 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800396 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100397 return ffa_error(FFA_DENIED);
398 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100399
Andrew Walbranca808b12020-05-15 17:22:28 +0100400 /*
401 * Ensure that all constituents are mapped with the same
402 * mode.
403 */
404 if (i == 0) {
405 *orig_mode = current_mode;
406 } else if (current_mode != *orig_mode) {
407 dlog_verbose(
408 "Expected mode %#x but was %#x for %d "
409 "pages at %#x.\n",
410 *orig_mode, current_mode,
411 fragments[i][j].page_count,
412 ipa_addr(begin));
413 return ffa_error(FFA_DENIED);
414 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100415 }
Jose Marinho75509b42019-04-09 09:34:59 +0100416 }
417
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100418 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000419}
420
421/**
422 * Verify that all pages have the same mode, that the starting mode
423 * constitutes a valid state and obtain the next mode to apply
424 * to the sending VM.
425 *
426 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100427 * 1) FFA_DENIED if a state transition was not found;
428 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100429 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100430 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100431 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100432 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
433 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000434 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100435static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100436 struct vm_locked from, uint32_t share_func,
J-Alves363f5722022-04-25 17:37:37 +0100437 struct ffa_memory_access *receivers, uint32_t receivers_count,
438 uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100439 struct ffa_memory_region_constituent **fragments,
440 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
441 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000442{
443 const uint32_t state_mask =
444 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100445 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000446
Andrew Walbranca808b12020-05-15 17:22:28 +0100447 ret = constituents_get_mode(from, orig_from_mode, fragments,
448 fragment_constituent_counts,
449 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100450 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100451 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100452 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100453 }
454
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000455 /* Ensure the address range is normal memory and not a device. */
456 if (*orig_from_mode & MM_MODE_D) {
457 dlog_verbose("Can't share device memory (mode is %#x).\n",
458 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100459 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000460 }
461
462 /*
463 * Ensure the sender is the owner and has exclusive access to the
464 * memory.
465 */
466 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100467 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100468 }
469
J-Alves363f5722022-04-25 17:37:37 +0100470 assert(receivers != NULL && receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100471
J-Alves363f5722022-04-25 17:37:37 +0100472 for (uint32_t i = 0U; i < receivers_count; i++) {
473 ffa_memory_access_permissions_t permissions =
474 receivers[i].receiver_permissions.permissions;
475 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
476 permissions, *orig_from_mode);
477
478 if ((*orig_from_mode & required_from_mode) !=
479 required_from_mode) {
480 dlog_verbose(
481 "Sender tried to send memory with permissions "
482 "which "
483 "required mode %#x but only had %#x itself.\n",
484 required_from_mode, *orig_from_mode);
485 return ffa_error(FFA_DENIED);
486 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000487 }
488
489 /* Find the appropriate new mode. */
490 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000491 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100492 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000493 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100494 break;
495
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100496 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000497 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100498 break;
499
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100500 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000501 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100502 break;
503
Jose Marinho75509b42019-04-09 09:34:59 +0100504 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100505 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100506 }
507
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100508 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000509}
510
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100511static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000512 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100513 struct ffa_memory_region_constituent **fragments,
514 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
515 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000516{
517 const uint32_t state_mask =
518 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
519 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100520 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000521
Andrew Walbranca808b12020-05-15 17:22:28 +0100522 ret = constituents_get_mode(from, orig_from_mode, fragments,
523 fragment_constituent_counts,
524 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100525 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100526 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000527 }
528
529 /* Ensure the address range is normal memory and not a device. */
530 if (*orig_from_mode & MM_MODE_D) {
531 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
532 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100533 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000534 }
535
536 /*
537 * Ensure the relinquishing VM is not the owner but has access to the
538 * memory.
539 */
540 orig_from_state = *orig_from_mode & state_mask;
541 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
542 dlog_verbose(
543 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100544 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000545 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100546 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000547 }
548
549 /* Find the appropriate new mode. */
550 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
551
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100552 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000553}
554
555/**
556 * Verify that all pages have the same mode, that the starting mode
557 * constitutes a valid state and obtain the next mode to apply
558 * to the retrieving VM.
559 *
560 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100561 * 1) FFA_DENIED if a state transition was not found;
562 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100563 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100564 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100565 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100566 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
567 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000568 */
J-Alvesfc19b372022-07-06 12:17:35 +0100569struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000570 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100571 struct ffa_memory_region_constituent **fragments,
572 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
573 uint32_t memory_to_attributes, uint32_t *to_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000574{
575 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100576 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000577
Andrew Walbranca808b12020-05-15 17:22:28 +0100578 ret = constituents_get_mode(to, &orig_to_mode, fragments,
579 fragment_constituent_counts,
580 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100581 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100582 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100583 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000584 }
585
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100586 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000587 /*
588 * If the original ffa memory send call has been processed
589 * successfully, it is expected the orig_to_mode would overlay
590 * with `state_mask`, as a result of the function
591 * `ffa_send_check_transition`.
592 */
J-Alves59ed0042022-07-28 18:26:41 +0100593 if (vm_id_is_current_world(to.vm->id)) {
594 assert((orig_to_mode &
595 (MM_MODE_INVALID | MM_MODE_UNOWNED |
596 MM_MODE_SHARED)) != 0U);
597 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000598 } else {
599 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +0100600 * If the retriever is from virtual FF-A instance:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000601 * Ensure the retriever has the expected state. We don't care
602 * about the MM_MODE_SHARED bit; either with or without it set
603 * are both valid representations of the !O-NA state.
604 */
J-Alvesa9cd7e32022-07-01 13:49:33 +0100605 if (vm_id_is_current_world(to.vm->id) &&
606 to.vm->id != HF_PRIMARY_VM_ID &&
607 (orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
608 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100609 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000610 }
611 }
612
613 /* Find the appropriate new mode. */
614 *to_mode = memory_to_attributes;
615 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100616 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000617 *to_mode |= 0;
618 break;
619
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100620 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000621 *to_mode |= MM_MODE_UNOWNED;
622 break;
623
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100624 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000625 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
626 break;
627
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100628 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000629 *to_mode |= 0;
630 break;
631
632 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100633 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100634 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000635 }
636
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100637 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100638}
Jose Marinho09b1db82019-08-08 09:16:59 +0100639
640/**
641 * Updates a VM's page table such that the given set of physical address ranges
642 * are mapped in the address space at the corresponding address ranges, in the
643 * mode provided.
644 *
645 * If commit is false, the page tables will be allocated from the mpool but no
646 * mappings will actually be updated. This function must always be called first
647 * with commit false to check that it will succeed before calling with commit
648 * true, to avoid leaving the page table in a half-updated state. To make a
649 * series of changes atomically you can call them all with commit false before
650 * calling them all with commit true.
651 *
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700652 * vm_ptable_defrag should always be called after a series of page table
653 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +0100654 *
655 * Returns true on success, or false if the update failed and no changes were
656 * made to memory mappings.
657 */
J-Alves66652252022-07-06 09:49:51 +0100658bool ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000659 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100660 struct ffa_memory_region_constituent **fragments,
661 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby4dd3f532021-09-21 09:57:08 +0100662 uint32_t mode, struct mpool *ppool, bool commit)
Jose Marinho09b1db82019-08-08 09:16:59 +0100663{
Andrew Walbranca808b12020-05-15 17:22:28 +0100664 uint32_t i;
665 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100666
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700667 if (vm_locked.vm->el0_partition) {
668 mode |= MM_MODE_USER | MM_MODE_NG;
669 }
670
Andrew Walbranca808b12020-05-15 17:22:28 +0100671 /* Iterate over the memory region constituents within each fragment. */
672 for (i = 0; i < fragment_count; ++i) {
673 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
674 size_t size = fragments[i][j].page_count * PAGE_SIZE;
675 paddr_t pa_begin =
676 pa_from_ipa(ipa_init(fragments[i][j].address));
677 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +0200678 uint32_t pa_bits =
679 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +0100680
681 /*
682 * Ensure the requested region falls into system's PA
683 * range.
684 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +0200685 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
686 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +0100687 dlog_error("Region is outside of PA Range\n");
688 return false;
689 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100690
691 if (commit) {
692 vm_identity_commit(vm_locked, pa_begin, pa_end,
693 mode, ppool, NULL);
694 } else if (!vm_identity_prepare(vm_locked, pa_begin,
695 pa_end, mode, ppool)) {
696 return false;
697 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100698 }
699 }
700
701 return true;
702}
703
704/**
705 * Clears a region of physical memory by overwriting it with zeros. The data is
706 * flushed from the cache so the memory has been cleared across the system.
707 */
J-Alves7db32002021-12-14 14:44:50 +0000708static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
709 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +0100710{
711 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000712 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100713 * global mapping of the whole range. Such an approach will limit
714 * the changes to stage-1 tables and will allow only local
715 * invalidation.
716 */
717 bool ret;
718 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +0000719 void *ptr = mm_identity_map(stage1_locked, begin, end,
720 MM_MODE_W | (extra_mode_attributes &
721 plat_ffa_other_world_mode()),
722 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100723 size_t size = pa_difference(begin, end);
724
725 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100726 goto fail;
727 }
728
729 memset_s(ptr, size, 0, size);
730 arch_mm_flush_dcache(ptr, size);
731 mm_unmap(stage1_locked, begin, end, ppool);
732
733 ret = true;
734 goto out;
735
736fail:
737 ret = false;
738
739out:
740 mm_unlock_stage1(&stage1_locked);
741
742 return ret;
743}
744
745/**
746 * Clears a region of physical memory by overwriting it with zeros. The data is
747 * flushed from the cache so the memory has been cleared across the system.
748 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100749static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +0000750 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100751 struct ffa_memory_region_constituent **fragments,
752 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
753 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100754{
755 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +0100756 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100757 bool ret = false;
758
759 /*
760 * Create a local pool so any freed memory can't be used by another
761 * thread. This is to ensure each constituent that is mapped can be
762 * unmapped again afterwards.
763 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000764 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100765
Andrew Walbranca808b12020-05-15 17:22:28 +0100766 /* Iterate over the memory region constituents within each fragment. */
767 for (i = 0; i < fragment_count; ++i) {
768 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100769
Andrew Walbranca808b12020-05-15 17:22:28 +0100770 for (j = 0; j < fragment_constituent_counts[j]; ++j) {
771 size_t size = fragments[i][j].page_count * PAGE_SIZE;
772 paddr_t begin =
773 pa_from_ipa(ipa_init(fragments[i][j].address));
774 paddr_t end = pa_add(begin, size);
775
J-Alves7db32002021-12-14 14:44:50 +0000776 if (!clear_memory(begin, end, &local_page_pool,
777 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100778 /*
779 * api_clear_memory will defrag on failure, so
780 * no need to do it here.
781 */
782 goto out;
783 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100784 }
785 }
786
Jose Marinho09b1db82019-08-08 09:16:59 +0100787 ret = true;
788
789out:
790 mpool_fini(&local_page_pool);
791 return ret;
792}
793
J-Alves5952d942022-12-22 16:03:00 +0000794static bool is_memory_range_within(ipaddr_t begin, ipaddr_t end,
795 ipaddr_t in_begin, ipaddr_t in_end)
796{
797 return (ipa_addr(begin) >= ipa_addr(in_begin) &&
798 ipa_addr(begin) < ipa_addr(in_end)) ||
799 (ipa_addr(end) <= ipa_addr(in_end) &&
800 ipa_addr(end) > ipa_addr(in_begin));
801}
802
803/**
804 * Receives a memory range and looks for overlaps with the remainder
805 * constituents of the memory share/lend/donate operation. Assumes they are
806 * passed in order to avoid having to loop over all the elements at each call.
807 * The function only compares the received memory ranges with those that follow
808 * within the same fragment, and subsequent fragments from the same operation.
809 */
810static bool ffa_memory_check_overlap(
811 struct ffa_memory_region_constituent **fragments,
812 const uint32_t *fragment_constituent_counts,
813 const uint32_t fragment_count, const uint32_t current_fragment,
814 const uint32_t current_constituent)
815{
816 uint32_t i = current_fragment;
817 uint32_t j = current_constituent;
818 ipaddr_t current_begin = ipa_init(fragments[i][j].address);
819 const uint32_t current_page_count = fragments[i][j].page_count;
820 size_t current_size = current_page_count * PAGE_SIZE;
821 ipaddr_t current_end = ipa_add(current_begin, current_size - 1);
822
823 if (current_size == 0 ||
824 current_size > UINT64_MAX - ipa_addr(current_begin)) {
825 dlog_verbose("Invalid page count. Addr: %x page_count: %x\n",
826 current_begin, current_page_count);
827 return false;
828 }
829
830 for (; i < fragment_count; i++) {
831 j = (i == current_fragment) ? j + 1 : 0;
832
833 for (; j < fragment_constituent_counts[i]; j++) {
834 ipaddr_t begin = ipa_init(fragments[i][j].address);
835 const uint32_t page_count = fragments[i][j].page_count;
836 size_t size = page_count * PAGE_SIZE;
837 ipaddr_t end = ipa_add(begin, size - 1);
838
839 if (size == 0 || size > UINT64_MAX - ipa_addr(begin)) {
840 dlog_verbose(
841 "Invalid page count. Addr: %x "
842 "page_count: %x\n",
843 begin, page_count);
844 return false;
845 }
846
847 /*
848 * Check if current ranges is within begin and end, as
849 * well as the reverse. This should help optimize the
850 * loop, and reduce the number of iterations.
851 */
852 if (is_memory_range_within(begin, end, current_begin,
853 current_end) ||
854 is_memory_range_within(current_begin, current_end,
855 begin, end)) {
856 dlog_verbose(
857 "Overlapping memory ranges: %#x - %#x "
858 "with %#x - %#x\n",
859 ipa_addr(begin), ipa_addr(end),
860 ipa_addr(current_begin),
861 ipa_addr(current_end));
862 return true;
863 }
864 }
865 }
866
867 return false;
868}
869
Jose Marinho09b1db82019-08-08 09:16:59 +0100870/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000871 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +0100872 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000873 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +0100874 *
875 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000876 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100877 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +0100878 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100879 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
880 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100881 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +0100882 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100883 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +0100884 */
J-Alves66652252022-07-06 09:49:51 +0100885struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000886 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100887 struct ffa_memory_region_constituent **fragments,
888 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves8f11cde2022-12-21 16:18:22 +0000889 uint32_t composite_total_page_count, uint32_t share_func,
890 struct ffa_memory_access *receivers, uint32_t receivers_count,
891 struct mpool *page_pool, bool clear, uint32_t *orig_from_mode_ret)
Jose Marinho09b1db82019-08-08 09:16:59 +0100892{
Andrew Walbranca808b12020-05-15 17:22:28 +0100893 uint32_t i;
J-Alves8f11cde2022-12-21 16:18:22 +0000894 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100895 uint32_t orig_from_mode;
896 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +0100897 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100898 struct ffa_value ret;
J-Alves8f11cde2022-12-21 16:18:22 +0000899 uint32_t constituents_total_page_count = 0;
Jose Marinho09b1db82019-08-08 09:16:59 +0100900
901 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +0100902 * Make sure constituents are properly aligned to a 64-bit boundary. If
903 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100904 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100905 for (i = 0; i < fragment_count; ++i) {
906 if (!is_aligned(fragments[i], 8)) {
907 dlog_verbose("Constituents not aligned.\n");
908 return ffa_error(FFA_INVALID_PARAMETERS);
909 }
J-Alves8f11cde2022-12-21 16:18:22 +0000910 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
911 constituents_total_page_count +=
912 fragments[i][j].page_count;
J-Alves5952d942022-12-22 16:03:00 +0000913 if (ffa_memory_check_overlap(
914 fragments, fragment_constituent_counts,
915 fragment_count, i, j)) {
916 return ffa_error(FFA_INVALID_PARAMETERS);
917 }
J-Alves8f11cde2022-12-21 16:18:22 +0000918 }
919 }
920
921 if (constituents_total_page_count != composite_total_page_count) {
922 dlog_verbose(
923 "Composite page count differs from calculated page "
924 "count from constituents.\n");
925 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +0100926 }
927
928 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000929 * Check if the state transition is lawful for the sender, ensure that
930 * all constituents of a memory region being shared are at the same
931 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +0100932 */
J-Alves363f5722022-04-25 17:37:37 +0100933 ret = ffa_send_check_transition(from_locked, share_func, receivers,
934 receivers_count, &orig_from_mode,
935 fragments, fragment_constituent_counts,
Andrew Walbranca808b12020-05-15 17:22:28 +0100936 fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100937 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100938 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100939 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100940 }
941
Andrew Walbran37c574e2020-06-03 11:45:46 +0100942 if (orig_from_mode_ret != NULL) {
943 *orig_from_mode_ret = orig_from_mode;
944 }
945
Jose Marinho09b1db82019-08-08 09:16:59 +0100946 /*
947 * Create a local pool so any freed memory can't be used by another
948 * thread. This is to ensure the original mapping can be restored if the
949 * clear fails.
950 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000951 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100952
953 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000954 * First reserve all required memory for the new page table entries
955 * without committing, to make sure the entire operation will succeed
956 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +0100957 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100958 if (!ffa_region_group_identity_map(
959 from_locked, fragments, fragment_constituent_counts,
960 fragment_count, from_mode, page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100961 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100962 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100963 goto out;
964 }
965
966 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000967 * Update the mapping for the sender. This won't allocate because the
968 * transaction was already prepared above, but may free pages in the
969 * case that a whole block is being unmapped that was previously
970 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +0100971 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100972 CHECK(ffa_region_group_identity_map(
973 from_locked, fragments, fragment_constituent_counts,
974 fragment_count, from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100975
976 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +0000977 if (clear &&
978 !ffa_clear_memory_constituents(
979 plat_ffa_owner_world_mode(from_locked.vm->id), fragments,
980 fragment_constituent_counts, fragment_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100981 /*
982 * On failure, roll back by returning memory to the sender. This
983 * may allocate pages which were previously freed into
984 * `local_page_pool` by the call above, but will never allocate
985 * more pages than that so can never fail.
986 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100987 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +0100988 from_locked, fragments, fragment_constituent_counts,
989 fragment_count, orig_from_mode, &local_page_pool,
990 true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100991
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100992 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100993 goto out;
994 }
995
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100996 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000997
998out:
999 mpool_fini(&local_page_pool);
1000
1001 /*
1002 * Tidy up the page table by reclaiming failed mappings (if there was an
1003 * error) or merging entries into blocks where possible (on success).
1004 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001005 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001006
1007 return ret;
1008}
1009
1010/**
1011 * Validates and maps memory shared from one VM to another.
1012 *
1013 * This function requires the calling context to hold the <to> lock.
1014 *
1015 * Returns:
1016 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001017 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001018 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001019 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001020 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001021 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001022 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001023struct ffa_value ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00001024 struct vm_locked to_locked, ffa_vm_id_t from_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01001025 struct ffa_memory_region_constituent **fragments,
1026 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1027 uint32_t memory_to_attributes, uint32_t share_func, bool clear,
1028 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001029{
Andrew Walbranca808b12020-05-15 17:22:28 +01001030 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001031 uint32_t to_mode;
1032 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001033 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001034
1035 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001036 * Make sure constituents are properly aligned to a 64-bit boundary. If
1037 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001038 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001039 for (i = 0; i < fragment_count; ++i) {
1040 if (!is_aligned(fragments[i], 8)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001041 dlog_verbose("Fragment not properly aligned.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +01001042 return ffa_error(FFA_INVALID_PARAMETERS);
1043 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001044 }
1045
1046 /*
1047 * Check if the state transition is lawful for the recipient, and ensure
1048 * that all constituents of the memory region being retrieved are at the
1049 * same state.
1050 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001051 ret = ffa_retrieve_check_transition(
1052 to_locked, share_func, fragments, fragment_constituent_counts,
1053 fragment_count, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001054 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001055 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001056 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001057 }
1058
1059 /*
1060 * Create a local pool so any freed memory can't be used by another
1061 * thread. This is to ensure the original mapping can be restored if the
1062 * clear fails.
1063 */
1064 mpool_init_with_fallback(&local_page_pool, page_pool);
1065
1066 /*
1067 * First reserve all required memory for the new page table entries in
1068 * the recipient page tables without committing, to make sure the entire
1069 * operation will succeed without exhausting the page pool.
1070 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001071 if (!ffa_region_group_identity_map(
1072 to_locked, fragments, fragment_constituent_counts,
1073 fragment_count, to_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001074 /* TODO: partial defrag of failed range. */
1075 dlog_verbose(
1076 "Insufficient memory to update recipient page "
1077 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001078 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001079 goto out;
1080 }
1081
1082 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001083 if (clear &&
1084 !ffa_clear_memory_constituents(
1085 plat_ffa_owner_world_mode(from_id), fragments,
1086 fragment_constituent_counts, fragment_count, page_pool)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001087 dlog_verbose("Couldn't clear constituents.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001088 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001089 goto out;
1090 }
1091
Jose Marinho09b1db82019-08-08 09:16:59 +01001092 /*
1093 * Complete the transfer by mapping the memory into the recipient. This
1094 * won't allocate because the transaction was already prepared above, so
1095 * it doesn't need to use the `local_page_pool`.
1096 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001097 CHECK(ffa_region_group_identity_map(
1098 to_locked, fragments, fragment_constituent_counts,
1099 fragment_count, to_mode, page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001100
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001101 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001102
1103out:
1104 mpool_fini(&local_page_pool);
1105
1106 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001107 * Tidy up the page table by reclaiming failed mappings (if there was an
1108 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001109 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001110 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001111
1112 return ret;
1113}
1114
Andrew Walbran996d1d12020-05-27 14:08:43 +01001115static struct ffa_value ffa_relinquish_check_update(
J-Alves3c5b2072022-11-21 12:45:40 +00001116 struct vm_locked from_locked, ffa_vm_id_t owner_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01001117 struct ffa_memory_region_constituent **fragments,
1118 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1119 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001120{
1121 uint32_t orig_from_mode;
1122 uint32_t from_mode;
1123 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001124 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001125
Andrew Walbranca808b12020-05-15 17:22:28 +01001126 ret = ffa_relinquish_check_transition(
1127 from_locked, &orig_from_mode, fragments,
1128 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001129 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001130 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001131 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001132 }
1133
1134 /*
1135 * Create a local pool so any freed memory can't be used by another
1136 * thread. This is to ensure the original mapping can be restored if the
1137 * clear fails.
1138 */
1139 mpool_init_with_fallback(&local_page_pool, page_pool);
1140
1141 /*
1142 * First reserve all required memory for the new page table entries
1143 * without committing, to make sure the entire operation will succeed
1144 * without exhausting the page pool.
1145 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001146 if (!ffa_region_group_identity_map(
1147 from_locked, fragments, fragment_constituent_counts,
1148 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001149 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001150 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001151 goto out;
1152 }
1153
1154 /*
1155 * Update the mapping for the sender. This won't allocate because the
1156 * transaction was already prepared above, but may free pages in the
1157 * case that a whole block is being unmapped that was previously
1158 * partially mapped.
1159 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001160 CHECK(ffa_region_group_identity_map(
1161 from_locked, fragments, fragment_constituent_counts,
1162 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001163
1164 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001165 if (clear &&
1166 !ffa_clear_memory_constituents(
J-Alves3c5b2072022-11-21 12:45:40 +00001167 plat_ffa_owner_world_mode(owner_id), fragments,
J-Alves7db32002021-12-14 14:44:50 +00001168 fragment_constituent_counts, fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001169 /*
1170 * On failure, roll back by returning memory to the sender. This
1171 * may allocate pages which were previously freed into
1172 * `local_page_pool` by the call above, but will never allocate
1173 * more pages than that so can never fail.
1174 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001175 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001176 from_locked, fragments, fragment_constituent_counts,
1177 fragment_count, orig_from_mode, &local_page_pool,
1178 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001179
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001180 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001181 goto out;
1182 }
1183
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001184 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001185
1186out:
1187 mpool_fini(&local_page_pool);
1188
1189 /*
1190 * Tidy up the page table by reclaiming failed mappings (if there was an
1191 * error) or merging entries into blocks where possible (on success).
1192 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001193 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001194
1195 return ret;
1196}
1197
1198/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001199 * Complete a memory sending operation by checking that it is valid, updating
1200 * the sender page table, and then either marking the share state as having
1201 * completed sending (on success) or freeing it (on failure).
1202 *
1203 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1204 */
J-Alvesfdd29272022-07-19 13:16:31 +01001205struct ffa_value ffa_memory_send_complete(
Andrew Walbranca808b12020-05-15 17:22:28 +01001206 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001207 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1208 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001209{
1210 struct ffa_memory_region *memory_region = share_state->memory_region;
J-Alves8f11cde2022-12-21 16:18:22 +00001211 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01001212 struct ffa_value ret;
1213
1214 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001215 assert(share_states.share_states != NULL);
J-Alves8f11cde2022-12-21 16:18:22 +00001216 assert(memory_region != NULL);
1217 composite = ffa_memory_region_get_composite(memory_region, 0);
1218 assert(composite != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001219
1220 /* Check that state is valid in sender page table and update. */
1221 ret = ffa_send_check_update(
1222 from_locked, share_state->fragments,
1223 share_state->fragment_constituent_counts,
J-Alves8f11cde2022-12-21 16:18:22 +00001224 share_state->fragment_count, composite->page_count,
1225 share_state->share_func, memory_region->receivers,
1226 memory_region->receiver_count, page_pool,
1227 memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001228 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001229 if (ret.func != FFA_SUCCESS_32) {
1230 /*
1231 * Free share state, it failed to send so it can't be retrieved.
1232 */
1233 dlog_verbose("Complete failed, freeing share state.\n");
1234 share_state_free(share_states, share_state, page_pool);
1235 return ret;
1236 }
1237
1238 share_state->sending_complete = true;
1239 dlog_verbose("Marked sending complete.\n");
1240
J-Alvesee68c542020-10-29 17:48:20 +00001241 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001242}
1243
1244/**
Federico Recanatia98603a2021-12-20 18:04:03 +01001245 * Check that the memory attributes match Hafnium expectations:
1246 * Normal Memory, Inner shareable, Write-Back Read-Allocate
1247 * Write-Allocate Cacheable.
1248 */
1249static struct ffa_value ffa_memory_attributes_validate(
J-Alves7a99d0d2023-02-08 13:49:48 +00001250 ffa_memory_attributes_t attributes)
Federico Recanatia98603a2021-12-20 18:04:03 +01001251{
1252 enum ffa_memory_type memory_type;
1253 enum ffa_memory_cacheability cacheability;
1254 enum ffa_memory_shareability shareability;
1255
1256 memory_type = ffa_get_memory_type_attr(attributes);
1257 if (memory_type != FFA_MEMORY_NORMAL_MEM) {
1258 dlog_verbose("Invalid memory type %#x, expected %#x.\n",
1259 memory_type, FFA_MEMORY_NORMAL_MEM);
Federico Recanati3d953f32022-02-17 09:31:29 +01001260 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001261 }
1262
1263 cacheability = ffa_get_memory_cacheability_attr(attributes);
1264 if (cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1265 dlog_verbose("Invalid cacheability %#x, expected %#x.\n",
1266 cacheability, FFA_MEMORY_CACHE_WRITE_BACK);
Federico Recanati3d953f32022-02-17 09:31:29 +01001267 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001268 }
1269
1270 shareability = ffa_get_memory_shareability_attr(attributes);
1271 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
1272 dlog_verbose("Invalid shareability %#x, expected #%x.\n",
1273 shareability, FFA_MEMORY_INNER_SHAREABLE);
Federico Recanati3d953f32022-02-17 09:31:29 +01001274 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001275 }
1276
1277 return (struct ffa_value){.func = FFA_SUCCESS_32};
1278}
1279
1280/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001281 * Check that the given `memory_region` represents a valid memory send request
1282 * of the given `share_func` type, return the clear flag and permissions via the
1283 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001284 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001285 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001286 * not.
1287 */
J-Alves66652252022-07-06 09:49:51 +01001288struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001289 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1290 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001291 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001292{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001293 struct ffa_composite_memory_region *composite;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001294 uint64_t receivers_end;
1295 uint64_t min_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001296 uint32_t composite_memory_region_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001297 uint32_t constituents_start;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001298 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001299 enum ffa_data_access data_access;
1300 enum ffa_instruction_access instruction_access;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001301 enum ffa_memory_security security_state;
Federico Recanatia98603a2021-12-20 18:04:03 +01001302 struct ffa_value ret;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001303 const size_t minimum_first_fragment_length =
1304 (sizeof(struct ffa_memory_region) +
1305 sizeof(struct ffa_memory_access) +
1306 sizeof(struct ffa_composite_memory_region));
1307
1308 if (fragment_length < minimum_first_fragment_length) {
1309 dlog_verbose("Fragment length %u too short (min %u).\n",
1310 (size_t)fragment_length,
1311 minimum_first_fragment_length);
1312 return ffa_error(FFA_INVALID_PARAMETERS);
1313 }
1314
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05001315 static_assert(sizeof(struct ffa_memory_region_constituent) == 16,
1316 "struct ffa_memory_region_constituent must be 16 bytes");
1317 if (!is_aligned(fragment_length,
1318 sizeof(struct ffa_memory_region_constituent)) ||
1319 !is_aligned(memory_share_length,
1320 sizeof(struct ffa_memory_region_constituent))) {
1321 dlog_verbose(
1322 "Fragment length %u or total length %u"
1323 " is not 16-byte aligned.\n",
1324 fragment_length, memory_share_length);
1325 return ffa_error(FFA_INVALID_PARAMETERS);
1326 }
1327
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001328 if (fragment_length > memory_share_length) {
1329 dlog_verbose(
1330 "Fragment length %u greater than total length %u.\n",
1331 (size_t)fragment_length, (size_t)memory_share_length);
1332 return ffa_error(FFA_INVALID_PARAMETERS);
1333 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001334
J-Alves0b6653d2022-04-22 13:17:38 +01001335 assert(memory_region->receivers_offset ==
1336 offsetof(struct ffa_memory_region, receivers));
1337 assert(memory_region->memory_access_desc_size ==
1338 sizeof(struct ffa_memory_access));
1339
J-Alves95df0ef2022-12-07 10:09:48 +00001340 /* The sender must match the caller. */
1341 if ((!vm_id_is_current_world(from_locked.vm->id) &&
1342 vm_id_is_current_world(memory_region->sender)) ||
1343 (vm_id_is_current_world(from_locked.vm->id) &&
1344 memory_region->sender != from_locked.vm->id)) {
1345 dlog_verbose("Invalid memory sender ID.\n");
1346 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001347 }
1348
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001349 if (memory_region->receiver_count <= 0) {
1350 dlog_verbose("No receivers!\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001351 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001352 }
1353
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001354 /*
1355 * Ensure that the composite header is within the memory bounds and
1356 * doesn't overlap the first part of the message. Cast to uint64_t
1357 * to prevent overflow.
1358 */
1359 receivers_end = ((uint64_t)sizeof(struct ffa_memory_access) *
1360 (uint64_t)memory_region->receiver_count) +
1361 sizeof(struct ffa_memory_region);
1362 min_length = receivers_end +
1363 sizeof(struct ffa_composite_memory_region) +
1364 sizeof(struct ffa_memory_region_constituent);
1365 if (min_length > memory_share_length) {
1366 dlog_verbose("Share too short: got %u but minimum is %u.\n",
1367 (size_t)memory_share_length, (size_t)min_length);
1368 return ffa_error(FFA_INVALID_PARAMETERS);
1369 }
1370
1371 composite_memory_region_offset =
1372 memory_region->receivers[0].composite_memory_region_offset;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001373
1374 /*
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001375 * Check that the composite memory region descriptor is after the access
1376 * descriptors, is at least 16-byte aligned, and fits in the first
1377 * fragment.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001378 */
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001379 if ((composite_memory_region_offset < receivers_end) ||
1380 (composite_memory_region_offset % 16 != 0) ||
1381 (composite_memory_region_offset >
1382 fragment_length - sizeof(struct ffa_composite_memory_region))) {
1383 dlog_verbose(
1384 "Invalid composite memory region descriptor offset "
1385 "%u.\n",
1386 (size_t)composite_memory_region_offset);
1387 return ffa_error(FFA_INVALID_PARAMETERS);
1388 }
1389
1390 /*
1391 * Compute the start of the constituent regions. Already checked
1392 * to be not more than fragment_length and thus not more than
1393 * memory_share_length.
1394 */
1395 constituents_start = composite_memory_region_offset +
1396 sizeof(struct ffa_composite_memory_region);
1397 constituents_length = memory_share_length - constituents_start;
1398
1399 /*
1400 * Check that the number of constituents is consistent with the length
1401 * of the constituent region.
1402 */
1403 composite = ffa_memory_region_get_composite(memory_region, 0);
1404 if ((constituents_length %
1405 sizeof(struct ffa_memory_region_constituent) !=
1406 0) ||
1407 ((constituents_length /
1408 sizeof(struct ffa_memory_region_constituent)) !=
1409 composite->constituent_count)) {
1410 dlog_verbose("Invalid length %u or composite offset %u.\n",
1411 (size_t)memory_share_length,
1412 (size_t)composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001413 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001414 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001415 if (fragment_length < memory_share_length &&
1416 fragment_length < HF_MAILBOX_SIZE) {
1417 dlog_warning(
1418 "Initial fragment length %d smaller than mailbox "
1419 "size.\n",
1420 fragment_length);
1421 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001422
Andrew Walbrana65a1322020-04-06 19:32:32 +01001423 /*
1424 * Clear is not allowed for memory sharing, as the sender still has
1425 * access to the memory.
1426 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001427 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1428 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001429 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001430 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001431 }
1432
1433 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001434 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001435 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001436 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001437 }
1438
J-Alves363f5722022-04-25 17:37:37 +01001439 /* Check that the permissions are valid, for each specified receiver. */
1440 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
1441 ffa_memory_access_permissions_t permissions =
1442 memory_region->receivers[i]
1443 .receiver_permissions.permissions;
1444 ffa_vm_id_t receiver_id =
1445 memory_region->receivers[i]
1446 .receiver_permissions.receiver;
1447
1448 if (memory_region->sender == receiver_id) {
1449 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001450 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001451 }
Federico Recanati85090c42021-12-15 13:17:54 +01001452
J-Alves363f5722022-04-25 17:37:37 +01001453 for (uint32_t j = i + 1; j < memory_region->receiver_count;
1454 j++) {
1455 if (receiver_id ==
1456 memory_region->receivers[j]
1457 .receiver_permissions.receiver) {
1458 dlog_verbose(
1459 "Repeated receiver(%x) in memory send "
1460 "operation.\n",
1461 memory_region->receivers[j]
1462 .receiver_permissions.receiver);
1463 return ffa_error(FFA_INVALID_PARAMETERS);
1464 }
1465 }
1466
1467 if (composite_memory_region_offset !=
1468 memory_region->receivers[i]
1469 .composite_memory_region_offset) {
1470 dlog_verbose(
1471 "All ffa_memory_access should point to the "
1472 "same composite memory region offset.\n");
1473 return ffa_error(FFA_INVALID_PARAMETERS);
1474 }
1475
1476 data_access = ffa_get_data_access_attr(permissions);
1477 instruction_access =
1478 ffa_get_instruction_access_attr(permissions);
1479 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1480 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
1481 dlog_verbose(
1482 "Reserved value for receiver permissions "
1483 "%#x.\n",
1484 permissions);
1485 return ffa_error(FFA_INVALID_PARAMETERS);
1486 }
1487 if (instruction_access !=
1488 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
1489 dlog_verbose(
1490 "Invalid instruction access permissions %#x "
1491 "for sending memory.\n",
1492 permissions);
1493 return ffa_error(FFA_INVALID_PARAMETERS);
1494 }
1495 if (share_func == FFA_MEM_SHARE_32) {
1496 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1497 dlog_verbose(
1498 "Invalid data access permissions %#x "
1499 "for sharing memory.\n",
1500 permissions);
1501 return ffa_error(FFA_INVALID_PARAMETERS);
1502 }
1503 /*
1504 * According to section 10.10.3 of the FF-A v1.1 EAC0
1505 * spec, NX is required for share operations (but must
1506 * not be specified by the sender) so set it in the
1507 * copy that we store, ready to be returned to the
1508 * retriever.
1509 */
J-Alvesb19731a2022-06-20 17:30:33 +01001510 if (vm_id_is_current_world(receiver_id)) {
1511 ffa_set_instruction_access_attr(
1512 &permissions,
1513 FFA_INSTRUCTION_ACCESS_NX);
1514 memory_region->receivers[i]
1515 .receiver_permissions.permissions =
1516 permissions;
1517 }
J-Alves363f5722022-04-25 17:37:37 +01001518 }
1519 if (share_func == FFA_MEM_LEND_32 &&
1520 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1521 dlog_verbose(
1522 "Invalid data access permissions %#x for "
1523 "lending memory.\n",
1524 permissions);
1525 return ffa_error(FFA_INVALID_PARAMETERS);
1526 }
1527
1528 if (share_func == FFA_MEM_DONATE_32 &&
1529 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
1530 dlog_verbose(
1531 "Invalid data access permissions %#x for "
1532 "donating memory.\n",
1533 permissions);
1534 return ffa_error(FFA_INVALID_PARAMETERS);
1535 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001536 }
1537
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001538 /* Memory region attributes NS-Bit MBZ for FFA_MEM_SHARE/LEND/DONATE. */
1539 security_state =
1540 ffa_get_memory_security_attr(memory_region->attributes);
1541 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
1542 dlog_verbose(
1543 "Invalid security state for memory share operation.\n");
1544 return ffa_error(FFA_INVALID_PARAMETERS);
1545 }
1546
Federico Recanatid937f5e2021-12-20 17:38:23 +01001547 /*
J-Alves807794e2022-06-16 13:42:47 +01001548 * If a memory donate or lend with single borrower, the memory type
1549 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01001550 */
J-Alves807794e2022-06-16 13:42:47 +01001551 if (share_func == FFA_MEM_DONATE_32 ||
1552 (share_func == FFA_MEM_LEND_32 &&
1553 memory_region->receiver_count == 1)) {
1554 if (ffa_get_memory_type_attr(memory_region->attributes) !=
1555 FFA_MEMORY_NOT_SPECIFIED_MEM) {
1556 dlog_verbose(
1557 "Memory type shall not be specified by "
1558 "sender.\n");
1559 return ffa_error(FFA_INVALID_PARAMETERS);
1560 }
1561 } else {
1562 /*
1563 * Check that sender's memory attributes match Hafnium
1564 * expectations: Normal Memory, Inner shareable, Write-Back
1565 * Read-Allocate Write-Allocate Cacheable.
1566 */
1567 ret = ffa_memory_attributes_validate(memory_region->attributes);
1568 if (ret.func != FFA_SUCCESS_32) {
1569 return ret;
1570 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01001571 }
1572
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001573 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001574}
1575
1576/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001577 * Gets the share state for continuing an operation to donate, lend or share
1578 * memory, and checks that it is a valid request.
1579 *
1580 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1581 * not.
1582 */
J-Alvesfdd29272022-07-19 13:16:31 +01001583struct ffa_value ffa_memory_send_continue_validate(
Andrew Walbranca808b12020-05-15 17:22:28 +01001584 struct share_states_locked share_states, ffa_memory_handle_t handle,
1585 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1586 struct mpool *page_pool)
1587{
1588 struct ffa_memory_share_state *share_state;
1589 struct ffa_memory_region *memory_region;
1590
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001591 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001592
1593 /*
1594 * Look up the share state by handle and make sure that the VM ID
1595 * matches.
1596 */
1597 if (!get_share_state(share_states, handle, &share_state)) {
1598 dlog_verbose(
1599 "Invalid handle %#x for memory send continuation.\n",
1600 handle);
1601 return ffa_error(FFA_INVALID_PARAMETERS);
1602 }
1603 memory_region = share_state->memory_region;
1604
J-Alvesfdd29272022-07-19 13:16:31 +01001605 if (vm_id_is_current_world(from_vm_id) &&
1606 memory_region->sender != from_vm_id) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001607 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1608 return ffa_error(FFA_INVALID_PARAMETERS);
1609 }
1610
1611 if (share_state->sending_complete) {
1612 dlog_verbose(
1613 "Sending of memory handle %#x is already complete.\n",
1614 handle);
1615 return ffa_error(FFA_INVALID_PARAMETERS);
1616 }
1617
1618 if (share_state->fragment_count == MAX_FRAGMENTS) {
1619 /*
1620 * Log a warning as this is a sign that MAX_FRAGMENTS should
1621 * probably be increased.
1622 */
1623 dlog_warning(
1624 "Too many fragments for memory share with handle %#x; "
1625 "only %d supported.\n",
1626 handle, MAX_FRAGMENTS);
1627 /* Free share state, as it's not possible to complete it. */
1628 share_state_free(share_states, share_state, page_pool);
1629 return ffa_error(FFA_NO_MEMORY);
1630 }
1631
1632 *share_state_ret = share_state;
1633
1634 return (struct ffa_value){.func = FFA_SUCCESS_32};
1635}
1636
1637/**
J-Alves95df0ef2022-12-07 10:09:48 +00001638 * Checks if there is at least one receiver from the other world.
1639 */
J-Alvesfdd29272022-07-19 13:16:31 +01001640bool memory_region_receivers_from_other_world(
J-Alves95df0ef2022-12-07 10:09:48 +00001641 struct ffa_memory_region *memory_region)
1642{
1643 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
1644 ffa_vm_id_t receiver = memory_region->receivers[i]
1645 .receiver_permissions.receiver;
1646 if (!vm_id_is_current_world(receiver)) {
1647 return true;
1648 }
1649 }
1650 return false;
1651}
1652
1653/**
J-Alves9da280b2022-12-21 14:55:39 +00001654 * Validates a call to donate, lend or share memory in which Hafnium is the
1655 * designated allocator of the memory handle. In practice, this also means
1656 * Hafnium is responsible for managing the state structures for the transaction.
1657 * If Hafnium is the SPMC, it should allocate the memory handle when either the
1658 * sender is an SP or there is at least one borrower that is an SP.
1659 * If Hafnium is the hypervisor, it should allocate the memory handle when
1660 * operation involves only NWd VMs.
1661 *
1662 * If validation goes well, Hafnium updates the stage-2 page tables of the
1663 * sender. Validation consists of checking if the message length and number of
1664 * memory region constituents match, and if the transition is valid for the
1665 * type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001666 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001667 * Assumes that the caller has already found and locked the sender VM and copied
1668 * the memory region descriptor from the sender's TX buffer to a freshly
1669 * allocated page from Hafnium's internal pool. The caller must have also
1670 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001671 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001672 * This function takes ownership of the `memory_region` passed in and will free
1673 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001674 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001675struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001676 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001677 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001678 uint32_t fragment_length, uint32_t share_func,
1679 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001680{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001681 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001682 struct share_states_locked share_states;
1683 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001684
1685 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001686 * If there is an error validating the `memory_region` then we need to
1687 * free it because we own it but we won't be storing it in a share state
1688 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001689 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001690 ret = ffa_memory_send_validate(from_locked, memory_region,
1691 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001692 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001693 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001694 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001695 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001696 }
1697
Andrew Walbrana65a1322020-04-06 19:32:32 +01001698 /* Set flag for share function, ready to be retrieved later. */
1699 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001700 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001701 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001702 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001703 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001704 case FFA_MEM_LEND_32:
1705 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001706 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001707 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001708 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001709 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001710 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001711 }
1712
Andrew Walbranca808b12020-05-15 17:22:28 +01001713 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001714 /*
1715 * Allocate a share state before updating the page table. Otherwise if
1716 * updating the page table succeeded but allocating the share state
1717 * failed then it would leave the memory in a state where nobody could
1718 * get it back.
1719 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001720 if (!allocate_share_state(share_states, share_func, memory_region,
1721 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1722 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001723 dlog_verbose("Failed to allocate share state.\n");
1724 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001725 ret = ffa_error(FFA_NO_MEMORY);
1726 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001727 }
1728
Andrew Walbranca808b12020-05-15 17:22:28 +01001729 if (fragment_length == memory_share_length) {
1730 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00001731 ret = ffa_memory_send_complete(
1732 from_locked, share_states, share_state, page_pool,
1733 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001734 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01001735 /*
1736 * Use sender ID from 'memory_region' assuming
1737 * that at this point it has been validated:
1738 * - MBZ at virtual FF-A instance.
1739 */
1740 ffa_vm_id_t sender_to_ret =
1741 (from_locked.vm->id == HF_OTHER_WORLD_ID)
1742 ? memory_region->sender
1743 : 0;
Andrew Walbranca808b12020-05-15 17:22:28 +01001744 ret = (struct ffa_value){
1745 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00001746 .arg1 = (uint32_t)memory_region->handle,
1747 .arg2 = (uint32_t)(memory_region->handle >> 32),
J-Alvesfdd29272022-07-19 13:16:31 +01001748 .arg3 = fragment_length,
1749 .arg4 = (uint32_t)(sender_to_ret & 0xffff) << 16};
Andrew Walbranca808b12020-05-15 17:22:28 +01001750 }
1751
1752out:
1753 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001754 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001755 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001756}
1757
1758/**
J-Alves8505a8a2022-06-15 18:10:18 +01001759 * Continues an operation to donate, lend or share memory to a VM from current
1760 * world. If this is the last fragment then checks that the transition is valid
1761 * for the type of memory sending operation and updates the stage-2 page tables
1762 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01001763 *
1764 * Assumes that the caller has already found and locked the sender VM and copied
1765 * the memory region descriptor from the sender's TX buffer to a freshly
1766 * allocated page from Hafnium's internal pool.
1767 *
1768 * This function takes ownership of the `fragment` passed in; it must not be
1769 * freed by the caller.
1770 */
1771struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1772 void *fragment,
1773 uint32_t fragment_length,
1774 ffa_memory_handle_t handle,
1775 struct mpool *page_pool)
1776{
1777 struct share_states_locked share_states = share_states_lock();
1778 struct ffa_memory_share_state *share_state;
1779 struct ffa_value ret;
1780 struct ffa_memory_region *memory_region;
1781
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05001782 CHECK(is_aligned(fragment,
1783 alignof(struct ffa_memory_region_constituent)));
1784 if (fragment_length % sizeof(struct ffa_memory_region_constituent) !=
1785 0) {
1786 dlog_verbose("Fragment length %u misaligned.\n",
1787 fragment_length);
1788 ret = ffa_error(FFA_INVALID_PARAMETERS);
1789 goto out_free_fragment;
1790 }
1791
Andrew Walbranca808b12020-05-15 17:22:28 +01001792 ret = ffa_memory_send_continue_validate(share_states, handle,
1793 &share_state,
1794 from_locked.vm->id, page_pool);
1795 if (ret.func != FFA_SUCCESS_32) {
1796 goto out_free_fragment;
1797 }
1798 memory_region = share_state->memory_region;
1799
J-Alves95df0ef2022-12-07 10:09:48 +00001800 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001801 dlog_error(
1802 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01001803 "other world. This should never happen, and indicates "
1804 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01001805 "EL3 code.\n");
1806 ret = ffa_error(FFA_INVALID_PARAMETERS);
1807 goto out_free_fragment;
1808 }
1809
1810 /* Add this fragment. */
1811 share_state->fragments[share_state->fragment_count] = fragment;
1812 share_state->fragment_constituent_counts[share_state->fragment_count] =
1813 fragment_length / sizeof(struct ffa_memory_region_constituent);
1814 share_state->fragment_count++;
1815
1816 /* Check whether the memory send operation is now ready to complete. */
1817 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00001818 ret = ffa_memory_send_complete(
1819 from_locked, share_states, share_state, page_pool,
1820 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001821 } else {
1822 ret = (struct ffa_value){
1823 .func = FFA_MEM_FRAG_RX_32,
1824 .arg1 = (uint32_t)handle,
1825 .arg2 = (uint32_t)(handle >> 32),
1826 .arg3 = share_state_next_fragment_offset(share_states,
1827 share_state)};
1828 }
1829 goto out;
1830
1831out_free_fragment:
1832 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001833
1834out:
Andrew Walbranca808b12020-05-15 17:22:28 +01001835 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001836 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001837}
1838
Andrew Walbranca808b12020-05-15 17:22:28 +01001839/** Clean up after the receiver has finished retrieving a memory region. */
1840static void ffa_memory_retrieve_complete(
1841 struct share_states_locked share_states,
1842 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
1843{
1844 if (share_state->share_func == FFA_MEM_DONATE_32) {
1845 /*
1846 * Memory that has been donated can't be relinquished,
1847 * so no need to keep the share state around.
1848 */
1849 share_state_free(share_states, share_state, page_pool);
1850 dlog_verbose("Freed share state for donate.\n");
1851 }
1852}
1853
J-Alves2d8457f2022-10-05 11:06:41 +01001854/**
1855 * Initialises the given memory region descriptor to be used for an
1856 * `FFA_MEM_RETRIEVE_RESP`, including the given constituents for the first
1857 * fragment.
1858 * The memory region descriptor is initialized according to retriever's
1859 * FF-A version.
1860 *
1861 * Returns true on success, or false if the given constituents won't all fit in
1862 * the first fragment.
1863 */
1864static bool ffa_retrieved_memory_region_init(
1865 void *response, uint32_t ffa_version, size_t response_max_size,
1866 ffa_vm_id_t sender, ffa_memory_attributes_t attributes,
1867 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
1868 ffa_vm_id_t receiver_id, ffa_memory_access_permissions_t permissions,
1869 uint32_t page_count, uint32_t total_constituent_count,
1870 const struct ffa_memory_region_constituent constituents[],
1871 uint32_t fragment_constituent_count, uint32_t *total_length,
1872 uint32_t *fragment_length)
1873{
1874 struct ffa_composite_memory_region *composite_memory_region;
1875 struct ffa_memory_access *receiver;
1876 uint32_t i;
1877 uint32_t constituents_offset;
1878 uint32_t receiver_count;
1879
1880 assert(response != NULL);
1881
1882 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1883 struct ffa_memory_region_v1_0 *retrieve_response =
1884 (struct ffa_memory_region_v1_0 *)response;
1885
J-Alves5da37d92022-10-24 16:33:48 +01001886 ffa_memory_region_init_header_v1_0(
1887 retrieve_response, sender, attributes, flags, handle, 0,
1888 RECEIVERS_COUNT_IN_RETRIEVE_RESP);
J-Alves2d8457f2022-10-05 11:06:41 +01001889
1890 receiver = &retrieve_response->receivers[0];
1891 receiver_count = retrieve_response->receiver_count;
1892
1893 receiver->composite_memory_region_offset =
1894 sizeof(struct ffa_memory_region_v1_0) +
1895 receiver_count * sizeof(struct ffa_memory_access);
1896
1897 composite_memory_region = ffa_memory_region_get_composite_v1_0(
1898 retrieve_response, 0);
1899 } else {
1900 /* Default to FF-A v1.1 version. */
1901 struct ffa_memory_region *retrieve_response =
1902 (struct ffa_memory_region *)response;
1903
1904 ffa_memory_region_init_header(retrieve_response, sender,
1905 attributes, flags, handle, 0, 1);
1906
1907 receiver = &retrieve_response->receivers[0];
1908 receiver_count = retrieve_response->receiver_count;
1909
1910 /*
1911 * Note that `sizeof(struct_ffa_memory_region)` and
1912 * `sizeof(struct ffa_memory_access)` must both be multiples of
1913 * 16 (as verified by the asserts in `ffa_memory.c`, so it is
1914 * guaranteed that the offset we calculate here is aligned to a
1915 * 64-bit boundary and so 64-bit values can be copied without
1916 * alignment faults.
1917 */
1918 receiver->composite_memory_region_offset =
1919 sizeof(struct ffa_memory_region) +
1920 receiver_count * sizeof(struct ffa_memory_access);
1921
1922 composite_memory_region =
1923 ffa_memory_region_get_composite(retrieve_response, 0);
1924 }
1925
1926 assert(receiver != NULL);
1927 assert(composite_memory_region != NULL);
1928
1929 /*
1930 * Initialized here as in memory retrieve responses we currently expect
1931 * one borrower to be specified.
1932 */
1933 ffa_memory_access_init_permissions(receiver, receiver_id, 0, 0, flags);
1934 receiver->receiver_permissions.permissions = permissions;
1935
1936 composite_memory_region->page_count = page_count;
1937 composite_memory_region->constituent_count = total_constituent_count;
1938 composite_memory_region->reserved_0 = 0;
1939
1940 constituents_offset = receiver->composite_memory_region_offset +
1941 sizeof(struct ffa_composite_memory_region);
1942 if (constituents_offset +
1943 fragment_constituent_count *
1944 sizeof(struct ffa_memory_region_constituent) >
1945 response_max_size) {
1946 return false;
1947 }
1948
1949 for (i = 0; i < fragment_constituent_count; ++i) {
1950 composite_memory_region->constituents[i] = constituents[i];
1951 }
1952
1953 if (total_length != NULL) {
1954 *total_length =
1955 constituents_offset +
1956 composite_memory_region->constituent_count *
1957 sizeof(struct ffa_memory_region_constituent);
1958 }
1959 if (fragment_length != NULL) {
1960 *fragment_length =
1961 constituents_offset +
1962 fragment_constituent_count *
1963 sizeof(struct ffa_memory_region_constituent);
1964 }
1965
1966 return true;
1967}
1968
J-Alves96de29f2022-04-26 16:05:24 +01001969/*
1970 * Gets the receiver's access permissions from 'struct ffa_memory_region' and
1971 * returns its index in the receiver's array. If receiver's ID doesn't exist
1972 * in the array, return the region's 'receiver_count'.
1973 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001974uint32_t ffa_memory_region_get_receiver(struct ffa_memory_region *memory_region,
1975 ffa_vm_id_t receiver)
J-Alves96de29f2022-04-26 16:05:24 +01001976{
1977 struct ffa_memory_access *receivers;
1978 uint32_t i;
1979
1980 assert(memory_region != NULL);
1981
1982 receivers = memory_region->receivers;
1983
1984 for (i = 0U; i < memory_region->receiver_count; i++) {
1985 if (receivers[i].receiver_permissions.receiver == receiver) {
1986 break;
1987 }
1988 }
1989
1990 return i;
1991}
1992
1993/**
1994 * Validates the retrieved permissions against those specified by the lender
1995 * of memory share operation. Optionally can help set the permissions to be used
1996 * for the S2 mapping, through the `permissions` argument.
1997 * Returns true if permissions are valid, false otherwise.
1998 */
1999static bool ffa_memory_retrieve_is_memory_access_valid(
2000 enum ffa_data_access sent_data_access,
2001 enum ffa_data_access requested_data_access,
2002 enum ffa_instruction_access sent_instruction_access,
2003 enum ffa_instruction_access requested_instruction_access,
2004 ffa_memory_access_permissions_t *permissions)
2005{
2006 switch (sent_data_access) {
2007 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2008 case FFA_DATA_ACCESS_RW:
2009 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2010 requested_data_access == FFA_DATA_ACCESS_RW) {
2011 if (permissions != NULL) {
2012 ffa_set_data_access_attr(permissions,
2013 FFA_DATA_ACCESS_RW);
2014 }
2015 break;
2016 }
2017 /* Intentional fall-through. */
2018 case FFA_DATA_ACCESS_RO:
2019 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2020 requested_data_access == FFA_DATA_ACCESS_RO) {
2021 if (permissions != NULL) {
2022 ffa_set_data_access_attr(permissions,
2023 FFA_DATA_ACCESS_RO);
2024 }
2025 break;
2026 }
2027 dlog_verbose(
2028 "Invalid data access requested; sender specified "
2029 "permissions %#x but receiver requested %#x.\n",
2030 sent_data_access, requested_data_access);
2031 return false;
2032 case FFA_DATA_ACCESS_RESERVED:
2033 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
2034 "checked before this point.");
2035 }
2036
2037 switch (sent_instruction_access) {
2038 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2039 case FFA_INSTRUCTION_ACCESS_X:
2040 if (requested_instruction_access ==
2041 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2042 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
2043 if (permissions != NULL) {
2044 ffa_set_instruction_access_attr(
2045 permissions, FFA_INSTRUCTION_ACCESS_X);
2046 }
2047 break;
2048 }
2049 case FFA_INSTRUCTION_ACCESS_NX:
2050 if (requested_instruction_access ==
2051 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2052 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2053 if (permissions != NULL) {
2054 ffa_set_instruction_access_attr(
2055 permissions, FFA_INSTRUCTION_ACCESS_NX);
2056 }
2057 break;
2058 }
2059 dlog_verbose(
2060 "Invalid instruction access requested; sender "
2061 "specified permissions %#x but receiver requested "
2062 "%#x.\n",
2063 sent_instruction_access, requested_instruction_access);
2064 return false;
2065 case FFA_INSTRUCTION_ACCESS_RESERVED:
2066 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
2067 "be checked before this point.");
2068 }
2069
2070 return true;
2071}
2072
2073/**
2074 * Validate the receivers' permissions in the retrieve request against those
2075 * specified by the lender.
2076 * In the `permissions` argument returns the permissions to set at S2 for the
2077 * caller to the FFA_MEMORY_RETRIEVE_REQ.
2078 * Returns FFA_SUCCESS if all specified permissions are valid.
2079 */
2080static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
2081 struct ffa_memory_region *memory_region,
2082 struct ffa_memory_region *retrieve_request, ffa_vm_id_t to_vm_id,
2083 ffa_memory_access_permissions_t *permissions)
2084{
2085 uint32_t retrieve_receiver_index;
2086
2087 assert(permissions != NULL);
2088
2089 if (retrieve_request->receiver_count != memory_region->receiver_count) {
2090 dlog_verbose(
2091 "Retrieve request should contain same list of "
2092 "borrowers, as specified by the lender.\n");
2093 return ffa_error(FFA_INVALID_PARAMETERS);
2094 }
2095
2096 retrieve_receiver_index = retrieve_request->receiver_count;
2097
2098 /* Should be populated with the permissions of the retriever. */
2099 *permissions = 0;
2100
2101 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
2102 ffa_memory_access_permissions_t sent_permissions;
2103 struct ffa_memory_access *current_receiver =
2104 &retrieve_request->receivers[i];
2105 ffa_memory_access_permissions_t requested_permissions =
2106 current_receiver->receiver_permissions.permissions;
2107 ffa_vm_id_t current_receiver_id =
2108 current_receiver->receiver_permissions.receiver;
2109 bool found_to_id = current_receiver_id == to_vm_id;
2110
2111 /*
2112 * Find the current receiver in the transaction descriptor from
2113 * sender.
2114 */
2115 uint32_t mem_region_receiver_index =
2116 ffa_memory_region_get_receiver(memory_region,
2117 current_receiver_id);
2118
2119 if (mem_region_receiver_index ==
2120 memory_region->receiver_count) {
2121 dlog_verbose("%s: receiver %x not found\n", __func__,
2122 current_receiver_id);
2123 return ffa_error(FFA_DENIED);
2124 }
2125
2126 sent_permissions =
2127 memory_region->receivers[mem_region_receiver_index]
2128 .receiver_permissions.permissions;
2129
2130 if (found_to_id) {
2131 retrieve_receiver_index = i;
2132 }
2133
2134 /*
2135 * Since we are traversing the list of receivers, save the index
2136 * of the caller. As it needs to be there.
2137 */
2138
2139 if (current_receiver->composite_memory_region_offset != 0U) {
2140 dlog_verbose(
2141 "Retriever specified address ranges not "
2142 "supported (got offset %d).\n",
2143 current_receiver
2144 ->composite_memory_region_offset);
2145 return ffa_error(FFA_INVALID_PARAMETERS);
2146 }
2147
2148 /*
2149 * Check permissions from sender against permissions requested
2150 * by receiver.
2151 */
2152 if (!ffa_memory_retrieve_is_memory_access_valid(
2153 ffa_get_data_access_attr(sent_permissions),
2154 ffa_get_data_access_attr(requested_permissions),
2155 ffa_get_instruction_access_attr(sent_permissions),
2156 ffa_get_instruction_access_attr(
2157 requested_permissions),
2158 found_to_id ? permissions : NULL)) {
2159 return ffa_error(FFA_DENIED);
2160 }
2161
2162 /*
2163 * Can't request PM to clear memory if only provided with RO
2164 * permissions.
2165 */
2166 if (found_to_id &&
2167 (ffa_get_data_access_attr(*permissions) ==
2168 FFA_DATA_ACCESS_RO) &&
2169 (retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2170 0U) {
2171 dlog_verbose(
2172 "Receiver has RO permissions can not request "
2173 "clear.\n");
2174 return ffa_error(FFA_DENIED);
2175 }
2176 }
2177
2178 if (retrieve_receiver_index == retrieve_request->receiver_count) {
2179 dlog_verbose(
2180 "Retrieve request does not contain caller's (%x) "
2181 "permissions\n",
2182 to_vm_id);
2183 return ffa_error(FFA_INVALID_PARAMETERS);
2184 }
2185
2186 return (struct ffa_value){.func = FFA_SUCCESS_32};
2187}
2188
J-Alvesa9cd7e32022-07-01 13:49:33 +01002189/*
2190 * According to section 16.4.3 of FF-A v1.1 EAC0 specification, the hypervisor
2191 * may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region description
2192 * of a pending memory sharing operation whose allocator is the SPM, for
2193 * validation purposes before forwarding an FFA_MEM_RECLAIM call. In doing so
2194 * the memory region descriptor of the retrieve request must be zeroed with the
2195 * exception of the sender ID and handle.
2196 */
2197bool is_ffa_memory_retrieve_borrower_request(struct ffa_memory_region *request,
2198 struct vm_locked to_locked)
2199{
2200 return to_locked.vm->id == HF_HYPERVISOR_VM_ID &&
2201 request->attributes == 0U && request->flags == 0U &&
2202 request->tag == 0U && request->receiver_count == 0U &&
2203 plat_ffa_memory_handle_allocated_by_current_world(
2204 request->handle);
2205}
2206
2207/*
2208 * Helper to reset count of fragments retrieved by the hypervisor.
2209 */
2210static void ffa_memory_retrieve_complete_from_hyp(
2211 struct ffa_memory_share_state *share_state)
2212{
2213 if (share_state->hypervisor_fragment_count ==
2214 share_state->fragment_count) {
2215 share_state->hypervisor_fragment_count = 0;
2216 }
2217}
2218
J-Alves089004f2022-07-13 14:25:44 +01002219/**
2220 * Validate that the memory region descriptor provided by the borrower on
2221 * FFA_MEM_RETRIEVE_REQ, against saved memory region provided by lender at the
2222 * memory sharing call.
2223 */
2224static struct ffa_value ffa_memory_retrieve_validate(
2225 ffa_vm_id_t receiver_id, struct ffa_memory_region *retrieve_request,
2226 struct ffa_memory_region *memory_region, uint32_t *receiver_index,
2227 uint32_t share_func)
2228{
2229 ffa_memory_region_flags_t transaction_type =
2230 retrieve_request->flags &
2231 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002232 enum ffa_memory_security security_state;
J-Alves089004f2022-07-13 14:25:44 +01002233
2234 assert(retrieve_request != NULL);
2235 assert(memory_region != NULL);
2236 assert(receiver_index != NULL);
2237 assert(retrieve_request->sender == memory_region->sender);
2238
2239 /*
2240 * Check that the transaction type expected by the receiver is
2241 * correct, if it has been specified.
2242 */
2243 if (transaction_type !=
2244 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
2245 transaction_type != (memory_region->flags &
2246 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
2247 dlog_verbose(
2248 "Incorrect transaction type %#x for "
2249 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
2250 transaction_type,
2251 memory_region->flags &
2252 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
2253 retrieve_request->handle);
2254 return ffa_error(FFA_INVALID_PARAMETERS);
2255 }
2256
2257 if (retrieve_request->tag != memory_region->tag) {
2258 dlog_verbose(
2259 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
2260 "%d for handle %#x.\n",
2261 retrieve_request->tag, memory_region->tag,
2262 retrieve_request->handle);
2263 return ffa_error(FFA_INVALID_PARAMETERS);
2264 }
2265
2266 *receiver_index =
2267 ffa_memory_region_get_receiver(memory_region, receiver_id);
2268
2269 if (*receiver_index == memory_region->receiver_count) {
2270 dlog_verbose(
2271 "Incorrect receiver VM ID %d for "
2272 "FFA_MEM_RETRIEVE_REQ, for handle %#x.\n",
J-Alves59ed0042022-07-28 18:26:41 +01002273 receiver_id, memory_region->handle);
J-Alves089004f2022-07-13 14:25:44 +01002274 return ffa_error(FFA_INVALID_PARAMETERS);
2275 }
2276
2277 if ((retrieve_request->flags &
2278 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
2279 dlog_verbose(
2280 "Retriever specified 'address range alignment 'hint' "
2281 "not supported.\n");
2282 return ffa_error(FFA_INVALID_PARAMETERS);
2283 }
2284 if ((retrieve_request->flags &
2285 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
2286 dlog_verbose(
2287 "Bits 8-5 must be zero in memory region's flags "
2288 "(address range alignment hint not supported).\n");
2289 return ffa_error(FFA_INVALID_PARAMETERS);
2290 }
2291
2292 if ((retrieve_request->flags & ~0x7FF) != 0U) {
2293 dlog_verbose(
2294 "Bits 31-10 must be zero in memory region's flags.\n");
2295 return ffa_error(FFA_INVALID_PARAMETERS);
2296 }
2297
2298 if (share_func == FFA_MEM_SHARE_32 &&
2299 (retrieve_request->flags &
2300 (FFA_MEMORY_REGION_FLAG_CLEAR |
2301 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
2302 dlog_verbose(
2303 "Memory Share operation can't clean after relinquish "
2304 "memory region.\n");
2305 return ffa_error(FFA_INVALID_PARAMETERS);
2306 }
2307
2308 /*
2309 * If the borrower needs the memory to be cleared before mapping
2310 * to its address space, the sender should have set the flag
2311 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
2312 * FFA_DENIED.
2313 */
2314 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
2315 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
2316 dlog_verbose(
2317 "Borrower needs memory cleared. Sender needs to set "
2318 "flag for clearing memory.\n");
2319 return ffa_error(FFA_DENIED);
2320 }
2321
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002322 /* Memory region attributes NS-Bit MBZ for FFA_MEM_RETRIEVE_REQ. */
2323 security_state =
2324 ffa_get_memory_security_attr(retrieve_request->attributes);
2325 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
2326 dlog_verbose(
2327 "Invalid security state for memory retrieve request "
2328 "operation.\n");
2329 return ffa_error(FFA_INVALID_PARAMETERS);
2330 }
2331
J-Alves089004f2022-07-13 14:25:44 +01002332 /*
2333 * If memory type is not specified, bypass validation of memory
2334 * attributes in the retrieve request. The retriever is expecting to
2335 * obtain this information from the SPMC.
2336 */
2337 if (ffa_get_memory_type_attr(retrieve_request->attributes) ==
2338 FFA_MEMORY_NOT_SPECIFIED_MEM) {
2339 return (struct ffa_value){.func = FFA_SUCCESS_32};
2340 }
2341
2342 /*
2343 * Ensure receiver's attributes are compatible with how
2344 * Hafnium maps memory: Normal Memory, Inner shareable,
2345 * Write-Back Read-Allocate Write-Allocate Cacheable.
2346 */
2347 return ffa_memory_attributes_validate(retrieve_request->attributes);
2348}
2349
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002350struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2351 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002352 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002353 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002354{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002355 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002356 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002357 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002358 sizeof(struct ffa_memory_access);
2359 ffa_memory_handle_t handle = retrieve_request->handle;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002360 struct ffa_memory_region *memory_region;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002361 ffa_memory_access_permissions_t permissions = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002362 uint32_t memory_to_attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002363 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002364 struct ffa_memory_share_state *share_state;
2365 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002366 struct ffa_composite_memory_region *composite;
2367 uint32_t total_length;
2368 uint32_t fragment_length;
J-Alves089004f2022-07-13 14:25:44 +01002369 ffa_vm_id_t receiver_id = to_locked.vm->id;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002370 bool is_send_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002371
2372 dump_share_states();
2373
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002374 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002375 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002376 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002377 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002378 expected_retrieve_request_length,
2379 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002380 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002381 }
2382
2383 share_states = share_states_lock();
2384 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002385 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002386 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002387 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002388 goto out;
2389 }
2390
J-Alves96de29f2022-04-26 16:05:24 +01002391 if (!share_state->sending_complete) {
2392 dlog_verbose(
2393 "Memory with handle %#x not fully sent, can't "
2394 "retrieve.\n",
2395 handle);
2396 ret = ffa_error(FFA_INVALID_PARAMETERS);
2397 goto out;
2398 }
2399
Andrew Walbrana65a1322020-04-06 19:32:32 +01002400 memory_region = share_state->memory_region;
J-Alves089004f2022-07-13 14:25:44 +01002401
Andrew Walbrana65a1322020-04-06 19:32:32 +01002402 CHECK(memory_region != NULL);
2403
J-Alves089004f2022-07-13 14:25:44 +01002404 if (retrieve_request->sender != memory_region->sender) {
2405 dlog_verbose(
2406 "Memory with handle %#x not fully sent, can't "
2407 "retrieve.\n",
2408 handle);
2409 ret = ffa_error(FFA_INVALID_PARAMETERS);
2410 goto out;
2411 }
J-Alves96de29f2022-04-26 16:05:24 +01002412
J-Alvesa9cd7e32022-07-01 13:49:33 +01002413 if (!is_ffa_memory_retrieve_borrower_request(retrieve_request,
2414 to_locked)) {
2415 uint32_t receiver_index;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002416
J-Alvesb5084cf2022-07-06 14:20:12 +01002417 /*
2418 * The SPMC can only process retrieve requests to memory share
2419 * operations with one borrower from the other world. It can't
2420 * determine the ID of the NWd VM that invoked the retrieve
2421 * request interface call. It relies on the hypervisor to
2422 * validate the caller's ID against that provided in the
2423 * `receivers` list of the retrieve response.
2424 * In case there is only one borrower from the NWd in the
2425 * transaction descriptor, record that in the `receiver_id` for
2426 * later use, and validate in the retrieve request message.
2427 */
2428 if (to_locked.vm->id == HF_HYPERVISOR_VM_ID) {
2429 uint32_t other_world_count = 0;
2430
2431 for (uint32_t i = 0; i < memory_region->receiver_count;
2432 i++) {
2433 receiver_id =
2434 retrieve_request->receivers[0]
2435 .receiver_permissions.receiver;
2436 if (!vm_id_is_current_world(receiver_id)) {
2437 other_world_count++;
2438 }
2439 }
2440 if (other_world_count > 1) {
2441 dlog_verbose(
2442 "Support one receiver from the other "
2443 "world.\n");
2444 return ffa_error(FFA_NOT_SUPPORTED);
2445 }
2446 }
2447
2448 /*
2449 * Validate retrieve request, according to what was sent by the
2450 * sender. Function will output the `receiver_index` from the
2451 * provided memory region, and will output `permissions` from
2452 * the validated requested permissions.
2453 */
J-Alves089004f2022-07-13 14:25:44 +01002454 ret = ffa_memory_retrieve_validate(
2455 receiver_id, retrieve_request, memory_region,
2456 &receiver_index, share_state->share_func);
2457 if (ret.func != FFA_SUCCESS_32) {
J-Alvesa9cd7e32022-07-01 13:49:33 +01002458 goto out;
2459 }
2460
2461 if (share_state->retrieved_fragment_count[receiver_index] !=
2462 0U) {
2463 dlog_verbose(
2464 "Memory with handle %#x already retrieved.\n",
2465 handle);
2466 ret = ffa_error(FFA_DENIED);
2467 goto out;
2468 }
2469
J-Alvesa9cd7e32022-07-01 13:49:33 +01002470 ret = ffa_memory_retrieve_validate_memory_access_list(
2471 memory_region, retrieve_request, receiver_id,
2472 &permissions);
J-Alves614d9f42022-06-28 14:03:10 +01002473 if (ret.func != FFA_SUCCESS_32) {
2474 goto out;
2475 }
Federico Recanatia98603a2021-12-20 18:04:03 +01002476
J-Alvesa9cd7e32022-07-01 13:49:33 +01002477 memory_to_attributes = ffa_memory_permissions_to_mode(
2478 permissions, share_state->sender_orig_mode);
J-Alves40e260e2022-09-22 17:52:43 +01002479
2480 if (to_locked.vm->el0_partition) {
2481 /*
2482 * Get extra mapping attributes for the given VM ID.
2483 * If the memory is shared by a VM executing in non
2484 * secure world, attribute MM_MODE_NS has to be set
2485 * while mapping that in a SP executing in secure world.
2486 */
2487 memory_to_attributes |=
2488 arch_mm_extra_attributes_from_vm(
2489 retrieve_request->sender);
2490 }
2491
J-Alvesa9cd7e32022-07-01 13:49:33 +01002492 ret = ffa_retrieve_check_update(
2493 to_locked, memory_region->sender,
2494 share_state->fragments,
2495 share_state->fragment_constituent_counts,
2496 share_state->fragment_count, memory_to_attributes,
2497 share_state->share_func, false, page_pool);
2498
2499 if (ret.func != FFA_SUCCESS_32) {
2500 goto out;
2501 }
2502
2503 share_state->retrieved_fragment_count[receiver_index] = 1;
2504 is_send_complete =
2505 share_state->retrieved_fragment_count[receiver_index] ==
2506 share_state->fragment_count;
J-Alves3c5b2072022-11-21 12:45:40 +00002507
2508 share_state->clear_after_relinquish =
2509 (retrieve_request->flags &
2510 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH) != 0U;
2511
J-Alvesa9cd7e32022-07-01 13:49:33 +01002512 } else {
2513 if (share_state->hypervisor_fragment_count != 0U) {
2514 dlog_verbose(
J-Alvesb5084cf2022-07-06 14:20:12 +01002515 "Memory with handle %#x already retrieved by "
J-Alvesa9cd7e32022-07-01 13:49:33 +01002516 "the hypervisor.\n",
2517 handle);
2518 ret = ffa_error(FFA_DENIED);
2519 goto out;
2520 }
2521
2522 share_state->hypervisor_fragment_count = 1;
2523
2524 ffa_memory_retrieve_complete_from_hyp(share_state);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002525 }
2526
J-Alvesb5084cf2022-07-06 14:20:12 +01002527 /* VMs acquire the RX buffer from SPMC. */
2528 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
2529
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002530 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002531 * Copy response to RX buffer of caller and deliver the message.
2532 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002533 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002534 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002535 composite = ffa_memory_region_get_composite(memory_region, 0);
2536 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002537 * Constituents which we received in the first fragment should
2538 * always fit in the first fragment we are sending, because the
2539 * header is the same size in both cases and we have a fixed
2540 * message buffer size. So `ffa_retrieved_memory_region_init`
2541 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01002542 */
2543 CHECK(ffa_retrieved_memory_region_init(
J-Alves2d8457f2022-10-05 11:06:41 +01002544 to_locked.vm->mailbox.recv, to_locked.vm->ffa_version,
2545 HF_MAILBOX_SIZE, memory_region->sender,
2546 memory_region->attributes, memory_region->flags, handle,
2547 receiver_id, permissions, composite->page_count,
2548 composite->constituent_count, share_state->fragments[0],
Andrew Walbranca808b12020-05-15 17:22:28 +01002549 share_state->fragment_constituent_counts[0], &total_length,
2550 &fragment_length));
J-Alvesb5084cf2022-07-06 14:20:12 +01002551
Andrew Walbranca808b12020-05-15 17:22:28 +01002552 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002553 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002554 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00002555 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002556
J-Alvesa9cd7e32022-07-01 13:49:33 +01002557 if (is_send_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002558 ffa_memory_retrieve_complete(share_states, share_state,
2559 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002560 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002561 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002562 .arg1 = total_length,
2563 .arg2 = fragment_length};
Andrew Walbranca808b12020-05-15 17:22:28 +01002564out:
2565 share_states_unlock(&share_states);
2566 dump_share_states();
2567 return ret;
2568}
2569
J-Alves5da37d92022-10-24 16:33:48 +01002570/**
2571 * Determine expected fragment offset according to the FF-A version of
2572 * the caller.
2573 */
2574static uint32_t ffa_memory_retrieve_expected_offset_per_ffa_version(
2575 struct ffa_memory_region *memory_region,
2576 uint32_t retrieved_constituents_count, uint32_t ffa_version)
2577{
2578 uint32_t expected_fragment_offset;
2579 uint32_t composite_constituents_offset;
2580
2581 if (ffa_version == MAKE_FFA_VERSION(1, 1)) {
2582 /*
2583 * Hafnium operates memory regions in FF-A v1.1 format, so we
2584 * can retrieve the constituents offset from descriptor.
2585 */
2586 composite_constituents_offset =
2587 ffa_composite_constituent_offset(memory_region, 0);
2588 } else if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
2589 /*
2590 * If retriever is FF-A v1.0, determine the composite offset
2591 * as it is expected to have been configured in the
2592 * retrieve response.
2593 */
2594 composite_constituents_offset =
2595 sizeof(struct ffa_memory_region_v1_0) +
2596 RECEIVERS_COUNT_IN_RETRIEVE_RESP *
2597 sizeof(struct ffa_memory_access) +
2598 sizeof(struct ffa_composite_memory_region);
2599 } else {
2600 panic("%s received an invalid FF-A version.\n", __func__);
2601 }
2602
2603 expected_fragment_offset =
2604 composite_constituents_offset +
2605 retrieved_constituents_count *
2606 sizeof(struct ffa_memory_region_constituent) -
2607 sizeof(struct ffa_memory_access) *
2608 (memory_region->receiver_count - 1);
2609
2610 return expected_fragment_offset;
2611}
2612
Andrew Walbranca808b12020-05-15 17:22:28 +01002613struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2614 ffa_memory_handle_t handle,
2615 uint32_t fragment_offset,
J-Alves59ed0042022-07-28 18:26:41 +01002616 ffa_vm_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01002617 struct mpool *page_pool)
2618{
2619 struct ffa_memory_region *memory_region;
2620 struct share_states_locked share_states;
2621 struct ffa_memory_share_state *share_state;
2622 struct ffa_value ret;
2623 uint32_t fragment_index;
2624 uint32_t retrieved_constituents_count;
2625 uint32_t i;
2626 uint32_t expected_fragment_offset;
2627 uint32_t remaining_constituent_count;
2628 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01002629 uint32_t receiver_index;
J-Alves59ed0042022-07-28 18:26:41 +01002630 bool continue_ffa_hyp_mem_retrieve_req;
Andrew Walbranca808b12020-05-15 17:22:28 +01002631
2632 dump_share_states();
2633
2634 share_states = share_states_lock();
2635 if (!get_share_state(share_states, handle, &share_state)) {
2636 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2637 handle);
2638 ret = ffa_error(FFA_INVALID_PARAMETERS);
2639 goto out;
2640 }
2641
2642 memory_region = share_state->memory_region;
2643 CHECK(memory_region != NULL);
2644
Andrew Walbranca808b12020-05-15 17:22:28 +01002645 if (!share_state->sending_complete) {
2646 dlog_verbose(
2647 "Memory with handle %#x not fully sent, can't "
2648 "retrieve.\n",
2649 handle);
2650 ret = ffa_error(FFA_INVALID_PARAMETERS);
2651 goto out;
2652 }
2653
J-Alves59ed0042022-07-28 18:26:41 +01002654 /*
2655 * If retrieve request from the hypervisor has been initiated in the
2656 * given share_state, continue it, else assume it is a continuation of
2657 * retrieve request from a NWd VM.
2658 */
2659 continue_ffa_hyp_mem_retrieve_req =
2660 (to_locked.vm->id == HF_HYPERVISOR_VM_ID) &&
2661 (share_state->hypervisor_fragment_count != 0U) &&
2662 plat_ffa_is_vm_id(sender_vm_id);
Andrew Walbranca808b12020-05-15 17:22:28 +01002663
J-Alves59ed0042022-07-28 18:26:41 +01002664 if (!continue_ffa_hyp_mem_retrieve_req) {
2665 receiver_index = ffa_memory_region_get_receiver(
2666 memory_region, to_locked.vm->id);
2667
2668 if (receiver_index == memory_region->receiver_count) {
2669 dlog_verbose(
2670 "Caller of FFA_MEM_FRAG_RX (%x) is not a "
2671 "borrower to memory sharing transaction (%x)\n",
2672 to_locked.vm->id, handle);
2673 ret = ffa_error(FFA_INVALID_PARAMETERS);
2674 goto out;
2675 }
2676
2677 if (share_state->retrieved_fragment_count[receiver_index] ==
2678 0 ||
2679 share_state->retrieved_fragment_count[receiver_index] >=
2680 share_state->fragment_count) {
2681 dlog_verbose(
2682 "Retrieval of memory with handle %#x not yet "
2683 "started or already completed (%d/%d fragments "
2684 "retrieved).\n",
2685 handle,
2686 share_state->retrieved_fragment_count
2687 [receiver_index],
2688 share_state->fragment_count);
2689 ret = ffa_error(FFA_INVALID_PARAMETERS);
2690 goto out;
2691 }
2692
2693 fragment_index =
2694 share_state->retrieved_fragment_count[receiver_index];
2695 } else {
2696 if (share_state->hypervisor_fragment_count == 0 ||
2697 share_state->hypervisor_fragment_count >=
2698 share_state->fragment_count) {
2699 dlog_verbose(
2700 "Retrieve of memory with handle %x not "
2701 "started from hypervisor.\n",
2702 handle);
2703 ret = ffa_error(FFA_INVALID_PARAMETERS);
2704 goto out;
2705 }
2706
2707 if (memory_region->sender != sender_vm_id) {
2708 dlog_verbose(
2709 "Sender ID (%x) is not as expected for memory "
2710 "handle %x\n",
2711 sender_vm_id, handle);
2712 ret = ffa_error(FFA_INVALID_PARAMETERS);
2713 goto out;
2714 }
2715
2716 fragment_index = share_state->hypervisor_fragment_count;
2717
2718 receiver_index = 0;
2719 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002720
2721 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002722 * Check that the given fragment offset is correct by counting
2723 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01002724 */
2725 retrieved_constituents_count = 0;
2726 for (i = 0; i < fragment_index; ++i) {
2727 retrieved_constituents_count +=
2728 share_state->fragment_constituent_counts[i];
2729 }
J-Alvesc7484f12022-05-13 12:41:14 +01002730
2731 CHECK(memory_region->receiver_count > 0);
2732
Andrew Walbranca808b12020-05-15 17:22:28 +01002733 expected_fragment_offset =
J-Alves5da37d92022-10-24 16:33:48 +01002734 ffa_memory_retrieve_expected_offset_per_ffa_version(
2735 memory_region, retrieved_constituents_count,
2736 to_locked.vm->ffa_version);
2737
Andrew Walbranca808b12020-05-15 17:22:28 +01002738 if (fragment_offset != expected_fragment_offset) {
2739 dlog_verbose("Fragment offset was %d but expected %d.\n",
2740 fragment_offset, expected_fragment_offset);
2741 ret = ffa_error(FFA_INVALID_PARAMETERS);
2742 goto out;
2743 }
2744
J-Alves59ed0042022-07-28 18:26:41 +01002745 /* VMs acquire the RX buffer from SPMC. */
2746 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
2747
Andrew Walbranca808b12020-05-15 17:22:28 +01002748 remaining_constituent_count = ffa_memory_fragment_init(
2749 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2750 share_state->fragments[fragment_index],
2751 share_state->fragment_constituent_counts[fragment_index],
2752 &fragment_length);
2753 CHECK(remaining_constituent_count == 0);
2754 to_locked.vm->mailbox.recv_size = fragment_length;
2755 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2756 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00002757 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbranca808b12020-05-15 17:22:28 +01002758
J-Alves59ed0042022-07-28 18:26:41 +01002759 if (!continue_ffa_hyp_mem_retrieve_req) {
2760 share_state->retrieved_fragment_count[receiver_index]++;
2761 if (share_state->retrieved_fragment_count[receiver_index] ==
2762 share_state->fragment_count) {
2763 ffa_memory_retrieve_complete(share_states, share_state,
2764 page_pool);
2765 }
2766 } else {
2767 share_state->hypervisor_fragment_count++;
2768
2769 ffa_memory_retrieve_complete_from_hyp(share_state);
2770 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002771 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2772 .arg1 = (uint32_t)handle,
2773 .arg2 = (uint32_t)(handle >> 32),
2774 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002775
2776out:
2777 share_states_unlock(&share_states);
2778 dump_share_states();
2779 return ret;
2780}
2781
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002782struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002783 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002784 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002785{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002786 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002787 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002788 struct ffa_memory_share_state *share_state;
2789 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002790 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002791 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01002792 uint32_t receiver_index;
J-Alves3c5b2072022-11-21 12:45:40 +00002793 bool receivers_relinquished_memory;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002794
Andrew Walbrana65a1322020-04-06 19:32:32 +01002795 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002796 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002797 "Stream endpoints not supported (got %d "
J-Alves668a86e2023-05-10 11:53:25 +01002798 "endpoints on FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002799 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002800 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002801 }
2802
Andrew Walbrana65a1322020-04-06 19:32:32 +01002803 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002804 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002805 "VM ID %d in relinquish message doesn't match "
J-Alves668a86e2023-05-10 11:53:25 +01002806 "calling VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002807 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002808 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002809 }
2810
2811 dump_share_states();
2812
2813 share_states = share_states_lock();
2814 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002815 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002816 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002817 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002818 goto out;
2819 }
2820
Andrew Walbranca808b12020-05-15 17:22:28 +01002821 if (!share_state->sending_complete) {
2822 dlog_verbose(
2823 "Memory with handle %#x not fully sent, can't "
2824 "relinquish.\n",
2825 handle);
2826 ret = ffa_error(FFA_INVALID_PARAMETERS);
2827 goto out;
2828 }
2829
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002830 memory_region = share_state->memory_region;
2831 CHECK(memory_region != NULL);
2832
J-Alves8eb19162022-04-28 10:56:48 +01002833 receiver_index = ffa_memory_region_get_receiver(memory_region,
2834 from_locked.vm->id);
2835
2836 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002837 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002838 "VM ID %d tried to relinquish memory region "
J-Alves668a86e2023-05-10 11:53:25 +01002839 "with handle %#x and it is not a valid borrower.\n",
J-Alves8eb19162022-04-28 10:56:48 +01002840 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002841 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002842 goto out;
2843 }
2844
J-Alves8eb19162022-04-28 10:56:48 +01002845 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01002846 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002847 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002848 "Memory with handle %#x not yet fully "
2849 "retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01002850 "receiver %x can't relinquish.\n",
2851 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002852 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002853 goto out;
2854 }
2855
J-Alves3c5b2072022-11-21 12:45:40 +00002856 /*
2857 * Either clear if requested in relinquish call, or in a retrieve
2858 * request from one of the borrowers.
2859 */
2860 receivers_relinquished_memory = true;
2861
2862 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
2863 struct ffa_memory_access *receiver =
2864 &memory_region->receivers[i];
2865
2866 if (receiver->receiver_permissions.receiver ==
2867 from_locked.vm->id) {
2868 continue;
2869 }
2870
2871 if (share_state->retrieved_fragment_count[i] != 0U) {
2872 receivers_relinquished_memory = false;
2873 break;
2874 }
2875 }
2876
2877 clear = receivers_relinquished_memory &&
2878 (share_state->clear_after_relinquish ||
2879 (relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2880 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002881
2882 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002883 * Clear is not allowed for memory that was shared, as the
2884 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002885 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002886 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002887 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002888 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002889 goto out;
2890 }
2891
Andrew Walbranca808b12020-05-15 17:22:28 +01002892 ret = ffa_relinquish_check_update(
J-Alves3c5b2072022-11-21 12:45:40 +00002893 from_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002894 share_state->fragment_constituent_counts,
2895 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002896
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002897 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002898 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002899 * Mark memory handle as not retrieved, so it can be
2900 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002901 */
J-Alves8eb19162022-04-28 10:56:48 +01002902 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002903 }
2904
2905out:
2906 share_states_unlock(&share_states);
2907 dump_share_states();
2908 return ret;
2909}
2910
2911/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01002912 * Validates that the reclaim transition is allowed for the given
2913 * handle, updates the page table of the reclaiming VM, and frees the
2914 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002915 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002916struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002917 ffa_memory_handle_t handle,
2918 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002919 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002920{
2921 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002922 struct ffa_memory_share_state *share_state;
2923 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002924 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002925
2926 dump_share_states();
2927
2928 share_states = share_states_lock();
J-Alvesb5084cf2022-07-06 14:20:12 +01002929 if (get_share_state(share_states, handle, &share_state)) {
2930 memory_region = share_state->memory_region;
2931 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002932 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002933 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002934 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002935 goto out;
2936 }
2937
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002938 CHECK(memory_region != NULL);
2939
J-Alvesa9cd7e32022-07-01 13:49:33 +01002940 if (vm_id_is_current_world(to_locked.vm->id) &&
2941 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002942 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002943 "VM %#x attempted to reclaim memory handle %#x "
2944 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002945 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002946 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002947 goto out;
2948 }
2949
Andrew Walbranca808b12020-05-15 17:22:28 +01002950 if (!share_state->sending_complete) {
2951 dlog_verbose(
2952 "Memory with handle %#x not fully sent, can't "
2953 "reclaim.\n",
2954 handle);
2955 ret = ffa_error(FFA_INVALID_PARAMETERS);
2956 goto out;
2957 }
2958
J-Alves752236c2022-04-28 11:07:47 +01002959 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
2960 if (share_state->retrieved_fragment_count[i] != 0) {
2961 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002962 "Tried to reclaim memory handle %#x "
J-Alves3c5b2072022-11-21 12:45:40 +00002963 "that has not been relinquished by all "
J-Alvesa9cd7e32022-07-01 13:49:33 +01002964 "borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01002965 handle,
2966 memory_region->receivers[i]
2967 .receiver_permissions.receiver);
2968 ret = ffa_error(FFA_DENIED);
2969 goto out;
2970 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002971 }
2972
Andrew Walbranca808b12020-05-15 17:22:28 +01002973 ret = ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00002974 to_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002975 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00002976 share_state->fragment_count, share_state->sender_orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01002977 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002978
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002979 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002980 share_state_free(share_states, share_state, page_pool);
J-Alves3c5b2072022-11-21 12:45:40 +00002981 dlog_verbose("Freed share state after successful reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002982 }
2983
2984out:
2985 share_states_unlock(&share_states);
2986 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002987}