blob: 58f39aed67e696aef06e30ee8e43c4450368557d [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
J-Alves5952d942022-12-22 16:03:00 +000015#include "hf/addr.h"
Jose Marinho75509b42019-04-09 09:34:59 +010016#include "hf/api.h"
Daniel Boulbya2f8c662021-11-26 17:52:53 +000017#include "hf/assert.h"
Jose Marinho09b1db82019-08-08 09:16:59 +010018#include "hf/check.h"
Jose Marinho75509b42019-04-09 09:34:59 +010019#include "hf/dlog.h"
J-Alves3456e032023-07-20 12:20:05 +010020#include "hf/ffa.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010021#include "hf/ffa_internal.h"
J-Alves66652252022-07-06 09:49:51 +010022#include "hf/ffa_memory_internal.h"
J-Alves3456e032023-07-20 12:20:05 +010023#include "hf/ffa_partition_manifest.h"
J-Alves5952d942022-12-22 16:03:00 +000024#include "hf/mm.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000025#include "hf/mpool.h"
Jose Marinho75509b42019-04-09 09:34:59 +010026#include "hf/std.h"
Andrew Scull3c257452019-11-26 13:32:50 +000027#include "hf/vm.h"
Jose Marinho75509b42019-04-09 09:34:59 +010028
J-Alves2d8457f2022-10-05 11:06:41 +010029#include "vmapi/hf/ffa_v1_0.h"
30
J-Alves5da37d92022-10-24 16:33:48 +010031#define RECEIVERS_COUNT_IN_RETRIEVE_RESP 1
32
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000033/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010034 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000035 * by this lock.
36 */
37static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010038static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000039
40/**
J-Alvesed508c82023-05-04 16:09:48 +010041 * Return the offset to the first constituent within the
42 * `ffa_composite_memory_region` for the given receiver from an
43 * `ffa_memory_region`. The caller must check that the receiver_index is within
44 * bounds, and that it has a composite memory region offset.
45 */
46static uint32_t ffa_composite_constituent_offset(
47 struct ffa_memory_region *memory_region, uint32_t receiver_index)
48{
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +000049 struct ffa_memory_access *receiver;
50 uint32_t composite_offset;
J-Alvesed508c82023-05-04 16:09:48 +010051
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +000052 CHECK(receiver_index < memory_region->receiver_count);
53
54 receiver =
55 ffa_memory_region_get_receiver(memory_region, receiver_index);
56 CHECK(receiver != NULL);
57
58 composite_offset = receiver->composite_memory_region_offset;
59
60 CHECK(composite_offset != 0);
61
62 return composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alvesed508c82023-05-04 16:09:48 +010063}
64
65/**
J-Alves917d2f22020-10-30 18:39:30 +000066 * Extracts the index from a memory handle allocated by Hafnium's current world.
67 */
68uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle)
69{
70 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
71}
72
73/**
Karl Meakin52cdfe72023-06-30 14:49:10 +010074 * Initialises the next available `struct ffa_memory_share_state`. If `handle`
75 * is `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle,
76 * otherwise uses the provided handle which is assumed to be globally unique.
Andrew Walbranca808b12020-05-15 17:22:28 +010077 *
Karl Meakin52cdfe72023-06-30 14:49:10 +010078 * Returns a pointer to the allocated `ffa_memory_share_state` on success or
79 * `NULL` if none are available.
Andrew Walbranca808b12020-05-15 17:22:28 +010080 */
Karl Meakin52cdfe72023-06-30 14:49:10 +010081struct ffa_memory_share_state *allocate_share_state(
82 struct share_states_locked share_states, uint32_t share_func,
83 struct ffa_memory_region *memory_region, uint32_t fragment_length,
84 ffa_memory_handle_t handle)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000085{
Daniel Boulbya2f8c662021-11-26 17:52:53 +000086 assert(share_states.share_states != NULL);
87 assert(memory_region != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000088
Karl Meakin52cdfe72023-06-30 14:49:10 +010089 for (uint64_t i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +010090 if (share_states.share_states[i].share_func == 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010091 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +010092 &share_states.share_states[i];
93 struct ffa_composite_memory_region *composite =
94 ffa_memory_region_get_composite(memory_region,
95 0);
96
97 if (handle == FFA_MEMORY_HANDLE_INVALID) {
J-Alvesee68c542020-10-29 17:48:20 +000098 memory_region->handle =
Olivier Deprez55a189e2021-06-09 15:45:27 +020099 plat_ffa_memory_handle_make(i);
Andrew Walbranca808b12020-05-15 17:22:28 +0100100 } else {
J-Alvesee68c542020-10-29 17:48:20 +0000101 memory_region->handle = handle;
Andrew Walbranca808b12020-05-15 17:22:28 +0100102 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000103 allocated_state->share_func = share_func;
104 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +0100105 allocated_state->fragment_count = 1;
106 allocated_state->fragments[0] = composite->constituents;
107 allocated_state->fragment_constituent_counts[0] =
108 (fragment_length -
109 ffa_composite_constituent_offset(memory_region,
110 0)) /
111 sizeof(struct ffa_memory_region_constituent);
112 allocated_state->sending_complete = false;
Karl Meakin52cdfe72023-06-30 14:49:10 +0100113 for (uint32_t j = 0; j < MAX_MEM_SHARE_RECIPIENTS;
114 ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100115 allocated_state->retrieved_fragment_count[j] =
116 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000117 }
Karl Meakin52cdfe72023-06-30 14:49:10 +0100118 return allocated_state;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000119 }
120 }
121
Karl Meakin52cdfe72023-06-30 14:49:10 +0100122 return NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000123}
124
125/** Locks the share states lock. */
126struct share_states_locked share_states_lock(void)
127{
128 sl_lock(&share_states_lock_instance);
129
130 return (struct share_states_locked){.share_states = share_states};
131}
132
133/** Unlocks the share states lock. */
J-Alves66652252022-07-06 09:49:51 +0100134void share_states_unlock(struct share_states_locked *share_states)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000135{
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000136 assert(share_states->share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000137 share_states->share_states = NULL;
138 sl_unlock(&share_states_lock_instance);
139}
140
141/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100142 * If the given handle is a valid handle for an allocated share state then
Karl Meakin4a2854a2023-06-30 16:26:52 +0100143 * returns a pointer to the share state. Otherwise returns NULL.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000144 */
Karl Meakin4a2854a2023-06-30 16:26:52 +0100145struct ffa_memory_share_state *get_share_state(
146 struct share_states_locked share_states, ffa_memory_handle_t handle)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000147{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100148 struct ffa_memory_share_state *share_state;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000149
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000150 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100151
152 /*
153 * First look for a share_state allocated by us, in which case the
154 * handle is based on the index.
155 */
Olivier Deprez55a189e2021-06-09 15:45:27 +0200156 if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100157 uint64_t index = ffa_memory_handle_get_index(handle);
158
Andrew Walbranca808b12020-05-15 17:22:28 +0100159 if (index < MAX_MEM_SHARES) {
160 share_state = &share_states.share_states[index];
161 if (share_state->share_func != 0) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100162 return share_state;
Andrew Walbranca808b12020-05-15 17:22:28 +0100163 }
164 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000165 }
166
Andrew Walbranca808b12020-05-15 17:22:28 +0100167 /* Fall back to a linear scan. */
Karl Meakin4a2854a2023-06-30 16:26:52 +0100168 for (uint64_t index = 0; index < MAX_MEM_SHARES; ++index) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100169 share_state = &share_states.share_states[index];
J-Alvesee68c542020-10-29 17:48:20 +0000170 if (share_state->memory_region != NULL &&
171 share_state->memory_region->handle == handle &&
Andrew Walbranca808b12020-05-15 17:22:28 +0100172 share_state->share_func != 0) {
Karl Meakin4a2854a2023-06-30 16:26:52 +0100173 return share_state;
Andrew Walbranca808b12020-05-15 17:22:28 +0100174 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000175 }
176
Karl Meakin4a2854a2023-06-30 16:26:52 +0100177 return NULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000178}
179
180/** Marks a share state as unallocated. */
J-Alvesfdd29272022-07-19 13:16:31 +0100181void share_state_free(struct share_states_locked share_states,
182 struct ffa_memory_share_state *share_state,
183 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000184{
Andrew Walbranca808b12020-05-15 17:22:28 +0100185 uint32_t i;
186
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000187 assert(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000188 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100189 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000190 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100191 /*
192 * First fragment is part of the same page as the `memory_region`, so it
193 * doesn't need to be freed separately.
194 */
195 share_state->fragments[0] = NULL;
196 share_state->fragment_constituent_counts[0] = 0;
197 for (i = 1; i < share_state->fragment_count; ++i) {
198 mpool_free(page_pool, share_state->fragments[i]);
199 share_state->fragments[i] = NULL;
200 share_state->fragment_constituent_counts[i] = 0;
201 }
202 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000203 share_state->memory_region = NULL;
J-Alvesa9cd7e32022-07-01 13:49:33 +0100204 share_state->hypervisor_fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000205}
206
Andrew Walbranca808b12020-05-15 17:22:28 +0100207/** Checks whether the given share state has been fully sent. */
J-Alvesfdd29272022-07-19 13:16:31 +0100208bool share_state_sending_complete(struct share_states_locked share_states,
209 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000210{
Andrew Walbranca808b12020-05-15 17:22:28 +0100211 struct ffa_composite_memory_region *composite;
212 uint32_t expected_constituent_count;
213 uint32_t fragment_constituent_count_total = 0;
214 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000215
Andrew Walbranca808b12020-05-15 17:22:28 +0100216 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000217 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100218
219 /*
220 * Share state must already be valid, or it's not possible to get hold
221 * of it.
222 */
223 CHECK(share_state->memory_region != NULL &&
224 share_state->share_func != 0);
225
226 composite =
227 ffa_memory_region_get_composite(share_state->memory_region, 0);
228 expected_constituent_count = composite->constituent_count;
229 for (i = 0; i < share_state->fragment_count; ++i) {
230 fragment_constituent_count_total +=
231 share_state->fragment_constituent_counts[i];
232 }
233 dlog_verbose(
234 "Checking completion: constituent count %d/%d from %d "
235 "fragments.\n",
236 fragment_constituent_count_total, expected_constituent_count,
237 share_state->fragment_count);
238
239 return fragment_constituent_count_total == expected_constituent_count;
240}
241
242/**
243 * Calculates the offset of the next fragment expected for the given share
244 * state.
245 */
J-Alvesfdd29272022-07-19 13:16:31 +0100246uint32_t share_state_next_fragment_offset(
Andrew Walbranca808b12020-05-15 17:22:28 +0100247 struct share_states_locked share_states,
248 struct ffa_memory_share_state *share_state)
249{
250 uint32_t next_fragment_offset;
251 uint32_t i;
252
253 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +0000254 assert(share_states.share_states != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +0100255
256 next_fragment_offset =
257 ffa_composite_constituent_offset(share_state->memory_region, 0);
258 for (i = 0; i < share_state->fragment_count; ++i) {
259 next_fragment_offset +=
260 share_state->fragment_constituent_counts[i] *
261 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000262 }
263
Andrew Walbranca808b12020-05-15 17:22:28 +0100264 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000265}
266
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100267static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000268{
269 uint32_t i;
270
271 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
272 return;
273 }
274
Olivier Deprez935e1b12020-12-22 18:01:29 +0100275 dlog("from VM %#x, attributes %#x, flags %#x, tag %u, to "
Olivier Deprezf92e5d42020-11-13 16:00:54 +0100276 "%u "
Andrew Walbrana65a1322020-04-06 19:32:32 +0100277 "recipients [",
278 memory_region->sender, memory_region->attributes,
Olivier Deprez935e1b12020-12-22 18:01:29 +0100279 memory_region->flags, memory_region->tag,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100280 memory_region->receiver_count);
281 for (i = 0; i < memory_region->receiver_count; ++i) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000282 struct ffa_memory_access *receiver =
283 ffa_memory_region_get_receiver(memory_region, i);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000284 if (i != 0) {
285 dlog(", ");
286 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +0000287 dlog("Receiver %#x: %#x (offset %u)",
288 receiver->receiver_permissions.receiver,
289 receiver->receiver_permissions.permissions,
290 receiver->composite_memory_region_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000291 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000292}
293
J-Alves66652252022-07-06 09:49:51 +0100294void dump_share_states(void)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000295{
296 uint32_t i;
297
298 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
299 return;
300 }
301
302 dlog("Current share states:\n");
303 sl_lock(&share_states_lock_instance);
304 for (i = 0; i < MAX_MEM_SHARES; ++i) {
305 if (share_states[i].share_func != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000306 switch (share_states[i].share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100307 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000308 dlog("SHARE");
309 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100310 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000311 dlog("LEND");
312 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100313 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000314 dlog("DONATE");
315 break;
316 default:
317 dlog("invalid share_func %#x",
318 share_states[i].share_func);
319 }
Olivier Deprez935e1b12020-12-22 18:01:29 +0100320 dlog(" %#x (", share_states[i].memory_region->handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000321 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100322 if (share_states[i].sending_complete) {
323 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000324 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100325 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000326 }
J-Alves2a0d2882020-10-29 14:49:50 +0000327 dlog(" with %d fragments, %d retrieved, "
328 " sender's original mode: %#x\n",
Andrew Walbranca808b12020-05-15 17:22:28 +0100329 share_states[i].fragment_count,
J-Alves2a0d2882020-10-29 14:49:50 +0000330 share_states[i].retrieved_fragment_count[0],
331 share_states[i].sender_orig_mode);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000332 }
333 }
334 sl_unlock(&share_states_lock_instance);
335}
336
Andrew Walbran475c1452020-02-07 13:22:22 +0000337/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100338static inline uint32_t ffa_memory_permissions_to_mode(
J-Alves7cd5eb32020-10-16 19:06:10 +0100339 ffa_memory_access_permissions_t permissions, uint32_t default_mode)
Andrew Walbran475c1452020-02-07 13:22:22 +0000340{
341 uint32_t mode = 0;
342
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100343 switch (ffa_get_data_access_attr(permissions)) {
344 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000345 mode = MM_MODE_R;
346 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100347 case FFA_DATA_ACCESS_RW:
Andrew Walbran475c1452020-02-07 13:22:22 +0000348 mode = MM_MODE_R | MM_MODE_W;
349 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100350 case FFA_DATA_ACCESS_NOT_SPECIFIED:
351 mode = (default_mode & (MM_MODE_R | MM_MODE_W));
352 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100353 case FFA_DATA_ACCESS_RESERVED:
354 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100355 }
356
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100357 switch (ffa_get_instruction_access_attr(permissions)) {
358 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000359 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100360 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100361 mode |= MM_MODE_X;
362 break;
J-Alves7cd5eb32020-10-16 19:06:10 +0100363 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
364 mode |= (default_mode & MM_MODE_X);
365 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100366 case FFA_INSTRUCTION_ACCESS_RESERVED:
367 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000368 }
369
Olivier Deprez878bd5b2021-04-15 19:05:10 +0200370 /* Set the security state bit if necessary. */
371 if ((default_mode & plat_ffa_other_world_mode()) != 0) {
372 mode |= plat_ffa_other_world_mode();
373 }
374
Andrew Walbran475c1452020-02-07 13:22:22 +0000375 return mode;
376}
377
Jose Marinho75509b42019-04-09 09:34:59 +0100378/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000379 * Get the current mode in the stage-2 page table of the given vm of all the
380 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100381 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100382 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100383static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000384 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100385 struct ffa_memory_region_constituent **fragments,
386 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100387{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100388 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100389 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100390
Andrew Walbranca808b12020-05-15 17:22:28 +0100391 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100392 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000393 * Fail if there are no constituents. Otherwise we would get an
394 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100395 */
Karl Meakin5df422c2023-07-11 17:31:38 +0100396 dlog_verbose("%s: no constituents\n", __func__);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100397 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100398 }
399
Andrew Walbranca808b12020-05-15 17:22:28 +0100400 for (i = 0; i < fragment_count; ++i) {
401 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
402 ipaddr_t begin = ipa_init(fragments[i][j].address);
403 size_t size = fragments[i][j].page_count * PAGE_SIZE;
404 ipaddr_t end = ipa_add(begin, size);
405 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100406
Andrew Walbranca808b12020-05-15 17:22:28 +0100407 /* Fail if addresses are not page-aligned. */
408 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
409 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100410 dlog_verbose("%s: addresses not page-aligned\n",
411 __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +0100412 return ffa_error(FFA_INVALID_PARAMETERS);
413 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100414
Andrew Walbranca808b12020-05-15 17:22:28 +0100415 /*
416 * Ensure that this constituent memory range is all
417 * mapped with the same mode.
418 */
Raghu Krishnamurthy785d52f2021-02-13 00:02:40 -0800419 if (!vm_mem_get_mode(vm, begin, end, &current_mode)) {
Karl Meakin5df422c2023-07-11 17:31:38 +0100420 dlog_verbose(
421 "%s: constituent memory range %#x..%#x "
422 "not mapped with the same mode\n",
423 __func__, begin, end);
Andrew Walbranca808b12020-05-15 17:22:28 +0100424 return ffa_error(FFA_DENIED);
425 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100426
Andrew Walbranca808b12020-05-15 17:22:28 +0100427 /*
428 * Ensure that all constituents are mapped with the same
429 * mode.
430 */
431 if (i == 0) {
432 *orig_mode = current_mode;
433 } else if (current_mode != *orig_mode) {
434 dlog_verbose(
Karl Meakin5df422c2023-07-11 17:31:38 +0100435 "%s: expected mode %#x but was %#x for "
436 "%d pages at %#x.\n",
437 __func__, *orig_mode, current_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100438 fragments[i][j].page_count,
439 ipa_addr(begin));
440 return ffa_error(FFA_DENIED);
441 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100442 }
Jose Marinho75509b42019-04-09 09:34:59 +0100443 }
444
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100445 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000446}
447
448/**
449 * Verify that all pages have the same mode, that the starting mode
450 * constitutes a valid state and obtain the next mode to apply
451 * to the sending VM.
452 *
453 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100454 * 1) FFA_DENIED if a state transition was not found;
455 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100456 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100457 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100458 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100459 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
460 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000461 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100462static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100463 struct vm_locked from, uint32_t share_func,
J-Alves363f5722022-04-25 17:37:37 +0100464 struct ffa_memory_access *receivers, uint32_t receivers_count,
465 uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100466 struct ffa_memory_region_constituent **fragments,
467 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
468 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000469{
470 const uint32_t state_mask =
471 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100472 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000473
Andrew Walbranca808b12020-05-15 17:22:28 +0100474 ret = constituents_get_mode(from, orig_from_mode, fragments,
475 fragment_constituent_counts,
476 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100477 if (ret.func != FFA_SUCCESS_32) {
Olivier Depreze7eb1682022-03-16 17:09:03 +0100478 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100479 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100480 }
481
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000482 /* Ensure the address range is normal memory and not a device. */
J-Alves788b4492023-04-18 14:01:23 +0100483 if ((*orig_from_mode & MM_MODE_D) != 0U) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000484 dlog_verbose("Can't share device memory (mode is %#x).\n",
485 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100486 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000487 }
488
489 /*
490 * Ensure the sender is the owner and has exclusive access to the
491 * memory.
492 */
493 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100494 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100495 }
496
J-Alves363f5722022-04-25 17:37:37 +0100497 assert(receivers != NULL && receivers_count > 0U);
J-Alves7cd5eb32020-10-16 19:06:10 +0100498
J-Alves363f5722022-04-25 17:37:37 +0100499 for (uint32_t i = 0U; i < receivers_count; i++) {
500 ffa_memory_access_permissions_t permissions =
501 receivers[i].receiver_permissions.permissions;
502 uint32_t required_from_mode = ffa_memory_permissions_to_mode(
503 permissions, *orig_from_mode);
504
J-Alves788b4492023-04-18 14:01:23 +0100505 /*
506 * The assumption is that at this point, the operation from
507 * SP to a receiver VM, should have returned an FFA_ERROR
508 * already.
509 */
510 if (!ffa_is_vm_id(from.vm->id)) {
511 assert(!ffa_is_vm_id(
512 receivers[i].receiver_permissions.receiver));
513 }
514
J-Alves363f5722022-04-25 17:37:37 +0100515 if ((*orig_from_mode & required_from_mode) !=
516 required_from_mode) {
517 dlog_verbose(
518 "Sender tried to send memory with permissions "
J-Alves788b4492023-04-18 14:01:23 +0100519 "which required mode %#x but only had %#x "
520 "itself.\n",
J-Alves363f5722022-04-25 17:37:37 +0100521 required_from_mode, *orig_from_mode);
522 return ffa_error(FFA_DENIED);
523 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000524 }
525
526 /* Find the appropriate new mode. */
527 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000528 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100529 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000530 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100531 break;
532
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100533 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000534 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100535 break;
536
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100537 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000538 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100539 break;
540
Jose Marinho75509b42019-04-09 09:34:59 +0100541 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100542 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100543 }
544
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100545 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000546}
547
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100548static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000549 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100550 struct ffa_memory_region_constituent **fragments,
551 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
552 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000553{
554 const uint32_t state_mask =
555 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
556 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100557 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000558
Andrew Walbranca808b12020-05-15 17:22:28 +0100559 ret = constituents_get_mode(from, orig_from_mode, fragments,
560 fragment_constituent_counts,
561 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100562 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100563 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000564 }
565
566 /* Ensure the address range is normal memory and not a device. */
567 if (*orig_from_mode & MM_MODE_D) {
568 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
569 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100570 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000571 }
572
573 /*
574 * Ensure the relinquishing VM is not the owner but has access to the
575 * memory.
576 */
577 orig_from_state = *orig_from_mode & state_mask;
578 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
579 dlog_verbose(
580 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100581 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000582 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100583 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000584 }
585
586 /* Find the appropriate new mode. */
587 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
588
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100589 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000590}
591
592/**
593 * Verify that all pages have the same mode, that the starting mode
594 * constitutes a valid state and obtain the next mode to apply
595 * to the retrieving VM.
596 *
597 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100598 * 1) FFA_DENIED if a state transition was not found;
599 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100600 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100601 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100602 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100603 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
604 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000605 */
J-Alvesfc19b372022-07-06 12:17:35 +0100606struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000607 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100608 struct ffa_memory_region_constituent **fragments,
609 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
610 uint32_t memory_to_attributes, uint32_t *to_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000611{
612 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100613 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000614
Andrew Walbranca808b12020-05-15 17:22:28 +0100615 ret = constituents_get_mode(to, &orig_to_mode, fragments,
616 fragment_constituent_counts,
617 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100618 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100619 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100620 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000621 }
622
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100623 if (share_func == FFA_MEM_RECLAIM_32) {
J-Alves9256f162021-12-09 13:18:43 +0000624 /*
625 * If the original ffa memory send call has been processed
626 * successfully, it is expected the orig_to_mode would overlay
627 * with `state_mask`, as a result of the function
628 * `ffa_send_check_transition`.
629 */
J-Alves59ed0042022-07-28 18:26:41 +0100630 if (vm_id_is_current_world(to.vm->id)) {
631 assert((orig_to_mode &
632 (MM_MODE_INVALID | MM_MODE_UNOWNED |
633 MM_MODE_SHARED)) != 0U);
634 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000635 } else {
636 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +0100637 * If the retriever is from virtual FF-A instance:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000638 * Ensure the retriever has the expected state. We don't care
639 * about the MM_MODE_SHARED bit; either with or without it set
640 * are both valid representations of the !O-NA state.
641 */
J-Alvesa9cd7e32022-07-01 13:49:33 +0100642 if (vm_id_is_current_world(to.vm->id) &&
643 to.vm->id != HF_PRIMARY_VM_ID &&
644 (orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
645 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100646 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000647 }
648 }
649
650 /* Find the appropriate new mode. */
651 *to_mode = memory_to_attributes;
652 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100653 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000654 *to_mode |= 0;
655 break;
656
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100657 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000658 *to_mode |= MM_MODE_UNOWNED;
659 break;
660
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100661 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000662 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
663 break;
664
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100665 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000666 *to_mode |= 0;
667 break;
668
669 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100670 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100671 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000672 }
673
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100674 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100675}
Jose Marinho09b1db82019-08-08 09:16:59 +0100676
677/**
678 * Updates a VM's page table such that the given set of physical address ranges
679 * are mapped in the address space at the corresponding address ranges, in the
680 * mode provided.
681 *
682 * If commit is false, the page tables will be allocated from the mpool but no
683 * mappings will actually be updated. This function must always be called first
684 * with commit false to check that it will succeed before calling with commit
685 * true, to avoid leaving the page table in a half-updated state. To make a
686 * series of changes atomically you can call them all with commit false before
687 * calling them all with commit true.
688 *
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700689 * vm_ptable_defrag should always be called after a series of page table
690 * updates, whether they succeed or fail.
Jose Marinho09b1db82019-08-08 09:16:59 +0100691 *
692 * Returns true on success, or false if the update failed and no changes were
693 * made to memory mappings.
694 */
J-Alves66652252022-07-06 09:49:51 +0100695bool ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000696 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100697 struct ffa_memory_region_constituent **fragments,
698 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
Daniel Boulby4dd3f532021-09-21 09:57:08 +0100699 uint32_t mode, struct mpool *ppool, bool commit)
Jose Marinho09b1db82019-08-08 09:16:59 +0100700{
Andrew Walbranca808b12020-05-15 17:22:28 +0100701 uint32_t i;
702 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100703
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -0700704 if (vm_locked.vm->el0_partition) {
705 mode |= MM_MODE_USER | MM_MODE_NG;
706 }
707
Andrew Walbranca808b12020-05-15 17:22:28 +0100708 /* Iterate over the memory region constituents within each fragment. */
709 for (i = 0; i < fragment_count; ++i) {
710 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
711 size_t size = fragments[i][j].page_count * PAGE_SIZE;
712 paddr_t pa_begin =
713 pa_from_ipa(ipa_init(fragments[i][j].address));
714 paddr_t pa_end = pa_add(pa_begin, size);
Jens Wiklander4f1880c2022-10-19 17:00:14 +0200715 uint32_t pa_bits =
716 arch_mm_get_pa_bits(arch_mm_get_pa_range());
Federico Recanati4fd065d2021-12-13 20:06:23 +0100717
718 /*
719 * Ensure the requested region falls into system's PA
720 * range.
721 */
Jens Wiklander4f1880c2022-10-19 17:00:14 +0200722 if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
723 ((pa_addr(pa_end) >> pa_bits) > 0)) {
Federico Recanati4fd065d2021-12-13 20:06:23 +0100724 dlog_error("Region is outside of PA Range\n");
725 return false;
726 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100727
728 if (commit) {
729 vm_identity_commit(vm_locked, pa_begin, pa_end,
730 mode, ppool, NULL);
731 } else if (!vm_identity_prepare(vm_locked, pa_begin,
732 pa_end, mode, ppool)) {
733 return false;
734 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100735 }
736 }
737
738 return true;
739}
740
741/**
742 * Clears a region of physical memory by overwriting it with zeros. The data is
743 * flushed from the cache so the memory has been cleared across the system.
744 */
J-Alves7db32002021-12-14 14:44:50 +0000745static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool,
746 uint32_t extra_mode_attributes)
Jose Marinho09b1db82019-08-08 09:16:59 +0100747{
748 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000749 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100750 * global mapping of the whole range. Such an approach will limit
751 * the changes to stage-1 tables and will allow only local
752 * invalidation.
753 */
754 bool ret;
755 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
J-Alves7db32002021-12-14 14:44:50 +0000756 void *ptr = mm_identity_map(stage1_locked, begin, end,
757 MM_MODE_W | (extra_mode_attributes &
758 plat_ffa_other_world_mode()),
759 ppool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100760 size_t size = pa_difference(begin, end);
761
762 if (!ptr) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100763 goto fail;
764 }
765
766 memset_s(ptr, size, 0, size);
767 arch_mm_flush_dcache(ptr, size);
768 mm_unmap(stage1_locked, begin, end, ppool);
769
770 ret = true;
771 goto out;
772
773fail:
774 ret = false;
775
776out:
777 mm_unlock_stage1(&stage1_locked);
778
779 return ret;
780}
781
782/**
783 * Clears a region of physical memory by overwriting it with zeros. The data is
784 * flushed from the cache so the memory has been cleared across the system.
785 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100786static bool ffa_clear_memory_constituents(
J-Alves7db32002021-12-14 14:44:50 +0000787 uint32_t security_state_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100788 struct ffa_memory_region_constituent **fragments,
789 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
790 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100791{
792 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +0100793 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100794 bool ret = false;
795
796 /*
797 * Create a local pool so any freed memory can't be used by another
798 * thread. This is to ensure each constituent that is mapped can be
799 * unmapped again afterwards.
800 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000801 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100802
Andrew Walbranca808b12020-05-15 17:22:28 +0100803 /* Iterate over the memory region constituents within each fragment. */
804 for (i = 0; i < fragment_count; ++i) {
805 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100806
J-Alves8457f932023-10-11 16:41:45 +0100807 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100808 size_t size = fragments[i][j].page_count * PAGE_SIZE;
809 paddr_t begin =
810 pa_from_ipa(ipa_init(fragments[i][j].address));
811 paddr_t end = pa_add(begin, size);
812
J-Alves7db32002021-12-14 14:44:50 +0000813 if (!clear_memory(begin, end, &local_page_pool,
814 security_state_mode)) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100815 /*
816 * api_clear_memory will defrag on failure, so
817 * no need to do it here.
818 */
819 goto out;
820 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100821 }
822 }
823
Jose Marinho09b1db82019-08-08 09:16:59 +0100824 ret = true;
825
826out:
827 mpool_fini(&local_page_pool);
828 return ret;
829}
830
J-Alves5952d942022-12-22 16:03:00 +0000831static bool is_memory_range_within(ipaddr_t begin, ipaddr_t end,
832 ipaddr_t in_begin, ipaddr_t in_end)
833{
834 return (ipa_addr(begin) >= ipa_addr(in_begin) &&
835 ipa_addr(begin) < ipa_addr(in_end)) ||
836 (ipa_addr(end) <= ipa_addr(in_end) &&
837 ipa_addr(end) > ipa_addr(in_begin));
838}
839
840/**
841 * Receives a memory range and looks for overlaps with the remainder
842 * constituents of the memory share/lend/donate operation. Assumes they are
843 * passed in order to avoid having to loop over all the elements at each call.
844 * The function only compares the received memory ranges with those that follow
845 * within the same fragment, and subsequent fragments from the same operation.
846 */
847static bool ffa_memory_check_overlap(
848 struct ffa_memory_region_constituent **fragments,
849 const uint32_t *fragment_constituent_counts,
850 const uint32_t fragment_count, const uint32_t current_fragment,
851 const uint32_t current_constituent)
852{
853 uint32_t i = current_fragment;
854 uint32_t j = current_constituent;
855 ipaddr_t current_begin = ipa_init(fragments[i][j].address);
856 const uint32_t current_page_count = fragments[i][j].page_count;
857 size_t current_size = current_page_count * PAGE_SIZE;
858 ipaddr_t current_end = ipa_add(current_begin, current_size - 1);
859
860 if (current_size == 0 ||
861 current_size > UINT64_MAX - ipa_addr(current_begin)) {
862 dlog_verbose("Invalid page count. Addr: %x page_count: %x\n",
863 current_begin, current_page_count);
864 return false;
865 }
866
867 for (; i < fragment_count; i++) {
868 j = (i == current_fragment) ? j + 1 : 0;
869
870 for (; j < fragment_constituent_counts[i]; j++) {
871 ipaddr_t begin = ipa_init(fragments[i][j].address);
872 const uint32_t page_count = fragments[i][j].page_count;
873 size_t size = page_count * PAGE_SIZE;
874 ipaddr_t end = ipa_add(begin, size - 1);
875
876 if (size == 0 || size > UINT64_MAX - ipa_addr(begin)) {
877 dlog_verbose(
878 "Invalid page count. Addr: %x "
879 "page_count: %x\n",
880 begin, page_count);
881 return false;
882 }
883
884 /*
885 * Check if current ranges is within begin and end, as
886 * well as the reverse. This should help optimize the
887 * loop, and reduce the number of iterations.
888 */
889 if (is_memory_range_within(begin, end, current_begin,
890 current_end) ||
891 is_memory_range_within(current_begin, current_end,
892 begin, end)) {
893 dlog_verbose(
894 "Overlapping memory ranges: %#x - %#x "
895 "with %#x - %#x\n",
896 ipa_addr(begin), ipa_addr(end),
897 ipa_addr(current_begin),
898 ipa_addr(current_end));
899 return true;
900 }
901 }
902 }
903
904 return false;
905}
906
Jose Marinho09b1db82019-08-08 09:16:59 +0100907/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000908 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +0100909 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000910 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +0100911 *
912 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000913 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100914 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +0100915 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100916 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
917 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100918 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +0100919 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100920 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +0100921 */
J-Alves66652252022-07-06 09:49:51 +0100922struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000923 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100924 struct ffa_memory_region_constituent **fragments,
925 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves8f11cde2022-12-21 16:18:22 +0000926 uint32_t composite_total_page_count, uint32_t share_func,
927 struct ffa_memory_access *receivers, uint32_t receivers_count,
928 struct mpool *page_pool, bool clear, uint32_t *orig_from_mode_ret)
Jose Marinho09b1db82019-08-08 09:16:59 +0100929{
Andrew Walbranca808b12020-05-15 17:22:28 +0100930 uint32_t i;
J-Alves8f11cde2022-12-21 16:18:22 +0000931 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100932 uint32_t orig_from_mode;
933 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +0100934 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100935 struct ffa_value ret;
J-Alves8f11cde2022-12-21 16:18:22 +0000936 uint32_t constituents_total_page_count = 0;
Jose Marinho09b1db82019-08-08 09:16:59 +0100937
938 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +0100939 * Make sure constituents are properly aligned to a 64-bit boundary. If
940 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100941 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100942 for (i = 0; i < fragment_count; ++i) {
943 if (!is_aligned(fragments[i], 8)) {
944 dlog_verbose("Constituents not aligned.\n");
945 return ffa_error(FFA_INVALID_PARAMETERS);
946 }
J-Alves8f11cde2022-12-21 16:18:22 +0000947 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
948 constituents_total_page_count +=
949 fragments[i][j].page_count;
J-Alves5952d942022-12-22 16:03:00 +0000950 if (ffa_memory_check_overlap(
951 fragments, fragment_constituent_counts,
952 fragment_count, i, j)) {
953 return ffa_error(FFA_INVALID_PARAMETERS);
954 }
J-Alves8f11cde2022-12-21 16:18:22 +0000955 }
956 }
957
958 if (constituents_total_page_count != composite_total_page_count) {
959 dlog_verbose(
960 "Composite page count differs from calculated page "
961 "count from constituents.\n");
962 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +0100963 }
964
965 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000966 * Check if the state transition is lawful for the sender, ensure that
967 * all constituents of a memory region being shared are at the same
968 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +0100969 */
J-Alves363f5722022-04-25 17:37:37 +0100970 ret = ffa_send_check_transition(from_locked, share_func, receivers,
971 receivers_count, &orig_from_mode,
972 fragments, fragment_constituent_counts,
Andrew Walbranca808b12020-05-15 17:22:28 +0100973 fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100974 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100975 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100976 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100977 }
978
Andrew Walbran37c574e2020-06-03 11:45:46 +0100979 if (orig_from_mode_ret != NULL) {
980 *orig_from_mode_ret = orig_from_mode;
981 }
982
Jose Marinho09b1db82019-08-08 09:16:59 +0100983 /*
984 * Create a local pool so any freed memory can't be used by another
985 * thread. This is to ensure the original mapping can be restored if the
986 * clear fails.
987 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000988 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100989
990 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000991 * First reserve all required memory for the new page table entries
992 * without committing, to make sure the entire operation will succeed
993 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +0100994 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100995 if (!ffa_region_group_identity_map(
996 from_locked, fragments, fragment_constituent_counts,
997 fragment_count, from_mode, page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100998 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100999 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +01001000 goto out;
1001 }
1002
1003 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001004 * Update the mapping for the sender. This won't allocate because the
1005 * transaction was already prepared above, but may free pages in the
1006 * case that a whole block is being unmapped that was previously
1007 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +01001008 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001009 CHECK(ffa_region_group_identity_map(
1010 from_locked, fragments, fragment_constituent_counts,
1011 fragment_count, from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001012
1013 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001014 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001015 !ffa_clear_memory_constituents(orig_from_mode, fragments,
1016 fragment_constituent_counts,
1017 fragment_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +01001018 /*
1019 * On failure, roll back by returning memory to the sender. This
1020 * may allocate pages which were previously freed into
1021 * `local_page_pool` by the call above, but will never allocate
1022 * more pages than that so can never fail.
1023 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001024 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001025 from_locked, fragments, fragment_constituent_counts,
1026 fragment_count, orig_from_mode, &local_page_pool,
1027 true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001028
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001029 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +01001030 goto out;
1031 }
1032
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001033 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001034
1035out:
1036 mpool_fini(&local_page_pool);
1037
1038 /*
1039 * Tidy up the page table by reclaiming failed mappings (if there was an
1040 * error) or merging entries into blocks where possible (on success).
1041 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001042 vm_ptable_defrag(from_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001043
1044 return ret;
1045}
1046
1047/**
1048 * Validates and maps memory shared from one VM to another.
1049 *
1050 * This function requires the calling context to hold the <to> lock.
1051 *
1052 * Returns:
1053 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001054 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001055 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001056 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001057 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001058 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001059 */
J-Alvesb5084cf2022-07-06 14:20:12 +01001060struct ffa_value ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01001061 struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001062 struct ffa_memory_region_constituent **fragments,
1063 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves26483382023-04-20 12:01:49 +01001064 uint32_t sender_orig_mode, uint32_t share_func, bool clear,
Andrew Walbranca808b12020-05-15 17:22:28 +01001065 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001066{
Andrew Walbranca808b12020-05-15 17:22:28 +01001067 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001068 uint32_t to_mode;
1069 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001070 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001071
1072 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001073 * Make sure constituents are properly aligned to a 64-bit boundary. If
1074 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001075 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001076 for (i = 0; i < fragment_count; ++i) {
1077 if (!is_aligned(fragments[i], 8)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001078 dlog_verbose("Fragment not properly aligned.\n");
Andrew Walbranca808b12020-05-15 17:22:28 +01001079 return ffa_error(FFA_INVALID_PARAMETERS);
1080 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001081 }
1082
1083 /*
1084 * Check if the state transition is lawful for the recipient, and ensure
1085 * that all constituents of the memory region being retrieved are at the
1086 * same state.
1087 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001088 ret = ffa_retrieve_check_transition(
1089 to_locked, share_func, fragments, fragment_constituent_counts,
J-Alves26483382023-04-20 12:01:49 +01001090 fragment_count, sender_orig_mode, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001091 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001092 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001093 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001094 }
1095
1096 /*
1097 * Create a local pool so any freed memory can't be used by another
1098 * thread. This is to ensure the original mapping can be restored if the
1099 * clear fails.
1100 */
1101 mpool_init_with_fallback(&local_page_pool, page_pool);
1102
1103 /*
1104 * First reserve all required memory for the new page table entries in
1105 * the recipient page tables without committing, to make sure the entire
1106 * operation will succeed without exhausting the page pool.
1107 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001108 if (!ffa_region_group_identity_map(
1109 to_locked, fragments, fragment_constituent_counts,
1110 fragment_count, to_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001111 /* TODO: partial defrag of failed range. */
1112 dlog_verbose(
1113 "Insufficient memory to update recipient page "
1114 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001115 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001116 goto out;
1117 }
1118
1119 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001120 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001121 !ffa_clear_memory_constituents(sender_orig_mode, fragments,
1122 fragment_constituent_counts,
1123 fragment_count, page_pool)) {
J-Alvesb5084cf2022-07-06 14:20:12 +01001124 dlog_verbose("Couldn't clear constituents.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001125 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001126 goto out;
1127 }
1128
Jose Marinho09b1db82019-08-08 09:16:59 +01001129 /*
1130 * Complete the transfer by mapping the memory into the recipient. This
1131 * won't allocate because the transaction was already prepared above, so
1132 * it doesn't need to use the `local_page_pool`.
1133 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001134 CHECK(ffa_region_group_identity_map(
1135 to_locked, fragments, fragment_constituent_counts,
1136 fragment_count, to_mode, page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001137
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001138 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001139
1140out:
1141 mpool_fini(&local_page_pool);
1142
1143 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001144 * Tidy up the page table by reclaiming failed mappings (if there was an
1145 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001146 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001147 vm_ptable_defrag(to_locked, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001148
1149 return ret;
1150}
1151
Andrew Walbran996d1d12020-05-27 14:08:43 +01001152static struct ffa_value ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01001153 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001154 struct ffa_memory_region_constituent **fragments,
1155 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1156 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001157{
1158 uint32_t orig_from_mode;
1159 uint32_t from_mode;
1160 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001161 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001162
Andrew Walbranca808b12020-05-15 17:22:28 +01001163 ret = ffa_relinquish_check_transition(
1164 from_locked, &orig_from_mode, fragments,
1165 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001166 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001167 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001168 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001169 }
1170
1171 /*
1172 * Create a local pool so any freed memory can't be used by another
1173 * thread. This is to ensure the original mapping can be restored if the
1174 * clear fails.
1175 */
1176 mpool_init_with_fallback(&local_page_pool, page_pool);
1177
1178 /*
1179 * First reserve all required memory for the new page table entries
1180 * without committing, to make sure the entire operation will succeed
1181 * without exhausting the page pool.
1182 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001183 if (!ffa_region_group_identity_map(
1184 from_locked, fragments, fragment_constituent_counts,
1185 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001186 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001187 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001188 goto out;
1189 }
1190
1191 /*
1192 * Update the mapping for the sender. This won't allocate because the
1193 * transaction was already prepared above, but may free pages in the
1194 * case that a whole block is being unmapped that was previously
1195 * partially mapped.
1196 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001197 CHECK(ffa_region_group_identity_map(
1198 from_locked, fragments, fragment_constituent_counts,
1199 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001200
1201 /* Clear the memory so no VM or device can see the previous contents. */
J-Alves7db32002021-12-14 14:44:50 +00001202 if (clear &&
J-Alves26483382023-04-20 12:01:49 +01001203 !ffa_clear_memory_constituents(orig_from_mode, fragments,
1204 fragment_constituent_counts,
1205 fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001206 /*
1207 * On failure, roll back by returning memory to the sender. This
1208 * may allocate pages which were previously freed into
1209 * `local_page_pool` by the call above, but will never allocate
1210 * more pages than that so can never fail.
1211 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001212 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001213 from_locked, fragments, fragment_constituent_counts,
1214 fragment_count, orig_from_mode, &local_page_pool,
1215 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001216
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001217 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001218 goto out;
1219 }
1220
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001221 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001222
1223out:
1224 mpool_fini(&local_page_pool);
1225
1226 /*
1227 * Tidy up the page table by reclaiming failed mappings (if there was an
1228 * error) or merging entries into blocks where possible (on success).
1229 */
Raghu Krishnamurthy7ad3d142021-03-28 00:47:35 -07001230 vm_ptable_defrag(from_locked, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001231
1232 return ret;
1233}
1234
1235/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001236 * Complete a memory sending operation by checking that it is valid, updating
1237 * the sender page table, and then either marking the share state as having
1238 * completed sending (on success) or freeing it (on failure).
1239 *
1240 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1241 */
J-Alvesfdd29272022-07-19 13:16:31 +01001242struct ffa_value ffa_memory_send_complete(
Andrew Walbranca808b12020-05-15 17:22:28 +01001243 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001244 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1245 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001246{
1247 struct ffa_memory_region *memory_region = share_state->memory_region;
J-Alves8f11cde2022-12-21 16:18:22 +00001248 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01001249 struct ffa_value ret;
1250
1251 /* Lock must be held. */
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001252 assert(share_states.share_states != NULL);
J-Alves8f11cde2022-12-21 16:18:22 +00001253 assert(memory_region != NULL);
1254 composite = ffa_memory_region_get_composite(memory_region, 0);
1255 assert(composite != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001256
1257 /* Check that state is valid in sender page table and update. */
1258 ret = ffa_send_check_update(
1259 from_locked, share_state->fragments,
1260 share_state->fragment_constituent_counts,
J-Alves8f11cde2022-12-21 16:18:22 +00001261 share_state->fragment_count, composite->page_count,
1262 share_state->share_func, memory_region->receivers,
1263 memory_region->receiver_count, page_pool,
1264 memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001265 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001266 if (ret.func != FFA_SUCCESS_32) {
1267 /*
1268 * Free share state, it failed to send so it can't be retrieved.
1269 */
Karl Meakin4cec5e82023-06-30 16:30:22 +01001270 dlog_verbose("%s: failed to send check update: %s(%s)\n",
1271 __func__, ffa_func_name(ret.func),
1272 ffa_error_name(ffa_error_code(ret)));
Andrew Walbranca808b12020-05-15 17:22:28 +01001273 share_state_free(share_states, share_state, page_pool);
1274 return ret;
1275 }
1276
1277 share_state->sending_complete = true;
Karl Meakin4cec5e82023-06-30 16:30:22 +01001278 dlog_verbose("%s: marked sending complete.\n", __func__);
Andrew Walbranca808b12020-05-15 17:22:28 +01001279
J-Alvesee68c542020-10-29 17:48:20 +00001280 return ffa_mem_success(share_state->memory_region->handle);
Andrew Walbranca808b12020-05-15 17:22:28 +01001281}
1282
1283/**
Federico Recanatia98603a2021-12-20 18:04:03 +01001284 * Check that the memory attributes match Hafnium expectations:
1285 * Normal Memory, Inner shareable, Write-Back Read-Allocate
1286 * Write-Allocate Cacheable.
1287 */
1288static struct ffa_value ffa_memory_attributes_validate(
J-Alves7a99d0d2023-02-08 13:49:48 +00001289 ffa_memory_attributes_t attributes)
Federico Recanatia98603a2021-12-20 18:04:03 +01001290{
1291 enum ffa_memory_type memory_type;
1292 enum ffa_memory_cacheability cacheability;
1293 enum ffa_memory_shareability shareability;
1294
1295 memory_type = ffa_get_memory_type_attr(attributes);
1296 if (memory_type != FFA_MEMORY_NORMAL_MEM) {
1297 dlog_verbose("Invalid memory type %#x, expected %#x.\n",
1298 memory_type, FFA_MEMORY_NORMAL_MEM);
Federico Recanati3d953f32022-02-17 09:31:29 +01001299 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001300 }
1301
1302 cacheability = ffa_get_memory_cacheability_attr(attributes);
1303 if (cacheability != FFA_MEMORY_CACHE_WRITE_BACK) {
1304 dlog_verbose("Invalid cacheability %#x, expected %#x.\n",
1305 cacheability, FFA_MEMORY_CACHE_WRITE_BACK);
Federico Recanati3d953f32022-02-17 09:31:29 +01001306 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001307 }
1308
1309 shareability = ffa_get_memory_shareability_attr(attributes);
1310 if (shareability != FFA_MEMORY_INNER_SHAREABLE) {
1311 dlog_verbose("Invalid shareability %#x, expected #%x.\n",
1312 shareability, FFA_MEMORY_INNER_SHAREABLE);
Federico Recanati3d953f32022-02-17 09:31:29 +01001313 return ffa_error(FFA_DENIED);
Federico Recanatia98603a2021-12-20 18:04:03 +01001314 }
1315
1316 return (struct ffa_value){.func = FFA_SUCCESS_32};
1317}
1318
1319/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001320 * Check that the given `memory_region` represents a valid memory send request
1321 * of the given `share_func` type, return the clear flag and permissions via the
1322 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001323 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001324 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001325 * not.
1326 */
J-Alves66652252022-07-06 09:49:51 +01001327struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001328 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1329 uint32_t memory_share_length, uint32_t fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001330 uint32_t share_func)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001331{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001332 struct ffa_composite_memory_region *composite;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001333 struct ffa_memory_access *receiver =
1334 ffa_memory_region_get_receiver(memory_region, 0);
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001335 uint64_t receivers_end;
1336 uint64_t min_length;
Federico Recanati872cd692022-01-05 13:10:10 +01001337 uint32_t composite_memory_region_offset;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001338 uint32_t constituents_start;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001339 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001340 enum ffa_data_access data_access;
1341 enum ffa_instruction_access instruction_access;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001342 enum ffa_memory_security security_state;
Federico Recanatia98603a2021-12-20 18:04:03 +01001343 struct ffa_value ret;
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001344 const size_t minimum_first_fragment_length =
1345 (sizeof(struct ffa_memory_region) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001346 memory_region->memory_access_desc_size +
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001347 sizeof(struct ffa_composite_memory_region));
1348
1349 if (fragment_length < minimum_first_fragment_length) {
1350 dlog_verbose("Fragment length %u too short (min %u).\n",
1351 (size_t)fragment_length,
1352 minimum_first_fragment_length);
1353 return ffa_error(FFA_INVALID_PARAMETERS);
1354 }
1355
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05001356 static_assert(sizeof(struct ffa_memory_region_constituent) == 16,
1357 "struct ffa_memory_region_constituent must be 16 bytes");
1358 if (!is_aligned(fragment_length,
1359 sizeof(struct ffa_memory_region_constituent)) ||
1360 !is_aligned(memory_share_length,
1361 sizeof(struct ffa_memory_region_constituent))) {
1362 dlog_verbose(
1363 "Fragment length %u or total length %u"
1364 " is not 16-byte aligned.\n",
1365 fragment_length, memory_share_length);
1366 return ffa_error(FFA_INVALID_PARAMETERS);
1367 }
1368
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001369 if (fragment_length > memory_share_length) {
1370 dlog_verbose(
1371 "Fragment length %u greater than total length %u.\n",
1372 (size_t)fragment_length, (size_t)memory_share_length);
1373 return ffa_error(FFA_INVALID_PARAMETERS);
1374 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001375
J-Alves0b6653d2022-04-22 13:17:38 +01001376 assert(memory_region->receivers_offset ==
1377 offsetof(struct ffa_memory_region, receivers));
J-Alves0b6653d2022-04-22 13:17:38 +01001378
J-Alves95df0ef2022-12-07 10:09:48 +00001379 /* The sender must match the caller. */
1380 if ((!vm_id_is_current_world(from_locked.vm->id) &&
1381 vm_id_is_current_world(memory_region->sender)) ||
1382 (vm_id_is_current_world(from_locked.vm->id) &&
1383 memory_region->sender != from_locked.vm->id)) {
1384 dlog_verbose("Invalid memory sender ID.\n");
1385 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001386 }
1387
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001388 if (memory_region->receiver_count <= 0) {
1389 dlog_verbose("No receivers!\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001390 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001391 }
1392
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001393 /*
1394 * Ensure that the composite header is within the memory bounds and
1395 * doesn't overlap the first part of the message. Cast to uint64_t
1396 * to prevent overflow.
1397 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001398 receivers_end = ((uint64_t)memory_region->memory_access_desc_size *
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001399 (uint64_t)memory_region->receiver_count) +
1400 sizeof(struct ffa_memory_region);
1401 min_length = receivers_end +
1402 sizeof(struct ffa_composite_memory_region) +
1403 sizeof(struct ffa_memory_region_constituent);
1404 if (min_length > memory_share_length) {
1405 dlog_verbose("Share too short: got %u but minimum is %u.\n",
1406 (size_t)memory_share_length, (size_t)min_length);
1407 return ffa_error(FFA_INVALID_PARAMETERS);
1408 }
1409
1410 composite_memory_region_offset =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001411 receiver->composite_memory_region_offset;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001412
1413 /*
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001414 * Check that the composite memory region descriptor is after the access
1415 * descriptors, is at least 16-byte aligned, and fits in the first
1416 * fragment.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001417 */
Demi Marie Obenourd4677412023-02-03 20:35:12 -05001418 if ((composite_memory_region_offset < receivers_end) ||
1419 (composite_memory_region_offset % 16 != 0) ||
1420 (composite_memory_region_offset >
1421 fragment_length - sizeof(struct ffa_composite_memory_region))) {
1422 dlog_verbose(
1423 "Invalid composite memory region descriptor offset "
1424 "%u.\n",
1425 (size_t)composite_memory_region_offset);
1426 return ffa_error(FFA_INVALID_PARAMETERS);
1427 }
1428
1429 /*
1430 * Compute the start of the constituent regions. Already checked
1431 * to be not more than fragment_length and thus not more than
1432 * memory_share_length.
1433 */
1434 constituents_start = composite_memory_region_offset +
1435 sizeof(struct ffa_composite_memory_region);
1436 constituents_length = memory_share_length - constituents_start;
1437
1438 /*
1439 * Check that the number of constituents is consistent with the length
1440 * of the constituent region.
1441 */
1442 composite = ffa_memory_region_get_composite(memory_region, 0);
1443 if ((constituents_length %
1444 sizeof(struct ffa_memory_region_constituent) !=
1445 0) ||
1446 ((constituents_length /
1447 sizeof(struct ffa_memory_region_constituent)) !=
1448 composite->constituent_count)) {
1449 dlog_verbose("Invalid length %u or composite offset %u.\n",
1450 (size_t)memory_share_length,
1451 (size_t)composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001452 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001453 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001454 if (fragment_length < memory_share_length &&
1455 fragment_length < HF_MAILBOX_SIZE) {
1456 dlog_warning(
1457 "Initial fragment length %d smaller than mailbox "
1458 "size.\n",
1459 fragment_length);
1460 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001461
Andrew Walbrana65a1322020-04-06 19:32:32 +01001462 /*
1463 * Clear is not allowed for memory sharing, as the sender still has
1464 * access to the memory.
1465 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001466 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1467 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001468 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001469 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001470 }
1471
1472 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001473 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001474 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001475 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001476 }
1477
J-Alves363f5722022-04-25 17:37:37 +01001478 /* Check that the permissions are valid, for each specified receiver. */
1479 for (uint32_t i = 0U; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001480 struct ffa_memory_region_attributes receiver_permissions;
1481
1482 receiver = ffa_memory_region_get_receiver(memory_region, i);
1483 assert(receiver != NULL);
1484 receiver_permissions = receiver->receiver_permissions;
J-Alves363f5722022-04-25 17:37:37 +01001485 ffa_memory_access_permissions_t permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001486 receiver_permissions.permissions;
1487 ffa_id_t receiver_id = receiver_permissions.receiver;
J-Alves363f5722022-04-25 17:37:37 +01001488
1489 if (memory_region->sender == receiver_id) {
1490 dlog_verbose("Can't share memory with itself.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001491 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001492 }
Federico Recanati85090c42021-12-15 13:17:54 +01001493
J-Alves363f5722022-04-25 17:37:37 +01001494 for (uint32_t j = i + 1; j < memory_region->receiver_count;
1495 j++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001496 struct ffa_memory_access *other_receiver =
1497 ffa_memory_region_get_receiver(memory_region,
1498 j);
1499 assert(other_receiver != NULL);
1500
J-Alves363f5722022-04-25 17:37:37 +01001501 if (receiver_id ==
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001502 other_receiver->receiver_permissions.receiver) {
J-Alves363f5722022-04-25 17:37:37 +01001503 dlog_verbose(
1504 "Repeated receiver(%x) in memory send "
1505 "operation.\n",
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001506 other_receiver->receiver_permissions
1507 .receiver);
J-Alves363f5722022-04-25 17:37:37 +01001508 return ffa_error(FFA_INVALID_PARAMETERS);
1509 }
1510 }
1511
1512 if (composite_memory_region_offset !=
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001513 receiver->composite_memory_region_offset) {
J-Alves363f5722022-04-25 17:37:37 +01001514 dlog_verbose(
1515 "All ffa_memory_access should point to the "
1516 "same composite memory region offset.\n");
1517 return ffa_error(FFA_INVALID_PARAMETERS);
1518 }
1519
1520 data_access = ffa_get_data_access_attr(permissions);
1521 instruction_access =
1522 ffa_get_instruction_access_attr(permissions);
1523 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1524 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
1525 dlog_verbose(
1526 "Reserved value for receiver permissions "
1527 "%#x.\n",
1528 permissions);
1529 return ffa_error(FFA_INVALID_PARAMETERS);
1530 }
1531 if (instruction_access !=
1532 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
1533 dlog_verbose(
1534 "Invalid instruction access permissions %#x "
1535 "for sending memory.\n",
1536 permissions);
1537 return ffa_error(FFA_INVALID_PARAMETERS);
1538 }
1539 if (share_func == FFA_MEM_SHARE_32) {
1540 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
1541 dlog_verbose(
1542 "Invalid data access permissions %#x "
1543 "for sharing memory.\n",
1544 permissions);
1545 return ffa_error(FFA_INVALID_PARAMETERS);
1546 }
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001547 /*
1548 * According to section 10.10.3 of the FF-A v1.1 EAC0
1549 * spec, NX is required for share operations (but must
1550 * not be specified by the sender) so set it in the
1551 * copy that we store, ready to be returned to the
1552 * retriever.
1553 */
1554 if (vm_id_is_current_world(receiver_id)) {
1555 ffa_set_instruction_access_attr(
1556 &permissions,
1557 FFA_INSTRUCTION_ACCESS_NX);
1558 receiver_permissions.permissions = 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
Olivier Deprez4342a3c2022-02-28 09:37:25 +01001580 /* Memory region attributes NS-Bit MBZ for FFA_MEM_SHARE/LEND/DONATE. */
1581 security_state =
1582 ffa_get_memory_security_attr(memory_region->attributes);
1583 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
1584 dlog_verbose(
1585 "Invalid security state for memory share operation.\n");
1586 return ffa_error(FFA_INVALID_PARAMETERS);
1587 }
1588
Federico Recanatid937f5e2021-12-20 17:38:23 +01001589 /*
J-Alves807794e2022-06-16 13:42:47 +01001590 * If a memory donate or lend with single borrower, the memory type
1591 * shall not be specified by the sender.
Federico Recanatid937f5e2021-12-20 17:38:23 +01001592 */
J-Alves807794e2022-06-16 13:42:47 +01001593 if (share_func == FFA_MEM_DONATE_32 ||
1594 (share_func == FFA_MEM_LEND_32 &&
1595 memory_region->receiver_count == 1)) {
1596 if (ffa_get_memory_type_attr(memory_region->attributes) !=
1597 FFA_MEMORY_NOT_SPECIFIED_MEM) {
1598 dlog_verbose(
1599 "Memory type shall not be specified by "
1600 "sender.\n");
1601 return ffa_error(FFA_INVALID_PARAMETERS);
1602 }
1603 } else {
1604 /*
1605 * Check that sender's memory attributes match Hafnium
1606 * expectations: Normal Memory, Inner shareable, Write-Back
1607 * Read-Allocate Write-Allocate Cacheable.
1608 */
1609 ret = ffa_memory_attributes_validate(memory_region->attributes);
1610 if (ret.func != FFA_SUCCESS_32) {
1611 return ret;
1612 }
Federico Recanatid937f5e2021-12-20 17:38:23 +01001613 }
1614
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001615 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001616}
1617
1618/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001619 * Gets the share state for continuing an operation to donate, lend or share
1620 * memory, and checks that it is a valid request.
1621 *
1622 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1623 * not.
1624 */
J-Alvesfdd29272022-07-19 13:16:31 +01001625struct ffa_value ffa_memory_send_continue_validate(
Andrew Walbranca808b12020-05-15 17:22:28 +01001626 struct share_states_locked share_states, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001627 struct ffa_memory_share_state **share_state_ret, ffa_id_t from_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01001628 struct mpool *page_pool)
1629{
1630 struct ffa_memory_share_state *share_state;
1631 struct ffa_memory_region *memory_region;
1632
Daniel Boulbya2f8c662021-11-26 17:52:53 +00001633 assert(share_state_ret != NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001634
1635 /*
1636 * Look up the share state by handle and make sure that the VM ID
1637 * matches.
1638 */
Karl Meakin4a2854a2023-06-30 16:26:52 +01001639 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00001640 if (share_state == NULL) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001641 dlog_verbose(
1642 "Invalid handle %#x for memory send continuation.\n",
1643 handle);
1644 return ffa_error(FFA_INVALID_PARAMETERS);
1645 }
1646 memory_region = share_state->memory_region;
1647
J-Alvesfdd29272022-07-19 13:16:31 +01001648 if (vm_id_is_current_world(from_vm_id) &&
1649 memory_region->sender != from_vm_id) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001650 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1651 return ffa_error(FFA_INVALID_PARAMETERS);
1652 }
1653
1654 if (share_state->sending_complete) {
1655 dlog_verbose(
1656 "Sending of memory handle %#x is already complete.\n",
1657 handle);
1658 return ffa_error(FFA_INVALID_PARAMETERS);
1659 }
1660
1661 if (share_state->fragment_count == MAX_FRAGMENTS) {
1662 /*
1663 * Log a warning as this is a sign that MAX_FRAGMENTS should
1664 * probably be increased.
1665 */
1666 dlog_warning(
1667 "Too many fragments for memory share with handle %#x; "
1668 "only %d supported.\n",
1669 handle, MAX_FRAGMENTS);
1670 /* Free share state, as it's not possible to complete it. */
1671 share_state_free(share_states, share_state, page_pool);
1672 return ffa_error(FFA_NO_MEMORY);
1673 }
1674
1675 *share_state_ret = share_state;
1676
1677 return (struct ffa_value){.func = FFA_SUCCESS_32};
1678}
1679
1680/**
J-Alves95df0ef2022-12-07 10:09:48 +00001681 * Checks if there is at least one receiver from the other world.
1682 */
J-Alvesfdd29272022-07-19 13:16:31 +01001683bool memory_region_receivers_from_other_world(
J-Alves95df0ef2022-12-07 10:09:48 +00001684 struct ffa_memory_region *memory_region)
1685{
1686 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001687 struct ffa_memory_access *receiver =
1688 ffa_memory_region_get_receiver(memory_region, i);
1689 assert(receiver != NULL);
1690 ffa_id_t receiver_id = receiver->receiver_permissions.receiver;
1691
1692 if (!vm_id_is_current_world(receiver_id)) {
J-Alves95df0ef2022-12-07 10:09:48 +00001693 return true;
1694 }
1695 }
1696 return false;
1697}
1698
1699/**
J-Alves9da280b2022-12-21 14:55:39 +00001700 * Validates a call to donate, lend or share memory in which Hafnium is the
1701 * designated allocator of the memory handle. In practice, this also means
1702 * Hafnium is responsible for managing the state structures for the transaction.
1703 * If Hafnium is the SPMC, it should allocate the memory handle when either the
1704 * sender is an SP or there is at least one borrower that is an SP.
1705 * If Hafnium is the hypervisor, it should allocate the memory handle when
1706 * operation involves only NWd VMs.
1707 *
1708 * If validation goes well, Hafnium updates the stage-2 page tables of the
1709 * sender. Validation consists of checking if the message length and number of
1710 * memory region constituents match, and if the transition is valid for the
1711 * type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001712 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001713 * Assumes that the caller has already found and locked the sender VM and copied
1714 * the memory region descriptor from the sender's TX buffer to a freshly
1715 * allocated page from Hafnium's internal pool. The caller must have also
1716 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001717 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001718 * This function takes ownership of the `memory_region` passed in and will free
1719 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001720 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001721struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001722 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001723 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001724 uint32_t fragment_length, uint32_t share_func,
1725 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001726{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001727 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001728 struct share_states_locked share_states;
1729 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001730
1731 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001732 * If there is an error validating the `memory_region` then we need to
1733 * free it because we own it but we won't be storing it in a share state
1734 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001735 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001736 ret = ffa_memory_send_validate(from_locked, memory_region,
1737 memory_share_length, fragment_length,
J-Alves363f5722022-04-25 17:37:37 +01001738 share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001739 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001740 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001741 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001742 }
1743
Andrew Walbrana65a1322020-04-06 19:32:32 +01001744 /* Set flag for share function, ready to be retrieved later. */
1745 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001746 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001747 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001748 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001749 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001750 case FFA_MEM_LEND_32:
1751 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001752 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001753 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001754 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001755 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001756 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001757 }
1758
Andrew Walbranca808b12020-05-15 17:22:28 +01001759 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001760 /*
1761 * Allocate a share state before updating the page table. Otherwise if
1762 * updating the page table succeeded but allocating the share state
1763 * failed then it would leave the memory in a state where nobody could
1764 * get it back.
1765 */
Karl Meakin52cdfe72023-06-30 14:49:10 +01001766 share_state = allocate_share_state(share_states, share_func,
1767 memory_region, fragment_length,
1768 FFA_MEMORY_HANDLE_INVALID);
J-Alvesb56aac82023-11-10 09:44:43 +00001769 if (share_state == NULL) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001770 dlog_verbose("Failed to allocate share state.\n");
1771 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001772 ret = ffa_error(FFA_NO_MEMORY);
1773 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001774 }
1775
Andrew Walbranca808b12020-05-15 17:22:28 +01001776 if (fragment_length == memory_share_length) {
1777 /* No more fragments to come, everything fit in one message. */
J-Alves2a0d2882020-10-29 14:49:50 +00001778 ret = ffa_memory_send_complete(
1779 from_locked, share_states, share_state, page_pool,
1780 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001781 } else {
J-Alvesfdd29272022-07-19 13:16:31 +01001782 /*
1783 * Use sender ID from 'memory_region' assuming
1784 * that at this point it has been validated:
1785 * - MBZ at virtual FF-A instance.
1786 */
J-Alves19e20cf2023-08-02 12:48:55 +01001787 ffa_id_t sender_to_ret =
J-Alvesfdd29272022-07-19 13:16:31 +01001788 (from_locked.vm->id == HF_OTHER_WORLD_ID)
1789 ? memory_region->sender
1790 : 0;
Andrew Walbranca808b12020-05-15 17:22:28 +01001791 ret = (struct ffa_value){
1792 .func = FFA_MEM_FRAG_RX_32,
J-Alvesee68c542020-10-29 17:48:20 +00001793 .arg1 = (uint32_t)memory_region->handle,
1794 .arg2 = (uint32_t)(memory_region->handle >> 32),
J-Alvesfdd29272022-07-19 13:16:31 +01001795 .arg3 = fragment_length,
1796 .arg4 = (uint32_t)(sender_to_ret & 0xffff) << 16};
Andrew Walbranca808b12020-05-15 17:22:28 +01001797 }
1798
1799out:
1800 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001801 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001802 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001803}
1804
1805/**
J-Alves8505a8a2022-06-15 18:10:18 +01001806 * Continues an operation to donate, lend or share memory to a VM from current
1807 * world. If this is the last fragment then checks that the transition is valid
1808 * for the type of memory sending operation and updates the stage-2 page tables
1809 * of the sender.
Andrew Walbranca808b12020-05-15 17:22:28 +01001810 *
1811 * Assumes that the caller has already found and locked the sender VM and copied
1812 * the memory region descriptor from the sender's TX buffer to a freshly
1813 * allocated page from Hafnium's internal pool.
1814 *
1815 * This function takes ownership of the `fragment` passed in; it must not be
1816 * freed by the caller.
1817 */
1818struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1819 void *fragment,
1820 uint32_t fragment_length,
1821 ffa_memory_handle_t handle,
1822 struct mpool *page_pool)
1823{
1824 struct share_states_locked share_states = share_states_lock();
1825 struct ffa_memory_share_state *share_state;
1826 struct ffa_value ret;
1827 struct ffa_memory_region *memory_region;
1828
Demi Marie Obenour73a1e942023-02-04 14:09:18 -05001829 CHECK(is_aligned(fragment,
1830 alignof(struct ffa_memory_region_constituent)));
1831 if (fragment_length % sizeof(struct ffa_memory_region_constituent) !=
1832 0) {
1833 dlog_verbose("Fragment length %u misaligned.\n",
1834 fragment_length);
1835 ret = ffa_error(FFA_INVALID_PARAMETERS);
1836 goto out_free_fragment;
1837 }
1838
Andrew Walbranca808b12020-05-15 17:22:28 +01001839 ret = ffa_memory_send_continue_validate(share_states, handle,
1840 &share_state,
1841 from_locked.vm->id, page_pool);
1842 if (ret.func != FFA_SUCCESS_32) {
1843 goto out_free_fragment;
1844 }
1845 memory_region = share_state->memory_region;
1846
J-Alves95df0ef2022-12-07 10:09:48 +00001847 if (memory_region_receivers_from_other_world(memory_region)) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001848 dlog_error(
1849 "Got hypervisor-allocated handle for memory send to "
J-Alves8505a8a2022-06-15 18:10:18 +01001850 "other world. This should never happen, and indicates "
1851 "a bug in "
Andrew Walbranca808b12020-05-15 17:22:28 +01001852 "EL3 code.\n");
1853 ret = ffa_error(FFA_INVALID_PARAMETERS);
1854 goto out_free_fragment;
1855 }
1856
1857 /* Add this fragment. */
1858 share_state->fragments[share_state->fragment_count] = fragment;
1859 share_state->fragment_constituent_counts[share_state->fragment_count] =
1860 fragment_length / sizeof(struct ffa_memory_region_constituent);
1861 share_state->fragment_count++;
1862
1863 /* Check whether the memory send operation is now ready to complete. */
1864 if (share_state_sending_complete(share_states, share_state)) {
J-Alves2a0d2882020-10-29 14:49:50 +00001865 ret = ffa_memory_send_complete(
1866 from_locked, share_states, share_state, page_pool,
1867 &(share_state->sender_orig_mode));
Andrew Walbranca808b12020-05-15 17:22:28 +01001868 } else {
1869 ret = (struct ffa_value){
1870 .func = FFA_MEM_FRAG_RX_32,
1871 .arg1 = (uint32_t)handle,
1872 .arg2 = (uint32_t)(handle >> 32),
1873 .arg3 = share_state_next_fragment_offset(share_states,
1874 share_state)};
1875 }
1876 goto out;
1877
1878out_free_fragment:
1879 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001880
1881out:
Andrew Walbranca808b12020-05-15 17:22:28 +01001882 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001883 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001884}
1885
Andrew Walbranca808b12020-05-15 17:22:28 +01001886/** Clean up after the receiver has finished retrieving a memory region. */
1887static void ffa_memory_retrieve_complete(
1888 struct share_states_locked share_states,
1889 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
1890{
1891 if (share_state->share_func == FFA_MEM_DONATE_32) {
1892 /*
1893 * Memory that has been donated can't be relinquished,
1894 * so no need to keep the share state around.
1895 */
1896 share_state_free(share_states, share_state, page_pool);
1897 dlog_verbose("Freed share state for donate.\n");
1898 }
1899}
1900
J-Alves2d8457f2022-10-05 11:06:41 +01001901/**
1902 * Initialises the given memory region descriptor to be used for an
1903 * `FFA_MEM_RETRIEVE_RESP`, including the given constituents for the first
1904 * fragment.
1905 * The memory region descriptor is initialized according to retriever's
1906 * FF-A version.
1907 *
1908 * Returns true on success, or false if the given constituents won't all fit in
1909 * the first fragment.
1910 */
1911static bool ffa_retrieved_memory_region_init(
1912 void *response, uint32_t ffa_version, size_t response_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01001913 ffa_id_t sender, ffa_memory_attributes_t attributes,
J-Alves2d8457f2022-10-05 11:06:41 +01001914 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001915 ffa_id_t receiver_id, uint32_t memory_access_desc_size,
1916 ffa_memory_access_permissions_t permissions, uint32_t page_count,
1917 uint32_t total_constituent_count,
J-Alves2d8457f2022-10-05 11:06:41 +01001918 const struct ffa_memory_region_constituent constituents[],
1919 uint32_t fragment_constituent_count, uint32_t *total_length,
1920 uint32_t *fragment_length)
1921{
1922 struct ffa_composite_memory_region *composite_memory_region;
J-Alves2d8457f2022-10-05 11:06:41 +01001923 uint32_t i;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001924 uint32_t composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01001925 uint32_t constituents_offset;
1926 uint32_t receiver_count;
1927
1928 assert(response != NULL);
1929
1930 if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
1931 struct ffa_memory_region_v1_0 *retrieve_response =
1932 (struct ffa_memory_region_v1_0 *)response;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001933 struct ffa_memory_access_v1_0 *receiver;
J-Alves2d8457f2022-10-05 11:06:41 +01001934
J-Alves5da37d92022-10-24 16:33:48 +01001935 ffa_memory_region_init_header_v1_0(
1936 retrieve_response, sender, attributes, flags, handle, 0,
1937 RECEIVERS_COUNT_IN_RETRIEVE_RESP);
J-Alves2d8457f2022-10-05 11:06:41 +01001938
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001939 receiver = (struct ffa_memory_access_v1_0 *)
1940 retrieve_response->receivers;
J-Alves2d8457f2022-10-05 11:06:41 +01001941 receiver_count = retrieve_response->receiver_count;
1942
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001943 /*
1944 * Initialized here as in memory retrieve responses we currently
1945 * expect one borrower to be specified.
1946 */
1947 ffa_memory_access_v1_0_init_permissions(
1948 receiver, receiver_id,
1949 ffa_get_data_access_attr(permissions),
1950 ffa_get_instruction_access_attr(permissions), flags);
1951
1952 composite_offset =
J-Alves2d8457f2022-10-05 11:06:41 +01001953 sizeof(struct ffa_memory_region_v1_0) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001954 receiver_count * sizeof(struct ffa_memory_access_v1_0);
1955 receiver->composite_memory_region_offset = composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01001956
1957 composite_memory_region = ffa_memory_region_get_composite_v1_0(
1958 retrieve_response, 0);
1959 } else {
J-Alves2d8457f2022-10-05 11:06:41 +01001960 struct ffa_memory_region *retrieve_response =
1961 (struct ffa_memory_region *)response;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001962 struct ffa_memory_access *receiver;
J-Alves2d8457f2022-10-05 11:06:41 +01001963
1964 ffa_memory_region_init_header(retrieve_response, sender,
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001965 attributes, flags, handle, 0, 1,
1966 memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01001967
J-Alves2d8457f2022-10-05 11:06:41 +01001968 receiver_count = retrieve_response->receiver_count;
1969
1970 /*
1971 * Note that `sizeof(struct_ffa_memory_region)` and
1972 * `sizeof(struct ffa_memory_access)` must both be multiples of
1973 * 16 (as verified by the asserts in `ffa_memory.c`, so it is
1974 * guaranteed that the offset we calculate here is aligned to a
1975 * 64-bit boundary and so 64-bit values can be copied without
1976 * alignment faults.
1977 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001978 composite_offset =
J-Alves2d8457f2022-10-05 11:06:41 +01001979 sizeof(struct ffa_memory_region) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001980 (uint32_t)(receiver_count *
1981 retrieve_response->memory_access_desc_size);
J-Alves2d8457f2022-10-05 11:06:41 +01001982
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001983 receiver = ffa_memory_region_get_receiver(retrieve_response, 0);
1984 assert(receiver != NULL);
1985
1986 /*
1987 * Initialized here as in memory retrieve responses we currently
1988 * expect one borrower to be specified.
1989 */
1990 ffa_memory_access_init_permissions(
1991 receiver, receiver_id,
1992 ffa_get_data_access_attr(permissions),
1993 ffa_get_instruction_access_attr(permissions), flags);
1994 receiver->composite_memory_region_offset = composite_offset;
J-Alves2d8457f2022-10-05 11:06:41 +01001995 composite_memory_region =
1996 ffa_memory_region_get_composite(retrieve_response, 0);
1997 }
1998
J-Alves2d8457f2022-10-05 11:06:41 +01001999 assert(composite_memory_region != NULL);
2000
J-Alves2d8457f2022-10-05 11:06:41 +01002001 composite_memory_region->page_count = page_count;
2002 composite_memory_region->constituent_count = total_constituent_count;
2003 composite_memory_region->reserved_0 = 0;
2004
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002005 constituents_offset =
2006 composite_offset + sizeof(struct ffa_composite_memory_region);
J-Alves2d8457f2022-10-05 11:06:41 +01002007 if (constituents_offset +
2008 fragment_constituent_count *
2009 sizeof(struct ffa_memory_region_constituent) >
2010 response_max_size) {
2011 return false;
2012 }
2013
2014 for (i = 0; i < fragment_constituent_count; ++i) {
2015 composite_memory_region->constituents[i] = constituents[i];
2016 }
2017
2018 if (total_length != NULL) {
2019 *total_length =
2020 constituents_offset +
2021 composite_memory_region->constituent_count *
2022 sizeof(struct ffa_memory_region_constituent);
2023 }
2024 if (fragment_length != NULL) {
2025 *fragment_length =
2026 constituents_offset +
2027 fragment_constituent_count *
2028 sizeof(struct ffa_memory_region_constituent);
2029 }
2030
2031 return true;
2032}
2033
J-Alves96de29f2022-04-26 16:05:24 +01002034/*
2035 * Gets the receiver's access permissions from 'struct ffa_memory_region' and
2036 * returns its index in the receiver's array. If receiver's ID doesn't exist
2037 * in the array, return the region's 'receiver_count'.
2038 */
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002039uint32_t ffa_memory_region_get_receiver_index(
2040 struct ffa_memory_region *memory_region, ffa_id_t receiver_id)
J-Alves96de29f2022-04-26 16:05:24 +01002041{
J-Alves96de29f2022-04-26 16:05:24 +01002042 uint32_t i;
2043
2044 assert(memory_region != NULL);
2045
J-Alves96de29f2022-04-26 16:05:24 +01002046 for (i = 0U; i < memory_region->receiver_count; i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002047 struct ffa_memory_access *receiver =
2048 ffa_memory_region_get_receiver(memory_region, i);
2049 assert(receiver != NULL);
2050 if (receiver->receiver_permissions.receiver == receiver_id) {
J-Alves96de29f2022-04-26 16:05:24 +01002051 break;
2052 }
2053 }
2054
2055 return i;
2056}
2057
2058/**
2059 * Validates the retrieved permissions against those specified by the lender
2060 * of memory share operation. Optionally can help set the permissions to be used
2061 * for the S2 mapping, through the `permissions` argument.
J-Alvesdcad8992023-09-15 14:10:35 +01002062 * Returns FFA_SUCCESS if all the fields are valid. FFA_ERROR, with error code:
2063 * - FFA_INVALID_PARAMETERS -> if the fields have invalid values as per the
2064 * specification for each ABI.
2065 * - FFA_DENIED -> if the permissions specified by the retriever are not
2066 * less permissive than those provided by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002067 */
J-Alvesdcad8992023-09-15 14:10:35 +01002068static struct ffa_value ffa_memory_retrieve_is_memory_access_valid(
2069 uint32_t share_func, enum ffa_data_access sent_data_access,
J-Alves96de29f2022-04-26 16:05:24 +01002070 enum ffa_data_access requested_data_access,
2071 enum ffa_instruction_access sent_instruction_access,
2072 enum ffa_instruction_access requested_instruction_access,
J-Alvesdcad8992023-09-15 14:10:35 +01002073 ffa_memory_access_permissions_t *permissions, bool multiple_borrowers)
J-Alves96de29f2022-04-26 16:05:24 +01002074{
2075 switch (sent_data_access) {
2076 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2077 case FFA_DATA_ACCESS_RW:
2078 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2079 requested_data_access == FFA_DATA_ACCESS_RW) {
2080 if (permissions != NULL) {
2081 ffa_set_data_access_attr(permissions,
2082 FFA_DATA_ACCESS_RW);
2083 }
2084 break;
2085 }
2086 /* Intentional fall-through. */
2087 case FFA_DATA_ACCESS_RO:
2088 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2089 requested_data_access == FFA_DATA_ACCESS_RO) {
2090 if (permissions != NULL) {
2091 ffa_set_data_access_attr(permissions,
2092 FFA_DATA_ACCESS_RO);
2093 }
2094 break;
2095 }
2096 dlog_verbose(
2097 "Invalid data access requested; sender specified "
2098 "permissions %#x but receiver requested %#x.\n",
2099 sent_data_access, requested_data_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002100 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002101 case FFA_DATA_ACCESS_RESERVED:
2102 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
2103 "checked before this point.");
2104 }
2105
J-Alvesdcad8992023-09-15 14:10:35 +01002106 /*
2107 * For operations with a single borrower, If it is an FFA_MEMORY_LEND
2108 * or FFA_MEMORY_DONATE the retriever should have specifed the
2109 * instruction permissions it wishes to receive.
2110 */
2111 switch (share_func) {
2112 case FFA_MEM_SHARE_32:
2113 if (requested_instruction_access !=
2114 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2115 dlog_verbose(
2116 "%s: for share instruction permissions must "
2117 "NOT be specified.\n",
2118 __func__);
2119 return ffa_error(FFA_INVALID_PARAMETERS);
2120 }
2121 break;
2122 case FFA_MEM_LEND_32:
2123 /*
2124 * For operations with multiple borrowers only permit XN
2125 * permissions, and both Sender and borrower should have used
2126 * FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED.
2127 */
2128 if (multiple_borrowers) {
2129 if (requested_instruction_access !=
2130 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2131 dlog_verbose(
2132 "%s: lend/share/donate with multiple "
2133 "borrowers "
2134 "instruction permissions must NOT be "
2135 "specified.\n",
2136 __func__);
2137 return ffa_error(FFA_INVALID_PARAMETERS);
2138 }
2139 break;
2140 }
2141 /* Fall through if the operation targets a single borrower. */
2142 case FFA_MEM_DONATE_32:
2143 if (!multiple_borrowers &&
2144 requested_instruction_access ==
2145 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
2146 dlog_verbose(
2147 "%s: for lend/donate with single borrower "
2148 "instruction permissions must be speficified "
2149 "by borrower\n",
2150 __func__);
2151 return ffa_error(FFA_INVALID_PARAMETERS);
2152 }
2153 break;
2154 default:
2155 panic("%s: Wrong func id provided.\n", __func__);
2156 }
2157
J-Alves96de29f2022-04-26 16:05:24 +01002158 switch (sent_instruction_access) {
2159 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2160 case FFA_INSTRUCTION_ACCESS_X:
J-Alvesdcad8992023-09-15 14:10:35 +01002161 if (requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
J-Alves96de29f2022-04-26 16:05:24 +01002162 if (permissions != NULL) {
2163 ffa_set_instruction_access_attr(
2164 permissions, FFA_INSTRUCTION_ACCESS_X);
2165 }
2166 break;
2167 }
J-Alvesdcad8992023-09-15 14:10:35 +01002168 /*
2169 * Fall through if requested permissions are less
2170 * permissive than those provided by the sender.
2171 */
J-Alves96de29f2022-04-26 16:05:24 +01002172 case FFA_INSTRUCTION_ACCESS_NX:
2173 if (requested_instruction_access ==
2174 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2175 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2176 if (permissions != NULL) {
2177 ffa_set_instruction_access_attr(
2178 permissions, FFA_INSTRUCTION_ACCESS_NX);
2179 }
2180 break;
2181 }
2182 dlog_verbose(
2183 "Invalid instruction access requested; sender "
2184 "specified permissions %#x but receiver requested "
2185 "%#x.\n",
2186 sent_instruction_access, requested_instruction_access);
J-Alvesdcad8992023-09-15 14:10:35 +01002187 return ffa_error(FFA_DENIED);
J-Alves96de29f2022-04-26 16:05:24 +01002188 case FFA_INSTRUCTION_ACCESS_RESERVED:
2189 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
2190 "be checked before this point.");
2191 }
2192
J-Alvesdcad8992023-09-15 14:10:35 +01002193 return (struct ffa_value){.func = FFA_SUCCESS_32};
J-Alves96de29f2022-04-26 16:05:24 +01002194}
2195
2196/**
2197 * Validate the receivers' permissions in the retrieve request against those
2198 * specified by the lender.
2199 * In the `permissions` argument returns the permissions to set at S2 for the
2200 * caller to the FFA_MEMORY_RETRIEVE_REQ.
J-Alves3456e032023-07-20 12:20:05 +01002201 * The function looks into the flag to bypass multiple borrower checks:
2202 * - If not set returns FFA_SUCCESS if all specified permissions are valid.
2203 * - If set returns FFA_SUCCESS if the descriptor contains the permissions
2204 * to the caller of FFA_MEM_RETRIEVE_REQ and they are valid. Other permissions
2205 * are ignored, if provided.
J-Alves96de29f2022-04-26 16:05:24 +01002206 */
2207static struct ffa_value ffa_memory_retrieve_validate_memory_access_list(
2208 struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01002209 struct ffa_memory_region *retrieve_request, ffa_id_t to_vm_id,
J-Alvesdcad8992023-09-15 14:10:35 +01002210 ffa_memory_access_permissions_t *permissions, uint32_t func_id)
J-Alves96de29f2022-04-26 16:05:24 +01002211{
2212 uint32_t retrieve_receiver_index;
J-Alves3456e032023-07-20 12:20:05 +01002213 bool bypass_multi_receiver_check =
2214 (retrieve_request->flags &
2215 FFA_MEMORY_REGION_FLAG_BYPASS_BORROWERS_CHECK) != 0U;
J-Alvesdcad8992023-09-15 14:10:35 +01002216 const uint32_t region_receiver_count = memory_region->receiver_count;
2217 struct ffa_value ret;
J-Alves96de29f2022-04-26 16:05:24 +01002218
2219 assert(permissions != NULL);
2220
J-Alves3456e032023-07-20 12:20:05 +01002221 if (!bypass_multi_receiver_check) {
J-Alvesdcad8992023-09-15 14:10:35 +01002222 if (retrieve_request->receiver_count != region_receiver_count) {
J-Alves3456e032023-07-20 12:20:05 +01002223 dlog_verbose(
2224 "Retrieve request should contain same list of "
2225 "borrowers, as specified by the lender.\n");
2226 return ffa_error(FFA_INVALID_PARAMETERS);
2227 }
2228 } else {
2229 if (retrieve_request->receiver_count != 1) {
2230 dlog_verbose(
2231 "Set bypass multiple borrower check, receiver "
2232 "list must be sized 1 (%x)\n",
2233 memory_region->receiver_count);
2234 return ffa_error(FFA_INVALID_PARAMETERS);
2235 }
J-Alves96de29f2022-04-26 16:05:24 +01002236 }
2237
2238 retrieve_receiver_index = retrieve_request->receiver_count;
2239
2240 /* Should be populated with the permissions of the retriever. */
2241 *permissions = 0;
2242
2243 for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) {
2244 ffa_memory_access_permissions_t sent_permissions;
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002245 struct ffa_memory_access *retrieve_request_receiver =
2246 ffa_memory_region_get_receiver(retrieve_request, i);
2247 assert(retrieve_request_receiver != NULL);
J-Alves96de29f2022-04-26 16:05:24 +01002248 ffa_memory_access_permissions_t requested_permissions =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002249 retrieve_request_receiver->receiver_permissions
2250 .permissions;
J-Alves19e20cf2023-08-02 12:48:55 +01002251 ffa_id_t current_receiver_id =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002252 retrieve_request_receiver->receiver_permissions
2253 .receiver;
J-Alves96de29f2022-04-26 16:05:24 +01002254 bool found_to_id = current_receiver_id == to_vm_id;
2255
J-Alves3456e032023-07-20 12:20:05 +01002256 if (bypass_multi_receiver_check && !found_to_id) {
2257 dlog_verbose(
2258 "Bypass multiple borrower check for id %x.\n",
2259 current_receiver_id);
2260 continue;
2261 }
2262
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002263 if (retrieve_request_receiver->composite_memory_region_offset !=
2264 0U) {
2265 dlog_verbose(
2266 "Retriever specified address ranges not "
2267 "supported (got offset %d).\n",
2268 retrieve_request_receiver
2269 ->composite_memory_region_offset);
2270 return ffa_error(FFA_INVALID_PARAMETERS);
2271 }
2272
J-Alves96de29f2022-04-26 16:05:24 +01002273 /*
2274 * Find the current receiver in the transaction descriptor from
2275 * sender.
2276 */
2277 uint32_t mem_region_receiver_index =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002278 ffa_memory_region_get_receiver_index(
2279 memory_region, current_receiver_id);
J-Alves96de29f2022-04-26 16:05:24 +01002280
2281 if (mem_region_receiver_index ==
2282 memory_region->receiver_count) {
2283 dlog_verbose("%s: receiver %x not found\n", __func__,
2284 current_receiver_id);
2285 return ffa_error(FFA_DENIED);
2286 }
2287
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002288 struct ffa_memory_access *receiver =
2289 ffa_memory_region_get_receiver(
2290 memory_region, mem_region_receiver_index);
2291 assert(receiver != NULL);
2292
2293 sent_permissions = receiver->receiver_permissions.permissions;
J-Alves96de29f2022-04-26 16:05:24 +01002294
2295 if (found_to_id) {
2296 retrieve_receiver_index = i;
2297 }
2298
2299 /*
J-Alvesdcad8992023-09-15 14:10:35 +01002300 * Check if retrieve request memory access list is valid:
2301 * - The retrieve request complies with the specification.
2302 * - Permissions are within those specified by the sender.
J-Alves96de29f2022-04-26 16:05:24 +01002303 */
J-Alvesdcad8992023-09-15 14:10:35 +01002304 ret = ffa_memory_retrieve_is_memory_access_valid(
2305 func_id, ffa_get_data_access_attr(sent_permissions),
2306 ffa_get_data_access_attr(requested_permissions),
2307 ffa_get_instruction_access_attr(sent_permissions),
2308 ffa_get_instruction_access_attr(requested_permissions),
2309 found_to_id ? permissions : NULL,
2310 region_receiver_count > 1);
2311 if (ret.func != FFA_SUCCESS_32) {
2312 return ret;
J-Alves96de29f2022-04-26 16:05:24 +01002313 }
2314
2315 /*
2316 * Can't request PM to clear memory if only provided with RO
2317 * permissions.
2318 */
2319 if (found_to_id &&
2320 (ffa_get_data_access_attr(*permissions) ==
2321 FFA_DATA_ACCESS_RO) &&
2322 (retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
2323 0U) {
2324 dlog_verbose(
2325 "Receiver has RO permissions can not request "
2326 "clear.\n");
2327 return ffa_error(FFA_DENIED);
2328 }
2329 }
2330
2331 if (retrieve_receiver_index == retrieve_request->receiver_count) {
2332 dlog_verbose(
2333 "Retrieve request does not contain caller's (%x) "
2334 "permissions\n",
2335 to_vm_id);
2336 return ffa_error(FFA_INVALID_PARAMETERS);
2337 }
2338
2339 return (struct ffa_value){.func = FFA_SUCCESS_32};
2340}
2341
J-Alvesa9cd7e32022-07-01 13:49:33 +01002342/*
2343 * According to section 16.4.3 of FF-A v1.1 EAC0 specification, the hypervisor
2344 * may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region description
2345 * of a pending memory sharing operation whose allocator is the SPM, for
2346 * validation purposes before forwarding an FFA_MEM_RECLAIM call. In doing so
2347 * the memory region descriptor of the retrieve request must be zeroed with the
2348 * exception of the sender ID and handle.
2349 */
2350bool is_ffa_memory_retrieve_borrower_request(struct ffa_memory_region *request,
2351 struct vm_locked to_locked)
2352{
2353 return to_locked.vm->id == HF_HYPERVISOR_VM_ID &&
2354 request->attributes == 0U && request->flags == 0U &&
2355 request->tag == 0U && request->receiver_count == 0U &&
2356 plat_ffa_memory_handle_allocated_by_current_world(
2357 request->handle);
2358}
2359
2360/*
2361 * Helper to reset count of fragments retrieved by the hypervisor.
2362 */
2363static void ffa_memory_retrieve_complete_from_hyp(
2364 struct ffa_memory_share_state *share_state)
2365{
2366 if (share_state->hypervisor_fragment_count ==
2367 share_state->fragment_count) {
2368 share_state->hypervisor_fragment_count = 0;
2369 }
2370}
2371
J-Alves089004f2022-07-13 14:25:44 +01002372/**
2373 * Validate that the memory region descriptor provided by the borrower on
2374 * FFA_MEM_RETRIEVE_REQ, against saved memory region provided by lender at the
2375 * memory sharing call.
2376 */
2377static struct ffa_value ffa_memory_retrieve_validate(
J-Alves19e20cf2023-08-02 12:48:55 +01002378 ffa_id_t receiver_id, struct ffa_memory_region *retrieve_request,
J-Alves089004f2022-07-13 14:25:44 +01002379 struct ffa_memory_region *memory_region, uint32_t *receiver_index,
2380 uint32_t share_func)
2381{
2382 ffa_memory_region_flags_t transaction_type =
2383 retrieve_request->flags &
2384 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002385 enum ffa_memory_security security_state;
J-Alves089004f2022-07-13 14:25:44 +01002386
2387 assert(retrieve_request != NULL);
2388 assert(memory_region != NULL);
2389 assert(receiver_index != NULL);
2390 assert(retrieve_request->sender == memory_region->sender);
2391
2392 /*
2393 * Check that the transaction type expected by the receiver is
2394 * correct, if it has been specified.
2395 */
2396 if (transaction_type !=
2397 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
2398 transaction_type != (memory_region->flags &
2399 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
2400 dlog_verbose(
2401 "Incorrect transaction type %#x for "
2402 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
2403 transaction_type,
2404 memory_region->flags &
2405 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
2406 retrieve_request->handle);
2407 return ffa_error(FFA_INVALID_PARAMETERS);
2408 }
2409
2410 if (retrieve_request->tag != memory_region->tag) {
2411 dlog_verbose(
2412 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
2413 "%d for handle %#x.\n",
2414 retrieve_request->tag, memory_region->tag,
2415 retrieve_request->handle);
2416 return ffa_error(FFA_INVALID_PARAMETERS);
2417 }
2418
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002419 *receiver_index = ffa_memory_region_get_receiver_index(memory_region,
2420 receiver_id);
J-Alves089004f2022-07-13 14:25:44 +01002421
2422 if (*receiver_index == memory_region->receiver_count) {
2423 dlog_verbose(
2424 "Incorrect receiver VM ID %d for "
2425 "FFA_MEM_RETRIEVE_REQ, for handle %#x.\n",
J-Alves59ed0042022-07-28 18:26:41 +01002426 receiver_id, memory_region->handle);
J-Alves089004f2022-07-13 14:25:44 +01002427 return ffa_error(FFA_INVALID_PARAMETERS);
2428 }
2429
2430 if ((retrieve_request->flags &
2431 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) {
2432 dlog_verbose(
2433 "Retriever specified 'address range alignment 'hint' "
2434 "not supported.\n");
2435 return ffa_error(FFA_INVALID_PARAMETERS);
2436 }
2437 if ((retrieve_request->flags &
2438 FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) {
2439 dlog_verbose(
2440 "Bits 8-5 must be zero in memory region's flags "
2441 "(address range alignment hint not supported).\n");
2442 return ffa_error(FFA_INVALID_PARAMETERS);
2443 }
2444
2445 if ((retrieve_request->flags & ~0x7FF) != 0U) {
2446 dlog_verbose(
2447 "Bits 31-10 must be zero in memory region's flags.\n");
2448 return ffa_error(FFA_INVALID_PARAMETERS);
2449 }
2450
2451 if (share_func == FFA_MEM_SHARE_32 &&
2452 (retrieve_request->flags &
2453 (FFA_MEMORY_REGION_FLAG_CLEAR |
2454 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) {
2455 dlog_verbose(
2456 "Memory Share operation can't clean after relinquish "
2457 "memory region.\n");
2458 return ffa_error(FFA_INVALID_PARAMETERS);
2459 }
2460
2461 /*
2462 * If the borrower needs the memory to be cleared before mapping
2463 * to its address space, the sender should have set the flag
2464 * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return
2465 * FFA_DENIED.
2466 */
2467 if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != 0U &&
2468 (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == 0U) {
2469 dlog_verbose(
2470 "Borrower needs memory cleared. Sender needs to set "
2471 "flag for clearing memory.\n");
2472 return ffa_error(FFA_DENIED);
2473 }
2474
Olivier Deprez4342a3c2022-02-28 09:37:25 +01002475 /* Memory region attributes NS-Bit MBZ for FFA_MEM_RETRIEVE_REQ. */
2476 security_state =
2477 ffa_get_memory_security_attr(retrieve_request->attributes);
2478 if (security_state != FFA_MEMORY_SECURITY_UNSPECIFIED) {
2479 dlog_verbose(
2480 "Invalid security state for memory retrieve request "
2481 "operation.\n");
2482 return ffa_error(FFA_INVALID_PARAMETERS);
2483 }
2484
J-Alves089004f2022-07-13 14:25:44 +01002485 /*
2486 * If memory type is not specified, bypass validation of memory
2487 * attributes in the retrieve request. The retriever is expecting to
2488 * obtain this information from the SPMC.
2489 */
2490 if (ffa_get_memory_type_attr(retrieve_request->attributes) ==
2491 FFA_MEMORY_NOT_SPECIFIED_MEM) {
2492 return (struct ffa_value){.func = FFA_SUCCESS_32};
2493 }
2494
2495 /*
2496 * Ensure receiver's attributes are compatible with how
2497 * Hafnium maps memory: Normal Memory, Inner shareable,
2498 * Write-Back Read-Allocate Write-Allocate Cacheable.
2499 */
2500 return ffa_memory_attributes_validate(retrieve_request->attributes);
2501}
2502
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002503struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2504 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002505 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002506 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002507{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002508 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002509 sizeof(struct ffa_memory_region) +
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002510 (uint32_t)(retrieve_request->receiver_count *
2511 retrieve_request->memory_access_desc_size);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002512 ffa_memory_handle_t handle = retrieve_request->handle;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002513 struct ffa_memory_region *memory_region;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002514 ffa_memory_access_permissions_t permissions = 0;
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002515 uint32_t memory_to_mode;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002516 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002517 struct ffa_memory_share_state *share_state;
2518 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002519 struct ffa_composite_memory_region *composite;
2520 uint32_t total_length;
2521 uint32_t fragment_length;
J-Alves19e20cf2023-08-02 12:48:55 +01002522 ffa_id_t receiver_id = to_locked.vm->id;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002523 bool is_send_complete = false;
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002524 ffa_memory_attributes_t attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002525
2526 dump_share_states();
2527
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002528 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002529 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002530 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002531 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002532 expected_retrieve_request_length,
2533 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002534 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002535 }
2536
2537 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01002538 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00002539 if (share_state == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002540 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002541 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002542 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002543 goto out;
2544 }
2545
J-Alves96de29f2022-04-26 16:05:24 +01002546 if (!share_state->sending_complete) {
2547 dlog_verbose(
2548 "Memory with handle %#x not fully sent, can't "
2549 "retrieve.\n",
2550 handle);
2551 ret = ffa_error(FFA_INVALID_PARAMETERS);
2552 goto out;
2553 }
2554
Andrew Walbrana65a1322020-04-06 19:32:32 +01002555 memory_region = share_state->memory_region;
J-Alves089004f2022-07-13 14:25:44 +01002556
Andrew Walbrana65a1322020-04-06 19:32:32 +01002557 CHECK(memory_region != NULL);
2558
J-Alves089004f2022-07-13 14:25:44 +01002559 if (retrieve_request->sender != memory_region->sender) {
2560 dlog_verbose(
2561 "Memory with handle %#x not fully sent, can't "
2562 "retrieve.\n",
2563 handle);
J-Alves41d4fef2023-11-16 16:20:09 +00002564 ret = ffa_error(FFA_DENIED);
J-Alves089004f2022-07-13 14:25:44 +01002565 goto out;
2566 }
J-Alves96de29f2022-04-26 16:05:24 +01002567
J-Alvesa9cd7e32022-07-01 13:49:33 +01002568 if (!is_ffa_memory_retrieve_borrower_request(retrieve_request,
2569 to_locked)) {
2570 uint32_t receiver_index;
J-Alvesa9cd7e32022-07-01 13:49:33 +01002571
J-Alvesb5084cf2022-07-06 14:20:12 +01002572 /*
2573 * The SPMC can only process retrieve requests to memory share
2574 * operations with one borrower from the other world. It can't
2575 * determine the ID of the NWd VM that invoked the retrieve
2576 * request interface call. It relies on the hypervisor to
2577 * validate the caller's ID against that provided in the
2578 * `receivers` list of the retrieve response.
2579 * In case there is only one borrower from the NWd in the
2580 * transaction descriptor, record that in the `receiver_id` for
2581 * later use, and validate in the retrieve request message.
J-Alves3fa82aa2023-09-20 18:19:21 +01002582 * This limitation is due to the fact SPMC can't determine the
2583 * index in the memory share structures state to update.
J-Alvesb5084cf2022-07-06 14:20:12 +01002584 */
2585 if (to_locked.vm->id == HF_HYPERVISOR_VM_ID) {
2586 uint32_t other_world_count = 0;
2587
2588 for (uint32_t i = 0; i < memory_region->receiver_count;
2589 i++) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002590 struct ffa_memory_access *receiver =
2591 ffa_memory_region_get_receiver(
2592 retrieve_request, 0);
2593
2594 assert(receiver != NULL);
J-Alvesb5084cf2022-07-06 14:20:12 +01002595 receiver_id =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002596 receiver->receiver_permissions.receiver;
J-Alvesb5084cf2022-07-06 14:20:12 +01002597 if (!vm_id_is_current_world(receiver_id)) {
2598 other_world_count++;
2599 }
2600 }
2601 if (other_world_count > 1) {
2602 dlog_verbose(
2603 "Support one receiver from the other "
2604 "world.\n");
2605 return ffa_error(FFA_NOT_SUPPORTED);
2606 }
2607 }
2608
2609 /*
2610 * Validate retrieve request, according to what was sent by the
2611 * sender. Function will output the `receiver_index` from the
J-Alves3fa82aa2023-09-20 18:19:21 +01002612 * provided memory region.
J-Alvesb5084cf2022-07-06 14:20:12 +01002613 */
J-Alves089004f2022-07-13 14:25:44 +01002614 ret = ffa_memory_retrieve_validate(
2615 receiver_id, retrieve_request, memory_region,
2616 &receiver_index, share_state->share_func);
2617 if (ret.func != FFA_SUCCESS_32) {
J-Alvesa9cd7e32022-07-01 13:49:33 +01002618 goto out;
2619 }
2620
2621 if (share_state->retrieved_fragment_count[receiver_index] !=
2622 0U) {
2623 dlog_verbose(
2624 "Memory with handle %#x already retrieved.\n",
2625 handle);
2626 ret = ffa_error(FFA_DENIED);
2627 goto out;
2628 }
2629
J-Alves3fa82aa2023-09-20 18:19:21 +01002630 /*
2631 * Validate the requested permissions against the sent
2632 * permissions.
2633 * Outputs the permissions to give to retriever at S2
2634 * PTs.
2635 */
J-Alvesa9cd7e32022-07-01 13:49:33 +01002636 ret = ffa_memory_retrieve_validate_memory_access_list(
2637 memory_region, retrieve_request, receiver_id,
J-Alvesdcad8992023-09-15 14:10:35 +01002638 &permissions, share_state->share_func);
J-Alves614d9f42022-06-28 14:03:10 +01002639 if (ret.func != FFA_SUCCESS_32) {
2640 goto out;
2641 }
Federico Recanatia98603a2021-12-20 18:04:03 +01002642
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002643 memory_to_mode = ffa_memory_permissions_to_mode(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002644 permissions, share_state->sender_orig_mode);
J-Alves40e260e2022-09-22 17:52:43 +01002645
J-Alvesa9cd7e32022-07-01 13:49:33 +01002646 ret = ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01002647 to_locked, share_state->fragments,
J-Alvesa9cd7e32022-07-01 13:49:33 +01002648 share_state->fragment_constituent_counts,
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002649 share_state->fragment_count, memory_to_mode,
J-Alvesa9cd7e32022-07-01 13:49:33 +01002650 share_state->share_func, false, page_pool);
2651
2652 if (ret.func != FFA_SUCCESS_32) {
2653 goto out;
2654 }
2655
2656 share_state->retrieved_fragment_count[receiver_index] = 1;
2657 is_send_complete =
2658 share_state->retrieved_fragment_count[receiver_index] ==
2659 share_state->fragment_count;
J-Alves3c5b2072022-11-21 12:45:40 +00002660
2661 share_state->clear_after_relinquish =
2662 (retrieve_request->flags &
2663 FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH) != 0U;
2664
J-Alvesa9cd7e32022-07-01 13:49:33 +01002665 } else {
2666 if (share_state->hypervisor_fragment_count != 0U) {
2667 dlog_verbose(
J-Alvesb5084cf2022-07-06 14:20:12 +01002668 "Memory with handle %#x already retrieved by "
J-Alvesa9cd7e32022-07-01 13:49:33 +01002669 "the hypervisor.\n",
2670 handle);
2671 ret = ffa_error(FFA_DENIED);
2672 goto out;
2673 }
2674
2675 share_state->hypervisor_fragment_count = 1;
2676
2677 ffa_memory_retrieve_complete_from_hyp(share_state);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002678 }
2679
J-Alvesb5084cf2022-07-06 14:20:12 +01002680 /* VMs acquire the RX buffer from SPMC. */
2681 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
2682
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002683 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002684 * Copy response to RX buffer of caller and deliver the message.
2685 * This must be done before the share_state is (possibly) freed.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002686 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002687 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002688 composite = ffa_memory_region_get_composite(memory_region, 0);
2689 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002690 * Constituents which we received in the first fragment should
2691 * always fit in the first fragment we are sending, because the
2692 * header is the same size in both cases and we have a fixed
2693 * message buffer size. So `ffa_retrieved_memory_region_init`
2694 * should never fail.
Andrew Walbranca808b12020-05-15 17:22:28 +01002695 */
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002696
2697 /*
2698 * Set the security state in the memory retrieve response attributes
2699 * if specified by the target mode.
2700 */
2701 attributes = plat_ffa_memory_security_mode(
2702 memory_region->attributes, share_state->sender_orig_mode);
2703
Andrew Walbranca808b12020-05-15 17:22:28 +01002704 CHECK(ffa_retrieved_memory_region_init(
J-Alves2d8457f2022-10-05 11:06:41 +01002705 to_locked.vm->mailbox.recv, to_locked.vm->ffa_version,
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002706 HF_MAILBOX_SIZE, memory_region->sender, attributes,
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002707 memory_region->flags, handle, receiver_id,
2708 memory_region->memory_access_desc_size, permissions,
Olivier Deprez878bd5b2021-04-15 19:05:10 +02002709 composite->page_count, composite->constituent_count,
2710 share_state->fragments[0],
Andrew Walbranca808b12020-05-15 17:22:28 +01002711 share_state->fragment_constituent_counts[0], &total_length,
2712 &fragment_length));
J-Alvesb5084cf2022-07-06 14:20:12 +01002713
Andrew Walbranca808b12020-05-15 17:22:28 +01002714 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002715 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002716 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00002717 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002718
J-Alvesa9cd7e32022-07-01 13:49:33 +01002719 if (is_send_complete) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002720 ffa_memory_retrieve_complete(share_states, share_state,
2721 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002722 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002723 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002724 .arg1 = total_length,
2725 .arg2 = fragment_length};
Andrew Walbranca808b12020-05-15 17:22:28 +01002726out:
2727 share_states_unlock(&share_states);
2728 dump_share_states();
2729 return ret;
2730}
2731
J-Alves5da37d92022-10-24 16:33:48 +01002732/**
2733 * Determine expected fragment offset according to the FF-A version of
2734 * the caller.
2735 */
2736static uint32_t ffa_memory_retrieve_expected_offset_per_ffa_version(
2737 struct ffa_memory_region *memory_region,
2738 uint32_t retrieved_constituents_count, uint32_t ffa_version)
2739{
2740 uint32_t expected_fragment_offset;
2741 uint32_t composite_constituents_offset;
2742
Kathleen Capellae4fe2962023-09-01 17:08:47 -04002743 if (ffa_version >= MAKE_FFA_VERSION(1, 1)) {
J-Alves5da37d92022-10-24 16:33:48 +01002744 /*
2745 * Hafnium operates memory regions in FF-A v1.1 format, so we
2746 * can retrieve the constituents offset from descriptor.
2747 */
2748 composite_constituents_offset =
2749 ffa_composite_constituent_offset(memory_region, 0);
2750 } else if (ffa_version == MAKE_FFA_VERSION(1, 0)) {
2751 /*
2752 * If retriever is FF-A v1.0, determine the composite offset
2753 * as it is expected to have been configured in the
2754 * retrieve response.
2755 */
2756 composite_constituents_offset =
2757 sizeof(struct ffa_memory_region_v1_0) +
2758 RECEIVERS_COUNT_IN_RETRIEVE_RESP *
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002759 sizeof(struct ffa_memory_access_v1_0) +
J-Alves5da37d92022-10-24 16:33:48 +01002760 sizeof(struct ffa_composite_memory_region);
2761 } else {
2762 panic("%s received an invalid FF-A version.\n", __func__);
2763 }
2764
2765 expected_fragment_offset =
2766 composite_constituents_offset +
2767 retrieved_constituents_count *
2768 sizeof(struct ffa_memory_region_constituent) -
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002769 (uint32_t)(memory_region->memory_access_desc_size *
2770 (memory_region->receiver_count - 1));
J-Alves5da37d92022-10-24 16:33:48 +01002771
2772 return expected_fragment_offset;
2773}
2774
Andrew Walbranca808b12020-05-15 17:22:28 +01002775struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2776 ffa_memory_handle_t handle,
2777 uint32_t fragment_offset,
J-Alves19e20cf2023-08-02 12:48:55 +01002778 ffa_id_t sender_vm_id,
Andrew Walbranca808b12020-05-15 17:22:28 +01002779 struct mpool *page_pool)
2780{
2781 struct ffa_memory_region *memory_region;
2782 struct share_states_locked share_states;
2783 struct ffa_memory_share_state *share_state;
2784 struct ffa_value ret;
2785 uint32_t fragment_index;
2786 uint32_t retrieved_constituents_count;
2787 uint32_t i;
2788 uint32_t expected_fragment_offset;
2789 uint32_t remaining_constituent_count;
2790 uint32_t fragment_length;
J-Alvesc7484f12022-05-13 12:41:14 +01002791 uint32_t receiver_index;
J-Alves59ed0042022-07-28 18:26:41 +01002792 bool continue_ffa_hyp_mem_retrieve_req;
Andrew Walbranca808b12020-05-15 17:22:28 +01002793
2794 dump_share_states();
2795
2796 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01002797 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00002798 if (share_state == NULL) {
Andrew Walbranca808b12020-05-15 17:22:28 +01002799 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2800 handle);
2801 ret = ffa_error(FFA_INVALID_PARAMETERS);
2802 goto out;
2803 }
2804
2805 memory_region = share_state->memory_region;
2806 CHECK(memory_region != NULL);
2807
Andrew Walbranca808b12020-05-15 17:22:28 +01002808 if (!share_state->sending_complete) {
2809 dlog_verbose(
2810 "Memory with handle %#x not fully sent, can't "
2811 "retrieve.\n",
2812 handle);
2813 ret = ffa_error(FFA_INVALID_PARAMETERS);
2814 goto out;
2815 }
2816
J-Alves59ed0042022-07-28 18:26:41 +01002817 /*
2818 * If retrieve request from the hypervisor has been initiated in the
2819 * given share_state, continue it, else assume it is a continuation of
2820 * retrieve request from a NWd VM.
2821 */
2822 continue_ffa_hyp_mem_retrieve_req =
2823 (to_locked.vm->id == HF_HYPERVISOR_VM_ID) &&
2824 (share_state->hypervisor_fragment_count != 0U) &&
J-Alves661e1b72023-08-02 13:39:40 +01002825 ffa_is_vm_id(sender_vm_id);
Andrew Walbranca808b12020-05-15 17:22:28 +01002826
J-Alves59ed0042022-07-28 18:26:41 +01002827 if (!continue_ffa_hyp_mem_retrieve_req) {
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002828 receiver_index = ffa_memory_region_get_receiver_index(
J-Alves59ed0042022-07-28 18:26:41 +01002829 memory_region, to_locked.vm->id);
2830
2831 if (receiver_index == memory_region->receiver_count) {
2832 dlog_verbose(
2833 "Caller of FFA_MEM_FRAG_RX (%x) is not a "
2834 "borrower to memory sharing transaction (%x)\n",
2835 to_locked.vm->id, handle);
2836 ret = ffa_error(FFA_INVALID_PARAMETERS);
2837 goto out;
2838 }
2839
2840 if (share_state->retrieved_fragment_count[receiver_index] ==
2841 0 ||
2842 share_state->retrieved_fragment_count[receiver_index] >=
2843 share_state->fragment_count) {
2844 dlog_verbose(
2845 "Retrieval of memory with handle %#x not yet "
2846 "started or already completed (%d/%d fragments "
2847 "retrieved).\n",
2848 handle,
2849 share_state->retrieved_fragment_count
2850 [receiver_index],
2851 share_state->fragment_count);
2852 ret = ffa_error(FFA_INVALID_PARAMETERS);
2853 goto out;
2854 }
2855
2856 fragment_index =
2857 share_state->retrieved_fragment_count[receiver_index];
2858 } else {
2859 if (share_state->hypervisor_fragment_count == 0 ||
2860 share_state->hypervisor_fragment_count >=
2861 share_state->fragment_count) {
2862 dlog_verbose(
2863 "Retrieve of memory with handle %x not "
2864 "started from hypervisor.\n",
2865 handle);
2866 ret = ffa_error(FFA_INVALID_PARAMETERS);
2867 goto out;
2868 }
2869
2870 if (memory_region->sender != sender_vm_id) {
2871 dlog_verbose(
2872 "Sender ID (%x) is not as expected for memory "
2873 "handle %x\n",
2874 sender_vm_id, handle);
2875 ret = ffa_error(FFA_INVALID_PARAMETERS);
2876 goto out;
2877 }
2878
2879 fragment_index = share_state->hypervisor_fragment_count;
2880
2881 receiver_index = 0;
2882 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002883
2884 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01002885 * Check that the given fragment offset is correct by counting
2886 * how many constituents were in the fragments previously sent.
Andrew Walbranca808b12020-05-15 17:22:28 +01002887 */
2888 retrieved_constituents_count = 0;
2889 for (i = 0; i < fragment_index; ++i) {
2890 retrieved_constituents_count +=
2891 share_state->fragment_constituent_counts[i];
2892 }
J-Alvesc7484f12022-05-13 12:41:14 +01002893
2894 CHECK(memory_region->receiver_count > 0);
2895
Andrew Walbranca808b12020-05-15 17:22:28 +01002896 expected_fragment_offset =
J-Alves5da37d92022-10-24 16:33:48 +01002897 ffa_memory_retrieve_expected_offset_per_ffa_version(
2898 memory_region, retrieved_constituents_count,
2899 to_locked.vm->ffa_version);
2900
Andrew Walbranca808b12020-05-15 17:22:28 +01002901 if (fragment_offset != expected_fragment_offset) {
2902 dlog_verbose("Fragment offset was %d but expected %d.\n",
2903 fragment_offset, expected_fragment_offset);
2904 ret = ffa_error(FFA_INVALID_PARAMETERS);
2905 goto out;
2906 }
2907
J-Alves59ed0042022-07-28 18:26:41 +01002908 /* VMs acquire the RX buffer from SPMC. */
2909 CHECK(plat_ffa_acquire_receiver_rx(to_locked, &ret));
2910
Andrew Walbranca808b12020-05-15 17:22:28 +01002911 remaining_constituent_count = ffa_memory_fragment_init(
2912 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2913 share_state->fragments[fragment_index],
2914 share_state->fragment_constituent_counts[fragment_index],
2915 &fragment_length);
2916 CHECK(remaining_constituent_count == 0);
2917 to_locked.vm->mailbox.recv_size = fragment_length;
2918 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2919 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
J-Alvese8c8c2b2022-12-16 15:34:48 +00002920 to_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
Andrew Walbranca808b12020-05-15 17:22:28 +01002921
J-Alves59ed0042022-07-28 18:26:41 +01002922 if (!continue_ffa_hyp_mem_retrieve_req) {
2923 share_state->retrieved_fragment_count[receiver_index]++;
2924 if (share_state->retrieved_fragment_count[receiver_index] ==
2925 share_state->fragment_count) {
2926 ffa_memory_retrieve_complete(share_states, share_state,
2927 page_pool);
2928 }
2929 } else {
2930 share_state->hypervisor_fragment_count++;
2931
2932 ffa_memory_retrieve_complete_from_hyp(share_state);
2933 }
Andrew Walbranca808b12020-05-15 17:22:28 +01002934 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2935 .arg1 = (uint32_t)handle,
2936 .arg2 = (uint32_t)(handle >> 32),
2937 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002938
2939out:
2940 share_states_unlock(&share_states);
2941 dump_share_states();
2942 return ret;
2943}
2944
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002945struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002946 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002947 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002948{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002949 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002950 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002951 struct ffa_memory_share_state *share_state;
2952 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002953 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002954 struct ffa_value ret;
J-Alves8eb19162022-04-28 10:56:48 +01002955 uint32_t receiver_index;
J-Alves3c5b2072022-11-21 12:45:40 +00002956 bool receivers_relinquished_memory;
J-Alves639ddfc2023-11-21 14:17:26 +00002957 ffa_memory_access_permissions_t receiver_permissions = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002958
Andrew Walbrana65a1322020-04-06 19:32:32 +01002959 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002960 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002961 "Stream endpoints not supported (got %d "
J-Alves668a86e2023-05-10 11:53:25 +01002962 "endpoints on FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002963 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002964 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002965 }
2966
Andrew Walbrana65a1322020-04-06 19:32:32 +01002967 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002968 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01002969 "VM ID %d in relinquish message doesn't match "
J-Alves668a86e2023-05-10 11:53:25 +01002970 "calling VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002971 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002972 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002973 }
2974
2975 dump_share_states();
2976
2977 share_states = share_states_lock();
Karl Meakin4a2854a2023-06-30 16:26:52 +01002978 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00002979 if (share_state == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002980 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002981 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002982 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002983 goto out;
2984 }
2985
Andrew Walbranca808b12020-05-15 17:22:28 +01002986 if (!share_state->sending_complete) {
2987 dlog_verbose(
2988 "Memory with handle %#x not fully sent, can't "
2989 "relinquish.\n",
2990 handle);
2991 ret = ffa_error(FFA_INVALID_PARAMETERS);
2992 goto out;
2993 }
2994
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002995 memory_region = share_state->memory_region;
2996 CHECK(memory_region != NULL);
2997
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00002998 receiver_index = ffa_memory_region_get_receiver_index(
2999 memory_region, from_locked.vm->id);
J-Alves8eb19162022-04-28 10:56:48 +01003000
3001 if (receiver_index == memory_region->receiver_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003002 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003003 "VM ID %d tried to relinquish memory region "
J-Alves668a86e2023-05-10 11:53:25 +01003004 "with handle %#x and it is not a valid borrower.\n",
J-Alves8eb19162022-04-28 10:56:48 +01003005 from_locked.vm->id, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003006 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003007 goto out;
3008 }
3009
J-Alves8eb19162022-04-28 10:56:48 +01003010 if (share_state->retrieved_fragment_count[receiver_index] !=
Andrew Walbranca808b12020-05-15 17:22:28 +01003011 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003012 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003013 "Memory with handle %#x not yet fully "
3014 "retrieved, "
J-Alves8eb19162022-04-28 10:56:48 +01003015 "receiver %x can't relinquish.\n",
3016 handle, from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003017 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003018 goto out;
3019 }
3020
J-Alves3c5b2072022-11-21 12:45:40 +00003021 /*
3022 * Either clear if requested in relinquish call, or in a retrieve
3023 * request from one of the borrowers.
3024 */
3025 receivers_relinquished_memory = true;
3026
3027 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3028 struct ffa_memory_access *receiver =
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003029 ffa_memory_region_get_receiver(memory_region, i);
3030 assert(receiver != NULL);
J-Alves3c5b2072022-11-21 12:45:40 +00003031 if (receiver->receiver_permissions.receiver ==
3032 from_locked.vm->id) {
J-Alves639ddfc2023-11-21 14:17:26 +00003033 receiver_permissions =
3034 receiver->receiver_permissions.permissions;
J-Alves3c5b2072022-11-21 12:45:40 +00003035 continue;
3036 }
3037
3038 if (share_state->retrieved_fragment_count[i] != 0U) {
3039 receivers_relinquished_memory = false;
3040 break;
3041 }
3042 }
3043
3044 clear = receivers_relinquished_memory &&
3045 (share_state->clear_after_relinquish ||
3046 (relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) !=
3047 0U);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003048
3049 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003050 * Clear is not allowed for memory that was shared, as the
3051 * original sender still has access to the memory.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003052 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003053 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003054 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003055 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003056 goto out;
3057 }
3058
J-Alves639ddfc2023-11-21 14:17:26 +00003059 if (clear && receiver_permissions == FFA_DATA_ACCESS_RO) {
3060 dlog_verbose("%s: RO memory can't use clear memory flag.\n",
3061 __func__);
3062 ret = ffa_error(FFA_DENIED);
3063 goto out;
3064 }
3065
Andrew Walbranca808b12020-05-15 17:22:28 +01003066 ret = ffa_relinquish_check_update(
J-Alves26483382023-04-20 12:01:49 +01003067 from_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003068 share_state->fragment_constituent_counts,
3069 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003070
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003071 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003072 /*
J-Alvesa9cd7e32022-07-01 13:49:33 +01003073 * Mark memory handle as not retrieved, so it can be
3074 * reclaimed (or retrieved again).
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003075 */
J-Alves8eb19162022-04-28 10:56:48 +01003076 share_state->retrieved_fragment_count[receiver_index] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003077 }
3078
3079out:
3080 share_states_unlock(&share_states);
3081 dump_share_states();
3082 return ret;
3083}
3084
3085/**
J-Alvesa9cd7e32022-07-01 13:49:33 +01003086 * Validates that the reclaim transition is allowed for the given
3087 * handle, updates the page table of the reclaiming VM, and frees the
3088 * internal state associated with the handle.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003089 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003090struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01003091 ffa_memory_handle_t handle,
3092 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003093 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003094{
3095 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003096 struct ffa_memory_share_state *share_state;
3097 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003098 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003099
3100 dump_share_states();
3101
3102 share_states = share_states_lock();
Karl Meakin52cdfe72023-06-30 14:49:10 +01003103
Karl Meakin4a2854a2023-06-30 16:26:52 +01003104 share_state = get_share_state(share_states, handle);
J-Alvesb56aac82023-11-10 09:44:43 +00003105 if (share_state == NULL) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003106 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003107 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003108 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003109 goto out;
3110 }
Karl Meakin4a2854a2023-06-30 16:26:52 +01003111 memory_region = share_state->memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003112
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003113 CHECK(memory_region != NULL);
3114
J-Alvesa9cd7e32022-07-01 13:49:33 +01003115 if (vm_id_is_current_world(to_locked.vm->id) &&
3116 to_locked.vm->id != memory_region->sender) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003117 dlog_verbose(
Olivier Deprezf92e5d42020-11-13 16:00:54 +01003118 "VM %#x attempted to reclaim memory handle %#x "
3119 "originally sent by VM %#x.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003120 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003121 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003122 goto out;
3123 }
3124
Andrew Walbranca808b12020-05-15 17:22:28 +01003125 if (!share_state->sending_complete) {
3126 dlog_verbose(
3127 "Memory with handle %#x not fully sent, can't "
3128 "reclaim.\n",
3129 handle);
3130 ret = ffa_error(FFA_INVALID_PARAMETERS);
3131 goto out;
3132 }
3133
J-Alves752236c2022-04-28 11:07:47 +01003134 for (uint32_t i = 0; i < memory_region->receiver_count; i++) {
3135 if (share_state->retrieved_fragment_count[i] != 0) {
3136 dlog_verbose(
J-Alvesa9cd7e32022-07-01 13:49:33 +01003137 "Tried to reclaim memory handle %#x "
J-Alves3c5b2072022-11-21 12:45:40 +00003138 "that has not been relinquished by all "
J-Alvesa9cd7e32022-07-01 13:49:33 +01003139 "borrowers(%x).\n",
J-Alves752236c2022-04-28 11:07:47 +01003140 handle,
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00003141 ffa_memory_region_get_receiver(memory_region, i)
3142 ->receiver_permissions.receiver);
J-Alves752236c2022-04-28 11:07:47 +01003143 ret = ffa_error(FFA_DENIED);
3144 goto out;
3145 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003146 }
3147
Andrew Walbranca808b12020-05-15 17:22:28 +01003148 ret = ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +01003149 to_locked, share_state->fragments,
Andrew Walbranca808b12020-05-15 17:22:28 +01003150 share_state->fragment_constituent_counts,
J-Alves2a0d2882020-10-29 14:49:50 +00003151 share_state->fragment_count, share_state->sender_orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +01003152 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003153
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003154 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003155 share_state_free(share_states, share_state, page_pool);
J-Alves3c5b2072022-11-21 12:45:40 +00003156 dlog_verbose("Freed share state after successful reclaim.\n");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00003157 }
3158
3159out:
3160 share_states_unlock(&share_states);
3161 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01003162}