blob: 6c11a8ef8a4ed9da099884f4d85dcf82148d8e59 [file] [log] [blame]
Jose Marinho75509b42019-04-09 09:34:59 +01001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Jose Marinho75509b42019-04-09 09:34:59 +01007 */
8
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01009#include "hf/ffa_memory.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000010
Federico Recanati4fd065d2021-12-13 20:06:23 +010011#include "hf/arch/mm.h"
Olivier Deprez112d2b52020-09-30 07:39:23 +020012#include "hf/arch/other_world.h"
Olivier Deprez55a189e2021-06-09 15:45:27 +020013#include "hf/arch/plat/ffa.h"
Andrew Walbran290b0c92020-02-03 16:37:14 +000014
Jose Marinho75509b42019-04-09 09:34:59 +010015#include "hf/api.h"
Daniel Boulbya2f8c662021-11-26 17:52:53 +000016#include "hf/assert.h"
Jose Marinho09b1db82019-08-08 09:16:59 +010017#include "hf/check.h"
Jose Marinho75509b42019-04-09 09:34:59 +010018#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010019#include "hf/ffa_internal.h"
J-Alves66652252022-07-06 09:49:51 +010020#include "hf/ffa_memory_internal.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000021#include "hf/mpool.h"
Jose Marinho75509b42019-04-09 09:34:59 +010022#include "hf/std.h"
Andrew Scull3c257452019-11-26 13:32:50 +000023#include "hf/vm.h"
Jose Marinho75509b42019-04-09 09:34:59 +010024
J-Alves2d8457f2022-10-05 11:06:41 +010025#include "vmapi/hf/ffa_v1_0.h"
26
J-Alves5da37d92022-10-24 16:33:48 +010027#define RECEIVERS_COUNT_IN_RETRIEVE_RESP 1
28
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000029/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010030 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000031 * by this lock.
32 */
33static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010034static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000035
36/**
J-Alves917d2f22020-10-30 18:39:30 +000037 * Extracts the index from a memory handle allocated by Hafnium's current world.
38 */
39uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle)
40{
41 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
42}
43
44/**
Andrew Walbranca808b12020-05-15 17:22:28 +010045 * Initialises the next available `struct ffa_memory_share_state` and sets
46 * `share_state_ret` to a pointer to it. If `handle` is
47 * `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle, otherwise
48 * uses the provided handle which is assumed to be globally unique.
49 *
50 * Returns true on success or false if none are available.
51 */
J-Alves66652252022-07-06 09:49:51 +010052bool allocate_share_state(struct share_states_locked share_states,
53 uint32_t share_func,
54 struct ffa_memory_region *memory_region,
55 uint32_t fragment_length, ffa_memory_handle_t handle,
56 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000057{
Andrew Walbrana65a1322020-04-06 19:32:32 +010058 uint64_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000059
Daniel Boulbya2f8c662021-11-26 17:52:53 +000060 assert(share_states.share_states != NULL);
61 assert(memory_region != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000062
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000063 for (i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +010064 if (share_states.share_states[i].share_func == 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000065 uint32_t j;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010066 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +010067 &share_states.share_states[i];
68 struct ffa_composite_memory_region *composite =
69 ffa_memory_region_get_composite(memory_region,
70 0);
71
72 if (handle == FFA_MEMORY_HANDLE_INVALID) {
J-Alvesee68c542020-10-29 17:48:20 +000073 memory_region->handle =
Olivier Deprez55a189e2021-06-09 15:45:27 +020074 plat_ffa_memory_handle_make(i);
Andrew Walbranca808b12020-05-15 17:22:28 +010075 } else {
J-Alvesee68c542020-10-29 17:48:20 +000076 memory_region->handle = handle;
Andrew Walbranca808b12020-05-15 17:22:28 +010077 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000078 allocated_state->share_func = share_func;
79 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +010080 allocated_state->fragment_count = 1;
81 allocated_state->fragments[0] = composite->constituents;
82 allocated_state->fragment_constituent_counts[0] =
83 (fragment_length -
84 ffa_composite_constituent_offset(memory_region,
85 0)) /
86 sizeof(struct ffa_memory_region_constituent);
87 allocated_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000088 for (j = 0; j < MAX_MEM_SHARE_RECIPIENTS; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +010089 allocated_state->retrieved_fragment_count[j] =
90 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000091 }
Andrew Walbranca808b12020-05-15 17:22:28 +010092 if (share_state_ret != NULL) {
93 *share_state_ret = allocated_state;
94 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000095 return true;
96 }
97 }
98
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000099 return false;
100}
101
102/** Locks the share states lock. */
103struct share_states_locked share_states_lock(void)
104{
105 sl_lock(&share_states_lock_instance);
106
107 return (struct share_states_locked){.share_states = share_states};
108}
109
110/** Unlocks the share states lock. */
J-Alves66652252022-07-06 09:49:51 +0100111void share_states_unlock(struct share_states_locked *share_states)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000112{
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000113 assert(share_states->share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000114 share_states->share_states = NULL;
115 sl_unlock(&share_states_lock_instance);
116}
117
118/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100119 * If the given handle is a valid handle for an allocated share state then
120 * initialises `share_state_ret` to point to the share state and returns true.
121 * Otherwise returns false.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000122 */
J-Alvesfdd29272022-07-19 13:16:31 +0100123bool get_share_state(struct share_states_locked share_states,
124 ffa_memory_handle_t handle,
125 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000126{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100127 struct ffa_memory_share_state *share_state;
J-Alves917d2f22020-10-30 18:39:30 +0000128 uint64_t index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000129
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000130 assert(share_states.share_states != NULL);
131 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100132
133 /*
134 * First look for a share_state allocated by us, in which case the
135 * handle is based on the index.
136 */
Olivier Deprez55a189e2021-06-09 15:45:27 +0200137 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
J-Alves917d2f22020-10-30 18:39:30 +0000138 index = ffa_memory_handle_get_index(handle);
Andrew Walbranca808b12020-05-15 17:22:28 +0100139 if (index < MAX_MEM_SHARES) {
140 share_state = &share_states.share_states[index];
141 if (share_state->share_func != 0) {
142 *share_state_ret = share_state;
143 return true;
144 }
145 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000146 }
147
Andrew Walbranca808b12020-05-15 17:22:28 +0100148 /* Fall back to a linear scan. */
149 for (index = 0; index < MAX_MEM_SHARES; ++index) {
150 share_state = &share_states.share_states[index];
J-Alvesee68c542020-10-29 17:48:20 +0000151 if (share_state->memory_region != NULL &&
152 share_state->memory_region->handle == handle &&
Andrew Walbranca808b12020-05-15 17:22:28 +0100153 share_state->share_func != 0) {
154 *share_state_ret = share_state;
155 return true;
156 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000157 }
158
Andrew Walbranca808b12020-05-15 17:22:28 +0100159 return false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000160}
161
162/** Marks a share state as unallocated. */
J-Alvesfdd29272022-07-19 13:16:31 +0100163void share_state_free(struct share_states_locked share_states,
164 struct ffa_memory_share_state *share_state,
165 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000166{
Andrew Walbranca808b12020-05-15 17:22:28 +0100167 uint32_t i;
168
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000169 assert(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000170 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100171 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000172 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100173 /*
174 * First fragment is part of the same page as the `memory_region`, so it
175 * doesn't need to be freed separately.
176 */
177 share_state->fragments[0] = NULL;
178 share_state->fragment_constituent_counts[0] = 0;
179 for (i = 1; i < share_state->fragment_count; ++i) {
180 mpool_free(page_pool, share_state->fragments[i]);
181 share_state->fragments[i] = NULL;
182 share_state->fragment_constituent_counts[i] = 0;
183 }
184 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000185 share_state->memory_region = NULL;
J-Alvesa9cd7e32022-07-01 13:49:33 +0100186 share_state->hypervisor_fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000187}
188
Andrew Walbranca808b12020-05-15 17:22:28 +0100189/** Checks whether the given share state has been fully sent. */
J-Alvesfdd29272022-07-19 13:16:31 +0100190bool share_state_sending_complete(struct share_states_locked share_states,
191 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000192{
Andrew Walbranca808b12020-05-15 17:22:28 +0100193 struct ffa_composite_memory_region *composite;
194 uint32_t expected_constituent_count;
195 uint32_t fragment_constituent_count_total = 0;
196 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000197
Andrew Walbranca808b12020-05-15 17:22:28 +0100198 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000199 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100200
201 /*
202 * Share state must already be valid, or it's not possible to get hold
203 * of it.
204 */
205 CHECK(share_state->memory_region != NULL &&
206 share_state->share_func != 0);
207
208 composite =
209 ffa_memory_region_get_composite(share_state->memory_region, 0);
210 expected_constituent_count = composite->constituent_count;
211 for (i = 0; i < share_state->fragment_count; ++i) {
212 fragment_constituent_count_total +=
213 share_state->fragment_constituent_counts[i];
214 }
215 dlog_verbose(
216 "Checking completion: constituent count %d/%d from %d "
217 "fragments.\n",
218 fragment_constituent_count_total, expected_constituent_count,
219 share_state->fragment_count);
220
221 return fragment_constituent_count_total == expected_constituent_count;
222}
223
224/**
225 * Calculates the offset of the next fragment expected for the given share
226 * state.
227 */
J-Alvesfdd29272022-07-19 13:16:31 +0100228uint32_t share_state_next_fragment_offset(
Andrew Walbranca808b12020-05-15 17:22:28 +0100229 struct share_states_locked share_states,
230 struct ffa_memory_share_state *share_state)
231{
232 uint32_t next_fragment_offset;
233 uint32_t i;
234
235 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000236 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100237
238 next_fragment_offset =
239 ffa_composite_constituent_offset(share_state->memory_region, 0);
240 for (i = 0; i < share_state->fragment_count; ++i) {
241 next_fragment_offset +=
242 share_state->fragment_constituent_counts[i] *
243 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000244 }
245
Andrew Walbranca808b12020-05-15 17:22:28 +0100246 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000247}
248
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100249static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000250{
251 uint32_t i;
252
253 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
254 return;
255 }
256
Olivier Deprez935e1b12020-12-22 18:01:29 +0100257 dlog("from VM %#x, attributes %#x, flags %#x, tag %u, to "
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100258 "%u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100259 "recipients [",
260 memory_region->sender, memory_region->attributes,
Olivier Deprez935e1b12020-12-22 18:01:29 +0100261 memory_region->flags, memory_region->tag,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100262 memory_region->receiver_count);
263 for (i = 0; i < memory_region->receiver_count; ++i) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000264 if (i != 0) {
265 dlog(", ");
266 }
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100267 dlog("VM %#x: %#x (offset %u)",
Andrew Walbrana65a1322020-04-06 19:32:32 +0100268 memory_region->receivers[i].receiver_permissions.receiver,
269 memory_region->receivers[i]
270 .receiver_permissions.permissions,
271 memory_region->receivers[i]
272 .composite_memory_region_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000273 }
274 dlog("]");
275}
276
J-Alves66652252022-07-06 09:49:51 +0100277void dump_share_states(void)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000278{
279 uint32_t i;
280
281 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
282 return;
283 }
284
285 dlog("Current share states:\n");
286 sl_lock(&share_states_lock_instance);
287 for (i = 0; i < MAX_MEM_SHARES; ++i) {
288 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000289 switch (share_states[i].share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100290 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000291 dlog("SHARE");
292 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100293 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000294 dlog("LEND");
295 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100296 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000297 dlog("DONATE");
298 break;
299 default:
300 dlog("invalid share_func %#x",
301 share_states[i].share_func);
302 }
Olivier Deprez935e1b12020-12-22 18:01:29 +0100303 dlog(" %#x (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000304 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100305 if (share_states[i].sending_complete) {
306 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000307 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100308 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000309 }
J-Alves2a0d2882020-10-29 14:49:50 +0000310 dlog(" with %d fragments, %d retrieved, "
311 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100312 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000313 share_states[i].retrieved_fragment_count[0],
314 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000315 }
316 }
317 sl_unlock(&share_states_lock_instance);
318}
319
Andrew Walbran475c1452020-02-07 13:22:22 +0000320/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100321static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100322 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000323{
324 uint32_t mode = 0;
325
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100326 switch (ffa_get_data_access_attr(permissions)) {
327 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000328 mode = MM_MODE_R;
329 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100330 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000331 mode = MM_MODE_R | MM_MODE_W;
332 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100333 case FFA_DATA_ACCESS_NOT_SPECIFIED:
334 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
335 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100336 case FFA_DATA_ACCESS_RESERVED:
337 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100338 }
339
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100340 switch (ffa_get_instruction_access_attr(permissions)) {
341 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000342 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100343 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100344 mode |= MM_MODE_X;
345 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100346 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
347 mode |= (default_mode & MM_MODE_X);
348 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100349 case FFA_INSTRUCTION_ACCESS_RESERVED:
350 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000351 }
352
353 return mode;
354}
355
Jose Marinho75509b42019-04-09 09:34:59 +0100356/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000357 * Get the current mode in the stage-2 page table of the given vm of all the
358 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100359 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100360 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100361static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000362 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100363 struct ffa_memory_region_constituent **fragments,
364 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100365{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100366 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100367 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100368
Andrew Walbranca808b12020-05-15 17:22:28 +0100369 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100370 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000371 * Fail if there are no constituents. Otherwise we would get an
372 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100373 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100374 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100375 }
376
Andrew Walbranca808b12020-05-15 17:22:28 +0100377 for (i = 0; i < fragment_count; ++i) {
378 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
379 ipaddr_t begin = ipa_init(fragments[i][j].address);
380 size_t size = fragments[i][j].page_count * PAGE_SIZE;
381 ipaddr_t end = ipa_add(begin, size);
382 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100383
Andrew Walbranca808b12020-05-15 17:22:28 +0100384 /* Fail if addresses are not page-aligned. */
385 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
386 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
387 return ffa_error(FFA_INVALID_PARAMETERS);
388 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100389
Andrew Walbranca808b12020-05-15 17:22:28 +0100390 /*
391 * Ensure that this constituent memory range is all
392 * mapped with the same mode.
393 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800394 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100395 return ffa_error(FFA_DENIED);
396 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100397
Andrew Walbranca808b12020-05-15 17:22:28 +0100398 /*
399 * Ensure that all constituents are mapped with the same
400 * mode.
401 */
402 if (i == 0) {
403 *orig_mode = current_mode;
404 } else if (current_mode != *orig_mode) {
405 dlog_verbose(
406 "Expected mode %#x but was %#x for %d "
407 "pages at %#x.\n",
408 *orig_mode, current_mode,
409 fragments[i][j].page_count,
410 ipa_addr(begin));
411 return ffa_error(FFA_DENIED);
412 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100413 }
Jose Marinho75509b42019-04-09 09:34:59 +0100414 }
415
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100416 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000417}
418
419/**
420 * Verify that all pages have the same mode, that the starting mode
421 * constitutes a valid state and obtain the next mode to apply
422 * to the sending VM.
423 *
424 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100425 * 1) FFA_DENIED if a state transition was not found;
426 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100427 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100428 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100429 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100430 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
431 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000432 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100433static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100434 struct vm_locked from, uint32_t share_func,
J-Alves363f5722022-04-25 17:37:37 +0100435 struct ffa_memory_access *receivers, uint32_t receivers_count,
436 uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100437 struct ffa_memory_region_constituent **fragments,
438 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
439 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000440{
441 const uint32_t state_mask =
442 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100443 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000444
Andrew Walbranca808b12020-05-15 17:22:28 +0100445 ret = constituents_get_mode(from, orig_from_mode, fragments,
446 fragment_constituent_counts,
447 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100448 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100449 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100450 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100451 }
452
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000453 /* Ensure the address range is normal memory and not a device. */
454 if (*orig_from_mode & MM_MODE_D) {
455 dlog_verbose("Can't share device memory (mode is %#x).\n",
456 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100457 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000458 }
459
460 /*
461 * Ensure the sender is the owner and has exclusive access to the
462 * memory.
463 */
464 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100465 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100466 }
467
J-Alves363f5722022-04-25 17:37:37 +0100468 assert(receivers != NULL && receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100469
J-Alves363f5722022-04-25 17:37:37 +0100470 for (uint32_t i = 0U; i < receivers_count; i++) {
471 ffa_memory_access_permissions_t permissions =
472 receivers[i].receiver_permissions.permissions;
473 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
474 permissions, *orig_from_mode);
475
476 if ((*orig_from_mode & required_from_mode) !=
477 required_from_mode) {
478 dlog_verbose(
479 "Sender tried to send memory with permissions "
480 "which "
481 "required mode %#x but only had %#x itself.\n",
482 required_from_mode, *orig_from_mode);
483 return ffa_error(FFA_DENIED);
484 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000485 }
486
487 /* Find the appropriate new mode. */
488 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000489 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100490 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000491 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100492 break;
493
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100494 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000495 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100496 break;
497
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100498 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000499 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100500 break;
501
Jose Marinho75509b42019-04-09 09:34:59 +0100502 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100503 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100504 }
505
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100506 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000507}
508
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100509static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000510 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100511 struct ffa_memory_region_constituent **fragments,
512 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
513 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000514{
515 const uint32_t state_mask =
516 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
517 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100518 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000519
Andrew Walbranca808b12020-05-15 17:22:28 +0100520 ret = constituents_get_mode(from, orig_from_mode, fragments,
521 fragment_constituent_counts,
522 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100523 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100524 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000525 }
526
527 /* Ensure the address range is normal memory and not a device. */
528 if (*orig_from_mode & MM_MODE_D) {
529 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
530 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100531 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000532 }
533
534 /*
535 * Ensure the relinquishing VM is not the owner but has access to the
536 * memory.
537 */
538 orig_from_state = *orig_from_mode & state_mask;
539 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
540 dlog_verbose(
541 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100542 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000543 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100544 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000545 }
546
547 /* Find the appropriate new mode. */
548 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
549
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100550 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000551}
552
553/**
554 * Verify that all pages have the same mode, that the starting mode
555 * constitutes a valid state and obtain the next mode to apply
556 * to the retrieving VM.
557 *
558 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100559 * 1) FFA_DENIED if a state transition was not found;
560 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100561 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100562 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100563 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100564 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
565 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000566 */
J-Alvesfc19b372022-07-06 12:17:35 +0100567struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000568 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100569 struct ffa_memory_region_constituent **fragments,
570 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
571 uint32_t memory_to_attributes, uint32_t *to_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000572{
573 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100574 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000575
Andrew Walbranca808b12020-05-15 17:22:28 +0100576 ret = constituents_get_mode(to, &orig_to_mode, fragments,
577 fragment_constituent_counts,
578 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100579 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100580 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100581 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000582 }
583
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100584 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000585 /*
586 * If the original ffa memory send call has been processed
587 * successfully, it is expected the orig_to_mode would overlay
588 * with `state_mask`, as a result of the function
589 * `ffa_send_check_transition`.
590 */
J-Alves59ed0042022-07-28 18:26:41 +0100591 if (vm_id_is_current_world(to.vm->id)) {
592 assert((orig_to_mode &
593 (MM_MODE_INVALID | MM_MODE_UNOWNED |
594 MM_MODE_SHARED)) != 0U);
595 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000596 } else {
597 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +0100598 * If the retriever is from virtual FF-A instance:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000599 * Ensure the retriever has the expected state. We don't care
600 * about the MM_MODE_SHARED bit; either with or without it set
601 * are both valid representations of the !O-NA state.
602 */
J-Alvesa9cd7e32022-07-01 13:49:33 +0100603 if (vm_id_is_current_world(to.vm->id) &&
604 to.vm->id != HF_PRIMARY_VM_ID &&
605 (orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
606 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100607 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000608 }
609 }
610
611 /* Find the appropriate new mode. */
612 *to_mode = memory_to_attributes;
613 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100614 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000615 *to_mode |= 0;
616 break;
617
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100618 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000619 *to_mode |= MM_MODE_UNOWNED;
620 break;
621
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100622 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000623 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
624 break;
625
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100626 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000627 *to_mode |= 0;
628 break;
629
630 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100631 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100632 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000633 }
634
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100635 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100636}
Jose Marinho09b1db82019-08-08 09:16:59 +0100637
638/**
639 * Updates a VM's page table such that the given set of physical address ranges
640 * are mapped in the address space at the corresponding address ranges, in the
641 * mode provided.
642 *
643 * If commit is false, the page tables will be allocated from the mpool but no
644 * mappings will actually be updated. This function must always be called first
645 * with commit false to check that it will succeed before calling with commit
646 * true, to avoid leaving the page table in a half-updated state. To make a
647 * series of changes atomically you can call them all with commit false before
648 * calling them all with commit true.
649 *
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700650 * vm_ptable_defrag should always be called after a series of page table
651 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +0100652 *
653 * Returns true on success, or false if the update failed and no changes were
654 * made to memory mappings.
655 */
J-Alves66652252022-07-06 09:49:51 +0100656bool ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000657 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100658 struct ffa_memory_region_constituent **fragments,
659 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby4dd3f532021-09-21 09:57:08 +0100660 uint32_t mode, struct mpool *ppool, bool commit)
Jose Marinho09b1db82019-08-08 09:16:59 +0100661{
Andrew Walbranca808b12020-05-15 17:22:28 +0100662 uint32_t i;
663 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100664
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700665 if (vm_locked.vm->el0_partition) {
666 mode |= MM_MODE_USER | MM_MODE_NG;
667 }
668
Andrew Walbranca808b12020-05-15 17:22:28 +0100669 /* Iterate over the memory region constituents within each fragment. */
670 for (i = 0; i < fragment_count; ++i) {
671 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
672 size_t size = fragments[i][j].page_count * PAGE_SIZE;
673 paddr_t pa_begin =
674 pa_from_ipa(ipa_init(fragments[i][j].address));
675 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +0200676 uint32_t pa_bits =
677 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +0100678
679 /*
680 * Ensure the requested region falls into system's PA
681 * range.
682 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +0200683 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
684 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +0100685 dlog_error("Region is outside of PA Range\n");
686 return false;
687 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100688
689 if (commit) {
690 vm_identity_commit(vm_locked, pa_begin, pa_end,
691 mode, ppool, NULL);
692 } else if (!vm_identity_prepare(vm_locked, pa_begin,
693 pa_end, mode, ppool)) {
694 return false;
695 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100696 }
697 }
698
699 return true;
700}
701
702/**
703 * Clears a region of physical memory by overwriting it with zeros. The data is
704 * flushed from the cache so the memory has been cleared across the system.
705 */
J-Alves7db32002021-12-14 14:44:50 +0000706static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
707 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +0100708{
709 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000710 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100711 * global mapping of the whole range. Such an approach will limit
712 * the changes to stage-1 tables and will allow only local
713 * invalidation.
714 */
715 bool ret;
716 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +0000717 void *ptr = mm_identity_map(stage1_locked, begin, end,
718 MM_MODE_W | (extra_mode_attributes &
719 plat_ffa_other_world_mode()),
720 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100721 size_t size = pa_difference(begin, end);
722
723 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100724 goto fail;
725 }
726
727 memset_s(ptr, size, 0, size);
728 arch_mm_flush_dcache(ptr, size);
729 mm_unmap(stage1_locked, begin, end, ppool);
730
731 ret = true;
732 goto out;
733
734fail:
735 ret = false;
736
737out:
738 mm_unlock_stage1(&stage1_locked);
739
740 return ret;
741}
742
743/**
744 * Clears a region of physical memory by overwriting it with zeros. The data is
745 * flushed from the cache so the memory has been cleared across the system.
746 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100747static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +0000748 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100749 struct ffa_memory_region_constituent **fragments,
750 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
751 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100752{
753 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +0100754 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100755 bool ret = false;
756
757 /*
758 * Create a local pool so any freed memory can't be used by another
759 * thread. This is to ensure each constituent that is mapped can be
760 * unmapped again afterwards.
761 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000762 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100763
Andrew Walbranca808b12020-05-15 17:22:28 +0100764 /* Iterate over the memory region constituents within each fragment. */
765 for (i = 0; i < fragment_count; ++i) {
766 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100767
Andrew Walbranca808b12020-05-15 17:22:28 +0100768 for (j = 0; j < fragment_constituent_counts[j]; ++j) {
769 size_t size = fragments[i][j].page_count * PAGE_SIZE;
770 paddr_t begin =
771 pa_from_ipa(ipa_init(fragments[i][j].address));
772 paddr_t end = pa_add(begin, size);
773
J-Alves7db32002021-12-14 14:44:50 +0000774 if (!clear_memory(begin, end, &local_page_pool,
775 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100776 /*
777 * api_clear_memory will defrag on failure, so
778 * no need to do it here.
779 */
780 goto out;
781 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100782 }
783 }
784
Jose Marinho09b1db82019-08-08 09:16:59 +0100785 ret = true;
786
787out:
788 mpool_fini(&local_page_pool);
789 return ret;
790}
791
792/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000793 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +0100794 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000795 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +0100796 *
797 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000798 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100799 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +0100800 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100801 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
802 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100803 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +0100804 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100805 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +0100806 */
J-Alves66652252022-07-06 09:49:51 +0100807struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000808 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100809 struct ffa_memory_region_constituent **fragments,
810 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves363f5722022-04-25 17:37:37 +0100811 uint32_t share_func, struct ffa_memory_access *receivers,
812 uint32_t receivers_count, struct mpool *page_pool, bool clear,
813 uint32_t *orig_from_mode_ret)
Jose Marinho09b1db82019-08-08 09:16:59 +0100814{
Andrew Walbranca808b12020-05-15 17:22:28 +0100815 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100816 uint32_t orig_from_mode;
817 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +0100818 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100819 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100820
821 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +0100822 * Make sure constituents are properly aligned to a 64-bit boundary. If
823 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100824 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100825 for (i = 0; i < fragment_count; ++i) {
826 if (!is_aligned(fragments[i], 8)) {
827 dlog_verbose("Constituents not aligned.\n");
828 return ffa_error(FFA_INVALID_PARAMETERS);
829 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100830 }
831
832 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000833 * Check if the state transition is lawful for the sender, ensure that
834 * all constituents of a memory region being shared are at the same
835 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +0100836 */
J-Alves363f5722022-04-25 17:37:37 +0100837 ret = ffa_send_check_transition(from_locked, share_func, receivers,
838 receivers_count, &orig_from_mode,
839 fragments, fragment_constituent_counts,
Andrew Walbranca808b12020-05-15 17:22:28 +0100840 fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100841 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100842 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100843 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100844 }
845
Andrew Walbran37c574e2020-06-03 11:45:46 +0100846 if (orig_from_mode_ret != NULL) {
847 *orig_from_mode_ret = orig_from_mode;
848 }
849
Jose Marinho09b1db82019-08-08 09:16:59 +0100850 /*
851 * Create a local pool so any freed memory can't be used by another
852 * thread. This is to ensure the original mapping can be restored if the
853 * clear fails.
854 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000855 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100856
857 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000858 * First reserve all required memory for the new page table entries
859 * without committing, to make sure the entire operation will succeed
860 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +0100861 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100862 if (!ffa_region_group_identity_map(
863 from_locked, fragments, fragment_constituent_counts,
864 fragment_count, from_mode, page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100865 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100866 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100867 goto out;
868 }
869
870 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000871 * Update the mapping for the sender. This won't allocate because the
872 * transaction was already prepared above, but may free pages in the
873 * case that a whole block is being unmapped that was previously
874 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +0100875 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100876 CHECK(ffa_region_group_identity_map(
877 from_locked, fragments, fragment_constituent_counts,
878 fragment_count, from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100879
880 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +0000881 if (clear &&
882 !ffa_clear_memory_constituents(
883 plat_ffa_owner_world_mode(from_locked.vm->id), fragments,
884 fragment_constituent_counts, fragment_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100885 /*
886 * On failure, roll back by returning memory to the sender. This
887 * may allocate pages which were previously freed into
888 * `local_page_pool` by the call above, but will never allocate
889 * more pages than that so can never fail.
890 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100891 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +0100892 from_locked, fragments, fragment_constituent_counts,
893 fragment_count, orig_from_mode, &local_page_pool,
894 true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100895
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100896 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100897 goto out;
898 }
899
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100900 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000901
902out:
903 mpool_fini(&local_page_pool);
904
905 /*
906 * Tidy up the page table by reclaiming failed mappings (if there was an
907 * error) or merging entries into blocks where possible (on success).
908 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700909 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000910
911 return ret;
912}
913
914/**
915 * Validates and maps memory shared from one VM to another.
916 *
917 * This function requires the calling context to hold the <to> lock.
918 *
919 * Returns:
920 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100921 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000922 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100923 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000924 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100925 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000926 */
J-Alvesb5084cf2022-07-06 14:20:12 +0100927struct ffa_value ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +0000928 struct vm_locked to_locked, ffa_vm_id_t from_id,
Andrew Walbranca808b12020-05-15 17:22:28 +0100929 struct ffa_memory_region_constituent **fragments,
930 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
931 uint32_t memory_to_attributes, uint32_t share_func, bool clear,
932 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000933{
Andrew Walbranca808b12020-05-15 17:22:28 +0100934 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000935 uint32_t to_mode;
936 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100937 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000938
939 /*
Andrew Walbranca808b12020-05-15 17:22:28 +0100940 * Make sure constituents are properly aligned to a 64-bit boundary. If
941 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000942 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100943 for (i = 0; i < fragment_count; ++i) {
944 if (!is_aligned(fragments[i], 8)) {
J-Alvesb5084cf2022-07-06 14:20:12 +0100945 dlog_verbose("Fragment not properly aligned.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +0100946 return ffa_error(FFA_INVALID_PARAMETERS);
947 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000948 }
949
950 /*
951 * Check if the state transition is lawful for the recipient, and ensure
952 * that all constituents of the memory region being retrieved are at the
953 * same state.
954 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100955 ret = ffa_retrieve_check_transition(
956 to_locked, share_func, fragments, fragment_constituent_counts,
957 fragment_count, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100958 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100959 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100960 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000961 }
962
963 /*
964 * Create a local pool so any freed memory can't be used by another
965 * thread. This is to ensure the original mapping can be restored if the
966 * clear fails.
967 */
968 mpool_init_with_fallback(&local_page_pool, page_pool);
969
970 /*
971 * First reserve all required memory for the new page table entries in
972 * the recipient page tables without committing, to make sure the entire
973 * operation will succeed without exhausting the page pool.
974 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100975 if (!ffa_region_group_identity_map(
976 to_locked, fragments, fragment_constituent_counts,
977 fragment_count, to_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000978 /* TODO: partial defrag of failed range. */
979 dlog_verbose(
980 "Insufficient memory to update recipient page "
981 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100982 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000983 goto out;
984 }
985
986 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +0000987 if (clear &&
988 !ffa_clear_memory_constituents(
989 plat_ffa_owner_world_mode(from_id), fragments,
990 fragment_constituent_counts, fragment_count, page_pool)) {
J-Alvesb5084cf2022-07-06 14:20:12 +0100991 dlog_verbose("Couldn't clear constituents.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100992 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000993 goto out;
994 }
995
Jose Marinho09b1db82019-08-08 09:16:59 +0100996 /*
997 * Complete the transfer by mapping the memory into the recipient. This
998 * won't allocate because the transaction was already prepared above, so
999 * it doesn't need to use the `local_page_pool`.
1000 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001001 CHECK(ffa_region_group_identity_map(
1002 to_locked, fragments, fragment_constituent_counts,
1003 fragment_count, to_mode, page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001004
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001005 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001006
1007out:
1008 mpool_fini(&local_page_pool);
1009
1010 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001011 * Tidy up the page table by reclaiming failed mappings (if there was an
1012 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001013 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001014 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001015
1016 return ret;
1017}
1018
Andrew Walbran996d1d12020-05-27 14:08:43 +01001019static struct ffa_value ffa_relinquish_check_update(
J-Alves3c5b2072022-11-21 12:45:40 +00001020 struct vm_locked from_locked, ffa_vm_id_t owner_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01001021 struct ffa_memory_region_constituent **fragments,
1022 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1023 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001024{
1025 uint32_t orig_from_mode;
1026 uint32_t from_mode;
1027 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001028 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001029
Andrew Walbranca808b12020-05-15 17:22:28 +01001030 ret = ffa_relinquish_check_transition(
1031 from_locked, &orig_from_mode, fragments,
1032 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001033 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001034 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001035 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001036 }
1037
1038 /*
1039 * Create a local pool so any freed memory can't be used by another
1040 * thread. This is to ensure the original mapping can be restored if the
1041 * clear fails.
1042 */
1043 mpool_init_with_fallback(&local_page_pool, page_pool);
1044
1045 /*
1046 * First reserve all required memory for the new page table entries
1047 * without committing, to make sure the entire operation will succeed
1048 * without exhausting the page pool.
1049 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001050 if (!ffa_region_group_identity_map(
1051 from_locked, fragments, fragment_constituent_counts,
1052 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001053 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001054 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001055 goto out;
1056 }
1057
1058 /*
1059 * Update the mapping for the sender. This won't allocate because the
1060 * transaction was already prepared above, but may free pages in the
1061 * case that a whole block is being unmapped that was previously
1062 * partially mapped.
1063 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001064 CHECK(ffa_region_group_identity_map(
1065 from_locked, fragments, fragment_constituent_counts,
1066 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001067
1068 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001069 if (clear &&
1070 !ffa_clear_memory_constituents(
J-Alves3c5b2072022-11-21 12:45:40 +00001071 plat_ffa_owner_world_mode(owner_id), fragments,
J-Alves7db32002021-12-14 14:44:50 +00001072 fragment_constituent_counts, fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001073 /*
1074 * On failure, roll back by returning memory to the sender. This
1075 * may allocate pages which were previously freed into
1076 * `local_page_pool` by the call above, but will never allocate
1077 * more pages than that so can never fail.
1078 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001079 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001080 from_locked, fragments, fragment_constituent_counts,
1081 fragment_count, orig_from_mode, &local_page_pool,
1082 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001083
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001084 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001085 goto out;
1086 }
1087
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001088 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001089
1090out:
1091 mpool_fini(&local_page_pool);
1092
1093 /*
1094 * Tidy up the page table by reclaiming failed mappings (if there was an
1095 * error) or merging entries into blocks where possible (on success).
1096 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001097 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001098
1099 return ret;
1100}
1101
1102/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001103 * Complete a memory sending operation by checking that it is valid, updating
1104 * the sender page table, and then either marking the share state as having
1105 * completed sending (on success) or freeing it (on failure).
1106 *
1107 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1108 */
J-Alvesfdd29272022-07-19 13:16:31 +01001109struct ffa_value ffa_memory_send_complete(
Andrew Walbranca808b12020-05-15 17:22:28 +01001110 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001111 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1112 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001113{
1114 struct ffa_memory_region *memory_region = share_state->memory_region;
1115 struct ffa_value ret;
1116
1117 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001118 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001119
1120 /* Check that state is valid in sender page table and update. */
1121 ret = ffa_send_check_update(
1122 from_locked, share_state->fragments,
1123 share_state->fragment_constituent_counts,
1124 share_state->fragment_count, share_state->share_func,
J-Alves363f5722022-04-25 17:37:37 +01001125 memory_region->receivers, memory_region->receiver_count,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001126 page_pool, memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1127 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001128 if (ret.func != FFA_SUCCESS_32) {
1129 /*
1130 * Free share state, it failed to send so it can't be retrieved.
1131 */
1132 dlog_verbose("Complete failed, freeing share state.\n");
1133 share_state_free(share_states, share_state, page_pool);
1134 return ret;
1135 }
1136
1137 share_state->sending_complete = true;
1138 dlog_verbose("Marked sending complete.\n");
1139
J-Alvesee68c542020-10-29 17:48:20 +00001140 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001141}
1142
1143/**
Federico Recanatia98603a2021-12-20 18:04:03 +01001144 * Check that the memory attributes match Hafnium expectations:
1145 * Normal Memory, Inner shareable, Write-Back Read-Allocate
1146 * Write-Allocate Cacheable.
1147 */
1148static struct ffa_value ffa_memory_attributes_validate(
J-Alves7a99d0d2023-02-08 13:49:48 +00001149 ffa_memory_attributes_t attributes)
Federico Recanatia98603a2021-12-20 18:04:03 +01001150{
1151 enum ffa_memory_type memory_type;
1152 enum ffa_memory_cacheability cacheability;
1153 enum ffa_memory_shareability shareability;
1154
1155 memory_type = ffa_get_memory_type_attr(attributes);
1156 if (memory_type != FFA_MEMORY_NORMAL_MEM) {
1157 dlog_verbose("Invalid memory type %#x, expected %#x.\n",
1158 memory_type, FFA_MEMORY_NORMAL_MEM);
Federico Recanati3d953f32022-02-17 09:31:29 +01001159 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001160 }
1161
1162 cacheability = ffa_get_memory_cacheability_attr(attributes);
1163 if (cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1164 dlog_verbose("Invalid cacheability %#x, expected %#x.\n",
1165 cacheability, FFA_MEMORY_CACHE_WRITE_BACK);
Federico Recanati3d953f32022-02-17 09:31:29 +01001166 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001167 }
1168
1169 shareability = ffa_get_memory_shareability_attr(attributes);
1170 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
1171 dlog_verbose("Invalid shareability %#x, expected #%x.\n",
1172 shareability, FFA_MEMORY_INNER_SHAREABLE);
Federico Recanati3d953f32022-02-17 09:31:29 +01001173 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001174 }
1175
1176 return (struct ffa_value){.func = FFA_SUCCESS_32};
1177}
1178
1179/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001180 * Check that the given `memory_region` represents a valid memory send request
1181 * of the given `share_func` type, return the clear flag and permissions via the
1182 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001183 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001184 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001185 * not.
1186 */
J-Alves66652252022-07-06 09:49:51 +01001187struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001188 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1189 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001190 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001191{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001192 struct ffa_composite_memory_region *composite;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001193 uint64_t receivers_end;
1194 uint64_t min_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001195 uint32_t composite_memory_region_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001196 uint32_t constituents_start;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001197 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001198 enum ffa_data_access data_access;
1199 enum ffa_instruction_access instruction_access;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001200 enum ffa_memory_security security_state;
Federico Recanatia98603a2021-12-20 18:04:03 +01001201 struct ffa_value ret;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001202 const size_t minimum_first_fragment_length =
1203 (sizeof(struct ffa_memory_region) +
1204 sizeof(struct ffa_memory_access) +
1205 sizeof(struct ffa_composite_memory_region));
1206
1207 if (fragment_length < minimum_first_fragment_length) {
1208 dlog_verbose("Fragment length %u too short (min %u).\n",
1209 (size_t)fragment_length,
1210 minimum_first_fragment_length);
1211 return ffa_error(FFA_INVALID_PARAMETERS);
1212 }
1213
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05001214 static_assert(sizeof(struct ffa_memory_region_constituent) == 16,
1215 "struct ffa_memory_region_constituent must be 16 bytes");
1216 if (!is_aligned(fragment_length,
1217 sizeof(struct ffa_memory_region_constituent)) ||
1218 !is_aligned(memory_share_length,
1219 sizeof(struct ffa_memory_region_constituent))) {
1220 dlog_verbose(
1221 "Fragment length %u or total length %u"
1222 " is not 16-byte aligned.\n",
1223 fragment_length, memory_share_length);
1224 return ffa_error(FFA_INVALID_PARAMETERS);
1225 }
1226
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001227 if (fragment_length > memory_share_length) {
1228 dlog_verbose(
1229 "Fragment length %u greater than total length %u.\n",
1230 (size_t)fragment_length, (size_t)memory_share_length);
1231 return ffa_error(FFA_INVALID_PARAMETERS);
1232 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001233
J-Alves0b6653d2022-04-22 13:17:38 +01001234 assert(memory_region->receivers_offset ==
1235 offsetof(struct ffa_memory_region, receivers));
1236 assert(memory_region->memory_access_desc_size ==
1237 sizeof(struct ffa_memory_access));
1238
J-Alves95df0ef2022-12-07 10:09:48 +00001239 /* The sender must match the caller. */
1240 if ((!vm_id_is_current_world(from_locked.vm->id) &&
1241 vm_id_is_current_world(memory_region->sender)) ||
1242 (vm_id_is_current_world(from_locked.vm->id) &&
1243 memory_region->sender != from_locked.vm->id)) {
1244 dlog_verbose("Invalid memory sender ID.\n");
1245 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001246 }
1247
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001248 if (memory_region->receiver_count <= 0) {
1249 dlog_verbose("No receivers!\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001250 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001251 }
1252
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001253 /*
1254 * Ensure that the composite header is within the memory bounds and
1255 * doesn't overlap the first part of the message. Cast to uint64_t
1256 * to prevent overflow.
1257 */
1258 receivers_end = ((uint64_t)sizeof(struct ffa_memory_access) *
1259 (uint64_t)memory_region->receiver_count) +
1260 sizeof(struct ffa_memory_region);
1261 min_length = receivers_end +
1262 sizeof(struct ffa_composite_memory_region) +
1263 sizeof(struct ffa_memory_region_constituent);
1264 if (min_length > memory_share_length) {
1265 dlog_verbose("Share too short: got %u but minimum is %u.\n",
1266 (size_t)memory_share_length, (size_t)min_length);
1267 return ffa_error(FFA_INVALID_PARAMETERS);
1268 }
1269
1270 composite_memory_region_offset =
1271 memory_region->receivers[0].composite_memory_region_offset;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001272
1273 /*
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001274 * Check that the composite memory region descriptor is after the access
1275 * descriptors, is at least 16-byte aligned, and fits in the first
1276 * fragment.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001277 */
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001278 if ((composite_memory_region_offset < receivers_end) ||
1279 (composite_memory_region_offset % 16 != 0) ||
1280 (composite_memory_region_offset >
1281 fragment_length - sizeof(struct ffa_composite_memory_region))) {
1282 dlog_verbose(
1283 "Invalid composite memory region descriptor offset "
1284 "%u.\n",
1285 (size_t)composite_memory_region_offset);
1286 return ffa_error(FFA_INVALID_PARAMETERS);
1287 }
1288
1289 /*
1290 * Compute the start of the constituent regions. Already checked
1291 * to be not more than fragment_length and thus not more than
1292 * memory_share_length.
1293 */
1294 constituents_start = composite_memory_region_offset +
1295 sizeof(struct ffa_composite_memory_region);
1296 constituents_length = memory_share_length - constituents_start;
1297
1298 /*
1299 * Check that the number of constituents is consistent with the length
1300 * of the constituent region.
1301 */
1302 composite = ffa_memory_region_get_composite(memory_region, 0);
1303 if ((constituents_length %
1304 sizeof(struct ffa_memory_region_constituent) !=
1305 0) ||
1306 ((constituents_length /
1307 sizeof(struct ffa_memory_region_constituent)) !=
1308 composite->constituent_count)) {
1309 dlog_verbose("Invalid length %u or composite offset %u.\n",
1310 (size_t)memory_share_length,
1311 (size_t)composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001312 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001313 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001314 if (fragment_length < memory_share_length &&
1315 fragment_length < HF_MAILBOX_SIZE) {
1316 dlog_warning(
1317 "Initial fragment length %d smaller than mailbox "
1318 "size.\n",
1319 fragment_length);
1320 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001321
Andrew Walbrana65a1322020-04-06 19:32:32 +01001322 /*
1323 * Clear is not allowed for memory sharing, as the sender still has
1324 * access to the memory.
1325 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001326 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1327 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001328 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001329 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001330 }
1331
1332 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001333 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001334 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001335 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001336 }
1337
J-Alves363f5722022-04-25 17:37:37 +01001338 /* Check that the permissions are valid, for each specified receiver. */
1339 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
1340 ffa_memory_access_permissions_t permissions =
1341 memory_region->receivers[i]
1342 .receiver_permissions.permissions;
1343 ffa_vm_id_t receiver_id =
1344 memory_region->receivers[i]
1345 .receiver_permissions.receiver;
1346
1347 if (memory_region->sender == receiver_id) {
1348 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001349 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001350 }
Federico Recanati85090c42021-12-15 13:17:54 +01001351
J-Alves363f5722022-04-25 17:37:37 +01001352 for (uint32_t j = i + 1; j < memory_region->receiver_count;
1353 j++) {
1354 if (receiver_id ==
1355 memory_region->receivers[j]
1356 .receiver_permissions.receiver) {
1357 dlog_verbose(
1358 "Repeated receiver(%x) in memory send "
1359 "operation.\n",
1360 memory_region->receivers[j]
1361 .receiver_permissions.receiver);
1362 return ffa_error(FFA_INVALID_PARAMETERS);
1363 }
1364 }
1365
1366 if (composite_memory_region_offset !=
1367 memory_region->receivers[i]
1368 .composite_memory_region_offset) {
1369 dlog_verbose(
1370 "All ffa_memory_access should point to the "
1371 "same composite memory region offset.\n");
1372 return ffa_error(FFA_INVALID_PARAMETERS);
1373 }
1374
1375 data_access = ffa_get_data_access_attr(permissions);
1376 instruction_access =
1377 ffa_get_instruction_access_attr(permissions);
1378 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1379 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
1380 dlog_verbose(
1381 "Reserved value for receiver permissions "
1382 "%#x.\n",
1383 permissions);
1384 return ffa_error(FFA_INVALID_PARAMETERS);
1385 }
1386 if (instruction_access !=
1387 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
1388 dlog_verbose(
1389 "Invalid instruction access permissions %#x "
1390 "for sending memory.\n",
1391 permissions);
1392 return ffa_error(FFA_INVALID_PARAMETERS);
1393 }
1394 if (share_func == FFA_MEM_SHARE_32) {
1395 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1396 dlog_verbose(
1397 "Invalid data access permissions %#x "
1398 "for sharing memory.\n",
1399 permissions);
1400 return ffa_error(FFA_INVALID_PARAMETERS);
1401 }
1402 /*
1403 * According to section 10.10.3 of the FF-A v1.1 EAC0
1404 * spec, NX is required for share operations (but must
1405 * not be specified by the sender) so set it in the
1406 * copy that we store, ready to be returned to the
1407 * retriever.
1408 */
J-Alvesb19731a2022-06-20 17:30:33 +01001409 if (vm_id_is_current_world(receiver_id)) {
1410 ffa_set_instruction_access_attr(
1411 &permissions,
1412 FFA_INSTRUCTION_ACCESS_NX);
1413 memory_region->receivers[i]
1414 .receiver_permissions.permissions =
1415 permissions;
1416 }
J-Alves363f5722022-04-25 17:37:37 +01001417 }
1418 if (share_func == FFA_MEM_LEND_32 &&
1419 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1420 dlog_verbose(
1421 "Invalid data access permissions %#x for "
1422 "lending memory.\n",
1423 permissions);
1424 return ffa_error(FFA_INVALID_PARAMETERS);
1425 }
1426
1427 if (share_func == FFA_MEM_DONATE_32 &&
1428 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
1429 dlog_verbose(
1430 "Invalid data access permissions %#x for "
1431 "donating memory.\n",
1432 permissions);
1433 return ffa_error(FFA_INVALID_PARAMETERS);
1434 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001435 }
1436
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001437 /* Memory region attributes NS-Bit MBZ for FFA_MEM_SHARE/LEND/DONATE. */
1438 security_state =
1439 ffa_get_memory_security_attr(memory_region->attributes);
1440 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
1441 dlog_verbose(
1442 "Invalid security state for memory share operation.\n");
1443 return ffa_error(FFA_INVALID_PARAMETERS);
1444 }
1445
Federico Recanatid937f5e2021-12-20 17:38:23 +01001446 /*
J-Alves807794e2022-06-16 13:42:47 +01001447 * If a memory donate or lend with single borrower, the memory type
1448 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01001449 */
J-Alves807794e2022-06-16 13:42:47 +01001450 if (share_func == FFA_MEM_DONATE_32 ||
1451 (share_func == FFA_MEM_LEND_32 &&
1452 memory_region->receiver_count == 1)) {
1453 if (ffa_get_memory_type_attr(memory_region->attributes) !=
1454 FFA_MEMORY_NOT_SPECIFIED_MEM) {
1455 dlog_verbose(
1456 "Memory type shall not be specified by "
1457 "sender.\n");
1458 return ffa_error(FFA_INVALID_PARAMETERS);
1459 }
1460 } else {
1461 /*
1462 * Check that sender's memory attributes match Hafnium
1463 * expectations: Normal Memory, Inner shareable, Write-Back
1464 * Read-Allocate Write-Allocate Cacheable.
1465 */
1466 ret = ffa_memory_attributes_validate(memory_region->attributes);
1467 if (ret.func != FFA_SUCCESS_32) {
1468 return ret;
1469 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01001470 }
1471
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001472 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001473}
1474
1475/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001476 * Gets the share state for continuing an operation to donate, lend or share
1477 * memory, and checks that it is a valid request.
1478 *
1479 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1480 * not.
1481 */
J-Alvesfdd29272022-07-19 13:16:31 +01001482struct ffa_value ffa_memory_send_continue_validate(
Andrew Walbranca808b12020-05-15 17:22:28 +01001483 struct share_states_locked share_states, ffa_memory_handle_t handle,
1484 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1485 struct mpool *page_pool)
1486{
1487 struct ffa_memory_share_state *share_state;
1488 struct ffa_memory_region *memory_region;
1489
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001490 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001491
1492 /*
1493 * Look up the share state by handle and make sure that the VM ID
1494 * matches.
1495 */
1496 if (!get_share_state(share_states, handle, &share_state)) {
1497 dlog_verbose(
1498 "Invalid handle %#x for memory send continuation.\n",
1499 handle);
1500 return ffa_error(FFA_INVALID_PARAMETERS);
1501 }
1502 memory_region = share_state->memory_region;
1503
J-Alvesfdd29272022-07-19 13:16:31 +01001504 if (vm_id_is_current_world(from_vm_id) &&
1505 memory_region->sender != from_vm_id) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001506 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1507 return ffa_error(FFA_INVALID_PARAMETERS);
1508 }
1509
1510 if (share_state->sending_complete) {
1511 dlog_verbose(
1512 "Sending of memory handle %#x is already complete.\n",
1513 handle);
1514 return ffa_error(FFA_INVALID_PARAMETERS);
1515 }
1516
1517 if (share_state->fragment_count == MAX_FRAGMENTS) {
1518 /*
1519 * Log a warning as this is a sign that MAX_FRAGMENTS should
1520 * probably be increased.
1521 */
1522 dlog_warning(
1523 "Too many fragments for memory share with handle %#x; "
1524 "only %d supported.\n",
1525 handle, MAX_FRAGMENTS);
1526 /* Free share state, as it's not possible to complete it. */
1527 share_state_free(share_states, share_state, page_pool);
1528 return ffa_error(FFA_NO_MEMORY);
1529 }
1530
1531 *share_state_ret = share_state;
1532
1533 return (struct ffa_value){.func = FFA_SUCCESS_32};
1534}
1535
1536/**
J-Alves95df0ef2022-12-07 10:09:48 +00001537 * Checks if there is at least one receiver from the other world.
1538 */
J-Alvesfdd29272022-07-19 13:16:31 +01001539bool memory_region_receivers_from_other_world(
J-Alves95df0ef2022-12-07 10:09:48 +00001540 struct ffa_memory_region *memory_region)
1541{
1542 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
1543 ffa_vm_id_t receiver = memory_region->receivers[i]
1544 .receiver_permissions.receiver;
1545 if (!vm_id_is_current_world(receiver)) {
1546 return true;
1547 }
1548 }
1549 return false;
1550}
1551
1552/**
J-Alves8505a8a2022-06-15 18:10:18 +01001553 * Validates a call to donate, lend or share memory to a non-other world VM and
1554 * then updates the stage-2 page tables. Specifically, check if the message
1555 * length and number of memory region constituents match, and if the transition
1556 * is valid for the type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001557 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001558 * Assumes that the caller has already found and locked the sender VM and copied
1559 * the memory region descriptor from the sender's TX buffer to a freshly
1560 * allocated page from Hafnium's internal pool. The caller must have also
1561 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001562 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001563 * This function takes ownership of the `memory_region` passed in and will free
1564 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001565 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001566struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001567 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001568 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001569 uint32_t fragment_length, uint32_t share_func,
1570 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001571{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001572 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001573 struct share_states_locked share_states;
1574 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001575
1576 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001577 * If there is an error validating the `memory_region` then we need to
1578 * free it because we own it but we won't be storing it in a share state
1579 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001580 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001581 ret = ffa_memory_send_validate(from_locked, memory_region,
1582 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001583 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001584 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001585 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001586 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001587 }
1588
Andrew Walbrana65a1322020-04-06 19:32:32 +01001589 /* Set flag for share function, ready to be retrieved later. */
1590 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001591 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001592 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001593 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001594 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001595 case FFA_MEM_LEND_32:
1596 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001597 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001598 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001599 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001600 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001601 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001602 }
1603
Andrew Walbranca808b12020-05-15 17:22:28 +01001604 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001605 /*
1606 * Allocate a share state before updating the page table. Otherwise if
1607 * updating the page table succeeded but allocating the share state
1608 * failed then it would leave the memory in a state where nobody could
1609 * get it back.
1610 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001611 if (!allocate_share_state(share_states, share_func, memory_region,
1612 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1613 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001614 dlog_verbose("Failed to allocate share state.\n");
1615 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001616 ret = ffa_error(FFA_NO_MEMORY);
1617 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001618 }
1619
Andrew Walbranca808b12020-05-15 17:22:28 +01001620 if (fragment_length == memory_share_length) {
1621 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00001622 ret = ffa_memory_send_complete(
1623 from_locked, share_states, share_state, page_pool,
1624 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001625 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01001626 /*
1627 * Use sender ID from 'memory_region' assuming
1628 * that at this point it has been validated:
1629 * - MBZ at virtual FF-A instance.
1630 */
1631 ffa_vm_id_t sender_to_ret =
1632 (from_locked.vm->id == HF_OTHER_WORLD_ID)
1633 ? memory_region->sender
1634 : 0;
Andrew Walbranca808b12020-05-15 17:22:28 +01001635 ret = (struct ffa_value){
1636 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00001637 .arg1 = (uint32_t)memory_region->handle,
1638 .arg2 = (uint32_t)(memory_region->handle >> 32),
J-Alvesfdd29272022-07-19 13:16:31 +01001639 .arg3 = fragment_length,
1640 .arg4 = (uint32_t)(sender_to_ret & 0xffff) << 16};
Andrew Walbranca808b12020-05-15 17:22:28 +01001641 }
1642
1643out:
1644 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001645 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001646 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001647}
1648
1649/**
J-Alves8505a8a2022-06-15 18:10:18 +01001650 * Continues an operation to donate, lend or share memory to a VM from current
1651 * world. If this is the last fragment then checks that the transition is valid
1652 * for the type of memory sending operation and updates the stage-2 page tables
1653 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01001654 *
1655 * Assumes that the caller has already found and locked the sender VM and copied
1656 * the memory region descriptor from the sender's TX buffer to a freshly
1657 * allocated page from Hafnium's internal pool.
1658 *
1659 * This function takes ownership of the `fragment` passed in; it must not be
1660 * freed by the caller.
1661 */
1662struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1663 void *fragment,
1664 uint32_t fragment_length,
1665 ffa_memory_handle_t handle,
1666 struct mpool *page_pool)
1667{
1668 struct share_states_locked share_states = share_states_lock();
1669 struct ffa_memory_share_state *share_state;
1670 struct ffa_value ret;
1671 struct ffa_memory_region *memory_region;
1672
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05001673 CHECK(is_aligned(fragment,
1674 alignof(struct ffa_memory_region_constituent)));
1675 if (fragment_length % sizeof(struct ffa_memory_region_constituent) !=
1676 0) {
1677 dlog_verbose("Fragment length %u misaligned.\n",
1678 fragment_length);
1679 ret = ffa_error(FFA_INVALID_PARAMETERS);
1680 goto out_free_fragment;
1681 }
1682
Andrew Walbranca808b12020-05-15 17:22:28 +01001683 ret = ffa_memory_send_continue_validate(share_states, handle,
1684 &share_state,
1685 from_locked.vm->id, page_pool);
1686 if (ret.func != FFA_SUCCESS_32) {
1687 goto out_free_fragment;
1688 }
1689 memory_region = share_state->memory_region;
1690
J-Alves95df0ef2022-12-07 10:09:48 +00001691 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001692 dlog_error(
1693 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01001694 "other world. This should never happen, and indicates "
1695 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01001696 "EL3 code.\n");
1697 ret = ffa_error(FFA_INVALID_PARAMETERS);
1698 goto out_free_fragment;
1699 }
1700
1701 /* Add this fragment. */
1702 share_state->fragments[share_state->fragment_count] = fragment;
1703 share_state->fragment_constituent_counts[share_state->fragment_count] =
1704 fragment_length / sizeof(struct ffa_memory_region_constituent);
1705 share_state->fragment_count++;
1706
1707 /* Check whether the memory send operation is now ready to complete. */
1708 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00001709 ret = ffa_memory_send_complete(
1710 from_locked, share_states, share_state, page_pool,
1711 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001712 } else {
1713 ret = (struct ffa_value){
1714 .func = FFA_MEM_FRAG_RX_32,
1715 .arg1 = (uint32_t)handle,
1716 .arg2 = (uint32_t)(handle >> 32),
1717 .arg3 = share_state_next_fragment_offset(share_states,
1718 share_state)};
1719 }
1720 goto out;
1721
1722out_free_fragment:
1723 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001724
1725out:
Andrew Walbranca808b12020-05-15 17:22:28 +01001726 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001727 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001728}
1729
Andrew Walbranca808b12020-05-15 17:22:28 +01001730/** Clean up after the receiver has finished retrieving a memory region. */
1731static void ffa_memory_retrieve_complete(
1732 struct share_states_locked share_states,
1733 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
1734{
1735 if (share_state->share_func == FFA_MEM_DONATE_32) {
1736 /*
1737 * Memory that has been donated can't be relinquished,
1738 * so no need to keep the share state around.
1739 */
1740 share_state_free(share_states, share_state, page_pool);
1741 dlog_verbose("Freed share state for donate.\n");
1742 }
1743}
1744
J-Alves2d8457f2022-10-05 11:06:41 +01001745/**
1746 * Initialises the given memory region descriptor to be used for an
1747 * `FFA_MEM_RETRIEVE_RESP`, including the given constituents for the first
1748 * fragment.
1749 * The memory region descriptor is initialized according to retriever's
1750 * FF-A version.
1751 *
1752 * Returns true on success, or false if the given constituents won't all fit in
1753 * the first fragment.
1754 */
1755static bool ffa_retrieved_memory_region_init(
1756 void *response, uint32_t ffa_version, size_t response_max_size,
1757 ffa_vm_id_t sender, ffa_memory_attributes_t attributes,
1758 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
1759 ffa_vm_id_t receiver_id, ffa_memory_access_permissions_t permissions,
1760 uint32_t page_count, uint32_t total_constituent_count,
1761 const struct ffa_memory_region_constituent constituents[],
1762 uint32_t fragment_constituent_count, uint32_t *total_length,
1763 uint32_t *fragment_length)
1764{
1765 struct ffa_composite_memory_region *composite_memory_region;
1766 struct ffa_memory_access *receiver;
1767 uint32_t i;
1768 uint32_t constituents_offset;
1769 uint32_t receiver_count;
1770
1771 assert(response != NULL);
1772
1773 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1774 struct ffa_memory_region_v1_0 *retrieve_response =
1775 (struct ffa_memory_region_v1_0 *)response;
1776
J-Alves5da37d92022-10-24 16:33:48 +01001777 ffa_memory_region_init_header_v1_0(
1778 retrieve_response, sender, attributes, flags, handle, 0,
1779 RECEIVERS_COUNT_IN_RETRIEVE_RESP);
J-Alves2d8457f2022-10-05 11:06:41 +01001780
1781 receiver = &retrieve_response->receivers[0];
1782 receiver_count = retrieve_response->receiver_count;
1783
1784 receiver->composite_memory_region_offset =
1785 sizeof(struct ffa_memory_region_v1_0) +
1786 receiver_count * sizeof(struct ffa_memory_access);
1787
1788 composite_memory_region = ffa_memory_region_get_composite_v1_0(
1789 retrieve_response, 0);
1790 } else {
1791 /* Default to FF-A v1.1 version. */
1792 struct ffa_memory_region *retrieve_response =
1793 (struct ffa_memory_region *)response;
1794
1795 ffa_memory_region_init_header(retrieve_response, sender,
1796 attributes, flags, handle, 0, 1);
1797
1798 receiver = &retrieve_response->receivers[0];
1799 receiver_count = retrieve_response->receiver_count;
1800
1801 /*
1802 * Note that `sizeof(struct_ffa_memory_region)` and
1803 * `sizeof(struct ffa_memory_access)` must both be multiples of
1804 * 16 (as verified by the asserts in `ffa_memory.c`, so it is
1805 * guaranteed that the offset we calculate here is aligned to a
1806 * 64-bit boundary and so 64-bit values can be copied without
1807 * alignment faults.
1808 */
1809 receiver->composite_memory_region_offset =
1810 sizeof(struct ffa_memory_region) +
1811 receiver_count * sizeof(struct ffa_memory_access);
1812
1813 composite_memory_region =
1814 ffa_memory_region_get_composite(retrieve_response, 0);
1815 }
1816
1817 assert(receiver != NULL);
1818 assert(composite_memory_region != NULL);
1819
1820 /*
1821 * Initialized here as in memory retrieve responses we currently expect
1822 * one borrower to be specified.
1823 */
1824 ffa_memory_access_init_permissions(receiver, receiver_id, 0, 0, flags);
1825 receiver->receiver_permissions.permissions = permissions;
1826
1827 composite_memory_region->page_count = page_count;
1828 composite_memory_region->constituent_count = total_constituent_count;
1829 composite_memory_region->reserved_0 = 0;
1830
1831 constituents_offset = receiver->composite_memory_region_offset +
1832 sizeof(struct ffa_composite_memory_region);
1833 if (constituents_offset +
1834 fragment_constituent_count *
1835 sizeof(struct ffa_memory_region_constituent) >
1836 response_max_size) {
1837 return false;
1838 }
1839
1840 for (i = 0; i < fragment_constituent_count; ++i) {
1841 composite_memory_region->constituents[i] = constituents[i];
1842 }
1843
1844 if (total_length != NULL) {
1845 *total_length =
1846 constituents_offset +
1847 composite_memory_region->constituent_count *
1848 sizeof(struct ffa_memory_region_constituent);
1849 }
1850 if (fragment_length != NULL) {
1851 *fragment_length =
1852 constituents_offset +
1853 fragment_constituent_count *
1854 sizeof(struct ffa_memory_region_constituent);
1855 }
1856
1857 return true;
1858}
1859
J-Alves96de29f2022-04-26 16:05:24 +01001860/*
1861 * Gets the receiver's access permissions from 'struct ffa_memory_region' and
1862 * returns its index in the receiver's array. If receiver's ID doesn't exist
1863 * in the array, return the region's 'receiver_count'.
1864 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001865uint32_t ffa_memory_region_get_receiver(struct ffa_memory_region *memory_region,
1866 ffa_vm_id_t receiver)
J-Alves96de29f2022-04-26 16:05:24 +01001867{
1868 struct ffa_memory_access *receivers;
1869 uint32_t i;
1870
1871 assert(memory_region != NULL);
1872
1873 receivers = memory_region->receivers;
1874
1875 for (i = 0U; i < memory_region->receiver_count; i++) {
1876 if (receivers[i].receiver_permissions.receiver == receiver) {
1877 break;
1878 }
1879 }
1880
1881 return i;
1882}
1883
1884/**
1885 * Validates the retrieved permissions against those specified by the lender
1886 * of memory share operation. Optionally can help set the permissions to be used
1887 * for the S2 mapping, through the `permissions` argument.
1888 * Returns true if permissions are valid, false otherwise.
1889 */
1890static bool ffa_memory_retrieve_is_memory_access_valid(
1891 enum ffa_data_access sent_data_access,
1892 enum ffa_data_access requested_data_access,
1893 enum ffa_instruction_access sent_instruction_access,
1894 enum ffa_instruction_access requested_instruction_access,
1895 ffa_memory_access_permissions_t *permissions)
1896{
1897 switch (sent_data_access) {
1898 case FFA_DATA_ACCESS_NOT_SPECIFIED:
1899 case FFA_DATA_ACCESS_RW:
1900 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
1901 requested_data_access == FFA_DATA_ACCESS_RW) {
1902 if (permissions != NULL) {
1903 ffa_set_data_access_attr(permissions,
1904 FFA_DATA_ACCESS_RW);
1905 }
1906 break;
1907 }
1908 /* Intentional fall-through. */
1909 case FFA_DATA_ACCESS_RO:
1910 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
1911 requested_data_access == FFA_DATA_ACCESS_RO) {
1912 if (permissions != NULL) {
1913 ffa_set_data_access_attr(permissions,
1914 FFA_DATA_ACCESS_RO);
1915 }
1916 break;
1917 }
1918 dlog_verbose(
1919 "Invalid data access requested; sender specified "
1920 "permissions %#x but receiver requested %#x.\n",
1921 sent_data_access, requested_data_access);
1922 return false;
1923 case FFA_DATA_ACCESS_RESERVED:
1924 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
1925 "checked before this point.");
1926 }
1927
1928 switch (sent_instruction_access) {
1929 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
1930 case FFA_INSTRUCTION_ACCESS_X:
1931 if (requested_instruction_access ==
1932 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
1933 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
1934 if (permissions != NULL) {
1935 ffa_set_instruction_access_attr(
1936 permissions, FFA_INSTRUCTION_ACCESS_X);
1937 }
1938 break;
1939 }
1940 case FFA_INSTRUCTION_ACCESS_NX:
1941 if (requested_instruction_access ==
1942 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
1943 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
1944 if (permissions != NULL) {
1945 ffa_set_instruction_access_attr(
1946 permissions, FFA_INSTRUCTION_ACCESS_NX);
1947 }
1948 break;
1949 }
1950 dlog_verbose(
1951 "Invalid instruction access requested; sender "
1952 "specified permissions %#x but receiver requested "
1953 "%#x.\n",
1954 sent_instruction_access, requested_instruction_access);
1955 return false;
1956 case FFA_INSTRUCTION_ACCESS_RESERVED:
1957 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
1958 "be checked before this point.");
1959 }
1960
1961 return true;
1962}
1963
1964/**
1965 * Validate the receivers' permissions in the retrieve request against those
1966 * specified by the lender.
1967 * In the `permissions` argument returns the permissions to set at S2 for the
1968 * caller to the FFA_MEMORY_RETRIEVE_REQ.
1969 * Returns FFA_SUCCESS if all specified permissions are valid.
1970 */
1971static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
1972 struct ffa_memory_region *memory_region,
1973 struct ffa_memory_region *retrieve_request, ffa_vm_id_t to_vm_id,
1974 ffa_memory_access_permissions_t *permissions)
1975{
1976 uint32_t retrieve_receiver_index;
1977
1978 assert(permissions != NULL);
1979
1980 if (retrieve_request->receiver_count != memory_region->receiver_count) {
1981 dlog_verbose(
1982 "Retrieve request should contain same list of "
1983 "borrowers, as specified by the lender.\n");
1984 return ffa_error(FFA_INVALID_PARAMETERS);
1985 }
1986
1987 retrieve_receiver_index = retrieve_request->receiver_count;
1988
1989 /* Should be populated with the permissions of the retriever. */
1990 *permissions = 0;
1991
1992 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
1993 ffa_memory_access_permissions_t sent_permissions;
1994 struct ffa_memory_access *current_receiver =
1995 &retrieve_request->receivers[i];
1996 ffa_memory_access_permissions_t requested_permissions =
1997 current_receiver->receiver_permissions.permissions;
1998 ffa_vm_id_t current_receiver_id =
1999 current_receiver->receiver_permissions.receiver;
2000 bool found_to_id = current_receiver_id == to_vm_id;
2001
2002 /*
2003 * Find the current receiver in the transaction descriptor from
2004 * sender.
2005 */
2006 uint32_t mem_region_receiver_index =
2007 ffa_memory_region_get_receiver(memory_region,
2008 current_receiver_id);
2009
2010 if (mem_region_receiver_index ==
2011 memory_region->receiver_count) {
2012 dlog_verbose("%s: receiver %x not found\n", __func__,
2013 current_receiver_id);
2014 return ffa_error(FFA_DENIED);
2015 }
2016
2017 sent_permissions =
2018 memory_region->receivers[mem_region_receiver_index]
2019 .receiver_permissions.permissions;
2020
2021 if (found_to_id) {
2022 retrieve_receiver_index = i;
2023 }
2024
2025 /*
2026 * Since we are traversing the list of receivers, save the index
2027 * of the caller. As it needs to be there.
2028 */
2029
2030 if (current_receiver->composite_memory_region_offset != 0U) {
2031 dlog_verbose(
2032 "Retriever specified address ranges not "
2033 "supported (got offset %d).\n",
2034 current_receiver
2035 ->composite_memory_region_offset);
2036 return ffa_error(FFA_INVALID_PARAMETERS);
2037 }
2038
2039 /*
2040 * Check permissions from sender against permissions requested
2041 * by receiver.
2042 */
2043 if (!ffa_memory_retrieve_is_memory_access_valid(
2044 ffa_get_data_access_attr(sent_permissions),
2045 ffa_get_data_access_attr(requested_permissions),
2046 ffa_get_instruction_access_attr(sent_permissions),
2047 ffa_get_instruction_access_attr(
2048 requested_permissions),
2049 found_to_id ? permissions : NULL)) {
2050 return ffa_error(FFA_DENIED);
2051 }
2052
2053 /*
2054 * Can't request PM to clear memory if only provided with RO
2055 * permissions.
2056 */
2057 if (found_to_id &&
2058 (ffa_get_data_access_attr(*permissions) ==
2059 FFA_DATA_ACCESS_RO) &&
2060 (retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2061 0U) {
2062 dlog_verbose(
2063 "Receiver has RO permissions can not request "
2064 "clear.\n");
2065 return ffa_error(FFA_DENIED);
2066 }
2067 }
2068
2069 if (retrieve_receiver_index == retrieve_request->receiver_count) {
2070 dlog_verbose(
2071 "Retrieve request does not contain caller's (%x) "
2072 "permissions\n",
2073 to_vm_id);
2074 return ffa_error(FFA_INVALID_PARAMETERS);
2075 }
2076
2077 return (struct ffa_value){.func = FFA_SUCCESS_32};
2078}
2079
J-Alvesa9cd7e32022-07-01 13:49:33 +01002080/*
2081 * According to section 16.4.3 of FF-A v1.1 EAC0 specification, the hypervisor
2082 * may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region description
2083 * of a pending memory sharing operation whose allocator is the SPM, for
2084 * validation purposes before forwarding an FFA_MEM_RECLAIM call. In doing so
2085 * the memory region descriptor of the retrieve request must be zeroed with the
2086 * exception of the sender ID and handle.
2087 */
2088bool is_ffa_memory_retrieve_borrower_request(struct ffa_memory_region *request,
2089 struct vm_locked to_locked)
2090{
2091 return to_locked.vm->id == HF_HYPERVISOR_VM_ID &&
2092 request->attributes == 0U && request->flags == 0U &&
2093 request->tag == 0U && request->receiver_count == 0U &&
2094 plat_ffa_memory_handle_allocated_by_current_world(
2095 request->handle);
2096}
2097
2098/*
2099 * Helper to reset count of fragments retrieved by the hypervisor.
2100 */
2101static void ffa_memory_retrieve_complete_from_hyp(
2102 struct ffa_memory_share_state *share_state)
2103{
2104 if (share_state->hypervisor_fragment_count ==
2105 share_state->fragment_count) {
2106 share_state->hypervisor_fragment_count = 0;
2107 }
2108}
2109
J-Alves089004f2022-07-13 14:25:44 +01002110/**
2111 * Validate that the memory region descriptor provided by the borrower on
2112 * FFA_MEM_RETRIEVE_REQ, against saved memory region provided by lender at the
2113 * memory sharing call.
2114 */
2115static struct ffa_value ffa_memory_retrieve_validate(
2116 ffa_vm_id_t receiver_id, struct ffa_memory_region *retrieve_request,
2117 struct ffa_memory_region *memory_region, uint32_t *receiver_index,
2118 uint32_t share_func)
2119{
2120 ffa_memory_region_flags_t transaction_type =
2121 retrieve_request->flags &
2122 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002123 enum ffa_memory_security security_state;
J-Alves089004f2022-07-13 14:25:44 +01002124
2125 assert(retrieve_request != NULL);
2126 assert(memory_region != NULL);
2127 assert(receiver_index != NULL);
2128 assert(retrieve_request->sender == memory_region->sender);
2129
2130 /*
2131 * Check that the transaction type expected by the receiver is
2132 * correct, if it has been specified.
2133 */
2134 if (transaction_type !=
2135 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
2136 transaction_type != (memory_region->flags &
2137 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
2138 dlog_verbose(
2139 "Incorrect transaction type %#x for "
2140 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
2141 transaction_type,
2142 memory_region->flags &
2143 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
2144 retrieve_request->handle);
2145 return ffa_error(FFA_INVALID_PARAMETERS);
2146 }
2147
2148 if (retrieve_request->tag != memory_region->tag) {
2149 dlog_verbose(
2150 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
2151 "%d for handle %#x.\n",
2152 retrieve_request->tag, memory_region->tag,
2153 retrieve_request->handle);
2154 return ffa_error(FFA_INVALID_PARAMETERS);
2155 }
2156
2157 *receiver_index =
2158 ffa_memory_region_get_receiver(memory_region, receiver_id);
2159
2160 if (*receiver_index == memory_region->receiver_count) {
2161 dlog_verbose(
2162 "Incorrect receiver VM ID %d for "
2163 "FFA_MEM_RETRIEVE_REQ, for handle %#x.\n",
J-Alves59ed0042022-07-28 18:26:41 +01002164 receiver_id, memory_region->handle);
J-Alves089004f2022-07-13 14:25:44 +01002165 return ffa_error(FFA_INVALID_PARAMETERS);
2166 }
2167
2168 if ((retrieve_request->flags &
2169 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
2170 dlog_verbose(
2171 "Retriever specified 'address range alignment 'hint' "
2172 "not supported.\n");
2173 return ffa_error(FFA_INVALID_PARAMETERS);
2174 }
2175 if ((retrieve_request->flags &
2176 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
2177 dlog_verbose(
2178 "Bits 8-5 must be zero in memory region's flags "
2179 "(address range alignment hint not supported).\n");
2180 return ffa_error(FFA_INVALID_PARAMETERS);
2181 }
2182
2183 if ((retrieve_request->flags & ~0x7FF) != 0U) {
2184 dlog_verbose(
2185 "Bits 31-10 must be zero in memory region's flags.\n");
2186 return ffa_error(FFA_INVALID_PARAMETERS);
2187 }
2188
2189 if (share_func == FFA_MEM_SHARE_32 &&
2190 (retrieve_request->flags &
2191 (FFA_MEMORY_REGION_FLAG_CLEAR |
2192 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
2193 dlog_verbose(
2194 "Memory Share operation can't clean after relinquish "
2195 "memory region.\n");
2196 return ffa_error(FFA_INVALID_PARAMETERS);
2197 }
2198
2199 /*
2200 * If the borrower needs the memory to be cleared before mapping
2201 * to its address space, the sender should have set the flag
2202 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
2203 * FFA_DENIED.
2204 */
2205 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
2206 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
2207 dlog_verbose(
2208 "Borrower needs memory cleared. Sender needs to set "
2209 "flag for clearing memory.\n");
2210 return ffa_error(FFA_DENIED);
2211 }
2212
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002213 /* Memory region attributes NS-Bit MBZ for FFA_MEM_RETRIEVE_REQ. */
2214 security_state =
2215 ffa_get_memory_security_attr(retrieve_request->attributes);
2216 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
2217 dlog_verbose(
2218 "Invalid security state for memory retrieve request "
2219 "operation.\n");
2220 return ffa_error(FFA_INVALID_PARAMETERS);
2221 }
2222
J-Alves089004f2022-07-13 14:25:44 +01002223 /*
2224 * If memory type is not specified, bypass validation of memory
2225 * attributes in the retrieve request. The retriever is expecting to
2226 * obtain this information from the SPMC.
2227 */
2228 if (ffa_get_memory_type_attr(retrieve_request->attributes) ==
2229 FFA_MEMORY_NOT_SPECIFIED_MEM) {
2230 return (struct ffa_value){.func = FFA_SUCCESS_32};
2231 }
2232
2233 /*
2234 * Ensure receiver's attributes are compatible with how
2235 * Hafnium maps memory: Normal Memory, Inner shareable,
2236 * Write-Back Read-Allocate Write-Allocate Cacheable.
2237 */
2238 return ffa_memory_attributes_validate(retrieve_request->attributes);
2239}
2240
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002241struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2242 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002243 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002244 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002245{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002246 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002247 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002248 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002249 sizeof(struct ffa_memory_access);
2250 ffa_memory_handle_t handle = retrieve_request->handle;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002251 struct ffa_memory_region *memory_region;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002252 ffa_memory_access_permissions_t permissions = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002253 uint32_t memory_to_attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002254 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002255 struct ffa_memory_share_state *share_state;
2256 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002257 struct ffa_composite_memory_region *composite;
2258 uint32_t total_length;
2259 uint32_t fragment_length;
J-Alves089004f2022-07-13 14:25:44 +01002260 ffa_vm_id_t receiver_id = to_locked.vm->id;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002261 bool is_send_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002262
2263 dump_share_states();
2264
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002265 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002266 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002267 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002268 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002269 expected_retrieve_request_length,
2270 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002271 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002272 }
2273
2274 share_states = share_states_lock();
2275 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002276 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002277 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002278 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002279 goto out;
2280 }
2281
J-Alves96de29f2022-04-26 16:05:24 +01002282 if (!share_state->sending_complete) {
2283 dlog_verbose(
2284 "Memory with handle %#x not fully sent, can't "
2285 "retrieve.\n",
2286 handle);
2287 ret = ffa_error(FFA_INVALID_PARAMETERS);
2288 goto out;
2289 }
2290
Andrew Walbrana65a1322020-04-06 19:32:32 +01002291 memory_region = share_state->memory_region;
J-Alves089004f2022-07-13 14:25:44 +01002292
Andrew Walbrana65a1322020-04-06 19:32:32 +01002293 CHECK(memory_region != NULL);
2294
J-Alves089004f2022-07-13 14:25:44 +01002295 if (retrieve_request->sender != memory_region->sender) {
2296 dlog_verbose(
2297 "Memory with handle %#x not fully sent, can't "
2298 "retrieve.\n",
2299 handle);
2300 ret = ffa_error(FFA_INVALID_PARAMETERS);
2301 goto out;
2302 }
J-Alves96de29f2022-04-26 16:05:24 +01002303
J-Alvesa9cd7e32022-07-01 13:49:33 +01002304 if (!is_ffa_memory_retrieve_borrower_request(retrieve_request,
2305 to_locked)) {
2306 uint32_t receiver_index;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002307
J-Alvesb5084cf2022-07-06 14:20:12 +01002308 /*
2309 * The SPMC can only process retrieve requests to memory share
2310 * operations with one borrower from the other world. It can't
2311 * determine the ID of the NWd VM that invoked the retrieve
2312 * request interface call. It relies on the hypervisor to
2313 * validate the caller's ID against that provided in the
2314 * `receivers` list of the retrieve response.
2315 * In case there is only one borrower from the NWd in the
2316 * transaction descriptor, record that in the `receiver_id` for
2317 * later use, and validate in the retrieve request message.
2318 */
2319 if (to_locked.vm->id == HF_HYPERVISOR_VM_ID) {
2320 uint32_t other_world_count = 0;
2321
2322 for (uint32_t i = 0; i < memory_region->receiver_count;
2323 i++) {
2324 receiver_id =
2325 retrieve_request->receivers[0]
2326 .receiver_permissions.receiver;
2327 if (!vm_id_is_current_world(receiver_id)) {
2328 other_world_count++;
2329 }
2330 }
2331 if (other_world_count > 1) {
2332 dlog_verbose(
2333 "Support one receiver from the other "
2334 "world.\n");
2335 return ffa_error(FFA_NOT_SUPPORTED);
2336 }
2337 }
2338
2339 /*
2340 * Validate retrieve request, according to what was sent by the
2341 * sender. Function will output the `receiver_index` from the
2342 * provided memory region, and will output `permissions` from
2343 * the validated requested permissions.
2344 */
J-Alves089004f2022-07-13 14:25:44 +01002345 ret = ffa_memory_retrieve_validate(
2346 receiver_id, retrieve_request, memory_region,
2347 &receiver_index, share_state->share_func);
2348 if (ret.func != FFA_SUCCESS_32) {
J-Alvesa9cd7e32022-07-01 13:49:33 +01002349 goto out;
2350 }
2351
2352 if (share_state->retrieved_fragment_count[receiver_index] !=
2353 0U) {
2354 dlog_verbose(
2355 "Memory with handle %#x already retrieved.\n",
2356 handle);
2357 ret = ffa_error(FFA_DENIED);
2358 goto out;
2359 }
2360
J-Alvesa9cd7e32022-07-01 13:49:33 +01002361 ret = ffa_memory_retrieve_validate_memory_access_list(
2362 memory_region, retrieve_request, receiver_id,
2363 &permissions);
J-Alves614d9f42022-06-28 14:03:10 +01002364 if (ret.func != FFA_SUCCESS_32) {
2365 goto out;
2366 }
Federico Recanatia98603a2021-12-20 18:04:03 +01002367
J-Alvesa9cd7e32022-07-01 13:49:33 +01002368 memory_to_attributes = ffa_memory_permissions_to_mode(
2369 permissions, share_state->sender_orig_mode);
J-Alves40e260e2022-09-22 17:52:43 +01002370
2371 if (to_locked.vm->el0_partition) {
2372 /*
2373 * Get extra mapping attributes for the given VM ID.
2374 * If the memory is shared by a VM executing in non
2375 * secure world, attribute MM_MODE_NS has to be set
2376 * while mapping that in a SP executing in secure world.
2377 */
2378 memory_to_attributes |=
2379 arch_mm_extra_attributes_from_vm(
2380 retrieve_request->sender);
2381 }
2382
J-Alvesa9cd7e32022-07-01 13:49:33 +01002383 ret = ffa_retrieve_check_update(
2384 to_locked, memory_region->sender,
2385 share_state->fragments,
2386 share_state->fragment_constituent_counts,
2387 share_state->fragment_count, memory_to_attributes,
2388 share_state->share_func, false, page_pool);
2389
2390 if (ret.func != FFA_SUCCESS_32) {
2391 goto out;
2392 }
2393
2394 share_state->retrieved_fragment_count[receiver_index] = 1;
2395 is_send_complete =
2396 share_state->retrieved_fragment_count[receiver_index] ==
2397 share_state->fragment_count;
J-Alves3c5b2072022-11-21 12:45:40 +00002398
2399 share_state->clear_after_relinquish =
2400 (retrieve_request->flags &
2401 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH) != 0U;
2402
J-Alvesa9cd7e32022-07-01 13:49:33 +01002403 } else {
2404 if (share_state->hypervisor_fragment_count != 0U) {
2405 dlog_verbose(
J-Alvesb5084cf2022-07-06 14:20:12 +01002406 "Memory with handle %#x already retrieved by "
J-Alvesa9cd7e32022-07-01 13:49:33 +01002407 "the hypervisor.\n",
2408 handle);
2409 ret = ffa_error(FFA_DENIED);
2410 goto out;
2411 }
2412
2413 share_state->hypervisor_fragment_count = 1;
2414
2415 ffa_memory_retrieve_complete_from_hyp(share_state);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002416 }
2417
J-Alvesb5084cf2022-07-06 14:20:12 +01002418 /* VMs acquire the RX buffer from SPMC. */
2419 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
2420
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002421 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002422 * Copy response to RX buffer of caller and deliver the message.
2423 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002424 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002425 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002426 composite = ffa_memory_region_get_composite(memory_region, 0);
2427 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002428 * Constituents which we received in the first fragment should
2429 * always fit in the first fragment we are sending, because the
2430 * header is the same size in both cases and we have a fixed
2431 * message buffer size. So `ffa_retrieved_memory_region_init`
2432 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01002433 */
2434 CHECK(ffa_retrieved_memory_region_init(
J-Alves2d8457f2022-10-05 11:06:41 +01002435 to_locked.vm->mailbox.recv, to_locked.vm->ffa_version,
2436 HF_MAILBOX_SIZE, memory_region->sender,
2437 memory_region->attributes, memory_region->flags, handle,
2438 receiver_id, permissions, composite->page_count,
2439 composite->constituent_count, share_state->fragments[0],
Andrew Walbranca808b12020-05-15 17:22:28 +01002440 share_state->fragment_constituent_counts[0], &total_length,
2441 &fragment_length));
J-Alvesb5084cf2022-07-06 14:20:12 +01002442
Andrew Walbranca808b12020-05-15 17:22:28 +01002443 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002444 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002445 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00002446 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002447
J-Alvesa9cd7e32022-07-01 13:49:33 +01002448 if (is_send_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002449 ffa_memory_retrieve_complete(share_states, share_state,
2450 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002451 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002452 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002453 .arg1 = total_length,
2454 .arg2 = fragment_length};
Andrew Walbranca808b12020-05-15 17:22:28 +01002455out:
2456 share_states_unlock(&share_states);
2457 dump_share_states();
2458 return ret;
2459}
2460
J-Alves5da37d92022-10-24 16:33:48 +01002461/**
2462 * Determine expected fragment offset according to the FF-A version of
2463 * the caller.
2464 */
2465static uint32_t ffa_memory_retrieve_expected_offset_per_ffa_version(
2466 struct ffa_memory_region *memory_region,
2467 uint32_t retrieved_constituents_count, uint32_t ffa_version)
2468{
2469 uint32_t expected_fragment_offset;
2470 uint32_t composite_constituents_offset;
2471
2472 if (ffa_version == MAKE_FFA_VERSION(1, 1)) {
2473 /*
2474 * Hafnium operates memory regions in FF-A v1.1 format, so we
2475 * can retrieve the constituents offset from descriptor.
2476 */
2477 composite_constituents_offset =
2478 ffa_composite_constituent_offset(memory_region, 0);
2479 } else if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
2480 /*
2481 * If retriever is FF-A v1.0, determine the composite offset
2482 * as it is expected to have been configured in the
2483 * retrieve response.
2484 */
2485 composite_constituents_offset =
2486 sizeof(struct ffa_memory_region_v1_0) +
2487 RECEIVERS_COUNT_IN_RETRIEVE_RESP *
2488 sizeof(struct ffa_memory_access) +
2489 sizeof(struct ffa_composite_memory_region);
2490 } else {
2491 panic("%s received an invalid FF-A version.\n", __func__);
2492 }
2493
2494 expected_fragment_offset =
2495 composite_constituents_offset +
2496 retrieved_constituents_count *
2497 sizeof(struct ffa_memory_region_constituent) -
2498 sizeof(struct ffa_memory_access) *
2499 (memory_region->receiver_count - 1);
2500
2501 return expected_fragment_offset;
2502}
2503
Andrew Walbranca808b12020-05-15 17:22:28 +01002504struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2505 ffa_memory_handle_t handle,
2506 uint32_t fragment_offset,
J-Alves59ed0042022-07-28 18:26:41 +01002507 ffa_vm_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01002508 struct mpool *page_pool)
2509{
2510 struct ffa_memory_region *memory_region;
2511 struct share_states_locked share_states;
2512 struct ffa_memory_share_state *share_state;
2513 struct ffa_value ret;
2514 uint32_t fragment_index;
2515 uint32_t retrieved_constituents_count;
2516 uint32_t i;
2517 uint32_t expected_fragment_offset;
2518 uint32_t remaining_constituent_count;
2519 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01002520 uint32_t receiver_index;
J-Alves59ed0042022-07-28 18:26:41 +01002521 bool continue_ffa_hyp_mem_retrieve_req;
Andrew Walbranca808b12020-05-15 17:22:28 +01002522
2523 dump_share_states();
2524
2525 share_states = share_states_lock();
2526 if (!get_share_state(share_states, handle, &share_state)) {
2527 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2528 handle);
2529 ret = ffa_error(FFA_INVALID_PARAMETERS);
2530 goto out;
2531 }
2532
2533 memory_region = share_state->memory_region;
2534 CHECK(memory_region != NULL);
2535
Andrew Walbranca808b12020-05-15 17:22:28 +01002536 if (!share_state->sending_complete) {
2537 dlog_verbose(
2538 "Memory with handle %#x not fully sent, can't "
2539 "retrieve.\n",
2540 handle);
2541 ret = ffa_error(FFA_INVALID_PARAMETERS);
2542 goto out;
2543 }
2544
J-Alves59ed0042022-07-28 18:26:41 +01002545 /*
2546 * If retrieve request from the hypervisor has been initiated in the
2547 * given share_state, continue it, else assume it is a continuation of
2548 * retrieve request from a NWd VM.
2549 */
2550 continue_ffa_hyp_mem_retrieve_req =
2551 (to_locked.vm->id == HF_HYPERVISOR_VM_ID) &&
2552 (share_state->hypervisor_fragment_count != 0U) &&
2553 plat_ffa_is_vm_id(sender_vm_id);
Andrew Walbranca808b12020-05-15 17:22:28 +01002554
J-Alves59ed0042022-07-28 18:26:41 +01002555 if (!continue_ffa_hyp_mem_retrieve_req) {
2556 receiver_index = ffa_memory_region_get_receiver(
2557 memory_region, to_locked.vm->id);
2558
2559 if (receiver_index == memory_region->receiver_count) {
2560 dlog_verbose(
2561 "Caller of FFA_MEM_FRAG_RX (%x) is not a "
2562 "borrower to memory sharing transaction (%x)\n",
2563 to_locked.vm->id, handle);
2564 ret = ffa_error(FFA_INVALID_PARAMETERS);
2565 goto out;
2566 }
2567
2568 if (share_state->retrieved_fragment_count[receiver_index] ==
2569 0 ||
2570 share_state->retrieved_fragment_count[receiver_index] >=
2571 share_state->fragment_count) {
2572 dlog_verbose(
2573 "Retrieval of memory with handle %#x not yet "
2574 "started or already completed (%d/%d fragments "
2575 "retrieved).\n",
2576 handle,
2577 share_state->retrieved_fragment_count
2578 [receiver_index],
2579 share_state->fragment_count);
2580 ret = ffa_error(FFA_INVALID_PARAMETERS);
2581 goto out;
2582 }
2583
2584 fragment_index =
2585 share_state->retrieved_fragment_count[receiver_index];
2586 } else {
2587 if (share_state->hypervisor_fragment_count == 0 ||
2588 share_state->hypervisor_fragment_count >=
2589 share_state->fragment_count) {
2590 dlog_verbose(
2591 "Retrieve of memory with handle %x not "
2592 "started from hypervisor.\n",
2593 handle);
2594 ret = ffa_error(FFA_INVALID_PARAMETERS);
2595 goto out;
2596 }
2597
2598 if (memory_region->sender != sender_vm_id) {
2599 dlog_verbose(
2600 "Sender ID (%x) is not as expected for memory "
2601 "handle %x\n",
2602 sender_vm_id, handle);
2603 ret = ffa_error(FFA_INVALID_PARAMETERS);
2604 goto out;
2605 }
2606
2607 fragment_index = share_state->hypervisor_fragment_count;
2608
2609 receiver_index = 0;
2610 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002611
2612 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002613 * Check that the given fragment offset is correct by counting
2614 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01002615 */
2616 retrieved_constituents_count = 0;
2617 for (i = 0; i < fragment_index; ++i) {
2618 retrieved_constituents_count +=
2619 share_state->fragment_constituent_counts[i];
2620 }
J-Alvesc7484f12022-05-13 12:41:14 +01002621
2622 CHECK(memory_region->receiver_count > 0);
2623
Andrew Walbranca808b12020-05-15 17:22:28 +01002624 expected_fragment_offset =
J-Alves5da37d92022-10-24 16:33:48 +01002625 ffa_memory_retrieve_expected_offset_per_ffa_version(
2626 memory_region, retrieved_constituents_count,
2627 to_locked.vm->ffa_version);
2628
Andrew Walbranca808b12020-05-15 17:22:28 +01002629 if (fragment_offset != expected_fragment_offset) {
2630 dlog_verbose("Fragment offset was %d but expected %d.\n",
2631 fragment_offset, expected_fragment_offset);
2632 ret = ffa_error(FFA_INVALID_PARAMETERS);
2633 goto out;
2634 }
2635
J-Alves59ed0042022-07-28 18:26:41 +01002636 /* VMs acquire the RX buffer from SPMC. */
2637 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
2638
Andrew Walbranca808b12020-05-15 17:22:28 +01002639 remaining_constituent_count = ffa_memory_fragment_init(
2640 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2641 share_state->fragments[fragment_index],
2642 share_state->fragment_constituent_counts[fragment_index],
2643 &fragment_length);
2644 CHECK(remaining_constituent_count == 0);
2645 to_locked.vm->mailbox.recv_size = fragment_length;
2646 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2647 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00002648 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbranca808b12020-05-15 17:22:28 +01002649
J-Alves59ed0042022-07-28 18:26:41 +01002650 if (!continue_ffa_hyp_mem_retrieve_req) {
2651 share_state->retrieved_fragment_count[receiver_index]++;
2652 if (share_state->retrieved_fragment_count[receiver_index] ==
2653 share_state->fragment_count) {
2654 ffa_memory_retrieve_complete(share_states, share_state,
2655 page_pool);
2656 }
2657 } else {
2658 share_state->hypervisor_fragment_count++;
2659
2660 ffa_memory_retrieve_complete_from_hyp(share_state);
2661 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002662 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2663 .arg1 = (uint32_t)handle,
2664 .arg2 = (uint32_t)(handle >> 32),
2665 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002666
2667out:
2668 share_states_unlock(&share_states);
2669 dump_share_states();
2670 return ret;
2671}
2672
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002673struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002674 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002675 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002676{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002677 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002678 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002679 struct ffa_memory_share_state *share_state;
2680 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002681 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002682 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01002683 uint32_t receiver_index;
J-Alves3c5b2072022-11-21 12:45:40 +00002684 bool receivers_relinquished_memory;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002685
Andrew Walbrana65a1322020-04-06 19:32:32 +01002686 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002687 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002688 "Stream endpoints not supported (got %d "
J-Alves668a86e2023-05-10 11:53:25 +01002689 "endpoints on FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002690 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002691 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002692 }
2693
Andrew Walbrana65a1322020-04-06 19:32:32 +01002694 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002695 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002696 "VM ID %d in relinquish message doesn't match "
J-Alves668a86e2023-05-10 11:53:25 +01002697 "calling VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002698 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002699 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002700 }
2701
2702 dump_share_states();
2703
2704 share_states = share_states_lock();
2705 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002706 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002707 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002708 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002709 goto out;
2710 }
2711
Andrew Walbranca808b12020-05-15 17:22:28 +01002712 if (!share_state->sending_complete) {
2713 dlog_verbose(
2714 "Memory with handle %#x not fully sent, can't "
2715 "relinquish.\n",
2716 handle);
2717 ret = ffa_error(FFA_INVALID_PARAMETERS);
2718 goto out;
2719 }
2720
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002721 memory_region = share_state->memory_region;
2722 CHECK(memory_region != NULL);
2723
J-Alves8eb19162022-04-28 10:56:48 +01002724 receiver_index = ffa_memory_region_get_receiver(memory_region,
2725 from_locked.vm->id);
2726
2727 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002728 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002729 "VM ID %d tried to relinquish memory region "
J-Alves668a86e2023-05-10 11:53:25 +01002730 "with handle %#x and it is not a valid borrower.\n",
J-Alves8eb19162022-04-28 10:56:48 +01002731 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002732 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002733 goto out;
2734 }
2735
J-Alves8eb19162022-04-28 10:56:48 +01002736 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01002737 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002738 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002739 "Memory with handle %#x not yet fully "
2740 "retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01002741 "receiver %x can't relinquish.\n",
2742 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002743 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002744 goto out;
2745 }
2746
J-Alves3c5b2072022-11-21 12:45:40 +00002747 /*
2748 * Either clear if requested in relinquish call, or in a retrieve
2749 * request from one of the borrowers.
2750 */
2751 receivers_relinquished_memory = true;
2752
2753 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
2754 struct ffa_memory_access *receiver =
2755 &memory_region->receivers[i];
2756
2757 if (receiver->receiver_permissions.receiver ==
2758 from_locked.vm->id) {
2759 continue;
2760 }
2761
2762 if (share_state->retrieved_fragment_count[i] != 0U) {
2763 receivers_relinquished_memory = false;
2764 break;
2765 }
2766 }
2767
2768 clear = receivers_relinquished_memory &&
2769 (share_state->clear_after_relinquish ||
2770 (relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2771 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002772
2773 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002774 * Clear is not allowed for memory that was shared, as the
2775 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002776 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002777 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002778 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002779 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002780 goto out;
2781 }
2782
Andrew Walbranca808b12020-05-15 17:22:28 +01002783 ret = ffa_relinquish_check_update(
J-Alves3c5b2072022-11-21 12:45:40 +00002784 from_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002785 share_state->fragment_constituent_counts,
2786 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002787
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002788 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002789 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002790 * Mark memory handle as not retrieved, so it can be
2791 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002792 */
J-Alves8eb19162022-04-28 10:56:48 +01002793 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002794 }
2795
2796out:
2797 share_states_unlock(&share_states);
2798 dump_share_states();
2799 return ret;
2800}
2801
2802/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01002803 * Validates that the reclaim transition is allowed for the given
2804 * handle, updates the page table of the reclaiming VM, and frees the
2805 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002806 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002807struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002808 ffa_memory_handle_t handle,
2809 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002810 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002811{
2812 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002813 struct ffa_memory_share_state *share_state;
2814 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002815 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002816
2817 dump_share_states();
2818
2819 share_states = share_states_lock();
J-Alvesb5084cf2022-07-06 14:20:12 +01002820 if (get_share_state(share_states, handle, &share_state)) {
2821 memory_region = share_state->memory_region;
2822 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002823 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002824 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002825 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002826 goto out;
2827 }
2828
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002829 CHECK(memory_region != NULL);
2830
J-Alvesa9cd7e32022-07-01 13:49:33 +01002831 if (vm_id_is_current_world(to_locked.vm->id) &&
2832 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002833 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002834 "VM %#x attempted to reclaim memory handle %#x "
2835 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002836 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002837 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002838 goto out;
2839 }
2840
Andrew Walbranca808b12020-05-15 17:22:28 +01002841 if (!share_state->sending_complete) {
2842 dlog_verbose(
2843 "Memory with handle %#x not fully sent, can't "
2844 "reclaim.\n",
2845 handle);
2846 ret = ffa_error(FFA_INVALID_PARAMETERS);
2847 goto out;
2848 }
2849
J-Alves752236c2022-04-28 11:07:47 +01002850 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
2851 if (share_state->retrieved_fragment_count[i] != 0) {
2852 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002853 "Tried to reclaim memory handle %#x "
J-Alves3c5b2072022-11-21 12:45:40 +00002854 "that has not been relinquished by all "
J-Alvesa9cd7e32022-07-01 13:49:33 +01002855 "borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01002856 handle,
2857 memory_region->receivers[i]
2858 .receiver_permissions.receiver);
2859 ret = ffa_error(FFA_DENIED);
2860 goto out;
2861 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002862 }
2863
Andrew Walbranca808b12020-05-15 17:22:28 +01002864 ret = ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00002865 to_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002866 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00002867 share_state->fragment_count, share_state->sender_orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01002868 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002869
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002870 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002871 share_state_free(share_states, share_state, page_pool);
J-Alves3c5b2072022-11-21 12:45:40 +00002872 dlog_verbose("Freed share state after successful reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002873 }
2874
2875out:
2876 share_states_unlock(&share_states);
2877 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002878}