blob: f43553333d459a80640fdf9661aef0a0204db0a9 [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(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001020 struct vm_locked from_locked,
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(
1071 plat_ffa_owner_world_mode(from_locked.vm->id), fragments,
1072 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;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001193 uint32_t receivers_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001194 uint32_t composite_memory_region_offset;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001195 uint32_t constituents_offset;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001196 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001197 enum ffa_data_access data_access;
1198 enum ffa_instruction_access instruction_access;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001199 enum ffa_memory_security security_state;
Federico Recanatia98603a2021-12-20 18:04:03 +01001200 struct ffa_value ret;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001201
J-Alves0b6653d2022-04-22 13:17:38 +01001202 assert(memory_region->receivers_offset ==
1203 offsetof(struct ffa_memory_region, receivers));
1204 assert(memory_region->memory_access_desc_size ==
1205 sizeof(struct ffa_memory_access));
1206
J-Alves95df0ef2022-12-07 10:09:48 +00001207 /* The sender must match the caller. */
1208 if ((!vm_id_is_current_world(from_locked.vm->id) &&
1209 vm_id_is_current_world(memory_region->sender)) ||
1210 (vm_id_is_current_world(from_locked.vm->id) &&
1211 memory_region->sender != from_locked.vm->id)) {
1212 dlog_verbose("Invalid memory sender ID.\n");
1213 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001214 }
1215
Andrew Walbrana65a1322020-04-06 19:32:32 +01001216 /*
1217 * Ensure that the composite header is within the memory bounds and
1218 * doesn't overlap the first part of the message.
1219 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001220 receivers_length = sizeof(struct ffa_memory_access) *
1221 memory_region->receiver_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001222 constituents_offset =
1223 ffa_composite_constituent_offset(memory_region, 0);
Federico Recanati872cd692022-01-05 13:10:10 +01001224 composite_memory_region_offset =
1225 memory_region->receivers[0].composite_memory_region_offset;
1226 if ((composite_memory_region_offset == 0) ||
1227 (composite_memory_region_offset <
1228 sizeof(struct ffa_memory_region) + receivers_length) ||
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001229 constituents_offset > fragment_length) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001230 dlog_verbose(
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001231 "Invalid composite memory region descriptor offset "
1232 "%d.\n",
1233 memory_region->receivers[0]
1234 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001235 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001236 }
1237
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001238 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001239
1240 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001241 * Ensure the number of constituents are within the memory bounds.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001242 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001243 constituents_length = sizeof(struct ffa_memory_region_constituent) *
1244 composite->constituent_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001245 if (memory_share_length != constituents_offset + constituents_length) {
1246 dlog_verbose("Invalid length %d or composite offset %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001247 memory_share_length,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001248 memory_region->receivers[0]
1249 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001250 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001251 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001252 if (fragment_length < memory_share_length &&
1253 fragment_length < HF_MAILBOX_SIZE) {
1254 dlog_warning(
1255 "Initial fragment length %d smaller than mailbox "
1256 "size.\n",
1257 fragment_length);
1258 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001259
Andrew Walbrana65a1322020-04-06 19:32:32 +01001260 /*
1261 * Clear is not allowed for memory sharing, as the sender still has
1262 * access to the memory.
1263 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001264 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1265 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001266 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001267 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001268 }
1269
1270 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001271 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001272 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001273 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001274 }
1275
J-Alves363f5722022-04-25 17:37:37 +01001276 /* Check that the permissions are valid, for each specified receiver. */
1277 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
1278 ffa_memory_access_permissions_t permissions =
1279 memory_region->receivers[i]
1280 .receiver_permissions.permissions;
1281 ffa_vm_id_t receiver_id =
1282 memory_region->receivers[i]
1283 .receiver_permissions.receiver;
1284
1285 if (memory_region->sender == receiver_id) {
1286 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001287 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001288 }
Federico Recanati85090c42021-12-15 13:17:54 +01001289
J-Alves363f5722022-04-25 17:37:37 +01001290 for (uint32_t j = i + 1; j < memory_region->receiver_count;
1291 j++) {
1292 if (receiver_id ==
1293 memory_region->receivers[j]
1294 .receiver_permissions.receiver) {
1295 dlog_verbose(
1296 "Repeated receiver(%x) in memory send "
1297 "operation.\n",
1298 memory_region->receivers[j]
1299 .receiver_permissions.receiver);
1300 return ffa_error(FFA_INVALID_PARAMETERS);
1301 }
1302 }
1303
1304 if (composite_memory_region_offset !=
1305 memory_region->receivers[i]
1306 .composite_memory_region_offset) {
1307 dlog_verbose(
1308 "All ffa_memory_access should point to the "
1309 "same composite memory region offset.\n");
1310 return ffa_error(FFA_INVALID_PARAMETERS);
1311 }
1312
1313 data_access = ffa_get_data_access_attr(permissions);
1314 instruction_access =
1315 ffa_get_instruction_access_attr(permissions);
1316 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1317 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
1318 dlog_verbose(
1319 "Reserved value for receiver permissions "
1320 "%#x.\n",
1321 permissions);
1322 return ffa_error(FFA_INVALID_PARAMETERS);
1323 }
1324 if (instruction_access !=
1325 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
1326 dlog_verbose(
1327 "Invalid instruction access permissions %#x "
1328 "for sending memory.\n",
1329 permissions);
1330 return ffa_error(FFA_INVALID_PARAMETERS);
1331 }
1332 if (share_func == FFA_MEM_SHARE_32) {
1333 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1334 dlog_verbose(
1335 "Invalid data access permissions %#x "
1336 "for sharing memory.\n",
1337 permissions);
1338 return ffa_error(FFA_INVALID_PARAMETERS);
1339 }
1340 /*
1341 * According to section 10.10.3 of the FF-A v1.1 EAC0
1342 * spec, NX is required for share operations (but must
1343 * not be specified by the sender) so set it in the
1344 * copy that we store, ready to be returned to the
1345 * retriever.
1346 */
J-Alvesb19731a2022-06-20 17:30:33 +01001347 if (vm_id_is_current_world(receiver_id)) {
1348 ffa_set_instruction_access_attr(
1349 &permissions,
1350 FFA_INSTRUCTION_ACCESS_NX);
1351 memory_region->receivers[i]
1352 .receiver_permissions.permissions =
1353 permissions;
1354 }
J-Alves363f5722022-04-25 17:37:37 +01001355 }
1356 if (share_func == FFA_MEM_LEND_32 &&
1357 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1358 dlog_verbose(
1359 "Invalid data access permissions %#x for "
1360 "lending memory.\n",
1361 permissions);
1362 return ffa_error(FFA_INVALID_PARAMETERS);
1363 }
1364
1365 if (share_func == FFA_MEM_DONATE_32 &&
1366 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
1367 dlog_verbose(
1368 "Invalid data access permissions %#x for "
1369 "donating memory.\n",
1370 permissions);
1371 return ffa_error(FFA_INVALID_PARAMETERS);
1372 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001373 }
1374
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001375 /* Memory region attributes NS-Bit MBZ for FFA_MEM_SHARE/LEND/DONATE. */
1376 security_state =
1377 ffa_get_memory_security_attr(memory_region->attributes);
1378 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
1379 dlog_verbose(
1380 "Invalid security state for memory share operation.\n");
1381 return ffa_error(FFA_INVALID_PARAMETERS);
1382 }
1383
Federico Recanatid937f5e2021-12-20 17:38:23 +01001384 /*
J-Alves807794e2022-06-16 13:42:47 +01001385 * If a memory donate or lend with single borrower, the memory type
1386 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01001387 */
J-Alves807794e2022-06-16 13:42:47 +01001388 if (share_func == FFA_MEM_DONATE_32 ||
1389 (share_func == FFA_MEM_LEND_32 &&
1390 memory_region->receiver_count == 1)) {
1391 if (ffa_get_memory_type_attr(memory_region->attributes) !=
1392 FFA_MEMORY_NOT_SPECIFIED_MEM) {
1393 dlog_verbose(
1394 "Memory type shall not be specified by "
1395 "sender.\n");
1396 return ffa_error(FFA_INVALID_PARAMETERS);
1397 }
1398 } else {
1399 /*
1400 * Check that sender's memory attributes match Hafnium
1401 * expectations: Normal Memory, Inner shareable, Write-Back
1402 * Read-Allocate Write-Allocate Cacheable.
1403 */
1404 ret = ffa_memory_attributes_validate(memory_region->attributes);
1405 if (ret.func != FFA_SUCCESS_32) {
1406 return ret;
1407 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01001408 }
1409
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001410 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001411}
1412
1413/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001414 * Gets the share state for continuing an operation to donate, lend or share
1415 * memory, and checks that it is a valid request.
1416 *
1417 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1418 * not.
1419 */
J-Alvesfdd29272022-07-19 13:16:31 +01001420struct ffa_value ffa_memory_send_continue_validate(
Andrew Walbranca808b12020-05-15 17:22:28 +01001421 struct share_states_locked share_states, ffa_memory_handle_t handle,
1422 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1423 struct mpool *page_pool)
1424{
1425 struct ffa_memory_share_state *share_state;
1426 struct ffa_memory_region *memory_region;
1427
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001428 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001429
1430 /*
1431 * Look up the share state by handle and make sure that the VM ID
1432 * matches.
1433 */
1434 if (!get_share_state(share_states, handle, &share_state)) {
1435 dlog_verbose(
1436 "Invalid handle %#x for memory send continuation.\n",
1437 handle);
1438 return ffa_error(FFA_INVALID_PARAMETERS);
1439 }
1440 memory_region = share_state->memory_region;
1441
J-Alvesfdd29272022-07-19 13:16:31 +01001442 if (vm_id_is_current_world(from_vm_id) &&
1443 memory_region->sender != from_vm_id) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001444 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1445 return ffa_error(FFA_INVALID_PARAMETERS);
1446 }
1447
1448 if (share_state->sending_complete) {
1449 dlog_verbose(
1450 "Sending of memory handle %#x is already complete.\n",
1451 handle);
1452 return ffa_error(FFA_INVALID_PARAMETERS);
1453 }
1454
1455 if (share_state->fragment_count == MAX_FRAGMENTS) {
1456 /*
1457 * Log a warning as this is a sign that MAX_FRAGMENTS should
1458 * probably be increased.
1459 */
1460 dlog_warning(
1461 "Too many fragments for memory share with handle %#x; "
1462 "only %d supported.\n",
1463 handle, MAX_FRAGMENTS);
1464 /* Free share state, as it's not possible to complete it. */
1465 share_state_free(share_states, share_state, page_pool);
1466 return ffa_error(FFA_NO_MEMORY);
1467 }
1468
1469 *share_state_ret = share_state;
1470
1471 return (struct ffa_value){.func = FFA_SUCCESS_32};
1472}
1473
1474/**
J-Alves95df0ef2022-12-07 10:09:48 +00001475 * Checks if there is at least one receiver from the other world.
1476 */
J-Alvesfdd29272022-07-19 13:16:31 +01001477bool memory_region_receivers_from_other_world(
J-Alves95df0ef2022-12-07 10:09:48 +00001478 struct ffa_memory_region *memory_region)
1479{
1480 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
1481 ffa_vm_id_t receiver = memory_region->receivers[i]
1482 .receiver_permissions.receiver;
1483 if (!vm_id_is_current_world(receiver)) {
1484 return true;
1485 }
1486 }
1487 return false;
1488}
1489
1490/**
J-Alves8505a8a2022-06-15 18:10:18 +01001491 * Validates a call to donate, lend or share memory to a non-other world VM and
1492 * then updates the stage-2 page tables. Specifically, check if the message
1493 * length and number of memory region constituents match, and if the transition
1494 * is valid for the type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001495 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001496 * Assumes that the caller has already found and locked the sender VM and copied
1497 * the memory region descriptor from the sender's TX buffer to a freshly
1498 * allocated page from Hafnium's internal pool. The caller must have also
1499 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001500 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001501 * This function takes ownership of the `memory_region` passed in and will free
1502 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001503 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001504struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001505 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001506 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001507 uint32_t fragment_length, uint32_t share_func,
1508 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001509{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001510 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001511 struct share_states_locked share_states;
1512 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001513
1514 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001515 * If there is an error validating the `memory_region` then we need to
1516 * free it because we own it but we won't be storing it in a share state
1517 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001518 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001519 ret = ffa_memory_send_validate(from_locked, memory_region,
1520 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001521 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001522 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001523 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001524 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001525 }
1526
Andrew Walbrana65a1322020-04-06 19:32:32 +01001527 /* Set flag for share function, ready to be retrieved later. */
1528 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001529 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001530 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001531 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001532 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001533 case FFA_MEM_LEND_32:
1534 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001535 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001536 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001537 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001538 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001539 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001540 }
1541
Andrew Walbranca808b12020-05-15 17:22:28 +01001542 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001543 /*
1544 * Allocate a share state before updating the page table. Otherwise if
1545 * updating the page table succeeded but allocating the share state
1546 * failed then it would leave the memory in a state where nobody could
1547 * get it back.
1548 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001549 if (!allocate_share_state(share_states, share_func, memory_region,
1550 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1551 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001552 dlog_verbose("Failed to allocate share state.\n");
1553 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001554 ret = ffa_error(FFA_NO_MEMORY);
1555 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001556 }
1557
Andrew Walbranca808b12020-05-15 17:22:28 +01001558 if (fragment_length == memory_share_length) {
1559 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00001560 ret = ffa_memory_send_complete(
1561 from_locked, share_states, share_state, page_pool,
1562 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001563 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01001564 /*
1565 * Use sender ID from 'memory_region' assuming
1566 * that at this point it has been validated:
1567 * - MBZ at virtual FF-A instance.
1568 */
1569 ffa_vm_id_t sender_to_ret =
1570 (from_locked.vm->id == HF_OTHER_WORLD_ID)
1571 ? memory_region->sender
1572 : 0;
Andrew Walbranca808b12020-05-15 17:22:28 +01001573 ret = (struct ffa_value){
1574 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00001575 .arg1 = (uint32_t)memory_region->handle,
1576 .arg2 = (uint32_t)(memory_region->handle >> 32),
J-Alvesfdd29272022-07-19 13:16:31 +01001577 .arg3 = fragment_length,
1578 .arg4 = (uint32_t)(sender_to_ret & 0xffff) << 16};
Andrew Walbranca808b12020-05-15 17:22:28 +01001579 }
1580
1581out:
1582 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001583 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001584 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001585}
1586
1587/**
J-Alves8505a8a2022-06-15 18:10:18 +01001588 * Continues an operation to donate, lend or share memory to a VM from current
1589 * world. If this is the last fragment then checks that the transition is valid
1590 * for the type of memory sending operation and updates the stage-2 page tables
1591 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01001592 *
1593 * Assumes that the caller has already found and locked the sender VM and copied
1594 * the memory region descriptor from the sender's TX buffer to a freshly
1595 * allocated page from Hafnium's internal pool.
1596 *
1597 * This function takes ownership of the `fragment` passed in; it must not be
1598 * freed by the caller.
1599 */
1600struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1601 void *fragment,
1602 uint32_t fragment_length,
1603 ffa_memory_handle_t handle,
1604 struct mpool *page_pool)
1605{
1606 struct share_states_locked share_states = share_states_lock();
1607 struct ffa_memory_share_state *share_state;
1608 struct ffa_value ret;
1609 struct ffa_memory_region *memory_region;
1610
1611 ret = ffa_memory_send_continue_validate(share_states, handle,
1612 &share_state,
1613 from_locked.vm->id, page_pool);
1614 if (ret.func != FFA_SUCCESS_32) {
1615 goto out_free_fragment;
1616 }
1617 memory_region = share_state->memory_region;
1618
J-Alves95df0ef2022-12-07 10:09:48 +00001619 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001620 dlog_error(
1621 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01001622 "other world. This should never happen, and indicates "
1623 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01001624 "EL3 code.\n");
1625 ret = ffa_error(FFA_INVALID_PARAMETERS);
1626 goto out_free_fragment;
1627 }
1628
1629 /* Add this fragment. */
1630 share_state->fragments[share_state->fragment_count] = fragment;
1631 share_state->fragment_constituent_counts[share_state->fragment_count] =
1632 fragment_length / sizeof(struct ffa_memory_region_constituent);
1633 share_state->fragment_count++;
1634
1635 /* Check whether the memory send operation is now ready to complete. */
1636 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00001637 ret = ffa_memory_send_complete(
1638 from_locked, share_states, share_state, page_pool,
1639 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001640 } else {
1641 ret = (struct ffa_value){
1642 .func = FFA_MEM_FRAG_RX_32,
1643 .arg1 = (uint32_t)handle,
1644 .arg2 = (uint32_t)(handle >> 32),
1645 .arg3 = share_state_next_fragment_offset(share_states,
1646 share_state)};
1647 }
1648 goto out;
1649
1650out_free_fragment:
1651 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001652
1653out:
Andrew Walbranca808b12020-05-15 17:22:28 +01001654 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001655 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001656}
1657
Andrew Walbranca808b12020-05-15 17:22:28 +01001658/** Clean up after the receiver has finished retrieving a memory region. */
1659static void ffa_memory_retrieve_complete(
1660 struct share_states_locked share_states,
1661 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
1662{
1663 if (share_state->share_func == FFA_MEM_DONATE_32) {
1664 /*
1665 * Memory that has been donated can't be relinquished,
1666 * so no need to keep the share state around.
1667 */
1668 share_state_free(share_states, share_state, page_pool);
1669 dlog_verbose("Freed share state for donate.\n");
1670 }
1671}
1672
J-Alves2d8457f2022-10-05 11:06:41 +01001673/**
1674 * Initialises the given memory region descriptor to be used for an
1675 * `FFA_MEM_RETRIEVE_RESP`, including the given constituents for the first
1676 * fragment.
1677 * The memory region descriptor is initialized according to retriever's
1678 * FF-A version.
1679 *
1680 * Returns true on success, or false if the given constituents won't all fit in
1681 * the first fragment.
1682 */
1683static bool ffa_retrieved_memory_region_init(
1684 void *response, uint32_t ffa_version, size_t response_max_size,
1685 ffa_vm_id_t sender, ffa_memory_attributes_t attributes,
1686 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
1687 ffa_vm_id_t receiver_id, ffa_memory_access_permissions_t permissions,
1688 uint32_t page_count, uint32_t total_constituent_count,
1689 const struct ffa_memory_region_constituent constituents[],
1690 uint32_t fragment_constituent_count, uint32_t *total_length,
1691 uint32_t *fragment_length)
1692{
1693 struct ffa_composite_memory_region *composite_memory_region;
1694 struct ffa_memory_access *receiver;
1695 uint32_t i;
1696 uint32_t constituents_offset;
1697 uint32_t receiver_count;
1698
1699 assert(response != NULL);
1700
1701 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1702 struct ffa_memory_region_v1_0 *retrieve_response =
1703 (struct ffa_memory_region_v1_0 *)response;
1704
J-Alves5da37d92022-10-24 16:33:48 +01001705 ffa_memory_region_init_header_v1_0(
1706 retrieve_response, sender, attributes, flags, handle, 0,
1707 RECEIVERS_COUNT_IN_RETRIEVE_RESP);
J-Alves2d8457f2022-10-05 11:06:41 +01001708
1709 receiver = &retrieve_response->receivers[0];
1710 receiver_count = retrieve_response->receiver_count;
1711
1712 receiver->composite_memory_region_offset =
1713 sizeof(struct ffa_memory_region_v1_0) +
1714 receiver_count * sizeof(struct ffa_memory_access);
1715
1716 composite_memory_region = ffa_memory_region_get_composite_v1_0(
1717 retrieve_response, 0);
1718 } else {
1719 /* Default to FF-A v1.1 version. */
1720 struct ffa_memory_region *retrieve_response =
1721 (struct ffa_memory_region *)response;
1722
1723 ffa_memory_region_init_header(retrieve_response, sender,
1724 attributes, flags, handle, 0, 1);
1725
1726 receiver = &retrieve_response->receivers[0];
1727 receiver_count = retrieve_response->receiver_count;
1728
1729 /*
1730 * Note that `sizeof(struct_ffa_memory_region)` and
1731 * `sizeof(struct ffa_memory_access)` must both be multiples of
1732 * 16 (as verified by the asserts in `ffa_memory.c`, so it is
1733 * guaranteed that the offset we calculate here is aligned to a
1734 * 64-bit boundary and so 64-bit values can be copied without
1735 * alignment faults.
1736 */
1737 receiver->composite_memory_region_offset =
1738 sizeof(struct ffa_memory_region) +
1739 receiver_count * sizeof(struct ffa_memory_access);
1740
1741 composite_memory_region =
1742 ffa_memory_region_get_composite(retrieve_response, 0);
1743 }
1744
1745 assert(receiver != NULL);
1746 assert(composite_memory_region != NULL);
1747
1748 /*
1749 * Initialized here as in memory retrieve responses we currently expect
1750 * one borrower to be specified.
1751 */
1752 ffa_memory_access_init_permissions(receiver, receiver_id, 0, 0, flags);
1753 receiver->receiver_permissions.permissions = permissions;
1754
1755 composite_memory_region->page_count = page_count;
1756 composite_memory_region->constituent_count = total_constituent_count;
1757 composite_memory_region->reserved_0 = 0;
1758
1759 constituents_offset = receiver->composite_memory_region_offset +
1760 sizeof(struct ffa_composite_memory_region);
1761 if (constituents_offset +
1762 fragment_constituent_count *
1763 sizeof(struct ffa_memory_region_constituent) >
1764 response_max_size) {
1765 return false;
1766 }
1767
1768 for (i = 0; i < fragment_constituent_count; ++i) {
1769 composite_memory_region->constituents[i] = constituents[i];
1770 }
1771
1772 if (total_length != NULL) {
1773 *total_length =
1774 constituents_offset +
1775 composite_memory_region->constituent_count *
1776 sizeof(struct ffa_memory_region_constituent);
1777 }
1778 if (fragment_length != NULL) {
1779 *fragment_length =
1780 constituents_offset +
1781 fragment_constituent_count *
1782 sizeof(struct ffa_memory_region_constituent);
1783 }
1784
1785 return true;
1786}
1787
J-Alves96de29f2022-04-26 16:05:24 +01001788/*
1789 * Gets the receiver's access permissions from 'struct ffa_memory_region' and
1790 * returns its index in the receiver's array. If receiver's ID doesn't exist
1791 * in the array, return the region's 'receiver_count'.
1792 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001793uint32_t ffa_memory_region_get_receiver(struct ffa_memory_region *memory_region,
1794 ffa_vm_id_t receiver)
J-Alves96de29f2022-04-26 16:05:24 +01001795{
1796 struct ffa_memory_access *receivers;
1797 uint32_t i;
1798
1799 assert(memory_region != NULL);
1800
1801 receivers = memory_region->receivers;
1802
1803 for (i = 0U; i < memory_region->receiver_count; i++) {
1804 if (receivers[i].receiver_permissions.receiver == receiver) {
1805 break;
1806 }
1807 }
1808
1809 return i;
1810}
1811
1812/**
1813 * Validates the retrieved permissions against those specified by the lender
1814 * of memory share operation. Optionally can help set the permissions to be used
1815 * for the S2 mapping, through the `permissions` argument.
1816 * Returns true if permissions are valid, false otherwise.
1817 */
1818static bool ffa_memory_retrieve_is_memory_access_valid(
1819 enum ffa_data_access sent_data_access,
1820 enum ffa_data_access requested_data_access,
1821 enum ffa_instruction_access sent_instruction_access,
1822 enum ffa_instruction_access requested_instruction_access,
1823 ffa_memory_access_permissions_t *permissions)
1824{
1825 switch (sent_data_access) {
1826 case FFA_DATA_ACCESS_NOT_SPECIFIED:
1827 case FFA_DATA_ACCESS_RW:
1828 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
1829 requested_data_access == FFA_DATA_ACCESS_RW) {
1830 if (permissions != NULL) {
1831 ffa_set_data_access_attr(permissions,
1832 FFA_DATA_ACCESS_RW);
1833 }
1834 break;
1835 }
1836 /* Intentional fall-through. */
1837 case FFA_DATA_ACCESS_RO:
1838 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
1839 requested_data_access == FFA_DATA_ACCESS_RO) {
1840 if (permissions != NULL) {
1841 ffa_set_data_access_attr(permissions,
1842 FFA_DATA_ACCESS_RO);
1843 }
1844 break;
1845 }
1846 dlog_verbose(
1847 "Invalid data access requested; sender specified "
1848 "permissions %#x but receiver requested %#x.\n",
1849 sent_data_access, requested_data_access);
1850 return false;
1851 case FFA_DATA_ACCESS_RESERVED:
1852 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
1853 "checked before this point.");
1854 }
1855
1856 switch (sent_instruction_access) {
1857 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
1858 case FFA_INSTRUCTION_ACCESS_X:
1859 if (requested_instruction_access ==
1860 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
1861 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
1862 if (permissions != NULL) {
1863 ffa_set_instruction_access_attr(
1864 permissions, FFA_INSTRUCTION_ACCESS_X);
1865 }
1866 break;
1867 }
1868 case FFA_INSTRUCTION_ACCESS_NX:
1869 if (requested_instruction_access ==
1870 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
1871 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
1872 if (permissions != NULL) {
1873 ffa_set_instruction_access_attr(
1874 permissions, FFA_INSTRUCTION_ACCESS_NX);
1875 }
1876 break;
1877 }
1878 dlog_verbose(
1879 "Invalid instruction access requested; sender "
1880 "specified permissions %#x but receiver requested "
1881 "%#x.\n",
1882 sent_instruction_access, requested_instruction_access);
1883 return false;
1884 case FFA_INSTRUCTION_ACCESS_RESERVED:
1885 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
1886 "be checked before this point.");
1887 }
1888
1889 return true;
1890}
1891
1892/**
1893 * Validate the receivers' permissions in the retrieve request against those
1894 * specified by the lender.
1895 * In the `permissions` argument returns the permissions to set at S2 for the
1896 * caller to the FFA_MEMORY_RETRIEVE_REQ.
1897 * Returns FFA_SUCCESS if all specified permissions are valid.
1898 */
1899static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
1900 struct ffa_memory_region *memory_region,
1901 struct ffa_memory_region *retrieve_request, ffa_vm_id_t to_vm_id,
1902 ffa_memory_access_permissions_t *permissions)
1903{
1904 uint32_t retrieve_receiver_index;
1905
1906 assert(permissions != NULL);
1907
1908 if (retrieve_request->receiver_count != memory_region->receiver_count) {
1909 dlog_verbose(
1910 "Retrieve request should contain same list of "
1911 "borrowers, as specified by the lender.\n");
1912 return ffa_error(FFA_INVALID_PARAMETERS);
1913 }
1914
1915 retrieve_receiver_index = retrieve_request->receiver_count;
1916
1917 /* Should be populated with the permissions of the retriever. */
1918 *permissions = 0;
1919
1920 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
1921 ffa_memory_access_permissions_t sent_permissions;
1922 struct ffa_memory_access *current_receiver =
1923 &retrieve_request->receivers[i];
1924 ffa_memory_access_permissions_t requested_permissions =
1925 current_receiver->receiver_permissions.permissions;
1926 ffa_vm_id_t current_receiver_id =
1927 current_receiver->receiver_permissions.receiver;
1928 bool found_to_id = current_receiver_id == to_vm_id;
1929
1930 /*
1931 * Find the current receiver in the transaction descriptor from
1932 * sender.
1933 */
1934 uint32_t mem_region_receiver_index =
1935 ffa_memory_region_get_receiver(memory_region,
1936 current_receiver_id);
1937
1938 if (mem_region_receiver_index ==
1939 memory_region->receiver_count) {
1940 dlog_verbose("%s: receiver %x not found\n", __func__,
1941 current_receiver_id);
1942 return ffa_error(FFA_DENIED);
1943 }
1944
1945 sent_permissions =
1946 memory_region->receivers[mem_region_receiver_index]
1947 .receiver_permissions.permissions;
1948
1949 if (found_to_id) {
1950 retrieve_receiver_index = i;
1951 }
1952
1953 /*
1954 * Since we are traversing the list of receivers, save the index
1955 * of the caller. As it needs to be there.
1956 */
1957
1958 if (current_receiver->composite_memory_region_offset != 0U) {
1959 dlog_verbose(
1960 "Retriever specified address ranges not "
1961 "supported (got offset %d).\n",
1962 current_receiver
1963 ->composite_memory_region_offset);
1964 return ffa_error(FFA_INVALID_PARAMETERS);
1965 }
1966
1967 /*
1968 * Check permissions from sender against permissions requested
1969 * by receiver.
1970 */
1971 if (!ffa_memory_retrieve_is_memory_access_valid(
1972 ffa_get_data_access_attr(sent_permissions),
1973 ffa_get_data_access_attr(requested_permissions),
1974 ffa_get_instruction_access_attr(sent_permissions),
1975 ffa_get_instruction_access_attr(
1976 requested_permissions),
1977 found_to_id ? permissions : NULL)) {
1978 return ffa_error(FFA_DENIED);
1979 }
1980
1981 /*
1982 * Can't request PM to clear memory if only provided with RO
1983 * permissions.
1984 */
1985 if (found_to_id &&
1986 (ffa_get_data_access_attr(*permissions) ==
1987 FFA_DATA_ACCESS_RO) &&
1988 (retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
1989 0U) {
1990 dlog_verbose(
1991 "Receiver has RO permissions can not request "
1992 "clear.\n");
1993 return ffa_error(FFA_DENIED);
1994 }
1995 }
1996
1997 if (retrieve_receiver_index == retrieve_request->receiver_count) {
1998 dlog_verbose(
1999 "Retrieve request does not contain caller's (%x) "
2000 "permissions\n",
2001 to_vm_id);
2002 return ffa_error(FFA_INVALID_PARAMETERS);
2003 }
2004
2005 return (struct ffa_value){.func = FFA_SUCCESS_32};
2006}
2007
J-Alvesa9cd7e32022-07-01 13:49:33 +01002008/*
2009 * According to section 16.4.3 of FF-A v1.1 EAC0 specification, the hypervisor
2010 * may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region description
2011 * of a pending memory sharing operation whose allocator is the SPM, for
2012 * validation purposes before forwarding an FFA_MEM_RECLAIM call. In doing so
2013 * the memory region descriptor of the retrieve request must be zeroed with the
2014 * exception of the sender ID and handle.
2015 */
2016bool is_ffa_memory_retrieve_borrower_request(struct ffa_memory_region *request,
2017 struct vm_locked to_locked)
2018{
2019 return to_locked.vm->id == HF_HYPERVISOR_VM_ID &&
2020 request->attributes == 0U && request->flags == 0U &&
2021 request->tag == 0U && request->receiver_count == 0U &&
2022 plat_ffa_memory_handle_allocated_by_current_world(
2023 request->handle);
2024}
2025
2026/*
2027 * Helper to reset count of fragments retrieved by the hypervisor.
2028 */
2029static void ffa_memory_retrieve_complete_from_hyp(
2030 struct ffa_memory_share_state *share_state)
2031{
2032 if (share_state->hypervisor_fragment_count ==
2033 share_state->fragment_count) {
2034 share_state->hypervisor_fragment_count = 0;
2035 }
2036}
2037
J-Alves089004f2022-07-13 14:25:44 +01002038/**
2039 * Validate that the memory region descriptor provided by the borrower on
2040 * FFA_MEM_RETRIEVE_REQ, against saved memory region provided by lender at the
2041 * memory sharing call.
2042 */
2043static struct ffa_value ffa_memory_retrieve_validate(
2044 ffa_vm_id_t receiver_id, struct ffa_memory_region *retrieve_request,
2045 struct ffa_memory_region *memory_region, uint32_t *receiver_index,
2046 uint32_t share_func)
2047{
2048 ffa_memory_region_flags_t transaction_type =
2049 retrieve_request->flags &
2050 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002051 enum ffa_memory_security security_state;
J-Alves089004f2022-07-13 14:25:44 +01002052
2053 assert(retrieve_request != NULL);
2054 assert(memory_region != NULL);
2055 assert(receiver_index != NULL);
2056 assert(retrieve_request->sender == memory_region->sender);
2057
2058 /*
2059 * Check that the transaction type expected by the receiver is
2060 * correct, if it has been specified.
2061 */
2062 if (transaction_type !=
2063 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
2064 transaction_type != (memory_region->flags &
2065 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
2066 dlog_verbose(
2067 "Incorrect transaction type %#x for "
2068 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
2069 transaction_type,
2070 memory_region->flags &
2071 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
2072 retrieve_request->handle);
2073 return ffa_error(FFA_INVALID_PARAMETERS);
2074 }
2075
2076 if (retrieve_request->tag != memory_region->tag) {
2077 dlog_verbose(
2078 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
2079 "%d for handle %#x.\n",
2080 retrieve_request->tag, memory_region->tag,
2081 retrieve_request->handle);
2082 return ffa_error(FFA_INVALID_PARAMETERS);
2083 }
2084
2085 *receiver_index =
2086 ffa_memory_region_get_receiver(memory_region, receiver_id);
2087
2088 if (*receiver_index == memory_region->receiver_count) {
2089 dlog_verbose(
2090 "Incorrect receiver VM ID %d for "
2091 "FFA_MEM_RETRIEVE_REQ, for handle %#x.\n",
J-Alves59ed0042022-07-28 18:26:41 +01002092 receiver_id, memory_region->handle);
J-Alves089004f2022-07-13 14:25:44 +01002093 return ffa_error(FFA_INVALID_PARAMETERS);
2094 }
2095
2096 if ((retrieve_request->flags &
2097 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
2098 dlog_verbose(
2099 "Retriever specified 'address range alignment 'hint' "
2100 "not supported.\n");
2101 return ffa_error(FFA_INVALID_PARAMETERS);
2102 }
2103 if ((retrieve_request->flags &
2104 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
2105 dlog_verbose(
2106 "Bits 8-5 must be zero in memory region's flags "
2107 "(address range alignment hint not supported).\n");
2108 return ffa_error(FFA_INVALID_PARAMETERS);
2109 }
2110
2111 if ((retrieve_request->flags & ~0x7FF) != 0U) {
2112 dlog_verbose(
2113 "Bits 31-10 must be zero in memory region's flags.\n");
2114 return ffa_error(FFA_INVALID_PARAMETERS);
2115 }
2116
2117 if (share_func == FFA_MEM_SHARE_32 &&
2118 (retrieve_request->flags &
2119 (FFA_MEMORY_REGION_FLAG_CLEAR |
2120 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
2121 dlog_verbose(
2122 "Memory Share operation can't clean after relinquish "
2123 "memory region.\n");
2124 return ffa_error(FFA_INVALID_PARAMETERS);
2125 }
2126
2127 /*
2128 * If the borrower needs the memory to be cleared before mapping
2129 * to its address space, the sender should have set the flag
2130 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
2131 * FFA_DENIED.
2132 */
2133 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
2134 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
2135 dlog_verbose(
2136 "Borrower needs memory cleared. Sender needs to set "
2137 "flag for clearing memory.\n");
2138 return ffa_error(FFA_DENIED);
2139 }
2140
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002141 /* Memory region attributes NS-Bit MBZ for FFA_MEM_RETRIEVE_REQ. */
2142 security_state =
2143 ffa_get_memory_security_attr(retrieve_request->attributes);
2144 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
2145 dlog_verbose(
2146 "Invalid security state for memory retrieve request "
2147 "operation.\n");
2148 return ffa_error(FFA_INVALID_PARAMETERS);
2149 }
2150
J-Alves089004f2022-07-13 14:25:44 +01002151 /*
2152 * If memory type is not specified, bypass validation of memory
2153 * attributes in the retrieve request. The retriever is expecting to
2154 * obtain this information from the SPMC.
2155 */
2156 if (ffa_get_memory_type_attr(retrieve_request->attributes) ==
2157 FFA_MEMORY_NOT_SPECIFIED_MEM) {
2158 return (struct ffa_value){.func = FFA_SUCCESS_32};
2159 }
2160
2161 /*
2162 * Ensure receiver's attributes are compatible with how
2163 * Hafnium maps memory: Normal Memory, Inner shareable,
2164 * Write-Back Read-Allocate Write-Allocate Cacheable.
2165 */
2166 return ffa_memory_attributes_validate(retrieve_request->attributes);
2167}
2168
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002169struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2170 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002171 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002172 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002173{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002174 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002175 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002176 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002177 sizeof(struct ffa_memory_access);
2178 ffa_memory_handle_t handle = retrieve_request->handle;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002179 struct ffa_memory_region *memory_region;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002180 ffa_memory_access_permissions_t permissions = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002181 uint32_t memory_to_attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002182 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002183 struct ffa_memory_share_state *share_state;
2184 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002185 struct ffa_composite_memory_region *composite;
2186 uint32_t total_length;
2187 uint32_t fragment_length;
J-Alves089004f2022-07-13 14:25:44 +01002188 ffa_vm_id_t receiver_id = to_locked.vm->id;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002189 bool is_send_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002190
2191 dump_share_states();
2192
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002193 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002194 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002195 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002196 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002197 expected_retrieve_request_length,
2198 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002199 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002200 }
2201
2202 share_states = share_states_lock();
2203 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002204 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002205 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002206 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002207 goto out;
2208 }
2209
J-Alves96de29f2022-04-26 16:05:24 +01002210 if (!share_state->sending_complete) {
2211 dlog_verbose(
2212 "Memory with handle %#x not fully sent, can't "
2213 "retrieve.\n",
2214 handle);
2215 ret = ffa_error(FFA_INVALID_PARAMETERS);
2216 goto out;
2217 }
2218
Andrew Walbrana65a1322020-04-06 19:32:32 +01002219 memory_region = share_state->memory_region;
J-Alves089004f2022-07-13 14:25:44 +01002220
Andrew Walbrana65a1322020-04-06 19:32:32 +01002221 CHECK(memory_region != NULL);
2222
J-Alves089004f2022-07-13 14:25:44 +01002223 if (retrieve_request->sender != memory_region->sender) {
2224 dlog_verbose(
2225 "Memory with handle %#x not fully sent, can't "
2226 "retrieve.\n",
2227 handle);
2228 ret = ffa_error(FFA_INVALID_PARAMETERS);
2229 goto out;
2230 }
J-Alves96de29f2022-04-26 16:05:24 +01002231
J-Alvesa9cd7e32022-07-01 13:49:33 +01002232 if (!is_ffa_memory_retrieve_borrower_request(retrieve_request,
2233 to_locked)) {
2234 uint32_t receiver_index;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002235
J-Alvesb5084cf2022-07-06 14:20:12 +01002236 /*
2237 * The SPMC can only process retrieve requests to memory share
2238 * operations with one borrower from the other world. It can't
2239 * determine the ID of the NWd VM that invoked the retrieve
2240 * request interface call. It relies on the hypervisor to
2241 * validate the caller's ID against that provided in the
2242 * `receivers` list of the retrieve response.
2243 * In case there is only one borrower from the NWd in the
2244 * transaction descriptor, record that in the `receiver_id` for
2245 * later use, and validate in the retrieve request message.
2246 */
2247 if (to_locked.vm->id == HF_HYPERVISOR_VM_ID) {
2248 uint32_t other_world_count = 0;
2249
2250 for (uint32_t i = 0; i < memory_region->receiver_count;
2251 i++) {
2252 receiver_id =
2253 retrieve_request->receivers[0]
2254 .receiver_permissions.receiver;
2255 if (!vm_id_is_current_world(receiver_id)) {
2256 other_world_count++;
2257 }
2258 }
2259 if (other_world_count > 1) {
2260 dlog_verbose(
2261 "Support one receiver from the other "
2262 "world.\n");
2263 return ffa_error(FFA_NOT_SUPPORTED);
2264 }
2265 }
2266
2267 /*
2268 * Validate retrieve request, according to what was sent by the
2269 * sender. Function will output the `receiver_index` from the
2270 * provided memory region, and will output `permissions` from
2271 * the validated requested permissions.
2272 */
J-Alves089004f2022-07-13 14:25:44 +01002273 ret = ffa_memory_retrieve_validate(
2274 receiver_id, retrieve_request, memory_region,
2275 &receiver_index, share_state->share_func);
2276 if (ret.func != FFA_SUCCESS_32) {
J-Alvesa9cd7e32022-07-01 13:49:33 +01002277 goto out;
2278 }
2279
2280 if (share_state->retrieved_fragment_count[receiver_index] !=
2281 0U) {
2282 dlog_verbose(
2283 "Memory with handle %#x already retrieved.\n",
2284 handle);
2285 ret = ffa_error(FFA_DENIED);
2286 goto out;
2287 }
2288
J-Alvesa9cd7e32022-07-01 13:49:33 +01002289 ret = ffa_memory_retrieve_validate_memory_access_list(
2290 memory_region, retrieve_request, receiver_id,
2291 &permissions);
J-Alves614d9f42022-06-28 14:03:10 +01002292 if (ret.func != FFA_SUCCESS_32) {
2293 goto out;
2294 }
Federico Recanatia98603a2021-12-20 18:04:03 +01002295
J-Alvesa9cd7e32022-07-01 13:49:33 +01002296 memory_to_attributes = ffa_memory_permissions_to_mode(
2297 permissions, share_state->sender_orig_mode);
J-Alves40e260e2022-09-22 17:52:43 +01002298
2299 if (to_locked.vm->el0_partition) {
2300 /*
2301 * Get extra mapping attributes for the given VM ID.
2302 * If the memory is shared by a VM executing in non
2303 * secure world, attribute MM_MODE_NS has to be set
2304 * while mapping that in a SP executing in secure world.
2305 */
2306 memory_to_attributes |=
2307 arch_mm_extra_attributes_from_vm(
2308 retrieve_request->sender);
2309 }
2310
J-Alvesa9cd7e32022-07-01 13:49:33 +01002311 ret = ffa_retrieve_check_update(
2312 to_locked, memory_region->sender,
2313 share_state->fragments,
2314 share_state->fragment_constituent_counts,
2315 share_state->fragment_count, memory_to_attributes,
2316 share_state->share_func, false, page_pool);
2317
2318 if (ret.func != FFA_SUCCESS_32) {
2319 goto out;
2320 }
2321
2322 share_state->retrieved_fragment_count[receiver_index] = 1;
2323 is_send_complete =
2324 share_state->retrieved_fragment_count[receiver_index] ==
2325 share_state->fragment_count;
2326 } else {
2327 if (share_state->hypervisor_fragment_count != 0U) {
2328 dlog_verbose(
J-Alvesb5084cf2022-07-06 14:20:12 +01002329 "Memory with handle %#x already retrieved by "
J-Alvesa9cd7e32022-07-01 13:49:33 +01002330 "the hypervisor.\n",
2331 handle);
2332 ret = ffa_error(FFA_DENIED);
2333 goto out;
2334 }
2335
2336 share_state->hypervisor_fragment_count = 1;
2337
2338 ffa_memory_retrieve_complete_from_hyp(share_state);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002339 }
2340
J-Alvesb5084cf2022-07-06 14:20:12 +01002341 /* VMs acquire the RX buffer from SPMC. */
2342 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
2343
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002344 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002345 * Copy response to RX buffer of caller and deliver the message.
2346 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002347 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002348 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002349 composite = ffa_memory_region_get_composite(memory_region, 0);
2350 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002351 * Constituents which we received in the first fragment should
2352 * always fit in the first fragment we are sending, because the
2353 * header is the same size in both cases and we have a fixed
2354 * message buffer size. So `ffa_retrieved_memory_region_init`
2355 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01002356 */
2357 CHECK(ffa_retrieved_memory_region_init(
J-Alves2d8457f2022-10-05 11:06:41 +01002358 to_locked.vm->mailbox.recv, to_locked.vm->ffa_version,
2359 HF_MAILBOX_SIZE, memory_region->sender,
2360 memory_region->attributes, memory_region->flags, handle,
2361 receiver_id, permissions, composite->page_count,
2362 composite->constituent_count, share_state->fragments[0],
Andrew Walbranca808b12020-05-15 17:22:28 +01002363 share_state->fragment_constituent_counts[0], &total_length,
2364 &fragment_length));
J-Alvesb5084cf2022-07-06 14:20:12 +01002365
Andrew Walbranca808b12020-05-15 17:22:28 +01002366 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002367 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002368 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00002369 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002370
J-Alvesa9cd7e32022-07-01 13:49:33 +01002371 if (is_send_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002372 ffa_memory_retrieve_complete(share_states, share_state,
2373 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002374 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002375 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002376 .arg1 = total_length,
2377 .arg2 = fragment_length};
Andrew Walbranca808b12020-05-15 17:22:28 +01002378out:
2379 share_states_unlock(&share_states);
2380 dump_share_states();
2381 return ret;
2382}
2383
J-Alves5da37d92022-10-24 16:33:48 +01002384/**
2385 * Determine expected fragment offset according to the FF-A version of
2386 * the caller.
2387 */
2388static uint32_t ffa_memory_retrieve_expected_offset_per_ffa_version(
2389 struct ffa_memory_region *memory_region,
2390 uint32_t retrieved_constituents_count, uint32_t ffa_version)
2391{
2392 uint32_t expected_fragment_offset;
2393 uint32_t composite_constituents_offset;
2394
2395 if (ffa_version == MAKE_FFA_VERSION(1, 1)) {
2396 /*
2397 * Hafnium operates memory regions in FF-A v1.1 format, so we
2398 * can retrieve the constituents offset from descriptor.
2399 */
2400 composite_constituents_offset =
2401 ffa_composite_constituent_offset(memory_region, 0);
2402 } else if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
2403 /*
2404 * If retriever is FF-A v1.0, determine the composite offset
2405 * as it is expected to have been configured in the
2406 * retrieve response.
2407 */
2408 composite_constituents_offset =
2409 sizeof(struct ffa_memory_region_v1_0) +
2410 RECEIVERS_COUNT_IN_RETRIEVE_RESP *
2411 sizeof(struct ffa_memory_access) +
2412 sizeof(struct ffa_composite_memory_region);
2413 } else {
2414 panic("%s received an invalid FF-A version.\n", __func__);
2415 }
2416
2417 expected_fragment_offset =
2418 composite_constituents_offset +
2419 retrieved_constituents_count *
2420 sizeof(struct ffa_memory_region_constituent) -
2421 sizeof(struct ffa_memory_access) *
2422 (memory_region->receiver_count - 1);
2423
2424 return expected_fragment_offset;
2425}
2426
Andrew Walbranca808b12020-05-15 17:22:28 +01002427struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2428 ffa_memory_handle_t handle,
2429 uint32_t fragment_offset,
J-Alves59ed0042022-07-28 18:26:41 +01002430 ffa_vm_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01002431 struct mpool *page_pool)
2432{
2433 struct ffa_memory_region *memory_region;
2434 struct share_states_locked share_states;
2435 struct ffa_memory_share_state *share_state;
2436 struct ffa_value ret;
2437 uint32_t fragment_index;
2438 uint32_t retrieved_constituents_count;
2439 uint32_t i;
2440 uint32_t expected_fragment_offset;
2441 uint32_t remaining_constituent_count;
2442 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01002443 uint32_t receiver_index;
J-Alves59ed0042022-07-28 18:26:41 +01002444 bool continue_ffa_hyp_mem_retrieve_req;
Andrew Walbranca808b12020-05-15 17:22:28 +01002445
2446 dump_share_states();
2447
2448 share_states = share_states_lock();
2449 if (!get_share_state(share_states, handle, &share_state)) {
2450 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2451 handle);
2452 ret = ffa_error(FFA_INVALID_PARAMETERS);
2453 goto out;
2454 }
2455
2456 memory_region = share_state->memory_region;
2457 CHECK(memory_region != NULL);
2458
Andrew Walbranca808b12020-05-15 17:22:28 +01002459 if (!share_state->sending_complete) {
2460 dlog_verbose(
2461 "Memory with handle %#x not fully sent, can't "
2462 "retrieve.\n",
2463 handle);
2464 ret = ffa_error(FFA_INVALID_PARAMETERS);
2465 goto out;
2466 }
2467
J-Alves59ed0042022-07-28 18:26:41 +01002468 /*
2469 * If retrieve request from the hypervisor has been initiated in the
2470 * given share_state, continue it, else assume it is a continuation of
2471 * retrieve request from a NWd VM.
2472 */
2473 continue_ffa_hyp_mem_retrieve_req =
2474 (to_locked.vm->id == HF_HYPERVISOR_VM_ID) &&
2475 (share_state->hypervisor_fragment_count != 0U) &&
2476 plat_ffa_is_vm_id(sender_vm_id);
Andrew Walbranca808b12020-05-15 17:22:28 +01002477
J-Alves59ed0042022-07-28 18:26:41 +01002478 if (!continue_ffa_hyp_mem_retrieve_req) {
2479 receiver_index = ffa_memory_region_get_receiver(
2480 memory_region, to_locked.vm->id);
2481
2482 if (receiver_index == memory_region->receiver_count) {
2483 dlog_verbose(
2484 "Caller of FFA_MEM_FRAG_RX (%x) is not a "
2485 "borrower to memory sharing transaction (%x)\n",
2486 to_locked.vm->id, handle);
2487 ret = ffa_error(FFA_INVALID_PARAMETERS);
2488 goto out;
2489 }
2490
2491 if (share_state->retrieved_fragment_count[receiver_index] ==
2492 0 ||
2493 share_state->retrieved_fragment_count[receiver_index] >=
2494 share_state->fragment_count) {
2495 dlog_verbose(
2496 "Retrieval of memory with handle %#x not yet "
2497 "started or already completed (%d/%d fragments "
2498 "retrieved).\n",
2499 handle,
2500 share_state->retrieved_fragment_count
2501 [receiver_index],
2502 share_state->fragment_count);
2503 ret = ffa_error(FFA_INVALID_PARAMETERS);
2504 goto out;
2505 }
2506
2507 fragment_index =
2508 share_state->retrieved_fragment_count[receiver_index];
2509 } else {
2510 if (share_state->hypervisor_fragment_count == 0 ||
2511 share_state->hypervisor_fragment_count >=
2512 share_state->fragment_count) {
2513 dlog_verbose(
2514 "Retrieve of memory with handle %x not "
2515 "started from hypervisor.\n",
2516 handle);
2517 ret = ffa_error(FFA_INVALID_PARAMETERS);
2518 goto out;
2519 }
2520
2521 if (memory_region->sender != sender_vm_id) {
2522 dlog_verbose(
2523 "Sender ID (%x) is not as expected for memory "
2524 "handle %x\n",
2525 sender_vm_id, handle);
2526 ret = ffa_error(FFA_INVALID_PARAMETERS);
2527 goto out;
2528 }
2529
2530 fragment_index = share_state->hypervisor_fragment_count;
2531
2532 receiver_index = 0;
2533 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002534
2535 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002536 * Check that the given fragment offset is correct by counting
2537 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01002538 */
2539 retrieved_constituents_count = 0;
2540 for (i = 0; i < fragment_index; ++i) {
2541 retrieved_constituents_count +=
2542 share_state->fragment_constituent_counts[i];
2543 }
J-Alvesc7484f12022-05-13 12:41:14 +01002544
2545 CHECK(memory_region->receiver_count > 0);
2546
Andrew Walbranca808b12020-05-15 17:22:28 +01002547 expected_fragment_offset =
J-Alves5da37d92022-10-24 16:33:48 +01002548 ffa_memory_retrieve_expected_offset_per_ffa_version(
2549 memory_region, retrieved_constituents_count,
2550 to_locked.vm->ffa_version);
2551
Andrew Walbranca808b12020-05-15 17:22:28 +01002552 if (fragment_offset != expected_fragment_offset) {
2553 dlog_verbose("Fragment offset was %d but expected %d.\n",
2554 fragment_offset, expected_fragment_offset);
2555 ret = ffa_error(FFA_INVALID_PARAMETERS);
2556 goto out;
2557 }
2558
J-Alves59ed0042022-07-28 18:26:41 +01002559 /* VMs acquire the RX buffer from SPMC. */
2560 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
2561
Andrew Walbranca808b12020-05-15 17:22:28 +01002562 remaining_constituent_count = ffa_memory_fragment_init(
2563 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2564 share_state->fragments[fragment_index],
2565 share_state->fragment_constituent_counts[fragment_index],
2566 &fragment_length);
2567 CHECK(remaining_constituent_count == 0);
2568 to_locked.vm->mailbox.recv_size = fragment_length;
2569 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2570 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00002571 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbranca808b12020-05-15 17:22:28 +01002572
J-Alves59ed0042022-07-28 18:26:41 +01002573 if (!continue_ffa_hyp_mem_retrieve_req) {
2574 share_state->retrieved_fragment_count[receiver_index]++;
2575 if (share_state->retrieved_fragment_count[receiver_index] ==
2576 share_state->fragment_count) {
2577 ffa_memory_retrieve_complete(share_states, share_state,
2578 page_pool);
2579 }
2580 } else {
2581 share_state->hypervisor_fragment_count++;
2582
2583 ffa_memory_retrieve_complete_from_hyp(share_state);
2584 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002585 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2586 .arg1 = (uint32_t)handle,
2587 .arg2 = (uint32_t)(handle >> 32),
2588 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002589
2590out:
2591 share_states_unlock(&share_states);
2592 dump_share_states();
2593 return ret;
2594}
2595
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002596struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002597 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002598 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002599{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002600 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002601 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002602 struct ffa_memory_share_state *share_state;
2603 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002604 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002605 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01002606 uint32_t receiver_index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002607
Andrew Walbrana65a1322020-04-06 19:32:32 +01002608 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002609 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002610 "Stream endpoints not supported (got %d "
2611 "endpoints on "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002612 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002613 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002614 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002615 }
2616
Andrew Walbrana65a1322020-04-06 19:32:32 +01002617 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002618 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002619 "VM ID %d in relinquish message doesn't match "
2620 "calling "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002621 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002622 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002623 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002624 }
2625
2626 dump_share_states();
2627
2628 share_states = share_states_lock();
2629 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002630 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002631 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002632 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002633 goto out;
2634 }
2635
Andrew Walbranca808b12020-05-15 17:22:28 +01002636 if (!share_state->sending_complete) {
2637 dlog_verbose(
2638 "Memory with handle %#x not fully sent, can't "
2639 "relinquish.\n",
2640 handle);
2641 ret = ffa_error(FFA_INVALID_PARAMETERS);
2642 goto out;
2643 }
2644
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002645 memory_region = share_state->memory_region;
2646 CHECK(memory_region != NULL);
2647
J-Alves8eb19162022-04-28 10:56:48 +01002648 receiver_index = ffa_memory_region_get_receiver(memory_region,
2649 from_locked.vm->id);
2650
2651 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002652 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002653 "VM ID %d tried to relinquish memory region "
2654 "with "
J-Alves8eb19162022-04-28 10:56:48 +01002655 "handle %#x and it is not a valid borrower.\n",
2656 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002657 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002658 goto out;
2659 }
2660
J-Alves8eb19162022-04-28 10:56:48 +01002661 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01002662 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002663 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002664 "Memory with handle %#x not yet fully "
2665 "retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01002666 "receiver %x can't relinquish.\n",
2667 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002668 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002669 goto out;
2670 }
2671
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002672 clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002673
2674 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002675 * Clear is not allowed for memory that was shared, as the
2676 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002677 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002678 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002679 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002680 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002681 goto out;
2682 }
2683
Andrew Walbranca808b12020-05-15 17:22:28 +01002684 ret = ffa_relinquish_check_update(
2685 from_locked, share_state->fragments,
2686 share_state->fragment_constituent_counts,
2687 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002688
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002689 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002690 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002691 * Mark memory handle as not retrieved, so it can be
2692 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002693 */
J-Alves8eb19162022-04-28 10:56:48 +01002694 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002695 }
2696
2697out:
2698 share_states_unlock(&share_states);
2699 dump_share_states();
2700 return ret;
2701}
2702
2703/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01002704 * Validates that the reclaim transition is allowed for the given
2705 * handle, updates the page table of the reclaiming VM, and frees the
2706 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002707 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002708struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002709 ffa_memory_handle_t handle,
2710 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002711 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002712{
2713 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002714 struct ffa_memory_share_state *share_state;
2715 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002716 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002717
2718 dump_share_states();
2719
2720 share_states = share_states_lock();
J-Alvesb5084cf2022-07-06 14:20:12 +01002721 if (get_share_state(share_states, handle, &share_state)) {
2722 memory_region = share_state->memory_region;
2723 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002724 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002725 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002726 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002727 goto out;
2728 }
2729
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002730 CHECK(memory_region != NULL);
2731
J-Alvesa9cd7e32022-07-01 13:49:33 +01002732 if (vm_id_is_current_world(to_locked.vm->id) &&
2733 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002734 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002735 "VM %#x attempted to reclaim memory handle %#x "
2736 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002737 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002738 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002739 goto out;
2740 }
2741
Andrew Walbranca808b12020-05-15 17:22:28 +01002742 if (!share_state->sending_complete) {
2743 dlog_verbose(
2744 "Memory with handle %#x not fully sent, can't "
2745 "reclaim.\n",
2746 handle);
2747 ret = ffa_error(FFA_INVALID_PARAMETERS);
2748 goto out;
2749 }
2750
J-Alves752236c2022-04-28 11:07:47 +01002751 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
2752 if (share_state->retrieved_fragment_count[i] != 0) {
2753 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002754 "Tried to reclaim memory handle %#x "
2755 "that has "
2756 "not been relinquished by all "
2757 "borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01002758 handle,
2759 memory_region->receivers[i]
2760 .receiver_permissions.receiver);
2761 ret = ffa_error(FFA_DENIED);
2762 goto out;
2763 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002764 }
2765
Andrew Walbranca808b12020-05-15 17:22:28 +01002766 ret = ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00002767 to_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002768 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00002769 share_state->fragment_count, share_state->sender_orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01002770 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002771
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002772 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002773 share_state_free(share_states, share_state, page_pool);
J-Alvesa9cd7e32022-07-01 13:49:33 +01002774 dlog_verbose(
2775 "Freed share state after successful "
2776 "reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002777 }
2778
2779out:
2780 share_states_unlock(&share_states);
2781 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002782}