blob: a7d94ec7f93d73f80a888f0253466d7b4af10628 [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;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001193 uint64_t receivers_end;
1194 uint64_t min_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001195 uint32_t composite_memory_region_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001196 uint32_t constituents_start;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001197 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001198 enum ffa_data_access data_access;
1199 enum ffa_instruction_access instruction_access;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001200 enum ffa_memory_security security_state;
Federico Recanatia98603a2021-12-20 18:04:03 +01001201 struct ffa_value ret;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001202 const size_t minimum_first_fragment_length =
1203 (sizeof(struct ffa_memory_region) +
1204 sizeof(struct ffa_memory_access) +
1205 sizeof(struct ffa_composite_memory_region));
1206
1207 if (fragment_length < minimum_first_fragment_length) {
1208 dlog_verbose("Fragment length %u too short (min %u).\n",
1209 (size_t)fragment_length,
1210 minimum_first_fragment_length);
1211 return ffa_error(FFA_INVALID_PARAMETERS);
1212 }
1213
1214 if (fragment_length > memory_share_length) {
1215 dlog_verbose(
1216 "Fragment length %u greater than total length %u.\n",
1217 (size_t)fragment_length, (size_t)memory_share_length);
1218 return ffa_error(FFA_INVALID_PARAMETERS);
1219 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001220
J-Alves0b6653d2022-04-22 13:17:38 +01001221 assert(memory_region->receivers_offset ==
1222 offsetof(struct ffa_memory_region, receivers));
1223 assert(memory_region->memory_access_desc_size ==
1224 sizeof(struct ffa_memory_access));
1225
J-Alves95df0ef2022-12-07 10:09:48 +00001226 /* The sender must match the caller. */
1227 if ((!vm_id_is_current_world(from_locked.vm->id) &&
1228 vm_id_is_current_world(memory_region->sender)) ||
1229 (vm_id_is_current_world(from_locked.vm->id) &&
1230 memory_region->sender != from_locked.vm->id)) {
1231 dlog_verbose("Invalid memory sender ID.\n");
1232 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001233 }
1234
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001235 if (memory_region->receiver_count <= 0) {
1236 dlog_verbose("No receivers!\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001237 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001238 }
1239
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001240 /*
1241 * Ensure that the composite header is within the memory bounds and
1242 * doesn't overlap the first part of the message. Cast to uint64_t
1243 * to prevent overflow.
1244 */
1245 receivers_end = ((uint64_t)sizeof(struct ffa_memory_access) *
1246 (uint64_t)memory_region->receiver_count) +
1247 sizeof(struct ffa_memory_region);
1248 min_length = receivers_end +
1249 sizeof(struct ffa_composite_memory_region) +
1250 sizeof(struct ffa_memory_region_constituent);
1251 if (min_length > memory_share_length) {
1252 dlog_verbose("Share too short: got %u but minimum is %u.\n",
1253 (size_t)memory_share_length, (size_t)min_length);
1254 return ffa_error(FFA_INVALID_PARAMETERS);
1255 }
1256
1257 composite_memory_region_offset =
1258 memory_region->receivers[0].composite_memory_region_offset;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001259
1260 /*
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001261 * Check that the composite memory region descriptor is after the access
1262 * descriptors, is at least 16-byte aligned, and fits in the first
1263 * fragment.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001264 */
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001265 if ((composite_memory_region_offset < receivers_end) ||
1266 (composite_memory_region_offset % 16 != 0) ||
1267 (composite_memory_region_offset >
1268 fragment_length - sizeof(struct ffa_composite_memory_region))) {
1269 dlog_verbose(
1270 "Invalid composite memory region descriptor offset "
1271 "%u.\n",
1272 (size_t)composite_memory_region_offset);
1273 return ffa_error(FFA_INVALID_PARAMETERS);
1274 }
1275
1276 /*
1277 * Compute the start of the constituent regions. Already checked
1278 * to be not more than fragment_length and thus not more than
1279 * memory_share_length.
1280 */
1281 constituents_start = composite_memory_region_offset +
1282 sizeof(struct ffa_composite_memory_region);
1283 constituents_length = memory_share_length - constituents_start;
1284
1285 /*
1286 * Check that the number of constituents is consistent with the length
1287 * of the constituent region.
1288 */
1289 composite = ffa_memory_region_get_composite(memory_region, 0);
1290 if ((constituents_length %
1291 sizeof(struct ffa_memory_region_constituent) !=
1292 0) ||
1293 ((constituents_length /
1294 sizeof(struct ffa_memory_region_constituent)) !=
1295 composite->constituent_count)) {
1296 dlog_verbose("Invalid length %u or composite offset %u.\n",
1297 (size_t)memory_share_length,
1298 (size_t)composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001299 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001300 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001301 if (fragment_length < memory_share_length &&
1302 fragment_length < HF_MAILBOX_SIZE) {
1303 dlog_warning(
1304 "Initial fragment length %d smaller than mailbox "
1305 "size.\n",
1306 fragment_length);
1307 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001308
Andrew Walbrana65a1322020-04-06 19:32:32 +01001309 /*
1310 * Clear is not allowed for memory sharing, as the sender still has
1311 * access to the memory.
1312 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001313 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1314 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001315 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001316 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001317 }
1318
1319 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001320 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001321 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001322 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001323 }
1324
J-Alves363f5722022-04-25 17:37:37 +01001325 /* Check that the permissions are valid, for each specified receiver. */
1326 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
1327 ffa_memory_access_permissions_t permissions =
1328 memory_region->receivers[i]
1329 .receiver_permissions.permissions;
1330 ffa_vm_id_t receiver_id =
1331 memory_region->receivers[i]
1332 .receiver_permissions.receiver;
1333
1334 if (memory_region->sender == receiver_id) {
1335 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001336 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001337 }
Federico Recanati85090c42021-12-15 13:17:54 +01001338
J-Alves363f5722022-04-25 17:37:37 +01001339 for (uint32_t j = i + 1; j < memory_region->receiver_count;
1340 j++) {
1341 if (receiver_id ==
1342 memory_region->receivers[j]
1343 .receiver_permissions.receiver) {
1344 dlog_verbose(
1345 "Repeated receiver(%x) in memory send "
1346 "operation.\n",
1347 memory_region->receivers[j]
1348 .receiver_permissions.receiver);
1349 return ffa_error(FFA_INVALID_PARAMETERS);
1350 }
1351 }
1352
1353 if (composite_memory_region_offset !=
1354 memory_region->receivers[i]
1355 .composite_memory_region_offset) {
1356 dlog_verbose(
1357 "All ffa_memory_access should point to the "
1358 "same composite memory region offset.\n");
1359 return ffa_error(FFA_INVALID_PARAMETERS);
1360 }
1361
1362 data_access = ffa_get_data_access_attr(permissions);
1363 instruction_access =
1364 ffa_get_instruction_access_attr(permissions);
1365 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1366 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
1367 dlog_verbose(
1368 "Reserved value for receiver permissions "
1369 "%#x.\n",
1370 permissions);
1371 return ffa_error(FFA_INVALID_PARAMETERS);
1372 }
1373 if (instruction_access !=
1374 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
1375 dlog_verbose(
1376 "Invalid instruction access permissions %#x "
1377 "for sending memory.\n",
1378 permissions);
1379 return ffa_error(FFA_INVALID_PARAMETERS);
1380 }
1381 if (share_func == FFA_MEM_SHARE_32) {
1382 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1383 dlog_verbose(
1384 "Invalid data access permissions %#x "
1385 "for sharing memory.\n",
1386 permissions);
1387 return ffa_error(FFA_INVALID_PARAMETERS);
1388 }
1389 /*
1390 * According to section 10.10.3 of the FF-A v1.1 EAC0
1391 * spec, NX is required for share operations (but must
1392 * not be specified by the sender) so set it in the
1393 * copy that we store, ready to be returned to the
1394 * retriever.
1395 */
J-Alvesb19731a2022-06-20 17:30:33 +01001396 if (vm_id_is_current_world(receiver_id)) {
1397 ffa_set_instruction_access_attr(
1398 &permissions,
1399 FFA_INSTRUCTION_ACCESS_NX);
1400 memory_region->receivers[i]
1401 .receiver_permissions.permissions =
1402 permissions;
1403 }
J-Alves363f5722022-04-25 17:37:37 +01001404 }
1405 if (share_func == FFA_MEM_LEND_32 &&
1406 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1407 dlog_verbose(
1408 "Invalid data access permissions %#x for "
1409 "lending memory.\n",
1410 permissions);
1411 return ffa_error(FFA_INVALID_PARAMETERS);
1412 }
1413
1414 if (share_func == FFA_MEM_DONATE_32 &&
1415 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
1416 dlog_verbose(
1417 "Invalid data access permissions %#x for "
1418 "donating memory.\n",
1419 permissions);
1420 return ffa_error(FFA_INVALID_PARAMETERS);
1421 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001422 }
1423
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001424 /* Memory region attributes NS-Bit MBZ for FFA_MEM_SHARE/LEND/DONATE. */
1425 security_state =
1426 ffa_get_memory_security_attr(memory_region->attributes);
1427 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
1428 dlog_verbose(
1429 "Invalid security state for memory share operation.\n");
1430 return ffa_error(FFA_INVALID_PARAMETERS);
1431 }
1432
Federico Recanatid937f5e2021-12-20 17:38:23 +01001433 /*
J-Alves807794e2022-06-16 13:42:47 +01001434 * If a memory donate or lend with single borrower, the memory type
1435 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01001436 */
J-Alves807794e2022-06-16 13:42:47 +01001437 if (share_func == FFA_MEM_DONATE_32 ||
1438 (share_func == FFA_MEM_LEND_32 &&
1439 memory_region->receiver_count == 1)) {
1440 if (ffa_get_memory_type_attr(memory_region->attributes) !=
1441 FFA_MEMORY_NOT_SPECIFIED_MEM) {
1442 dlog_verbose(
1443 "Memory type shall not be specified by "
1444 "sender.\n");
1445 return ffa_error(FFA_INVALID_PARAMETERS);
1446 }
1447 } else {
1448 /*
1449 * Check that sender's memory attributes match Hafnium
1450 * expectations: Normal Memory, Inner shareable, Write-Back
1451 * Read-Allocate Write-Allocate Cacheable.
1452 */
1453 ret = ffa_memory_attributes_validate(memory_region->attributes);
1454 if (ret.func != FFA_SUCCESS_32) {
1455 return ret;
1456 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01001457 }
1458
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001459 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001460}
1461
1462/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001463 * Gets the share state for continuing an operation to donate, lend or share
1464 * memory, and checks that it is a valid request.
1465 *
1466 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1467 * not.
1468 */
J-Alvesfdd29272022-07-19 13:16:31 +01001469struct ffa_value ffa_memory_send_continue_validate(
Andrew Walbranca808b12020-05-15 17:22:28 +01001470 struct share_states_locked share_states, ffa_memory_handle_t handle,
1471 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1472 struct mpool *page_pool)
1473{
1474 struct ffa_memory_share_state *share_state;
1475 struct ffa_memory_region *memory_region;
1476
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001477 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001478
1479 /*
1480 * Look up the share state by handle and make sure that the VM ID
1481 * matches.
1482 */
1483 if (!get_share_state(share_states, handle, &share_state)) {
1484 dlog_verbose(
1485 "Invalid handle %#x for memory send continuation.\n",
1486 handle);
1487 return ffa_error(FFA_INVALID_PARAMETERS);
1488 }
1489 memory_region = share_state->memory_region;
1490
J-Alvesfdd29272022-07-19 13:16:31 +01001491 if (vm_id_is_current_world(from_vm_id) &&
1492 memory_region->sender != from_vm_id) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001493 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1494 return ffa_error(FFA_INVALID_PARAMETERS);
1495 }
1496
1497 if (share_state->sending_complete) {
1498 dlog_verbose(
1499 "Sending of memory handle %#x is already complete.\n",
1500 handle);
1501 return ffa_error(FFA_INVALID_PARAMETERS);
1502 }
1503
1504 if (share_state->fragment_count == MAX_FRAGMENTS) {
1505 /*
1506 * Log a warning as this is a sign that MAX_FRAGMENTS should
1507 * probably be increased.
1508 */
1509 dlog_warning(
1510 "Too many fragments for memory share with handle %#x; "
1511 "only %d supported.\n",
1512 handle, MAX_FRAGMENTS);
1513 /* Free share state, as it's not possible to complete it. */
1514 share_state_free(share_states, share_state, page_pool);
1515 return ffa_error(FFA_NO_MEMORY);
1516 }
1517
1518 *share_state_ret = share_state;
1519
1520 return (struct ffa_value){.func = FFA_SUCCESS_32};
1521}
1522
1523/**
J-Alves95df0ef2022-12-07 10:09:48 +00001524 * Checks if there is at least one receiver from the other world.
1525 */
J-Alvesfdd29272022-07-19 13:16:31 +01001526bool memory_region_receivers_from_other_world(
J-Alves95df0ef2022-12-07 10:09:48 +00001527 struct ffa_memory_region *memory_region)
1528{
1529 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
1530 ffa_vm_id_t receiver = memory_region->receivers[i]
1531 .receiver_permissions.receiver;
1532 if (!vm_id_is_current_world(receiver)) {
1533 return true;
1534 }
1535 }
1536 return false;
1537}
1538
1539/**
J-Alves8505a8a2022-06-15 18:10:18 +01001540 * Validates a call to donate, lend or share memory to a non-other world VM and
1541 * then updates the stage-2 page tables. Specifically, check if the message
1542 * length and number of memory region constituents match, and if the transition
1543 * is valid for the type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001544 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001545 * Assumes that the caller has already found and locked the sender VM and copied
1546 * the memory region descriptor from the sender's TX buffer to a freshly
1547 * allocated page from Hafnium's internal pool. The caller must have also
1548 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001549 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001550 * This function takes ownership of the `memory_region` passed in and will free
1551 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001552 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001553struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001554 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001555 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001556 uint32_t fragment_length, uint32_t share_func,
1557 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001558{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001559 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001560 struct share_states_locked share_states;
1561 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001562
1563 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001564 * If there is an error validating the `memory_region` then we need to
1565 * free it because we own it but we won't be storing it in a share state
1566 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001567 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001568 ret = ffa_memory_send_validate(from_locked, memory_region,
1569 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001570 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001571 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001572 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001573 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001574 }
1575
Andrew Walbrana65a1322020-04-06 19:32:32 +01001576 /* Set flag for share function, ready to be retrieved later. */
1577 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001578 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001579 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001580 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001581 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001582 case FFA_MEM_LEND_32:
1583 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001584 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001585 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001586 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001587 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001588 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001589 }
1590
Andrew Walbranca808b12020-05-15 17:22:28 +01001591 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001592 /*
1593 * Allocate a share state before updating the page table. Otherwise if
1594 * updating the page table succeeded but allocating the share state
1595 * failed then it would leave the memory in a state where nobody could
1596 * get it back.
1597 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001598 if (!allocate_share_state(share_states, share_func, memory_region,
1599 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1600 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001601 dlog_verbose("Failed to allocate share state.\n");
1602 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001603 ret = ffa_error(FFA_NO_MEMORY);
1604 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001605 }
1606
Andrew Walbranca808b12020-05-15 17:22:28 +01001607 if (fragment_length == memory_share_length) {
1608 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00001609 ret = ffa_memory_send_complete(
1610 from_locked, share_states, share_state, page_pool,
1611 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001612 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01001613 /*
1614 * Use sender ID from 'memory_region' assuming
1615 * that at this point it has been validated:
1616 * - MBZ at virtual FF-A instance.
1617 */
1618 ffa_vm_id_t sender_to_ret =
1619 (from_locked.vm->id == HF_OTHER_WORLD_ID)
1620 ? memory_region->sender
1621 : 0;
Andrew Walbranca808b12020-05-15 17:22:28 +01001622 ret = (struct ffa_value){
1623 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00001624 .arg1 = (uint32_t)memory_region->handle,
1625 .arg2 = (uint32_t)(memory_region->handle >> 32),
J-Alvesfdd29272022-07-19 13:16:31 +01001626 .arg3 = fragment_length,
1627 .arg4 = (uint32_t)(sender_to_ret & 0xffff) << 16};
Andrew Walbranca808b12020-05-15 17:22:28 +01001628 }
1629
1630out:
1631 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001632 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001633 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001634}
1635
1636/**
J-Alves8505a8a2022-06-15 18:10:18 +01001637 * Continues an operation to donate, lend or share memory to a VM from current
1638 * world. If this is the last fragment then checks that the transition is valid
1639 * for the type of memory sending operation and updates the stage-2 page tables
1640 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01001641 *
1642 * Assumes that the caller has already found and locked the sender VM and copied
1643 * the memory region descriptor from the sender's TX buffer to a freshly
1644 * allocated page from Hafnium's internal pool.
1645 *
1646 * This function takes ownership of the `fragment` passed in; it must not be
1647 * freed by the caller.
1648 */
1649struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1650 void *fragment,
1651 uint32_t fragment_length,
1652 ffa_memory_handle_t handle,
1653 struct mpool *page_pool)
1654{
1655 struct share_states_locked share_states = share_states_lock();
1656 struct ffa_memory_share_state *share_state;
1657 struct ffa_value ret;
1658 struct ffa_memory_region *memory_region;
1659
1660 ret = ffa_memory_send_continue_validate(share_states, handle,
1661 &share_state,
1662 from_locked.vm->id, page_pool);
1663 if (ret.func != FFA_SUCCESS_32) {
1664 goto out_free_fragment;
1665 }
1666 memory_region = share_state->memory_region;
1667
J-Alves95df0ef2022-12-07 10:09:48 +00001668 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001669 dlog_error(
1670 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01001671 "other world. This should never happen, and indicates "
1672 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01001673 "EL3 code.\n");
1674 ret = ffa_error(FFA_INVALID_PARAMETERS);
1675 goto out_free_fragment;
1676 }
1677
1678 /* Add this fragment. */
1679 share_state->fragments[share_state->fragment_count] = fragment;
1680 share_state->fragment_constituent_counts[share_state->fragment_count] =
1681 fragment_length / sizeof(struct ffa_memory_region_constituent);
1682 share_state->fragment_count++;
1683
1684 /* Check whether the memory send operation is now ready to complete. */
1685 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00001686 ret = ffa_memory_send_complete(
1687 from_locked, share_states, share_state, page_pool,
1688 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001689 } else {
1690 ret = (struct ffa_value){
1691 .func = FFA_MEM_FRAG_RX_32,
1692 .arg1 = (uint32_t)handle,
1693 .arg2 = (uint32_t)(handle >> 32),
1694 .arg3 = share_state_next_fragment_offset(share_states,
1695 share_state)};
1696 }
1697 goto out;
1698
1699out_free_fragment:
1700 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001701
1702out:
Andrew Walbranca808b12020-05-15 17:22:28 +01001703 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001704 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001705}
1706
Andrew Walbranca808b12020-05-15 17:22:28 +01001707/** Clean up after the receiver has finished retrieving a memory region. */
1708static void ffa_memory_retrieve_complete(
1709 struct share_states_locked share_states,
1710 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
1711{
1712 if (share_state->share_func == FFA_MEM_DONATE_32) {
1713 /*
1714 * Memory that has been donated can't be relinquished,
1715 * so no need to keep the share state around.
1716 */
1717 share_state_free(share_states, share_state, page_pool);
1718 dlog_verbose("Freed share state for donate.\n");
1719 }
1720}
1721
J-Alves2d8457f2022-10-05 11:06:41 +01001722/**
1723 * Initialises the given memory region descriptor to be used for an
1724 * `FFA_MEM_RETRIEVE_RESP`, including the given constituents for the first
1725 * fragment.
1726 * The memory region descriptor is initialized according to retriever's
1727 * FF-A version.
1728 *
1729 * Returns true on success, or false if the given constituents won't all fit in
1730 * the first fragment.
1731 */
1732static bool ffa_retrieved_memory_region_init(
1733 void *response, uint32_t ffa_version, size_t response_max_size,
1734 ffa_vm_id_t sender, ffa_memory_attributes_t attributes,
1735 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
1736 ffa_vm_id_t receiver_id, ffa_memory_access_permissions_t permissions,
1737 uint32_t page_count, uint32_t total_constituent_count,
1738 const struct ffa_memory_region_constituent constituents[],
1739 uint32_t fragment_constituent_count, uint32_t *total_length,
1740 uint32_t *fragment_length)
1741{
1742 struct ffa_composite_memory_region *composite_memory_region;
1743 struct ffa_memory_access *receiver;
1744 uint32_t i;
1745 uint32_t constituents_offset;
1746 uint32_t receiver_count;
1747
1748 assert(response != NULL);
1749
1750 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1751 struct ffa_memory_region_v1_0 *retrieve_response =
1752 (struct ffa_memory_region_v1_0 *)response;
1753
J-Alves5da37d92022-10-24 16:33:48 +01001754 ffa_memory_region_init_header_v1_0(
1755 retrieve_response, sender, attributes, flags, handle, 0,
1756 RECEIVERS_COUNT_IN_RETRIEVE_RESP);
J-Alves2d8457f2022-10-05 11:06:41 +01001757
1758 receiver = &retrieve_response->receivers[0];
1759 receiver_count = retrieve_response->receiver_count;
1760
1761 receiver->composite_memory_region_offset =
1762 sizeof(struct ffa_memory_region_v1_0) +
1763 receiver_count * sizeof(struct ffa_memory_access);
1764
1765 composite_memory_region = ffa_memory_region_get_composite_v1_0(
1766 retrieve_response, 0);
1767 } else {
1768 /* Default to FF-A v1.1 version. */
1769 struct ffa_memory_region *retrieve_response =
1770 (struct ffa_memory_region *)response;
1771
1772 ffa_memory_region_init_header(retrieve_response, sender,
1773 attributes, flags, handle, 0, 1);
1774
1775 receiver = &retrieve_response->receivers[0];
1776 receiver_count = retrieve_response->receiver_count;
1777
1778 /*
1779 * Note that `sizeof(struct_ffa_memory_region)` and
1780 * `sizeof(struct ffa_memory_access)` must both be multiples of
1781 * 16 (as verified by the asserts in `ffa_memory.c`, so it is
1782 * guaranteed that the offset we calculate here is aligned to a
1783 * 64-bit boundary and so 64-bit values can be copied without
1784 * alignment faults.
1785 */
1786 receiver->composite_memory_region_offset =
1787 sizeof(struct ffa_memory_region) +
1788 receiver_count * sizeof(struct ffa_memory_access);
1789
1790 composite_memory_region =
1791 ffa_memory_region_get_composite(retrieve_response, 0);
1792 }
1793
1794 assert(receiver != NULL);
1795 assert(composite_memory_region != NULL);
1796
1797 /*
1798 * Initialized here as in memory retrieve responses we currently expect
1799 * one borrower to be specified.
1800 */
1801 ffa_memory_access_init_permissions(receiver, receiver_id, 0, 0, flags);
1802 receiver->receiver_permissions.permissions = permissions;
1803
1804 composite_memory_region->page_count = page_count;
1805 composite_memory_region->constituent_count = total_constituent_count;
1806 composite_memory_region->reserved_0 = 0;
1807
1808 constituents_offset = receiver->composite_memory_region_offset +
1809 sizeof(struct ffa_composite_memory_region);
1810 if (constituents_offset +
1811 fragment_constituent_count *
1812 sizeof(struct ffa_memory_region_constituent) >
1813 response_max_size) {
1814 return false;
1815 }
1816
1817 for (i = 0; i < fragment_constituent_count; ++i) {
1818 composite_memory_region->constituents[i] = constituents[i];
1819 }
1820
1821 if (total_length != NULL) {
1822 *total_length =
1823 constituents_offset +
1824 composite_memory_region->constituent_count *
1825 sizeof(struct ffa_memory_region_constituent);
1826 }
1827 if (fragment_length != NULL) {
1828 *fragment_length =
1829 constituents_offset +
1830 fragment_constituent_count *
1831 sizeof(struct ffa_memory_region_constituent);
1832 }
1833
1834 return true;
1835}
1836
J-Alves96de29f2022-04-26 16:05:24 +01001837/*
1838 * Gets the receiver's access permissions from 'struct ffa_memory_region' and
1839 * returns its index in the receiver's array. If receiver's ID doesn't exist
1840 * in the array, return the region's 'receiver_count'.
1841 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001842uint32_t ffa_memory_region_get_receiver(struct ffa_memory_region *memory_region,
1843 ffa_vm_id_t receiver)
J-Alves96de29f2022-04-26 16:05:24 +01001844{
1845 struct ffa_memory_access *receivers;
1846 uint32_t i;
1847
1848 assert(memory_region != NULL);
1849
1850 receivers = memory_region->receivers;
1851
1852 for (i = 0U; i < memory_region->receiver_count; i++) {
1853 if (receivers[i].receiver_permissions.receiver == receiver) {
1854 break;
1855 }
1856 }
1857
1858 return i;
1859}
1860
1861/**
1862 * Validates the retrieved permissions against those specified by the lender
1863 * of memory share operation. Optionally can help set the permissions to be used
1864 * for the S2 mapping, through the `permissions` argument.
1865 * Returns true if permissions are valid, false otherwise.
1866 */
1867static bool ffa_memory_retrieve_is_memory_access_valid(
1868 enum ffa_data_access sent_data_access,
1869 enum ffa_data_access requested_data_access,
1870 enum ffa_instruction_access sent_instruction_access,
1871 enum ffa_instruction_access requested_instruction_access,
1872 ffa_memory_access_permissions_t *permissions)
1873{
1874 switch (sent_data_access) {
1875 case FFA_DATA_ACCESS_NOT_SPECIFIED:
1876 case FFA_DATA_ACCESS_RW:
1877 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
1878 requested_data_access == FFA_DATA_ACCESS_RW) {
1879 if (permissions != NULL) {
1880 ffa_set_data_access_attr(permissions,
1881 FFA_DATA_ACCESS_RW);
1882 }
1883 break;
1884 }
1885 /* Intentional fall-through. */
1886 case FFA_DATA_ACCESS_RO:
1887 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
1888 requested_data_access == FFA_DATA_ACCESS_RO) {
1889 if (permissions != NULL) {
1890 ffa_set_data_access_attr(permissions,
1891 FFA_DATA_ACCESS_RO);
1892 }
1893 break;
1894 }
1895 dlog_verbose(
1896 "Invalid data access requested; sender specified "
1897 "permissions %#x but receiver requested %#x.\n",
1898 sent_data_access, requested_data_access);
1899 return false;
1900 case FFA_DATA_ACCESS_RESERVED:
1901 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
1902 "checked before this point.");
1903 }
1904
1905 switch (sent_instruction_access) {
1906 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
1907 case FFA_INSTRUCTION_ACCESS_X:
1908 if (requested_instruction_access ==
1909 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
1910 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
1911 if (permissions != NULL) {
1912 ffa_set_instruction_access_attr(
1913 permissions, FFA_INSTRUCTION_ACCESS_X);
1914 }
1915 break;
1916 }
1917 case FFA_INSTRUCTION_ACCESS_NX:
1918 if (requested_instruction_access ==
1919 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
1920 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
1921 if (permissions != NULL) {
1922 ffa_set_instruction_access_attr(
1923 permissions, FFA_INSTRUCTION_ACCESS_NX);
1924 }
1925 break;
1926 }
1927 dlog_verbose(
1928 "Invalid instruction access requested; sender "
1929 "specified permissions %#x but receiver requested "
1930 "%#x.\n",
1931 sent_instruction_access, requested_instruction_access);
1932 return false;
1933 case FFA_INSTRUCTION_ACCESS_RESERVED:
1934 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
1935 "be checked before this point.");
1936 }
1937
1938 return true;
1939}
1940
1941/**
1942 * Validate the receivers' permissions in the retrieve request against those
1943 * specified by the lender.
1944 * In the `permissions` argument returns the permissions to set at S2 for the
1945 * caller to the FFA_MEMORY_RETRIEVE_REQ.
1946 * Returns FFA_SUCCESS if all specified permissions are valid.
1947 */
1948static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
1949 struct ffa_memory_region *memory_region,
1950 struct ffa_memory_region *retrieve_request, ffa_vm_id_t to_vm_id,
1951 ffa_memory_access_permissions_t *permissions)
1952{
1953 uint32_t retrieve_receiver_index;
1954
1955 assert(permissions != NULL);
1956
1957 if (retrieve_request->receiver_count != memory_region->receiver_count) {
1958 dlog_verbose(
1959 "Retrieve request should contain same list of "
1960 "borrowers, as specified by the lender.\n");
1961 return ffa_error(FFA_INVALID_PARAMETERS);
1962 }
1963
1964 retrieve_receiver_index = retrieve_request->receiver_count;
1965
1966 /* Should be populated with the permissions of the retriever. */
1967 *permissions = 0;
1968
1969 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
1970 ffa_memory_access_permissions_t sent_permissions;
1971 struct ffa_memory_access *current_receiver =
1972 &retrieve_request->receivers[i];
1973 ffa_memory_access_permissions_t requested_permissions =
1974 current_receiver->receiver_permissions.permissions;
1975 ffa_vm_id_t current_receiver_id =
1976 current_receiver->receiver_permissions.receiver;
1977 bool found_to_id = current_receiver_id == to_vm_id;
1978
1979 /*
1980 * Find the current receiver in the transaction descriptor from
1981 * sender.
1982 */
1983 uint32_t mem_region_receiver_index =
1984 ffa_memory_region_get_receiver(memory_region,
1985 current_receiver_id);
1986
1987 if (mem_region_receiver_index ==
1988 memory_region->receiver_count) {
1989 dlog_verbose("%s: receiver %x not found\n", __func__,
1990 current_receiver_id);
1991 return ffa_error(FFA_DENIED);
1992 }
1993
1994 sent_permissions =
1995 memory_region->receivers[mem_region_receiver_index]
1996 .receiver_permissions.permissions;
1997
1998 if (found_to_id) {
1999 retrieve_receiver_index = i;
2000 }
2001
2002 /*
2003 * Since we are traversing the list of receivers, save the index
2004 * of the caller. As it needs to be there.
2005 */
2006
2007 if (current_receiver->composite_memory_region_offset != 0U) {
2008 dlog_verbose(
2009 "Retriever specified address ranges not "
2010 "supported (got offset %d).\n",
2011 current_receiver
2012 ->composite_memory_region_offset);
2013 return ffa_error(FFA_INVALID_PARAMETERS);
2014 }
2015
2016 /*
2017 * Check permissions from sender against permissions requested
2018 * by receiver.
2019 */
2020 if (!ffa_memory_retrieve_is_memory_access_valid(
2021 ffa_get_data_access_attr(sent_permissions),
2022 ffa_get_data_access_attr(requested_permissions),
2023 ffa_get_instruction_access_attr(sent_permissions),
2024 ffa_get_instruction_access_attr(
2025 requested_permissions),
2026 found_to_id ? permissions : NULL)) {
2027 return ffa_error(FFA_DENIED);
2028 }
2029
2030 /*
2031 * Can't request PM to clear memory if only provided with RO
2032 * permissions.
2033 */
2034 if (found_to_id &&
2035 (ffa_get_data_access_attr(*permissions) ==
2036 FFA_DATA_ACCESS_RO) &&
2037 (retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2038 0U) {
2039 dlog_verbose(
2040 "Receiver has RO permissions can not request "
2041 "clear.\n");
2042 return ffa_error(FFA_DENIED);
2043 }
2044 }
2045
2046 if (retrieve_receiver_index == retrieve_request->receiver_count) {
2047 dlog_verbose(
2048 "Retrieve request does not contain caller's (%x) "
2049 "permissions\n",
2050 to_vm_id);
2051 return ffa_error(FFA_INVALID_PARAMETERS);
2052 }
2053
2054 return (struct ffa_value){.func = FFA_SUCCESS_32};
2055}
2056
J-Alvesa9cd7e32022-07-01 13:49:33 +01002057/*
2058 * According to section 16.4.3 of FF-A v1.1 EAC0 specification, the hypervisor
2059 * may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region description
2060 * of a pending memory sharing operation whose allocator is the SPM, for
2061 * validation purposes before forwarding an FFA_MEM_RECLAIM call. In doing so
2062 * the memory region descriptor of the retrieve request must be zeroed with the
2063 * exception of the sender ID and handle.
2064 */
2065bool is_ffa_memory_retrieve_borrower_request(struct ffa_memory_region *request,
2066 struct vm_locked to_locked)
2067{
2068 return to_locked.vm->id == HF_HYPERVISOR_VM_ID &&
2069 request->attributes == 0U && request->flags == 0U &&
2070 request->tag == 0U && request->receiver_count == 0U &&
2071 plat_ffa_memory_handle_allocated_by_current_world(
2072 request->handle);
2073}
2074
2075/*
2076 * Helper to reset count of fragments retrieved by the hypervisor.
2077 */
2078static void ffa_memory_retrieve_complete_from_hyp(
2079 struct ffa_memory_share_state *share_state)
2080{
2081 if (share_state->hypervisor_fragment_count ==
2082 share_state->fragment_count) {
2083 share_state->hypervisor_fragment_count = 0;
2084 }
2085}
2086
J-Alves089004f2022-07-13 14:25:44 +01002087/**
2088 * Validate that the memory region descriptor provided by the borrower on
2089 * FFA_MEM_RETRIEVE_REQ, against saved memory region provided by lender at the
2090 * memory sharing call.
2091 */
2092static struct ffa_value ffa_memory_retrieve_validate(
2093 ffa_vm_id_t receiver_id, struct ffa_memory_region *retrieve_request,
2094 struct ffa_memory_region *memory_region, uint32_t *receiver_index,
2095 uint32_t share_func)
2096{
2097 ffa_memory_region_flags_t transaction_type =
2098 retrieve_request->flags &
2099 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002100 enum ffa_memory_security security_state;
J-Alves089004f2022-07-13 14:25:44 +01002101
2102 assert(retrieve_request != NULL);
2103 assert(memory_region != NULL);
2104 assert(receiver_index != NULL);
2105 assert(retrieve_request->sender == memory_region->sender);
2106
2107 /*
2108 * Check that the transaction type expected by the receiver is
2109 * correct, if it has been specified.
2110 */
2111 if (transaction_type !=
2112 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
2113 transaction_type != (memory_region->flags &
2114 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
2115 dlog_verbose(
2116 "Incorrect transaction type %#x for "
2117 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
2118 transaction_type,
2119 memory_region->flags &
2120 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
2121 retrieve_request->handle);
2122 return ffa_error(FFA_INVALID_PARAMETERS);
2123 }
2124
2125 if (retrieve_request->tag != memory_region->tag) {
2126 dlog_verbose(
2127 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
2128 "%d for handle %#x.\n",
2129 retrieve_request->tag, memory_region->tag,
2130 retrieve_request->handle);
2131 return ffa_error(FFA_INVALID_PARAMETERS);
2132 }
2133
2134 *receiver_index =
2135 ffa_memory_region_get_receiver(memory_region, receiver_id);
2136
2137 if (*receiver_index == memory_region->receiver_count) {
2138 dlog_verbose(
2139 "Incorrect receiver VM ID %d for "
2140 "FFA_MEM_RETRIEVE_REQ, for handle %#x.\n",
J-Alves59ed0042022-07-28 18:26:41 +01002141 receiver_id, memory_region->handle);
J-Alves089004f2022-07-13 14:25:44 +01002142 return ffa_error(FFA_INVALID_PARAMETERS);
2143 }
2144
2145 if ((retrieve_request->flags &
2146 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
2147 dlog_verbose(
2148 "Retriever specified 'address range alignment 'hint' "
2149 "not supported.\n");
2150 return ffa_error(FFA_INVALID_PARAMETERS);
2151 }
2152 if ((retrieve_request->flags &
2153 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
2154 dlog_verbose(
2155 "Bits 8-5 must be zero in memory region's flags "
2156 "(address range alignment hint not supported).\n");
2157 return ffa_error(FFA_INVALID_PARAMETERS);
2158 }
2159
2160 if ((retrieve_request->flags & ~0x7FF) != 0U) {
2161 dlog_verbose(
2162 "Bits 31-10 must be zero in memory region's flags.\n");
2163 return ffa_error(FFA_INVALID_PARAMETERS);
2164 }
2165
2166 if (share_func == FFA_MEM_SHARE_32 &&
2167 (retrieve_request->flags &
2168 (FFA_MEMORY_REGION_FLAG_CLEAR |
2169 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
2170 dlog_verbose(
2171 "Memory Share operation can't clean after relinquish "
2172 "memory region.\n");
2173 return ffa_error(FFA_INVALID_PARAMETERS);
2174 }
2175
2176 /*
2177 * If the borrower needs the memory to be cleared before mapping
2178 * to its address space, the sender should have set the flag
2179 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
2180 * FFA_DENIED.
2181 */
2182 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
2183 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
2184 dlog_verbose(
2185 "Borrower needs memory cleared. Sender needs to set "
2186 "flag for clearing memory.\n");
2187 return ffa_error(FFA_DENIED);
2188 }
2189
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002190 /* Memory region attributes NS-Bit MBZ for FFA_MEM_RETRIEVE_REQ. */
2191 security_state =
2192 ffa_get_memory_security_attr(retrieve_request->attributes);
2193 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
2194 dlog_verbose(
2195 "Invalid security state for memory retrieve request "
2196 "operation.\n");
2197 return ffa_error(FFA_INVALID_PARAMETERS);
2198 }
2199
J-Alves089004f2022-07-13 14:25:44 +01002200 /*
2201 * If memory type is not specified, bypass validation of memory
2202 * attributes in the retrieve request. The retriever is expecting to
2203 * obtain this information from the SPMC.
2204 */
2205 if (ffa_get_memory_type_attr(retrieve_request->attributes) ==
2206 FFA_MEMORY_NOT_SPECIFIED_MEM) {
2207 return (struct ffa_value){.func = FFA_SUCCESS_32};
2208 }
2209
2210 /*
2211 * Ensure receiver's attributes are compatible with how
2212 * Hafnium maps memory: Normal Memory, Inner shareable,
2213 * Write-Back Read-Allocate Write-Allocate Cacheable.
2214 */
2215 return ffa_memory_attributes_validate(retrieve_request->attributes);
2216}
2217
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002218struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2219 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002220 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002221 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002222{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002223 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002224 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002225 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002226 sizeof(struct ffa_memory_access);
2227 ffa_memory_handle_t handle = retrieve_request->handle;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002228 struct ffa_memory_region *memory_region;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002229 ffa_memory_access_permissions_t permissions = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002230 uint32_t memory_to_attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002231 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002232 struct ffa_memory_share_state *share_state;
2233 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002234 struct ffa_composite_memory_region *composite;
2235 uint32_t total_length;
2236 uint32_t fragment_length;
J-Alves089004f2022-07-13 14:25:44 +01002237 ffa_vm_id_t receiver_id = to_locked.vm->id;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002238 bool is_send_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002239
2240 dump_share_states();
2241
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002242 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002243 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002244 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002245 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002246 expected_retrieve_request_length,
2247 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002248 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002249 }
2250
2251 share_states = share_states_lock();
2252 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002253 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002254 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002255 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002256 goto out;
2257 }
2258
J-Alves96de29f2022-04-26 16:05:24 +01002259 if (!share_state->sending_complete) {
2260 dlog_verbose(
2261 "Memory with handle %#x not fully sent, can't "
2262 "retrieve.\n",
2263 handle);
2264 ret = ffa_error(FFA_INVALID_PARAMETERS);
2265 goto out;
2266 }
2267
Andrew Walbrana65a1322020-04-06 19:32:32 +01002268 memory_region = share_state->memory_region;
J-Alves089004f2022-07-13 14:25:44 +01002269
Andrew Walbrana65a1322020-04-06 19:32:32 +01002270 CHECK(memory_region != NULL);
2271
J-Alves089004f2022-07-13 14:25:44 +01002272 if (retrieve_request->sender != memory_region->sender) {
2273 dlog_verbose(
2274 "Memory with handle %#x not fully sent, can't "
2275 "retrieve.\n",
2276 handle);
2277 ret = ffa_error(FFA_INVALID_PARAMETERS);
2278 goto out;
2279 }
J-Alves96de29f2022-04-26 16:05:24 +01002280
J-Alvesa9cd7e32022-07-01 13:49:33 +01002281 if (!is_ffa_memory_retrieve_borrower_request(retrieve_request,
2282 to_locked)) {
2283 uint32_t receiver_index;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002284
J-Alvesb5084cf2022-07-06 14:20:12 +01002285 /*
2286 * The SPMC can only process retrieve requests to memory share
2287 * operations with one borrower from the other world. It can't
2288 * determine the ID of the NWd VM that invoked the retrieve
2289 * request interface call. It relies on the hypervisor to
2290 * validate the caller's ID against that provided in the
2291 * `receivers` list of the retrieve response.
2292 * In case there is only one borrower from the NWd in the
2293 * transaction descriptor, record that in the `receiver_id` for
2294 * later use, and validate in the retrieve request message.
2295 */
2296 if (to_locked.vm->id == HF_HYPERVISOR_VM_ID) {
2297 uint32_t other_world_count = 0;
2298
2299 for (uint32_t i = 0; i < memory_region->receiver_count;
2300 i++) {
2301 receiver_id =
2302 retrieve_request->receivers[0]
2303 .receiver_permissions.receiver;
2304 if (!vm_id_is_current_world(receiver_id)) {
2305 other_world_count++;
2306 }
2307 }
2308 if (other_world_count > 1) {
2309 dlog_verbose(
2310 "Support one receiver from the other "
2311 "world.\n");
2312 return ffa_error(FFA_NOT_SUPPORTED);
2313 }
2314 }
2315
2316 /*
2317 * Validate retrieve request, according to what was sent by the
2318 * sender. Function will output the `receiver_index` from the
2319 * provided memory region, and will output `permissions` from
2320 * the validated requested permissions.
2321 */
J-Alves089004f2022-07-13 14:25:44 +01002322 ret = ffa_memory_retrieve_validate(
2323 receiver_id, retrieve_request, memory_region,
2324 &receiver_index, share_state->share_func);
2325 if (ret.func != FFA_SUCCESS_32) {
J-Alvesa9cd7e32022-07-01 13:49:33 +01002326 goto out;
2327 }
2328
2329 if (share_state->retrieved_fragment_count[receiver_index] !=
2330 0U) {
2331 dlog_verbose(
2332 "Memory with handle %#x already retrieved.\n",
2333 handle);
2334 ret = ffa_error(FFA_DENIED);
2335 goto out;
2336 }
2337
J-Alvesa9cd7e32022-07-01 13:49:33 +01002338 ret = ffa_memory_retrieve_validate_memory_access_list(
2339 memory_region, retrieve_request, receiver_id,
2340 &permissions);
J-Alves614d9f42022-06-28 14:03:10 +01002341 if (ret.func != FFA_SUCCESS_32) {
2342 goto out;
2343 }
Federico Recanatia98603a2021-12-20 18:04:03 +01002344
J-Alvesa9cd7e32022-07-01 13:49:33 +01002345 memory_to_attributes = ffa_memory_permissions_to_mode(
2346 permissions, share_state->sender_orig_mode);
J-Alves40e260e2022-09-22 17:52:43 +01002347
2348 if (to_locked.vm->el0_partition) {
2349 /*
2350 * Get extra mapping attributes for the given VM ID.
2351 * If the memory is shared by a VM executing in non
2352 * secure world, attribute MM_MODE_NS has to be set
2353 * while mapping that in a SP executing in secure world.
2354 */
2355 memory_to_attributes |=
2356 arch_mm_extra_attributes_from_vm(
2357 retrieve_request->sender);
2358 }
2359
J-Alvesa9cd7e32022-07-01 13:49:33 +01002360 ret = ffa_retrieve_check_update(
2361 to_locked, memory_region->sender,
2362 share_state->fragments,
2363 share_state->fragment_constituent_counts,
2364 share_state->fragment_count, memory_to_attributes,
2365 share_state->share_func, false, page_pool);
2366
2367 if (ret.func != FFA_SUCCESS_32) {
2368 goto out;
2369 }
2370
2371 share_state->retrieved_fragment_count[receiver_index] = 1;
2372 is_send_complete =
2373 share_state->retrieved_fragment_count[receiver_index] ==
2374 share_state->fragment_count;
2375 } else {
2376 if (share_state->hypervisor_fragment_count != 0U) {
2377 dlog_verbose(
J-Alvesb5084cf2022-07-06 14:20:12 +01002378 "Memory with handle %#x already retrieved by "
J-Alvesa9cd7e32022-07-01 13:49:33 +01002379 "the hypervisor.\n",
2380 handle);
2381 ret = ffa_error(FFA_DENIED);
2382 goto out;
2383 }
2384
2385 share_state->hypervisor_fragment_count = 1;
2386
2387 ffa_memory_retrieve_complete_from_hyp(share_state);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002388 }
2389
J-Alvesb5084cf2022-07-06 14:20:12 +01002390 /* VMs acquire the RX buffer from SPMC. */
2391 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
2392
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002393 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002394 * Copy response to RX buffer of caller and deliver the message.
2395 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002396 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002397 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002398 composite = ffa_memory_region_get_composite(memory_region, 0);
2399 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002400 * Constituents which we received in the first fragment should
2401 * always fit in the first fragment we are sending, because the
2402 * header is the same size in both cases and we have a fixed
2403 * message buffer size. So `ffa_retrieved_memory_region_init`
2404 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01002405 */
2406 CHECK(ffa_retrieved_memory_region_init(
J-Alves2d8457f2022-10-05 11:06:41 +01002407 to_locked.vm->mailbox.recv, to_locked.vm->ffa_version,
2408 HF_MAILBOX_SIZE, memory_region->sender,
2409 memory_region->attributes, memory_region->flags, handle,
2410 receiver_id, permissions, composite->page_count,
2411 composite->constituent_count, share_state->fragments[0],
Andrew Walbranca808b12020-05-15 17:22:28 +01002412 share_state->fragment_constituent_counts[0], &total_length,
2413 &fragment_length));
J-Alvesb5084cf2022-07-06 14:20:12 +01002414
Andrew Walbranca808b12020-05-15 17:22:28 +01002415 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002416 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002417 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00002418 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002419
J-Alvesa9cd7e32022-07-01 13:49:33 +01002420 if (is_send_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002421 ffa_memory_retrieve_complete(share_states, share_state,
2422 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002423 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002424 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002425 .arg1 = total_length,
2426 .arg2 = fragment_length};
Andrew Walbranca808b12020-05-15 17:22:28 +01002427out:
2428 share_states_unlock(&share_states);
2429 dump_share_states();
2430 return ret;
2431}
2432
J-Alves5da37d92022-10-24 16:33:48 +01002433/**
2434 * Determine expected fragment offset according to the FF-A version of
2435 * the caller.
2436 */
2437static uint32_t ffa_memory_retrieve_expected_offset_per_ffa_version(
2438 struct ffa_memory_region *memory_region,
2439 uint32_t retrieved_constituents_count, uint32_t ffa_version)
2440{
2441 uint32_t expected_fragment_offset;
2442 uint32_t composite_constituents_offset;
2443
2444 if (ffa_version == MAKE_FFA_VERSION(1, 1)) {
2445 /*
2446 * Hafnium operates memory regions in FF-A v1.1 format, so we
2447 * can retrieve the constituents offset from descriptor.
2448 */
2449 composite_constituents_offset =
2450 ffa_composite_constituent_offset(memory_region, 0);
2451 } else if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
2452 /*
2453 * If retriever is FF-A v1.0, determine the composite offset
2454 * as it is expected to have been configured in the
2455 * retrieve response.
2456 */
2457 composite_constituents_offset =
2458 sizeof(struct ffa_memory_region_v1_0) +
2459 RECEIVERS_COUNT_IN_RETRIEVE_RESP *
2460 sizeof(struct ffa_memory_access) +
2461 sizeof(struct ffa_composite_memory_region);
2462 } else {
2463 panic("%s received an invalid FF-A version.\n", __func__);
2464 }
2465
2466 expected_fragment_offset =
2467 composite_constituents_offset +
2468 retrieved_constituents_count *
2469 sizeof(struct ffa_memory_region_constituent) -
2470 sizeof(struct ffa_memory_access) *
2471 (memory_region->receiver_count - 1);
2472
2473 return expected_fragment_offset;
2474}
2475
Andrew Walbranca808b12020-05-15 17:22:28 +01002476struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2477 ffa_memory_handle_t handle,
2478 uint32_t fragment_offset,
J-Alves59ed0042022-07-28 18:26:41 +01002479 ffa_vm_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01002480 struct mpool *page_pool)
2481{
2482 struct ffa_memory_region *memory_region;
2483 struct share_states_locked share_states;
2484 struct ffa_memory_share_state *share_state;
2485 struct ffa_value ret;
2486 uint32_t fragment_index;
2487 uint32_t retrieved_constituents_count;
2488 uint32_t i;
2489 uint32_t expected_fragment_offset;
2490 uint32_t remaining_constituent_count;
2491 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01002492 uint32_t receiver_index;
J-Alves59ed0042022-07-28 18:26:41 +01002493 bool continue_ffa_hyp_mem_retrieve_req;
Andrew Walbranca808b12020-05-15 17:22:28 +01002494
2495 dump_share_states();
2496
2497 share_states = share_states_lock();
2498 if (!get_share_state(share_states, handle, &share_state)) {
2499 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2500 handle);
2501 ret = ffa_error(FFA_INVALID_PARAMETERS);
2502 goto out;
2503 }
2504
2505 memory_region = share_state->memory_region;
2506 CHECK(memory_region != NULL);
2507
Andrew Walbranca808b12020-05-15 17:22:28 +01002508 if (!share_state->sending_complete) {
2509 dlog_verbose(
2510 "Memory with handle %#x not fully sent, can't "
2511 "retrieve.\n",
2512 handle);
2513 ret = ffa_error(FFA_INVALID_PARAMETERS);
2514 goto out;
2515 }
2516
J-Alves59ed0042022-07-28 18:26:41 +01002517 /*
2518 * If retrieve request from the hypervisor has been initiated in the
2519 * given share_state, continue it, else assume it is a continuation of
2520 * retrieve request from a NWd VM.
2521 */
2522 continue_ffa_hyp_mem_retrieve_req =
2523 (to_locked.vm->id == HF_HYPERVISOR_VM_ID) &&
2524 (share_state->hypervisor_fragment_count != 0U) &&
2525 plat_ffa_is_vm_id(sender_vm_id);
Andrew Walbranca808b12020-05-15 17:22:28 +01002526
J-Alves59ed0042022-07-28 18:26:41 +01002527 if (!continue_ffa_hyp_mem_retrieve_req) {
2528 receiver_index = ffa_memory_region_get_receiver(
2529 memory_region, to_locked.vm->id);
2530
2531 if (receiver_index == memory_region->receiver_count) {
2532 dlog_verbose(
2533 "Caller of FFA_MEM_FRAG_RX (%x) is not a "
2534 "borrower to memory sharing transaction (%x)\n",
2535 to_locked.vm->id, handle);
2536 ret = ffa_error(FFA_INVALID_PARAMETERS);
2537 goto out;
2538 }
2539
2540 if (share_state->retrieved_fragment_count[receiver_index] ==
2541 0 ||
2542 share_state->retrieved_fragment_count[receiver_index] >=
2543 share_state->fragment_count) {
2544 dlog_verbose(
2545 "Retrieval of memory with handle %#x not yet "
2546 "started or already completed (%d/%d fragments "
2547 "retrieved).\n",
2548 handle,
2549 share_state->retrieved_fragment_count
2550 [receiver_index],
2551 share_state->fragment_count);
2552 ret = ffa_error(FFA_INVALID_PARAMETERS);
2553 goto out;
2554 }
2555
2556 fragment_index =
2557 share_state->retrieved_fragment_count[receiver_index];
2558 } else {
2559 if (share_state->hypervisor_fragment_count == 0 ||
2560 share_state->hypervisor_fragment_count >=
2561 share_state->fragment_count) {
2562 dlog_verbose(
2563 "Retrieve of memory with handle %x not "
2564 "started from hypervisor.\n",
2565 handle);
2566 ret = ffa_error(FFA_INVALID_PARAMETERS);
2567 goto out;
2568 }
2569
2570 if (memory_region->sender != sender_vm_id) {
2571 dlog_verbose(
2572 "Sender ID (%x) is not as expected for memory "
2573 "handle %x\n",
2574 sender_vm_id, handle);
2575 ret = ffa_error(FFA_INVALID_PARAMETERS);
2576 goto out;
2577 }
2578
2579 fragment_index = share_state->hypervisor_fragment_count;
2580
2581 receiver_index = 0;
2582 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002583
2584 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002585 * Check that the given fragment offset is correct by counting
2586 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01002587 */
2588 retrieved_constituents_count = 0;
2589 for (i = 0; i < fragment_index; ++i) {
2590 retrieved_constituents_count +=
2591 share_state->fragment_constituent_counts[i];
2592 }
J-Alvesc7484f12022-05-13 12:41:14 +01002593
2594 CHECK(memory_region->receiver_count > 0);
2595
Andrew Walbranca808b12020-05-15 17:22:28 +01002596 expected_fragment_offset =
J-Alves5da37d92022-10-24 16:33:48 +01002597 ffa_memory_retrieve_expected_offset_per_ffa_version(
2598 memory_region, retrieved_constituents_count,
2599 to_locked.vm->ffa_version);
2600
Andrew Walbranca808b12020-05-15 17:22:28 +01002601 if (fragment_offset != expected_fragment_offset) {
2602 dlog_verbose("Fragment offset was %d but expected %d.\n",
2603 fragment_offset, expected_fragment_offset);
2604 ret = ffa_error(FFA_INVALID_PARAMETERS);
2605 goto out;
2606 }
2607
J-Alves59ed0042022-07-28 18:26:41 +01002608 /* VMs acquire the RX buffer from SPMC. */
2609 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
2610
Andrew Walbranca808b12020-05-15 17:22:28 +01002611 remaining_constituent_count = ffa_memory_fragment_init(
2612 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2613 share_state->fragments[fragment_index],
2614 share_state->fragment_constituent_counts[fragment_index],
2615 &fragment_length);
2616 CHECK(remaining_constituent_count == 0);
2617 to_locked.vm->mailbox.recv_size = fragment_length;
2618 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2619 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00002620 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbranca808b12020-05-15 17:22:28 +01002621
J-Alves59ed0042022-07-28 18:26:41 +01002622 if (!continue_ffa_hyp_mem_retrieve_req) {
2623 share_state->retrieved_fragment_count[receiver_index]++;
2624 if (share_state->retrieved_fragment_count[receiver_index] ==
2625 share_state->fragment_count) {
2626 ffa_memory_retrieve_complete(share_states, share_state,
2627 page_pool);
2628 }
2629 } else {
2630 share_state->hypervisor_fragment_count++;
2631
2632 ffa_memory_retrieve_complete_from_hyp(share_state);
2633 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002634 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2635 .arg1 = (uint32_t)handle,
2636 .arg2 = (uint32_t)(handle >> 32),
2637 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002638
2639out:
2640 share_states_unlock(&share_states);
2641 dump_share_states();
2642 return ret;
2643}
2644
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002645struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002646 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002647 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002648{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002649 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002650 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002651 struct ffa_memory_share_state *share_state;
2652 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002653 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002654 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01002655 uint32_t receiver_index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002656
Andrew Walbrana65a1322020-04-06 19:32:32 +01002657 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002658 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002659 "Stream endpoints not supported (got %d "
2660 "endpoints on "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002661 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002662 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002663 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002664 }
2665
Andrew Walbrana65a1322020-04-06 19:32:32 +01002666 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002667 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002668 "VM ID %d in relinquish message doesn't match "
2669 "calling "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002670 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002671 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002672 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002673 }
2674
2675 dump_share_states();
2676
2677 share_states = share_states_lock();
2678 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002679 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002680 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002681 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002682 goto out;
2683 }
2684
Andrew Walbranca808b12020-05-15 17:22:28 +01002685 if (!share_state->sending_complete) {
2686 dlog_verbose(
2687 "Memory with handle %#x not fully sent, can't "
2688 "relinquish.\n",
2689 handle);
2690 ret = ffa_error(FFA_INVALID_PARAMETERS);
2691 goto out;
2692 }
2693
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002694 memory_region = share_state->memory_region;
2695 CHECK(memory_region != NULL);
2696
J-Alves8eb19162022-04-28 10:56:48 +01002697 receiver_index = ffa_memory_region_get_receiver(memory_region,
2698 from_locked.vm->id);
2699
2700 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002701 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002702 "VM ID %d tried to relinquish memory region "
2703 "with "
J-Alves8eb19162022-04-28 10:56:48 +01002704 "handle %#x and it is not a valid borrower.\n",
2705 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002706 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002707 goto out;
2708 }
2709
J-Alves8eb19162022-04-28 10:56:48 +01002710 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01002711 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002712 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002713 "Memory with handle %#x not yet fully "
2714 "retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01002715 "receiver %x can't relinquish.\n",
2716 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002717 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002718 goto out;
2719 }
2720
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002721 clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002722
2723 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002724 * Clear is not allowed for memory that was shared, as the
2725 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002726 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002727 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002728 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002729 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002730 goto out;
2731 }
2732
Andrew Walbranca808b12020-05-15 17:22:28 +01002733 ret = ffa_relinquish_check_update(
2734 from_locked, share_state->fragments,
2735 share_state->fragment_constituent_counts,
2736 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002737
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002738 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002739 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002740 * Mark memory handle as not retrieved, so it can be
2741 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002742 */
J-Alves8eb19162022-04-28 10:56:48 +01002743 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002744 }
2745
2746out:
2747 share_states_unlock(&share_states);
2748 dump_share_states();
2749 return ret;
2750}
2751
2752/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01002753 * Validates that the reclaim transition is allowed for the given
2754 * handle, updates the page table of the reclaiming VM, and frees the
2755 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002756 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002757struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002758 ffa_memory_handle_t handle,
2759 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002760 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002761{
2762 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002763 struct ffa_memory_share_state *share_state;
2764 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002765 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002766
2767 dump_share_states();
2768
2769 share_states = share_states_lock();
J-Alvesb5084cf2022-07-06 14:20:12 +01002770 if (get_share_state(share_states, handle, &share_state)) {
2771 memory_region = share_state->memory_region;
2772 } else {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002773 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002774 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002775 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002776 goto out;
2777 }
2778
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002779 CHECK(memory_region != NULL);
2780
J-Alvesa9cd7e32022-07-01 13:49:33 +01002781 if (vm_id_is_current_world(to_locked.vm->id) &&
2782 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002783 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002784 "VM %#x attempted to reclaim memory handle %#x "
2785 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002786 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002787 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002788 goto out;
2789 }
2790
Andrew Walbranca808b12020-05-15 17:22:28 +01002791 if (!share_state->sending_complete) {
2792 dlog_verbose(
2793 "Memory with handle %#x not fully sent, can't "
2794 "reclaim.\n",
2795 handle);
2796 ret = ffa_error(FFA_INVALID_PARAMETERS);
2797 goto out;
2798 }
2799
J-Alves752236c2022-04-28 11:07:47 +01002800 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
2801 if (share_state->retrieved_fragment_count[i] != 0) {
2802 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002803 "Tried to reclaim memory handle %#x "
2804 "that has "
2805 "not been relinquished by all "
2806 "borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01002807 handle,
2808 memory_region->receivers[i]
2809 .receiver_permissions.receiver);
2810 ret = ffa_error(FFA_DENIED);
2811 goto out;
2812 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002813 }
2814
Andrew Walbranca808b12020-05-15 17:22:28 +01002815 ret = ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00002816 to_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002817 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00002818 share_state->fragment_count, share_state->sender_orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01002819 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002820
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002821 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002822 share_state_free(share_states, share_state, page_pool);
J-Alvesa9cd7e32022-07-01 13:49:33 +01002823 dlog_verbose(
2824 "Freed share state after successful "
2825 "reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002826 }
2827
2828out:
2829 share_states_unlock(&share_states);
2830 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002831}