blob: 47f45f5253a0f749cc0886a169e34a0db6ee4a6d [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 Walbranb5ab43c2020-04-30 11:32:54 +010039static_assert(sizeof(struct ffa_memory_region_constituent) % 16 == 0,
40 "struct ffa_memory_region_constituent must be a multiple of 16 "
Andrew Walbranc34c7b22020-02-28 11:16:59 +000041 "bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010042static_assert(sizeof(struct ffa_composite_memory_region) % 16 == 0,
43 "struct ffa_composite_memory_region must be a multiple of 16 "
Andrew Walbranc34c7b22020-02-28 11:16:59 +000044 "bytes long.");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010045static_assert(sizeof(struct ffa_memory_region_attributes) == 4,
46 "struct ffa_memory_region_attributes must be 4bytes long.");
47static_assert(sizeof(struct ffa_memory_access) % 16 == 0,
48 "struct ffa_memory_access must be a multiple of 16 bytes long.");
49static_assert(sizeof(struct ffa_memory_region) % 16 == 0,
50 "struct ffa_memory_region must be a multiple of 16 bytes long.");
51static_assert(sizeof(struct ffa_mem_relinquish) % 16 == 0,
52 "struct ffa_mem_relinquish must be a multiple of 16 "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000053 "bytes long.");
Andrew Walbranc34c7b22020-02-28 11:16:59 +000054
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010055struct ffa_memory_share_state {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000056 /**
57 * The memory region being shared, or NULL if this share state is
58 * unallocated.
59 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010060 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000061
62 /**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010063 * The FF-A function used for sharing the memory. Must be one of
64 * FFA_MEM_DONATE_32, FFA_MEM_LEND_32 or FFA_MEM_SHARE_32 if the
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000065 * share state is allocated, or 0.
66 */
67 uint32_t share_func;
68
69 /**
70 * Whether each recipient has retrieved the memory region yet. The order
71 * of this array matches the order of the attribute descriptors in the
72 * memory region descriptor. Any entries beyond the attribute_count will
73 * always be false.
74 */
75 bool retrieved[MAX_MEM_SHARE_RECIPIENTS];
Andrew Walbran475c1452020-02-07 13:22:22 +000076};
77
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000078/**
79 * Encapsulates the set of share states while the `share_states_lock` is held.
80 */
81struct share_states_locked {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010082 struct ffa_memory_share_state *share_states;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000083};
84
85/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010086 * All access to members of a `struct ffa_memory_share_state` must be guarded
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000087 * by this lock.
88 */
89static struct spinlock share_states_lock_instance = SPINLOCK_INIT;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010090static struct ffa_memory_share_state share_states[MAX_MEM_SHARES];
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000091
92/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010093 * Initialises the next available `struct ffa_memory_share_state` and sets
Andrew Walbran5de9c3d2020-02-10 13:35:29 +000094 * `handle` to its handle. Returns true on succes or false if none are
95 * available.
96 */
97static bool allocate_share_state(uint32_t share_func,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010098 struct ffa_memory_region *memory_region,
99 ffa_memory_handle_t *handle)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000100{
Andrew Walbrana65a1322020-04-06 19:32:32 +0100101 uint64_t i;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000102
103 CHECK(memory_region != NULL);
104
105 sl_lock(&share_states_lock_instance);
106 for (i = 0; i < MAX_MEM_SHARES; ++i) {
107 if (share_states[i].share_func == 0) {
108 uint32_t j;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100109 struct ffa_memory_share_state *allocated_state =
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000110 &share_states[i];
111 allocated_state->share_func = share_func;
112 allocated_state->memory_region = memory_region;
113 for (j = 0; j < MAX_MEM_SHARE_RECIPIENTS; ++j) {
114 allocated_state->retrieved[j] = false;
115 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100116 *handle = i | FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000117 sl_unlock(&share_states_lock_instance);
118 return true;
119 }
120 }
121
122 sl_unlock(&share_states_lock_instance);
123 return false;
124}
125
126/** Locks the share states lock. */
127struct share_states_locked share_states_lock(void)
128{
129 sl_lock(&share_states_lock_instance);
130
131 return (struct share_states_locked){.share_states = share_states};
132}
133
134/** Unlocks the share states lock. */
135static void share_states_unlock(struct share_states_locked *share_states)
136{
137 CHECK(share_states->share_states != NULL);
138 share_states->share_states = NULL;
139 sl_unlock(&share_states_lock_instance);
140}
141
142/**
143 * If the given handle is a valid handle for an allocated share state then takes
144 * the lock, initialises `share_state_locked` to point to the share state and
145 * returns true. Otherwise returns false and doesn't take the lock.
146 */
147static bool get_share_state(struct share_states_locked share_states,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100148 ffa_memory_handle_t handle,
149 struct ffa_memory_share_state **share_state_ret)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000150{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100151 struct ffa_memory_share_state *share_state;
152 uint32_t index = handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000153
154 if (index >= MAX_MEM_SHARES) {
155 return false;
156 }
157
158 share_state = &share_states.share_states[index];
159
160 if (share_state->share_func == 0) {
161 return false;
162 }
163
164 *share_state_ret = share_state;
165 return true;
166}
167
168/** Marks a share state as unallocated. */
169static void share_state_free(struct share_states_locked share_states,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100170 struct ffa_memory_share_state *share_state,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000171 struct mpool *page_pool)
172{
173 CHECK(share_states.share_states != NULL);
174 share_state->share_func = 0;
175 mpool_free(page_pool, share_state->memory_region);
176 share_state->memory_region = NULL;
177}
178
179/**
180 * Marks the share state with the given handle as unallocated, or returns false
181 * if the handle was invalid.
182 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100183static bool share_state_free_handle(ffa_memory_handle_t handle,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000184 struct mpool *page_pool)
185{
186 struct share_states_locked share_states = share_states_lock();
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100187 struct ffa_memory_share_state *share_state;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000188
189 if (!get_share_state(share_states, handle, &share_state)) {
190 share_states_unlock(&share_states);
191 return false;
192 }
193
194 share_state_free(share_states, share_state, page_pool);
195 share_states_unlock(&share_states);
196
197 return true;
198}
199
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100200static void dump_memory_region(struct ffa_memory_region *memory_region)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000201{
202 uint32_t i;
203
204 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
205 return;
206 }
207
Andrew Walbrana65a1322020-04-06 19:32:32 +0100208 dlog("from VM %d, attributes %#x, flags %#x, handle %#x, tag %d, to %d "
209 "recipients [",
210 memory_region->sender, memory_region->attributes,
211 memory_region->flags, memory_region->handle, memory_region->tag,
212 memory_region->receiver_count);
213 for (i = 0; i < memory_region->receiver_count; ++i) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000214 if (i != 0) {
215 dlog(", ");
216 }
Andrew Walbrana65a1322020-04-06 19:32:32 +0100217 dlog("VM %d: %#x (offset %d)",
218 memory_region->receivers[i].receiver_permissions.receiver,
219 memory_region->receivers[i]
220 .receiver_permissions.permissions,
221 memory_region->receivers[i]
222 .composite_memory_region_offset);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000223 }
224 dlog("]");
225}
226
227static void dump_share_states(void)
228{
229 uint32_t i;
230
231 if (LOG_LEVEL < LOG_LEVEL_VERBOSE) {
232 return;
233 }
234
235 dlog("Current share states:\n");
236 sl_lock(&share_states_lock_instance);
237 for (i = 0; i < MAX_MEM_SHARES; ++i) {
238 if (share_states[i].share_func != 0) {
239 dlog("%d: ", i);
240 switch (share_states[i].share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100241 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000242 dlog("SHARE");
243 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100244 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000245 dlog("LEND");
246 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100247 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000248 dlog("DONATE");
249 break;
250 default:
251 dlog("invalid share_func %#x",
252 share_states[i].share_func);
253 }
254 dlog(" (");
255 dump_memory_region(share_states[i].memory_region);
256 if (share_states[i].retrieved[0]) {
257 dlog("): retrieved\n");
258 } else {
259 dlog("): not retrieved\n");
260 }
261 break;
262 }
263 }
264 sl_unlock(&share_states_lock_instance);
265}
266
Andrew Walbran475c1452020-02-07 13:22:22 +0000267/* TODO: Add device attributes: GRE, cacheability, shareability. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100268static inline uint32_t ffa_memory_permissions_to_mode(
269 ffa_memory_access_permissions_t permissions)
Andrew Walbran475c1452020-02-07 13:22:22 +0000270{
271 uint32_t mode = 0;
272
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100273 switch (ffa_get_data_access_attr(permissions)) {
274 case FFA_DATA_ACCESS_RO:
Andrew Walbran475c1452020-02-07 13:22:22 +0000275 mode = MM_MODE_R;
276 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100277 case FFA_DATA_ACCESS_RW:
278 case FFA_DATA_ACCESS_NOT_SPECIFIED:
Andrew Walbran475c1452020-02-07 13:22:22 +0000279 mode = MM_MODE_R | MM_MODE_W;
280 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100281 case FFA_DATA_ACCESS_RESERVED:
282 panic("Tried to convert FFA_DATA_ACCESS_RESERVED.");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100283 }
284
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100285 switch (ffa_get_instruction_access_attr(permissions)) {
286 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbran475c1452020-02-07 13:22:22 +0000287 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100288 case FFA_INSTRUCTION_ACCESS_X:
289 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
Andrew Walbrana65a1322020-04-06 19:32:32 +0100290 mode |= MM_MODE_X;
291 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100292 case FFA_INSTRUCTION_ACCESS_RESERVED:
293 panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED.");
Andrew Walbran475c1452020-02-07 13:22:22 +0000294 }
295
296 return mode;
297}
298
Jose Marinho75509b42019-04-09 09:34:59 +0100299/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000300 * Get the current mode in the stage-2 page table of the given vm of all the
301 * pages in the given constituents, if they all have the same mode, or return
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100302 * an appropriate FF-A error if not.
Jose Marinho75509b42019-04-09 09:34:59 +0100303 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100304static struct ffa_value constituents_get_mode(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000305 struct vm_locked vm, uint32_t *orig_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100306 struct ffa_memory_region_constituent *constituents,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000307 uint32_t constituent_count)
Jose Marinho75509b42019-04-09 09:34:59 +0100308{
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100309 uint32_t i;
Jose Marinho75509b42019-04-09 09:34:59 +0100310
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000311 if (constituent_count == 0) {
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100312 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000313 * Fail if there are no constituents. Otherwise we would get an
314 * uninitialised *orig_mode.
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100315 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100316 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100317 }
318
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000319 for (i = 0; i < constituent_count; ++i) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100320 ipaddr_t begin = ipa_init(constituents[i].address);
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100321 size_t size = constituents[i].page_count * PAGE_SIZE;
322 ipaddr_t end = ipa_add(begin, size);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000323 uint32_t current_mode;
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100324
325 /* Fail if addresses are not page-aligned. */
326 if (!is_aligned(ipa_addr(begin), PAGE_SIZE) ||
327 !is_aligned(ipa_addr(end), PAGE_SIZE)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100328 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100329 }
330
331 /*
332 * Ensure that this constituent memory range is all mapped with
333 * the same mode.
334 */
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000335 if (!mm_vm_get_mode(&vm.vm->ptable, begin, end,
336 &current_mode)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100337 return ffa_error(FFA_DENIED);
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100338 }
339
340 /*
341 * Ensure that all constituents are mapped with the same mode.
342 */
343 if (i == 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000344 *orig_mode = current_mode;
345 } else if (current_mode != *orig_mode) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100346 return ffa_error(FFA_DENIED);
Jose Marinho7fbbf2e2019-08-05 13:19:58 +0100347 }
Jose Marinho75509b42019-04-09 09:34:59 +0100348 }
349
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100350 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000351}
352
353/**
354 * Verify that all pages have the same mode, that the starting mode
355 * constitutes a valid state and obtain the next mode to apply
356 * to the sending VM.
357 *
358 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100359 * 1) FFA_DENIED if a state transition was not found;
360 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100361 * the <from> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100362 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100363 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100364 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
365 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000366 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100367static struct ffa_value ffa_send_check_transition(
Andrew Walbrana65a1322020-04-06 19:32:32 +0100368 struct vm_locked from, uint32_t share_func,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100369 ffa_memory_access_permissions_t permissions, uint32_t *orig_from_mode,
370 struct ffa_memory_region_constituent *constituents,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000371 uint32_t constituent_count, uint32_t *from_mode)
372{
373 const uint32_t state_mask =
374 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
Andrew Walbrana65a1322020-04-06 19:32:32 +0100375 const uint32_t required_from_mode =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100376 ffa_memory_permissions_to_mode(permissions);
377 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000378
Andrew Walbrana65a1322020-04-06 19:32:32 +0100379 ret = constituents_get_mode(from, orig_from_mode, constituents,
380 constituent_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100381 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100382 return ret;
Andrew Scullb5f49e02019-10-02 13:20:47 +0100383 }
384
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000385 /* Ensure the address range is normal memory and not a device. */
386 if (*orig_from_mode & MM_MODE_D) {
387 dlog_verbose("Can't share device memory (mode is %#x).\n",
388 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100389 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000390 }
391
392 /*
393 * Ensure the sender is the owner and has exclusive access to the
394 * memory.
395 */
396 if ((*orig_from_mode & state_mask) != 0) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100397 return ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +0100398 }
399
400 if ((*orig_from_mode & required_from_mode) != required_from_mode) {
401 dlog_verbose(
402 "Sender tried to send memory with permissions which "
403 "required mode %#x but only had %#x itself.\n",
404 required_from_mode, *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100405 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000406 }
407
408 /* Find the appropriate new mode. */
409 *from_mode = ~state_mask & *orig_from_mode;
Andrew Walbrane7ad3c02019-12-24 17:03:04 +0000410 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100411 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000412 *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED;
Jose Marinho75509b42019-04-09 09:34:59 +0100413 break;
414
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100415 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000416 *from_mode |= MM_MODE_INVALID;
Andrew Walbran648fc3e2019-10-22 16:23:05 +0100417 break;
418
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100419 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000420 *from_mode |= MM_MODE_SHARED;
Jose Marinho56c25732019-05-20 09:48:53 +0100421 break;
422
Jose Marinho75509b42019-04-09 09:34:59 +0100423 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100424 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho75509b42019-04-09 09:34:59 +0100425 }
426
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100427 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000428}
429
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100430static struct ffa_value ffa_relinquish_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000431 struct vm_locked from, uint32_t *orig_from_mode,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100432 struct ffa_memory_region_constituent *constituents,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000433 uint32_t constituent_count, uint32_t *from_mode)
434{
435 const uint32_t state_mask =
436 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
437 uint32_t orig_from_state;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100438 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000439
Andrew Walbrana65a1322020-04-06 19:32:32 +0100440 ret = constituents_get_mode(from, orig_from_mode, constituents,
441 constituent_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100442 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100443 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000444 }
445
446 /* Ensure the address range is normal memory and not a device. */
447 if (*orig_from_mode & MM_MODE_D) {
448 dlog_verbose("Can't relinquish device memory (mode is %#x).\n",
449 *orig_from_mode);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100450 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000451 }
452
453 /*
454 * Ensure the relinquishing VM is not the owner but has access to the
455 * memory.
456 */
457 orig_from_state = *orig_from_mode & state_mask;
458 if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) {
459 dlog_verbose(
460 "Tried to relinquish memory in state %#x (masked %#x "
461 "but "
462 "should be %#x).\n",
463 *orig_from_mode, orig_from_state, MM_MODE_UNOWNED);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100464 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000465 }
466
467 /* Find the appropriate new mode. */
468 *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK;
469
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100470 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000471}
472
473/**
474 * Verify that all pages have the same mode, that the starting mode
475 * constitutes a valid state and obtain the next mode to apply
476 * to the retrieving VM.
477 *
478 * Returns:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100479 * 1) FFA_DENIED if a state transition was not found;
480 * 2) FFA_DENIED if the pages being shared do not have the same mode within
Andrew Walbrana65a1322020-04-06 19:32:32 +0100481 * the <to> VM;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100482 * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page
Andrew Walbrana65a1322020-04-06 19:32:32 +0100483 * aligned;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100484 * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled.
485 * Or FFA_SUCCESS on success.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000486 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100487static struct ffa_value ffa_retrieve_check_transition(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000488 struct vm_locked to, uint32_t share_func,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100489 struct ffa_memory_region_constituent *constituents,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000490 uint32_t constituent_count, uint32_t memory_to_attributes,
491 uint32_t *to_mode)
492{
493 uint32_t orig_to_mode;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100494 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000495
Andrew Walbrana65a1322020-04-06 19:32:32 +0100496 ret = constituents_get_mode(to, &orig_to_mode, constituents,
497 constituent_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100498 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100499 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000500 }
501
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100502 if (share_func == FFA_MEM_RECLAIM_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000503 const uint32_t state_mask =
504 MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED;
505 uint32_t orig_to_state = orig_to_mode & state_mask;
506
507 if (orig_to_state != MM_MODE_INVALID &&
508 orig_to_state != MM_MODE_SHARED) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100509 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000510 }
511 } else {
512 /*
513 * Ensure the retriever has the expected state. We don't care
514 * about the MM_MODE_SHARED bit; either with or without it set
515 * are both valid representations of the !O-NA state.
516 */
517 if ((orig_to_mode & MM_MODE_UNMAPPED_MASK) !=
518 MM_MODE_UNMAPPED_MASK) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100519 return ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000520 }
521 }
522
523 /* Find the appropriate new mode. */
524 *to_mode = memory_to_attributes;
525 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100526 case FFA_MEM_DONATE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000527 *to_mode |= 0;
528 break;
529
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100530 case FFA_MEM_LEND_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000531 *to_mode |= MM_MODE_UNOWNED;
532 break;
533
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100534 case FFA_MEM_SHARE_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000535 *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED;
536 break;
537
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100538 case FFA_MEM_RECLAIM_32:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000539 *to_mode |= 0;
540 break;
541
542 default:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100543 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000544 }
545
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100546 return (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho75509b42019-04-09 09:34:59 +0100547}
Jose Marinho09b1db82019-08-08 09:16:59 +0100548
549/**
550 * Updates a VM's page table such that the given set of physical address ranges
551 * are mapped in the address space at the corresponding address ranges, in the
552 * mode provided.
553 *
554 * If commit is false, the page tables will be allocated from the mpool but no
555 * mappings will actually be updated. This function must always be called first
556 * with commit false to check that it will succeed before calling with commit
557 * true, to avoid leaving the page table in a half-updated state. To make a
558 * series of changes atomically you can call them all with commit false before
559 * calling them all with commit true.
560 *
561 * mm_vm_defrag should always be called after a series of page table updates,
562 * whether they succeed or fail.
563 *
564 * Returns true on success, or false if the update failed and no changes were
565 * made to memory mappings.
566 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100567static bool ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000568 struct vm_locked vm_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100569 struct ffa_memory_region_constituent *constituents,
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000570 uint32_t constituent_count, int mode, struct mpool *ppool, bool commit)
Jose Marinho09b1db82019-08-08 09:16:59 +0100571{
Jose Marinho09b1db82019-08-08 09:16:59 +0100572 /* Iterate over the memory region constituents. */
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000573 for (uint32_t index = 0; index < constituent_count; index++) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100574 size_t size = constituents[index].page_count * PAGE_SIZE;
Andrew Walbrana65a1322020-04-06 19:32:32 +0100575 paddr_t pa_begin =
576 pa_from_ipa(ipa_init(constituents[index].address));
Jose Marinho09b1db82019-08-08 09:16:59 +0100577 paddr_t pa_end = pa_add(pa_begin, size);
578
579 if (commit) {
Andrew Scull3c257452019-11-26 13:32:50 +0000580 vm_identity_commit(vm_locked, pa_begin, pa_end, mode,
581 ppool, NULL);
582 } else if (!vm_identity_prepare(vm_locked, pa_begin, pa_end,
583 mode, ppool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100584 return false;
585 }
586 }
587
588 return true;
589}
590
591/**
592 * Clears a region of physical memory by overwriting it with zeros. The data is
593 * flushed from the cache so the memory has been cleared across the system.
594 */
595static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool)
596{
597 /*
Fuad Tabbaed294af2019-12-20 10:43:01 +0000598 * TODO: change this to a CPU local single page window rather than a
Jose Marinho09b1db82019-08-08 09:16:59 +0100599 * global mapping of the whole range. Such an approach will limit
600 * the changes to stage-1 tables and will allow only local
601 * invalidation.
602 */
603 bool ret;
604 struct mm_stage1_locked stage1_locked = mm_lock_stage1();
605 void *ptr =
606 mm_identity_map(stage1_locked, begin, end, MM_MODE_W, ppool);
607 size_t size = pa_difference(begin, end);
608
609 if (!ptr) {
610 /* TODO: partial defrag of failed range. */
611 /* Recover any memory consumed in failed mapping. */
612 mm_defrag(stage1_locked, ppool);
613 goto fail;
614 }
615
616 memset_s(ptr, size, 0, size);
617 arch_mm_flush_dcache(ptr, size);
618 mm_unmap(stage1_locked, begin, end, ppool);
619
620 ret = true;
621 goto out;
622
623fail:
624 ret = false;
625
626out:
627 mm_unlock_stage1(&stage1_locked);
628
629 return ret;
630}
631
632/**
633 * Clears a region of physical memory by overwriting it with zeros. The data is
634 * flushed from the cache so the memory has been cleared across the system.
635 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100636static bool ffa_clear_memory_constituents(
637 struct ffa_memory_region_constituent *constituents,
Andrew Walbran475c1452020-02-07 13:22:22 +0000638 uint32_t constituent_count, struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +0100639{
640 struct mpool local_page_pool;
Jose Marinho09b1db82019-08-08 09:16:59 +0100641 struct mm_stage1_locked stage1_locked;
642 bool ret = false;
643
644 /*
645 * Create a local pool so any freed memory can't be used by another
646 * thread. This is to ensure each constituent that is mapped can be
647 * unmapped again afterwards.
648 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000649 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100650
651 /* Iterate over the memory region constituents. */
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000652 for (uint32_t i = 0; i < constituent_count; ++i) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100653 size_t size = constituents[i].page_count * PAGE_SIZE;
Andrew Walbrana65a1322020-04-06 19:32:32 +0100654 paddr_t begin = pa_from_ipa(ipa_init(constituents[i].address));
Jose Marinho09b1db82019-08-08 09:16:59 +0100655 paddr_t end = pa_add(begin, size);
656
657 if (!clear_memory(begin, end, &local_page_pool)) {
658 /*
659 * api_clear_memory will defrag on failure, so no need
660 * to do it here.
661 */
662 goto out;
663 }
664 }
665
666 /*
667 * Need to defrag after clearing, as it may have added extra mappings to
668 * the stage 1 page table.
669 */
670 stage1_locked = mm_lock_stage1();
671 mm_defrag(stage1_locked, &local_page_pool);
672 mm_unlock_stage1(&stage1_locked);
673
674 ret = true;
675
676out:
677 mpool_fini(&local_page_pool);
678 return ret;
679}
680
681/**
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000682 * Validates and prepares memory to be sent from the calling VM to another.
Jose Marinho09b1db82019-08-08 09:16:59 +0100683 *
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000684 * This function requires the calling context to hold the <from> VM lock.
Jose Marinho09b1db82019-08-08 09:16:59 +0100685 *
686 * Returns:
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000687 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100688 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Jose Marinho09b1db82019-08-08 09:16:59 +0100689 * erroneous;
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100690 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the
691 * request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100692 * 3) FFA_DENIED - The sender doesn't have sufficient access to send the
Andrew Walbrana65a1322020-04-06 19:32:32 +0100693 * memory with the given permissions.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100694 * Success is indicated by FFA_SUCCESS.
Jose Marinho09b1db82019-08-08 09:16:59 +0100695 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100696static struct ffa_value ffa_send_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000697 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100698 struct ffa_memory_region_constituent *constituents,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000699 uint32_t constituent_count, uint32_t share_func,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100700 ffa_memory_access_permissions_t permissions, struct mpool *page_pool,
Andrew Walbrana65a1322020-04-06 19:32:32 +0100701 bool clear)
Jose Marinho09b1db82019-08-08 09:16:59 +0100702{
Jose Marinho09b1db82019-08-08 09:16:59 +0100703 struct vm *from = from_locked.vm;
704 uint32_t orig_from_mode;
705 uint32_t from_mode;
Jose Marinho09b1db82019-08-08 09:16:59 +0100706 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100707 struct ffa_value ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100708
709 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +0100710 * Make sure constituents are properly aligned to a 64-bit boundary. If
711 * not we would get alignment faults trying to read (64-bit) values.
Jose Marinho09b1db82019-08-08 09:16:59 +0100712 */
Andrew Walbrana65a1322020-04-06 19:32:32 +0100713 if (!is_aligned(constituents, 8)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100714 return ffa_error(FFA_INVALID_PARAMETERS);
Jose Marinho09b1db82019-08-08 09:16:59 +0100715 }
716
717 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000718 * Check if the state transition is lawful for the sender, ensure that
719 * all constituents of a memory region being shared are at the same
720 * state.
Jose Marinho09b1db82019-08-08 09:16:59 +0100721 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100722 ret = ffa_send_check_transition(from_locked, share_func, permissions,
723 &orig_from_mode, constituents,
724 constituent_count, &from_mode);
725 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +0100726 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +0100727 }
728
729 /*
730 * Create a local pool so any freed memory can't be used by another
731 * thread. This is to ensure the original mapping can be restored if the
732 * clear fails.
733 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000734 mpool_init_with_fallback(&local_page_pool, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +0100735
736 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000737 * First reserve all required memory for the new page table entries
738 * without committing, to make sure the entire operation will succeed
739 * without exhausting the page pool.
Jose Marinho09b1db82019-08-08 09:16:59 +0100740 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100741 if (!ffa_region_group_identity_map(from_locked, constituents,
742 constituent_count, from_mode,
743 page_pool, false)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100744 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100745 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100746 goto out;
747 }
748
749 /*
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000750 * Update the mapping for the sender. This won't allocate because the
751 * transaction was already prepared above, but may free pages in the
752 * case that a whole block is being unmapped that was previously
753 * partially mapped.
Jose Marinho09b1db82019-08-08 09:16:59 +0100754 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100755 CHECK(ffa_region_group_identity_map(from_locked, constituents,
756 constituent_count, from_mode,
757 &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100758
759 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100760 if (clear && !ffa_clear_memory_constituents(
Andrew Walbran475c1452020-02-07 13:22:22 +0000761 constituents, constituent_count, page_pool)) {
Jose Marinho09b1db82019-08-08 09:16:59 +0100762 /*
763 * On failure, roll back by returning memory to the sender. This
764 * may allocate pages which were previously freed into
765 * `local_page_pool` by the call above, but will never allocate
766 * more pages than that so can never fail.
767 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100768 CHECK(ffa_region_group_identity_map(
Andrew Walbranf4b51af2020-02-03 14:44:54 +0000769 from_locked, constituents, constituent_count,
770 orig_from_mode, &local_page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100771
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100772 ret = ffa_error(FFA_NO_MEMORY);
Jose Marinho09b1db82019-08-08 09:16:59 +0100773 goto out;
774 }
775
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100776 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000777
778out:
779 mpool_fini(&local_page_pool);
780
781 /*
782 * Tidy up the page table by reclaiming failed mappings (if there was an
783 * error) or merging entries into blocks where possible (on success).
784 */
785 mm_vm_defrag(&from->ptable, page_pool);
786
787 return ret;
788}
789
790/**
791 * Validates and maps memory shared from one VM to another.
792 *
793 * This function requires the calling context to hold the <to> lock.
794 *
795 * Returns:
796 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100797 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000798 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100799 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000800 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100801 * Success is indicated by FFA_SUCCESS.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000802 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100803static struct ffa_value ffa_retrieve_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000804 struct vm_locked to_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100805 struct ffa_memory_region_constituent *constituents,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000806 uint32_t constituent_count, uint32_t memory_to_attributes,
807 uint32_t share_func, bool clear, struct mpool *page_pool)
808{
809 struct vm *to = to_locked.vm;
810 uint32_t to_mode;
811 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100812 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000813
814 /*
815 * Make sure constituents are properly aligned to a 32-bit boundary. If
816 * not we would get alignment faults trying to read (32-bit) values.
817 */
818 if (!is_aligned(constituents, 4)) {
819 dlog_verbose("Constituents not aligned.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100820 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000821 }
822
823 /*
824 * Check if the state transition is lawful for the recipient, and ensure
825 * that all constituents of the memory region being retrieved are at the
826 * same state.
827 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100828 ret = ffa_retrieve_check_transition(to_locked, share_func, constituents,
829 constituent_count,
830 memory_to_attributes, &to_mode);
831 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000832 dlog_verbose("Invalid transition.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +0100833 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000834 }
835
836 /*
837 * Create a local pool so any freed memory can't be used by another
838 * thread. This is to ensure the original mapping can be restored if the
839 * clear fails.
840 */
841 mpool_init_with_fallback(&local_page_pool, page_pool);
842
843 /*
844 * First reserve all required memory for the new page table entries in
845 * the recipient page tables without committing, to make sure the entire
846 * operation will succeed without exhausting the page pool.
847 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100848 if (!ffa_region_group_identity_map(to_locked, constituents,
849 constituent_count, to_mode,
850 page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000851 /* TODO: partial defrag of failed range. */
852 dlog_verbose(
853 "Insufficient memory to update recipient page "
854 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100855 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000856 goto out;
857 }
858
859 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100860 if (clear && !ffa_clear_memory_constituents(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000861 constituents, constituent_count, page_pool)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100862 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000863 goto out;
864 }
865
Jose Marinho09b1db82019-08-08 09:16:59 +0100866 /*
867 * Complete the transfer by mapping the memory into the recipient. This
868 * won't allocate because the transaction was already prepared above, so
869 * it doesn't need to use the `local_page_pool`.
870 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100871 CHECK(ffa_region_group_identity_map(to_locked, constituents,
872 constituent_count, to_mode,
873 page_pool, true));
Jose Marinho09b1db82019-08-08 09:16:59 +0100874
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100875 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Jose Marinho09b1db82019-08-08 09:16:59 +0100876
877out:
878 mpool_fini(&local_page_pool);
879
880 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100881 * Tidy up the page table by reclaiming failed mappings (if there was an
882 * error) or merging entries into blocks where possible (on success).
Jose Marinho09b1db82019-08-08 09:16:59 +0100883 */
Andrew Walbran475c1452020-02-07 13:22:22 +0000884 mm_vm_defrag(&to->ptable, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +0000885
886 return ret;
887}
888
Andrew Walbran290b0c92020-02-03 16:37:14 +0000889/**
890 * Reclaims the given memory from the TEE. To do this space is first reserved in
891 * the <to> VM's page table, then the reclaim request is sent on to the TEE,
892 * then (if that is successful) the memory is mapped back into the <to> VM's
893 * page table.
894 *
895 * This function requires the calling context to hold the <to> lock.
896 *
897 * Returns:
898 * In case of error, one of the following values is returned:
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100899 * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were
Andrew Walbran290b0c92020-02-03 16:37:14 +0000900 * erroneous;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100901 * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete
Andrew Walbran290b0c92020-02-03 16:37:14 +0000902 * the request.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100903 * Success is indicated by FFA_SUCCESS.
Andrew Walbran290b0c92020-02-03 16:37:14 +0000904 */
Andrew Walbran996d1d12020-05-27 14:08:43 +0100905static struct ffa_value ffa_tee_reclaim_check_update(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100906 struct vm_locked to_locked, ffa_memory_handle_t handle,
907 struct ffa_memory_region_constituent *constituents,
Andrew Walbran290b0c92020-02-03 16:37:14 +0000908 uint32_t constituent_count, uint32_t memory_to_attributes, bool clear,
909 struct mpool *page_pool)
910{
911 struct vm *to = to_locked.vm;
912 uint32_t to_mode;
913 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100914 struct ffa_value ret;
915 ffa_memory_region_flags_t tee_flags;
Andrew Walbran290b0c92020-02-03 16:37:14 +0000916
917 /*
918 * Make sure constituents are properly aligned to a 32-bit boundary. If
919 * not we would get alignment faults trying to read (32-bit) values.
920 */
921 if (!is_aligned(constituents, 4)) {
922 dlog_verbose("Constituents not aligned.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100923 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +0000924 }
925
926 /*
927 * Check if the state transition is lawful for the recipient, and ensure
928 * that all constituents of the memory region being retrieved are at the
929 * same state.
930 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100931 ret = ffa_retrieve_check_transition(to_locked, FFA_MEM_RECLAIM_32,
932 constituents, constituent_count,
933 memory_to_attributes, &to_mode);
934 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +0000935 dlog_verbose("Invalid transition.\n");
936 return ret;
937 }
938
939 /*
940 * Create a local pool so any freed memory can't be used by another
941 * thread. This is to ensure the original mapping can be restored if the
942 * clear fails.
943 */
944 mpool_init_with_fallback(&local_page_pool, page_pool);
945
946 /*
947 * First reserve all required memory for the new page table entries in
948 * the recipient page tables without committing, to make sure the entire
949 * operation will succeed without exhausting the page pool.
950 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100951 if (!ffa_region_group_identity_map(to_locked, constituents,
952 constituent_count, to_mode,
953 page_pool, false)) {
Andrew Walbran290b0c92020-02-03 16:37:14 +0000954 /* TODO: partial defrag of failed range. */
955 dlog_verbose(
956 "Insufficient memory to update recipient page "
957 "table.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100958 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran290b0c92020-02-03 16:37:14 +0000959 goto out;
960 }
961
962 /*
963 * Forward the request to the TEE and see what happens.
964 */
965 tee_flags = 0;
966 if (clear) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100967 tee_flags |= FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran290b0c92020-02-03 16:37:14 +0000968 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100969 ret = arch_tee_call((struct ffa_value){.func = FFA_MEM_RECLAIM_32,
970 .arg1 = (uint32_t)handle,
971 .arg2 = (uint32_t)(handle >> 32),
972 .arg3 = tee_flags});
Andrew Walbran290b0c92020-02-03 16:37:14 +0000973
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100974 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran290b0c92020-02-03 16:37:14 +0000975 dlog_verbose(
Andrew Walbran1a86aa92020-05-15 17:22:28 +0100976 "Got %#x (%d) from TEE in response to "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100977 "FFA_MEM_RECLAIM_32, expected FFA_SUCCESS_32.\n",
Andrew Walbran290b0c92020-02-03 16:37:14 +0000978 ret.func, ret.arg2);
979 goto out;
980 }
981
982 /*
983 * The TEE was happy with it, so complete the reclaim by mapping the
984 * memory into the recipient. This won't allocate because the
985 * transaction was already prepared above, so it doesn't need to use the
986 * `local_page_pool`.
987 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100988 CHECK(ffa_region_group_identity_map(to_locked, constituents,
989 constituent_count, to_mode,
990 page_pool, true));
Andrew Walbran290b0c92020-02-03 16:37:14 +0000991
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100992 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran290b0c92020-02-03 16:37:14 +0000993
994out:
995 mpool_fini(&local_page_pool);
996
997 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +0100998 * Tidy up the page table by reclaiming failed mappings (if there was an
999 * error) or merging entries into blocks where possible (on success).
Andrew Walbran290b0c92020-02-03 16:37:14 +00001000 */
1001 mm_vm_defrag(&to->ptable, page_pool);
1002
1003 return ret;
1004}
1005
Andrew Walbran996d1d12020-05-27 14:08:43 +01001006static struct ffa_value ffa_relinquish_check_update(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001007 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001008 struct ffa_memory_region_constituent *constituents,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001009 uint32_t constituent_count, struct mpool *page_pool, bool clear)
1010{
1011 uint32_t orig_from_mode;
1012 uint32_t from_mode;
1013 struct mpool local_page_pool;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001014 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001015
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001016 ret = ffa_relinquish_check_transition(from_locked, &orig_from_mode,
1017 constituents, constituent_count,
1018 &from_mode);
1019 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001020 dlog_verbose("Invalid transition.\n");
Andrew Walbrana65a1322020-04-06 19:32:32 +01001021 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001022 }
1023
1024 /*
1025 * Create a local pool so any freed memory can't be used by another
1026 * thread. This is to ensure the original mapping can be restored if the
1027 * clear fails.
1028 */
1029 mpool_init_with_fallback(&local_page_pool, page_pool);
1030
1031 /*
1032 * First reserve all required memory for the new page table entries
1033 * without committing, to make sure the entire operation will succeed
1034 * without exhausting the page pool.
1035 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001036 if (!ffa_region_group_identity_map(from_locked, constituents,
1037 constituent_count, from_mode,
1038 page_pool, false)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001039 /* TODO: partial defrag of failed range. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001040 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001041 goto out;
1042 }
1043
1044 /*
1045 * Update the mapping for the sender. This won't allocate because the
1046 * transaction was already prepared above, but may free pages in the
1047 * case that a whole block is being unmapped that was previously
1048 * partially mapped.
1049 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001050 CHECK(ffa_region_group_identity_map(from_locked, constituents,
1051 constituent_count, from_mode,
1052 &local_page_pool, true));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001053
1054 /* Clear the memory so no VM or device can see the previous contents. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001055 if (clear && !ffa_clear_memory_constituents(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001056 constituents, constituent_count, page_pool)) {
1057 /*
1058 * On failure, roll back by returning memory to the sender. This
1059 * may allocate pages which were previously freed into
1060 * `local_page_pool` by the call above, but will never allocate
1061 * more pages than that so can never fail.
1062 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001063 CHECK(ffa_region_group_identity_map(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001064 from_locked, constituents, constituent_count,
1065 orig_from_mode, &local_page_pool, true));
1066
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001067 ret = ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001068 goto out;
1069 }
1070
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001071 ret = (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001072
1073out:
1074 mpool_fini(&local_page_pool);
1075
1076 /*
1077 * Tidy up the page table by reclaiming failed mappings (if there was an
1078 * error) or merging entries into blocks where possible (on success).
1079 */
1080 mm_vm_defrag(&from_locked.vm->ptable, page_pool);
Jose Marinho09b1db82019-08-08 09:16:59 +01001081
1082 return ret;
1083}
1084
1085/**
Andrew Walbrana65a1322020-04-06 19:32:32 +01001086 * Check that the given `memory_region` represents a valid memory send request
1087 * of the given `share_func` type, return the clear flag and permissions via the
1088 * respective output parameters, and update the permissions if necessary.
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001089 *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001090 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001091 * not.
1092 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001093static struct ffa_value ffa_memory_send_validate(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001094 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
1095 uint32_t memory_share_length, uint32_t fragment_length,
1096 uint32_t share_func, ffa_memory_access_permissions_t *permissions)
Andrew Walbrana65a1322020-04-06 19:32:32 +01001097{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001098 struct ffa_composite_memory_region *composite;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001099 uint32_t receivers_length;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001100 uint32_t constituents_offset;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001101 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001102 enum ffa_data_access data_access;
1103 enum ffa_instruction_access instruction_access;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001104
Andrew Walbrana65a1322020-04-06 19:32:32 +01001105 CHECK(permissions != NULL);
1106
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001107 /*
1108 * This should already be checked by the caller, just making the
1109 * assumption clear here.
1110 */
1111 CHECK(memory_region->receiver_count == 1);
1112
Andrew Walbrana65a1322020-04-06 19:32:32 +01001113 /* The sender must match the message sender. */
1114 if (memory_region->sender != from_locked.vm->id) {
1115 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001116 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001117 }
1118
Andrew Walbrana65a1322020-04-06 19:32:32 +01001119 /*
1120 * Ensure that the composite header is within the memory bounds and
1121 * doesn't overlap the first part of the message.
1122 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001123 receivers_length = sizeof(struct ffa_memory_access) *
1124 memory_region->receiver_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001125 constituents_offset =
1126 ffa_composite_constituent_offset(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001127 if (memory_region->receivers[0].composite_memory_region_offset <
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001128 sizeof(struct ffa_memory_region) + receivers_length ||
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001129 constituents_offset > fragment_length) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001130 dlog_verbose(
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001131 "Invalid composite memory region descriptor offset "
1132 "%d.\n",
1133 memory_region->receivers[0]
1134 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001135 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001136 }
1137
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001138 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001139
1140 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001141 * Ensure the number of constituents are within the memory bounds.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001142 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001143 constituents_length = sizeof(struct ffa_memory_region_constituent) *
1144 composite->constituent_count;
Andrew Walbran352aa3d2020-05-01 17:51:33 +01001145 if (memory_share_length != constituents_offset + constituents_length) {
1146 dlog_verbose("Invalid length %d or composite offset %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001147 memory_share_length,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001148 memory_region->receivers[0]
1149 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001150 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001151 }
1152
Andrew Walbrana65a1322020-04-06 19:32:32 +01001153 /*
1154 * Clear is not allowed for memory sharing, as the sender still has
1155 * access to the memory.
1156 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001157 if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) &&
1158 share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001159 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001160 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001161 }
1162
1163 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001164 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001165 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001166 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001167 }
1168
1169 /* Check that the permissions are valid. */
1170 *permissions =
1171 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001172 data_access = ffa_get_data_access_attr(*permissions);
1173 instruction_access = ffa_get_instruction_access_attr(*permissions);
1174 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1175 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001176 dlog_verbose("Reserved value for receiver permissions %#x.\n",
1177 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001178 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001179 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001180 if (instruction_access != FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001181 dlog_verbose(
1182 "Invalid instruction access permissions %#x for "
1183 "sending memory.\n",
1184 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001185 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001186 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001187 if (share_func == FFA_MEM_SHARE_32) {
1188 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001189 dlog_verbose(
1190 "Invalid data access permissions %#x for "
1191 "sharing memory.\n",
1192 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001193 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001194 }
1195 /*
1196 * According to section 6.11.3 of the FF-A spec NX is required
1197 * for share operations (but must not be specified by the
1198 * sender) so set it in the copy that we store, ready to be
1199 * returned to the retriever.
1200 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001201 ffa_set_instruction_access_attr(permissions,
1202 FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001203 memory_region->receivers[0].receiver_permissions.permissions =
1204 *permissions;
1205 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001206 if (share_func == FFA_MEM_LEND_32 &&
1207 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001208 dlog_verbose(
1209 "Invalid data access permissions %#x for lending "
1210 "memory.\n",
1211 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001212 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001213 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001214 if (share_func == FFA_MEM_DONATE_32 &&
1215 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001216 dlog_verbose(
1217 "Invalid data access permissions %#x for donating "
1218 "memory.\n",
1219 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001220 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001221 }
1222
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001223 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001224}
1225
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001226/** Forwards a memory send message on to the TEE. */
1227static struct ffa_value memory_send_tee_forward(
1228 struct vm_locked tee_locked, ffa_vm_id_t sender_vm_id,
1229 uint32_t share_func, struct ffa_memory_region *memory_region,
1230 uint32_t memory_share_length, uint32_t fragment_length)
1231{
1232 struct ffa_value ret;
1233
1234 memcpy_s(tee_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX,
1235 memory_region, fragment_length);
1236 tee_locked.vm->mailbox.recv_size = fragment_length;
1237 tee_locked.vm->mailbox.recv_sender = sender_vm_id;
1238 tee_locked.vm->mailbox.recv_func = share_func;
1239 tee_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED;
1240 ret = arch_tee_call((struct ffa_value){.func = share_func,
1241 .arg1 = memory_share_length,
1242 .arg2 = fragment_length});
1243 /*
1244 * After the call to the TEE completes it must have finished reading its
1245 * RX buffer, so it is ready for another message.
1246 */
1247 tee_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY;
1248
1249 return ret;
1250}
1251
Andrew Walbrana65a1322020-04-06 19:32:32 +01001252/**
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001253 * Validates a call to donate, lend or share memory to a non-TEE VM and then
1254 * updates the stage-2 page tables. Specifically, check if the message length
1255 * and number of memory region constituents match, and if the transition is
1256 * valid for the type of memory sending operation.
Andrew Walbran475c1452020-02-07 13:22:22 +00001257 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001258 * Assumes that the caller has already found and locked the sender VM and copied
1259 * the memory region descriptor from the sender's TX buffer to a freshly
1260 * allocated page from Hafnium's internal pool. The caller must have also
1261 * validated that the receiver VM ID is valid.
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001262 *
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001263 * This function takes ownership of the `memory_region` passed in and will free
1264 * it when necessary; it must not be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001265 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001266struct ffa_value ffa_memory_send(struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001267 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001268 uint32_t memory_share_length,
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001269 uint32_t fragment_length, uint32_t share_func,
1270 struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001271{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001272 ffa_memory_access_permissions_t permissions;
1273 struct ffa_value ret;
1274 ffa_memory_handle_t handle;
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001275 struct ffa_composite_memory_region *composite;
Jose Marinho09b1db82019-08-08 09:16:59 +01001276
1277 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001278 * If there is an error validating the `memory_region` then we need to
1279 * free it because we own it but we won't be storing it in a share state
1280 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001281 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001282 ret = ffa_memory_send_validate(from_locked, memory_region,
1283 memory_share_length, fragment_length,
1284 share_func, &permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001285 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001286 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001287 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001288 }
1289
Andrew Walbrana65a1322020-04-06 19:32:32 +01001290 /* Set flag for share function, ready to be retrieved later. */
1291 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001292 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001293 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001294 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001295 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001296 case FFA_MEM_LEND_32:
1297 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001298 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001299 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001300 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001301 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001302 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001303 }
1304
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001305 /*
1306 * Allocate a share state before updating the page table. Otherwise if
1307 * updating the page table succeeded but allocating the share state
1308 * failed then it would leave the memory in a state where nobody could
1309 * get it back.
1310 */
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001311 if (!allocate_share_state(share_func, memory_region, &handle)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001312 dlog_verbose("Failed to allocate share state.\n");
1313 mpool_free(page_pool, memory_region);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001314 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001315 }
1316
1317 dump_share_states();
1318
1319 /* Check that state is valid in sender page table and update. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001320 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbran996d1d12020-05-27 14:08:43 +01001321 ret = ffa_send_check_update(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001322 from_locked, composite->constituents,
1323 composite->constituent_count, share_func, permissions,
1324 page_pool, memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001325 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001326 /* Free share state. */
1327 CHECK(share_state_free_handle(handle, page_pool));
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001328 return ret;
1329 }
1330
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001331 return ffa_mem_success(handle);
1332}
1333
1334/**
1335 * Validates a call to donate, lend or share memory to the TEE and then updates
1336 * the stage-2 page tables. Specifically, check if the message length and number
1337 * of memory region constituents match, and if the transition is valid for the
1338 * type of memory sending operation.
1339 *
1340 * Assumes that the caller has already found and locked the sender VM and the
1341 * TEE VM, and copied the memory region descriptor from the sender's TX buffer
1342 * to a freshly allocated page from Hafnium's internal pool. The caller must
1343 * have also validated that the receiver VM ID is valid.
1344 *
1345 * This function takes ownership of the `memory_region` passed in and will free
1346 * it when necessary; it must not be freed by the caller.
1347 */
1348struct ffa_value ffa_memory_tee_send(
1349 struct vm_locked from_locked, struct vm_locked to_locked,
1350 struct ffa_memory_region *memory_region, uint32_t memory_share_length,
1351 uint32_t fragment_length, uint32_t share_func, struct mpool *page_pool)
1352{
1353 ffa_memory_access_permissions_t permissions;
1354 struct ffa_value ret;
1355 struct ffa_composite_memory_region *composite;
1356
1357 /*
1358 * If there is an error validating the `memory_region` then we need to
1359 * free it because we own it but we won't be storing it in a share state
1360 * after all.
1361 */
1362 ret = ffa_memory_send_validate(from_locked, memory_region,
1363 memory_share_length, fragment_length,
1364 share_func, &permissions);
1365 if (ret.func != FFA_SUCCESS_32) {
1366 goto out;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001367 }
1368
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001369 /* Check that state is valid in sender page table and update. */
1370 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbran996d1d12020-05-27 14:08:43 +01001371 ret = ffa_send_check_update(
Andrew Walbran1a86aa92020-05-15 17:22:28 +01001372 from_locked, composite->constituents,
1373 composite->constituent_count, share_func, permissions,
1374 page_pool, memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR);
1375 if (ret.func != FFA_SUCCESS_32) {
1376 goto out;
1377 }
1378
1379 /* Forward memory send message on to TEE. */
1380 ret = memory_send_tee_forward(to_locked, from_locked.vm->id, share_func,
1381 memory_region, memory_share_length,
1382 fragment_length);
1383
1384out:
1385 mpool_free(page_pool, memory_region);
1386 return ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001387}
1388
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001389struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
1390 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001391 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001392 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001393{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001394 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001395 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01001396 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001397 sizeof(struct ffa_memory_access);
1398 ffa_memory_handle_t handle = retrieve_request->handle;
1399 ffa_memory_region_flags_t transaction_type =
Andrew Walbrana65a1322020-04-06 19:32:32 +01001400 retrieve_request->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001401 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
1402 struct ffa_memory_region *memory_region;
1403 ffa_memory_access_permissions_t sent_permissions;
1404 enum ffa_data_access sent_data_access;
1405 enum ffa_instruction_access sent_instruction_access;
1406 ffa_memory_access_permissions_t requested_permissions;
1407 enum ffa_data_access requested_data_access;
1408 enum ffa_instruction_access requested_instruction_access;
1409 ffa_memory_access_permissions_t permissions;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001410 uint32_t memory_to_attributes;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001411 struct ffa_composite_memory_region *composite;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001412 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001413 struct ffa_memory_share_state *share_state;
1414 struct ffa_value ret;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001415 uint32_t response_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001416
1417 dump_share_states();
1418
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001419 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001420 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001421 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001422 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001423 expected_retrieve_request_length,
1424 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001425 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001426 }
1427
Andrew Walbrana65a1322020-04-06 19:32:32 +01001428 if (retrieve_request->receiver_count != 1) {
1429 dlog_verbose(
1430 "Multi-way memory sharing not supported (got %d "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001431 "receivers descriptors on FFA_MEM_RETRIEVE_REQ, "
Andrew Walbrana65a1322020-04-06 19:32:32 +01001432 "expected 1).\n",
1433 retrieve_request->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001434 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001435 }
1436
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001437 share_states = share_states_lock();
1438 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001439 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001440 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001441 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001442 goto out;
1443 }
1444
Andrew Walbrana65a1322020-04-06 19:32:32 +01001445 memory_region = share_state->memory_region;
1446 CHECK(memory_region != NULL);
1447
1448 /*
1449 * Check that the transaction type expected by the receiver is correct,
1450 * if it has been specified.
1451 */
1452 if (transaction_type !=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001453 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
Andrew Walbrana65a1322020-04-06 19:32:32 +01001454 transaction_type != (memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001455 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001456 dlog_verbose(
1457 "Incorrect transaction type %#x for "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001458 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01001459 transaction_type,
1460 memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001461 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001462 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001463 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001464 goto out;
1465 }
1466
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001467 if (retrieve_request->sender != memory_region->sender) {
1468 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001469 "Incorrect sender ID %d for FFA_MEM_RETRIEVE_REQ, "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001470 "expected %d for handle %#x.\n",
1471 retrieve_request->sender, memory_region->sender,
1472 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001473 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001474 goto out;
1475 }
1476
1477 if (retrieve_request->tag != memory_region->tag) {
1478 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001479 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001480 "%d for handle %#x.\n",
1481 retrieve_request->tag, memory_region->tag, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001482 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001483 goto out;
1484 }
1485
Andrew Walbrana65a1322020-04-06 19:32:32 +01001486 if (retrieve_request->receivers[0].receiver_permissions.receiver !=
1487 to_locked.vm->id) {
1488 dlog_verbose(
1489 "Retrieve request receiver VM ID %d didn't match "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001490 "caller of FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01001491 retrieve_request->receivers[0]
1492 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001493 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001494 goto out;
1495 }
1496
1497 if (memory_region->receivers[0].receiver_permissions.receiver !=
1498 to_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001499 dlog_verbose(
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001500 "Incorrect receiver VM ID %d for FFA_MEM_RETRIEVE_REQ, "
1501 "expected %d for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01001502 to_locked.vm->id,
1503 memory_region->receivers[0]
1504 .receiver_permissions.receiver,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001505 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001506 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001507 goto out;
1508 }
1509
1510 if (share_state->retrieved[0]) {
1511 dlog_verbose("Memory with handle %#x already retrieved.\n",
1512 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001513 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001514 goto out;
1515 }
1516
Andrew Walbrana65a1322020-04-06 19:32:32 +01001517 if (retrieve_request->receivers[0].composite_memory_region_offset !=
1518 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001519 dlog_verbose(
1520 "Retriever specified address ranges not supported (got "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001521 "offset %d).\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01001522 retrieve_request->receivers[0]
1523 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001524 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001525 goto out;
1526 }
1527
Andrew Walbrana65a1322020-04-06 19:32:32 +01001528 /*
1529 * Check permissions from sender against permissions requested by
1530 * receiver.
1531 */
1532 /* TODO: Check attributes too. */
1533 sent_permissions =
1534 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001535 sent_data_access = ffa_get_data_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001536 sent_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001537 ffa_get_instruction_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001538 requested_permissions =
1539 retrieve_request->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001540 requested_data_access = ffa_get_data_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001541 requested_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001542 ffa_get_instruction_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001543 permissions = 0;
1544 switch (sent_data_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001545 case FFA_DATA_ACCESS_NOT_SPECIFIED:
1546 case FFA_DATA_ACCESS_RW:
1547 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
1548 requested_data_access == FFA_DATA_ACCESS_RW) {
1549 ffa_set_data_access_attr(&permissions,
1550 FFA_DATA_ACCESS_RW);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001551 break;
1552 }
1553 /* Intentional fall-through. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001554 case FFA_DATA_ACCESS_RO:
1555 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
1556 requested_data_access == FFA_DATA_ACCESS_RO) {
1557 ffa_set_data_access_attr(&permissions,
1558 FFA_DATA_ACCESS_RO);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001559 break;
1560 }
1561 dlog_verbose(
1562 "Invalid data access requested; sender specified "
1563 "permissions %#x but receiver requested %#x.\n",
1564 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001565 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001566 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001567 case FFA_DATA_ACCESS_RESERVED:
1568 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
Andrew Walbrana65a1322020-04-06 19:32:32 +01001569 "checked before this point.");
1570 }
1571 switch (sent_instruction_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001572 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
1573 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001574 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001575 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
1576 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
1577 ffa_set_instruction_access_attr(
1578 &permissions, FFA_INSTRUCTION_ACCESS_X);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001579 break;
1580 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001581 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001582 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001583 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
1584 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
1585 ffa_set_instruction_access_attr(
1586 &permissions, FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001587 break;
1588 }
1589 dlog_verbose(
1590 "Invalid instruction access requested; sender "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001591 "specified permissions %#x but receiver requested "
1592 "%#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01001593 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001594 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001595 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001596 case FFA_INSTRUCTION_ACCESS_RESERVED:
1597 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
Andrew Walbrana65a1322020-04-06 19:32:32 +01001598 "be checked before this point.");
1599 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001600 memory_to_attributes = ffa_memory_permissions_to_mode(permissions);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001601
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001602 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbran996d1d12020-05-27 14:08:43 +01001603 ret = ffa_retrieve_check_update(
1604 to_locked, composite->constituents,
1605 composite->constituent_count, memory_to_attributes,
1606 share_state->share_func, false, page_pool);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001607 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001608 goto out;
1609 }
1610
1611 /*
1612 * Copy response to RX buffer of caller and deliver the message. This
1613 * must be done before the share_state is (possibly) freed.
1614 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01001615 /* TODO: combine attributes from sender and request. */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001616 response_length = ffa_retrieved_memory_region_init(
Andrew Walbrana65a1322020-04-06 19:32:32 +01001617 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
1618 memory_region->sender, memory_region->attributes,
1619 memory_region->flags, handle, to_locked.vm->id, permissions,
1620 composite->constituents, composite->constituent_count);
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001621 to_locked.vm->mailbox.recv_size = response_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001622 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001623 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001624 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
1625
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001626 if (share_state->share_func == FFA_MEM_DONATE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001627 /*
1628 * Memory that has been donated can't be relinquished, so no
1629 * need to keep the share state around.
1630 */
1631 share_state_free(share_states, share_state, page_pool);
1632 dlog_verbose("Freed share state for donate.\n");
1633 } else {
1634 share_state->retrieved[0] = true;
1635 }
1636
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001637 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001638 .arg1 = response_length,
1639 .arg2 = response_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001640
1641out:
1642 share_states_unlock(&share_states);
1643 dump_share_states();
1644 return ret;
1645}
1646
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001647struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001648 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001649 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001650{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001651 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001652 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001653 struct ffa_memory_share_state *share_state;
1654 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001655 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001656 struct ffa_composite_memory_region *composite;
1657 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001658
Andrew Walbrana65a1322020-04-06 19:32:32 +01001659 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001660 dlog_verbose(
Andrew Walbrana65a1322020-04-06 19:32:32 +01001661 "Stream endpoints not supported (got %d endpoints on "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001662 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001663 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001664 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001665 }
1666
Andrew Walbrana65a1322020-04-06 19:32:32 +01001667 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001668 dlog_verbose(
1669 "VM ID %d in relinquish message doesn't match calling "
1670 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01001671 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001672 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001673 }
1674
1675 dump_share_states();
1676
1677 share_states = share_states_lock();
1678 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001679 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001680 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001681 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001682 goto out;
1683 }
1684
1685 memory_region = share_state->memory_region;
1686 CHECK(memory_region != NULL);
1687
Andrew Walbrana65a1322020-04-06 19:32:32 +01001688 if (memory_region->receivers[0].receiver_permissions.receiver !=
1689 from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001690 dlog_verbose(
1691 "VM ID %d tried to relinquish memory region with "
1692 "handle %#x but receiver was %d.\n",
1693 from_locked.vm->id, handle,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001694 memory_region->receivers[0]
1695 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001696 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001697 goto out;
1698 }
1699
1700 if (!share_state->retrieved[0]) {
1701 dlog_verbose(
1702 "Memory with handle %#x not yet retrieved, can't "
1703 "relinquish.\n",
1704 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001705 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001706 goto out;
1707 }
1708
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001709 clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001710
1711 /*
1712 * Clear is not allowed for memory that was shared, as the original
1713 * sender still has access to the memory.
1714 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001715 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001716 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001717 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001718 goto out;
1719 }
1720
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001721 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbran996d1d12020-05-27 14:08:43 +01001722 ret = ffa_relinquish_check_update(from_locked, composite->constituents,
1723 composite->constituent_count,
1724 page_pool, clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001725
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001726 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001727 /*
1728 * Mark memory handle as not retrieved, so it can be reclaimed
1729 * (or retrieved again).
1730 */
1731 share_state->retrieved[0] = false;
1732 }
1733
1734out:
1735 share_states_unlock(&share_states);
1736 dump_share_states();
1737 return ret;
1738}
1739
1740/**
1741 * Validates that the reclaim transition is allowed for the given handle,
1742 * updates the page table of the reclaiming VM, and frees the internal state
1743 * associated with the handle.
1744 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001745struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
1746 ffa_memory_handle_t handle, bool clear,
1747 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001748{
1749 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001750 struct ffa_memory_share_state *share_state;
1751 struct ffa_memory_region *memory_region;
1752 struct ffa_composite_memory_region *composite;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001753 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001754 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001755
1756 dump_share_states();
1757
1758 share_states = share_states_lock();
1759 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001760 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001761 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001762 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001763 goto out;
1764 }
1765
1766 memory_region = share_state->memory_region;
1767 CHECK(memory_region != NULL);
1768
1769 if (to_locked.vm->id != memory_region->sender) {
1770 dlog_verbose(
1771 "VM %d attempted to reclaim memory handle %#x "
1772 "originally sent by VM %d.\n",
1773 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001774 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001775 goto out;
1776 }
1777
1778 if (share_state->retrieved[0]) {
1779 dlog_verbose(
1780 "Tried to reclaim memory handle %#x that has not been "
1781 "relinquished.\n",
1782 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001783 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001784 goto out;
1785 }
1786
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001787 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbran996d1d12020-05-27 14:08:43 +01001788 ret = ffa_retrieve_check_update(to_locked, composite->constituents,
1789 composite->constituent_count,
1790 memory_to_attributes,
1791 FFA_MEM_RECLAIM_32, clear, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001792
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001793 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001794 share_state_free(share_states, share_state, page_pool);
1795 dlog_verbose("Freed share state after successful reclaim.\n");
1796 }
1797
1798out:
1799 share_states_unlock(&share_states);
1800 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001801}
Andrew Walbran290b0c92020-02-03 16:37:14 +00001802
1803/**
1804 * Validates that the reclaim transition is allowed for the given memory region
1805 * and updates the page table of the reclaiming VM.
1806 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001807struct ffa_value ffa_memory_tee_reclaim(struct vm_locked to_locked,
1808 ffa_memory_handle_t handle,
1809 struct ffa_memory_region *memory_region,
1810 bool clear, struct mpool *page_pool)
Andrew Walbran290b0c92020-02-03 16:37:14 +00001811{
1812 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001813 struct ffa_composite_memory_region *composite;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001814
1815 if (memory_region->receiver_count != 1) {
1816 /* Only one receiver supported by Hafnium for now. */
1817 dlog_verbose(
1818 "Multiple recipients not supported (got %d, expected "
1819 "1).\n",
1820 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001821 return ffa_error(FFA_NOT_SUPPORTED);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001822 }
1823
1824 if (memory_region->handle != handle) {
1825 dlog_verbose(
1826 "Got memory region handle %#x from TEE but requested "
1827 "handle %#x.\n",
1828 memory_region->handle, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001829 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001830 }
1831
1832 /* The original sender must match the caller. */
1833 if (to_locked.vm->id != memory_region->sender) {
1834 dlog_verbose(
1835 "VM %d attempted to reclaim memory handle %#x "
1836 "originally sent by VM %d.\n",
1837 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001838 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001839 }
1840
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001841 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001842
1843 /*
1844 * Forward the request to the TEE and then map the memory back into the
1845 * caller's stage-2 page table.
1846 */
Andrew Walbran996d1d12020-05-27 14:08:43 +01001847 return ffa_tee_reclaim_check_update(
1848 to_locked, handle, composite->constituents,
1849 composite->constituent_count, memory_to_attributes, clear,
1850 page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001851}