blob: 7df1491aaa7e61970cc6e509c8c63dec49e46ab3 [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
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000025/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010026 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000027 * by this lock.
28 */
29static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010030static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000031
32/**
J-Alves8505a8a2022-06-15 18:10:18 +010033 * Buffer for retrieving memory region information from the other world for when
34 * a region is reclaimed by a VM. Access to this buffer must be guarded by the
35 * VM lock of the other world VM.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000036 */
Andrew Walbranca808b12020-05-15 17:22:28 +010037alignas(PAGE_SIZE) static uint8_t
J-Alves8505a8a2022-06-15 18:10:18 +010038 other_world_retrieve_buffer[HF_MAILBOX_SIZE * MAX_FRAGMENTS];
Andrew Walbranca808b12020-05-15 17:22:28 +010039
40/**
J-Alves917d2f22020-10-30 18:39:30 +000041 * Extracts the index from a memory handle allocated by Hafnium's current world.
42 */
43uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle)
44{
45 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
46}
47
48/**
Andrew Walbranca808b12020-05-15 17:22:28 +010049 * Initialises the next available `struct ffa_memory_share_state` and sets
50 * `share_state_ret` to a pointer to it. If `handle` is
51 * `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle, otherwise
52 * uses the provided handle which is assumed to be globally unique.
53 *
54 * Returns true on success or false if none are available.
55 */
J-Alves66652252022-07-06 09:49:51 +010056bool allocate_share_state(struct share_states_locked share_states,
57 uint32_t share_func,
58 struct ffa_memory_region *memory_region,
59 uint32_t fragment_length, ffa_memory_handle_t handle,
60 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000061{
Andrew Walbrana65a1322020-04-06 19:32:32 +010062 uint64_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000063
Daniel Boulbya2f8c662021-11-26 17:52:53 +000064 assert(share_states.share_states != NULL);
65 assert(memory_region != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000066
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000067 for (i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +010068 if (share_states.share_states[i].share_func == 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000069 uint32_t j;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010070 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +010071 &share_states.share_states[i];
72 struct ffa_composite_memory_region *composite =
73 ffa_memory_region_get_composite(memory_region,
74 0);
75
76 if (handle == FFA_MEMORY_HANDLE_INVALID) {
J-Alvesee68c542020-10-29 17:48:20 +000077 memory_region->handle =
Olivier Deprez55a189e2021-06-09 15:45:27 +020078 plat_ffa_memory_handle_make(i);
Andrew Walbranca808b12020-05-15 17:22:28 +010079 } else {
J-Alvesee68c542020-10-29 17:48:20 +000080 memory_region->handle = handle;
Andrew Walbranca808b12020-05-15 17:22:28 +010081 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000082 allocated_state->share_func = share_func;
83 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +010084 allocated_state->fragment_count = 1;
85 allocated_state->fragments[0] = composite->constituents;
86 allocated_state->fragment_constituent_counts[0] =
87 (fragment_length -
88 ffa_composite_constituent_offset(memory_region,
89 0)) /
90 sizeof(struct ffa_memory_region_constituent);
91 allocated_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000092 for (j = 0; j < MAX_MEM_SHARE_RECIPIENTS; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +010093 allocated_state->retrieved_fragment_count[j] =
94 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000095 }
Andrew Walbranca808b12020-05-15 17:22:28 +010096 if (share_state_ret != NULL) {
97 *share_state_ret = allocated_state;
98 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000099 return true;
100 }
101 }
102
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000103 return false;
104}
105
106/** Locks the share states lock. */
107struct share_states_locked share_states_lock(void)
108{
109 sl_lock(&share_states_lock_instance);
110
111 return (struct share_states_locked){.share_states = share_states};
112}
113
114/** Unlocks the share states lock. */
J-Alves66652252022-07-06 09:49:51 +0100115void share_states_unlock(struct share_states_locked *share_states)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000116{
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000117 assert(share_states->share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000118 share_states->share_states = NULL;
119 sl_unlock(&share_states_lock_instance);
120}
121
122/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100123 * If the given handle is a valid handle for an allocated share state then
124 * initialises `share_state_ret` to point to the share state and returns true.
125 * Otherwise returns false.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000126 */
127static bool get_share_state(struct share_states_locked share_states,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100128 ffa_memory_handle_t handle,
129 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000130{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100131 struct ffa_memory_share_state *share_state;
J-Alves917d2f22020-10-30 18:39:30 +0000132 uint64_t index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000133
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000134 assert(share_states.share_states != NULL);
135 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100136
137 /*
138 * First look for a share_state allocated by us, in which case the
139 * handle is based on the index.
140 */
Olivier Deprez55a189e2021-06-09 15:45:27 +0200141 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
J-Alves917d2f22020-10-30 18:39:30 +0000142 index = ffa_memory_handle_get_index(handle);
Andrew Walbranca808b12020-05-15 17:22:28 +0100143 if (index < MAX_MEM_SHARES) {
144 share_state = &share_states.share_states[index];
145 if (share_state->share_func != 0) {
146 *share_state_ret = share_state;
147 return true;
148 }
149 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000150 }
151
Andrew Walbranca808b12020-05-15 17:22:28 +0100152 /* Fall back to a linear scan. */
153 for (index = 0; index < MAX_MEM_SHARES; ++index) {
154 share_state = &share_states.share_states[index];
J-Alvesee68c542020-10-29 17:48:20 +0000155 if (share_state->memory_region != NULL &&
156 share_state->memory_region->handle == handle &&
Andrew Walbranca808b12020-05-15 17:22:28 +0100157 share_state->share_func != 0) {
158 *share_state_ret = share_state;
159 return true;
160 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000161 }
162
Andrew Walbranca808b12020-05-15 17:22:28 +0100163 return false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000164}
165
166/** Marks a share state as unallocated. */
167static void share_state_free(struct share_states_locked share_states,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100168 struct ffa_memory_share_state *share_state,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000169 struct mpool *page_pool)
170{
Andrew Walbranca808b12020-05-15 17:22:28 +0100171 uint32_t i;
172
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000173 assert(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000174 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100175 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000176 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100177 /*
178 * First fragment is part of the same page as the `memory_region`, so it
179 * doesn't need to be freed separately.
180 */
181 share_state->fragments[0] = NULL;
182 share_state->fragment_constituent_counts[0] = 0;
183 for (i = 1; i < share_state->fragment_count; ++i) {
184 mpool_free(page_pool, share_state->fragments[i]);
185 share_state->fragments[i] = NULL;
186 share_state->fragment_constituent_counts[i] = 0;
187 }
188 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000189 share_state->memory_region = NULL;
J-Alvesa9cd7e32022-07-01 13:49:33 +0100190 share_state->hypervisor_fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000191}
192
Andrew Walbranca808b12020-05-15 17:22:28 +0100193/** Checks whether the given share state has been fully sent. */
194static bool share_state_sending_complete(
195 struct share_states_locked share_states,
196 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000197{
Andrew Walbranca808b12020-05-15 17:22:28 +0100198 struct ffa_composite_memory_region *composite;
199 uint32_t expected_constituent_count;
200 uint32_t fragment_constituent_count_total = 0;
201 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000202
Andrew Walbranca808b12020-05-15 17:22:28 +0100203 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000204 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100205
206 /*
207 * Share state must already be valid, or it's not possible to get hold
208 * of it.
209 */
210 CHECK(share_state->memory_region != NULL &&
211 share_state->share_func != 0);
212
213 composite =
214 ffa_memory_region_get_composite(share_state->memory_region, 0);
215 expected_constituent_count = composite->constituent_count;
216 for (i = 0; i < share_state->fragment_count; ++i) {
217 fragment_constituent_count_total +=
218 share_state->fragment_constituent_counts[i];
219 }
220 dlog_verbose(
221 "Checking completion: constituent count %d/%d from %d "
222 "fragments.\n",
223 fragment_constituent_count_total, expected_constituent_count,
224 share_state->fragment_count);
225
226 return fragment_constituent_count_total == expected_constituent_count;
227}
228
229/**
230 * Calculates the offset of the next fragment expected for the given share
231 * state.
232 */
233static uint32_t share_state_next_fragment_offset(
234 struct share_states_locked share_states,
235 struct ffa_memory_share_state *share_state)
236{
237 uint32_t next_fragment_offset;
238 uint32_t i;
239
240 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000241 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100242
243 next_fragment_offset =
244 ffa_composite_constituent_offset(share_state->memory_region, 0);
245 for (i = 0; i < share_state->fragment_count; ++i) {
246 next_fragment_offset +=
247 share_state->fragment_constituent_counts[i] *
248 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000249 }
250
Andrew Walbranca808b12020-05-15 17:22:28 +0100251 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000252}
253
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100254static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000255{
256 uint32_t i;
257
258 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
259 return;
260 }
261
Olivier Deprez935e1b12020-12-22 18:01:29 +0100262 dlog("from VM %#x, attributes %#x, flags %#x, tag %u, to "
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100263 "%u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100264 "recipients [",
265 memory_region->sender, memory_region->attributes,
Olivier Deprez935e1b12020-12-22 18:01:29 +0100266 memory_region->flags, memory_region->tag,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100267 memory_region->receiver_count);
268 for (i = 0; i < memory_region->receiver_count; ++i) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000269 if (i != 0) {
270 dlog(", ");
271 }
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100272 dlog("VM %#x: %#x (offset %u)",
Andrew Walbrana65a1322020-04-06 19:32:32 +0100273 memory_region->receivers[i].receiver_permissions.receiver,
274 memory_region->receivers[i]
275 .receiver_permissions.permissions,
276 memory_region->receivers[i]
277 .composite_memory_region_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000278 }
279 dlog("]");
280}
281
J-Alves66652252022-07-06 09:49:51 +0100282void dump_share_states(void)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000283{
284 uint32_t i;
285
286 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
287 return;
288 }
289
290 dlog("Current share states:\n");
291 sl_lock(&share_states_lock_instance);
292 for (i = 0; i < MAX_MEM_SHARES; ++i) {
293 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000294 switch (share_states[i].share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100295 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000296 dlog("SHARE");
297 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100298 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000299 dlog("LEND");
300 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100301 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000302 dlog("DONATE");
303 break;
304 default:
305 dlog("invalid share_func %#x",
306 share_states[i].share_func);
307 }
Olivier Deprez935e1b12020-12-22 18:01:29 +0100308 dlog(" %#x (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000309 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100310 if (share_states[i].sending_complete) {
311 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000312 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100313 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000314 }
J-Alves2a0d2882020-10-29 14:49:50 +0000315 dlog(" with %d fragments, %d retrieved, "
316 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100317 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000318 share_states[i].retrieved_fragment_count[0],
319 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000320 }
321 }
322 sl_unlock(&share_states_lock_instance);
323}
324
Andrew Walbran475c1452020-02-07 13:22:22 +0000325/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100326static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100327 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000328{
329 uint32_t mode = 0;
330
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100331 switch (ffa_get_data_access_attr(permissions)) {
332 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000333 mode = MM_MODE_R;
334 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100335 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000336 mode = MM_MODE_R | MM_MODE_W;
337 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100338 case FFA_DATA_ACCESS_NOT_SPECIFIED:
339 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
340 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100341 case FFA_DATA_ACCESS_RESERVED:
342 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100343 }
344
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100345 switch (ffa_get_instruction_access_attr(permissions)) {
346 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000347 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100348 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100349 mode |= MM_MODE_X;
350 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100351 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
352 mode |= (default_mode & MM_MODE_X);
353 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100354 case FFA_INSTRUCTION_ACCESS_RESERVED:
355 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000356 }
357
358 return mode;
359}
360
Jose Marinho75509b42019-04-09 09:34:59 +0100361/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000362 * Get the current mode in the stage-2 page table of the given vm of all the
363 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100364 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100365 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100366static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000367 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100368 struct ffa_memory_region_constituent **fragments,
369 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100370{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100371 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100372 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100373
Andrew Walbranca808b12020-05-15 17:22:28 +0100374 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100375 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000376 * Fail if there are no constituents. Otherwise we would get an
377 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100378 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100379 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100380 }
381
Andrew Walbranca808b12020-05-15 17:22:28 +0100382 for (i = 0; i < fragment_count; ++i) {
383 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
384 ipaddr_t begin = ipa_init(fragments[i][j].address);
385 size_t size = fragments[i][j].page_count * PAGE_SIZE;
386 ipaddr_t end = ipa_add(begin, size);
387 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100388
Andrew Walbranca808b12020-05-15 17:22:28 +0100389 /* Fail if addresses are not page-aligned. */
390 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
391 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
392 return ffa_error(FFA_INVALID_PARAMETERS);
393 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100394
Andrew Walbranca808b12020-05-15 17:22:28 +0100395 /*
396 * Ensure that this constituent memory range is all
397 * mapped with the same mode.
398 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800399 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100400 return ffa_error(FFA_DENIED);
401 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100402
Andrew Walbranca808b12020-05-15 17:22:28 +0100403 /*
404 * Ensure that all constituents are mapped with the same
405 * mode.
406 */
407 if (i == 0) {
408 *orig_mode = current_mode;
409 } else if (current_mode != *orig_mode) {
410 dlog_verbose(
411 "Expected mode %#x but was %#x for %d "
412 "pages at %#x.\n",
413 *orig_mode, current_mode,
414 fragments[i][j].page_count,
415 ipa_addr(begin));
416 return ffa_error(FFA_DENIED);
417 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100418 }
Jose Marinho75509b42019-04-09 09:34:59 +0100419 }
420
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100421 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000422}
423
424/**
425 * Verify that all pages have the same mode, that the starting mode
426 * constitutes a valid state and obtain the next mode to apply
427 * to the sending VM.
428 *
429 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100430 * 1) FFA_DENIED if a state transition was not found;
431 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100432 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100433 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100434 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100435 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
436 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000437 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100438static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100439 struct vm_locked from, uint32_t share_func,
J-Alves363f5722022-04-25 17:37:37 +0100440 struct ffa_memory_access *receivers, uint32_t receivers_count,
441 uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100442 struct ffa_memory_region_constituent **fragments,
443 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
444 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000445{
446 const uint32_t state_mask =
447 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100448 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000449
Andrew Walbranca808b12020-05-15 17:22:28 +0100450 ret = constituents_get_mode(from, orig_from_mode, fragments,
451 fragment_constituent_counts,
452 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100453 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100454 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100455 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100456 }
457
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000458 /* Ensure the address range is normal memory and not a device. */
459 if (*orig_from_mode & MM_MODE_D) {
460 dlog_verbose("Can't share device memory (mode is %#x).\n",
461 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100462 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000463 }
464
465 /*
466 * Ensure the sender is the owner and has exclusive access to the
467 * memory.
468 */
469 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100470 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100471 }
472
J-Alves363f5722022-04-25 17:37:37 +0100473 assert(receivers != NULL && receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100474
J-Alves363f5722022-04-25 17:37:37 +0100475 for (uint32_t i = 0U; i < receivers_count; i++) {
476 ffa_memory_access_permissions_t permissions =
477 receivers[i].receiver_permissions.permissions;
478 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
479 permissions, *orig_from_mode);
480
481 if ((*orig_from_mode & required_from_mode) !=
482 required_from_mode) {
483 dlog_verbose(
484 "Sender tried to send memory with permissions "
485 "which "
486 "required mode %#x but only had %#x itself.\n",
487 required_from_mode, *orig_from_mode);
488 return ffa_error(FFA_DENIED);
489 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000490 }
491
492 /* Find the appropriate new mode. */
493 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000494 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100495 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000496 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100497 break;
498
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100499 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000500 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100501 break;
502
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100503 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000504 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100505 break;
506
Jose Marinho75509b42019-04-09 09:34:59 +0100507 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100508 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100509 }
510
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100511 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000512}
513
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100514static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000515 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100516 struct ffa_memory_region_constituent **fragments,
517 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
518 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000519{
520 const uint32_t state_mask =
521 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
522 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100523 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000524
Andrew Walbranca808b12020-05-15 17:22:28 +0100525 ret = constituents_get_mode(from, orig_from_mode, fragments,
526 fragment_constituent_counts,
527 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100528 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100529 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000530 }
531
532 /* Ensure the address range is normal memory and not a device. */
533 if (*orig_from_mode & MM_MODE_D) {
534 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
535 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100536 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000537 }
538
539 /*
540 * Ensure the relinquishing VM is not the owner but has access to the
541 * memory.
542 */
543 orig_from_state = *orig_from_mode & state_mask;
544 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
545 dlog_verbose(
546 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100547 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000548 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100549 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000550 }
551
552 /* Find the appropriate new mode. */
553 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
554
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100555 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000556}
557
558/**
559 * Verify that all pages have the same mode, that the starting mode
560 * constitutes a valid state and obtain the next mode to apply
561 * to the retrieving VM.
562 *
563 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100564 * 1) FFA_DENIED if a state transition was not found;
565 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100566 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100567 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100568 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100569 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
570 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000571 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100572static struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000573 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100574 struct ffa_memory_region_constituent **fragments,
575 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
576 uint32_t memory_to_attributes, uint32_t *to_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000577{
578 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100579 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000580
Andrew Walbranca808b12020-05-15 17:22:28 +0100581 ret = constituents_get_mode(to, &orig_to_mode, fragments,
582 fragment_constituent_counts,
583 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100584 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100585 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100586 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000587 }
588
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100589 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000590 /*
591 * If the original ffa memory send call has been processed
592 * successfully, it is expected the orig_to_mode would overlay
593 * with `state_mask`, as a result of the function
594 * `ffa_send_check_transition`.
595 */
Daniel Boulby9133dad2022-04-25 14:38:44 +0100596 assert((orig_to_mode & (MM_MODE_INVALID | MM_MODE_UNOWNED |
597 MM_MODE_SHARED)) != 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000598 } else {
599 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +0100600 * If the retriever is from virtual FF-A instance:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000601 * Ensure the retriever has the expected state. We don't care
602 * about the MM_MODE_SHARED bit; either with or without it set
603 * are both valid representations of the !O-NA state.
604 */
J-Alvesa9cd7e32022-07-01 13:49:33 +0100605 if (vm_id_is_current_world(to.vm->id) &&
606 to.vm->id != HF_PRIMARY_VM_ID &&
607 (orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
608 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100609 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000610 }
611 }
612
613 /* Find the appropriate new mode. */
614 *to_mode = memory_to_attributes;
615 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100616 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000617 *to_mode |= 0;
618 break;
619
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100620 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000621 *to_mode |= MM_MODE_UNOWNED;
622 break;
623
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100624 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000625 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
626 break;
627
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100628 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000629 *to_mode |= 0;
630 break;
631
632 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100633 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100634 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000635 }
636
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100637 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100638}
Jose Marinho09b1db82019-08-08 09:16:59 +0100639
640/**
641 * Updates a VM's page table such that the given set of physical address ranges
642 * are mapped in the address space at the corresponding address ranges, in the
643 * mode provided.
644 *
645 * If commit is false, the page tables will be allocated from the mpool but no
646 * mappings will actually be updated. This function must always be called first
647 * with commit false to check that it will succeed before calling with commit
648 * true, to avoid leaving the page table in a half-updated state. To make a
649 * series of changes atomically you can call them all with commit false before
650 * calling them all with commit true.
651 *
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700652 * vm_ptable_defrag should always be called after a series of page table
653 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +0100654 *
655 * Returns true on success, or false if the update failed and no changes were
656 * made to memory mappings.
657 */
J-Alves66652252022-07-06 09:49:51 +0100658bool ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000659 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100660 struct ffa_memory_region_constituent **fragments,
661 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby4dd3f532021-09-21 09:57:08 +0100662 uint32_t mode, struct mpool *ppool, bool commit)
Jose Marinho09b1db82019-08-08 09:16:59 +0100663{
Andrew Walbranca808b12020-05-15 17:22:28 +0100664 uint32_t i;
665 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100666
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700667 if (vm_locked.vm->el0_partition) {
668 mode |= MM_MODE_USER | MM_MODE_NG;
669 }
670
Andrew Walbranca808b12020-05-15 17:22:28 +0100671 /* Iterate over the memory region constituents within each fragment. */
672 for (i = 0; i < fragment_count; ++i) {
673 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
674 size_t size = fragments[i][j].page_count * PAGE_SIZE;
675 paddr_t pa_begin =
676 pa_from_ipa(ipa_init(fragments[i][j].address));
677 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +0200678 uint32_t pa_bits =
679 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +0100680
681 /*
682 * Ensure the requested region falls into system's PA
683 * range.
684 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +0200685 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
686 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +0100687 dlog_error("Region is outside of PA Range\n");
688 return false;
689 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100690
691 if (commit) {
692 vm_identity_commit(vm_locked, pa_begin, pa_end,
693 mode, ppool, NULL);
694 } else if (!vm_identity_prepare(vm_locked, pa_begin,
695 pa_end, mode, ppool)) {
696 return false;
697 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100698 }
699 }
700
701 return true;
702}
703
704/**
705 * Clears a region of physical memory by overwriting it with zeros. The data is
706 * flushed from the cache so the memory has been cleared across the system.
707 */
J-Alves7db32002021-12-14 14:44:50 +0000708static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
709 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +0100710{
711 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000712 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100713 * global mapping of the whole range. Such an approach will limit
714 * the changes to stage-1 tables and will allow only local
715 * invalidation.
716 */
717 bool ret;
718 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +0000719 void *ptr = mm_identity_map(stage1_locked, begin, end,
720 MM_MODE_W | (extra_mode_attributes &
721 plat_ffa_other_world_mode()),
722 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100723 size_t size = pa_difference(begin, end);
724
725 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100726 goto fail;
727 }
728
729 memset_s(ptr, size, 0, size);
730 arch_mm_flush_dcache(ptr, size);
731 mm_unmap(stage1_locked, begin, end, ppool);
732
733 ret = true;
734 goto out;
735
736fail:
737 ret = false;
738
739out:
740 mm_unlock_stage1(&stage1_locked);
741
742 return ret;
743}
744
745/**
746 * Clears a region of physical memory by overwriting it with zeros. The data is
747 * flushed from the cache so the memory has been cleared across the system.
748 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100749static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +0000750 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100751 struct ffa_memory_region_constituent **fragments,
752 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
753 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100754{
755 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +0100756 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100757 bool ret = false;
758
759 /*
760 * Create a local pool so any freed memory can't be used by another
761 * thread. This is to ensure each constituent that is mapped can be
762 * unmapped again afterwards.
763 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000764 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100765
Andrew Walbranca808b12020-05-15 17:22:28 +0100766 /* Iterate over the memory region constituents within each fragment. */
767 for (i = 0; i < fragment_count; ++i) {
768 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100769
Andrew Walbranca808b12020-05-15 17:22:28 +0100770 for (j = 0; j < fragment_constituent_counts[j]; ++j) {
771 size_t size = fragments[i][j].page_count * PAGE_SIZE;
772 paddr_t begin =
773 pa_from_ipa(ipa_init(fragments[i][j].address));
774 paddr_t end = pa_add(begin, size);
775
J-Alves7db32002021-12-14 14:44:50 +0000776 if (!clear_memory(begin, end, &local_page_pool,
777 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100778 /*
779 * api_clear_memory will defrag on failure, so
780 * no need to do it here.
781 */
782 goto out;
783 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100784 }
785 }
786
Jose Marinho09b1db82019-08-08 09:16:59 +0100787 ret = true;
788
789out:
790 mpool_fini(&local_page_pool);
791 return ret;
792}
793
794/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000795 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +0100796 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000797 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +0100798 *
799 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000800 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100801 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +0100802 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100803 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
804 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100805 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +0100806 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100807 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +0100808 */
J-Alves66652252022-07-06 09:49:51 +0100809struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000810 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100811 struct ffa_memory_region_constituent **fragments,
812 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves363f5722022-04-25 17:37:37 +0100813 uint32_t share_func, struct ffa_memory_access *receivers,
814 uint32_t receivers_count, struct mpool *page_pool, bool clear,
815 uint32_t *orig_from_mode_ret)
Jose Marinho09b1db82019-08-08 09:16:59 +0100816{
Andrew Walbranca808b12020-05-15 17:22:28 +0100817 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100818 uint32_t orig_from_mode;
819 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +0100820 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100821 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100822
823 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +0100824 * Make sure constituents are properly aligned to a 64-bit boundary. If
825 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100826 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100827 for (i = 0; i < fragment_count; ++i) {
828 if (!is_aligned(fragments[i], 8)) {
829 dlog_verbose("Constituents not aligned.\n");
830 return ffa_error(FFA_INVALID_PARAMETERS);
831 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100832 }
833
834 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000835 * Check if the state transition is lawful for the sender, ensure that
836 * all constituents of a memory region being shared are at the same
837 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +0100838 */
J-Alves363f5722022-04-25 17:37:37 +0100839 ret = ffa_send_check_transition(from_locked, share_func, receivers,
840 receivers_count, &orig_from_mode,
841 fragments, fragment_constituent_counts,
Andrew Walbranca808b12020-05-15 17:22:28 +0100842 fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100843 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100844 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100845 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100846 }
847
Andrew Walbran37c574e2020-06-03 11:45:46 +0100848 if (orig_from_mode_ret != NULL) {
849 *orig_from_mode_ret = orig_from_mode;
850 }
851
Jose Marinho09b1db82019-08-08 09:16:59 +0100852 /*
853 * Create a local pool so any freed memory can't be used by another
854 * thread. This is to ensure the original mapping can be restored if the
855 * clear fails.
856 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000857 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100858
859 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000860 * First reserve all required memory for the new page table entries
861 * without committing, to make sure the entire operation will succeed
862 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +0100863 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100864 if (!ffa_region_group_identity_map(
865 from_locked, fragments, fragment_constituent_counts,
866 fragment_count, from_mode, page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100867 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100868 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100869 goto out;
870 }
871
872 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000873 * Update the mapping for the sender. This won't allocate because the
874 * transaction was already prepared above, but may free pages in the
875 * case that a whole block is being unmapped that was previously
876 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +0100877 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100878 CHECK(ffa_region_group_identity_map(
879 from_locked, fragments, fragment_constituent_counts,
880 fragment_count, from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100881
882 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +0000883 if (clear &&
884 !ffa_clear_memory_constituents(
885 plat_ffa_owner_world_mode(from_locked.vm->id), fragments,
886 fragment_constituent_counts, fragment_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100887 /*
888 * On failure, roll back by returning memory to the sender. This
889 * may allocate pages which were previously freed into
890 * `local_page_pool` by the call above, but will never allocate
891 * more pages than that so can never fail.
892 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100893 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +0100894 from_locked, fragments, fragment_constituent_counts,
895 fragment_count, orig_from_mode, &local_page_pool,
896 true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100897
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100898 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100899 goto out;
900 }
901
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100902 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000903
904out:
905 mpool_fini(&local_page_pool);
906
907 /*
908 * Tidy up the page table by reclaiming failed mappings (if there was an
909 * error) or merging entries into blocks where possible (on success).
910 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700911 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000912
913 return ret;
914}
915
916/**
917 * Validates and maps memory shared from one VM to another.
918 *
919 * This function requires the calling context to hold the <to> lock.
920 *
921 * Returns:
922 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100923 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000924 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100925 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000926 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100927 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000928 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100929static struct ffa_value ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +0000930 struct vm_locked to_locked, ffa_vm_id_t from_id,
Andrew Walbranca808b12020-05-15 17:22:28 +0100931 struct ffa_memory_region_constituent **fragments,
932 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
933 uint32_t memory_to_attributes, uint32_t share_func, bool clear,
934 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000935{
Andrew Walbranca808b12020-05-15 17:22:28 +0100936 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000937 uint32_t to_mode;
938 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100939 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000940
941 /*
Andrew Walbranca808b12020-05-15 17:22:28 +0100942 * Make sure constituents are properly aligned to a 64-bit boundary. If
943 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000944 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100945 for (i = 0; i < fragment_count; ++i) {
946 if (!is_aligned(fragments[i], 8)) {
947 return ffa_error(FFA_INVALID_PARAMETERS);
948 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000949 }
950
951 /*
952 * Check if the state transition is lawful for the recipient, and ensure
953 * that all constituents of the memory region being retrieved are at the
954 * same state.
955 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100956 ret = ffa_retrieve_check_transition(
957 to_locked, share_func, fragments, fragment_constituent_counts,
958 fragment_count, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100959 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100960 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100961 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000962 }
963
964 /*
965 * Create a local pool so any freed memory can't be used by another
966 * thread. This is to ensure the original mapping can be restored if the
967 * clear fails.
968 */
969 mpool_init_with_fallback(&local_page_pool, page_pool);
970
971 /*
972 * First reserve all required memory for the new page table entries in
973 * the recipient page tables without committing, to make sure the entire
974 * operation will succeed without exhausting the page pool.
975 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100976 if (!ffa_region_group_identity_map(
977 to_locked, fragments, fragment_constituent_counts,
978 fragment_count, to_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000979 /* TODO: partial defrag of failed range. */
980 dlog_verbose(
981 "Insufficient memory to update recipient page "
982 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100983 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000984 goto out;
985 }
986
987 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +0000988 if (clear &&
989 !ffa_clear_memory_constituents(
990 plat_ffa_owner_world_mode(from_id), fragments,
991 fragment_constituent_counts, fragment_count, page_pool)) {
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 Walbran290b0c92020-02-03 16:37:14 +00001019/**
J-Alves8505a8a2022-06-15 18:10:18 +01001020 * Reclaims the given memory from the other world. To do this space is first
1021 * reserved in the <to> VM's page table, then the reclaim request is sent on to
1022 * the other world. then (if that is successful) the memory is mapped back into
1023 * the <to> VM's page table.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001024 *
1025 * This function requires the calling context to hold the <to> lock.
1026 *
1027 * Returns:
1028 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001029 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran290b0c92020-02-03 16:37:14 +00001030 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001031 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran290b0c92020-02-03 16:37:14 +00001032 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001033 * Success is indicated by FFA_SUCCESS.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001034 */
J-Alves8505a8a2022-06-15 18:10:18 +01001035static struct ffa_value ffa_other_world_reclaim_check_update(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001036 struct vm_locked to_locked, ffa_memory_handle_t handle,
1037 struct ffa_memory_region_constituent *constituents,
Andrew Walbran290b0c92020-02-03 16:37:14 +00001038 uint32_t constituent_count, uint32_t memory_to_attributes, bool clear,
1039 struct mpool *page_pool)
1040{
Andrew Walbran290b0c92020-02-03 16:37:14 +00001041 uint32_t to_mode;
1042 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001043 struct ffa_value ret;
J-Alves8505a8a2022-06-15 18:10:18 +01001044 ffa_memory_region_flags_t other_world_flags;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001045
1046 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001047 * Make sure constituents are properly aligned to a 64-bit boundary. If
1048 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001049 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001050 if (!is_aligned(constituents, 8)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001051 dlog_verbose("Constituents not aligned.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001052 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001053 }
1054
1055 /*
1056 * Check if the state transition is lawful for the recipient, and ensure
1057 * that all constituents of the memory region being retrieved are at the
1058 * same state.
1059 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001060 ret = ffa_retrieve_check_transition(to_locked, FFA_MEM_RECLAIM_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01001061 &constituents, &constituent_count,
1062 1, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001063 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001064 dlog_verbose("Invalid transition.\n");
1065 return ret;
1066 }
1067
1068 /*
1069 * Create a local pool so any freed memory can't be used by another
1070 * thread. This is to ensure the original mapping can be restored if the
1071 * clear fails.
1072 */
1073 mpool_init_with_fallback(&local_page_pool, page_pool);
1074
1075 /*
1076 * First reserve all required memory for the new page table entries in
1077 * the recipient page tables without committing, to make sure the entire
1078 * operation will succeed without exhausting the page pool.
1079 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001080 if (!ffa_region_group_identity_map(to_locked, &constituents,
1081 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001082 page_pool, false)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001083 /* TODO: partial defrag of failed range. */
1084 dlog_verbose(
1085 "Insufficient memory to update recipient page "
1086 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001087 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001088 goto out;
1089 }
1090
1091 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01001092 * Forward the request to the other world, check if SPMC returned
1093 * FFA_SUCCESS_32. If not, terminate and return the error to caller
1094 * VM.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001095 */
J-Alves8505a8a2022-06-15 18:10:18 +01001096 other_world_flags = 0;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001097 if (clear) {
J-Alves8505a8a2022-06-15 18:10:18 +01001098 other_world_flags |= FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001099 }
Olivier Deprez112d2b52020-09-30 07:39:23 +02001100 ret = arch_other_world_call(
1101 (struct ffa_value){.func = FFA_MEM_RECLAIM_32,
1102 .arg1 = (uint32_t)handle,
1103 .arg2 = (uint32_t)(handle >> 32),
J-Alves8505a8a2022-06-15 18:10:18 +01001104 .arg3 = other_world_flags});
Andrew Walbran290b0c92020-02-03 16:37:14 +00001105
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001106 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001107 dlog_verbose(
J-Alves8505a8a2022-06-15 18:10:18 +01001108 "Got %#x (%d) from other world in response to "
1109 "FFA_MEM_RECLAIM, "
Andrew Walbranca808b12020-05-15 17:22:28 +01001110 "expected FFA_SUCCESS.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00001111 ret.func, ret.arg2);
1112 goto out;
1113 }
1114
1115 /*
J-Alves8505a8a2022-06-15 18:10:18 +01001116 * The other world was happy with it, so complete the reclaim by mapping
1117 * the memory into the recipient. This won't allocate because the
Andrew Walbran290b0c92020-02-03 16:37:14 +00001118 * transaction was already prepared above, so it doesn't need to use the
1119 * `local_page_pool`.
1120 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001121 CHECK(ffa_region_group_identity_map(to_locked, &constituents,
1122 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001123 page_pool, true));
Andrew Walbran290b0c92020-02-03 16:37:14 +00001124
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001125 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran290b0c92020-02-03 16:37:14 +00001126
1127out:
1128 mpool_fini(&local_page_pool);
1129
1130 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001131 * Tidy up the page table by reclaiming failed mappings (if there was an
1132 * error) or merging entries into blocks where possible (on success).
Andrew Walbran290b0c92020-02-03 16:37:14 +00001133 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001134 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001135
1136 return ret;
1137}
1138
Andrew Walbran996d1d12020-05-27 14:08:43 +01001139static struct ffa_value ffa_relinquish_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001140 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001141 struct ffa_memory_region_constituent **fragments,
1142 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1143 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001144{
1145 uint32_t orig_from_mode;
1146 uint32_t from_mode;
1147 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001148 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001149
Andrew Walbranca808b12020-05-15 17:22:28 +01001150 ret = ffa_relinquish_check_transition(
1151 from_locked, &orig_from_mode, fragments,
1152 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001153 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001154 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001155 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001156 }
1157
1158 /*
1159 * Create a local pool so any freed memory can't be used by another
1160 * thread. This is to ensure the original mapping can be restored if the
1161 * clear fails.
1162 */
1163 mpool_init_with_fallback(&local_page_pool, page_pool);
1164
1165 /*
1166 * First reserve all required memory for the new page table entries
1167 * without committing, to make sure the entire operation will succeed
1168 * without exhausting the page pool.
1169 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001170 if (!ffa_region_group_identity_map(
1171 from_locked, fragments, fragment_constituent_counts,
1172 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001173 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001174 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001175 goto out;
1176 }
1177
1178 /*
1179 * Update the mapping for the sender. This won't allocate because the
1180 * transaction was already prepared above, but may free pages in the
1181 * case that a whole block is being unmapped that was previously
1182 * partially mapped.
1183 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001184 CHECK(ffa_region_group_identity_map(
1185 from_locked, fragments, fragment_constituent_counts,
1186 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001187
1188 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001189 if (clear &&
1190 !ffa_clear_memory_constituents(
1191 plat_ffa_owner_world_mode(from_locked.vm->id), fragments,
1192 fragment_constituent_counts, fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001193 /*
1194 * On failure, roll back by returning memory to the sender. This
1195 * may allocate pages which were previously freed into
1196 * `local_page_pool` by the call above, but will never allocate
1197 * more pages than that so can never fail.
1198 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001199 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001200 from_locked, fragments, fragment_constituent_counts,
1201 fragment_count, orig_from_mode, &local_page_pool,
1202 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001203
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001204 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001205 goto out;
1206 }
1207
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001208 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001209
1210out:
1211 mpool_fini(&local_page_pool);
1212
1213 /*
1214 * Tidy up the page table by reclaiming failed mappings (if there was an
1215 * error) or merging entries into blocks where possible (on success).
1216 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001217 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001218
1219 return ret;
1220}
1221
1222/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001223 * Complete a memory sending operation by checking that it is valid, updating
1224 * the sender page table, and then either marking the share state as having
1225 * completed sending (on success) or freeing it (on failure).
1226 *
1227 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1228 */
1229static struct ffa_value ffa_memory_send_complete(
1230 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001231 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1232 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001233{
1234 struct ffa_memory_region *memory_region = share_state->memory_region;
1235 struct ffa_value ret;
1236
1237 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001238 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001239
1240 /* Check that state is valid in sender page table and update. */
1241 ret = ffa_send_check_update(
1242 from_locked, share_state->fragments,
1243 share_state->fragment_constituent_counts,
1244 share_state->fragment_count, share_state->share_func,
J-Alves363f5722022-04-25 17:37:37 +01001245 memory_region->receivers, memory_region->receiver_count,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001246 page_pool, memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1247 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001248 if (ret.func != FFA_SUCCESS_32) {
1249 /*
1250 * Free share state, it failed to send so it can't be retrieved.
1251 */
1252 dlog_verbose("Complete failed, freeing share state.\n");
1253 share_state_free(share_states, share_state, page_pool);
1254 return ret;
1255 }
1256
1257 share_state->sending_complete = true;
1258 dlog_verbose("Marked sending complete.\n");
1259
J-Alvesee68c542020-10-29 17:48:20 +00001260 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001261}
1262
1263/**
Federico Recanatia98603a2021-12-20 18:04:03 +01001264 * Check that the memory attributes match Hafnium expectations:
1265 * Normal Memory, Inner shareable, Write-Back Read-Allocate
1266 * Write-Allocate Cacheable.
1267 */
1268static struct ffa_value ffa_memory_attributes_validate(
1269 ffa_memory_access_permissions_t attributes)
1270{
1271 enum ffa_memory_type memory_type;
1272 enum ffa_memory_cacheability cacheability;
1273 enum ffa_memory_shareability shareability;
1274
1275 memory_type = ffa_get_memory_type_attr(attributes);
1276 if (memory_type != FFA_MEMORY_NORMAL_MEM) {
1277 dlog_verbose("Invalid memory type %#x, expected %#x.\n",
1278 memory_type, FFA_MEMORY_NORMAL_MEM);
Federico Recanati3d953f32022-02-17 09:31:29 +01001279 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001280 }
1281
1282 cacheability = ffa_get_memory_cacheability_attr(attributes);
1283 if (cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1284 dlog_verbose("Invalid cacheability %#x, expected %#x.\n",
1285 cacheability, FFA_MEMORY_CACHE_WRITE_BACK);
Federico Recanati3d953f32022-02-17 09:31:29 +01001286 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001287 }
1288
1289 shareability = ffa_get_memory_shareability_attr(attributes);
1290 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
1291 dlog_verbose("Invalid shareability %#x, expected #%x.\n",
1292 shareability, FFA_MEMORY_INNER_SHAREABLE);
Federico Recanati3d953f32022-02-17 09:31:29 +01001293 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001294 }
1295
1296 return (struct ffa_value){.func = FFA_SUCCESS_32};
1297}
1298
1299/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001300 * Check that the given `memory_region` represents a valid memory send request
1301 * of the given `share_func` type, return the clear flag and permissions via the
1302 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001303 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001304 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001305 * not.
1306 */
J-Alves66652252022-07-06 09:49:51 +01001307struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001308 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1309 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001310 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001311{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001312 struct ffa_composite_memory_region *composite;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001313 uint32_t receivers_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001314 uint32_t composite_memory_region_offset;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001315 uint32_t constituents_offset;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001316 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001317 enum ffa_data_access data_access;
1318 enum ffa_instruction_access instruction_access;
Federico Recanatia98603a2021-12-20 18:04:03 +01001319 struct ffa_value ret;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001320
J-Alves95df0ef2022-12-07 10:09:48 +00001321 /* The sender must match the caller. */
1322 if ((!vm_id_is_current_world(from_locked.vm->id) &&
1323 vm_id_is_current_world(memory_region->sender)) ||
1324 (vm_id_is_current_world(from_locked.vm->id) &&
1325 memory_region->sender != from_locked.vm->id)) {
1326 dlog_verbose("Invalid memory sender ID.\n");
1327 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001328 }
1329
Andrew Walbrana65a1322020-04-06 19:32:32 +01001330 /*
1331 * Ensure that the composite header is within the memory bounds and
1332 * doesn't overlap the first part of the message.
1333 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001334 receivers_length = sizeof(struct ffa_memory_access) *
1335 memory_region->receiver_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001336 constituents_offset =
1337 ffa_composite_constituent_offset(memory_region, 0);
Federico Recanati872cd692022-01-05 13:10:10 +01001338 composite_memory_region_offset =
1339 memory_region->receivers[0].composite_memory_region_offset;
1340 if ((composite_memory_region_offset == 0) ||
1341 (composite_memory_region_offset <
1342 sizeof(struct ffa_memory_region) + receivers_length) ||
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001343 constituents_offset > fragment_length) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001344 dlog_verbose(
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001345 "Invalid composite memory region descriptor offset "
1346 "%d.\n",
1347 memory_region->receivers[0]
1348 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001349 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001350 }
1351
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001352 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001353
1354 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001355 * Ensure the number of constituents are within the memory bounds.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001356 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001357 constituents_length = sizeof(struct ffa_memory_region_constituent) *
1358 composite->constituent_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001359 if (memory_share_length != constituents_offset + constituents_length) {
1360 dlog_verbose("Invalid length %d or composite offset %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001361 memory_share_length,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001362 memory_region->receivers[0]
1363 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001364 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001365 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001366 if (fragment_length < memory_share_length &&
1367 fragment_length < HF_MAILBOX_SIZE) {
1368 dlog_warning(
1369 "Initial fragment length %d smaller than mailbox "
1370 "size.\n",
1371 fragment_length);
1372 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001373
Andrew Walbrana65a1322020-04-06 19:32:32 +01001374 /*
1375 * Clear is not allowed for memory sharing, as the sender still has
1376 * access to the memory.
1377 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001378 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1379 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001380 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001381 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001382 }
1383
1384 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001385 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001386 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001387 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001388 }
1389
J-Alves363f5722022-04-25 17:37:37 +01001390 /* Check that the permissions are valid, for each specified receiver. */
1391 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
1392 ffa_memory_access_permissions_t permissions =
1393 memory_region->receivers[i]
1394 .receiver_permissions.permissions;
1395 ffa_vm_id_t receiver_id =
1396 memory_region->receivers[i]
1397 .receiver_permissions.receiver;
1398
1399 if (memory_region->sender == receiver_id) {
1400 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001401 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001402 }
Federico Recanati85090c42021-12-15 13:17:54 +01001403
J-Alves363f5722022-04-25 17:37:37 +01001404 for (uint32_t j = i + 1; j < memory_region->receiver_count;
1405 j++) {
1406 if (receiver_id ==
1407 memory_region->receivers[j]
1408 .receiver_permissions.receiver) {
1409 dlog_verbose(
1410 "Repeated receiver(%x) in memory send "
1411 "operation.\n",
1412 memory_region->receivers[j]
1413 .receiver_permissions.receiver);
1414 return ffa_error(FFA_INVALID_PARAMETERS);
1415 }
1416 }
1417
1418 if (composite_memory_region_offset !=
1419 memory_region->receivers[i]
1420 .composite_memory_region_offset) {
1421 dlog_verbose(
1422 "All ffa_memory_access should point to the "
1423 "same composite memory region offset.\n");
1424 return ffa_error(FFA_INVALID_PARAMETERS);
1425 }
1426
1427 data_access = ffa_get_data_access_attr(permissions);
1428 instruction_access =
1429 ffa_get_instruction_access_attr(permissions);
1430 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1431 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
1432 dlog_verbose(
1433 "Reserved value for receiver permissions "
1434 "%#x.\n",
1435 permissions);
1436 return ffa_error(FFA_INVALID_PARAMETERS);
1437 }
1438 if (instruction_access !=
1439 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
1440 dlog_verbose(
1441 "Invalid instruction access permissions %#x "
1442 "for sending memory.\n",
1443 permissions);
1444 return ffa_error(FFA_INVALID_PARAMETERS);
1445 }
1446 if (share_func == FFA_MEM_SHARE_32) {
1447 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1448 dlog_verbose(
1449 "Invalid data access permissions %#x "
1450 "for sharing memory.\n",
1451 permissions);
1452 return ffa_error(FFA_INVALID_PARAMETERS);
1453 }
1454 /*
1455 * According to section 10.10.3 of the FF-A v1.1 EAC0
1456 * spec, NX is required for share operations (but must
1457 * not be specified by the sender) so set it in the
1458 * copy that we store, ready to be returned to the
1459 * retriever.
1460 */
J-Alvesb19731a2022-06-20 17:30:33 +01001461 if (vm_id_is_current_world(receiver_id)) {
1462 ffa_set_instruction_access_attr(
1463 &permissions,
1464 FFA_INSTRUCTION_ACCESS_NX);
1465 memory_region->receivers[i]
1466 .receiver_permissions.permissions =
1467 permissions;
1468 }
J-Alves363f5722022-04-25 17:37:37 +01001469 }
1470 if (share_func == FFA_MEM_LEND_32 &&
1471 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1472 dlog_verbose(
1473 "Invalid data access permissions %#x for "
1474 "lending memory.\n",
1475 permissions);
1476 return ffa_error(FFA_INVALID_PARAMETERS);
1477 }
1478
1479 if (share_func == FFA_MEM_DONATE_32 &&
1480 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
1481 dlog_verbose(
1482 "Invalid data access permissions %#x for "
1483 "donating memory.\n",
1484 permissions);
1485 return ffa_error(FFA_INVALID_PARAMETERS);
1486 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001487 }
1488
Federico Recanatid937f5e2021-12-20 17:38:23 +01001489 /*
J-Alves807794e2022-06-16 13:42:47 +01001490 * If a memory donate or lend with single borrower, the memory type
1491 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01001492 */
J-Alves807794e2022-06-16 13:42:47 +01001493 if (share_func == FFA_MEM_DONATE_32 ||
1494 (share_func == FFA_MEM_LEND_32 &&
1495 memory_region->receiver_count == 1)) {
1496 if (ffa_get_memory_type_attr(memory_region->attributes) !=
1497 FFA_MEMORY_NOT_SPECIFIED_MEM) {
1498 dlog_verbose(
1499 "Memory type shall not be specified by "
1500 "sender.\n");
1501 return ffa_error(FFA_INVALID_PARAMETERS);
1502 }
1503 } else {
1504 /*
1505 * Check that sender's memory attributes match Hafnium
1506 * expectations: Normal Memory, Inner shareable, Write-Back
1507 * Read-Allocate Write-Allocate Cacheable.
1508 */
1509 ret = ffa_memory_attributes_validate(memory_region->attributes);
1510 if (ret.func != FFA_SUCCESS_32) {
1511 return ret;
1512 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01001513 }
1514
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001515 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001516}
1517
1518/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001519 * Gets the share state for continuing an operation to donate, lend or share
1520 * memory, and checks that it is a valid request.
1521 *
1522 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1523 * not.
1524 */
1525static struct ffa_value ffa_memory_send_continue_validate(
1526 struct share_states_locked share_states, ffa_memory_handle_t handle,
1527 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1528 struct mpool *page_pool)
1529{
1530 struct ffa_memory_share_state *share_state;
1531 struct ffa_memory_region *memory_region;
1532
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001533 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001534
1535 /*
1536 * Look up the share state by handle and make sure that the VM ID
1537 * matches.
1538 */
1539 if (!get_share_state(share_states, handle, &share_state)) {
1540 dlog_verbose(
1541 "Invalid handle %#x for memory send continuation.\n",
1542 handle);
1543 return ffa_error(FFA_INVALID_PARAMETERS);
1544 }
1545 memory_region = share_state->memory_region;
1546
1547 if (memory_region->sender != from_vm_id) {
1548 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1549 return ffa_error(FFA_INVALID_PARAMETERS);
1550 }
1551
1552 if (share_state->sending_complete) {
1553 dlog_verbose(
1554 "Sending of memory handle %#x is already complete.\n",
1555 handle);
1556 return ffa_error(FFA_INVALID_PARAMETERS);
1557 }
1558
1559 if (share_state->fragment_count == MAX_FRAGMENTS) {
1560 /*
1561 * Log a warning as this is a sign that MAX_FRAGMENTS should
1562 * probably be increased.
1563 */
1564 dlog_warning(
1565 "Too many fragments for memory share with handle %#x; "
1566 "only %d supported.\n",
1567 handle, MAX_FRAGMENTS);
1568 /* Free share state, as it's not possible to complete it. */
1569 share_state_free(share_states, share_state, page_pool);
1570 return ffa_error(FFA_NO_MEMORY);
1571 }
1572
1573 *share_state_ret = share_state;
1574
1575 return (struct ffa_value){.func = FFA_SUCCESS_32};
1576}
1577
1578/**
J-Alves8505a8a2022-06-15 18:10:18 +01001579 * Forwards a memory send continuation message on to the other world.
Andrew Walbranca808b12020-05-15 17:22:28 +01001580 */
J-Alves8505a8a2022-06-15 18:10:18 +01001581static struct ffa_value memory_send_continue_other_world_forward(
1582 struct vm_locked other_world_locked, ffa_vm_id_t sender_vm_id,
1583 void *fragment, uint32_t fragment_length, ffa_memory_handle_t handle)
Andrew Walbranca808b12020-05-15 17:22:28 +01001584{
1585 struct ffa_value ret;
1586
J-Alves8505a8a2022-06-15 18:10:18 +01001587 memcpy_s(other_world_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
1588 fragment, fragment_length);
1589 other_world_locked.vm->mailbox.recv_size = fragment_length;
1590 other_world_locked.vm->mailbox.recv_sender = sender_vm_id;
1591 other_world_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
1592 other_world_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001593 ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01001594 (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
1595 .arg1 = (uint32_t)handle,
1596 .arg2 = (uint32_t)(handle >> 32),
1597 .arg3 = fragment_length,
1598 .arg4 = (uint64_t)sender_vm_id << 16});
1599 /*
J-Alves8505a8a2022-06-15 18:10:18 +01001600 * After the call to the other world completes it must have finished
1601 * reading its RX buffer, so it is ready for another message.
Andrew Walbranca808b12020-05-15 17:22:28 +01001602 */
J-Alves8505a8a2022-06-15 18:10:18 +01001603 other_world_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbranca808b12020-05-15 17:22:28 +01001604
1605 return ret;
1606}
1607
1608/**
J-Alves95df0ef2022-12-07 10:09:48 +00001609 * Checks if there is at least one receiver from the other world.
1610 */
1611static bool memory_region_receivers_from_other_world(
1612 struct ffa_memory_region *memory_region)
1613{
1614 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
1615 ffa_vm_id_t receiver = memory_region->receivers[i]
1616 .receiver_permissions.receiver;
1617 if (!vm_id_is_current_world(receiver)) {
1618 return true;
1619 }
1620 }
1621 return false;
1622}
1623
1624/**
J-Alves8505a8a2022-06-15 18:10:18 +01001625 * Validates a call to donate, lend or share memory to a non-other world VM and
1626 * then updates the stage-2 page tables. Specifically, check if the message
1627 * length and number of memory region constituents match, and if the transition
1628 * is valid for the type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001629 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001630 * Assumes that the caller has already found and locked the sender VM and copied
1631 * the memory region descriptor from the sender's TX buffer to a freshly
1632 * allocated page from Hafnium's internal pool. The caller must have also
1633 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001634 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001635 * This function takes ownership of the `memory_region` passed in and will free
1636 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001637 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001638struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001639 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001640 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001641 uint32_t fragment_length, uint32_t share_func,
1642 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001643{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001644 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001645 struct share_states_locked share_states;
1646 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001647
1648 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001649 * If there is an error validating the `memory_region` then we need to
1650 * free it because we own it but we won't be storing it in a share state
1651 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001652 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001653 ret = ffa_memory_send_validate(from_locked, memory_region,
1654 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001655 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001656 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001657 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001658 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001659 }
1660
Andrew Walbrana65a1322020-04-06 19:32:32 +01001661 /* Set flag for share function, ready to be retrieved later. */
1662 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001663 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001664 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001665 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001666 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001667 case FFA_MEM_LEND_32:
1668 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001669 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001670 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001671 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001672 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001673 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001674 }
1675
Andrew Walbranca808b12020-05-15 17:22:28 +01001676 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001677 /*
1678 * Allocate a share state before updating the page table. Otherwise if
1679 * updating the page table succeeded but allocating the share state
1680 * failed then it would leave the memory in a state where nobody could
1681 * get it back.
1682 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001683 if (!allocate_share_state(share_states, share_func, memory_region,
1684 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1685 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001686 dlog_verbose("Failed to allocate share state.\n");
1687 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001688 ret = ffa_error(FFA_NO_MEMORY);
1689 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001690 }
1691
Andrew Walbranca808b12020-05-15 17:22:28 +01001692 if (fragment_length == memory_share_length) {
1693 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00001694 ret = ffa_memory_send_complete(
1695 from_locked, share_states, share_state, page_pool,
1696 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001697 } else {
1698 ret = (struct ffa_value){
1699 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00001700 .arg1 = (uint32_t)memory_region->handle,
1701 .arg2 = (uint32_t)(memory_region->handle >> 32),
Andrew Walbranca808b12020-05-15 17:22:28 +01001702 .arg3 = fragment_length};
1703 }
1704
1705out:
1706 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001707 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001708 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001709}
1710
1711/**
J-Alves8505a8a2022-06-15 18:10:18 +01001712 * Continues an operation to donate, lend or share memory to a VM from current
1713 * world. If this is the last fragment then checks that the transition is valid
1714 * for the type of memory sending operation and updates the stage-2 page tables
1715 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01001716 *
1717 * Assumes that the caller has already found and locked the sender VM and copied
1718 * the memory region descriptor from the sender's TX buffer to a freshly
1719 * allocated page from Hafnium's internal pool.
1720 *
1721 * This function takes ownership of the `fragment` passed in; it must not be
1722 * freed by the caller.
1723 */
1724struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1725 void *fragment,
1726 uint32_t fragment_length,
1727 ffa_memory_handle_t handle,
1728 struct mpool *page_pool)
1729{
1730 struct share_states_locked share_states = share_states_lock();
1731 struct ffa_memory_share_state *share_state;
1732 struct ffa_value ret;
1733 struct ffa_memory_region *memory_region;
1734
1735 ret = ffa_memory_send_continue_validate(share_states, handle,
1736 &share_state,
1737 from_locked.vm->id, page_pool);
1738 if (ret.func != FFA_SUCCESS_32) {
1739 goto out_free_fragment;
1740 }
1741 memory_region = share_state->memory_region;
1742
J-Alves95df0ef2022-12-07 10:09:48 +00001743 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001744 dlog_error(
1745 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01001746 "other world. This should never happen, and indicates "
1747 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01001748 "EL3 code.\n");
1749 ret = ffa_error(FFA_INVALID_PARAMETERS);
1750 goto out_free_fragment;
1751 }
1752
1753 /* Add this fragment. */
1754 share_state->fragments[share_state->fragment_count] = fragment;
1755 share_state->fragment_constituent_counts[share_state->fragment_count] =
1756 fragment_length / sizeof(struct ffa_memory_region_constituent);
1757 share_state->fragment_count++;
1758
1759 /* Check whether the memory send operation is now ready to complete. */
1760 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00001761 ret = ffa_memory_send_complete(
1762 from_locked, share_states, share_state, page_pool,
1763 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001764 } else {
1765 ret = (struct ffa_value){
1766 .func = FFA_MEM_FRAG_RX_32,
1767 .arg1 = (uint32_t)handle,
1768 .arg2 = (uint32_t)(handle >> 32),
1769 .arg3 = share_state_next_fragment_offset(share_states,
1770 share_state)};
1771 }
1772 goto out;
1773
1774out_free_fragment:
1775 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001776
1777out:
Andrew Walbranca808b12020-05-15 17:22:28 +01001778 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001779 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001780}
1781
Andrew Walbranca808b12020-05-15 17:22:28 +01001782/**
J-Alves8505a8a2022-06-15 18:10:18 +01001783 * Continues an operation to donate, lend or share memory to the other world VM.
1784 * If this is the last fragment then checks that the transition is valid for the
1785 * type of memory sending operation and updates the stage-2 page tables of the
1786 * sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01001787 *
1788 * Assumes that the caller has already found and locked the sender VM and copied
1789 * the memory region descriptor from the sender's TX buffer to a freshly
1790 * allocated page from Hafnium's internal pool.
1791 *
1792 * This function takes ownership of the `memory_region` passed in and will free
1793 * it when necessary; it must not be freed by the caller.
1794 */
J-Alves8505a8a2022-06-15 18:10:18 +01001795struct ffa_value ffa_memory_other_world_send_continue(
1796 struct vm_locked from_locked, struct vm_locked to_locked,
1797 void *fragment, uint32_t fragment_length, ffa_memory_handle_t handle,
1798 struct mpool *page_pool)
Andrew Walbranca808b12020-05-15 17:22:28 +01001799{
1800 struct share_states_locked share_states = share_states_lock();
1801 struct ffa_memory_share_state *share_state;
1802 struct ffa_value ret;
1803 struct ffa_memory_region *memory_region;
1804
1805 ret = ffa_memory_send_continue_validate(share_states, handle,
1806 &share_state,
1807 from_locked.vm->id, page_pool);
1808 if (ret.func != FFA_SUCCESS_32) {
1809 goto out_free_fragment;
1810 }
1811 memory_region = share_state->memory_region;
1812
J-Alves95df0ef2022-12-07 10:09:48 +00001813 if (!memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001814 dlog_error(
J-Alves8505a8a2022-06-15 18:10:18 +01001815 "Got SPM-allocated handle for memory send to non-other "
1816 "world VM. This should never happen, and indicates a "
1817 "bug.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +01001818 ret = ffa_error(FFA_INVALID_PARAMETERS);
1819 goto out_free_fragment;
1820 }
1821
1822 if (to_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
1823 to_locked.vm->mailbox.recv == NULL) {
1824 /*
J-Alves8505a8a2022-06-15 18:10:18 +01001825 * If the other world RX buffer is not available, tell the
1826 * sender to retry by returning the current offset again.
Andrew Walbranca808b12020-05-15 17:22:28 +01001827 */
1828 ret = (struct ffa_value){
1829 .func = FFA_MEM_FRAG_RX_32,
1830 .arg1 = (uint32_t)handle,
1831 .arg2 = (uint32_t)(handle >> 32),
1832 .arg3 = share_state_next_fragment_offset(share_states,
1833 share_state),
1834 };
1835 goto out_free_fragment;
1836 }
1837
1838 /* Add this fragment. */
1839 share_state->fragments[share_state->fragment_count] = fragment;
1840 share_state->fragment_constituent_counts[share_state->fragment_count] =
1841 fragment_length / sizeof(struct ffa_memory_region_constituent);
1842 share_state->fragment_count++;
1843
1844 /* Check whether the memory send operation is now ready to complete. */
1845 if (share_state_sending_complete(share_states, share_state)) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001846 struct mpool local_page_pool;
1847 uint32_t orig_from_mode;
1848
1849 /*
1850 * Use a local page pool so that we can roll back if necessary.
1851 */
1852 mpool_init_with_fallback(&local_page_pool, page_pool);
1853
Andrew Walbranca808b12020-05-15 17:22:28 +01001854 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001855 share_state, &local_page_pool,
1856 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001857
1858 if (ret.func == FFA_SUCCESS_32) {
1859 /*
J-Alves8505a8a2022-06-15 18:10:18 +01001860 * Forward final fragment on to the other world so that
Andrew Walbranca808b12020-05-15 17:22:28 +01001861 * it can complete the memory sending operation.
1862 */
J-Alves8505a8a2022-06-15 18:10:18 +01001863 ret = memory_send_continue_other_world_forward(
Andrew Walbranca808b12020-05-15 17:22:28 +01001864 to_locked, from_locked.vm->id, fragment,
1865 fragment_length, handle);
1866
1867 if (ret.func != FFA_SUCCESS_32) {
1868 /*
1869 * The error will be passed on to the caller,
1870 * but log it here too.
1871 */
1872 dlog_verbose(
J-Alves8505a8a2022-06-15 18:10:18 +01001873 "other world didn't successfully "
1874 "complete "
Andrew Walbranca808b12020-05-15 17:22:28 +01001875 "memory send operation; returned %#x "
Andrew Walbran37c574e2020-06-03 11:45:46 +01001876 "(%d). Rolling back.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01001877 ret.func, ret.arg2);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001878
1879 /*
J-Alves8505a8a2022-06-15 18:10:18 +01001880 * The other world failed to complete the send
Andrew Walbran37c574e2020-06-03 11:45:46 +01001881 * operation, so roll back the page table update
1882 * for the VM. This can't fail because it won't
1883 * try to allocate more memory than was freed
1884 * into the `local_page_pool` by
1885 * `ffa_send_check_update` in the initial
1886 * update.
1887 */
1888 CHECK(ffa_region_group_identity_map(
1889 from_locked, share_state->fragments,
1890 share_state
1891 ->fragment_constituent_counts,
1892 share_state->fragment_count,
1893 orig_from_mode, &local_page_pool,
1894 true));
Andrew Walbranca808b12020-05-15 17:22:28 +01001895 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01001896
Andrew Walbranca808b12020-05-15 17:22:28 +01001897 /* Free share state. */
1898 share_state_free(share_states, share_state, page_pool);
1899 } else {
J-Alves8505a8a2022-06-15 18:10:18 +01001900 /* Abort sending to other world. */
1901 struct ffa_value other_world_ret =
Olivier Deprez112d2b52020-09-30 07:39:23 +02001902 arch_other_world_call((struct ffa_value){
Andrew Walbranca808b12020-05-15 17:22:28 +01001903 .func = FFA_MEM_RECLAIM_32,
1904 .arg1 = (uint32_t)handle,
1905 .arg2 = (uint32_t)(handle >> 32)});
1906
J-Alves8505a8a2022-06-15 18:10:18 +01001907 if (other_world_ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001908 /*
J-Alves8505a8a2022-06-15 18:10:18 +01001909 * Nothing we can do if other world doesn't
1910 * abort properly, just log it.
Andrew Walbranca808b12020-05-15 17:22:28 +01001911 */
1912 dlog_verbose(
J-Alves8505a8a2022-06-15 18:10:18 +01001913 "other world didn't successfully abort "
1914 "failed "
Andrew Walbranca808b12020-05-15 17:22:28 +01001915 "memory send operation; returned %#x "
1916 "(%d).\n",
J-Alves8505a8a2022-06-15 18:10:18 +01001917 other_world_ret.func,
1918 other_world_ret.arg2);
Andrew Walbranca808b12020-05-15 17:22:28 +01001919 }
1920 /*
1921 * We don't need to free the share state in this case
1922 * because ffa_memory_send_complete does that already.
1923 */
1924 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01001925
1926 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001927 } else {
1928 uint32_t next_fragment_offset =
1929 share_state_next_fragment_offset(share_states,
1930 share_state);
1931
J-Alves8505a8a2022-06-15 18:10:18 +01001932 ret = memory_send_continue_other_world_forward(
Andrew Walbranca808b12020-05-15 17:22:28 +01001933 to_locked, from_locked.vm->id, fragment,
1934 fragment_length, handle);
1935
1936 if (ret.func != FFA_MEM_FRAG_RX_32 ||
1937 ffa_frag_handle(ret) != handle ||
1938 ret.arg3 != next_fragment_offset ||
1939 ffa_frag_sender(ret) != from_locked.vm->id) {
1940 dlog_verbose(
1941 "Got unexpected result from forwarding "
J-Alves8505a8a2022-06-15 18:10:18 +01001942 "FFA_MEM_FRAG_TX to other world. %#x (handle "
1943 "%#x, "
Andrew Walbranca808b12020-05-15 17:22:28 +01001944 "offset %d, sender %d); expected "
1945 "FFA_MEM_FRAG_RX (handle %#x, offset %d, "
1946 "sender %d).\n",
1947 ret.func, ffa_frag_handle(ret), ret.arg3,
1948 ffa_frag_sender(ret), handle,
1949 next_fragment_offset, from_locked.vm->id);
1950 /* Free share state. */
1951 share_state_free(share_states, share_state, page_pool);
1952 ret = ffa_error(FFA_INVALID_PARAMETERS);
1953 goto out;
1954 }
1955
1956 ret = (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
1957 .arg1 = (uint32_t)handle,
1958 .arg2 = (uint32_t)(handle >> 32),
1959 .arg3 = next_fragment_offset};
1960 }
1961 goto out;
1962
1963out_free_fragment:
1964 mpool_free(page_pool, fragment);
1965
1966out:
1967 share_states_unlock(&share_states);
1968 return ret;
1969}
1970
1971/** Clean up after the receiver has finished retrieving a memory region. */
1972static void ffa_memory_retrieve_complete(
1973 struct share_states_locked share_states,
1974 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
1975{
1976 if (share_state->share_func == FFA_MEM_DONATE_32) {
1977 /*
1978 * Memory that has been donated can't be relinquished,
1979 * so no need to keep the share state around.
1980 */
1981 share_state_free(share_states, share_state, page_pool);
1982 dlog_verbose("Freed share state for donate.\n");
1983 }
1984}
1985
J-Alves96de29f2022-04-26 16:05:24 +01001986/*
1987 * Gets the receiver's access permissions from 'struct ffa_memory_region' and
1988 * returns its index in the receiver's array. If receiver's ID doesn't exist
1989 * in the array, return the region's 'receiver_count'.
1990 */
1991static uint32_t ffa_memory_region_get_receiver(
1992 struct ffa_memory_region *memory_region, ffa_vm_id_t receiver)
1993{
1994 struct ffa_memory_access *receivers;
1995 uint32_t i;
1996
1997 assert(memory_region != NULL);
1998
1999 receivers = memory_region->receivers;
2000
2001 for (i = 0U; i < memory_region->receiver_count; i++) {
2002 if (receivers[i].receiver_permissions.receiver == receiver) {
2003 break;
2004 }
2005 }
2006
2007 return i;
2008}
2009
2010/**
2011 * Validates the retrieved permissions against those specified by the lender
2012 * of memory share operation. Optionally can help set the permissions to be used
2013 * for the S2 mapping, through the `permissions` argument.
2014 * Returns true if permissions are valid, false otherwise.
2015 */
2016static bool ffa_memory_retrieve_is_memory_access_valid(
2017 enum ffa_data_access sent_data_access,
2018 enum ffa_data_access requested_data_access,
2019 enum ffa_instruction_access sent_instruction_access,
2020 enum ffa_instruction_access requested_instruction_access,
2021 ffa_memory_access_permissions_t *permissions)
2022{
2023 switch (sent_data_access) {
2024 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2025 case FFA_DATA_ACCESS_RW:
2026 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2027 requested_data_access == FFA_DATA_ACCESS_RW) {
2028 if (permissions != NULL) {
2029 ffa_set_data_access_attr(permissions,
2030 FFA_DATA_ACCESS_RW);
2031 }
2032 break;
2033 }
2034 /* Intentional fall-through. */
2035 case FFA_DATA_ACCESS_RO:
2036 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2037 requested_data_access == FFA_DATA_ACCESS_RO) {
2038 if (permissions != NULL) {
2039 ffa_set_data_access_attr(permissions,
2040 FFA_DATA_ACCESS_RO);
2041 }
2042 break;
2043 }
2044 dlog_verbose(
2045 "Invalid data access requested; sender specified "
2046 "permissions %#x but receiver requested %#x.\n",
2047 sent_data_access, requested_data_access);
2048 return false;
2049 case FFA_DATA_ACCESS_RESERVED:
2050 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
2051 "checked before this point.");
2052 }
2053
2054 switch (sent_instruction_access) {
2055 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2056 case FFA_INSTRUCTION_ACCESS_X:
2057 if (requested_instruction_access ==
2058 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2059 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
2060 if (permissions != NULL) {
2061 ffa_set_instruction_access_attr(
2062 permissions, FFA_INSTRUCTION_ACCESS_X);
2063 }
2064 break;
2065 }
2066 case FFA_INSTRUCTION_ACCESS_NX:
2067 if (requested_instruction_access ==
2068 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2069 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2070 if (permissions != NULL) {
2071 ffa_set_instruction_access_attr(
2072 permissions, FFA_INSTRUCTION_ACCESS_NX);
2073 }
2074 break;
2075 }
2076 dlog_verbose(
2077 "Invalid instruction access requested; sender "
2078 "specified permissions %#x but receiver requested "
2079 "%#x.\n",
2080 sent_instruction_access, requested_instruction_access);
2081 return false;
2082 case FFA_INSTRUCTION_ACCESS_RESERVED:
2083 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
2084 "be checked before this point.");
2085 }
2086
2087 return true;
2088}
2089
2090/**
2091 * Validate the receivers' permissions in the retrieve request against those
2092 * specified by the lender.
2093 * In the `permissions` argument returns the permissions to set at S2 for the
2094 * caller to the FFA_MEMORY_RETRIEVE_REQ.
2095 * Returns FFA_SUCCESS if all specified permissions are valid.
2096 */
2097static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
2098 struct ffa_memory_region *memory_region,
2099 struct ffa_memory_region *retrieve_request, ffa_vm_id_t to_vm_id,
2100 ffa_memory_access_permissions_t *permissions)
2101{
2102 uint32_t retrieve_receiver_index;
2103
2104 assert(permissions != NULL);
2105
2106 if (retrieve_request->receiver_count != memory_region->receiver_count) {
2107 dlog_verbose(
2108 "Retrieve request should contain same list of "
2109 "borrowers, as specified by the lender.\n");
2110 return ffa_error(FFA_INVALID_PARAMETERS);
2111 }
2112
2113 retrieve_receiver_index = retrieve_request->receiver_count;
2114
2115 /* Should be populated with the permissions of the retriever. */
2116 *permissions = 0;
2117
2118 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
2119 ffa_memory_access_permissions_t sent_permissions;
2120 struct ffa_memory_access *current_receiver =
2121 &retrieve_request->receivers[i];
2122 ffa_memory_access_permissions_t requested_permissions =
2123 current_receiver->receiver_permissions.permissions;
2124 ffa_vm_id_t current_receiver_id =
2125 current_receiver->receiver_permissions.receiver;
2126 bool found_to_id = current_receiver_id == to_vm_id;
2127
2128 /*
2129 * Find the current receiver in the transaction descriptor from
2130 * sender.
2131 */
2132 uint32_t mem_region_receiver_index =
2133 ffa_memory_region_get_receiver(memory_region,
2134 current_receiver_id);
2135
2136 if (mem_region_receiver_index ==
2137 memory_region->receiver_count) {
2138 dlog_verbose("%s: receiver %x not found\n", __func__,
2139 current_receiver_id);
2140 return ffa_error(FFA_DENIED);
2141 }
2142
2143 sent_permissions =
2144 memory_region->receivers[mem_region_receiver_index]
2145 .receiver_permissions.permissions;
2146
2147 if (found_to_id) {
2148 retrieve_receiver_index = i;
2149 }
2150
2151 /*
2152 * Since we are traversing the list of receivers, save the index
2153 * of the caller. As it needs to be there.
2154 */
2155
2156 if (current_receiver->composite_memory_region_offset != 0U) {
2157 dlog_verbose(
2158 "Retriever specified address ranges not "
2159 "supported (got offset %d).\n",
2160 current_receiver
2161 ->composite_memory_region_offset);
2162 return ffa_error(FFA_INVALID_PARAMETERS);
2163 }
2164
2165 /*
2166 * Check permissions from sender against permissions requested
2167 * by receiver.
2168 */
2169 if (!ffa_memory_retrieve_is_memory_access_valid(
2170 ffa_get_data_access_attr(sent_permissions),
2171 ffa_get_data_access_attr(requested_permissions),
2172 ffa_get_instruction_access_attr(sent_permissions),
2173 ffa_get_instruction_access_attr(
2174 requested_permissions),
2175 found_to_id ? permissions : NULL)) {
2176 return ffa_error(FFA_DENIED);
2177 }
2178
2179 /*
2180 * Can't request PM to clear memory if only provided with RO
2181 * permissions.
2182 */
2183 if (found_to_id &&
2184 (ffa_get_data_access_attr(*permissions) ==
2185 FFA_DATA_ACCESS_RO) &&
2186 (retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2187 0U) {
2188 dlog_verbose(
2189 "Receiver has RO permissions can not request "
2190 "clear.\n");
2191 return ffa_error(FFA_DENIED);
2192 }
2193 }
2194
2195 if (retrieve_receiver_index == retrieve_request->receiver_count) {
2196 dlog_verbose(
2197 "Retrieve request does not contain caller's (%x) "
2198 "permissions\n",
2199 to_vm_id);
2200 return ffa_error(FFA_INVALID_PARAMETERS);
2201 }
2202
2203 return (struct ffa_value){.func = FFA_SUCCESS_32};
2204}
2205
J-Alvesa9cd7e32022-07-01 13:49:33 +01002206/*
2207 * According to section 16.4.3 of FF-A v1.1 EAC0 specification, the hypervisor
2208 * may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region description
2209 * of a pending memory sharing operation whose allocator is the SPM, for
2210 * validation purposes before forwarding an FFA_MEM_RECLAIM call. In doing so
2211 * the memory region descriptor of the retrieve request must be zeroed with the
2212 * exception of the sender ID and handle.
2213 */
2214bool is_ffa_memory_retrieve_borrower_request(struct ffa_memory_region *request,
2215 struct vm_locked to_locked)
2216{
2217 return to_locked.vm->id == HF_HYPERVISOR_VM_ID &&
2218 request->attributes == 0U && request->flags == 0U &&
2219 request->tag == 0U && request->receiver_count == 0U &&
2220 plat_ffa_memory_handle_allocated_by_current_world(
2221 request->handle);
2222}
2223
2224/*
2225 * Helper to reset count of fragments retrieved by the hypervisor.
2226 */
2227static void ffa_memory_retrieve_complete_from_hyp(
2228 struct ffa_memory_share_state *share_state)
2229{
2230 if (share_state->hypervisor_fragment_count ==
2231 share_state->fragment_count) {
2232 share_state->hypervisor_fragment_count = 0;
2233 }
2234}
2235
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002236struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2237 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002238 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002239 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002240{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002241 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002242 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002243 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002244 sizeof(struct ffa_memory_access);
2245 ffa_memory_handle_t handle = retrieve_request->handle;
2246 ffa_memory_region_flags_t transaction_type =
Andrew Walbrana65a1322020-04-06 19:32:32 +01002247 retrieve_request->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002248 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
2249 struct ffa_memory_region *memory_region;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002250 ffa_memory_access_permissions_t permissions = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002251 uint32_t memory_to_attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002252 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002253 struct ffa_memory_share_state *share_state;
2254 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002255 struct ffa_composite_memory_region *composite;
2256 uint32_t total_length;
2257 uint32_t fragment_length;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002258 ffa_vm_id_t receiver_id;
2259 bool is_send_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002260
2261 dump_share_states();
2262
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002263 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002264 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002265 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002266 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002267 expected_retrieve_request_length,
2268 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002269 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002270 }
2271
2272 share_states = share_states_lock();
2273 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002274 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002275 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002276 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002277 goto out;
2278 }
2279
J-Alves96de29f2022-04-26 16:05:24 +01002280 if (!share_state->sending_complete) {
2281 dlog_verbose(
2282 "Memory with handle %#x not fully sent, can't "
2283 "retrieve.\n",
2284 handle);
2285 ret = ffa_error(FFA_INVALID_PARAMETERS);
2286 goto out;
2287 }
2288
Andrew Walbrana65a1322020-04-06 19:32:32 +01002289 memory_region = share_state->memory_region;
2290 CHECK(memory_region != NULL);
2291
J-Alvesa9cd7e32022-07-01 13:49:33 +01002292 receiver_id = to_locked.vm->id;
J-Alves96de29f2022-04-26 16:05:24 +01002293
J-Alvesa9cd7e32022-07-01 13:49:33 +01002294 if (!is_ffa_memory_retrieve_borrower_request(retrieve_request,
2295 to_locked)) {
2296 uint32_t receiver_index;
J-Alves614d9f42022-06-28 14:03:10 +01002297 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002298 * Find receiver index in the receivers list specified by the
2299 * sender.
J-Alves614d9f42022-06-28 14:03:10 +01002300 */
J-Alvesa9cd7e32022-07-01 13:49:33 +01002301 receiver_index = ffa_memory_region_get_receiver(
2302 memory_region, to_locked.vm->id);
2303
2304 if (receiver_index == memory_region->receiver_count) {
2305 dlog_verbose(
2306 "Incorrect receiver VM ID %x for "
2307 "FFA_MEM_RETRIEVE_REQ, "
2308 "for handle %#x.\n",
2309 to_locked.vm->id, handle);
2310 ret = ffa_error(FFA_INVALID_PARAMETERS);
2311 goto out;
2312 }
2313
2314 if (share_state->retrieved_fragment_count[receiver_index] !=
2315 0U) {
2316 dlog_verbose(
2317 "Memory with handle %#x already retrieved.\n",
2318 handle);
2319 ret = ffa_error(FFA_DENIED);
2320 goto out;
2321 }
2322
2323 /*
2324 * Check that the transaction type expected by the receiver is
2325 * correct, if it has been specified.
2326 */
2327 if (transaction_type !=
2328 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
2329 transaction_type !=
2330 (memory_region->flags &
2331 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
2332 dlog_verbose(
2333 "Incorrect transaction type %#x for "
2334 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle "
2335 "%#x.\n",
2336 transaction_type,
2337 memory_region->flags &
2338 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
2339 handle);
2340 ret = ffa_error(FFA_INVALID_PARAMETERS);
2341 goto out;
2342 }
2343
2344 if (retrieve_request->sender != memory_region->sender) {
2345 dlog_verbose(
2346 "Incorrect sender ID %d for "
2347 "FFA_MEM_RETRIEVE_REQ, "
2348 "expected %d for handle %#x.\n",
2349 retrieve_request->sender, memory_region->sender,
2350 handle);
2351 ret = ffa_error(FFA_DENIED);
2352 goto out;
2353 }
2354
2355 if (retrieve_request->tag != memory_region->tag) {
2356 dlog_verbose(
2357 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, "
2358 "expected "
2359 "%d for handle %#x.\n",
2360 retrieve_request->tag, memory_region->tag,
2361 handle);
2362 ret = ffa_error(FFA_INVALID_PARAMETERS);
2363 goto out;
2364 }
2365
2366 if ((retrieve_request->flags &
2367 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
2368 dlog_verbose(
2369 "Retriever specified 'address range alignment "
2370 "hint' not supported.\n");
2371 ret = ffa_error(FFA_INVALID_PARAMETERS);
2372 goto out;
2373 }
2374
2375 if ((retrieve_request->flags &
2376 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
2377 dlog_verbose(
2378 "Bits 8-5 must be zero in memory region's "
2379 "flags (address range alignment hint not "
2380 "supported).\n");
2381 ret = ffa_error(FFA_INVALID_PARAMETERS);
2382 goto out;
2383 }
2384
2385 if ((retrieve_request->flags & ~0x7FF) != 0U) {
2386 dlog_verbose(
2387 "Bits 31-10 must be zero in memory region's "
2388 "flags.\n");
2389 ret = ffa_error(FFA_INVALID_PARAMETERS);
2390 goto out;
2391 }
2392
2393 if (share_state->share_func == FFA_MEM_SHARE_32 &&
2394 (retrieve_request->flags &
2395 (FFA_MEMORY_REGION_FLAG_CLEAR |
2396 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
2397 dlog_verbose(
2398 "Memory Share operation can't clean after "
2399 "relinquish "
2400 "memory region.\n");
2401 ret = ffa_error(FFA_INVALID_PARAMETERS);
2402 goto out;
2403 }
2404
2405 /*
2406 * If the borrower needs the memory to be cleared before mapping
2407 * to its address space, the sender should have set the flag
2408 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
2409 * FFA_DENIED.
2410 */
2411 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2412 0U &&
2413 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) ==
2414 0U) {
2415 dlog_verbose(
2416 "Borrower needs memory cleared. Sender needs "
2417 "to set "
2418 "flag for clearing memory.\n");
2419 ret = ffa_error(FFA_DENIED);
2420 goto out;
2421 }
2422
2423 ret = ffa_memory_retrieve_validate_memory_access_list(
2424 memory_region, retrieve_request, receiver_id,
2425 &permissions);
J-Alves614d9f42022-06-28 14:03:10 +01002426 if (ret.func != FFA_SUCCESS_32) {
2427 goto out;
2428 }
Federico Recanatia98603a2021-12-20 18:04:03 +01002429
J-Alvesa9cd7e32022-07-01 13:49:33 +01002430 if (ffa_get_memory_type_attr(retrieve_request->attributes) !=
2431 FFA_MEMORY_NOT_SPECIFIED_MEM) {
2432 /*
2433 * Ensure receiver's attributes are compatible with how
2434 * Hafnium maps memory: Normal Memory, Inner shareable,
2435 * Write-Back Read-Allocate Write-Allocate Cacheable.
2436 */
2437 ret = ffa_memory_attributes_validate(
2438 retrieve_request->attributes);
2439 if (ret.func != FFA_SUCCESS_32) {
2440 goto out;
2441 }
2442 }
2443
2444 memory_to_attributes = ffa_memory_permissions_to_mode(
2445 permissions, share_state->sender_orig_mode);
2446 ret = ffa_retrieve_check_update(
2447 to_locked, memory_region->sender,
2448 share_state->fragments,
2449 share_state->fragment_constituent_counts,
2450 share_state->fragment_count, memory_to_attributes,
2451 share_state->share_func, false, page_pool);
2452
2453 if (ret.func != FFA_SUCCESS_32) {
2454 goto out;
2455 }
2456
2457 share_state->retrieved_fragment_count[receiver_index] = 1;
2458 is_send_complete =
2459 share_state->retrieved_fragment_count[receiver_index] ==
2460 share_state->fragment_count;
2461 } else {
2462 if (share_state->hypervisor_fragment_count != 0U) {
2463 dlog_verbose(
2464 "Memory with handle %#x already "
2465 "retrieved by "
2466 "the hypervisor.\n",
2467 handle);
2468 ret = ffa_error(FFA_DENIED);
2469 goto out;
2470 }
2471
2472 share_state->hypervisor_fragment_count = 1;
2473
2474 ffa_memory_retrieve_complete_from_hyp(share_state);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002475 }
2476
2477 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002478 * Copy response to RX buffer of caller and deliver the message.
2479 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002480 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002481 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002482 composite = ffa_memory_region_get_composite(memory_region, 0);
2483 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002484 * Constituents which we received in the first fragment should
2485 * always fit in the first fragment we are sending, because the
2486 * header is the same size in both cases and we have a fixed
2487 * message buffer size. So `ffa_retrieved_memory_region_init`
2488 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01002489 */
2490 CHECK(ffa_retrieved_memory_region_init(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002491 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2492 memory_region->sender, memory_region->attributes,
J-Alvesa9cd7e32022-07-01 13:49:33 +01002493 memory_region->flags, handle, receiver_id, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +01002494 composite->page_count, composite->constituent_count,
2495 share_state->fragments[0],
2496 share_state->fragment_constituent_counts[0], &total_length,
2497 &fragment_length));
2498 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002499 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002500 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002501 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2502
J-Alvesa9cd7e32022-07-01 13:49:33 +01002503 if (is_send_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002504 ffa_memory_retrieve_complete(share_states, share_state,
2505 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002506 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002507 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002508 .arg1 = total_length,
2509 .arg2 = fragment_length};
2510
2511out:
2512 share_states_unlock(&share_states);
2513 dump_share_states();
2514 return ret;
2515}
2516
2517struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2518 ffa_memory_handle_t handle,
2519 uint32_t fragment_offset,
2520 struct mpool *page_pool)
2521{
2522 struct ffa_memory_region *memory_region;
2523 struct share_states_locked share_states;
2524 struct ffa_memory_share_state *share_state;
2525 struct ffa_value ret;
2526 uint32_t fragment_index;
2527 uint32_t retrieved_constituents_count;
2528 uint32_t i;
2529 uint32_t expected_fragment_offset;
2530 uint32_t remaining_constituent_count;
2531 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01002532 uint32_t receiver_index;
Andrew Walbranca808b12020-05-15 17:22:28 +01002533
2534 dump_share_states();
2535
2536 share_states = share_states_lock();
2537 if (!get_share_state(share_states, handle, &share_state)) {
2538 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2539 handle);
2540 ret = ffa_error(FFA_INVALID_PARAMETERS);
2541 goto out;
2542 }
2543
2544 memory_region = share_state->memory_region;
2545 CHECK(memory_region != NULL);
2546
J-Alvesc7484f12022-05-13 12:41:14 +01002547 receiver_index =
2548 ffa_memory_region_get_receiver(memory_region, to_locked.vm->id);
2549
2550 if (receiver_index == memory_region->receiver_count) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002551 dlog_verbose(
J-Alvesc7484f12022-05-13 12:41:14 +01002552 "Caller of FFA_MEM_FRAG_RX (%x) is not a borrower to "
2553 "memory sharing transaction (%x)\n",
2554 to_locked.vm->id, handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01002555 ret = ffa_error(FFA_INVALID_PARAMETERS);
2556 goto out;
2557 }
2558
2559 if (!share_state->sending_complete) {
2560 dlog_verbose(
2561 "Memory with handle %#x not fully sent, can't "
2562 "retrieve.\n",
2563 handle);
2564 ret = ffa_error(FFA_INVALID_PARAMETERS);
2565 goto out;
2566 }
2567
J-Alvesc7484f12022-05-13 12:41:14 +01002568 if (share_state->retrieved_fragment_count[receiver_index] == 0 ||
2569 share_state->retrieved_fragment_count[receiver_index] >=
Andrew Walbranca808b12020-05-15 17:22:28 +01002570 share_state->fragment_count) {
2571 dlog_verbose(
2572 "Retrieval of memory with handle %#x not yet started "
2573 "or already completed (%d/%d fragments retrieved).\n",
J-Alvesc7484f12022-05-13 12:41:14 +01002574 handle,
2575 share_state->retrieved_fragment_count[receiver_index],
Andrew Walbranca808b12020-05-15 17:22:28 +01002576 share_state->fragment_count);
2577 ret = ffa_error(FFA_INVALID_PARAMETERS);
2578 goto out;
2579 }
2580
J-Alvesc7484f12022-05-13 12:41:14 +01002581 fragment_index = share_state->retrieved_fragment_count[receiver_index];
Andrew Walbranca808b12020-05-15 17:22:28 +01002582
2583 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002584 * Check that the given fragment offset is correct by counting
2585 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01002586 */
2587 retrieved_constituents_count = 0;
2588 for (i = 0; i < fragment_index; ++i) {
2589 retrieved_constituents_count +=
2590 share_state->fragment_constituent_counts[i];
2591 }
J-Alvesc7484f12022-05-13 12:41:14 +01002592
2593 CHECK(memory_region->receiver_count > 0);
2594
Andrew Walbranca808b12020-05-15 17:22:28 +01002595 expected_fragment_offset =
J-Alvesc7484f12022-05-13 12:41:14 +01002596 ffa_composite_constituent_offset(memory_region,
2597 receiver_index) +
Andrew Walbranca808b12020-05-15 17:22:28 +01002598 retrieved_constituents_count *
J-Alvesc7484f12022-05-13 12:41:14 +01002599 sizeof(struct ffa_memory_region_constituent) -
2600 sizeof(struct ffa_memory_access) *
2601 (memory_region->receiver_count - 1);
Andrew Walbranca808b12020-05-15 17:22:28 +01002602 if (fragment_offset != expected_fragment_offset) {
2603 dlog_verbose("Fragment offset was %d but expected %d.\n",
2604 fragment_offset, expected_fragment_offset);
2605 ret = ffa_error(FFA_INVALID_PARAMETERS);
2606 goto out;
2607 }
2608
2609 remaining_constituent_count = ffa_memory_fragment_init(
2610 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2611 share_state->fragments[fragment_index],
2612 share_state->fragment_constituent_counts[fragment_index],
2613 &fragment_length);
2614 CHECK(remaining_constituent_count == 0);
2615 to_locked.vm->mailbox.recv_size = fragment_length;
2616 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2617 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
2618 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
J-Alvesc7484f12022-05-13 12:41:14 +01002619 share_state->retrieved_fragment_count[receiver_index]++;
2620 if (share_state->retrieved_fragment_count[receiver_index] ==
Andrew Walbranca808b12020-05-15 17:22:28 +01002621 share_state->fragment_count) {
2622 ffa_memory_retrieve_complete(share_states, share_state,
2623 page_pool);
2624 }
2625
2626 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2627 .arg1 = (uint32_t)handle,
2628 .arg2 = (uint32_t)(handle >> 32),
2629 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002630
2631out:
2632 share_states_unlock(&share_states);
2633 dump_share_states();
2634 return ret;
2635}
2636
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002637struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002638 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002639 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002640{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002641 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002642 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002643 struct ffa_memory_share_state *share_state;
2644 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002645 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002646 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01002647 uint32_t receiver_index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002648
Andrew Walbrana65a1322020-04-06 19:32:32 +01002649 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002650 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002651 "Stream endpoints not supported (got %d "
2652 "endpoints on "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002653 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002654 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002655 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002656 }
2657
Andrew Walbrana65a1322020-04-06 19:32:32 +01002658 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002659 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002660 "VM ID %d in relinquish message doesn't match "
2661 "calling "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002662 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002663 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002664 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002665 }
2666
2667 dump_share_states();
2668
2669 share_states = share_states_lock();
2670 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002671 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002672 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002673 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002674 goto out;
2675 }
2676
Andrew Walbranca808b12020-05-15 17:22:28 +01002677 if (!share_state->sending_complete) {
2678 dlog_verbose(
2679 "Memory with handle %#x not fully sent, can't "
2680 "relinquish.\n",
2681 handle);
2682 ret = ffa_error(FFA_INVALID_PARAMETERS);
2683 goto out;
2684 }
2685
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002686 memory_region = share_state->memory_region;
2687 CHECK(memory_region != NULL);
2688
J-Alves8eb19162022-04-28 10:56:48 +01002689 receiver_index = ffa_memory_region_get_receiver(memory_region,
2690 from_locked.vm->id);
2691
2692 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002693 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002694 "VM ID %d tried to relinquish memory region "
2695 "with "
J-Alves8eb19162022-04-28 10:56:48 +01002696 "handle %#x and it is not a valid borrower.\n",
2697 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002698 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002699 goto out;
2700 }
2701
J-Alves8eb19162022-04-28 10:56:48 +01002702 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01002703 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002704 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002705 "Memory with handle %#x not yet fully "
2706 "retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01002707 "receiver %x can't relinquish.\n",
2708 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002709 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002710 goto out;
2711 }
2712
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002713 clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002714
2715 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002716 * Clear is not allowed for memory that was shared, as the
2717 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002718 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002719 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002720 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002721 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002722 goto out;
2723 }
2724
Andrew Walbranca808b12020-05-15 17:22:28 +01002725 ret = ffa_relinquish_check_update(
2726 from_locked, share_state->fragments,
2727 share_state->fragment_constituent_counts,
2728 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002729
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002730 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002731 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002732 * Mark memory handle as not retrieved, so it can be
2733 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002734 */
J-Alves8eb19162022-04-28 10:56:48 +01002735 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002736 }
2737
2738out:
2739 share_states_unlock(&share_states);
2740 dump_share_states();
2741 return ret;
2742}
2743
2744/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01002745 * Validates that the reclaim transition is allowed for the given
2746 * handle, updates the page table of the reclaiming VM, and frees the
2747 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002748 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002749struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002750 ffa_memory_handle_t handle,
2751 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002752 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002753{
2754 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002755 struct ffa_memory_share_state *share_state;
2756 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002757 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002758
2759 dump_share_states();
2760
2761 share_states = share_states_lock();
2762 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002763 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002764 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002765 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002766 goto out;
2767 }
2768
2769 memory_region = share_state->memory_region;
2770 CHECK(memory_region != NULL);
2771
J-Alvesa9cd7e32022-07-01 13:49:33 +01002772 if (vm_id_is_current_world(to_locked.vm->id) &&
2773 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002774 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002775 "VM %#x attempted to reclaim memory handle %#x "
2776 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002777 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002778 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002779 goto out;
2780 }
2781
Andrew Walbranca808b12020-05-15 17:22:28 +01002782 if (!share_state->sending_complete) {
2783 dlog_verbose(
2784 "Memory with handle %#x not fully sent, can't "
2785 "reclaim.\n",
2786 handle);
2787 ret = ffa_error(FFA_INVALID_PARAMETERS);
2788 goto out;
2789 }
2790
J-Alves752236c2022-04-28 11:07:47 +01002791 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
2792 if (share_state->retrieved_fragment_count[i] != 0) {
2793 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002794 "Tried to reclaim memory handle %#x "
2795 "that has "
2796 "not been relinquished by all "
2797 "borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01002798 handle,
2799 memory_region->receivers[i]
2800 .receiver_permissions.receiver);
2801 ret = ffa_error(FFA_DENIED);
2802 goto out;
2803 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002804 }
2805
Andrew Walbranca808b12020-05-15 17:22:28 +01002806 ret = ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00002807 to_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01002808 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00002809 share_state->fragment_count, share_state->sender_orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01002810 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002811
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002812 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002813 share_state_free(share_states, share_state, page_pool);
J-Alvesa9cd7e32022-07-01 13:49:33 +01002814 dlog_verbose(
2815 "Freed share state after successful "
2816 "reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002817 }
2818
2819out:
2820 share_states_unlock(&share_states);
2821 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002822}
Andrew Walbran290b0c92020-02-03 16:37:14 +00002823
2824/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01002825 * Validates that the reclaim transition is allowed for the memory
2826 * region with the given handle which was previously shared with the
2827 * other world. Tells the other world to mark it as reclaimed, and
2828 * updates the page table of the reclaiming VM.
Andrew Walbranca808b12020-05-15 17:22:28 +01002829 *
J-Alvesa9cd7e32022-07-01 13:49:33 +01002830 * To do this information about the memory region is first fetched from
2831 * the other world.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002832 */
J-Alves8505a8a2022-06-15 18:10:18 +01002833struct ffa_value ffa_memory_other_world_reclaim(struct vm_locked to_locked,
2834 struct vm_locked from_locked,
2835 ffa_memory_handle_t handle,
2836 ffa_memory_region_flags_t flags,
2837 struct mpool *page_pool)
Andrew Walbran290b0c92020-02-03 16:37:14 +00002838{
Andrew Walbranca808b12020-05-15 17:22:28 +01002839 uint32_t request_length = ffa_memory_lender_retrieve_request_init(
2840 from_locked.vm->mailbox.recv, handle, to_locked.vm->id);
J-Alves8505a8a2022-06-15 18:10:18 +01002841 struct ffa_value other_world_ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002842 uint32_t length;
2843 uint32_t fragment_length;
2844 uint32_t fragment_offset;
2845 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002846 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01002847 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
2848
2849 CHECK(request_length <= HF_MAILBOX_SIZE);
J-Alves8505a8a2022-06-15 18:10:18 +01002850 CHECK(from_locked.vm->id == HF_OTHER_WORLD_ID);
Andrew Walbranca808b12020-05-15 17:22:28 +01002851
J-Alves8505a8a2022-06-15 18:10:18 +01002852 /* Retrieve memory region information from the other world. */
2853 other_world_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01002854 (struct ffa_value){.func = FFA_MEM_RETRIEVE_REQ_32,
2855 .arg1 = request_length,
2856 .arg2 = request_length});
J-Alves8505a8a2022-06-15 18:10:18 +01002857 if (other_world_ret.func == FFA_ERROR_32) {
2858 dlog_verbose("Got error %d from EL3.\n", other_world_ret.arg2);
2859 return other_world_ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002860 }
J-Alves8505a8a2022-06-15 18:10:18 +01002861 if (other_world_ret.func != FFA_MEM_RETRIEVE_RESP_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002862 dlog_verbose(
2863 "Got %#x from EL3, expected FFA_MEM_RETRIEVE_RESP.\n",
J-Alves8505a8a2022-06-15 18:10:18 +01002864 other_world_ret.func);
Andrew Walbranca808b12020-05-15 17:22:28 +01002865 return ffa_error(FFA_INVALID_PARAMETERS);
2866 }
2867
J-Alves8505a8a2022-06-15 18:10:18 +01002868 length = other_world_ret.arg1;
2869 fragment_length = other_world_ret.arg2;
Andrew Walbranca808b12020-05-15 17:22:28 +01002870
2871 if (fragment_length > HF_MAILBOX_SIZE || fragment_length > length ||
J-Alves8505a8a2022-06-15 18:10:18 +01002872 length > sizeof(other_world_retrieve_buffer)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002873 dlog_verbose("Invalid fragment length %d/%d (max %d/%d).\n",
2874 fragment_length, length, HF_MAILBOX_SIZE,
J-Alves8505a8a2022-06-15 18:10:18 +01002875 sizeof(other_world_retrieve_buffer));
Andrew Walbranca808b12020-05-15 17:22:28 +01002876 return ffa_error(FFA_INVALID_PARAMETERS);
2877 }
2878
2879 /*
2880 * Copy the first fragment of the memory region descriptor to an
2881 * internal buffer.
2882 */
J-Alves8505a8a2022-06-15 18:10:18 +01002883 memcpy_s(other_world_retrieve_buffer,
2884 sizeof(other_world_retrieve_buffer),
Andrew Walbranca808b12020-05-15 17:22:28 +01002885 from_locked.vm->mailbox.send, fragment_length);
2886
2887 /* Fetch the remaining fragments into the same buffer. */
2888 fragment_offset = fragment_length;
2889 while (fragment_offset < length) {
J-Alves8505a8a2022-06-15 18:10:18 +01002890 other_world_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01002891 (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2892 .arg1 = (uint32_t)handle,
2893 .arg2 = (uint32_t)(handle >> 32),
2894 .arg3 = fragment_offset});
J-Alves8505a8a2022-06-15 18:10:18 +01002895 if (other_world_ret.func != FFA_MEM_FRAG_TX_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002896 dlog_verbose(
J-Alves8505a8a2022-06-15 18:10:18 +01002897 "Got %#x (%d) from other world in response to "
Andrew Walbranca808b12020-05-15 17:22:28 +01002898 "FFA_MEM_FRAG_RX, expected FFA_MEM_FRAG_TX.\n",
J-Alves8505a8a2022-06-15 18:10:18 +01002899 other_world_ret.func, other_world_ret.arg2);
2900 return other_world_ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002901 }
J-Alves8505a8a2022-06-15 18:10:18 +01002902 if (ffa_frag_handle(other_world_ret) != handle) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002903 dlog_verbose(
2904 "Got FFA_MEM_FRAG_TX for unexpected handle %#x "
2905 "in response to FFA_MEM_FRAG_RX for handle "
2906 "%#x.\n",
J-Alves8505a8a2022-06-15 18:10:18 +01002907 ffa_frag_handle(other_world_ret), handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01002908 return ffa_error(FFA_INVALID_PARAMETERS);
2909 }
J-Alves8505a8a2022-06-15 18:10:18 +01002910 if (ffa_frag_sender(other_world_ret) != 0) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002911 dlog_verbose(
2912 "Got FFA_MEM_FRAG_TX with unexpected sender %d "
2913 "(expected 0).\n",
J-Alves8505a8a2022-06-15 18:10:18 +01002914 ffa_frag_sender(other_world_ret));
Andrew Walbranca808b12020-05-15 17:22:28 +01002915 return ffa_error(FFA_INVALID_PARAMETERS);
2916 }
J-Alves8505a8a2022-06-15 18:10:18 +01002917 fragment_length = other_world_ret.arg3;
Andrew Walbranca808b12020-05-15 17:22:28 +01002918 if (fragment_length > HF_MAILBOX_SIZE ||
2919 fragment_offset + fragment_length > length) {
2920 dlog_verbose(
2921 "Invalid fragment length %d at offset %d (max "
2922 "%d).\n",
2923 fragment_length, fragment_offset,
2924 HF_MAILBOX_SIZE);
2925 return ffa_error(FFA_INVALID_PARAMETERS);
2926 }
J-Alves8505a8a2022-06-15 18:10:18 +01002927 memcpy_s(other_world_retrieve_buffer + fragment_offset,
2928 sizeof(other_world_retrieve_buffer) - fragment_offset,
Andrew Walbranca808b12020-05-15 17:22:28 +01002929 from_locked.vm->mailbox.send, fragment_length);
2930
2931 fragment_offset += fragment_length;
2932 }
2933
J-Alves8505a8a2022-06-15 18:10:18 +01002934 memory_region = (struct ffa_memory_region *)other_world_retrieve_buffer;
Andrew Walbran290b0c92020-02-03 16:37:14 +00002935
2936 if (memory_region->receiver_count != 1) {
2937 /* Only one receiver supported by Hafnium for now. */
2938 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002939 "Multiple recipients not supported (got %d, "
2940 "expected 1).\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00002941 memory_region->receiver_count);
Andrew Walbranca808b12020-05-15 17:22:28 +01002942 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002943 }
2944
2945 if (memory_region->handle != handle) {
2946 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002947 "Got memory region handle %#x from other world "
2948 "but requested handle %#x.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00002949 memory_region->handle, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002950 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002951 }
2952
2953 /* The original sender must match the caller. */
2954 if (to_locked.vm->id != memory_region->sender) {
2955 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01002956 "VM %#x attempted to reclaim memory handle %#x "
2957 "originally sent by VM %#x.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00002958 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002959 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002960 }
2961
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002962 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002963
2964 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002965 * Validate that the reclaim transition is allowed for the given
2966 * memory region, forward the request to the other world and
2967 * then map the memory back into the caller's stage-2 page
2968 * table.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002969 */
J-Alves8505a8a2022-06-15 18:10:18 +01002970 return ffa_other_world_reclaim_check_update(
Andrew Walbran996d1d12020-05-27 14:08:43 +01002971 to_locked, handle, composite->constituents,
Andrew Walbranca808b12020-05-15 17:22:28 +01002972 composite->constituent_count, memory_to_attributes,
2973 flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002974}