blob: 89c372de21c42071abc11f989ac1e1140f3ae82c [file] [log] [blame]
Jose Marinho75509b42019-04-09 09:34:59 +01001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010017#include "hf/ffa_memory.h"
Andrew Walbran475c1452020-02-07 13:22:22 +000018
Andrew Walbran290b0c92020-02-03 16:37:14 +000019#include "hf/arch/tee.h"
20
Jose Marinho75509b42019-04-09 09:34:59 +010021#include "hf/api.h"
Jose Marinho09b1db82019-08-08 09:16:59 +010022#include "hf/check.h"
Jose Marinho75509b42019-04-09 09:34:59 +010023#include "hf/dlog.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010024#include "hf/ffa_internal.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
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000029/** The maximum number of recipients a memory region may be sent to. */
30#define MAX_MEM_SHARE_RECIPIENTS 1
31
32/**
33 * The maximum number of memory sharing handles which may be active at once. A
34 * DONATE handle is active from when it is sent to when it is retrieved; a SHARE
35 * or LEND handle is active from when it is sent to when it is reclaimed.
36 */
37#define MAX_MEM_SHARES 100
38
Andrew Walbranca808b12020-05-15 17:22:28 +010039/**
40 * The maximum number of fragments into which a memory sharing message may be
41 * broken.
42 */
43#define MAX_FRAGMENTS 20
44
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010045static_assert(sizeof(struct ffa_memory_region_constituent) % 16 == 0,
46 "struct ffa_memory_region_constituent must be a multiple of 16 "
Andrew Walbranc34c7b22020-02-28 11:16:59 +000047 "bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010048static_assert(sizeof(struct ffa_composite_memory_region) % 16 == 0,
49 "struct ffa_composite_memory_region must be a multiple of 16 "
Andrew Walbranc34c7b22020-02-28 11:16:59 +000050 "bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010051static_assert(sizeof(struct ffa_memory_region_attributes) == 4,
52 "struct ffa_memory_region_attributes must be 4bytes long.");
53static_assert(sizeof(struct ffa_memory_access) % 16 == 0,
54 "struct ffa_memory_access must be a multiple of 16 bytes long.");
55static_assert(sizeof(struct ffa_memory_region) % 16 == 0,
56 "struct ffa_memory_region must be a multiple of 16 bytes long.");
57static_assert(sizeof(struct ffa_mem_relinquish) % 16 == 0,
58 "struct ffa_mem_relinquish must be a multiple of 16 "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000059 "bytes long.");
Andrew Walbranc34c7b22020-02-28 11:16:59 +000060
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010061struct ffa_memory_share_state {
Andrew Walbranca808b12020-05-15 17:22:28 +010062 ffa_memory_handle_t handle;
63
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000064 /**
65 * The memory region being shared, or NULL if this share state is
66 * unallocated.
67 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010068 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000069
Andrew Walbranca808b12020-05-15 17:22:28 +010070 struct ffa_memory_region_constituent *fragments[MAX_FRAGMENTS];
71
72 /** The number of constituents in each fragment. */
73 uint32_t fragment_constituent_counts[MAX_FRAGMENTS];
74
75 /**
76 * The number of valid elements in the `fragments` and
77 * `fragment_constituent_counts` arrays.
78 */
79 uint32_t fragment_count;
80
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000081 /**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010082 * The FF-A function used for sharing the memory. Must be one of
83 * FFA_MEM_DONATE_32, FFA_MEM_LEND_32 or FFA_MEM_SHARE_32 if the
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000084 * share state is allocated, or 0.
85 */
86 uint32_t share_func;
87
88 /**
Andrew Walbranca808b12020-05-15 17:22:28 +010089 * True if all the fragments of this sharing request have been sent and
90 * Hafnium has updated the sender page table accordingly.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000091 */
Andrew Walbranca808b12020-05-15 17:22:28 +010092 bool sending_complete;
93
94 /**
95 * How many fragments of the memory region each recipient has retrieved
96 * so far. The order of this array matches the order of the endpoint
97 * memory access descriptors in the memory region descriptor. Any
98 * entries beyond the receiver_count will always be 0.
99 */
100 uint32_t retrieved_fragment_count[MAX_MEM_SHARE_RECIPIENTS];
Andrew Walbran475c1452020-02-07 13:22:22 +0000101};
102
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000103/**
104 * Encapsulates the set of share states while the `share_states_lock` is held.
105 */
106struct share_states_locked {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100107 struct ffa_memory_share_state *share_states;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000108};
109
110/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100111 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000112 * by this lock.
113 */
114static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100115static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000116
117/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100118 * Buffer for retrieving memory region information from the TEE for when a
119 * region is reclaimed by a VM. Access to this buffer must be guarded by the VM
120 * lock of the TEE VM.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000121 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100122alignas(PAGE_SIZE) static uint8_t
123 tee_retrieve_buffer[HF_MAILBOX_SIZE * MAX_FRAGMENTS];
124
125/**
126 * Initialises the next available `struct ffa_memory_share_state` and sets
127 * `share_state_ret` to a pointer to it. If `handle` is
128 * `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle, otherwise
129 * uses the provided handle which is assumed to be globally unique.
130 *
131 * Returns true on success or false if none are available.
132 */
133static bool allocate_share_state(
134 struct share_states_locked share_states, uint32_t share_func,
135 struct ffa_memory_region *memory_region, uint32_t fragment_length,
136 ffa_memory_handle_t handle,
137 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000138{
Andrew Walbrana65a1322020-04-06 19:32:32 +0100139 uint64_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000140
Andrew Walbranca808b12020-05-15 17:22:28 +0100141 CHECK(share_states.share_states != NULL);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000142 CHECK(memory_region != NULL);
143
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000144 for (i = 0; i < MAX_MEM_SHARES; ++i) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100145 if (share_states.share_states[i].share_func == 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000146 uint32_t j;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100147 struct ffa_memory_share_state *allocated_state =
Andrew Walbranca808b12020-05-15 17:22:28 +0100148 &share_states.share_states[i];
149 struct ffa_composite_memory_region *composite =
150 ffa_memory_region_get_composite(memory_region,
151 0);
152
153 if (handle == FFA_MEMORY_HANDLE_INVALID) {
154 allocated_state->handle =
155 i |
156 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR;
157 } else {
158 allocated_state->handle = handle;
159 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000160 allocated_state->share_func = share_func;
161 allocated_state->memory_region = memory_region;
Andrew Walbranca808b12020-05-15 17:22:28 +0100162 allocated_state->fragment_count = 1;
163 allocated_state->fragments[0] = composite->constituents;
164 allocated_state->fragment_constituent_counts[0] =
165 (fragment_length -
166 ffa_composite_constituent_offset(memory_region,
167 0)) /
168 sizeof(struct ffa_memory_region_constituent);
169 allocated_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000170 for (j = 0; j < MAX_MEM_SHARE_RECIPIENTS; ++j) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100171 allocated_state->retrieved_fragment_count[j] =
172 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000173 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100174 if (share_state_ret != NULL) {
175 *share_state_ret = allocated_state;
176 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000177 return true;
178 }
179 }
180
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000181 return false;
182}
183
184/** Locks the share states lock. */
185struct share_states_locked share_states_lock(void)
186{
187 sl_lock(&share_states_lock_instance);
188
189 return (struct share_states_locked){.share_states = share_states};
190}
191
192/** Unlocks the share states lock. */
193static void share_states_unlock(struct share_states_locked *share_states)
194{
195 CHECK(share_states->share_states != NULL);
196 share_states->share_states = NULL;
197 sl_unlock(&share_states_lock_instance);
198}
199
200/**
Andrew Walbranca808b12020-05-15 17:22:28 +0100201 * If the given handle is a valid handle for an allocated share state then
202 * initialises `share_state_ret` to point to the share state and returns true.
203 * Otherwise returns false.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000204 */
205static bool get_share_state(struct share_states_locked share_states,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100206 ffa_memory_handle_t handle,
207 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000208{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100209 struct ffa_memory_share_state *share_state;
Andrew Walbranca808b12020-05-15 17:22:28 +0100210 uint32_t index;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000211
Andrew Walbranca808b12020-05-15 17:22:28 +0100212 CHECK(share_states.share_states != NULL);
213 CHECK(share_state_ret != NULL);
214
215 /*
216 * First look for a share_state allocated by us, in which case the
217 * handle is based on the index.
218 */
219 if ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
220 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR) {
221 index = handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
222 if (index < MAX_MEM_SHARES) {
223 share_state = &share_states.share_states[index];
224 if (share_state->share_func != 0) {
225 *share_state_ret = share_state;
226 return true;
227 }
228 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000229 }
230
Andrew Walbranca808b12020-05-15 17:22:28 +0100231 /* Fall back to a linear scan. */
232 for (index = 0; index < MAX_MEM_SHARES; ++index) {
233 share_state = &share_states.share_states[index];
234 if (share_state->handle == handle &&
235 share_state->share_func != 0) {
236 *share_state_ret = share_state;
237 return true;
238 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000239 }
240
Andrew Walbranca808b12020-05-15 17:22:28 +0100241 return false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000242}
243
244/** Marks a share state as unallocated. */
245static void share_state_free(struct share_states_locked share_states,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100246 struct ffa_memory_share_state *share_state,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000247 struct mpool *page_pool)
248{
Andrew Walbranca808b12020-05-15 17:22:28 +0100249 uint32_t i;
250
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000251 CHECK(share_states.share_states != NULL);
252 share_state->share_func = 0;
Andrew Walbranca808b12020-05-15 17:22:28 +0100253 share_state->sending_complete = false;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000254 mpool_free(page_pool, share_state->memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100255 /*
256 * First fragment is part of the same page as the `memory_region`, so it
257 * doesn't need to be freed separately.
258 */
259 share_state->fragments[0] = NULL;
260 share_state->fragment_constituent_counts[0] = 0;
261 for (i = 1; i < share_state->fragment_count; ++i) {
262 mpool_free(page_pool, share_state->fragments[i]);
263 share_state->fragments[i] = NULL;
264 share_state->fragment_constituent_counts[i] = 0;
265 }
266 share_state->fragment_count = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000267 share_state->memory_region = NULL;
268}
269
Andrew Walbranca808b12020-05-15 17:22:28 +0100270/** Checks whether the given share state has been fully sent. */
271static bool share_state_sending_complete(
272 struct share_states_locked share_states,
273 struct ffa_memory_share_state *share_state)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000274{
Andrew Walbranca808b12020-05-15 17:22:28 +0100275 struct ffa_composite_memory_region *composite;
276 uint32_t expected_constituent_count;
277 uint32_t fragment_constituent_count_total = 0;
278 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000279
Andrew Walbranca808b12020-05-15 17:22:28 +0100280 /* Lock must be held. */
281 CHECK(share_states.share_states != NULL);
282
283 /*
284 * Share state must already be valid, or it's not possible to get hold
285 * of it.
286 */
287 CHECK(share_state->memory_region != NULL &&
288 share_state->share_func != 0);
289
290 composite =
291 ffa_memory_region_get_composite(share_state->memory_region, 0);
292 expected_constituent_count = composite->constituent_count;
293 for (i = 0; i < share_state->fragment_count; ++i) {
294 fragment_constituent_count_total +=
295 share_state->fragment_constituent_counts[i];
296 }
297 dlog_verbose(
298 "Checking completion: constituent count %d/%d from %d "
299 "fragments.\n",
300 fragment_constituent_count_total, expected_constituent_count,
301 share_state->fragment_count);
302
303 return fragment_constituent_count_total == expected_constituent_count;
304}
305
306/**
307 * Calculates the offset of the next fragment expected for the given share
308 * state.
309 */
310static uint32_t share_state_next_fragment_offset(
311 struct share_states_locked share_states,
312 struct ffa_memory_share_state *share_state)
313{
314 uint32_t next_fragment_offset;
315 uint32_t i;
316
317 /* Lock must be held. */
318 CHECK(share_states.share_states != NULL);
319
320 next_fragment_offset =
321 ffa_composite_constituent_offset(share_state->memory_region, 0);
322 for (i = 0; i < share_state->fragment_count; ++i) {
323 next_fragment_offset +=
324 share_state->fragment_constituent_counts[i] *
325 sizeof(struct ffa_memory_region_constituent);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000326 }
327
Andrew Walbranca808b12020-05-15 17:22:28 +0100328 return next_fragment_offset;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000329}
330
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100331static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000332{
333 uint32_t i;
334
335 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
336 return;
337 }
338
Andrew Walbrana65a1322020-04-06 19:32:32 +0100339 dlog("from VM %d, attributes %#x, flags %#x, handle %#x, tag %d, to %d "
340 "recipients [",
341 memory_region->sender, memory_region->attributes,
342 memory_region->flags, memory_region->handle, memory_region->tag,
343 memory_region->receiver_count);
344 for (i = 0; i < memory_region->receiver_count; ++i) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000345 if (i != 0) {
346 dlog(", ");
347 }
Andrew Walbrana65a1322020-04-06 19:32:32 +0100348 dlog("VM %d: %#x (offset %d)",
349 memory_region->receivers[i].receiver_permissions.receiver,
350 memory_region->receivers[i]
351 .receiver_permissions.permissions,
352 memory_region->receivers[i]
353 .composite_memory_region_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000354 }
355 dlog("]");
356}
357
358static void dump_share_states(void)
359{
360 uint32_t i;
361
362 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
363 return;
364 }
365
366 dlog("Current share states:\n");
367 sl_lock(&share_states_lock_instance);
368 for (i = 0; i < MAX_MEM_SHARES; ++i) {
369 if (share_states[i].share_func != 0) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100370 dlog("%#x: ", share_states[i].handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000371 switch (share_states[i].share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100372 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000373 dlog("SHARE");
374 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100375 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000376 dlog("LEND");
377 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100378 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000379 dlog("DONATE");
380 break;
381 default:
382 dlog("invalid share_func %#x",
383 share_states[i].share_func);
384 }
385 dlog(" (");
386 dump_memory_region(share_states[i].memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +0100387 if (share_states[i].sending_complete) {
388 dlog("): fully sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000389 } else {
Andrew Walbranca808b12020-05-15 17:22:28 +0100390 dlog("): partially sent");
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000391 }
Andrew Walbranca808b12020-05-15 17:22:28 +0100392 dlog(" with %d fragments, %d retrieved\n",
393 share_states[i].fragment_count,
394 share_states[i].retrieved_fragment_count[0]);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000395 break;
396 }
397 }
398 sl_unlock(&share_states_lock_instance);
399}
400
Andrew Walbran475c1452020-02-07 13:22:22 +0000401/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100402static inline uint32_t ffa_memory_permissions_to_mode(
403 ffa_memory_access_permissions_t permissions)
Andrew Walbran475c1452020-02-07 13:22:22 +0000404{
405 uint32_t mode = 0;
406
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100407 switch (ffa_get_data_access_attr(permissions)) {
408 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000409 mode = MM_MODE_R;
410 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100411 case FFA_DATA_ACCESS_RW:
412 case FFA_DATA_ACCESS_NOT_SPECIFIED:
Andrew Walbran475c1452020-02-07 13:22:22 +0000413 mode = MM_MODE_R | MM_MODE_W;
414 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100415 case FFA_DATA_ACCESS_RESERVED:
416 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100417 }
418
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100419 switch (ffa_get_instruction_access_attr(permissions)) {
420 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000421 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100422 case FFA_INSTRUCTION_ACCESS_X:
423 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100424 mode |= MM_MODE_X;
425 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100426 case FFA_INSTRUCTION_ACCESS_RESERVED:
427 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000428 }
429
430 return mode;
431}
432
Jose Marinho75509b42019-04-09 09:34:59 +0100433/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000434 * Get the current mode in the stage-2 page table of the given vm of all the
435 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100436 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100437 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100438static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000439 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100440 struct ffa_memory_region_constituent **fragments,
441 const uint32_t *fragment_constituent_counts, uint32_t fragment_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100442{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100443 uint32_t i;
Andrew Walbranca808b12020-05-15 17:22:28 +0100444 uint32_t j;
Jose Marinho75509b42019-04-09 09:34:59 +0100445
Andrew Walbranca808b12020-05-15 17:22:28 +0100446 if (fragment_count == 0 || fragment_constituent_counts[0] == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100447 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000448 * Fail if there are no constituents. Otherwise we would get an
449 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100450 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100451 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100452 }
453
Andrew Walbranca808b12020-05-15 17:22:28 +0100454 for (i = 0; i < fragment_count; ++i) {
455 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
456 ipaddr_t begin = ipa_init(fragments[i][j].address);
457 size_t size = fragments[i][j].page_count * PAGE_SIZE;
458 ipaddr_t end = ipa_add(begin, size);
459 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100460
Andrew Walbranca808b12020-05-15 17:22:28 +0100461 /* Fail if addresses are not page-aligned. */
462 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
463 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
464 return ffa_error(FFA_INVALID_PARAMETERS);
465 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100466
Andrew Walbranca808b12020-05-15 17:22:28 +0100467 /*
468 * Ensure that this constituent memory range is all
469 * mapped with the same mode.
470 */
471 if (!mm_vm_get_mode(&vm.vm->ptable, begin, end,
472 &current_mode)) {
473 return ffa_error(FFA_DENIED);
474 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100475
Andrew Walbranca808b12020-05-15 17:22:28 +0100476 /*
477 * Ensure that all constituents are mapped with the same
478 * mode.
479 */
480 if (i == 0) {
481 *orig_mode = current_mode;
482 } else if (current_mode != *orig_mode) {
483 dlog_verbose(
484 "Expected mode %#x but was %#x for %d "
485 "pages at %#x.\n",
486 *orig_mode, current_mode,
487 fragments[i][j].page_count,
488 ipa_addr(begin));
489 return ffa_error(FFA_DENIED);
490 }
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100491 }
Jose Marinho75509b42019-04-09 09:34:59 +0100492 }
493
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100494 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000495}
496
497/**
498 * Verify that all pages have the same mode, that the starting mode
499 * constitutes a valid state and obtain the next mode to apply
500 * to the sending VM.
501 *
502 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100503 * 1) FFA_DENIED if a state transition was not found;
504 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100505 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100506 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100507 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100508 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
509 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000510 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100511static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100512 struct vm_locked from, uint32_t share_func,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100513 ffa_memory_access_permissions_t permissions, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100514 struct ffa_memory_region_constituent **fragments,
515 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
516 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000517{
518 const uint32_t state_mask =
519 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbrana65a1322020-04-06 19:32:32 +0100520 const uint32_t required_from_mode =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100521 ffa_memory_permissions_to_mode(permissions);
522 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000523
Andrew Walbranca808b12020-05-15 17:22:28 +0100524 ret = constituents_get_mode(from, orig_from_mode, fragments,
525 fragment_constituent_counts,
526 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100527 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100528 dlog_verbose("Inconsistent modes.\n", fragment_count);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100529 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100530 }
531
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000532 /* Ensure the address range is normal memory and not a device. */
533 if (*orig_from_mode & MM_MODE_D) {
534 dlog_verbose("Can't share device memory (mode is %#x).\n",
535 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100536 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000537 }
538
539 /*
540 * Ensure the sender is the owner and has exclusive access to the
541 * memory.
542 */
543 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100544 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100545 }
546
547 if ((*orig_from_mode & required_from_mode) != required_from_mode) {
548 dlog_verbose(
549 "Sender tried to send memory with permissions which "
550 "required mode %#x but only had %#x itself.\n",
551 required_from_mode, *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100552 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000553 }
554
555 /* Find the appropriate new mode. */
556 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000557 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100558 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000559 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100560 break;
561
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100562 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000563 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100564 break;
565
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100566 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000567 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100568 break;
569
Jose Marinho75509b42019-04-09 09:34:59 +0100570 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100571 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100572 }
573
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100574 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000575}
576
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100577static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000578 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranca808b12020-05-15 17:22:28 +0100579 struct ffa_memory_region_constituent **fragments,
580 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
581 uint32_t *from_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000582{
583 const uint32_t state_mask =
584 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
585 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100586 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000587
Andrew Walbranca808b12020-05-15 17:22:28 +0100588 ret = constituents_get_mode(from, orig_from_mode, fragments,
589 fragment_constituent_counts,
590 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100591 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100592 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000593 }
594
595 /* Ensure the address range is normal memory and not a device. */
596 if (*orig_from_mode & MM_MODE_D) {
597 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
598 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100599 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000600 }
601
602 /*
603 * Ensure the relinquishing VM is not the owner but has access to the
604 * memory.
605 */
606 orig_from_state = *orig_from_mode & state_mask;
607 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
608 dlog_verbose(
609 "Tried to relinquish memory in state %#x (masked %#x "
Andrew Walbranca808b12020-05-15 17:22:28 +0100610 "but should be %#x).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000611 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100612 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000613 }
614
615 /* Find the appropriate new mode. */
616 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
617
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100618 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000619}
620
621/**
622 * Verify that all pages have the same mode, that the starting mode
623 * constitutes a valid state and obtain the next mode to apply
624 * to the retrieving VM.
625 *
626 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100627 * 1) FFA_DENIED if a state transition was not found;
628 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100629 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100630 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100631 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100632 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
633 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000634 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100635static struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000636 struct vm_locked to, uint32_t share_func,
Andrew Walbranca808b12020-05-15 17:22:28 +0100637 struct ffa_memory_region_constituent **fragments,
638 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
639 uint32_t memory_to_attributes, uint32_t *to_mode)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000640{
641 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100642 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000643
Andrew Walbranca808b12020-05-15 17:22:28 +0100644 ret = constituents_get_mode(to, &orig_to_mode, fragments,
645 fragment_constituent_counts,
646 fragment_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100647 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100648 dlog_verbose("Inconsistent modes.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100649 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000650 }
651
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100652 if (share_func == FFA_MEM_RECLAIM_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000653 const uint32_t state_mask =
654 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
655 uint32_t orig_to_state = orig_to_mode & state_mask;
656
657 if (orig_to_state != MM_MODE_INVALID &&
658 orig_to_state != MM_MODE_SHARED) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100659 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000660 }
661 } else {
662 /*
663 * Ensure the retriever has the expected state. We don't care
664 * about the MM_MODE_SHARED bit; either with or without it set
665 * are both valid representations of the !O-NA state.
666 */
667 if ((orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
668 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100669 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000670 }
671 }
672
673 /* Find the appropriate new mode. */
674 *to_mode = memory_to_attributes;
675 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100676 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000677 *to_mode |= 0;
678 break;
679
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100680 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000681 *to_mode |= MM_MODE_UNOWNED;
682 break;
683
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100684 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000685 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
686 break;
687
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100688 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000689 *to_mode |= 0;
690 break;
691
692 default:
Andrew Walbranca808b12020-05-15 17:22:28 +0100693 dlog_error("Invalid share_func %#x.\n", share_func);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100694 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000695 }
696
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100697 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100698}
Jose Marinho09b1db82019-08-08 09:16:59 +0100699
700/**
701 * Updates a VM's page table such that the given set of physical address ranges
702 * are mapped in the address space at the corresponding address ranges, in the
703 * mode provided.
704 *
705 * If commit is false, the page tables will be allocated from the mpool but no
706 * mappings will actually be updated. This function must always be called first
707 * with commit false to check that it will succeed before calling with commit
708 * true, to avoid leaving the page table in a half-updated state. To make a
709 * series of changes atomically you can call them all with commit false before
710 * calling them all with commit true.
711 *
712 * mm_vm_defrag should always be called after a series of page table updates,
713 * whether they succeed or fail.
714 *
715 * Returns true on success, or false if the update failed and no changes were
716 * made to memory mappings.
717 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100718static bool ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000719 struct vm_locked vm_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100720 struct ffa_memory_region_constituent **fragments,
721 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
722 int mode, struct mpool *ppool, bool commit)
Jose Marinho09b1db82019-08-08 09:16:59 +0100723{
Andrew Walbranca808b12020-05-15 17:22:28 +0100724 uint32_t i;
725 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100726
Andrew Walbranca808b12020-05-15 17:22:28 +0100727 /* Iterate over the memory region constituents within each fragment. */
728 for (i = 0; i < fragment_count; ++i) {
729 for (j = 0; j < fragment_constituent_counts[i]; ++j) {
730 size_t size = fragments[i][j].page_count * PAGE_SIZE;
731 paddr_t pa_begin =
732 pa_from_ipa(ipa_init(fragments[i][j].address));
733 paddr_t pa_end = pa_add(pa_begin, size);
734
735 if (commit) {
736 vm_identity_commit(vm_locked, pa_begin, pa_end,
737 mode, ppool, NULL);
738 } else if (!vm_identity_prepare(vm_locked, pa_begin,
739 pa_end, mode, ppool)) {
740 return false;
741 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100742 }
743 }
744
745 return true;
746}
747
748/**
749 * Clears a region of physical memory by overwriting it with zeros. The data is
750 * flushed from the cache so the memory has been cleared across the system.
751 */
752static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool)
753{
754 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000755 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100756 * global mapping of the whole range. Such an approach will limit
757 * the changes to stage-1 tables and will allow only local
758 * invalidation.
759 */
760 bool ret;
761 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
762 void *ptr =
763 mm_identity_map(stage1_locked, begin, end, MM_MODE_W, ppool);
764 size_t size = pa_difference(begin, end);
765
766 if (!ptr) {
767 /* TODO: partial defrag of failed range. */
768 /* Recover any memory consumed in failed mapping. */
769 mm_defrag(stage1_locked, ppool);
770 goto fail;
771 }
772
773 memset_s(ptr, size, 0, size);
774 arch_mm_flush_dcache(ptr, size);
775 mm_unmap(stage1_locked, begin, end, ppool);
776
777 ret = true;
778 goto out;
779
780fail:
781 ret = false;
782
783out:
784 mm_unlock_stage1(&stage1_locked);
785
786 return ret;
787}
788
789/**
790 * Clears a region of physical memory by overwriting it with zeros. The data is
791 * flushed from the cache so the memory has been cleared across the system.
792 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100793static bool ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +0100794 struct ffa_memory_region_constituent **fragments,
795 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
796 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100797{
798 struct mpool local_page_pool;
Andrew Walbranca808b12020-05-15 17:22:28 +0100799 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100800 struct mm_stage1_locked stage1_locked;
801 bool ret = false;
802
803 /*
804 * Create a local pool so any freed memory can't be used by another
805 * thread. This is to ensure each constituent that is mapped can be
806 * unmapped again afterwards.
807 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000808 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100809
Andrew Walbranca808b12020-05-15 17:22:28 +0100810 /* Iterate over the memory region constituents within each fragment. */
811 for (i = 0; i < fragment_count; ++i) {
812 uint32_t j;
Jose Marinho09b1db82019-08-08 09:16:59 +0100813
Andrew Walbranca808b12020-05-15 17:22:28 +0100814 for (j = 0; j < fragment_constituent_counts[j]; ++j) {
815 size_t size = fragments[i][j].page_count * PAGE_SIZE;
816 paddr_t begin =
817 pa_from_ipa(ipa_init(fragments[i][j].address));
818 paddr_t end = pa_add(begin, size);
819
820 if (!clear_memory(begin, end, &local_page_pool)) {
821 /*
822 * api_clear_memory will defrag on failure, so
823 * no need to do it here.
824 */
825 goto out;
826 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100827 }
828 }
829
830 /*
831 * Need to defrag after clearing, as it may have added extra mappings to
832 * the stage 1 page table.
833 */
834 stage1_locked = mm_lock_stage1();
835 mm_defrag(stage1_locked, &local_page_pool);
836 mm_unlock_stage1(&stage1_locked);
837
838 ret = true;
839
840out:
841 mpool_fini(&local_page_pool);
842 return ret;
843}
844
845/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000846 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +0100847 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000848 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +0100849 *
850 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000851 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100852 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +0100853 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100854 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
855 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100856 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +0100857 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100858 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +0100859 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100860static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000861 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100862 struct ffa_memory_region_constituent **fragments,
863 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
864 uint32_t share_func, ffa_memory_access_permissions_t permissions,
Andrew Walbran37c574e2020-06-03 11:45:46 +0100865 struct mpool *page_pool, bool clear, uint32_t *orig_from_mode_ret)
Jose Marinho09b1db82019-08-08 09:16:59 +0100866{
Jose Marinho09b1db82019-08-08 09:16:59 +0100867 struct vm *from = from_locked.vm;
Andrew Walbranca808b12020-05-15 17:22:28 +0100868 uint32_t i;
Jose Marinho09b1db82019-08-08 09:16:59 +0100869 uint32_t orig_from_mode;
870 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +0100871 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100872 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100873
874 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +0100875 * Make sure constituents are properly aligned to a 64-bit boundary. If
876 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100877 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100878 for (i = 0; i < fragment_count; ++i) {
879 if (!is_aligned(fragments[i], 8)) {
880 dlog_verbose("Constituents not aligned.\n");
881 return ffa_error(FFA_INVALID_PARAMETERS);
882 }
Jose Marinho09b1db82019-08-08 09:16:59 +0100883 }
884
885 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000886 * Check if the state transition is lawful for the sender, ensure that
887 * all constituents of a memory region being shared are at the same
888 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +0100889 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100890 ret = ffa_send_check_transition(from_locked, share_func, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +0100891 &orig_from_mode, fragments,
892 fragment_constituent_counts,
893 fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100894 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +0100895 dlog_verbose("Invalid transition for send.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100896 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100897 }
898
Andrew Walbran37c574e2020-06-03 11:45:46 +0100899 if (orig_from_mode_ret != NULL) {
900 *orig_from_mode_ret = orig_from_mode;
901 }
902
Jose Marinho09b1db82019-08-08 09:16:59 +0100903 /*
904 * Create a local pool so any freed memory can't be used by another
905 * thread. This is to ensure the original mapping can be restored if the
906 * clear fails.
907 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000908 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100909
910 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000911 * First reserve all required memory for the new page table entries
912 * without committing, to make sure the entire operation will succeed
913 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +0100914 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100915 if (!ffa_region_group_identity_map(
916 from_locked, fragments, fragment_constituent_counts,
917 fragment_count, from_mode, page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100918 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100919 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100920 goto out;
921 }
922
923 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000924 * Update the mapping for the sender. This won't allocate because the
925 * transaction was already prepared above, but may free pages in the
926 * case that a whole block is being unmapped that was previously
927 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +0100928 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100929 CHECK(ffa_region_group_identity_map(
930 from_locked, fragments, fragment_constituent_counts,
931 fragment_count, from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100932
933 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100934 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +0100935 fragments, fragment_constituent_counts,
936 fragment_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100937 /*
938 * On failure, roll back by returning memory to the sender. This
939 * may allocate pages which were previously freed into
940 * `local_page_pool` by the call above, but will never allocate
941 * more pages than that so can never fail.
942 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100943 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +0100944 from_locked, fragments, fragment_constituent_counts,
945 fragment_count, orig_from_mode, &local_page_pool,
946 true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100947
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100948 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100949 goto out;
950 }
951
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100952 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000953
954out:
955 mpool_fini(&local_page_pool);
956
957 /*
958 * Tidy up the page table by reclaiming failed mappings (if there was an
959 * error) or merging entries into blocks where possible (on success).
960 */
961 mm_vm_defrag(&from->ptable, page_pool);
962
963 return ret;
964}
965
966/**
967 * Validates and maps memory shared from one VM to another.
968 *
969 * This function requires the calling context to hold the <to> lock.
970 *
971 * Returns:
972 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100973 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000974 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100975 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000976 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100977 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000978 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100979static struct ffa_value ffa_retrieve_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000980 struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +0100981 struct ffa_memory_region_constituent **fragments,
982 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
983 uint32_t memory_to_attributes, uint32_t share_func, bool clear,
984 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000985{
986 struct vm *to = to_locked.vm;
Andrew Walbranca808b12020-05-15 17:22:28 +0100987 uint32_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000988 uint32_t to_mode;
989 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100990 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000991
992 /*
Andrew Walbranca808b12020-05-15 17:22:28 +0100993 * Make sure constituents are properly aligned to a 64-bit boundary. If
994 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000995 */
Andrew Walbranca808b12020-05-15 17:22:28 +0100996 for (i = 0; i < fragment_count; ++i) {
997 if (!is_aligned(fragments[i], 8)) {
998 return ffa_error(FFA_INVALID_PARAMETERS);
999 }
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001000 }
1001
1002 /*
1003 * Check if the state transition is lawful for the recipient, and ensure
1004 * that all constituents of the memory region being retrieved are at the
1005 * same state.
1006 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001007 ret = ffa_retrieve_check_transition(
1008 to_locked, share_func, fragments, fragment_constituent_counts,
1009 fragment_count, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001010 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001011 dlog_verbose("Invalid transition for retrieve.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001012 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001013 }
1014
1015 /*
1016 * Create a local pool so any freed memory can't be used by another
1017 * thread. This is to ensure the original mapping can be restored if the
1018 * clear fails.
1019 */
1020 mpool_init_with_fallback(&local_page_pool, page_pool);
1021
1022 /*
1023 * First reserve all required memory for the new page table entries in
1024 * the recipient page tables without committing, to make sure the entire
1025 * operation will succeed without exhausting the page pool.
1026 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001027 if (!ffa_region_group_identity_map(
1028 to_locked, fragments, fragment_constituent_counts,
1029 fragment_count, to_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001030 /* TODO: partial defrag of failed range. */
1031 dlog_verbose(
1032 "Insufficient memory to update recipient page "
1033 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001034 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001035 goto out;
1036 }
1037
1038 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001039 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +01001040 fragments, fragment_constituent_counts,
1041 fragment_count, page_pool)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001042 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001043 goto out;
1044 }
1045
Jose Marinho09b1db82019-08-08 09:16:59 +01001046 /*
1047 * Complete the transfer by mapping the memory into the recipient. This
1048 * won't allocate because the transaction was already prepared above, so
1049 * it doesn't need to use the `local_page_pool`.
1050 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001051 CHECK(ffa_region_group_identity_map(
1052 to_locked, fragments, fragment_constituent_counts,
1053 fragment_count, to_mode, page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +01001054
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001055 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +01001056
1057out:
1058 mpool_fini(&local_page_pool);
1059
1060 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001061 * Tidy up the page table by reclaiming failed mappings (if there was an
1062 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +01001063 */
Andrew Walbran475c1452020-02-07 13:22:22 +00001064 mm_vm_defrag(&to->ptable, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001065
1066 return ret;
1067}
1068
Andrew Walbran290b0c92020-02-03 16:37:14 +00001069/**
1070 * Reclaims the given memory from the TEE. To do this space is first reserved in
1071 * the <to> VM's page table, then the reclaim request is sent on to the TEE,
1072 * then (if that is successful) the memory is mapped back into the <to> VM's
1073 * page table.
1074 *
1075 * This function requires the calling context to hold the <to> lock.
1076 *
1077 * Returns:
1078 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001079 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran290b0c92020-02-03 16:37:14 +00001080 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001081 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran290b0c92020-02-03 16:37:14 +00001082 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001083 * Success is indicated by FFA_SUCCESS.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001084 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01001085static struct ffa_value ffa_tee_reclaim_check_update(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001086 struct vm_locked to_locked, ffa_memory_handle_t handle,
1087 struct ffa_memory_region_constituent *constituents,
Andrew Walbran290b0c92020-02-03 16:37:14 +00001088 uint32_t constituent_count, uint32_t memory_to_attributes, bool clear,
1089 struct mpool *page_pool)
1090{
1091 struct vm *to = to_locked.vm;
1092 uint32_t to_mode;
1093 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001094 struct ffa_value ret;
1095 ffa_memory_region_flags_t tee_flags;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001096
1097 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01001098 * Make sure constituents are properly aligned to a 64-bit boundary. If
1099 * not we would get alignment faults trying to read (64-bit) values.
Andrew Walbran290b0c92020-02-03 16:37:14 +00001100 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001101 if (!is_aligned(constituents, 8)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001102 dlog_verbose("Constituents not aligned.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001103 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001104 }
1105
1106 /*
1107 * Check if the state transition is lawful for the recipient, and ensure
1108 * that all constituents of the memory region being retrieved are at the
1109 * same state.
1110 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001111 ret = ffa_retrieve_check_transition(to_locked, FFA_MEM_RECLAIM_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01001112 &constituents, &constituent_count,
1113 1, memory_to_attributes, &to_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001114 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001115 dlog_verbose("Invalid transition.\n");
1116 return ret;
1117 }
1118
1119 /*
1120 * Create a local pool so any freed memory can't be used by another
1121 * thread. This is to ensure the original mapping can be restored if the
1122 * clear fails.
1123 */
1124 mpool_init_with_fallback(&local_page_pool, page_pool);
1125
1126 /*
1127 * First reserve all required memory for the new page table entries in
1128 * the recipient page tables without committing, to make sure the entire
1129 * operation will succeed without exhausting the page pool.
1130 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001131 if (!ffa_region_group_identity_map(to_locked, &constituents,
1132 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001133 page_pool, false)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001134 /* TODO: partial defrag of failed range. */
1135 dlog_verbose(
1136 "Insufficient memory to update recipient page "
1137 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001138 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001139 goto out;
1140 }
1141
1142 /*
1143 * Forward the request to the TEE and see what happens.
1144 */
1145 tee_flags = 0;
1146 if (clear) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001147 tee_flags |= FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001148 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001149 ret = arch_tee_call((struct ffa_value){.func = FFA_MEM_RECLAIM_32,
1150 .arg1 = (uint32_t)handle,
1151 .arg2 = (uint32_t)(handle >> 32),
1152 .arg3 = tee_flags});
Andrew Walbran290b0c92020-02-03 16:37:14 +00001153
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001154 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +00001155 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01001156 "Got %#x (%d) from TEE in response to FFA_MEM_RECLAIM, "
1157 "expected FFA_SUCCESS.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +00001158 ret.func, ret.arg2);
1159 goto out;
1160 }
1161
1162 /*
1163 * The TEE was happy with it, so complete the reclaim by mapping the
1164 * memory into the recipient. This won't allocate because the
1165 * transaction was already prepared above, so it doesn't need to use the
1166 * `local_page_pool`.
1167 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001168 CHECK(ffa_region_group_identity_map(to_locked, &constituents,
1169 &constituent_count, 1, to_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001170 page_pool, true));
Andrew Walbran290b0c92020-02-03 16:37:14 +00001171
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001172 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran290b0c92020-02-03 16:37:14 +00001173
1174out:
1175 mpool_fini(&local_page_pool);
1176
1177 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001178 * Tidy up the page table by reclaiming failed mappings (if there was an
1179 * error) or merging entries into blocks where possible (on success).
Andrew Walbran290b0c92020-02-03 16:37:14 +00001180 */
1181 mm_vm_defrag(&to->ptable, page_pool);
1182
1183 return ret;
1184}
1185
Andrew Walbran996d1d12020-05-27 14:08:43 +01001186static struct ffa_value ffa_relinquish_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001187 struct vm_locked from_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01001188 struct ffa_memory_region_constituent **fragments,
1189 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
1190 struct mpool *page_pool, bool clear)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001191{
1192 uint32_t orig_from_mode;
1193 uint32_t from_mode;
1194 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001195 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001196
Andrew Walbranca808b12020-05-15 17:22:28 +01001197 ret = ffa_relinquish_check_transition(
1198 from_locked, &orig_from_mode, fragments,
1199 fragment_constituent_counts, fragment_count, &from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001200 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbranca808b12020-05-15 17:22:28 +01001201 dlog_verbose("Invalid transition for relinquish.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001202 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001203 }
1204
1205 /*
1206 * Create a local pool so any freed memory can't be used by another
1207 * thread. This is to ensure the original mapping can be restored if the
1208 * clear fails.
1209 */
1210 mpool_init_with_fallback(&local_page_pool, page_pool);
1211
1212 /*
1213 * First reserve all required memory for the new page table entries
1214 * without committing, to make sure the entire operation will succeed
1215 * without exhausting the page pool.
1216 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001217 if (!ffa_region_group_identity_map(
1218 from_locked, fragments, fragment_constituent_counts,
1219 fragment_count, from_mode, page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001220 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001221 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001222 goto out;
1223 }
1224
1225 /*
1226 * Update the mapping for the sender. This won't allocate because the
1227 * transaction was already prepared above, but may free pages in the
1228 * case that a whole block is being unmapped that was previously
1229 * partially mapped.
1230 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001231 CHECK(ffa_region_group_identity_map(
1232 from_locked, fragments, fragment_constituent_counts,
1233 fragment_count, from_mode, &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001234
1235 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001236 if (clear && !ffa_clear_memory_constituents(
Andrew Walbranca808b12020-05-15 17:22:28 +01001237 fragments, fragment_constituent_counts,
1238 fragment_count, page_pool)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001239 /*
1240 * On failure, roll back by returning memory to the sender. This
1241 * may allocate pages which were previously freed into
1242 * `local_page_pool` by the call above, but will never allocate
1243 * more pages than that so can never fail.
1244 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001245 CHECK(ffa_region_group_identity_map(
Andrew Walbranca808b12020-05-15 17:22:28 +01001246 from_locked, fragments, fragment_constituent_counts,
1247 fragment_count, orig_from_mode, &local_page_pool,
1248 true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001249
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001250 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001251 goto out;
1252 }
1253
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001254 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001255
1256out:
1257 mpool_fini(&local_page_pool);
1258
1259 /*
1260 * Tidy up the page table by reclaiming failed mappings (if there was an
1261 * error) or merging entries into blocks where possible (on success).
1262 */
1263 mm_vm_defrag(&from_locked.vm->ptable, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001264
1265 return ret;
1266}
1267
1268/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001269 * Complete a memory sending operation by checking that it is valid, updating
1270 * the sender page table, and then either marking the share state as having
1271 * completed sending (on success) or freeing it (on failure).
1272 *
1273 * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR.
1274 */
1275static struct ffa_value ffa_memory_send_complete(
1276 struct vm_locked from_locked, struct share_states_locked share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001277 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
1278 uint32_t *orig_from_mode_ret)
Andrew Walbranca808b12020-05-15 17:22:28 +01001279{
1280 struct ffa_memory_region *memory_region = share_state->memory_region;
1281 struct ffa_value ret;
1282
1283 /* Lock must be held. */
1284 CHECK(share_states.share_states != NULL);
1285
1286 /* Check that state is valid in sender page table and update. */
1287 ret = ffa_send_check_update(
1288 from_locked, share_state->fragments,
1289 share_state->fragment_constituent_counts,
1290 share_state->fragment_count, share_state->share_func,
1291 memory_region->receivers[0].receiver_permissions.permissions,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001292 page_pool, memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1293 orig_from_mode_ret);
Andrew Walbranca808b12020-05-15 17:22:28 +01001294 if (ret.func != FFA_SUCCESS_32) {
1295 /*
1296 * Free share state, it failed to send so it can't be retrieved.
1297 */
1298 dlog_verbose("Complete failed, freeing share state.\n");
1299 share_state_free(share_states, share_state, page_pool);
1300 return ret;
1301 }
1302
1303 share_state->sending_complete = true;
1304 dlog_verbose("Marked sending complete.\n");
1305
1306 return ffa_mem_success(share_state->handle);
1307}
1308
1309/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001310 * Check that the given `memory_region` represents a valid memory send request
1311 * of the given `share_func` type, return the clear flag and permissions via the
1312 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001313 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001314 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001315 * not.
1316 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001317static struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001318 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1319 uint32_t memory_share_length, uint32_t fragment_length,
1320 uint32_t share_func, ffa_memory_access_permissions_t *permissions)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001321{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001322 struct ffa_composite_memory_region *composite;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001323 uint32_t receivers_length;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001324 uint32_t constituents_offset;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001325 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001326 enum ffa_data_access data_access;
1327 enum ffa_instruction_access instruction_access;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001328
Andrew Walbrana65a1322020-04-06 19:32:32 +01001329 CHECK(permissions != NULL);
1330
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001331 /*
1332 * This should already be checked by the caller, just making the
1333 * assumption clear here.
1334 */
1335 CHECK(memory_region->receiver_count == 1);
1336
Andrew Walbrana65a1322020-04-06 19:32:32 +01001337 /* The sender must match the message sender. */
1338 if (memory_region->sender != from_locked.vm->id) {
1339 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001340 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001341 }
1342
Andrew Walbrana65a1322020-04-06 19:32:32 +01001343 /*
1344 * Ensure that the composite header is within the memory bounds and
1345 * doesn't overlap the first part of the message.
1346 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001347 receivers_length = sizeof(struct ffa_memory_access) *
1348 memory_region->receiver_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001349 constituents_offset =
1350 ffa_composite_constituent_offset(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001351 if (memory_region->receivers[0].composite_memory_region_offset <
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001352 sizeof(struct ffa_memory_region) + receivers_length ||
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001353 constituents_offset > fragment_length) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001354 dlog_verbose(
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001355 "Invalid composite memory region descriptor offset "
1356 "%d.\n",
1357 memory_region->receivers[0]
1358 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001359 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001360 }
1361
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001362 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001363
1364 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001365 * Ensure the number of constituents are within the memory bounds.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001366 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001367 constituents_length = sizeof(struct ffa_memory_region_constituent) *
1368 composite->constituent_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001369 if (memory_share_length != constituents_offset + constituents_length) {
1370 dlog_verbose("Invalid length %d or composite offset %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001371 memory_share_length,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001372 memory_region->receivers[0]
1373 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001374 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001375 }
Andrew Walbranca808b12020-05-15 17:22:28 +01001376 if (fragment_length < memory_share_length &&
1377 fragment_length < HF_MAILBOX_SIZE) {
1378 dlog_warning(
1379 "Initial fragment length %d smaller than mailbox "
1380 "size.\n",
1381 fragment_length);
1382 }
Andrew Walbrana65a1322020-04-06 19:32:32 +01001383
Andrew Walbrana65a1322020-04-06 19:32:32 +01001384 /*
1385 * Clear is not allowed for memory sharing, as the sender still has
1386 * access to the memory.
1387 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001388 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1389 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001390 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001391 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001392 }
1393
1394 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001395 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001396 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001397 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001398 }
1399
1400 /* Check that the permissions are valid. */
1401 *permissions =
1402 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001403 data_access = ffa_get_data_access_attr(*permissions);
1404 instruction_access = ffa_get_instruction_access_attr(*permissions);
1405 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1406 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001407 dlog_verbose("Reserved value for receiver permissions %#x.\n",
1408 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001409 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001410 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001411 if (instruction_access != FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001412 dlog_verbose(
1413 "Invalid instruction access permissions %#x for "
1414 "sending memory.\n",
1415 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001416 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001417 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001418 if (share_func == FFA_MEM_SHARE_32) {
1419 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001420 dlog_verbose(
1421 "Invalid data access permissions %#x for "
1422 "sharing memory.\n",
1423 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001424 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001425 }
1426 /*
1427 * According to section 6.11.3 of the FF-A spec NX is required
1428 * for share operations (but must not be specified by the
1429 * sender) so set it in the copy that we store, ready to be
1430 * returned to the retriever.
1431 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001432 ffa_set_instruction_access_attr(permissions,
1433 FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001434 memory_region->receivers[0].receiver_permissions.permissions =
1435 *permissions;
1436 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001437 if (share_func == FFA_MEM_LEND_32 &&
1438 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001439 dlog_verbose(
1440 "Invalid data access permissions %#x for lending "
1441 "memory.\n",
1442 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001443 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001444 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001445 if (share_func == FFA_MEM_DONATE_32 &&
1446 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001447 dlog_verbose(
1448 "Invalid data access permissions %#x for donating "
1449 "memory.\n",
1450 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001451 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001452 }
1453
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001454 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001455}
1456
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001457/** Forwards a memory send message on to the TEE. */
1458static struct ffa_value memory_send_tee_forward(
1459 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id,
1460 uint32_t share_func, struct ffa_memory_region *memory_region,
1461 uint32_t memory_share_length, uint32_t fragment_length)
1462{
1463 struct ffa_value ret;
1464
1465 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
1466 memory_region, fragment_length);
1467 tee_locked.vm->mailbox.recv_size = fragment_length;
1468 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1469 tee_locked.vm->mailbox.recv_func = share_func;
1470 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1471 ret = arch_tee_call((struct ffa_value){.func = share_func,
1472 .arg1 = memory_share_length,
1473 .arg2 = fragment_length});
1474 /*
1475 * After the call to the TEE completes it must have finished reading its
1476 * RX buffer, so it is ready for another message.
1477 */
1478 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1479
1480 return ret;
1481}
1482
Andrew Walbrana65a1322020-04-06 19:32:32 +01001483/**
Andrew Walbranca808b12020-05-15 17:22:28 +01001484 * Gets the share state for continuing an operation to donate, lend or share
1485 * memory, and checks that it is a valid request.
1486 *
1487 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
1488 * not.
1489 */
1490static struct ffa_value ffa_memory_send_continue_validate(
1491 struct share_states_locked share_states, ffa_memory_handle_t handle,
1492 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
1493 struct mpool *page_pool)
1494{
1495 struct ffa_memory_share_state *share_state;
1496 struct ffa_memory_region *memory_region;
1497
1498 CHECK(share_state_ret != NULL);
1499
1500 /*
1501 * Look up the share state by handle and make sure that the VM ID
1502 * matches.
1503 */
1504 if (!get_share_state(share_states, handle, &share_state)) {
1505 dlog_verbose(
1506 "Invalid handle %#x for memory send continuation.\n",
1507 handle);
1508 return ffa_error(FFA_INVALID_PARAMETERS);
1509 }
1510 memory_region = share_state->memory_region;
1511
1512 if (memory_region->sender != from_vm_id) {
1513 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
1514 return ffa_error(FFA_INVALID_PARAMETERS);
1515 }
1516
1517 if (share_state->sending_complete) {
1518 dlog_verbose(
1519 "Sending of memory handle %#x is already complete.\n",
1520 handle);
1521 return ffa_error(FFA_INVALID_PARAMETERS);
1522 }
1523
1524 if (share_state->fragment_count == MAX_FRAGMENTS) {
1525 /*
1526 * Log a warning as this is a sign that MAX_FRAGMENTS should
1527 * probably be increased.
1528 */
1529 dlog_warning(
1530 "Too many fragments for memory share with handle %#x; "
1531 "only %d supported.\n",
1532 handle, MAX_FRAGMENTS);
1533 /* Free share state, as it's not possible to complete it. */
1534 share_state_free(share_states, share_state, page_pool);
1535 return ffa_error(FFA_NO_MEMORY);
1536 }
1537
1538 *share_state_ret = share_state;
1539
1540 return (struct ffa_value){.func = FFA_SUCCESS_32};
1541}
1542
1543/**
1544 * Forwards a memory send continuation message on to the TEE.
1545 */
1546static struct ffa_value memory_send_continue_tee_forward(
1547 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id, void *fragment,
1548 uint32_t fragment_length, ffa_memory_handle_t handle)
1549{
1550 struct ffa_value ret;
1551
1552 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, fragment,
1553 fragment_length);
1554 tee_locked.vm->mailbox.recv_size = fragment_length;
1555 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1556 tee_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
1557 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1558 ret = arch_tee_call(
1559 (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
1560 .arg1 = (uint32_t)handle,
1561 .arg2 = (uint32_t)(handle >> 32),
1562 .arg3 = fragment_length,
1563 .arg4 = (uint64_t)sender_vm_id << 16});
1564 /*
1565 * After the call to the TEE completes it must have finished reading its
1566 * RX buffer, so it is ready for another message.
1567 */
1568 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1569
1570 return ret;
1571}
1572
1573/**
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001574 * Validates a call to donate, lend or share memory to a non-TEE VM and then
1575 * updates the stage-2 page tables. Specifically, check if the message length
1576 * and number of memory region constituents match, and if the transition is
1577 * valid for the type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001578 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001579 * Assumes that the caller has already found and locked the sender VM and copied
1580 * the memory region descriptor from the sender's TX buffer to a freshly
1581 * allocated page from Hafnium's internal pool. The caller must have also
1582 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001583 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001584 * This function takes ownership of the `memory_region` passed in and will free
1585 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001586 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001587struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001588 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001589 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001590 uint32_t fragment_length, uint32_t share_func,
1591 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001592{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001593 ffa_memory_access_permissions_t permissions;
1594 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01001595 struct share_states_locked share_states;
1596 struct ffa_memory_share_state *share_state;
Jose Marinho09b1db82019-08-08 09:16:59 +01001597
1598 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001599 * If there is an error validating the `memory_region` then we need to
1600 * free it because we own it but we won't be storing it in a share state
1601 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001602 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001603 ret = ffa_memory_send_validate(from_locked, memory_region,
1604 memory_share_length, fragment_length,
1605 share_func, &permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001606 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001607 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001608 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001609 }
1610
Andrew Walbrana65a1322020-04-06 19:32:32 +01001611 /* Set flag for share function, ready to be retrieved later. */
1612 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001613 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001614 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001615 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001616 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001617 case FFA_MEM_LEND_32:
1618 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001619 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001620 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001621 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001622 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001623 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001624 }
1625
Andrew Walbranca808b12020-05-15 17:22:28 +01001626 share_states = share_states_lock();
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001627 /*
1628 * Allocate a share state before updating the page table. Otherwise if
1629 * updating the page table succeeded but allocating the share state
1630 * failed then it would leave the memory in a state where nobody could
1631 * get it back.
1632 */
Andrew Walbranca808b12020-05-15 17:22:28 +01001633 if (!allocate_share_state(share_states, share_func, memory_region,
1634 fragment_length, FFA_MEMORY_HANDLE_INVALID,
1635 &share_state)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001636 dlog_verbose("Failed to allocate share state.\n");
1637 mpool_free(page_pool, memory_region);
Andrew Walbranca808b12020-05-15 17:22:28 +01001638 ret = ffa_error(FFA_NO_MEMORY);
1639 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001640 }
1641
Andrew Walbranca808b12020-05-15 17:22:28 +01001642 if (fragment_length == memory_share_length) {
1643 /* No more fragments to come, everything fit in one message. */
1644 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001645 share_state, page_pool, NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001646 } else {
1647 ret = (struct ffa_value){
1648 .func = FFA_MEM_FRAG_RX_32,
1649 .arg1 = (uint32_t)share_state->handle,
1650 .arg2 = (uint32_t)(share_state->handle >> 32),
1651 .arg3 = fragment_length};
1652 }
1653
1654out:
1655 share_states_unlock(&share_states);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001656 dump_share_states();
Andrew Walbranca808b12020-05-15 17:22:28 +01001657 return ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001658}
1659
1660/**
1661 * Validates a call to donate, lend or share memory to the TEE and then updates
1662 * the stage-2 page tables. Specifically, check if the message length and number
1663 * of memory region constituents match, and if the transition is valid for the
1664 * type of memory sending operation.
1665 *
1666 * Assumes that the caller has already found and locked the sender VM and the
1667 * TEE VM, and copied the memory region descriptor from the sender's TX buffer
1668 * to a freshly allocated page from Hafnium's internal pool. The caller must
1669 * have also validated that the receiver VM ID is valid.
1670 *
1671 * This function takes ownership of the `memory_region` passed in and will free
1672 * it when necessary; it must not be freed by the caller.
1673 */
1674struct ffa_value ffa_memory_tee_send(
1675 struct vm_locked from_locked, struct vm_locked to_locked,
1676 struct ffa_memory_region *memory_region, uint32_t memory_share_length,
1677 uint32_t fragment_length, uint32_t share_func, struct mpool *page_pool)
1678{
1679 ffa_memory_access_permissions_t permissions;
1680 struct ffa_value ret;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001681
1682 /*
1683 * If there is an error validating the `memory_region` then we need to
1684 * free it because we own it but we won't be storing it in a share state
1685 * after all.
1686 */
1687 ret = ffa_memory_send_validate(from_locked, memory_region,
1688 memory_share_length, fragment_length,
1689 share_func, &permissions);
1690 if (ret.func != FFA_SUCCESS_32) {
1691 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001692 }
1693
Andrew Walbranca808b12020-05-15 17:22:28 +01001694 if (fragment_length == memory_share_length) {
1695 /* No more fragments to come, everything fit in one message. */
1696 struct ffa_composite_memory_region *composite =
1697 ffa_memory_region_get_composite(memory_region, 0);
1698 struct ffa_memory_region_constituent *constituents =
1699 composite->constituents;
Andrew Walbran37c574e2020-06-03 11:45:46 +01001700 struct mpool local_page_pool;
1701 uint32_t orig_from_mode;
1702
1703 /*
1704 * Use a local page pool so that we can roll back if necessary.
1705 */
1706 mpool_init_with_fallback(&local_page_pool, page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001707
1708 ret = ffa_send_check_update(
1709 from_locked, &constituents,
1710 &composite->constituent_count, 1, share_func,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001711 permissions, &local_page_pool,
1712 memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR,
1713 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001714 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001715 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001716 goto out;
1717 }
1718
1719 /* Forward memory send message on to TEE. */
1720 ret = memory_send_tee_forward(
1721 to_locked, from_locked.vm->id, share_func,
1722 memory_region, memory_share_length, fragment_length);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001723
1724 if (ret.func != FFA_SUCCESS_32) {
1725 dlog_verbose(
1726 "TEE didn't successfully complete memory send "
1727 "operation; returned %#x (%d). Rolling back.\n",
1728 ret.func, ret.arg2);
1729
1730 /*
1731 * The TEE failed to complete the send operation, so
1732 * roll back the page table update for the VM. This
1733 * can't fail because it won't try to allocate more
1734 * memory than was freed into the `local_page_pool` by
1735 * `ffa_send_check_update` in the initial update.
1736 */
1737 CHECK(ffa_region_group_identity_map(
1738 from_locked, &constituents,
1739 &composite->constituent_count, 1,
1740 orig_from_mode, &local_page_pool, true));
1741 }
1742
1743 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01001744 } else {
1745 struct share_states_locked share_states = share_states_lock();
1746 ffa_memory_handle_t handle;
1747
1748 /*
1749 * We need to wait for the rest of the fragments before we can
1750 * check whether the transaction is valid and unmap the memory.
1751 * Call the TEE so it can do its initial validation and assign a
1752 * handle, and allocate a share state to keep what we have so
1753 * far.
1754 */
1755 ret = memory_send_tee_forward(
1756 to_locked, from_locked.vm->id, share_func,
1757 memory_region, memory_share_length, fragment_length);
1758 if (ret.func == FFA_ERROR_32) {
1759 goto out_unlock;
1760 } else if (ret.func != FFA_MEM_FRAG_RX_32) {
1761 dlog_warning(
1762 "Got %#x from TEE in response to %#x for "
1763 "fragment with with %d/%d, expected "
1764 "FFA_MEM_FRAG_RX.\n",
1765 ret.func, share_func, fragment_length,
1766 memory_share_length);
1767 ret = ffa_error(FFA_INVALID_PARAMETERS);
1768 goto out_unlock;
1769 }
1770 handle = ffa_frag_handle(ret);
1771 if (ret.arg3 != fragment_length) {
1772 dlog_warning(
1773 "Got unexpected fragment offset %d for "
1774 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1775 ret.arg3, fragment_length);
1776 ret = ffa_error(FFA_INVALID_PARAMETERS);
1777 goto out_unlock;
1778 }
1779 if (ffa_frag_sender(ret) != from_locked.vm->id) {
1780 dlog_warning(
1781 "Got unexpected sender ID %d for "
1782 "FFA_MEM_FRAG_RX from TEE (expected %d).\n",
1783 ffa_frag_sender(ret), from_locked.vm->id);
1784 ret = ffa_error(FFA_INVALID_PARAMETERS);
1785 goto out_unlock;
1786 }
1787
1788 if (!allocate_share_state(share_states, share_func,
1789 memory_region, fragment_length,
1790 handle, NULL)) {
1791 dlog_verbose("Failed to allocate share state.\n");
1792 ret = ffa_error(FFA_NO_MEMORY);
1793 goto out_unlock;
1794 }
1795 /*
1796 * Don't free the memory region fragment, as it has been stored
1797 * in the share state.
1798 */
1799 memory_region = NULL;
1800 out_unlock:
1801 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001802 }
1803
Andrew Walbranca808b12020-05-15 17:22:28 +01001804out:
1805 if (memory_region != NULL) {
1806 mpool_free(page_pool, memory_region);
1807 }
1808 dump_share_states();
1809 return ret;
1810}
1811
1812/**
1813 * Continues an operation to donate, lend or share memory to a non-TEE VM. If
1814 * this is the last fragment then checks that the transition is valid for the
1815 * type of memory sending operation and updates the stage-2 page tables of the
1816 * sender.
1817 *
1818 * Assumes that the caller has already found and locked the sender VM and copied
1819 * the memory region descriptor from the sender's TX buffer to a freshly
1820 * allocated page from Hafnium's internal pool.
1821 *
1822 * This function takes ownership of the `fragment` passed in; it must not be
1823 * freed by the caller.
1824 */
1825struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked,
1826 void *fragment,
1827 uint32_t fragment_length,
1828 ffa_memory_handle_t handle,
1829 struct mpool *page_pool)
1830{
1831 struct share_states_locked share_states = share_states_lock();
1832 struct ffa_memory_share_state *share_state;
1833 struct ffa_value ret;
1834 struct ffa_memory_region *memory_region;
1835
1836 ret = ffa_memory_send_continue_validate(share_states, handle,
1837 &share_state,
1838 from_locked.vm->id, page_pool);
1839 if (ret.func != FFA_SUCCESS_32) {
1840 goto out_free_fragment;
1841 }
1842 memory_region = share_state->memory_region;
1843
1844 if (memory_region->receivers[0].receiver_permissions.receiver ==
1845 HF_TEE_VM_ID) {
1846 dlog_error(
1847 "Got hypervisor-allocated handle for memory send to "
1848 "TEE. This should never happen, and indicates a bug in "
1849 "EL3 code.\n");
1850 ret = ffa_error(FFA_INVALID_PARAMETERS);
1851 goto out_free_fragment;
1852 }
1853
1854 /* Add this fragment. */
1855 share_state->fragments[share_state->fragment_count] = fragment;
1856 share_state->fragment_constituent_counts[share_state->fragment_count] =
1857 fragment_length / sizeof(struct ffa_memory_region_constituent);
1858 share_state->fragment_count++;
1859
1860 /* Check whether the memory send operation is now ready to complete. */
1861 if (share_state_sending_complete(share_states, share_state)) {
1862 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001863 share_state, page_pool, NULL);
Andrew Walbranca808b12020-05-15 17:22:28 +01001864 } else {
1865 ret = (struct ffa_value){
1866 .func = FFA_MEM_FRAG_RX_32,
1867 .arg1 = (uint32_t)handle,
1868 .arg2 = (uint32_t)(handle >> 32),
1869 .arg3 = share_state_next_fragment_offset(share_states,
1870 share_state)};
1871 }
1872 goto out;
1873
1874out_free_fragment:
1875 mpool_free(page_pool, fragment);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001876
1877out:
Andrew Walbranca808b12020-05-15 17:22:28 +01001878 share_states_unlock(&share_states);
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001879 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001880}
1881
Andrew Walbranca808b12020-05-15 17:22:28 +01001882/**
1883 * Continues an operation to donate, lend or share memory to the TEE VM. If this
1884 * is the last fragment then checks that the transition is valid for the type of
1885 * memory sending operation and updates the stage-2 page tables of the sender.
1886 *
1887 * Assumes that the caller has already found and locked the sender VM and copied
1888 * the memory region descriptor from the sender's TX buffer to a freshly
1889 * allocated page from Hafnium's internal pool.
1890 *
1891 * This function takes ownership of the `memory_region` passed in and will free
1892 * it when necessary; it must not be freed by the caller.
1893 */
1894struct ffa_value ffa_memory_tee_send_continue(struct vm_locked from_locked,
1895 struct vm_locked to_locked,
1896 void *fragment,
1897 uint32_t fragment_length,
1898 ffa_memory_handle_t handle,
1899 struct mpool *page_pool)
1900{
1901 struct share_states_locked share_states = share_states_lock();
1902 struct ffa_memory_share_state *share_state;
1903 struct ffa_value ret;
1904 struct ffa_memory_region *memory_region;
1905
1906 ret = ffa_memory_send_continue_validate(share_states, handle,
1907 &share_state,
1908 from_locked.vm->id, page_pool);
1909 if (ret.func != FFA_SUCCESS_32) {
1910 goto out_free_fragment;
1911 }
1912 memory_region = share_state->memory_region;
1913
1914 if (memory_region->receivers[0].receiver_permissions.receiver !=
1915 HF_TEE_VM_ID) {
1916 dlog_error(
1917 "Got SPM-allocated handle for memory send to non-TEE "
1918 "VM. This should never happen, and indicates a bug.\n");
1919 ret = ffa_error(FFA_INVALID_PARAMETERS);
1920 goto out_free_fragment;
1921 }
1922
1923 if (to_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY ||
1924 to_locked.vm->mailbox.recv == NULL) {
1925 /*
1926 * If the TEE RX buffer is not available, tell the sender to
1927 * retry by returning the current offset again.
1928 */
1929 ret = (struct ffa_value){
1930 .func = FFA_MEM_FRAG_RX_32,
1931 .arg1 = (uint32_t)handle,
1932 .arg2 = (uint32_t)(handle >> 32),
1933 .arg3 = share_state_next_fragment_offset(share_states,
1934 share_state),
1935 };
1936 goto out_free_fragment;
1937 }
1938
1939 /* Add this fragment. */
1940 share_state->fragments[share_state->fragment_count] = fragment;
1941 share_state->fragment_constituent_counts[share_state->fragment_count] =
1942 fragment_length / sizeof(struct ffa_memory_region_constituent);
1943 share_state->fragment_count++;
1944
1945 /* Check whether the memory send operation is now ready to complete. */
1946 if (share_state_sending_complete(share_states, share_state)) {
Andrew Walbran37c574e2020-06-03 11:45:46 +01001947 struct mpool local_page_pool;
1948 uint32_t orig_from_mode;
1949
1950 /*
1951 * Use a local page pool so that we can roll back if necessary.
1952 */
1953 mpool_init_with_fallback(&local_page_pool, page_pool);
1954
Andrew Walbranca808b12020-05-15 17:22:28 +01001955 ret = ffa_memory_send_complete(from_locked, share_states,
Andrew Walbran37c574e2020-06-03 11:45:46 +01001956 share_state, &local_page_pool,
1957 &orig_from_mode);
Andrew Walbranca808b12020-05-15 17:22:28 +01001958
1959 if (ret.func == FFA_SUCCESS_32) {
1960 /*
1961 * Forward final fragment on to the TEE so that
1962 * it can complete the memory sending operation.
1963 */
1964 ret = memory_send_continue_tee_forward(
1965 to_locked, from_locked.vm->id, fragment,
1966 fragment_length, handle);
1967
1968 if (ret.func != FFA_SUCCESS_32) {
1969 /*
1970 * The error will be passed on to the caller,
1971 * but log it here too.
1972 */
1973 dlog_verbose(
1974 "TEE didn't successfully complete "
1975 "memory send operation; returned %#x "
Andrew Walbran37c574e2020-06-03 11:45:46 +01001976 "(%d). Rolling back.\n",
Andrew Walbranca808b12020-05-15 17:22:28 +01001977 ret.func, ret.arg2);
Andrew Walbran37c574e2020-06-03 11:45:46 +01001978
1979 /*
1980 * The TEE failed to complete the send
1981 * operation, so roll back the page table update
1982 * for the VM. This can't fail because it won't
1983 * try to allocate more memory than was freed
1984 * into the `local_page_pool` by
1985 * `ffa_send_check_update` in the initial
1986 * update.
1987 */
1988 CHECK(ffa_region_group_identity_map(
1989 from_locked, share_state->fragments,
1990 share_state
1991 ->fragment_constituent_counts,
1992 share_state->fragment_count,
1993 orig_from_mode, &local_page_pool,
1994 true));
Andrew Walbranca808b12020-05-15 17:22:28 +01001995 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01001996
Andrew Walbranca808b12020-05-15 17:22:28 +01001997 /* Free share state. */
1998 share_state_free(share_states, share_state, page_pool);
1999 } else {
2000 /* Abort sending to TEE. */
2001 struct ffa_value tee_ret =
2002 arch_tee_call((struct ffa_value){
2003 .func = FFA_MEM_RECLAIM_32,
2004 .arg1 = (uint32_t)handle,
2005 .arg2 = (uint32_t)(handle >> 32)});
2006
2007 if (tee_ret.func != FFA_SUCCESS_32) {
2008 /*
2009 * Nothing we can do if TEE doesn't abort
2010 * properly, just log it.
2011 */
2012 dlog_verbose(
2013 "TEE didn't successfully abort failed "
2014 "memory send operation; returned %#x "
2015 "(%d).\n",
2016 tee_ret.func, tee_ret.arg2);
2017 }
2018 /*
2019 * We don't need to free the share state in this case
2020 * because ffa_memory_send_complete does that already.
2021 */
2022 }
Andrew Walbran37c574e2020-06-03 11:45:46 +01002023
2024 mpool_fini(&local_page_pool);
Andrew Walbranca808b12020-05-15 17:22:28 +01002025 } else {
2026 uint32_t next_fragment_offset =
2027 share_state_next_fragment_offset(share_states,
2028 share_state);
2029
2030 ret = memory_send_continue_tee_forward(
2031 to_locked, from_locked.vm->id, fragment,
2032 fragment_length, handle);
2033
2034 if (ret.func != FFA_MEM_FRAG_RX_32 ||
2035 ffa_frag_handle(ret) != handle ||
2036 ret.arg3 != next_fragment_offset ||
2037 ffa_frag_sender(ret) != from_locked.vm->id) {
2038 dlog_verbose(
2039 "Got unexpected result from forwarding "
2040 "FFA_MEM_FRAG_TX to TEE: %#x (handle %#x, "
2041 "offset %d, sender %d); expected "
2042 "FFA_MEM_FRAG_RX (handle %#x, offset %d, "
2043 "sender %d).\n",
2044 ret.func, ffa_frag_handle(ret), ret.arg3,
2045 ffa_frag_sender(ret), handle,
2046 next_fragment_offset, from_locked.vm->id);
2047 /* Free share state. */
2048 share_state_free(share_states, share_state, page_pool);
2049 ret = ffa_error(FFA_INVALID_PARAMETERS);
2050 goto out;
2051 }
2052
2053 ret = (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2054 .arg1 = (uint32_t)handle,
2055 .arg2 = (uint32_t)(handle >> 32),
2056 .arg3 = next_fragment_offset};
2057 }
2058 goto out;
2059
2060out_free_fragment:
2061 mpool_free(page_pool, fragment);
2062
2063out:
2064 share_states_unlock(&share_states);
2065 return ret;
2066}
2067
2068/** Clean up after the receiver has finished retrieving a memory region. */
2069static void ffa_memory_retrieve_complete(
2070 struct share_states_locked share_states,
2071 struct ffa_memory_share_state *share_state, struct mpool *page_pool)
2072{
2073 if (share_state->share_func == FFA_MEM_DONATE_32) {
2074 /*
2075 * Memory that has been donated can't be relinquished,
2076 * so no need to keep the share state around.
2077 */
2078 share_state_free(share_states, share_state, page_pool);
2079 dlog_verbose("Freed share state for donate.\n");
2080 }
2081}
2082
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002083struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
2084 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002085 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002086 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002087{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002088 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002089 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01002090 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002091 sizeof(struct ffa_memory_access);
2092 ffa_memory_handle_t handle = retrieve_request->handle;
2093 ffa_memory_region_flags_t transaction_type =
Andrew Walbrana65a1322020-04-06 19:32:32 +01002094 retrieve_request->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002095 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
2096 struct ffa_memory_region *memory_region;
2097 ffa_memory_access_permissions_t sent_permissions;
2098 enum ffa_data_access sent_data_access;
2099 enum ffa_instruction_access sent_instruction_access;
2100 ffa_memory_access_permissions_t requested_permissions;
2101 enum ffa_data_access requested_data_access;
2102 enum ffa_instruction_access requested_instruction_access;
2103 ffa_memory_access_permissions_t permissions;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002104 uint32_t memory_to_attributes;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002105 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002106 struct ffa_memory_share_state *share_state;
2107 struct ffa_value ret;
Andrew Walbranca808b12020-05-15 17:22:28 +01002108 struct ffa_composite_memory_region *composite;
2109 uint32_t total_length;
2110 uint32_t fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002111
2112 dump_share_states();
2113
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002114 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002115 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002116 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002117 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01002118 expected_retrieve_request_length,
2119 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002120 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002121 }
2122
Andrew Walbrana65a1322020-04-06 19:32:32 +01002123 if (retrieve_request->receiver_count != 1) {
2124 dlog_verbose(
2125 "Multi-way memory sharing not supported (got %d "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002126 "receivers descriptors on FFA_MEM_RETRIEVE_REQ, "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002127 "expected 1).\n",
2128 retrieve_request->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002129 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002130 }
2131
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002132 share_states = share_states_lock();
2133 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002134 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002135 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002136 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002137 goto out;
2138 }
2139
Andrew Walbrana65a1322020-04-06 19:32:32 +01002140 memory_region = share_state->memory_region;
2141 CHECK(memory_region != NULL);
2142
2143 /*
2144 * Check that the transaction type expected by the receiver is correct,
2145 * if it has been specified.
2146 */
2147 if (transaction_type !=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002148 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
Andrew Walbrana65a1322020-04-06 19:32:32 +01002149 transaction_type != (memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002150 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002151 dlog_verbose(
2152 "Incorrect transaction type %#x for "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002153 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002154 transaction_type,
2155 memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002156 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002157 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002158 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002159 goto out;
2160 }
2161
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002162 if (retrieve_request->sender != memory_region->sender) {
2163 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002164 "Incorrect sender ID %d for FFA_MEM_RETRIEVE_REQ, "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002165 "expected %d for handle %#x.\n",
2166 retrieve_request->sender, memory_region->sender,
2167 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002168 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002169 goto out;
2170 }
2171
2172 if (retrieve_request->tag != memory_region->tag) {
2173 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002174 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002175 "%d for handle %#x.\n",
2176 retrieve_request->tag, memory_region->tag, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002177 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002178 goto out;
2179 }
2180
Andrew Walbrana65a1322020-04-06 19:32:32 +01002181 if (retrieve_request->receivers[0].receiver_permissions.receiver !=
2182 to_locked.vm->id) {
2183 dlog_verbose(
2184 "Retrieve request receiver VM ID %d didn't match "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002185 "caller of FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002186 retrieve_request->receivers[0]
2187 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002188 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002189 goto out;
2190 }
2191
2192 if (memory_region->receivers[0].receiver_permissions.receiver !=
2193 to_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002194 dlog_verbose(
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002195 "Incorrect receiver VM ID %d for FFA_MEM_RETRIEVE_REQ, "
2196 "expected %d for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002197 to_locked.vm->id,
2198 memory_region->receivers[0]
2199 .receiver_permissions.receiver,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002200 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002201 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002202 goto out;
2203 }
2204
Andrew Walbranca808b12020-05-15 17:22:28 +01002205 if (!share_state->sending_complete) {
2206 dlog_verbose(
2207 "Memory with handle %#x not fully sent, can't "
2208 "retrieve.\n",
2209 handle);
2210 ret = ffa_error(FFA_INVALID_PARAMETERS);
2211 goto out;
2212 }
2213
2214 if (share_state->retrieved_fragment_count[0] != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002215 dlog_verbose("Memory with handle %#x already retrieved.\n",
2216 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002217 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002218 goto out;
2219 }
2220
Andrew Walbrana65a1322020-04-06 19:32:32 +01002221 if (retrieve_request->receivers[0].composite_memory_region_offset !=
2222 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002223 dlog_verbose(
2224 "Retriever specified address ranges not supported (got "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002225 "offset %d).\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002226 retrieve_request->receivers[0]
2227 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002228 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002229 goto out;
2230 }
2231
Andrew Walbrana65a1322020-04-06 19:32:32 +01002232 /*
2233 * Check permissions from sender against permissions requested by
2234 * receiver.
2235 */
2236 /* TODO: Check attributes too. */
2237 sent_permissions =
2238 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002239 sent_data_access = ffa_get_data_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002240 sent_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002241 ffa_get_instruction_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002242 requested_permissions =
2243 retrieve_request->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002244 requested_data_access = ffa_get_data_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002245 requested_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002246 ffa_get_instruction_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002247 permissions = 0;
2248 switch (sent_data_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002249 case FFA_DATA_ACCESS_NOT_SPECIFIED:
2250 case FFA_DATA_ACCESS_RW:
2251 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2252 requested_data_access == FFA_DATA_ACCESS_RW) {
2253 ffa_set_data_access_attr(&permissions,
2254 FFA_DATA_ACCESS_RW);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002255 break;
2256 }
2257 /* Intentional fall-through. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002258 case FFA_DATA_ACCESS_RO:
2259 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
2260 requested_data_access == FFA_DATA_ACCESS_RO) {
2261 ffa_set_data_access_attr(&permissions,
2262 FFA_DATA_ACCESS_RO);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002263 break;
2264 }
2265 dlog_verbose(
2266 "Invalid data access requested; sender specified "
2267 "permissions %#x but receiver requested %#x.\n",
2268 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002269 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002270 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002271 case FFA_DATA_ACCESS_RESERVED:
2272 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002273 "checked before this point.");
2274 }
2275 switch (sent_instruction_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002276 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
2277 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002278 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002279 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2280 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
2281 ffa_set_instruction_access_attr(
2282 &permissions, FFA_INSTRUCTION_ACCESS_X);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002283 break;
2284 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002285 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbrana65a1322020-04-06 19:32:32 +01002286 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002287 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
2288 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
2289 ffa_set_instruction_access_attr(
2290 &permissions, FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002291 break;
2292 }
2293 dlog_verbose(
2294 "Invalid instruction access requested; sender "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01002295 "specified permissions %#x but receiver requested "
2296 "%#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002297 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002298 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01002299 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002300 case FFA_INSTRUCTION_ACCESS_RESERVED:
2301 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
Andrew Walbrana65a1322020-04-06 19:32:32 +01002302 "be checked before this point.");
2303 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002304 memory_to_attributes = ffa_memory_permissions_to_mode(permissions);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002305
Andrew Walbran996d1d12020-05-27 14:08:43 +01002306 ret = ffa_retrieve_check_update(
Andrew Walbranca808b12020-05-15 17:22:28 +01002307 to_locked, share_state->fragments,
2308 share_state->fragment_constituent_counts,
2309 share_state->fragment_count, memory_to_attributes,
Andrew Walbran996d1d12020-05-27 14:08:43 +01002310 share_state->share_func, false, page_pool);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002311 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002312 goto out;
2313 }
2314
2315 /*
2316 * Copy response to RX buffer of caller and deliver the message. This
2317 * must be done before the share_state is (possibly) freed.
2318 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01002319 /* TODO: combine attributes from sender and request. */
Andrew Walbranca808b12020-05-15 17:22:28 +01002320 composite = ffa_memory_region_get_composite(memory_region, 0);
2321 /*
2322 * Constituents which we received in the first fragment should always
2323 * fit in the first fragment we are sending, because the header is the
2324 * same size in both cases and we have a fixed message buffer size. So
2325 * `ffa_retrieved_memory_region_init` should never fail.
2326 */
2327 CHECK(ffa_retrieved_memory_region_init(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002328 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2329 memory_region->sender, memory_region->attributes,
2330 memory_region->flags, handle, to_locked.vm->id, permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +01002331 composite->page_count, composite->constituent_count,
2332 share_state->fragments[0],
2333 share_state->fragment_constituent_counts[0], &total_length,
2334 &fragment_length));
2335 to_locked.vm->mailbox.recv_size = fragment_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002336 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002337 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002338 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2339
Andrew Walbranca808b12020-05-15 17:22:28 +01002340 share_state->retrieved_fragment_count[0] = 1;
2341 if (share_state->retrieved_fragment_count[0] ==
2342 share_state->fragment_count) {
2343 ffa_memory_retrieve_complete(share_states, share_state,
2344 page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002345 }
2346
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002347 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbranca808b12020-05-15 17:22:28 +01002348 .arg1 = total_length,
2349 .arg2 = fragment_length};
2350
2351out:
2352 share_states_unlock(&share_states);
2353 dump_share_states();
2354 return ret;
2355}
2356
2357struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked,
2358 ffa_memory_handle_t handle,
2359 uint32_t fragment_offset,
2360 struct mpool *page_pool)
2361{
2362 struct ffa_memory_region *memory_region;
2363 struct share_states_locked share_states;
2364 struct ffa_memory_share_state *share_state;
2365 struct ffa_value ret;
2366 uint32_t fragment_index;
2367 uint32_t retrieved_constituents_count;
2368 uint32_t i;
2369 uint32_t expected_fragment_offset;
2370 uint32_t remaining_constituent_count;
2371 uint32_t fragment_length;
2372
2373 dump_share_states();
2374
2375 share_states = share_states_lock();
2376 if (!get_share_state(share_states, handle, &share_state)) {
2377 dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n",
2378 handle);
2379 ret = ffa_error(FFA_INVALID_PARAMETERS);
2380 goto out;
2381 }
2382
2383 memory_region = share_state->memory_region;
2384 CHECK(memory_region != NULL);
2385
2386 if (memory_region->receivers[0].receiver_permissions.receiver !=
2387 to_locked.vm->id) {
2388 dlog_verbose(
2389 "Caller of FFA_MEM_FRAG_RX (%d) is not receiver (%d) "
2390 "of handle %#x.\n",
2391 to_locked.vm->id,
2392 memory_region->receivers[0]
2393 .receiver_permissions.receiver,
2394 handle);
2395 ret = ffa_error(FFA_INVALID_PARAMETERS);
2396 goto out;
2397 }
2398
2399 if (!share_state->sending_complete) {
2400 dlog_verbose(
2401 "Memory with handle %#x not fully sent, can't "
2402 "retrieve.\n",
2403 handle);
2404 ret = ffa_error(FFA_INVALID_PARAMETERS);
2405 goto out;
2406 }
2407
2408 if (share_state->retrieved_fragment_count[0] == 0 ||
2409 share_state->retrieved_fragment_count[0] >=
2410 share_state->fragment_count) {
2411 dlog_verbose(
2412 "Retrieval of memory with handle %#x not yet started "
2413 "or already completed (%d/%d fragments retrieved).\n",
2414 handle, share_state->retrieved_fragment_count[0],
2415 share_state->fragment_count);
2416 ret = ffa_error(FFA_INVALID_PARAMETERS);
2417 goto out;
2418 }
2419
2420 fragment_index = share_state->retrieved_fragment_count[0];
2421
2422 /*
2423 * Check that the given fragment offset is correct by counting how many
2424 * constituents were in the fragments previously sent.
2425 */
2426 retrieved_constituents_count = 0;
2427 for (i = 0; i < fragment_index; ++i) {
2428 retrieved_constituents_count +=
2429 share_state->fragment_constituent_counts[i];
2430 }
2431 expected_fragment_offset =
2432 ffa_composite_constituent_offset(memory_region, 0) +
2433 retrieved_constituents_count *
2434 sizeof(struct ffa_memory_region_constituent);
2435 if (fragment_offset != expected_fragment_offset) {
2436 dlog_verbose("Fragment offset was %d but expected %d.\n",
2437 fragment_offset, expected_fragment_offset);
2438 ret = ffa_error(FFA_INVALID_PARAMETERS);
2439 goto out;
2440 }
2441
2442 remaining_constituent_count = ffa_memory_fragment_init(
2443 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
2444 share_state->fragments[fragment_index],
2445 share_state->fragment_constituent_counts[fragment_index],
2446 &fragment_length);
2447 CHECK(remaining_constituent_count == 0);
2448 to_locked.vm->mailbox.recv_size = fragment_length;
2449 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
2450 to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32;
2451 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
2452 share_state->retrieved_fragment_count[0]++;
2453 if (share_state->retrieved_fragment_count[0] ==
2454 share_state->fragment_count) {
2455 ffa_memory_retrieve_complete(share_states, share_state,
2456 page_pool);
2457 }
2458
2459 ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32,
2460 .arg1 = (uint32_t)handle,
2461 .arg2 = (uint32_t)(handle >> 32),
2462 .arg3 = fragment_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002463
2464out:
2465 share_states_unlock(&share_states);
2466 dump_share_states();
2467 return ret;
2468}
2469
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002470struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002471 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002472 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002473{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002474 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002475 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002476 struct ffa_memory_share_state *share_state;
2477 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002478 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002479 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002480
Andrew Walbrana65a1322020-04-06 19:32:32 +01002481 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002482 dlog_verbose(
Andrew Walbrana65a1322020-04-06 19:32:32 +01002483 "Stream endpoints not supported (got %d endpoints on "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002484 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002485 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002486 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002487 }
2488
Andrew Walbrana65a1322020-04-06 19:32:32 +01002489 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002490 dlog_verbose(
2491 "VM ID %d in relinquish message doesn't match calling "
2492 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01002493 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002494 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002495 }
2496
2497 dump_share_states();
2498
2499 share_states = share_states_lock();
2500 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002501 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002502 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002503 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002504 goto out;
2505 }
2506
Andrew Walbranca808b12020-05-15 17:22:28 +01002507 if (!share_state->sending_complete) {
2508 dlog_verbose(
2509 "Memory with handle %#x not fully sent, can't "
2510 "relinquish.\n",
2511 handle);
2512 ret = ffa_error(FFA_INVALID_PARAMETERS);
2513 goto out;
2514 }
2515
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002516 memory_region = share_state->memory_region;
2517 CHECK(memory_region != NULL);
2518
Andrew Walbrana65a1322020-04-06 19:32:32 +01002519 if (memory_region->receivers[0].receiver_permissions.receiver !=
2520 from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002521 dlog_verbose(
2522 "VM ID %d tried to relinquish memory region with "
2523 "handle %#x but receiver was %d.\n",
2524 from_locked.vm->id, handle,
Andrew Walbrana65a1322020-04-06 19:32:32 +01002525 memory_region->receivers[0]
2526 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002527 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002528 goto out;
2529 }
2530
Andrew Walbranca808b12020-05-15 17:22:28 +01002531 if (share_state->retrieved_fragment_count[0] !=
2532 share_state->fragment_count) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002533 dlog_verbose(
Andrew Walbranca808b12020-05-15 17:22:28 +01002534 "Memory with handle %#x not yet fully retrieved, can't "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002535 "relinquish.\n",
2536 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002537 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002538 goto out;
2539 }
2540
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002541 clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002542
2543 /*
2544 * Clear is not allowed for memory that was shared, as the original
2545 * sender still has access to the memory.
2546 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002547 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002548 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002549 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002550 goto out;
2551 }
2552
Andrew Walbranca808b12020-05-15 17:22:28 +01002553 ret = ffa_relinquish_check_update(
2554 from_locked, share_state->fragments,
2555 share_state->fragment_constituent_counts,
2556 share_state->fragment_count, page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002557
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002558 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002559 /*
2560 * Mark memory handle as not retrieved, so it can be reclaimed
2561 * (or retrieved again).
2562 */
Andrew Walbranca808b12020-05-15 17:22:28 +01002563 share_state->retrieved_fragment_count[0] = 0;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002564 }
2565
2566out:
2567 share_states_unlock(&share_states);
2568 dump_share_states();
2569 return ret;
2570}
2571
2572/**
2573 * Validates that the reclaim transition is allowed for the given handle,
2574 * updates the page table of the reclaiming VM, and frees the internal state
2575 * associated with the handle.
2576 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002577struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002578 ffa_memory_handle_t handle,
2579 ffa_memory_region_flags_t flags,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002580 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002581{
2582 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002583 struct ffa_memory_share_state *share_state;
2584 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002585 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002586 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002587
2588 dump_share_states();
2589
2590 share_states = share_states_lock();
2591 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002592 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002593 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002594 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002595 goto out;
2596 }
2597
2598 memory_region = share_state->memory_region;
2599 CHECK(memory_region != NULL);
2600
2601 if (to_locked.vm->id != memory_region->sender) {
2602 dlog_verbose(
2603 "VM %d attempted to reclaim memory handle %#x "
2604 "originally sent by VM %d.\n",
2605 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002606 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002607 goto out;
2608 }
2609
Andrew Walbranca808b12020-05-15 17:22:28 +01002610 if (!share_state->sending_complete) {
2611 dlog_verbose(
2612 "Memory with handle %#x not fully sent, can't "
2613 "reclaim.\n",
2614 handle);
2615 ret = ffa_error(FFA_INVALID_PARAMETERS);
2616 goto out;
2617 }
2618
2619 if (share_state->retrieved_fragment_count[0] != 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002620 dlog_verbose(
2621 "Tried to reclaim memory handle %#x that has not been "
2622 "relinquished.\n",
2623 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002624 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002625 goto out;
2626 }
2627
Andrew Walbranca808b12020-05-15 17:22:28 +01002628 ret = ffa_retrieve_check_update(
2629 to_locked, share_state->fragments,
2630 share_state->fragment_constituent_counts,
2631 share_state->fragment_count, memory_to_attributes,
2632 FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002633
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002634 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00002635 share_state_free(share_states, share_state, page_pool);
2636 dlog_verbose("Freed share state after successful reclaim.\n");
2637 }
2638
2639out:
2640 share_states_unlock(&share_states);
2641 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01002642}
Andrew Walbran290b0c92020-02-03 16:37:14 +00002643
2644/**
Andrew Walbranca808b12020-05-15 17:22:28 +01002645 * Validates that the reclaim transition is allowed for the memory region with
2646 * the given handle which was previously shared with the TEE, tells the TEE to
2647 * mark it as reclaimed, and updates the page table of the reclaiming VM.
2648 *
2649 * To do this information about the memory region is first fetched from the TEE.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002650 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002651struct ffa_value ffa_memory_tee_reclaim(struct vm_locked to_locked,
Andrew Walbranca808b12020-05-15 17:22:28 +01002652 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002653 ffa_memory_handle_t handle,
Andrew Walbranca808b12020-05-15 17:22:28 +01002654 ffa_memory_region_flags_t flags,
2655 struct mpool *page_pool)
Andrew Walbran290b0c92020-02-03 16:37:14 +00002656{
Andrew Walbranca808b12020-05-15 17:22:28 +01002657 uint32_t request_length = ffa_memory_lender_retrieve_request_init(
2658 from_locked.vm->mailbox.recv, handle, to_locked.vm->id);
2659 struct ffa_value tee_ret;
2660 uint32_t length;
2661 uint32_t fragment_length;
2662 uint32_t fragment_offset;
2663 struct ffa_memory_region *memory_region;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002664 struct ffa_composite_memory_region *composite;
Andrew Walbranca808b12020-05-15 17:22:28 +01002665 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
2666
2667 CHECK(request_length <= HF_MAILBOX_SIZE);
2668 CHECK(from_locked.vm->id == HF_TEE_VM_ID);
2669
2670 /* Retrieve memory region information from the TEE. */
2671 tee_ret = arch_tee_call(
2672 (struct ffa_value){.func = FFA_MEM_RETRIEVE_REQ_32,
2673 .arg1 = request_length,
2674 .arg2 = request_length});
2675 if (tee_ret.func == FFA_ERROR_32) {
2676 dlog_verbose("Got error %d from EL3.\n", tee_ret.arg2);
2677 return tee_ret;
2678 }
2679 if (tee_ret.func != FFA_MEM_RETRIEVE_RESP_32) {
2680 dlog_verbose(
2681 "Got %#x from EL3, expected FFA_MEM_RETRIEVE_RESP.\n",
2682 tee_ret.func);
2683 return ffa_error(FFA_INVALID_PARAMETERS);
2684 }
2685
2686 length = tee_ret.arg1;
2687 fragment_length = tee_ret.arg2;
2688
2689 if (fragment_length > HF_MAILBOX_SIZE || fragment_length > length ||
2690 length > sizeof(tee_retrieve_buffer)) {
2691 dlog_verbose("Invalid fragment length %d/%d (max %d/%d).\n",
2692 fragment_length, length, HF_MAILBOX_SIZE,
2693 sizeof(tee_retrieve_buffer));
2694 return ffa_error(FFA_INVALID_PARAMETERS);
2695 }
2696
2697 /*
2698 * Copy the first fragment of the memory region descriptor to an
2699 * internal buffer.
2700 */
2701 memcpy_s(tee_retrieve_buffer, sizeof(tee_retrieve_buffer),
2702 from_locked.vm->mailbox.send, fragment_length);
2703
2704 /* Fetch the remaining fragments into the same buffer. */
2705 fragment_offset = fragment_length;
2706 while (fragment_offset < length) {
2707 tee_ret = arch_tee_call(
2708 (struct ffa_value){.func = FFA_MEM_FRAG_RX_32,
2709 .arg1 = (uint32_t)handle,
2710 .arg2 = (uint32_t)(handle >> 32),
2711 .arg3 = fragment_offset});
2712 if (tee_ret.func != FFA_MEM_FRAG_TX_32) {
2713 dlog_verbose(
2714 "Got %#x (%d) from TEE in response to "
2715 "FFA_MEM_FRAG_RX, expected FFA_MEM_FRAG_TX.\n",
2716 tee_ret.func, tee_ret.arg2);
2717 return tee_ret;
2718 }
2719 if (ffa_frag_handle(tee_ret) != handle) {
2720 dlog_verbose(
2721 "Got FFA_MEM_FRAG_TX for unexpected handle %#x "
2722 "in response to FFA_MEM_FRAG_RX for handle "
2723 "%#x.\n",
2724 ffa_frag_handle(tee_ret), handle);
2725 return ffa_error(FFA_INVALID_PARAMETERS);
2726 }
2727 if (ffa_frag_sender(tee_ret) != 0) {
2728 dlog_verbose(
2729 "Got FFA_MEM_FRAG_TX with unexpected sender %d "
2730 "(expected 0).\n",
2731 ffa_frag_sender(tee_ret));
2732 return ffa_error(FFA_INVALID_PARAMETERS);
2733 }
2734 fragment_length = tee_ret.arg3;
2735 if (fragment_length > HF_MAILBOX_SIZE ||
2736 fragment_offset + fragment_length > length) {
2737 dlog_verbose(
2738 "Invalid fragment length %d at offset %d (max "
2739 "%d).\n",
2740 fragment_length, fragment_offset,
2741 HF_MAILBOX_SIZE);
2742 return ffa_error(FFA_INVALID_PARAMETERS);
2743 }
2744 memcpy_s(tee_retrieve_buffer + fragment_offset,
2745 sizeof(tee_retrieve_buffer) - fragment_offset,
2746 from_locked.vm->mailbox.send, fragment_length);
2747
2748 fragment_offset += fragment_length;
2749 }
2750
2751 memory_region = (struct ffa_memory_region *)tee_retrieve_buffer;
Andrew Walbran290b0c92020-02-03 16:37:14 +00002752
2753 if (memory_region->receiver_count != 1) {
2754 /* Only one receiver supported by Hafnium for now. */
2755 dlog_verbose(
2756 "Multiple recipients not supported (got %d, expected "
2757 "1).\n",
2758 memory_region->receiver_count);
Andrew Walbranca808b12020-05-15 17:22:28 +01002759 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002760 }
2761
2762 if (memory_region->handle != handle) {
2763 dlog_verbose(
2764 "Got memory region handle %#x from TEE but requested "
2765 "handle %#x.\n",
2766 memory_region->handle, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002767 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002768 }
2769
2770 /* The original sender must match the caller. */
2771 if (to_locked.vm->id != memory_region->sender) {
2772 dlog_verbose(
2773 "VM %d attempted to reclaim memory handle %#x "
2774 "originally sent by VM %d.\n",
2775 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002776 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002777 }
2778
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01002779 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002780
2781 /*
Andrew Walbranca808b12020-05-15 17:22:28 +01002782 * Validate that the reclaim transition is allowed for the given memory
2783 * region, forward the request to the TEE and then map the memory back
2784 * into the caller's stage-2 page table.
Andrew Walbran290b0c92020-02-03 16:37:14 +00002785 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01002786 return ffa_tee_reclaim_check_update(
2787 to_locked, handle, composite->constituents,
Andrew Walbranca808b12020-05-15 17:22:28 +01002788 composite->constituent_count, memory_to_attributes,
2789 flags & FFA_MEM_RECLAIM_CLEAR, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00002790}