blob: 16bcb828c97be3fb5d8250d9be9e17ba564b0d67 [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"
Andrew Walbran475c1452020-02-07 13:22:22 +000020#include "hf/mpool.h"
Jose Marinho75509b42019-04-09 09:34:59 +010021#include "hf/std.h"
Andrew Scull3c257452019-11-26 13:32:50 +000022#include "hf/vm.h"
Jose Marinho75509b42019-04-09 09:34:59 +010023
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000024/**
25 * The maximum number of memory sharing handles which may be active at once. A
26 * DONATE handle is active from when it is sent to when it is retrieved; a SHARE
27 * or LEND handle is active from when it is sent to when it is reclaimed.
28 */
29#define MAX_MEM_SHARES 100
30
Andrew Walbranca808b12020-05-15 17:22:28 +010031/**
32 * The maximum number of fragments into which a memory sharing message may be
33 * broken.
34 */
35#define MAX_FRAGMENTS 20
36
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010037static_assert(sizeof(struct ffa_memory_region_constituent) % 16 == 0,
38 "struct ffa_memory_region_constituent must be a multiple of 16 "
Andrew Walbranc34c7b22020-02-28 11:16:59 +000039 "bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010040static_assert(sizeof(struct ffa_composite_memory_region) % 16 == 0,
41 "struct ffa_composite_memory_region must be a multiple of 16 "
Andrew Walbranc34c7b22020-02-28 11:16:59 +000042 "bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010043static_assert(sizeof(struct ffa_memory_region_attributes) == 4,
Andrew Walbran41890ff2020-09-23 15:09:39 +010044 "struct ffa_memory_region_attributes must be 4 bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010045static_assert(sizeof(struct ffa_memory_access) % 16 == 0,
46 "struct ffa_memory_access must be a multiple of 16 bytes long.");
47static_assert(sizeof(struct ffa_memory_region) % 16 == 0,
48 "struct ffa_memory_region must be a multiple of 16 bytes long.");
49static_assert(sizeof(struct ffa_mem_relinquish) % 16 == 0,
50 "struct ffa_mem_relinquish must be a multiple of 16 "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000051 "bytes long.");
Andrew Walbranc34c7b22020-02-28 11:16:59 +000052
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010053struct ffa_memory_share_state {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000054 /**
55 * The memory region being shared, or NULL if this share state is
56 * unallocated.
57 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010058 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000059
Andrew Walbranca808b12020-05-15 17:22:28 +010060 struct ffa_memory_region_constituent *fragments[MAX_FRAGMENTS];
61
62 /** The number of constituents in each fragment. */
63 uint32_t fragment_constituent_counts[MAX_FRAGMENTS];
64
65 /**
66 * The number of valid elements in the `fragments` and
67 * `fragment_constituent_counts` arrays.
68 */
69 uint32_t fragment_count;
70
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000071 /**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010072 * The FF-A function used for sharing the memory. Must be one of
73 * FFA_MEM_DONATE_32, FFA_MEM_LEND_32 or FFA_MEM_SHARE_32 if the
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000074 * share state is allocated, or 0.
75 */
76 uint32_t share_func;
77
78 /**
J-Alves2a0d2882020-10-29 14:49:50 +000079 * The sender's original mode before invoking the FF-A function for
80 * sharing the memory.
81 * This is used to reset the original configuration when sender invokes
82 * FFA_MEM_RECLAIM_32.
83 */
84 uint32_t sender_orig_mode;
85
86 /**
Andrew Walbranca808b12020-05-15 17:22:28 +010087 * True if all the fragments of this sharing request have been sent and
88 * Hafnium has updated the sender page table accordingly.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000089 */
Andrew Walbranca808b12020-05-15 17:22:28 +010090 bool sending_complete;
91
92 /**
93 * How many fragments of the memory region each recipient has retrieved
94 * so far. The order of this array matches the order of the endpoint
95 * memory access descriptors in the memory region descriptor. Any
96 * entries beyond the receiver_count will always be 0.
97 */
98 uint32_t retrieved_fragment_count[MAX_MEM_SHARE_RECIPIENTS];
J-Alvesa9cd7e32022-07-01 13:49:33 +010099
100 /**
101 * Field for the SPMC to keep track of how many fragments of the memory
102 * region the hypervisor has managed to retrieve, using a
103 * `hypervisor retrieve request`, as defined by FF-A v1.1 EAC0
104 * specification.
105 */
106 uint32_t hypervisor_fragment_count;
Andrew Walbran475c1452020-02-07 13:22:22 +0000107};
108
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000109/**
110 * Encapsulates the set of share states while the `share_states_lock` is held.
111 */
112struct share_states_locked {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100113 struct ffa_memory_share_state *share_states;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000114};
115
116/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100117 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000118 * by this lock.
119 */
120static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100121static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000122
123/**
J-Alves8505a8a2022-06-15 18:10:18 +0100124 * Buffer for retrieving memory region information from the other world for when
125 * a region is reclaimed by a VM. Access to this buffer must be guarded by the
126 * VM lock of the other world VM.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000127 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100128alignas(PAGE_SIZE) static uint8_t
J-Alves8505a8a2022-06-15 18:10:18 +0100129 other_world_retrieve_buffer[HF_MAILBOX_SIZE * MAX_FRAGMENTS];
Andrew Walbranca808b12020-05-15 17:22:28 +0100130
131/**
J-Alves917d2f22020-10-30 18:39:30 +0000132 * Extracts the index from a memory handle allocated by Hafnium's current world.
133 */
134uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle)
135{
136 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
137}
138
139/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100140 * Initialises the next available `struct ffa_memory_share_state` and sets
141 * `share_state_ret` to a pointer to it. If `handle` is
142 * `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle, otherwise
143 * uses the provided handle which is assumed to be globally unique.
144 *
145 * Returns true on success or false if none are available.
146 */
147static bool allocate_share_state(
148 struct share_states_locked share_states, uint32_t share_func,
149 struct ffa_memory_region *memory_region, uint32_t fragment_length,
150 ffa_memory_handle_t handle,
151 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000152{
Andrew Walbrana65a1322020-04-06 19:32:32 +0100153 uint64_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000154
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000155 assert(share_states.share_states != NULL);
156 assert(memory_region != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000157
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000158 for (i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100159 if (share_states.share_states[i].share_func == 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000160 uint32_t j;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100161 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +0100162 &share_states.share_states[i];
163 struct ffa_composite_memory_region *composite =
164 ffa_memory_region_get_composite(memory_region,
165 0);
166
167 if (handle == FFA_MEMORY_HANDLE_INVALID) {
J-Alvesee68c542020-10-29 17:48:20 +0000168 memory_region->handle =
Olivier Deprez55a189e2021-06-09 15:45:27 +0200169 plat_ffa_memory_handle_make(i);
Andrew Walbranca808b12020-05-15 17:22:28 +0100170 } else {
J-Alvesee68c542020-10-29 17:48:20 +0000171 memory_region->handle = handle;
Andrew Walbranca808b12020-05-15 17:22:28 +0100172 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000173 allocated_state->share_func = share_func;
174 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +0100175 allocated_state->fragment_count = 1;
176 allocated_state->fragments[0] = composite->constituents;
177 allocated_state->fragment_constituent_counts[0] =
178 (fragment_length -
179 ffa_composite_constituent_offset(memory_region,
180 0)) /
181 sizeof(struct ffa_memory_region_constituent);
182 allocated_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000183 for (j = 0; j < MAX_MEM_SHARE_RECIPIENTS; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100184 allocated_state->retrieved_fragment_count[j] =
185 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000186 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100187 if (share_state_ret != NULL) {
188 *share_state_ret = allocated_state;
189 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000190 return true;
191 }
192 }
193
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000194 return false;
195}
196
197/** Locks the share states lock. */
198struct share_states_locked share_states_lock(void)
199{
200 sl_lock(&share_states_lock_instance);
201
202 return (struct share_states_locked){.share_states = share_states};
203}
204
205/** Unlocks the share states lock. */
206static void share_states_unlock(struct share_states_locked *share_states)
207{
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000208 assert(share_states->share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000209 share_states->share_states = NULL;
210 sl_unlock(&share_states_lock_instance);
211}
212
213/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100214 * If the given handle is a valid handle for an allocated share state then
215 * initialises `share_state_ret` to point to the share state and returns true.
216 * Otherwise returns false.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000217 */
218static bool get_share_state(struct share_states_locked share_states,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100219 ffa_memory_handle_t handle,
220 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000221{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100222 struct ffa_memory_share_state *share_state;
J-Alves917d2f22020-10-30 18:39:30 +0000223 uint64_t index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000224
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000225 assert(share_states.share_states != NULL);
226 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100227
228 /*
229 * First look for a share_state allocated by us, in which case the
230 * handle is based on the index.
231 */
Olivier Deprez55a189e2021-06-09 15:45:27 +0200232 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
J-Alves917d2f22020-10-30 18:39:30 +0000233 index = ffa_memory_handle_get_index(handle);
Andrew Walbranca808b12020-05-15 17:22:28 +0100234 if (index < MAX_MEM_SHARES) {
235 share_state = &share_states.share_states[index];
236 if (share_state->share_func != 0) {
237 *share_state_ret = share_state;
238 return true;
239 }
240 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000241 }
242
Andrew Walbranca808b12020-05-15 17:22:28 +0100243 /* Fall back to a linear scan. */
244 for (index = 0; index < MAX_MEM_SHARES; ++index) {
245 share_state = &share_states.share_states[index];
J-Alvesee68c542020-10-29 17:48:20 +0000246 if (share_state->memory_region != NULL &&
247 share_state->memory_region->handle == handle &&
Andrew Walbranca808b12020-05-15 17:22:28 +0100248 share_state->share_func != 0) {
249 *share_state_ret = share_state;
250 return true;
251 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000252 }
253
Andrew Walbranca808b12020-05-15 17:22:28 +0100254 return false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000255}
256
257/** Marks a share state as unallocated. */
258static void share_state_free(struct share_states_locked share_states,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100259 struct ffa_memory_share_state *share_state,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000260 struct mpool *page_pool)
261{
Andrew Walbranca808b12020-05-15 17:22:28 +0100262 uint32_t i;
263
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000264 assert(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000265 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100266 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000267 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100268 /*
269 * First fragment is part of the same page as the `memory_region`, so it
270 * doesn't need to be freed separately.
271 */
272 share_state->fragments[0] = NULL;
273 share_state->fragment_constituent_counts[0] = 0;
274 for (i = 1; i < share_state->fragment_count; ++i) {
275 mpool_free(page_pool, share_state->fragments[i]);
276 share_state->fragments[i] = NULL;
277 share_state->fragment_constituent_counts[i] = 0;
278 }
279 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000280 share_state->memory_region = NULL;
J-Alvesa9cd7e32022-07-01 13:49:33 +0100281 share_state->hypervisor_fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000282}
283
Andrew Walbranca808b12020-05-15 17:22:28 +0100284/** Checks whether the given share state has been fully sent. */
285static bool share_state_sending_complete(
286 struct share_states_locked share_states,
287 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000288{
Andrew Walbranca808b12020-05-15 17:22:28 +0100289 struct ffa_composite_memory_region *composite;
290 uint32_t expected_constituent_count;
291 uint32_t fragment_constituent_count_total = 0;
292 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000293
Andrew Walbranca808b12020-05-15 17:22:28 +0100294 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000295 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100296
297 /*
298 * Share state must already be valid, or it's not possible to get hold
299 * of it.
300 */
301 CHECK(share_state->memory_region != NULL &&
302 share_state->share_func != 0);
303
304 composite =
305 ffa_memory_region_get_composite(share_state->memory_region, 0);
306 expected_constituent_count = composite->constituent_count;
307 for (i = 0; i < share_state->fragment_count; ++i) {
308 fragment_constituent_count_total +=
309 share_state->fragment_constituent_counts[i];
310 }
311 dlog_verbose(
312 "Checking completion: constituent count %d/%d from %d "
313 "fragments.\n",
314 fragment_constituent_count_total, expected_constituent_count,
315 share_state->fragment_count);
316
317 return fragment_constituent_count_total == expected_constituent_count;
318}
319
320/**
321 * Calculates the offset of the next fragment expected for the given share
322 * state.
323 */
324static uint32_t share_state_next_fragment_offset(
325 struct share_states_locked share_states,
326 struct ffa_memory_share_state *share_state)
327{
328 uint32_t next_fragment_offset;
329 uint32_t i;
330
331 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000332 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100333
334 next_fragment_offset =
335 ffa_composite_constituent_offset(share_state->memory_region, 0);
336 for (i = 0; i < share_state->fragment_count; ++i) {
337 next_fragment_offset +=
338 share_state->fragment_constituent_counts[i] *
339 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000340 }
341
Andrew Walbranca808b12020-05-15 17:22:28 +0100342 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000343}
344
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100345static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000346{
347 uint32_t i;
348
349 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
350 return;
351 }
352
Olivier Deprez935e1b12020-12-22 18:01:29 +0100353 dlog("from VM %#x, attributes %#x, flags %#x, tag %u, to "
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100354 "%u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100355 "recipients [",
356 memory_region->sender, memory_region->attributes,
Olivier Deprez935e1b12020-12-22 18:01:29 +0100357 memory_region->flags, memory_region->tag,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100358 memory_region->receiver_count);
359 for (i = 0; i < memory_region->receiver_count; ++i) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000360 if (i != 0) {
361 dlog(", ");
362 }
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100363 dlog("VM %#x: %#x (offset %u)",
Andrew Walbrana65a1322020-04-06 19:32:32 +0100364 memory_region->receivers[i].receiver_permissions.receiver,
365 memory_region->receivers[i]
366 .receiver_permissions.permissions,
367 memory_region->receivers[i]
368 .composite_memory_region_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000369 }
370 dlog("]");
371}
372
373static void dump_share_states(void)
374{
375 uint32_t i;
376
377 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
378 return;
379 }
380
381 dlog("Current share states:\n");
382 sl_lock(&share_states_lock_instance);
383 for (i = 0; i < MAX_MEM_SHARES; ++i) {
384 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000385 switch (share_states[i].share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100386 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000387 dlog("SHARE");
388 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100389 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000390 dlog("LEND");
391 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100392 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000393 dlog("DONATE");
394 break;
395 default:
396 dlog("invalid share_func %#x",
397 share_states[i].share_func);
398 }
Olivier Deprez935e1b12020-12-22 18:01:29 +0100399 dlog(" %#x (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000400 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100401 if (share_states[i].sending_complete) {
402 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000403 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100404 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000405 }
J-Alves2a0d2882020-10-29 14:49:50 +0000406 dlog(" with %d fragments, %d retrieved, "
407 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100408 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000409 share_states[i].retrieved_fragment_count[0],
410 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000411 }
412 }
413 sl_unlock(&share_states_lock_instance);
414}
415
Andrew Walbran475c1452020-02-07 13:22:22 +0000416/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100417static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100418 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000419{
420 uint32_t mode = 0;
421
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100422 switch (ffa_get_data_access_attr(permissions)) {
423 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000424 mode = MM_MODE_R;
425 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100426 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000427 mode = MM_MODE_R | MM_MODE_W;
428 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100429 case FFA_DATA_ACCESS_NOT_SPECIFIED:
430 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
431 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100432 case FFA_DATA_ACCESS_RESERVED:
433 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100434 }
435
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100436 switch (ffa_get_instruction_access_attr(permissions)) {
437 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000438 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100439 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100440 mode |= MM_MODE_X;
441 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100442 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
443 mode |= (default_mode & MM_MODE_X);
444 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100445 case FFA_INSTRUCTION_ACCESS_RESERVED:
446 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000447 }
448
449 return mode;
450}
451
Jose Marinho75509b42019-04-09 09:34:59 +0100452/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000453 * Get the current mode in the stage-2 page table of the given vm of all the
454 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100455 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100456 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100457static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000458 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100459 struct ffa_memory_region_constituent **fragments,
460 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100461{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100462 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100463 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100464
Andrew Walbranca808b12020-05-15 17:22:28 +0100465 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100466 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000467 * Fail if there are no constituents. Otherwise we would get an
468 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100469 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100470 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100471 }
472
Andrew Walbranca808b12020-05-15 17:22:28 +0100473 for (i = 0; i < fragment_count; ++i) {
474 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
475 ipaddr_t begin = ipa_init(fragments[i][j].address);
476 size_t size = fragments[i][j].page_count * PAGE_SIZE;
477 ipaddr_t end = ipa_add(begin, size);
478 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100479
Andrew Walbranca808b12020-05-15 17:22:28 +0100480 /* Fail if addresses are not page-aligned. */
481 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
482 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
483 return ffa_error(FFA_INVALID_PARAMETERS);
484 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100485
Andrew Walbranca808b12020-05-15 17:22:28 +0100486 /*
487 * Ensure that this constituent memory range is all
488 * mapped with the same mode.
489 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800490 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100491 return ffa_error(FFA_DENIED);
492 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100493
Andrew Walbranca808b12020-05-15 17:22:28 +0100494 /*
495 * Ensure that all constituents are mapped with the same
496 * mode.
497 */
498 if (i == 0) {
499 *orig_mode = current_mode;
500 } else if (current_mode != *orig_mode) {
501 dlog_verbose(
502 "Expected mode %#x but was %#x for %d "
503 "pages at %#x.\n",
504 *orig_mode, current_mode,
505 fragments[i][j].page_count,
506 ipa_addr(begin));
507 return ffa_error(FFA_DENIED);
508 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100509 }
Jose Marinho75509b42019-04-09 09:34:59 +0100510 }
511
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100512 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000513}
514
515/**
516 * Verify that all pages have the same mode, that the starting mode
517 * constitutes a valid state and obtain the next mode to apply
518 * to the sending VM.
519 *
520 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100521 * 1) FFA_DENIED if a state transition was not found;
522 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100523 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100524 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100525 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100526 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
527 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000528 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100529static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100530 struct vm_locked from, uint32_t share_func,
J-Alves363f5722022-04-25 17:37:37 +0100531 struct ffa_memory_access *receivers, uint32_t receivers_count,
532 uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100533 struct ffa_memory_region_constituent **fragments,
534 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
535 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000536{
537 const uint32_t state_mask =
538 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100539 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000540
Andrew Walbranca808b12020-05-15 17:22:28 +0100541 ret = constituents_get_mode(from, orig_from_mode, fragments,
542 fragment_constituent_counts,
543 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100544 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100545 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100546 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100547 }
548
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000549 /* Ensure the address range is normal memory and not a device. */
550 if (*orig_from_mode & MM_MODE_D) {
551 dlog_verbose("Can't share device memory (mode is %#x).\n",
552 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100553 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000554 }
555
556 /*
557 * Ensure the sender is the owner and has exclusive access to the
558 * memory.
559 */
560 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100561 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100562 }
563
J-Alves363f5722022-04-25 17:37:37 +0100564 assert(receivers != NULL && receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100565
J-Alves363f5722022-04-25 17:37:37 +0100566 for (uint32_t i = 0U; i < receivers_count; i++) {
567 ffa_memory_access_permissions_t permissions =
568 receivers[i].receiver_permissions.permissions;
569 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
570 permissions, *orig_from_mode);
571
572 if ((*orig_from_mode & required_from_mode) !=
573 required_from_mode) {
574 dlog_verbose(
575 "Sender tried to send memory with permissions "
576 "which "
577 "required mode %#x but only had %#x itself.\n",
578 required_from_mode, *orig_from_mode);
579 return ffa_error(FFA_DENIED);
580 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000581 }
582
583 /* Find the appropriate new mode. */
584 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000585 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100586 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000587 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100588 break;
589
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100590 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000591 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100592 break;
593
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100594 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000595 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100596 break;
597
Jose Marinho75509b42019-04-09 09:34:59 +0100598 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100599 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100600 }
601
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100602 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000603}
604
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100605static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000606 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100607 struct ffa_memory_region_constituent **fragments,
608 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
609 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000610{
611 const uint32_t state_mask =
612 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
613 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100614 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000615
Andrew Walbranca808b12020-05-15 17:22:28 +0100616 ret = constituents_get_mode(from, orig_from_mode, fragments,
617 fragment_constituent_counts,
618 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100619 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100620 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000621 }
622
623 /* Ensure the address range is normal memory and not a device. */
624 if (*orig_from_mode & MM_MODE_D) {
625 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
626 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100627 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000628 }
629
630 /*
631 * Ensure the relinquishing VM is not the owner but has access to the
632 * memory.
633 */
634 orig_from_state = *orig_from_mode & state_mask;
635 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
636 dlog_verbose(
637 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100638 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000639 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100640 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000641 }
642
643 /* Find the appropriate new mode. */
644 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
645
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100646 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000647}
648
649/**
650 * Verify that all pages have the same mode, that the starting mode
651 * constitutes a valid state and obtain the next mode to apply
652 * to the retrieving VM.
653 *
654 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100655 * 1) FFA_DENIED if a state transition was not found;
656 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100657 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100658 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100659 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100660 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
661 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000662 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100663static struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000664 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100665 struct ffa_memory_region_constituent **fragments,
666 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
667 uint32_t memory_to_attributes, uint32_t *to_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000668{
669 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100670 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000671
Andrew Walbranca808b12020-05-15 17:22:28 +0100672 ret = constituents_get_mode(to, &orig_to_mode, fragments,
673 fragment_constituent_counts,
674 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100675 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100676 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100677 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000678 }
679
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100680 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000681 /*
682 * If the original ffa memory send call has been processed
683 * successfully, it is expected the orig_to_mode would overlay
684 * with `state_mask`, as a result of the function
685 * `ffa_send_check_transition`.
686 */
Daniel Boulby9133dad2022-04-25 14:38:44 +0100687 assert((orig_to_mode & (MM_MODE_INVALID | MM_MODE_UNOWNED |
688 MM_MODE_SHARED)) != 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000689 } else {
690 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +0100691 * If the retriever is from virtual FF-A instance:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000692 * Ensure the retriever has the expected state. We don't care
693 * about the MM_MODE_SHARED bit; either with or without it set
694 * are both valid representations of the !O-NA state.
695 */
J-Alvesa9cd7e32022-07-01 13:49:33 +0100696 if (vm_id_is_current_world(to.vm->id) &&
697 to.vm->id != HF_PRIMARY_VM_ID &&
698 (orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
699 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100700 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000701 }
702 }
703
704 /* Find the appropriate new mode. */
705 *to_mode = memory_to_attributes;
706 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100707 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000708 *to_mode |= 0;
709 break;
710
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100711 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000712 *to_mode |= MM_MODE_UNOWNED;
713 break;
714
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100715 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000716 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
717 break;
718
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100719 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000720 *to_mode |= 0;
721 break;
722
723 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100724 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100725 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000726 }
727
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100728 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100729}
Jose Marinho09b1db82019-08-08 09:16:59 +0100730
731/**
732 * Updates a VM's page table such that the given set of physical address ranges
733 * are mapped in the address space at the corresponding address ranges, in the
734 * mode provided.
735 *
736 * If commit is false, the page tables will be allocated from the mpool but no
737 * mappings will actually be updated. This function must always be called first
738 * with commit false to check that it will succeed before calling with commit
739 * true, to avoid leaving the page table in a half-updated state. To make a
740 * series of changes atomically you can call them all with commit false before
741 * calling them all with commit true.
742 *
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700743 * vm_ptable_defrag should always be called after a series of page table
744 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +0100745 *
746 * Returns true on success, or false if the update failed and no changes were
747 * made to memory mappings.
748 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100749static bool ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000750 struct vm_locked vm_locked,
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,
Daniel Boulby4dd3f532021-09-21 09:57:08 +0100753 uint32_t mode, struct mpool *ppool, bool commit)
Jose Marinho09b1db82019-08-08 09:16:59 +0100754{
Andrew Walbranca808b12020-05-15 17:22:28 +0100755 uint32_t i;
756 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100757
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700758 if (vm_locked.vm->el0_partition) {
759 mode |= MM_MODE_USER | MM_MODE_NG;
760 }
761
Andrew Walbranca808b12020-05-15 17:22:28 +0100762 /* Iterate over the memory region constituents within each fragment. */
763 for (i = 0; i < fragment_count; ++i) {
764 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
765 size_t size = fragments[i][j].page_count * PAGE_SIZE;
766 paddr_t pa_begin =
767 pa_from_ipa(ipa_init(fragments[i][j].address));
768 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +0200769 uint32_t pa_bits =
770 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +0100771
772 /*
773 * Ensure the requested region falls into system's PA
774 * range.
775 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +0200776 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
777 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +0100778 dlog_error("Region is outside of PA Range\n");
779 return false;
780 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100781
782 if (commit) {
783 vm_identity_commit(vm_locked, pa_begin, pa_end,
784 mode, ppool, NULL);
785 } else if (!vm_identity_prepare(vm_locked, pa_begin,
786 pa_end, mode, ppool)) {
787 return false;
788 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100789 }
790 }
791
792 return true;
793}
794
795/**
796 * Clears a region of physical memory by overwriting it with zeros. The data is
797 * flushed from the cache so the memory has been cleared across the system.
798 */
J-Alves7db32002021-12-14 14:44:50 +0000799static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
800 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +0100801{
802 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000803 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100804 * global mapping of the whole range. Such an approach will limit
805 * the changes to stage-1 tables and will allow only local
806 * invalidation.
807 */
808 bool ret;
809 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +0000810 void *ptr = mm_identity_map(stage1_locked, begin, end,
811 MM_MODE_W | (extra_mode_attributes &
812 plat_ffa_other_world_mode()),
813 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100814 size_t size = pa_difference(begin, end);
815
816 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100817 goto fail;
818 }
819
820 memset_s(ptr, size, 0, size);
821 arch_mm_flush_dcache(ptr, size);
822 mm_unmap(stage1_locked, begin, end, ppool);
823
824 ret = true;
825 goto out;
826
827fail:
828 ret = false;
829
830out:
831 mm_unlock_stage1(&stage1_locked);
832
833 return ret;
834}
835
836/**
837 * Clears a region of physical memory by overwriting it with zeros. The data is
838 * flushed from the cache so the memory has been cleared across the system.
839 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100840static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +0000841 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100842 struct ffa_memory_region_constituent **fragments,
843 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
844 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100845{
846 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +0100847 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100848 bool ret = false;
849
850 /*
851 * Create a local pool so any freed memory can't be used by another
852 * thread. This is to ensure each constituent that is mapped can be
853 * unmapped again afterwards.
854 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000855 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100856
Andrew Walbranca808b12020-05-15 17:22:28 +0100857 /* Iterate over the memory region constituents within each fragment. */
858 for (i = 0; i < fragment_count; ++i) {
859 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100860
Andrew Walbranca808b12020-05-15 17:22:28 +0100861 for (j = 0; j < fragment_constituent_counts[j]; ++j) {
862 size_t size = fragments[i][j].page_count * PAGE_SIZE;
863 paddr_t begin =
864 pa_from_ipa(ipa_init(fragments[i][j].address));
865 paddr_t end = pa_add(begin, size);
866
J-Alves7db32002021-12-14 14:44:50 +0000867 if (!clear_memory(begin, end, &local_page_pool,
868 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100869 /*
870 * api_clear_memory will defrag on failure, so
871 * no need to do it here.
872 */
873 goto out;
874 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100875 }
876 }
877
Jose Marinho09b1db82019-08-08 09:16:59 +0100878 ret = true;
879
880out:
881 mpool_fini(&local_page_pool);
882 return ret;
883}
884
885/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000886 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +0100887 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000888 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +0100889 *
890 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000891 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100892 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +0100893 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100894 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
895 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100896 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +0100897 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100898 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +0100899 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100900static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000901 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100902 struct ffa_memory_region_constituent **fragments,
903 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves363f5722022-04-25 17:37:37 +0100904 uint32_t share_func, struct ffa_memory_access *receivers,
905 uint32_t receivers_count, struct mpool *page_pool, bool clear,
906 uint32_t *orig_from_mode_ret)
Jose Marinho09b1db82019-08-08 09:16:59 +0100907{
Andrew Walbranca808b12020-05-15 17:22:28 +0100908 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100909 uint32_t orig_from_mode;
910 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +0100911 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100912 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100913
914 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +0100915 * Make sure constituents are properly aligned to a 64-bit boundary. If
916 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100917 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100918 for (i = 0; i < fragment_count; ++i) {
919 if (!is_aligned(fragments[i], 8)) {
920 dlog_verbose("Constituents not aligned.\n");
921 return ffa_error(FFA_INVALID_PARAMETERS);
922 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100923 }
924
925 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000926 * Check if the state transition is lawful for the sender, ensure that
927 * all constituents of a memory region being shared are at the same
928 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +0100929 */
J-Alves363f5722022-04-25 17:37:37 +0100930 ret = ffa_send_check_transition(from_locked, share_func, receivers,
931 receivers_count, &orig_from_mode,
932 fragments, fragment_constituent_counts,
Andrew Walbranca808b12020-05-15 17:22:28 +0100933 fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100934 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100935 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100936 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100937 }
938
Andrew Walbran37c574e2020-06-03 11:45:46 +0100939 if (orig_from_mode_ret != NULL) {
940 *orig_from_mode_ret = orig_from_mode;
941 }
942
Jose Marinho09b1db82019-08-08 09:16:59 +0100943 /*
944 * Create a local pool so any freed memory can't be used by another
945 * thread. This is to ensure the original mapping can be restored if the
946 * clear fails.
947 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000948 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100949
950 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000951 * First reserve all required memory for the new page table entries
952 * without committing, to make sure the entire operation will succeed
953 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +0100954 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100955 if (!ffa_region_group_identity_map(
956 from_locked, fragments, fragment_constituent_counts,
957 fragment_count, from_mode, page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100958 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100959 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100960 goto out;
961 }
962
963 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000964 * Update the mapping for the sender. This won't allocate because the
965 * transaction was already prepared above, but may free pages in the
966 * case that a whole block is being unmapped that was previously
967 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +0100968 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100969 CHECK(ffa_region_group_identity_map(
970 from_locked, fragments, fragment_constituent_counts,
971 fragment_count, from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100972
973 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +0000974 if (clear &&
975 !ffa_clear_memory_constituents(
976 plat_ffa_owner_world_mode(from_locked.vm->id), fragments,
977 fragment_constituent_counts, fragment_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100978 /*
979 * On failure, roll back by returning memory to the sender. This
980 * may allocate pages which were previously freed into
981 * `local_page_pool` by the call above, but will never allocate
982 * more pages than that so can never fail.
983 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100984 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +0100985 from_locked, fragments, fragment_constituent_counts,
986 fragment_count, orig_from_mode, &local_page_pool,
987 true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100988
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100989 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100990 goto out;
991 }
992
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100993 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000994
995out:
996 mpool_fini(&local_page_pool);
997
998 /*
999 * Tidy up the page table by reclaiming failed mappings (if there was an
1000 * error) or merging entries into blocks where possible (on success).
1001 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001002 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001003
1004 return ret;
1005}
1006
1007/**
1008 * Validates and maps memory shared from one VM to another.
1009 *
1010 * This function requires the calling context to hold the <to> lock.
1011 *
1012 * Returns:
1013 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001014 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001015 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001016 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001017 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001018 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001019 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01001020static struct ffa_value ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00001021 struct vm_locked to_locked, ffa_vm_id_t from_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01001022 struct ffa_memory_region_constituent **fragments,
1023 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1024 uint32_t memory_to_attributes, uint32_t share_func, bool clear,
1025 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001026{
Andrew Walbranca808b12020-05-15 17:22:28 +01001027 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001028 uint32_t to_mode;
1029 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001030 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001031
1032 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001033 * Make sure constituents are properly aligned to a 64-bit boundary. If
1034 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001035 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001036 for (i = 0; i < fragment_count; ++i) {
1037 if (!is_aligned(fragments[i], 8)) {
1038 return ffa_error(FFA_INVALID_PARAMETERS);
1039 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001040 }
1041
1042 /*
1043 * Check if the state transition is lawful for the recipient, and ensure
1044 * that all constituents of the memory region being retrieved are at the
1045 * same state.
1046 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001047 ret = ffa_retrieve_check_transition(
1048 to_locked, share_func, fragments, fragment_constituent_counts,
1049 fragment_count, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001050 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001051 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001052 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001053 }
1054
1055 /*
1056 * Create a local pool so any freed memory can't be used by another
1057 * thread. This is to ensure the original mapping can be restored if the
1058 * clear fails.
1059 */
1060 mpool_init_with_fallback(&local_page_pool, page_pool);
1061
1062 /*
1063 * First reserve all required memory for the new page table entries in
1064 * the recipient page tables without committing, to make sure the entire
1065 * operation will succeed without exhausting the page pool.
1066 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001067 if (!ffa_region_group_identity_map(
1068 to_locked, fragments, fragment_constituent_counts,
1069 fragment_count, to_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001070 /* TODO: partial defrag of failed range. */
1071 dlog_verbose(
1072 "Insufficient memory to update recipient page "
1073 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001074 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001075 goto out;
1076 }
1077
1078 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001079 if (clear &&
1080 !ffa_clear_memory_constituents(
1081 plat_ffa_owner_world_mode(from_id), fragments,
1082 fragment_constituent_counts, fragment_count, page_pool)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001083 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001084 goto out;
1085 }
1086
Jose Marinho09b1db82019-08-08 09:16:59 +01001087 /*
1088 * Complete the transfer by mapping the memory into the recipient. This
1089 * won't allocate because the transaction was already prepared above, so
1090 * it doesn't need to use the `local_page_pool`.
1091 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001092 CHECK(ffa_region_group_identity_map(
1093 to_locked, fragments, fragment_constituent_counts,
1094 fragment_count, to_mode, page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001095
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001096 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001097
1098out:
1099 mpool_fini(&local_page_pool);
1100
1101 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001102 * Tidy up the page table by reclaiming failed mappings (if there was an
1103 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001104 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001105 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001106
1107 return ret;
1108}
1109
Andrew Walbran290b0c92020-02-03 16:37:14 +00001110/**
J-Alves8505a8a2022-06-15 18:10:18 +01001111 * Reclaims the given memory from the other world. To do this space is first
1112 * reserved in the <to> VM's page table, then the reclaim request is sent on to
1113 * the other world. then (if that is successful) the memory is mapped back into
1114 * the <to> VM's page table.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001115 *
1116 * This function requires the calling context to hold the <to> lock.
1117 *
1118 * Returns:
1119 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001120 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran290b0c92020-02-03 16:37:14 +00001121 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001122 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran290b0c92020-02-03 16:37:14 +00001123 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001124 * Success is indicated by FFA_SUCCESS.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001125 */
J-Alves8505a8a2022-06-15 18:10:18 +01001126static struct ffa_value ffa_other_world_reclaim_check_update(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001127 struct vm_locked to_locked, ffa_memory_handle_t handle,
1128 struct ffa_memory_region_constituent *constituents,
Andrew Walbran290b0c92020-02-03 16:37:14 +00001129 uint32_t constituent_count, uint32_t memory_to_attributes, bool clear,
1130 struct mpool *page_pool)
1131{
Andrew Walbran290b0c92020-02-03 16:37:14 +00001132 uint32_t to_mode;
1133 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001134 struct ffa_value ret;
J-Alves8505a8a2022-06-15 18:10:18 +01001135 ffa_memory_region_flags_t other_world_flags;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001136
1137 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001138 * Make sure constituents are properly aligned to a 64-bit boundary. If
1139 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001140 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001141 if (!is_aligned(constituents, 8)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001142 dlog_verbose("Constituents not aligned.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001143 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001144 }
1145
1146 /*
1147 * Check if the state transition is lawful for the recipient, and ensure
1148 * that all constituents of the memory region being retrieved are at the
1149 * same state.
1150 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001151 ret = ffa_retrieve_check_transition(to_locked, FFA_MEM_RECLAIM_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01001152 &constituents, &constituent_count,
1153 1, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001154 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001155 dlog_verbose("Invalid transition.\n");
1156 return ret;
1157 }
1158
1159 /*
1160 * Create a local pool so any freed memory can't be used by another
1161 * thread. This is to ensure the original mapping can be restored if the
1162 * clear fails.
1163 */
1164 mpool_init_with_fallback(&local_page_pool, page_pool);
1165
1166 /*
1167 * First reserve all required memory for the new page table entries in
1168 * the recipient page tables without committing, to make sure the entire
1169 * operation will succeed without exhausting the page pool.
1170 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001171 if (!ffa_region_group_identity_map(to_locked, &constituents,
1172 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001173 page_pool, false)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001174 /* TODO: partial defrag of failed range. */
1175 dlog_verbose(
1176 "Insufficient memory to update recipient page "
1177 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001178 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001179 goto out;
1180 }
1181
1182 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01001183 * Forward the request to the other world, check if SPMC returned
1184 * FFA_SUCCESS_32. If not, terminate and return the error to caller
1185 * VM.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001186 */
J-Alves8505a8a2022-06-15 18:10:18 +01001187 other_world_flags = 0;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001188 if (clear) {
J-Alves8505a8a2022-06-15 18:10:18 +01001189 other_world_flags |= FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001190 }
Olivier Deprez112d2b52020-09-30 07:39:23 +02001191 ret = arch_other_world_call(
1192 (struct ffa_value){.func = FFA_MEM_RECLAIM_32,
1193 .arg1 = (uint32_t)handle,
1194 .arg2 = (uint32_t)(handle >> 32),
J-Alves8505a8a2022-06-15 18:10:18 +01001195 .arg3 = other_world_flags});
Andrew Walbran290b0c92020-02-03 16:37:14 +00001196
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001197 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001198 dlog_verbose(
J-Alves8505a8a2022-06-15 18:10:18 +01001199 "Got %#x (%d) from other world in response to "
1200 "FFA_MEM_RECLAIM, "
Andrew Walbranca808b12020-05-15 17:22:28 +01001201 "expected FFA_SUCCESS.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00001202 ret.func, ret.arg2);
1203 goto out;
1204 }
1205
1206 /*
J-Alves8505a8a2022-06-15 18:10:18 +01001207 * The other world was happy with it, so complete the reclaim by mapping
1208 * the memory into the recipient. This won't allocate because the
Andrew Walbran290b0c92020-02-03 16:37:14 +00001209 * transaction was already prepared above, so it doesn't need to use the
1210 * `local_page_pool`.
1211 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001212 CHECK(ffa_region_group_identity_map(to_locked, &constituents,
1213 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001214 page_pool, true));
Andrew Walbran290b0c92020-02-03 16:37:14 +00001215
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001216 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran290b0c92020-02-03 16:37:14 +00001217
1218out:
1219 mpool_fini(&local_page_pool);
1220
1221 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001222 * Tidy up the page table by reclaiming failed mappings (if there was an
1223 * error) or merging entries into blocks where possible (on success).
Andrew Walbran290b0c92020-02-03 16:37:14 +00001224 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001225 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001226
1227 return ret;
1228}
1229
Andrew Walbran996d1d12020-05-27 14:08:43 +01001230static struct ffa_value ffa_relinquish_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001231 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001232 struct ffa_memory_region_constituent **fragments,
1233 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1234 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001235{
1236 uint32_t orig_from_mode;
1237 uint32_t from_mode;
1238 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001239 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001240
Andrew Walbranca808b12020-05-15 17:22:28 +01001241 ret = ffa_relinquish_check_transition(
1242 from_locked, &orig_from_mode, fragments,
1243 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001244 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001245 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001246 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001247 }
1248
1249 /*
1250 * Create a local pool so any freed memory can't be used by another
1251 * thread. This is to ensure the original mapping can be restored if the
1252 * clear fails.
1253 */
1254 mpool_init_with_fallback(&local_page_pool, page_pool);
1255
1256 /*
1257 * First reserve all required memory for the new page table entries
1258 * without committing, to make sure the entire operation will succeed
1259 * without exhausting the page pool.
1260 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001261 if (!ffa_region_group_identity_map(
1262 from_locked, fragments, fragment_constituent_counts,
1263 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001264 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001265 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001266 goto out;
1267 }
1268
1269 /*
1270 * Update the mapping for the sender. This won't allocate because the
1271 * transaction was already prepared above, but may free pages in the
1272 * case that a whole block is being unmapped that was previously
1273 * partially mapped.
1274 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001275 CHECK(ffa_region_group_identity_map(
1276 from_locked, fragments, fragment_constituent_counts,
1277 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001278
1279 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001280 if (clear &&
1281 !ffa_clear_memory_constituents(
1282 plat_ffa_owner_world_mode(from_locked.vm->id), fragments,
1283 fragment_constituent_counts, fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001284 /*
1285 * On failure, roll back by returning memory to the sender. This
1286 * may allocate pages which were previously freed into
1287 * `local_page_pool` by the call above, but will never allocate
1288 * more pages than that so can never fail.
1289 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001290 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001291 from_locked, fragments, fragment_constituent_counts,
1292 fragment_count, orig_from_mode, &local_page_pool,
1293 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001294
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001295 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001296 goto out;
1297 }
1298
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001299 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001300
1301out:
1302 mpool_fini(&local_page_pool);
1303
1304 /*
1305 * Tidy up the page table by reclaiming failed mappings (if there was an
1306 * error) or merging entries into blocks where possible (on success).
1307 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001308 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001309
1310 return ret;
1311}
1312
1313/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001314 * Complete a memory sending operation by checking that it is valid, updating
1315 * the sender page table, and then either marking the share state as having
1316 * completed sending (on success) or freeing it (on failure).
1317 *
1318 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1319 */
1320static struct ffa_value ffa_memory_send_complete(
1321 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001322 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1323 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001324{
1325 struct ffa_memory_region *memory_region = share_state->memory_region;
1326 struct ffa_value ret;
1327
1328 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001329 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001330
1331 /* Check that state is valid in sender page table and update. */
1332 ret = ffa_send_check_update(
1333 from_locked, share_state->fragments,
1334 share_state->fragment_constituent_counts,
1335 share_state->fragment_count, share_state->share_func,
J-Alves363f5722022-04-25 17:37:37 +01001336 memory_region->receivers, memory_region->receiver_count,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001337 page_pool, memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1338 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001339 if (ret.func != FFA_SUCCESS_32) {
1340 /*
1341 * Free share state, it failed to send so it can't be retrieved.
1342 */
1343 dlog_verbose("Complete failed, freeing share state.\n");
1344 share_state_free(share_states, share_state, page_pool);
1345 return ret;
1346 }
1347
1348 share_state->sending_complete = true;
1349 dlog_verbose("Marked sending complete.\n");
1350
J-Alvesee68c542020-10-29 17:48:20 +00001351 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001352}
1353
1354/**
Federico Recanatia98603a2021-12-20 18:04:03 +01001355 * Check that the memory attributes match Hafnium expectations:
1356 * Normal Memory, Inner shareable, Write-Back Read-Allocate
1357 * Write-Allocate Cacheable.
1358 */
1359static struct ffa_value ffa_memory_attributes_validate(
1360 ffa_memory_access_permissions_t attributes)
1361{
1362 enum ffa_memory_type memory_type;
1363 enum ffa_memory_cacheability cacheability;
1364 enum ffa_memory_shareability shareability;
1365
1366 memory_type = ffa_get_memory_type_attr(attributes);
1367 if (memory_type != FFA_MEMORY_NORMAL_MEM) {
1368 dlog_verbose("Invalid memory type %#x, expected %#x.\n",
1369 memory_type, FFA_MEMORY_NORMAL_MEM);
Federico Recanati3d953f32022-02-17 09:31:29 +01001370 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001371 }
1372
1373 cacheability = ffa_get_memory_cacheability_attr(attributes);
1374 if (cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1375 dlog_verbose("Invalid cacheability %#x, expected %#x.\n",
1376 cacheability, FFA_MEMORY_CACHE_WRITE_BACK);
Federico Recanati3d953f32022-02-17 09:31:29 +01001377 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001378 }
1379
1380 shareability = ffa_get_memory_shareability_attr(attributes);
1381 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
1382 dlog_verbose("Invalid shareability %#x, expected #%x.\n",
1383 shareability, FFA_MEMORY_INNER_SHAREABLE);
Federico Recanati3d953f32022-02-17 09:31:29 +01001384 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001385 }
1386
1387 return (struct ffa_value){.func = FFA_SUCCESS_32};
1388}
1389
1390/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001391 * Check that the given `memory_region` represents a valid memory send request
1392 * of the given `share_func` type, return the clear flag and permissions via the
1393 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001394 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001395 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001396 * not.
1397 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001398static struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001399 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1400 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001401 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001402{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001403 struct ffa_composite_memory_region *composite;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001404 uint32_t receivers_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001405 uint32_t composite_memory_region_offset;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001406 uint32_t constituents_offset;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001407 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001408 enum ffa_data_access data_access;
1409 enum ffa_instruction_access instruction_access;
Federico Recanatia98603a2021-12-20 18:04:03 +01001410 struct ffa_value ret;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001411
J-Alves95df0ef2022-12-07 10:09:48 +00001412 /* The sender must match the caller. */
1413 if ((!vm_id_is_current_world(from_locked.vm->id) &&
1414 vm_id_is_current_world(memory_region->sender)) ||
1415 (vm_id_is_current_world(from_locked.vm->id) &&
1416 memory_region->sender != from_locked.vm->id)) {
1417 dlog_verbose("Invalid memory sender ID.\n");
1418 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001419 }
1420
Andrew Walbrana65a1322020-04-06 19:32:32 +01001421 /*
1422 * Ensure that the composite header is within the memory bounds and
1423 * doesn't overlap the first part of the message.
1424 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001425 receivers_length = sizeof(struct ffa_memory_access) *
1426 memory_region->receiver_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001427 constituents_offset =
1428 ffa_composite_constituent_offset(memory_region, 0);
Federico Recanati872cd692022-01-05 13:10:10 +01001429 composite_memory_region_offset =
1430 memory_region->receivers[0].composite_memory_region_offset;
1431 if ((composite_memory_region_offset == 0) ||
1432 (composite_memory_region_offset <
1433 sizeof(struct ffa_memory_region) + receivers_length) ||
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001434 constituents_offset > fragment_length) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001435 dlog_verbose(
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001436 "Invalid composite memory region descriptor offset "
1437 "%d.\n",
1438 memory_region->receivers[0]
1439 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001440 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001441 }
1442
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001443 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001444
1445 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001446 * Ensure the number of constituents are within the memory bounds.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001447 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001448 constituents_length = sizeof(struct ffa_memory_region_constituent) *
1449 composite->constituent_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001450 if (memory_share_length != constituents_offset + constituents_length) {
1451 dlog_verbose("Invalid length %d or composite offset %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001452 memory_share_length,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001453 memory_region->receivers[0]
1454 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001455 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001456 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001457 if (fragment_length < memory_share_length &&
1458 fragment_length < HF_MAILBOX_SIZE) {
1459 dlog_warning(
1460 "Initial fragment length %d smaller than mailbox "
1461 "size.\n",
1462 fragment_length);
1463 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001464
Andrew Walbrana65a1322020-04-06 19:32:32 +01001465 /*
1466 * Clear is not allowed for memory sharing, as the sender still has
1467 * access to the memory.
1468 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001469 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1470 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001471 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001472 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001473 }
1474
1475 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001476 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001477 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001478 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001479 }
1480
J-Alves363f5722022-04-25 17:37:37 +01001481 /* Check that the permissions are valid, for each specified receiver. */
1482 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
1483 ffa_memory_access_permissions_t permissions =
1484 memory_region->receivers[i]
1485 .receiver_permissions.permissions;
1486 ffa_vm_id_t receiver_id =
1487 memory_region->receivers[i]
1488 .receiver_permissions.receiver;
1489
1490 if (memory_region->sender == receiver_id) {
1491 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001492 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001493 }
Federico Recanati85090c42021-12-15 13:17:54 +01001494
J-Alves363f5722022-04-25 17:37:37 +01001495 for (uint32_t j = i + 1; j < memory_region->receiver_count;
1496 j++) {
1497 if (receiver_id ==
1498 memory_region->receivers[j]
1499 .receiver_permissions.receiver) {
1500 dlog_verbose(
1501 "Repeated receiver(%x) in memory send "
1502 "operation.\n",
1503 memory_region->receivers[j]
1504 .receiver_permissions.receiver);
1505 return ffa_error(FFA_INVALID_PARAMETERS);
1506 }
1507 }
1508
1509 if (composite_memory_region_offset !=
1510 memory_region->receivers[i]
1511 .composite_memory_region_offset) {
1512 dlog_verbose(
1513 "All ffa_memory_access should point to the "
1514 "same composite memory region offset.\n");
1515 return ffa_error(FFA_INVALID_PARAMETERS);
1516 }
1517
1518 data_access = ffa_get_data_access_attr(permissions);
1519 instruction_access =
1520 ffa_get_instruction_access_attr(permissions);
1521 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1522 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
1523 dlog_verbose(
1524 "Reserved value for receiver permissions "
1525 "%#x.\n",
1526 permissions);
1527 return ffa_error(FFA_INVALID_PARAMETERS);
1528 }
1529 if (instruction_access !=
1530 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
1531 dlog_verbose(
1532 "Invalid instruction access permissions %#x "
1533 "for sending memory.\n",
1534 permissions);
1535 return ffa_error(FFA_INVALID_PARAMETERS);
1536 }
1537 if (share_func == FFA_MEM_SHARE_32) {
1538 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1539 dlog_verbose(
1540 "Invalid data access permissions %#x "
1541 "for sharing memory.\n",
1542 permissions);
1543 return ffa_error(FFA_INVALID_PARAMETERS);
1544 }
1545 /*
1546 * According to section 10.10.3 of the FF-A v1.1 EAC0
1547 * spec, NX is required for share operations (but must
1548 * not be specified by the sender) so set it in the
1549 * copy that we store, ready to be returned to the
1550 * retriever.
1551 */
J-Alvesb19731a2022-06-20 17:30:33 +01001552 if (vm_id_is_current_world(receiver_id)) {
1553 ffa_set_instruction_access_attr(
1554 &permissions,
1555 FFA_INSTRUCTION_ACCESS_NX);
1556 memory_region->receivers[i]
1557 .receiver_permissions.permissions =
1558 permissions;
1559 }
J-Alves363f5722022-04-25 17:37:37 +01001560 }
1561 if (share_func == FFA_MEM_LEND_32 &&
1562 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1563 dlog_verbose(
1564 "Invalid data access permissions %#x for "
1565 "lending memory.\n",
1566 permissions);
1567 return ffa_error(FFA_INVALID_PARAMETERS);
1568 }
1569
1570 if (share_func == FFA_MEM_DONATE_32 &&
1571 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
1572 dlog_verbose(
1573 "Invalid data access permissions %#x for "
1574 "donating memory.\n",
1575 permissions);
1576 return ffa_error(FFA_INVALID_PARAMETERS);
1577 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001578 }
1579
Federico Recanatid937f5e2021-12-20 17:38:23 +01001580 /*
J-Alves807794e2022-06-16 13:42:47 +01001581 * If a memory donate or lend with single borrower, the memory type
1582 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01001583 */
J-Alves807794e2022-06-16 13:42:47 +01001584 if (share_func == FFA_MEM_DONATE_32 ||
1585 (share_func == FFA_MEM_LEND_32 &&
1586 memory_region->receiver_count == 1)) {
1587 if (ffa_get_memory_type_attr(memory_region->attributes) !=
1588 FFA_MEMORY_NOT_SPECIFIED_MEM) {
1589 dlog_verbose(
1590 "Memory type shall not be specified by "
1591 "sender.\n");
1592 return ffa_error(FFA_INVALID_PARAMETERS);
1593 }
1594 } else {
1595 /*
1596 * Check that sender's memory attributes match Hafnium
1597 * expectations: Normal Memory, Inner shareable, Write-Back
1598 * Read-Allocate Write-Allocate Cacheable.
1599 */
1600 ret = ffa_memory_attributes_validate(memory_region->attributes);
1601 if (ret.func != FFA_SUCCESS_32) {
1602 return ret;
1603 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01001604 }
1605
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001606 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001607}
1608
J-Alves8505a8a2022-06-15 18:10:18 +01001609/** Forwards a memory send message on to the other world. */
1610static struct ffa_value memory_send_other_world_forward(
1611 struct vm_locked other_world_locked, ffa_vm_id_t sender_vm_id,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001612 uint32_t share_func, struct ffa_memory_region *memory_region,
1613 uint32_t memory_share_length, uint32_t fragment_length)
1614{
1615 struct ffa_value ret;
1616
J-Alves8505a8a2022-06-15 18:10:18 +01001617 /* Use its own RX buffer. */
1618 memcpy_s(other_world_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001619 memory_region, fragment_length);
J-Alves8505a8a2022-06-15 18:10:18 +01001620 other_world_locked.vm->mailbox.recv_size = fragment_length;
1621 other_world_locked.vm->mailbox.recv_sender = sender_vm_id;
1622 other_world_locked.vm->mailbox.recv_func = share_func;
1623 other_world_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001624 ret = arch_other_world_call(
1625 (struct ffa_value){.func = share_func,
1626 .arg1 = memory_share_length,
1627 .arg2 = fragment_length});
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001628 /*
J-Alves8505a8a2022-06-15 18:10:18 +01001629 * After the call to the other world completes it must have finished
1630 * reading its RX buffer, so it is ready for another message.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001631 */
J-Alves8505a8a2022-06-15 18:10:18 +01001632 other_world_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001633
1634 return ret;
1635}
1636
Andrew Walbrana65a1322020-04-06 19:32:32 +01001637/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001638 * Gets the share state for continuing an operation to donate, lend or share
1639 * memory, and checks that it is a valid request.
1640 *
1641 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1642 * not.
1643 */
1644static struct ffa_value ffa_memory_send_continue_validate(
1645 struct share_states_locked share_states, ffa_memory_handle_t handle,
1646 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1647 struct mpool *page_pool)
1648{
1649 struct ffa_memory_share_state *share_state;
1650 struct ffa_memory_region *memory_region;
1651
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001652 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001653
1654 /*
1655 * Look up the share state by handle and make sure that the VM ID
1656 * matches.
1657 */
1658 if (!get_share_state(share_states, handle, &share_state)) {
1659 dlog_verbose(
1660 "Invalid handle %#x for memory send continuation.\n",
1661 handle);
1662 return ffa_error(FFA_INVALID_PARAMETERS);
1663 }
1664 memory_region = share_state->memory_region;
1665
1666 if (memory_region->sender != from_vm_id) {
1667 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1668 return ffa_error(FFA_INVALID_PARAMETERS);
1669 }
1670
1671 if (share_state->sending_complete) {
1672 dlog_verbose(
1673 "Sending of memory handle %#x is already complete.\n",
1674 handle);
1675 return ffa_error(FFA_INVALID_PARAMETERS);
1676 }
1677
1678 if (share_state->fragment_count == MAX_FRAGMENTS) {
1679 /*
1680 * Log a warning as this is a sign that MAX_FRAGMENTS should
1681 * probably be increased.
1682 */
1683 dlog_warning(
1684 "Too many fragments for memory share with handle %#x; "
1685 "only %d supported.\n",
1686 handle, MAX_FRAGMENTS);
1687 /* Free share state, as it's not possible to complete it. */
1688 share_state_free(share_states, share_state, page_pool);
1689 return ffa_error(FFA_NO_MEMORY);
1690 }
1691
1692 *share_state_ret = share_state;
1693
1694 return (struct ffa_value){.func = FFA_SUCCESS_32};
1695}
1696
1697/**
J-Alves8505a8a2022-06-15 18:10:18 +01001698 * Forwards a memory send continuation message on to the other world.
Andrew Walbranca808b12020-05-15 17:22:28 +01001699 */
J-Alves8505a8a2022-06-15 18:10:18 +01001700static struct ffa_value memory_send_continue_other_world_forward(
1701 struct vm_locked other_world_locked, ffa_vm_id_t sender_vm_id,
1702 void *fragment, uint32_t fragment_length, ffa_memory_handle_t handle)
Andrew Walbranca808b12020-05-15 17:22:28 +01001703{
1704 struct ffa_value ret;
1705
J-Alves8505a8a2022-06-15 18:10:18 +01001706 memcpy_s(other_world_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
1707 fragment, fragment_length);
1708 other_world_locked.vm->mailbox.recv_size = fragment_length;
1709 other_world_locked.vm->mailbox.recv_sender = sender_vm_id;
1710 other_world_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
1711 other_world_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
Olivier Deprez112d2b52020-09-30 07:39:23 +02001712 ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01001713 (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
1714 .arg1 = (uint32_t)handle,
1715 .arg2 = (uint32_t)(handle >> 32),
1716 .arg3 = fragment_length,
1717 .arg4 = (uint64_t)sender_vm_id << 16});
1718 /*
J-Alves8505a8a2022-06-15 18:10:18 +01001719 * After the call to the other world completes it must have finished
1720 * reading its RX buffer, so it is ready for another message.
Andrew Walbranca808b12020-05-15 17:22:28 +01001721 */
J-Alves8505a8a2022-06-15 18:10:18 +01001722 other_world_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
Andrew Walbranca808b12020-05-15 17:22:28 +01001723
1724 return ret;
1725}
1726
1727/**
J-Alves95df0ef2022-12-07 10:09:48 +00001728 * Checks if there is at least one receiver from the other world.
1729 */
1730static bool memory_region_receivers_from_other_world(
1731 struct ffa_memory_region *memory_region)
1732{
1733 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
1734 ffa_vm_id_t receiver = memory_region->receivers[i]
1735 .receiver_permissions.receiver;
1736 if (!vm_id_is_current_world(receiver)) {
1737 return true;
1738 }
1739 }
1740 return false;
1741}
1742
1743/**
J-Alves8505a8a2022-06-15 18:10:18 +01001744 * Validates a call to donate, lend or share memory to a non-other world VM and
1745 * then updates the stage-2 page tables. Specifically, check if the message
1746 * length and number of memory region constituents match, and if the transition
1747 * is valid for the type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001748 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001749 * Assumes that the caller has already found and locked the sender VM and copied
1750 * the memory region descriptor from the sender's TX buffer to a freshly
1751 * allocated page from Hafnium's internal pool. The caller must have also
1752 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001753 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001754 * This function takes ownership of the `memory_region` passed in and will free
1755 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001756 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001757struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001758 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001759 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001760 uint32_t fragment_length, uint32_t share_func,
1761 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001762{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001763 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001764 struct share_states_locked share_states;
1765 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001766
1767 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001768 * If there is an error validating the `memory_region` then we need to
1769 * free it because we own it but we won't be storing it in a share state
1770 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001771 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001772 ret = ffa_memory_send_validate(from_locked, memory_region,
1773 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001774 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001775 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001776 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001777 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001778 }
1779
Andrew Walbrana65a1322020-04-06 19:32:32 +01001780 /* Set flag for share function, ready to be retrieved later. */
1781 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001782 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001783 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001784 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001785 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001786 case FFA_MEM_LEND_32:
1787 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001788 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001789 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001790 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001791 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001792 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001793 }
1794
Andrew Walbranca808b12020-05-15 17:22:28 +01001795 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001796 /*
1797 * Allocate a share state before updating the page table. Otherwise if
1798 * updating the page table succeeded but allocating the share state
1799 * failed then it would leave the memory in a state where nobody could
1800 * get it back.
1801 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001802 if (!allocate_share_state(share_states, share_func, memory_region,
1803 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1804 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001805 dlog_verbose("Failed to allocate share state.\n");
1806 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001807 ret = ffa_error(FFA_NO_MEMORY);
1808 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001809 }
1810
Andrew Walbranca808b12020-05-15 17:22:28 +01001811 if (fragment_length == memory_share_length) {
1812 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00001813 ret = ffa_memory_send_complete(
1814 from_locked, share_states, share_state, page_pool,
1815 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001816 } else {
1817 ret = (struct ffa_value){
1818 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00001819 .arg1 = (uint32_t)memory_region->handle,
1820 .arg2 = (uint32_t)(memory_region->handle >> 32),
Andrew Walbranca808b12020-05-15 17:22:28 +01001821 .arg3 = fragment_length};
1822 }
1823
1824out:
1825 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001826 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001827 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001828}
1829
1830/**
J-Alves8505a8a2022-06-15 18:10:18 +01001831 * Validates a call to donate, lend or share memory to the other world and then
1832 * updates the stage-2 page tables. Specifically, check if the message length
1833 * and number of memory region constituents match, and if the transition is
1834 * valid for the type of memory sending operation.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001835 *
1836 * Assumes that the caller has already found and locked the sender VM and the
J-Alves8505a8a2022-06-15 18:10:18 +01001837 * other world VM, and copied the memory region descriptor from the sender's TX
1838 * buffer to a freshly allocated page from Hafnium's internal pool. The caller
1839 * must have also validated that the receiver VM ID is valid.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001840 *
1841 * This function takes ownership of the `memory_region` passed in and will free
1842 * it when necessary; it must not be freed by the caller.
1843 */
J-Alves8505a8a2022-06-15 18:10:18 +01001844struct ffa_value ffa_memory_other_world_send(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001845 struct vm_locked from_locked, struct vm_locked to_locked,
1846 struct ffa_memory_region *memory_region, uint32_t memory_share_length,
1847 uint32_t fragment_length, uint32_t share_func, struct mpool *page_pool)
1848{
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001849 struct ffa_value ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001850
1851 /*
1852 * If there is an error validating the `memory_region` then we need to
1853 * free it because we own it but we won't be storing it in a share state
1854 * after all.
1855 */
1856 ret = ffa_memory_send_validate(from_locked, memory_region,
1857 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001858 share_func);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001859 if (ret.func != FFA_SUCCESS_32) {
1860 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001861 }
1862
Andrew Walbranca808b12020-05-15 17:22:28 +01001863 if (fragment_length == memory_share_length) {
1864 /* No more fragments to come, everything fit in one message. */
1865 struct ffa_composite_memory_region *composite =
1866 ffa_memory_region_get_composite(memory_region, 0);
1867 struct ffa_memory_region_constituent *constituents =
1868 composite->constituents;
Andrew Walbran37c574e2020-06-03 11:45:46 +01001869 struct mpool local_page_pool;
1870 uint32_t orig_from_mode;
1871
1872 /*
1873 * Use a local page pool so that we can roll back if necessary.
1874 */
1875 mpool_init_with_fallback(&local_page_pool, page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001876
1877 ret = ffa_send_check_update(
1878 from_locked, &constituents,
1879 &composite->constituent_count, 1, share_func,
J-Alves363f5722022-04-25 17:37:37 +01001880 memory_region->receivers, memory_region->receiver_count,
1881 &local_page_pool,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001882 memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1883 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001884 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001885 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001886 goto out;
1887 }
1888
J-Alves8505a8a2022-06-15 18:10:18 +01001889 /* Forward memory send message on to other world. */
1890 ret = memory_send_other_world_forward(
Andrew Walbranca808b12020-05-15 17:22:28 +01001891 to_locked, from_locked.vm->id, share_func,
1892 memory_region, memory_share_length, fragment_length);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001893
1894 if (ret.func != FFA_SUCCESS_32) {
1895 dlog_verbose(
J-Alves8505a8a2022-06-15 18:10:18 +01001896 "Other world didn't successfully complete "
1897 "memory send operation; returned %#x (%d). "
1898 "Rolling back.\n",
Andrew Walbran37c574e2020-06-03 11:45:46 +01001899 ret.func, ret.arg2);
1900
1901 /*
J-Alves8505a8a2022-06-15 18:10:18 +01001902 * The other world failed to complete the send
1903 * operation, so roll back the page table update for the
1904 * VM. This can't fail because it won't try to allocate
1905 * more memory than was freed into the `local_page_pool`
1906 * by `ffa_send_check_update` in the initial update.
Andrew Walbran37c574e2020-06-03 11:45:46 +01001907 */
1908 CHECK(ffa_region_group_identity_map(
1909 from_locked, &constituents,
1910 &composite->constituent_count, 1,
1911 orig_from_mode, &local_page_pool, true));
1912 }
1913
1914 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001915 } else {
1916 struct share_states_locked share_states = share_states_lock();
1917 ffa_memory_handle_t handle;
1918
1919 /*
1920 * We need to wait for the rest of the fragments before we can
1921 * check whether the transaction is valid and unmap the memory.
J-Alves8505a8a2022-06-15 18:10:18 +01001922 * Call the other world so it can do its initial validation and
1923 * assign a handle, and allocate a share state to keep what we
1924 * have so far.
Andrew Walbranca808b12020-05-15 17:22:28 +01001925 */
J-Alves8505a8a2022-06-15 18:10:18 +01001926 ret = memory_send_other_world_forward(
Andrew Walbranca808b12020-05-15 17:22:28 +01001927 to_locked, from_locked.vm->id, share_func,
1928 memory_region, memory_share_length, fragment_length);
1929 if (ret.func == FFA_ERROR_32) {
1930 goto out_unlock;
1931 } else if (ret.func != FFA_MEM_FRAG_RX_32) {
1932 dlog_warning(
J-Alves8505a8a2022-06-15 18:10:18 +01001933 "Got %#x from other world in response to %#x "
1934 "for "
Olivier Deprez701e8bf2022-04-06 18:45:18 +02001935 "fragment with %d/%d, expected "
Andrew Walbranca808b12020-05-15 17:22:28 +01001936 "FFA_MEM_FRAG_RX.\n",
1937 ret.func, share_func, fragment_length,
1938 memory_share_length);
1939 ret = ffa_error(FFA_INVALID_PARAMETERS);
1940 goto out_unlock;
1941 }
1942 handle = ffa_frag_handle(ret);
1943 if (ret.arg3 != fragment_length) {
1944 dlog_warning(
1945 "Got unexpected fragment offset %d for "
J-Alves8505a8a2022-06-15 18:10:18 +01001946 "FFA_MEM_FRAG_RX from other world (expected "
1947 "%d).\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01001948 ret.arg3, fragment_length);
1949 ret = ffa_error(FFA_INVALID_PARAMETERS);
1950 goto out_unlock;
1951 }
1952 if (ffa_frag_sender(ret) != from_locked.vm->id) {
1953 dlog_warning(
1954 "Got unexpected sender ID %d for "
J-Alves8505a8a2022-06-15 18:10:18 +01001955 "FFA_MEM_FRAG_RX from other world (expected "
1956 "%d).\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01001957 ffa_frag_sender(ret), from_locked.vm->id);
1958 ret = ffa_error(FFA_INVALID_PARAMETERS);
1959 goto out_unlock;
1960 }
1961
1962 if (!allocate_share_state(share_states, share_func,
1963 memory_region, fragment_length,
1964 handle, NULL)) {
1965 dlog_verbose("Failed to allocate share state.\n");
1966 ret = ffa_error(FFA_NO_MEMORY);
1967 goto out_unlock;
1968 }
1969 /*
1970 * Don't free the memory region fragment, as it has been stored
1971 * in the share state.
1972 */
1973 memory_region = NULL;
1974 out_unlock:
1975 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001976 }
1977
Andrew Walbranca808b12020-05-15 17:22:28 +01001978out:
1979 if (memory_region != NULL) {
1980 mpool_free(page_pool, memory_region);
1981 }
1982 dump_share_states();
1983 return ret;
1984}
1985
1986/**
J-Alves8505a8a2022-06-15 18:10:18 +01001987 * Continues an operation to donate, lend or share memory to a VM from current
1988 * world. If this is the last fragment then checks that the transition is valid
1989 * for the type of memory sending operation and updates the stage-2 page tables
1990 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01001991 *
1992 * Assumes that the caller has already found and locked the sender VM and copied
1993 * the memory region descriptor from the sender's TX buffer to a freshly
1994 * allocated page from Hafnium's internal pool.
1995 *
1996 * This function takes ownership of the `fragment` passed in; it must not be
1997 * freed by the caller.
1998 */
1999struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
2000 void *fragment,
2001 uint32_t fragment_length,
2002 ffa_memory_handle_t handle,
2003 struct mpool *page_pool)
2004{
2005 struct share_states_locked share_states = share_states_lock();
2006 struct ffa_memory_share_state *share_state;
2007 struct ffa_value ret;
2008 struct ffa_memory_region *memory_region;
2009
2010 ret = ffa_memory_send_continue_validate(share_states, handle,
2011 &share_state,
2012 from_locked.vm->id, page_pool);
2013 if (ret.func != FFA_SUCCESS_32) {
2014 goto out_free_fragment;
2015 }
2016 memory_region = share_state->memory_region;
2017
J-Alves95df0ef2022-12-07 10:09:48 +00002018 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002019 dlog_error(
2020 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01002021 "other world. This should never happen, and indicates "
2022 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01002023 "EL3 code.\n");
2024 ret = ffa_error(FFA_INVALID_PARAMETERS);
2025 goto out_free_fragment;
2026 }
2027
2028 /* Add this fragment. */
2029 share_state->fragments[share_state->fragment_count] = fragment;
2030 share_state->fragment_constituent_counts[share_state->fragment_count] =
2031 fragment_length / sizeof(struct ffa_memory_region_constituent);
2032 share_state->fragment_count++;
2033
2034 /* Check whether the memory send operation is now ready to complete. */
2035 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00002036 ret = ffa_memory_send_complete(
2037 from_locked, share_states, share_state, page_pool,
2038 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01002039 } else {
2040 ret = (struct ffa_value){
2041 .func = FFA_MEM_FRAG_RX_32,
2042 .arg1 = (uint32_t)handle,
2043 .arg2 = (uint32_t)(handle >> 32),
2044 .arg3 = share_state_next_fragment_offset(share_states,
2045 share_state)};
2046 }
2047 goto out;
2048
2049out_free_fragment:
2050 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002051
2052out:
Andrew Walbranca808b12020-05-15 17:22:28 +01002053 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01002054 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002055}
2056
Andrew Walbranca808b12020-05-15 17:22:28 +01002057/**
J-Alves8505a8a2022-06-15 18:10:18 +01002058 * Continues an operation to donate, lend or share memory to the other world VM.
2059 * If this is the last fragment then checks that the transition is valid for the
2060 * type of memory sending operation and updates the stage-2 page tables of the
2061 * sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01002062 *
2063 * Assumes that the caller has already found and locked the sender VM and copied
2064 * the memory region descriptor from the sender's TX buffer to a freshly
2065 * allocated page from Hafnium's internal pool.
2066 *
2067 * This function takes ownership of the `memory_region` passed in and will free
2068 * it when necessary; it must not be freed by the caller.
2069 */
J-Alves8505a8a2022-06-15 18:10:18 +01002070struct ffa_value ffa_memory_other_world_send_continue(
2071 struct vm_locked from_locked, struct vm_locked to_locked,
2072 void *fragment, uint32_t fragment_length, ffa_memory_handle_t handle,
2073 struct mpool *page_pool)
Andrew Walbranca808b12020-05-15 17:22:28 +01002074{
2075 struct share_states_locked share_states = share_states_lock();
2076 struct ffa_memory_share_state *share_state;
2077 struct ffa_value ret;
2078 struct ffa_memory_region *memory_region;
2079
2080 ret = ffa_memory_send_continue_validate(share_states, handle,
2081 &share_state,
2082 from_locked.vm->id, page_pool);
2083 if (ret.func != FFA_SUCCESS_32) {
2084 goto out_free_fragment;
2085 }
2086 memory_region = share_state->memory_region;
2087
J-Alves95df0ef2022-12-07 10:09:48 +00002088 if (!memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002089 dlog_error(
J-Alves8505a8a2022-06-15 18:10:18 +01002090 "Got SPM-allocated handle for memory send to non-other "
2091 "world VM. This should never happen, and indicates a "
2092 "bug.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +01002093 ret = ffa_error(FFA_INVALID_PARAMETERS);
2094 goto out_free_fragment;
2095 }
2096
2097 if (to_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
2098 to_locked.vm->mailbox.recv == NULL) {
2099 /*
J-Alves8505a8a2022-06-15 18:10:18 +01002100 * If the other world RX buffer is not available, tell the
2101 * sender to retry by returning the current offset again.
Andrew Walbranca808b12020-05-15 17:22:28 +01002102 */
2103 ret = (struct ffa_value){
2104 .func = FFA_MEM_FRAG_RX_32,
2105 .arg1 = (uint32_t)handle,
2106 .arg2 = (uint32_t)(handle >> 32),
2107 .arg3 = share_state_next_fragment_offset(share_states,
2108 share_state),
2109 };
2110 goto out_free_fragment;
2111 }
2112
2113 /* Add this fragment. */
2114 share_state->fragments[share_state->fragment_count] = fragment;
2115 share_state->fragment_constituent_counts[share_state->fragment_count] =
2116 fragment_length / sizeof(struct ffa_memory_region_constituent);
2117 share_state->fragment_count++;
2118
2119 /* Check whether the memory send operation is now ready to complete. */
2120 if (share_state_sending_complete(share_states, share_state)) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01002121 struct mpool local_page_pool;
2122 uint32_t orig_from_mode;
2123
2124 /*
2125 * Use a local page pool so that we can roll back if necessary.
2126 */
2127 mpool_init_with_fallback(&local_page_pool, page_pool);
2128
Andrew Walbranca808b12020-05-15 17:22:28 +01002129 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01002130 share_state, &local_page_pool,
2131 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01002132
2133 if (ret.func == FFA_SUCCESS_32) {
2134 /*
J-Alves8505a8a2022-06-15 18:10:18 +01002135 * Forward final fragment on to the other world so that
Andrew Walbranca808b12020-05-15 17:22:28 +01002136 * it can complete the memory sending operation.
2137 */
J-Alves8505a8a2022-06-15 18:10:18 +01002138 ret = memory_send_continue_other_world_forward(
Andrew Walbranca808b12020-05-15 17:22:28 +01002139 to_locked, from_locked.vm->id, fragment,
2140 fragment_length, handle);
2141
2142 if (ret.func != FFA_SUCCESS_32) {
2143 /*
2144 * The error will be passed on to the caller,
2145 * but log it here too.
2146 */
2147 dlog_verbose(
J-Alves8505a8a2022-06-15 18:10:18 +01002148 "other world didn't successfully "
2149 "complete "
Andrew Walbranca808b12020-05-15 17:22:28 +01002150 "memory send operation; returned %#x "
Andrew Walbran37c574e2020-06-03 11:45:46 +01002151 "(%d). Rolling back.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01002152 ret.func, ret.arg2);
Andrew Walbran37c574e2020-06-03 11:45:46 +01002153
2154 /*
J-Alves8505a8a2022-06-15 18:10:18 +01002155 * The other world failed to complete the send
Andrew Walbran37c574e2020-06-03 11:45:46 +01002156 * operation, so roll back the page table update
2157 * for the VM. This can't fail because it won't
2158 * try to allocate more memory than was freed
2159 * into the `local_page_pool` by
2160 * `ffa_send_check_update` in the initial
2161 * update.
2162 */
2163 CHECK(ffa_region_group_identity_map(
2164 from_locked, share_state->fragments,
2165 share_state
2166 ->fragment_constituent_counts,
2167 share_state->fragment_count,
2168 orig_from_mode, &local_page_pool,
2169 true));
Andrew Walbranca808b12020-05-15 17:22:28 +01002170 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002171
Andrew Walbranca808b12020-05-15 17:22:28 +01002172 /* Free share state. */
2173 share_state_free(share_states, share_state, page_pool);
2174 } else {
J-Alves8505a8a2022-06-15 18:10:18 +01002175 /* Abort sending to other world. */
2176 struct ffa_value other_world_ret =
Olivier Deprez112d2b52020-09-30 07:39:23 +02002177 arch_other_world_call((struct ffa_value){
Andrew Walbranca808b12020-05-15 17:22:28 +01002178 .func = FFA_MEM_RECLAIM_32,
2179 .arg1 = (uint32_t)handle,
2180 .arg2 = (uint32_t)(handle >> 32)});
2181
J-Alves8505a8a2022-06-15 18:10:18 +01002182 if (other_world_ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002183 /*
J-Alves8505a8a2022-06-15 18:10:18 +01002184 * Nothing we can do if other world doesn't
2185 * abort properly, just log it.
Andrew Walbranca808b12020-05-15 17:22:28 +01002186 */
2187 dlog_verbose(
J-Alves8505a8a2022-06-15 18:10:18 +01002188 "other world didn't successfully abort "
2189 "failed "
Andrew Walbranca808b12020-05-15 17:22:28 +01002190 "memory send operation; returned %#x "
2191 "(%d).\n",
J-Alves8505a8a2022-06-15 18:10:18 +01002192 other_world_ret.func,
2193 other_world_ret.arg2);
Andrew Walbranca808b12020-05-15 17:22:28 +01002194 }
2195 /*
2196 * We don't need to free the share state in this case
2197 * because ffa_memory_send_complete does that already.
2198 */
2199 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002200
2201 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01002202 } else {
2203 uint32_t next_fragment_offset =
2204 share_state_next_fragment_offset(share_states,
2205 share_state);
2206
J-Alves8505a8a2022-06-15 18:10:18 +01002207 ret = memory_send_continue_other_world_forward(
Andrew Walbranca808b12020-05-15 17:22:28 +01002208 to_locked, from_locked.vm->id, fragment,
2209 fragment_length, handle);
2210
2211 if (ret.func != FFA_MEM_FRAG_RX_32 ||
2212 ffa_frag_handle(ret) != handle ||
2213 ret.arg3 != next_fragment_offset ||
2214 ffa_frag_sender(ret) != from_locked.vm->id) {
2215 dlog_verbose(
2216 "Got unexpected result from forwarding "
J-Alves8505a8a2022-06-15 18:10:18 +01002217 "FFA_MEM_FRAG_TX to other world. %#x (handle "
2218 "%#x, "
Andrew Walbranca808b12020-05-15 17:22:28 +01002219 "offset %d, sender %d); expected "
2220 "FFA_MEM_FRAG_RX (handle %#x, offset %d, "
2221 "sender %d).\n",
2222 ret.func, ffa_frag_handle(ret), ret.arg3,
2223 ffa_frag_sender(ret), handle,
2224 next_fragment_offset, from_locked.vm->id);
2225 /* Free share state. */
2226 share_state_free(share_states, share_state, page_pool);
2227 ret = ffa_error(FFA_INVALID_PARAMETERS);
2228 goto out;
2229 }
2230
2231 ret = (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2232 .arg1 = (uint32_t)handle,
2233 .arg2 = (uint32_t)(handle >> 32),
2234 .arg3 = next_fragment_offset};
2235 }
2236 goto out;
2237
2238out_free_fragment:
2239 mpool_free(page_pool, fragment);
2240
2241out:
2242 share_states_unlock(&share_states);
2243 return ret;
2244}
2245
2246/** Clean up after the receiver has finished retrieving a memory region. */
2247static void ffa_memory_retrieve_complete(
2248 struct share_states_locked share_states,
2249 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2250{
2251 if (share_state->share_func == FFA_MEM_DONATE_32) {
2252 /*
2253 * Memory that has been donated can't be relinquished,
2254 * so no need to keep the share state around.
2255 */
2256 share_state_free(share_states, share_state, page_pool);
2257 dlog_verbose("Freed share state for donate.\n");
2258 }
2259}
2260
J-Alves96de29f2022-04-26 16:05:24 +01002261/*
2262 * Gets the receiver's access permissions from 'struct ffa_memory_region' and
2263 * returns its index in the receiver's array. If receiver's ID doesn't exist
2264 * in the array, return the region's 'receiver_count'.
2265 */
2266static uint32_t ffa_memory_region_get_receiver(
2267 struct ffa_memory_region *memory_region, ffa_vm_id_t receiver)
2268{
2269 struct ffa_memory_access *receivers;
2270 uint32_t i;
2271
2272 assert(memory_region != NULL);
2273
2274 receivers = memory_region->receivers;
2275
2276 for (i = 0U; i < memory_region->receiver_count; i++) {
2277 if (receivers[i].receiver_permissions.receiver == receiver) {
2278 break;
2279 }
2280 }
2281
2282 return i;
2283}
2284
2285/**
2286 * Validates the retrieved permissions against those specified by the lender
2287 * of memory share operation. Optionally can help set the permissions to be used
2288 * for the S2 mapping, through the `permissions` argument.
2289 * Returns true if permissions are valid, false otherwise.
2290 */
2291static bool ffa_memory_retrieve_is_memory_access_valid(
2292 enum ffa_data_access sent_data_access,
2293 enum ffa_data_access requested_data_access,
2294 enum ffa_instruction_access sent_instruction_access,
2295 enum ffa_instruction_access requested_instruction_access,
2296 ffa_memory_access_permissions_t *permissions)
2297{
2298 switch (sent_data_access) {
2299 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2300 case FFA_DATA_ACCESS_RW:
2301 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2302 requested_data_access == FFA_DATA_ACCESS_RW) {
2303 if (permissions != NULL) {
2304 ffa_set_data_access_attr(permissions,
2305 FFA_DATA_ACCESS_RW);
2306 }
2307 break;
2308 }
2309 /* Intentional fall-through. */
2310 case FFA_DATA_ACCESS_RO:
2311 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2312 requested_data_access == FFA_DATA_ACCESS_RO) {
2313 if (permissions != NULL) {
2314 ffa_set_data_access_attr(permissions,
2315 FFA_DATA_ACCESS_RO);
2316 }
2317 break;
2318 }
2319 dlog_verbose(
2320 "Invalid data access requested; sender specified "
2321 "permissions %#x but receiver requested %#x.\n",
2322 sent_data_access, requested_data_access);
2323 return false;
2324 case FFA_DATA_ACCESS_RESERVED:
2325 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
2326 "checked before this point.");
2327 }
2328
2329 switch (sent_instruction_access) {
2330 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2331 case FFA_INSTRUCTION_ACCESS_X:
2332 if (requested_instruction_access ==
2333 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2334 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
2335 if (permissions != NULL) {
2336 ffa_set_instruction_access_attr(
2337 permissions, FFA_INSTRUCTION_ACCESS_X);
2338 }
2339 break;
2340 }
2341 case FFA_INSTRUCTION_ACCESS_NX:
2342 if (requested_instruction_access ==
2343 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2344 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2345 if (permissions != NULL) {
2346 ffa_set_instruction_access_attr(
2347 permissions, FFA_INSTRUCTION_ACCESS_NX);
2348 }
2349 break;
2350 }
2351 dlog_verbose(
2352 "Invalid instruction access requested; sender "
2353 "specified permissions %#x but receiver requested "
2354 "%#x.\n",
2355 sent_instruction_access, requested_instruction_access);
2356 return false;
2357 case FFA_INSTRUCTION_ACCESS_RESERVED:
2358 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
2359 "be checked before this point.");
2360 }
2361
2362 return true;
2363}
2364
2365/**
2366 * Validate the receivers' permissions in the retrieve request against those
2367 * specified by the lender.
2368 * In the `permissions` argument returns the permissions to set at S2 for the
2369 * caller to the FFA_MEMORY_RETRIEVE_REQ.
2370 * Returns FFA_SUCCESS if all specified permissions are valid.
2371 */
2372static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
2373 struct ffa_memory_region *memory_region,
2374 struct ffa_memory_region *retrieve_request, ffa_vm_id_t to_vm_id,
2375 ffa_memory_access_permissions_t *permissions)
2376{
2377 uint32_t retrieve_receiver_index;
2378
2379 assert(permissions != NULL);
2380
2381 if (retrieve_request->receiver_count != memory_region->receiver_count) {
2382 dlog_verbose(
2383 "Retrieve request should contain same list of "
2384 "borrowers, as specified by the lender.\n");
2385 return ffa_error(FFA_INVALID_PARAMETERS);
2386 }
2387
2388 retrieve_receiver_index = retrieve_request->receiver_count;
2389
2390 /* Should be populated with the permissions of the retriever. */
2391 *permissions = 0;
2392
2393 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
2394 ffa_memory_access_permissions_t sent_permissions;
2395 struct ffa_memory_access *current_receiver =
2396 &retrieve_request->receivers[i];
2397 ffa_memory_access_permissions_t requested_permissions =
2398 current_receiver->receiver_permissions.permissions;
2399 ffa_vm_id_t current_receiver_id =
2400 current_receiver->receiver_permissions.receiver;
2401 bool found_to_id = current_receiver_id == to_vm_id;
2402
2403 /*
2404 * Find the current receiver in the transaction descriptor from
2405 * sender.
2406 */
2407 uint32_t mem_region_receiver_index =
2408 ffa_memory_region_get_receiver(memory_region,
2409 current_receiver_id);
2410
2411 if (mem_region_receiver_index ==
2412 memory_region->receiver_count) {
2413 dlog_verbose("%s: receiver %x not found\n", __func__,
2414 current_receiver_id);
2415 return ffa_error(FFA_DENIED);
2416 }
2417
2418 sent_permissions =
2419 memory_region->receivers[mem_region_receiver_index]
2420 .receiver_permissions.permissions;
2421
2422 if (found_to_id) {
2423 retrieve_receiver_index = i;
2424 }
2425
2426 /*
2427 * Since we are traversing the list of receivers, save the index
2428 * of the caller. As it needs to be there.
2429 */
2430
2431 if (current_receiver->composite_memory_region_offset != 0U) {
2432 dlog_verbose(
2433 "Retriever specified address ranges not "
2434 "supported (got offset %d).\n",
2435 current_receiver
2436 ->composite_memory_region_offset);
2437 return ffa_error(FFA_INVALID_PARAMETERS);
2438 }
2439
2440 /*
2441 * Check permissions from sender against permissions requested
2442 * by receiver.
2443 */
2444 if (!ffa_memory_retrieve_is_memory_access_valid(
2445 ffa_get_data_access_attr(sent_permissions),
2446 ffa_get_data_access_attr(requested_permissions),
2447 ffa_get_instruction_access_attr(sent_permissions),
2448 ffa_get_instruction_access_attr(
2449 requested_permissions),
2450 found_to_id ? permissions : NULL)) {
2451 return ffa_error(FFA_DENIED);
2452 }
2453
2454 /*
2455 * Can't request PM to clear memory if only provided with RO
2456 * permissions.
2457 */
2458 if (found_to_id &&
2459 (ffa_get_data_access_attr(*permissions) ==
2460 FFA_DATA_ACCESS_RO) &&
2461 (retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2462 0U) {
2463 dlog_verbose(
2464 "Receiver has RO permissions can not request "
2465 "clear.\n");
2466 return ffa_error(FFA_DENIED);
2467 }
2468 }
2469
2470 if (retrieve_receiver_index == retrieve_request->receiver_count) {
2471 dlog_verbose(
2472 "Retrieve request does not contain caller's (%x) "
2473 "permissions\n",
2474 to_vm_id);
2475 return ffa_error(FFA_INVALID_PARAMETERS);
2476 }
2477
2478 return (struct ffa_value){.func = FFA_SUCCESS_32};
2479}
2480
J-Alvesa9cd7e32022-07-01 13:49:33 +01002481/*
2482 * According to section 16.4.3 of FF-A v1.1 EAC0 specification, the hypervisor
2483 * may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region description
2484 * of a pending memory sharing operation whose allocator is the SPM, for
2485 * validation purposes before forwarding an FFA_MEM_RECLAIM call. In doing so
2486 * the memory region descriptor of the retrieve request must be zeroed with the
2487 * exception of the sender ID and handle.
2488 */
2489bool is_ffa_memory_retrieve_borrower_request(struct ffa_memory_region *request,
2490 struct vm_locked to_locked)
2491{
2492 return to_locked.vm->id == HF_HYPERVISOR_VM_ID &&
2493 request->attributes == 0U && request->flags == 0U &&
2494 request->tag == 0U && request->receiver_count == 0U &&
2495 plat_ffa_memory_handle_allocated_by_current_world(
2496 request->handle);
2497}
2498
2499/*
2500 * Helper to reset count of fragments retrieved by the hypervisor.
2501 */
2502static void ffa_memory_retrieve_complete_from_hyp(
2503 struct ffa_memory_share_state *share_state)
2504{
2505 if (share_state->hypervisor_fragment_count ==
2506 share_state->fragment_count) {
2507 share_state->hypervisor_fragment_count = 0;
2508 }
2509}
2510
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002511struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2512 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002513 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002514 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002515{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002516 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002517 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002518 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002519 sizeof(struct ffa_memory_access);
2520 ffa_memory_handle_t handle = retrieve_request->handle;
2521 ffa_memory_region_flags_t transaction_type =
Andrew Walbrana65a1322020-04-06 19:32:32 +01002522 retrieve_request->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002523 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
2524 struct ffa_memory_region *memory_region;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002525 ffa_memory_access_permissions_t permissions = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002526 uint32_t memory_to_attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002527 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002528 struct ffa_memory_share_state *share_state;
2529 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002530 struct ffa_composite_memory_region *composite;
2531 uint32_t total_length;
2532 uint32_t fragment_length;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002533 ffa_vm_id_t receiver_id;
2534 bool is_send_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002535
2536 dump_share_states();
2537
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002538 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002539 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002540 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002541 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002542 expected_retrieve_request_length,
2543 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002544 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002545 }
2546
2547 share_states = share_states_lock();
2548 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002549 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002550 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002551 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002552 goto out;
2553 }
2554
J-Alves96de29f2022-04-26 16:05:24 +01002555 if (!share_state->sending_complete) {
2556 dlog_verbose(
2557 "Memory with handle %#x not fully sent, can't "
2558 "retrieve.\n",
2559 handle);
2560 ret = ffa_error(FFA_INVALID_PARAMETERS);
2561 goto out;
2562 }
2563
Andrew Walbrana65a1322020-04-06 19:32:32 +01002564 memory_region = share_state->memory_region;
2565 CHECK(memory_region != NULL);
2566
J-Alvesa9cd7e32022-07-01 13:49:33 +01002567 receiver_id = to_locked.vm->id;
J-Alves96de29f2022-04-26 16:05:24 +01002568
J-Alvesa9cd7e32022-07-01 13:49:33 +01002569 if (!is_ffa_memory_retrieve_borrower_request(retrieve_request,
2570 to_locked)) {
2571 uint32_t receiver_index;
J-Alves614d9f42022-06-28 14:03:10 +01002572 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002573 * Find receiver index in the receivers list specified by the
2574 * sender.
J-Alves614d9f42022-06-28 14:03:10 +01002575 */
J-Alvesa9cd7e32022-07-01 13:49:33 +01002576 receiver_index = ffa_memory_region_get_receiver(
2577 memory_region, to_locked.vm->id);
2578
2579 if (receiver_index == memory_region->receiver_count) {
2580 dlog_verbose(
2581 "Incorrect receiver VM ID %x for "
2582 "FFA_MEM_RETRIEVE_REQ, "
2583 "for handle %#x.\n",
2584 to_locked.vm->id, handle);
2585 ret = ffa_error(FFA_INVALID_PARAMETERS);
2586 goto out;
2587 }
2588
2589 if (share_state->retrieved_fragment_count[receiver_index] !=
2590 0U) {
2591 dlog_verbose(
2592 "Memory with handle %#x already retrieved.\n",
2593 handle);
2594 ret = ffa_error(FFA_DENIED);
2595 goto out;
2596 }
2597
2598 /*
2599 * Check that the transaction type expected by the receiver is
2600 * correct, if it has been specified.
2601 */
2602 if (transaction_type !=
2603 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
2604 transaction_type !=
2605 (memory_region->flags &
2606 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
2607 dlog_verbose(
2608 "Incorrect transaction type %#x for "
2609 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle "
2610 "%#x.\n",
2611 transaction_type,
2612 memory_region->flags &
2613 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
2614 handle);
2615 ret = ffa_error(FFA_INVALID_PARAMETERS);
2616 goto out;
2617 }
2618
2619 if (retrieve_request->sender != memory_region->sender) {
2620 dlog_verbose(
2621 "Incorrect sender ID %d for "
2622 "FFA_MEM_RETRIEVE_REQ, "
2623 "expected %d for handle %#x.\n",
2624 retrieve_request->sender, memory_region->sender,
2625 handle);
2626 ret = ffa_error(FFA_DENIED);
2627 goto out;
2628 }
2629
2630 if (retrieve_request->tag != memory_region->tag) {
2631 dlog_verbose(
2632 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, "
2633 "expected "
2634 "%d for handle %#x.\n",
2635 retrieve_request->tag, memory_region->tag,
2636 handle);
2637 ret = ffa_error(FFA_INVALID_PARAMETERS);
2638 goto out;
2639 }
2640
2641 if ((retrieve_request->flags &
2642 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
2643 dlog_verbose(
2644 "Retriever specified 'address range alignment "
2645 "hint' not supported.\n");
2646 ret = ffa_error(FFA_INVALID_PARAMETERS);
2647 goto out;
2648 }
2649
2650 if ((retrieve_request->flags &
2651 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
2652 dlog_verbose(
2653 "Bits 8-5 must be zero in memory region's "
2654 "flags (address range alignment hint not "
2655 "supported).\n");
2656 ret = ffa_error(FFA_INVALID_PARAMETERS);
2657 goto out;
2658 }
2659
2660 if ((retrieve_request->flags & ~0x7FF) != 0U) {
2661 dlog_verbose(
2662 "Bits 31-10 must be zero in memory region's "
2663 "flags.\n");
2664 ret = ffa_error(FFA_INVALID_PARAMETERS);
2665 goto out;
2666 }
2667
2668 if (share_state->share_func == FFA_MEM_SHARE_32 &&
2669 (retrieve_request->flags &
2670 (FFA_MEMORY_REGION_FLAG_CLEAR |
2671 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
2672 dlog_verbose(
2673 "Memory Share operation can't clean after "
2674 "relinquish "
2675 "memory region.\n");
2676 ret = ffa_error(FFA_INVALID_PARAMETERS);
2677 goto out;
2678 }
2679
2680 /*
2681 * If the borrower needs the memory to be cleared before mapping
2682 * to its address space, the sender should have set the flag
2683 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
2684 * FFA_DENIED.
2685 */
2686 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2687 0U &&
2688 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) ==
2689 0U) {
2690 dlog_verbose(
2691 "Borrower needs memory cleared. Sender needs "
2692 "to set "
2693 "flag for clearing memory.\n");
2694 ret = ffa_error(FFA_DENIED);
2695 goto out;
2696 }
2697
2698 ret = ffa_memory_retrieve_validate_memory_access_list(
2699 memory_region, retrieve_request, receiver_id,
2700 &permissions);
J-Alves614d9f42022-06-28 14:03:10 +01002701 if (ret.func != FFA_SUCCESS_32) {
2702 goto out;
2703 }
Federico Recanatia98603a2021-12-20 18:04:03 +01002704
J-Alvesa9cd7e32022-07-01 13:49:33 +01002705 if (ffa_get_memory_type_attr(retrieve_request->attributes) !=
2706 FFA_MEMORY_NOT_SPECIFIED_MEM) {
2707 /*
2708 * Ensure receiver's attributes are compatible with how
2709 * Hafnium maps memory: Normal Memory, Inner shareable,
2710 * Write-Back Read-Allocate Write-Allocate Cacheable.
2711 */
2712 ret = ffa_memory_attributes_validate(
2713 retrieve_request->attributes);
2714 if (ret.func != FFA_SUCCESS_32) {
2715 goto out;
2716 }
2717 }
2718
2719 memory_to_attributes = ffa_memory_permissions_to_mode(
2720 permissions, share_state->sender_orig_mode);
2721 ret = ffa_retrieve_check_update(
2722 to_locked, memory_region->sender,
2723 share_state->fragments,
2724 share_state->fragment_constituent_counts,
2725 share_state->fragment_count, memory_to_attributes,
2726 share_state->share_func, false, page_pool);
2727
2728 if (ret.func != FFA_SUCCESS_32) {
2729 goto out;
2730 }
2731
2732 share_state->retrieved_fragment_count[receiver_index] = 1;
2733 is_send_complete =
2734 share_state->retrieved_fragment_count[receiver_index] ==
2735 share_state->fragment_count;
2736 } else {
2737 if (share_state->hypervisor_fragment_count != 0U) {
2738 dlog_verbose(
2739 "Memory with handle %#x already "
2740 "retrieved by "
2741 "the hypervisor.\n",
2742 handle);
2743 ret = ffa_error(FFA_DENIED);
2744 goto out;
2745 }
2746
2747 share_state->hypervisor_fragment_count = 1;
2748
2749 ffa_memory_retrieve_complete_from_hyp(share_state);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002750 }
2751
2752 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002753 * Copy response to RX buffer of caller and deliver the message.
2754 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002755 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002756 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002757 composite = ffa_memory_region_get_composite(memory_region, 0);
2758 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002759 * Constituents which we received in the first fragment should
2760 * always fit in the first fragment we are sending, because the
2761 * header is the same size in both cases and we have a fixed
2762 * message buffer size. So `ffa_retrieved_memory_region_init`
2763 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01002764 */
2765 CHECK(ffa_retrieved_memory_region_init(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002766 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2767 memory_region->sender, memory_region->attributes,
J-Alvesa9cd7e32022-07-01 13:49:33 +01002768 memory_region->flags, handle, receiver_id, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +01002769 composite->page_count, composite->constituent_count,
2770 share_state->fragments[0],
2771 share_state->fragment_constituent_counts[0], &total_length,
2772 &fragment_length));
2773 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002774 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002775 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002776 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2777
J-Alvesa9cd7e32022-07-01 13:49:33 +01002778 if (is_send_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002779 ffa_memory_retrieve_complete(share_states, share_state,
2780 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002781 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002782 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002783 .arg1 = total_length,
2784 .arg2 = fragment_length};
2785
2786out:
2787 share_states_unlock(&share_states);
2788 dump_share_states();
2789 return ret;
2790}
2791
2792struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2793 ffa_memory_handle_t handle,
2794 uint32_t fragment_offset,
2795 struct mpool *page_pool)
2796{
2797 struct ffa_memory_region *memory_region;
2798 struct share_states_locked share_states;
2799 struct ffa_memory_share_state *share_state;
2800 struct ffa_value ret;
2801 uint32_t fragment_index;
2802 uint32_t retrieved_constituents_count;
2803 uint32_t i;
2804 uint32_t expected_fragment_offset;
2805 uint32_t remaining_constituent_count;
2806 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01002807 uint32_t receiver_index;
Andrew Walbranca808b12020-05-15 17:22:28 +01002808
2809 dump_share_states();
2810
2811 share_states = share_states_lock();
2812 if (!get_share_state(share_states, handle, &share_state)) {
2813 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2814 handle);
2815 ret = ffa_error(FFA_INVALID_PARAMETERS);
2816 goto out;
2817 }
2818
2819 memory_region = share_state->memory_region;
2820 CHECK(memory_region != NULL);
2821
J-Alvesc7484f12022-05-13 12:41:14 +01002822 receiver_index =
2823 ffa_memory_region_get_receiver(memory_region, to_locked.vm->id);
2824
2825 if (receiver_index == memory_region->receiver_count) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002826 dlog_verbose(
J-Alvesc7484f12022-05-13 12:41:14 +01002827 "Caller of FFA_MEM_FRAG_RX (%x) is not a borrower to "
2828 "memory sharing transaction (%x)\n",
2829 to_locked.vm->id, handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01002830 ret = ffa_error(FFA_INVALID_PARAMETERS);
2831 goto out;
2832 }
2833
2834 if (!share_state->sending_complete) {
2835 dlog_verbose(
2836 "Memory with handle %#x not fully sent, can't "
2837 "retrieve.\n",
2838 handle);
2839 ret = ffa_error(FFA_INVALID_PARAMETERS);
2840 goto out;
2841 }
2842
J-Alvesc7484f12022-05-13 12:41:14 +01002843 if (share_state->retrieved_fragment_count[receiver_index] == 0 ||
2844 share_state->retrieved_fragment_count[receiver_index] >=
Andrew Walbranca808b12020-05-15 17:22:28 +01002845 share_state->fragment_count) {
2846 dlog_verbose(
2847 "Retrieval of memory with handle %#x not yet started "
2848 "or already completed (%d/%d fragments retrieved).\n",
J-Alvesc7484f12022-05-13 12:41:14 +01002849 handle,
2850 share_state->retrieved_fragment_count[receiver_index],
Andrew Walbranca808b12020-05-15 17:22:28 +01002851 share_state->fragment_count);
2852 ret = ffa_error(FFA_INVALID_PARAMETERS);
2853 goto out;
2854 }
2855
J-Alvesc7484f12022-05-13 12:41:14 +01002856 fragment_index = share_state->retrieved_fragment_count[receiver_index];
Andrew Walbranca808b12020-05-15 17:22:28 +01002857
2858 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002859 * Check that the given fragment offset is correct by counting
2860 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01002861 */
2862 retrieved_constituents_count = 0;
2863 for (i = 0; i < fragment_index; ++i) {
2864 retrieved_constituents_count +=
2865 share_state->fragment_constituent_counts[i];
2866 }
J-Alvesc7484f12022-05-13 12:41:14 +01002867
2868 CHECK(memory_region->receiver_count > 0);
2869
Andrew Walbranca808b12020-05-15 17:22:28 +01002870 expected_fragment_offset =
J-Alvesc7484f12022-05-13 12:41:14 +01002871 ffa_composite_constituent_offset(memory_region,
2872 receiver_index) +
Andrew Walbranca808b12020-05-15 17:22:28 +01002873 retrieved_constituents_count *
J-Alvesc7484f12022-05-13 12:41:14 +01002874 sizeof(struct ffa_memory_region_constituent) -
2875 sizeof(struct ffa_memory_access) *
2876 (memory_region->receiver_count - 1);
Andrew Walbranca808b12020-05-15 17:22:28 +01002877 if (fragment_offset != expected_fragment_offset) {
2878 dlog_verbose("Fragment offset was %d but expected %d.\n",
2879 fragment_offset, expected_fragment_offset);
2880 ret = ffa_error(FFA_INVALID_PARAMETERS);
2881 goto out;
2882 }
2883
2884 remaining_constituent_count = ffa_memory_fragment_init(
2885 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2886 share_state->fragments[fragment_index],
2887 share_state->fragment_constituent_counts[fragment_index],
2888 &fragment_length);
2889 CHECK(remaining_constituent_count == 0);
2890 to_locked.vm->mailbox.recv_size = fragment_length;
2891 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2892 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
2893 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
J-Alvesc7484f12022-05-13 12:41:14 +01002894 share_state->retrieved_fragment_count[receiver_index]++;
2895 if (share_state->retrieved_fragment_count[receiver_index] ==
Andrew Walbranca808b12020-05-15 17:22:28 +01002896 share_state->fragment_count) {
2897 ffa_memory_retrieve_complete(share_states, share_state,
2898 page_pool);
2899 }
2900
2901 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2902 .arg1 = (uint32_t)handle,
2903 .arg2 = (uint32_t)(handle >> 32),
2904 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002905
2906out:
2907 share_states_unlock(&share_states);
2908 dump_share_states();
2909 return ret;
2910}
2911
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002912struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002913 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002914 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002915{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002916 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002917 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002918 struct ffa_memory_share_state *share_state;
2919 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002920 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002921 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01002922 uint32_t receiver_index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002923
Andrew Walbrana65a1322020-04-06 19:32:32 +01002924 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002925 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002926 "Stream endpoints not supported (got %d "
2927 "endpoints on "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002928 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002929 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002930 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002931 }
2932
Andrew Walbrana65a1322020-04-06 19:32:32 +01002933 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002934 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002935 "VM ID %d in relinquish message doesn't match "
2936 "calling "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002937 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002938 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002939 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002940 }
2941
2942 dump_share_states();
2943
2944 share_states = share_states_lock();
2945 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002946 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002947 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002948 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002949 goto out;
2950 }
2951
Andrew Walbranca808b12020-05-15 17:22:28 +01002952 if (!share_state->sending_complete) {
2953 dlog_verbose(
2954 "Memory with handle %#x not fully sent, can't "
2955 "relinquish.\n",
2956 handle);
2957 ret = ffa_error(FFA_INVALID_PARAMETERS);
2958 goto out;
2959 }
2960
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002961 memory_region = share_state->memory_region;
2962 CHECK(memory_region != NULL);
2963
J-Alves8eb19162022-04-28 10:56:48 +01002964 receiver_index = ffa_memory_region_get_receiver(memory_region,
2965 from_locked.vm->id);
2966
2967 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002968 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002969 "VM ID %d tried to relinquish memory region "
2970 "with "
J-Alves8eb19162022-04-28 10:56:48 +01002971 "handle %#x and it is not a valid borrower.\n",
2972 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002973 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002974 goto out;
2975 }
2976
J-Alves8eb19162022-04-28 10:56:48 +01002977 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01002978 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002979 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002980 "Memory with handle %#x not yet fully "
2981 "retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01002982 "receiver %x can't relinquish.\n",
2983 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002984 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002985 goto out;
2986 }
2987
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002988 clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002989
2990 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002991 * Clear is not allowed for memory that was shared, as the
2992 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002993 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002994 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002995 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002996 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002997 goto out;
2998 }
2999
Andrew Walbranca808b12020-05-15 17:22:28 +01003000 ret = ffa_relinquish_check_update(
3001 from_locked, share_state->fragments,
3002 share_state->fragment_constituent_counts,
3003 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003004
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003005 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003006 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003007 * Mark memory handle as not retrieved, so it can be
3008 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003009 */
J-Alves8eb19162022-04-28 10:56:48 +01003010 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003011 }
3012
3013out:
3014 share_states_unlock(&share_states);
3015 dump_share_states();
3016 return ret;
3017}
3018
3019/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01003020 * Validates that the reclaim transition is allowed for the given
3021 * handle, updates the page table of the reclaiming VM, and frees the
3022 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003023 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003024struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01003025 ffa_memory_handle_t handle,
3026 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003027 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003028{
3029 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003030 struct ffa_memory_share_state *share_state;
3031 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003032 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003033
3034 dump_share_states();
3035
3036 share_states = share_states_lock();
3037 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003038 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003039 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003040 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003041 goto out;
3042 }
3043
3044 memory_region = share_state->memory_region;
3045 CHECK(memory_region != NULL);
3046
J-Alvesa9cd7e32022-07-01 13:49:33 +01003047 if (vm_id_is_current_world(to_locked.vm->id) &&
3048 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003049 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01003050 "VM %#x attempted to reclaim memory handle %#x "
3051 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003052 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003053 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003054 goto out;
3055 }
3056
Andrew Walbranca808b12020-05-15 17:22:28 +01003057 if (!share_state->sending_complete) {
3058 dlog_verbose(
3059 "Memory with handle %#x not fully sent, can't "
3060 "reclaim.\n",
3061 handle);
3062 ret = ffa_error(FFA_INVALID_PARAMETERS);
3063 goto out;
3064 }
3065
J-Alves752236c2022-04-28 11:07:47 +01003066 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3067 if (share_state->retrieved_fragment_count[i] != 0) {
3068 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003069 "Tried to reclaim memory handle %#x "
3070 "that has "
3071 "not been relinquished by all "
3072 "borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01003073 handle,
3074 memory_region->receivers[i]
3075 .receiver_permissions.receiver);
3076 ret = ffa_error(FFA_DENIED);
3077 goto out;
3078 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003079 }
3080
Andrew Walbranca808b12020-05-15 17:22:28 +01003081 ret = ffa_retrieve_check_update(
J-Alves7db32002021-12-14 14:44:50 +00003082 to_locked, memory_region->sender, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003083 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00003084 share_state->fragment_count, share_state->sender_orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01003085 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003086
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003087 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003088 share_state_free(share_states, share_state, page_pool);
J-Alvesa9cd7e32022-07-01 13:49:33 +01003089 dlog_verbose(
3090 "Freed share state after successful "
3091 "reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003092 }
3093
3094out:
3095 share_states_unlock(&share_states);
3096 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01003097}
Andrew Walbran290b0c92020-02-03 16:37:14 +00003098
3099/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01003100 * Validates that the reclaim transition is allowed for the memory
3101 * region with the given handle which was previously shared with the
3102 * other world. Tells the other world to mark it as reclaimed, and
3103 * updates the page table of the reclaiming VM.
Andrew Walbranca808b12020-05-15 17:22:28 +01003104 *
J-Alvesa9cd7e32022-07-01 13:49:33 +01003105 * To do this information about the memory region is first fetched from
3106 * the other world.
Andrew Walbran290b0c92020-02-03 16:37:14 +00003107 */
J-Alves8505a8a2022-06-15 18:10:18 +01003108struct ffa_value ffa_memory_other_world_reclaim(struct vm_locked to_locked,
3109 struct vm_locked from_locked,
3110 ffa_memory_handle_t handle,
3111 ffa_memory_region_flags_t flags,
3112 struct mpool *page_pool)
Andrew Walbran290b0c92020-02-03 16:37:14 +00003113{
Andrew Walbranca808b12020-05-15 17:22:28 +01003114 uint32_t request_length = ffa_memory_lender_retrieve_request_init(
3115 from_locked.vm->mailbox.recv, handle, to_locked.vm->id);
J-Alves8505a8a2022-06-15 18:10:18 +01003116 struct ffa_value other_world_ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01003117 uint32_t length;
3118 uint32_t fragment_length;
3119 uint32_t fragment_offset;
3120 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003121 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01003122 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
3123
3124 CHECK(request_length <= HF_MAILBOX_SIZE);
J-Alves8505a8a2022-06-15 18:10:18 +01003125 CHECK(from_locked.vm->id == HF_OTHER_WORLD_ID);
Andrew Walbranca808b12020-05-15 17:22:28 +01003126
J-Alves8505a8a2022-06-15 18:10:18 +01003127 /* Retrieve memory region information from the other world. */
3128 other_world_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01003129 (struct ffa_value){.func = FFA_MEM_RETRIEVE_REQ_32,
3130 .arg1 = request_length,
3131 .arg2 = request_length});
J-Alves8505a8a2022-06-15 18:10:18 +01003132 if (other_world_ret.func == FFA_ERROR_32) {
3133 dlog_verbose("Got error %d from EL3.\n", other_world_ret.arg2);
3134 return other_world_ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01003135 }
J-Alves8505a8a2022-06-15 18:10:18 +01003136 if (other_world_ret.func != FFA_MEM_RETRIEVE_RESP_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003137 dlog_verbose(
3138 "Got %#x from EL3, expected FFA_MEM_RETRIEVE_RESP.\n",
J-Alves8505a8a2022-06-15 18:10:18 +01003139 other_world_ret.func);
Andrew Walbranca808b12020-05-15 17:22:28 +01003140 return ffa_error(FFA_INVALID_PARAMETERS);
3141 }
3142
J-Alves8505a8a2022-06-15 18:10:18 +01003143 length = other_world_ret.arg1;
3144 fragment_length = other_world_ret.arg2;
Andrew Walbranca808b12020-05-15 17:22:28 +01003145
3146 if (fragment_length > HF_MAILBOX_SIZE || fragment_length > length ||
J-Alves8505a8a2022-06-15 18:10:18 +01003147 length > sizeof(other_world_retrieve_buffer)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003148 dlog_verbose("Invalid fragment length %d/%d (max %d/%d).\n",
3149 fragment_length, length, HF_MAILBOX_SIZE,
J-Alves8505a8a2022-06-15 18:10:18 +01003150 sizeof(other_world_retrieve_buffer));
Andrew Walbranca808b12020-05-15 17:22:28 +01003151 return ffa_error(FFA_INVALID_PARAMETERS);
3152 }
3153
3154 /*
3155 * Copy the first fragment of the memory region descriptor to an
3156 * internal buffer.
3157 */
J-Alves8505a8a2022-06-15 18:10:18 +01003158 memcpy_s(other_world_retrieve_buffer,
3159 sizeof(other_world_retrieve_buffer),
Andrew Walbranca808b12020-05-15 17:22:28 +01003160 from_locked.vm->mailbox.send, fragment_length);
3161
3162 /* Fetch the remaining fragments into the same buffer. */
3163 fragment_offset = fragment_length;
3164 while (fragment_offset < length) {
J-Alves8505a8a2022-06-15 18:10:18 +01003165 other_world_ret = arch_other_world_call(
Andrew Walbranca808b12020-05-15 17:22:28 +01003166 (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
3167 .arg1 = (uint32_t)handle,
3168 .arg2 = (uint32_t)(handle >> 32),
3169 .arg3 = fragment_offset});
J-Alves8505a8a2022-06-15 18:10:18 +01003170 if (other_world_ret.func != FFA_MEM_FRAG_TX_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003171 dlog_verbose(
J-Alves8505a8a2022-06-15 18:10:18 +01003172 "Got %#x (%d) from other world in response to "
Andrew Walbranca808b12020-05-15 17:22:28 +01003173 "FFA_MEM_FRAG_RX, expected FFA_MEM_FRAG_TX.\n",
J-Alves8505a8a2022-06-15 18:10:18 +01003174 other_world_ret.func, other_world_ret.arg2);
3175 return other_world_ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01003176 }
J-Alves8505a8a2022-06-15 18:10:18 +01003177 if (ffa_frag_handle(other_world_ret) != handle) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003178 dlog_verbose(
3179 "Got FFA_MEM_FRAG_TX for unexpected handle %#x "
3180 "in response to FFA_MEM_FRAG_RX for handle "
3181 "%#x.\n",
J-Alves8505a8a2022-06-15 18:10:18 +01003182 ffa_frag_handle(other_world_ret), handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01003183 return ffa_error(FFA_INVALID_PARAMETERS);
3184 }
J-Alves8505a8a2022-06-15 18:10:18 +01003185 if (ffa_frag_sender(other_world_ret) != 0) {
Andrew Walbranca808b12020-05-15 17:22:28 +01003186 dlog_verbose(
3187 "Got FFA_MEM_FRAG_TX with unexpected sender %d "
3188 "(expected 0).\n",
J-Alves8505a8a2022-06-15 18:10:18 +01003189 ffa_frag_sender(other_world_ret));
Andrew Walbranca808b12020-05-15 17:22:28 +01003190 return ffa_error(FFA_INVALID_PARAMETERS);
3191 }
J-Alves8505a8a2022-06-15 18:10:18 +01003192 fragment_length = other_world_ret.arg3;
Andrew Walbranca808b12020-05-15 17:22:28 +01003193 if (fragment_length > HF_MAILBOX_SIZE ||
3194 fragment_offset + fragment_length > length) {
3195 dlog_verbose(
3196 "Invalid fragment length %d at offset %d (max "
3197 "%d).\n",
3198 fragment_length, fragment_offset,
3199 HF_MAILBOX_SIZE);
3200 return ffa_error(FFA_INVALID_PARAMETERS);
3201 }
J-Alves8505a8a2022-06-15 18:10:18 +01003202 memcpy_s(other_world_retrieve_buffer + fragment_offset,
3203 sizeof(other_world_retrieve_buffer) - fragment_offset,
Andrew Walbranca808b12020-05-15 17:22:28 +01003204 from_locked.vm->mailbox.send, fragment_length);
3205
3206 fragment_offset += fragment_length;
3207 }
3208
J-Alves8505a8a2022-06-15 18:10:18 +01003209 memory_region = (struct ffa_memory_region *)other_world_retrieve_buffer;
Andrew Walbran290b0c92020-02-03 16:37:14 +00003210
3211 if (memory_region->receiver_count != 1) {
3212 /* Only one receiver supported by Hafnium for now. */
3213 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003214 "Multiple recipients not supported (got %d, "
3215 "expected 1).\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00003216 memory_region->receiver_count);
Andrew Walbranca808b12020-05-15 17:22:28 +01003217 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003218 }
3219
3220 if (memory_region->handle != handle) {
3221 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003222 "Got memory region handle %#x from other world "
3223 "but requested handle %#x.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00003224 memory_region->handle, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003225 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003226 }
3227
3228 /* The original sender must match the caller. */
3229 if (to_locked.vm->id != memory_region->sender) {
3230 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01003231 "VM %#x attempted to reclaim memory handle %#x "
3232 "originally sent by VM %#x.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00003233 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003234 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003235 }
3236
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003237 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003238
3239 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003240 * Validate that the reclaim transition is allowed for the given
3241 * memory region, forward the request to the other world and
3242 * then map the memory back into the caller's stage-2 page
3243 * table.
Andrew Walbran290b0c92020-02-03 16:37:14 +00003244 */
J-Alves8505a8a2022-06-15 18:10:18 +01003245 return ffa_other_world_reclaim_check_update(
Andrew Walbran996d1d12020-05-27 14:08:43 +01003246 to_locked, handle, composite->constituents,
Andrew Walbranca808b12020-05-15 17:22:28 +01003247 composite->constituent_count, memory_to_attributes,
3248 flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00003249}