blob: 6ca8a51c2398e7e0cfd40f26ef285e0cfe638fa2 [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 Walbranb5ab43c2020-04-30 11:32:54 +0100696static struct ffa_value ffa_send_memory(
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 Walbranb5ab43c2020-04-30 11:32:54 +0100803static struct ffa_value ffa_retrieve_memory(
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 Walbranb5ab43c2020-04-30 11:32:54 +0100905static struct ffa_value ffa_tee_reclaim_memory(
906 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(
976 "Got %#x (%d) from EL3 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 Walbranb5ab43c2020-04-30 11:32:54 +01001006static struct ffa_value ffa_relinquish_memory(
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 Walbranb5ab43c2020-04-30 11:32:54 +01001089 * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if
Andrew Walbrana65a1322020-04-06 19:32:32 +01001090 * not.
1091 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001092static struct ffa_value ffa_memory_send_validate(
Andrew Walbrana65a1322020-04-06 19:32:32 +01001093 struct vm *to, struct vm_locked from_locked,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001094 struct ffa_memory_region *memory_region, uint32_t memory_share_length,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001095 uint32_t share_func, bool *clear,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001096 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;
1100 uint32_t constituents_length;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001101 enum ffa_data_access data_access;
1102 enum ffa_instruction_access instruction_access;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001103
1104 CHECK(clear != NULL);
1105 CHECK(permissions != NULL);
1106
1107 /* The sender must match the message sender. */
1108 if (memory_region->sender != from_locked.vm->id) {
1109 dlog_verbose("Invalid sender %d.\n", memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001110 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001111 }
1112
1113 /* We only support a single recipient. */
1114 if (memory_region->receiver_count != 1) {
1115 dlog_verbose("Multiple recipients not supported.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001116 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001117 }
1118
1119 /*
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 Walbrana65a1322020-04-06 19:32:32 +01001125 if (memory_region->receivers[0].composite_memory_region_offset <
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001126 sizeof(struct ffa_memory_region) + receivers_length ||
Andrew Walbrana65a1322020-04-06 19:32:32 +01001127 memory_region->receivers[0].composite_memory_region_offset +
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001128 sizeof(struct ffa_composite_memory_region) >=
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001129 memory_share_length) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001130 dlog_verbose(
1131 "Invalid composite memory region descriptor offset.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001132 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001133 }
1134
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001135 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001136
1137 /*
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001138 * Ensure the number of constituents are within the memory bounds.
Andrew Walbrana65a1322020-04-06 19:32:32 +01001139 */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001140 constituents_length = sizeof(struct ffa_memory_region_constituent) *
1141 composite->constituent_count;
1142 if (memory_share_length !=
Andrew Walbrana65a1322020-04-06 19:32:32 +01001143 memory_region->receivers[0].composite_memory_region_offset +
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001144 sizeof(struct ffa_composite_memory_region) +
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001145 constituents_length) {
1146 dlog_verbose("Invalid length %d or constituent offset %d.\n",
1147 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
1153 /* The recipient must match the message recipient. */
1154 if (memory_region->receivers[0].receiver_permissions.receiver !=
1155 to->id) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001156 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001157 }
1158
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001159 *clear = memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001160 /*
1161 * Clear is not allowed for memory sharing, as the sender still has
1162 * access to the memory.
1163 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001164 if (*clear && share_func == FFA_MEM_SHARE_32) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001165 dlog_verbose("Memory can't be cleared while being shared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001166 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001167 }
1168
1169 /* No other flags are allowed/supported here. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001170 if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001171 dlog_verbose("Invalid flags %#x.\n", memory_region->flags);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001172 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001173 }
1174
1175 /* Check that the permissions are valid. */
1176 *permissions =
1177 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001178 data_access = ffa_get_data_access_attr(*permissions);
1179 instruction_access = ffa_get_instruction_access_attr(*permissions);
1180 if (data_access == FFA_DATA_ACCESS_RESERVED ||
1181 instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001182 dlog_verbose("Reserved value for receiver permissions %#x.\n",
1183 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001184 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001185 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001186 if (instruction_access != FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001187 dlog_verbose(
1188 "Invalid instruction access permissions %#x for "
1189 "sending memory.\n",
1190 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001191 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001192 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001193 if (share_func == FFA_MEM_SHARE_32) {
1194 if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001195 dlog_verbose(
1196 "Invalid data access permissions %#x for "
1197 "sharing memory.\n",
1198 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001199 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001200 }
1201 /*
1202 * According to section 6.11.3 of the FF-A spec NX is required
1203 * for share operations (but must not be specified by the
1204 * sender) so set it in the copy that we store, ready to be
1205 * returned to the retriever.
1206 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001207 ffa_set_instruction_access_attr(permissions,
1208 FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001209 memory_region->receivers[0].receiver_permissions.permissions =
1210 *permissions;
1211 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001212 if (share_func == FFA_MEM_LEND_32 &&
1213 data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001214 dlog_verbose(
1215 "Invalid data access permissions %#x for lending "
1216 "memory.\n",
1217 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001218 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001219 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001220 if (share_func == FFA_MEM_DONATE_32 &&
1221 data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001222 dlog_verbose(
1223 "Invalid data access permissions %#x for donating "
1224 "memory.\n",
1225 *permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001226 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001227 }
1228
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001229 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbrana65a1322020-04-06 19:32:32 +01001230}
1231
1232/**
Andrew Walbran475c1452020-02-07 13:22:22 +00001233 * Validates a call to donate, lend or share memory and then updates the stage-2
1234 * page tables. Specifically, check if the message length and number of memory
1235 * region constituents match, and if the transition is valid for the type of
1236 * memory sending operation.
1237 *
1238 * Assumes that the caller has already found and locked both VMs and ensured
1239 * that the destination RX buffer is available, and copied the memory region
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001240 * descriptor from the sender's TX buffer to a freshly allocated page from
1241 * Hafnium's internal pool.
1242 *
1243 * This function takes ownership of the `memory_region` passed in; it must not
1244 * be freed by the caller.
Jose Marinho09b1db82019-08-08 09:16:59 +01001245 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001246struct ffa_value ffa_memory_send(struct vm *to, struct vm_locked from_locked,
1247 struct ffa_memory_region *memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001248 uint32_t memory_share_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001249 uint32_t share_func, struct mpool *page_pool)
Jose Marinho09b1db82019-08-08 09:16:59 +01001250{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001251 struct ffa_composite_memory_region *composite;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001252 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001253 ffa_memory_access_permissions_t permissions;
1254 struct ffa_value ret;
1255 ffa_memory_handle_t handle;
Jose Marinho09b1db82019-08-08 09:16:59 +01001256
1257 /*
Andrew Walbrana65a1322020-04-06 19:32:32 +01001258 * If there is an error validating the `memory_region` then we need to
1259 * free it because we own it but we won't be storing it in a share state
1260 * after all.
Jose Marinho09b1db82019-08-08 09:16:59 +01001261 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001262 ret = ffa_memory_send_validate(to, from_locked, memory_region,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001263 memory_share_length, share_func, &clear,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001264 &permissions);
1265 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001266 mpool_free(page_pool, memory_region);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001267 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001268 }
1269
Andrew Walbrana65a1322020-04-06 19:32:32 +01001270 /* Set flag for share function, ready to be retrieved later. */
1271 switch (share_func) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001272 case FFA_MEM_SHARE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001273 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001274 FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001275 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001276 case FFA_MEM_LEND_32:
1277 memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001278 break;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001279 case FFA_MEM_DONATE_32:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001280 memory_region->flags |=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001281 FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE;
Andrew Walbrana65a1322020-04-06 19:32:32 +01001282 break;
Jose Marinho09b1db82019-08-08 09:16:59 +01001283 }
1284
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001285 /*
1286 * Allocate a share state before updating the page table. Otherwise if
1287 * updating the page table succeeded but allocating the share state
1288 * failed then it would leave the memory in a state where nobody could
1289 * get it back.
1290 */
1291 if (to->id != HF_TEE_VM_ID &&
1292 !allocate_share_state(share_func, memory_region, &handle)) {
1293 dlog_verbose("Failed to allocate share state.\n");
1294 mpool_free(page_pool, memory_region);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001295 return ffa_error(FFA_NO_MEMORY);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001296 }
1297
1298 dump_share_states();
1299
1300 /* Check that state is valid in sender page table and update. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001301 composite = ffa_memory_region_get_composite(memory_region, 0);
1302 ret = ffa_send_memory(from_locked, composite->constituents,
1303 composite->constituent_count, share_func,
1304 permissions, page_pool, clear);
1305 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001306 if (to->id != HF_TEE_VM_ID) {
1307 /* Free share state. */
1308 bool freed = share_state_free_handle(handle, page_pool);
1309
1310 CHECK(freed);
1311 }
1312
1313 return ret;
1314 }
1315
1316 if (to->id == HF_TEE_VM_ID) {
Andrew Walbrana65a1322020-04-06 19:32:32 +01001317 /* No share state allocated here so no handle to return. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001318 return (struct ffa_value){.func = FFA_SUCCESS_32};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001319 }
1320
Andrew Walbran1bbe9402020-04-30 16:47:13 +01001321 return ffa_mem_success(handle);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001322}
1323
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001324struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
1325 struct ffa_memory_region *retrieve_request,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001326 uint32_t retrieve_request_length,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001327 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001328{
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001329 uint32_t expected_retrieve_request_length =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001330 sizeof(struct ffa_memory_region) +
Andrew Walbrana65a1322020-04-06 19:32:32 +01001331 retrieve_request->receiver_count *
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001332 sizeof(struct ffa_memory_access);
1333 ffa_memory_handle_t handle = retrieve_request->handle;
1334 ffa_memory_region_flags_t transaction_type =
Andrew Walbrana65a1322020-04-06 19:32:32 +01001335 retrieve_request->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001336 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK;
1337 struct ffa_memory_region *memory_region;
1338 ffa_memory_access_permissions_t sent_permissions;
1339 enum ffa_data_access sent_data_access;
1340 enum ffa_instruction_access sent_instruction_access;
1341 ffa_memory_access_permissions_t requested_permissions;
1342 enum ffa_data_access requested_data_access;
1343 enum ffa_instruction_access requested_instruction_access;
1344 ffa_memory_access_permissions_t permissions;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001345 uint32_t memory_to_attributes;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001346 struct ffa_composite_memory_region *composite;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001347 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001348 struct ffa_memory_share_state *share_state;
1349 struct ffa_value ret;
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001350 uint32_t response_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001351
1352 dump_share_states();
1353
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001354 if (retrieve_request_length != expected_retrieve_request_length) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001355 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001356 "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001357 "but was %d.\n",
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001358 expected_retrieve_request_length,
1359 retrieve_request_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001360 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001361 }
1362
Andrew Walbrana65a1322020-04-06 19:32:32 +01001363 if (retrieve_request->receiver_count != 1) {
1364 dlog_verbose(
1365 "Multi-way memory sharing not supported (got %d "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001366 "receivers descriptors on FFA_MEM_RETRIEVE_REQ, "
Andrew Walbrana65a1322020-04-06 19:32:32 +01001367 "expected 1).\n",
1368 retrieve_request->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001369 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001370 }
1371
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001372 share_states = share_states_lock();
1373 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001374 dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001375 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001376 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001377 goto out;
1378 }
1379
Andrew Walbrana65a1322020-04-06 19:32:32 +01001380 memory_region = share_state->memory_region;
1381 CHECK(memory_region != NULL);
1382
1383 /*
1384 * Check that the transaction type expected by the receiver is correct,
1385 * if it has been specified.
1386 */
1387 if (transaction_type !=
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001388 FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED &&
Andrew Walbrana65a1322020-04-06 19:32:32 +01001389 transaction_type != (memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001390 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001391 dlog_verbose(
1392 "Incorrect transaction type %#x for "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001393 "FFA_MEM_RETRIEVE_REQ, expected %#x for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01001394 transaction_type,
1395 memory_region->flags &
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001396 FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK,
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001397 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001398 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001399 goto out;
1400 }
1401
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001402 if (retrieve_request->sender != memory_region->sender) {
1403 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001404 "Incorrect sender ID %d for FFA_MEM_RETRIEVE_REQ, "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001405 "expected %d for handle %#x.\n",
1406 retrieve_request->sender, memory_region->sender,
1407 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001408 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001409 goto out;
1410 }
1411
1412 if (retrieve_request->tag != memory_region->tag) {
1413 dlog_verbose(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001414 "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, expected "
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001415 "%d for handle %#x.\n",
1416 retrieve_request->tag, memory_region->tag, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001417 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001418 goto out;
1419 }
1420
Andrew Walbrana65a1322020-04-06 19:32:32 +01001421 if (retrieve_request->receivers[0].receiver_permissions.receiver !=
1422 to_locked.vm->id) {
1423 dlog_verbose(
1424 "Retrieve request receiver VM ID %d didn't match "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001425 "caller of FFA_MEM_RETRIEVE_REQ.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01001426 retrieve_request->receivers[0]
1427 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001428 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001429 goto out;
1430 }
1431
1432 if (memory_region->receivers[0].receiver_permissions.receiver !=
1433 to_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001434 dlog_verbose(
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001435 "Incorrect receiver VM ID %d for FFA_MEM_RETRIEVE_REQ, "
1436 "expected %d for handle %#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01001437 to_locked.vm->id,
1438 memory_region->receivers[0]
1439 .receiver_permissions.receiver,
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
1445 if (share_state->retrieved[0]) {
1446 dlog_verbose("Memory with handle %#x already retrieved.\n",
1447 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001448 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001449 goto out;
1450 }
1451
Andrew Walbrana65a1322020-04-06 19:32:32 +01001452 if (retrieve_request->receivers[0].composite_memory_region_offset !=
1453 0) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001454 dlog_verbose(
1455 "Retriever specified address ranges not supported (got "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001456 "offset %d).\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01001457 retrieve_request->receivers[0]
1458 .composite_memory_region_offset);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001459 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001460 goto out;
1461 }
1462
Andrew Walbrana65a1322020-04-06 19:32:32 +01001463 /*
1464 * Check permissions from sender against permissions requested by
1465 * receiver.
1466 */
1467 /* TODO: Check attributes too. */
1468 sent_permissions =
1469 memory_region->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001470 sent_data_access = ffa_get_data_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001471 sent_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001472 ffa_get_instruction_access_attr(sent_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001473 requested_permissions =
1474 retrieve_request->receivers[0].receiver_permissions.permissions;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001475 requested_data_access = ffa_get_data_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001476 requested_instruction_access =
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001477 ffa_get_instruction_access_attr(requested_permissions);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001478 permissions = 0;
1479 switch (sent_data_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001480 case FFA_DATA_ACCESS_NOT_SPECIFIED:
1481 case FFA_DATA_ACCESS_RW:
1482 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
1483 requested_data_access == FFA_DATA_ACCESS_RW) {
1484 ffa_set_data_access_attr(&permissions,
1485 FFA_DATA_ACCESS_RW);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001486 break;
1487 }
1488 /* Intentional fall-through. */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001489 case FFA_DATA_ACCESS_RO:
1490 if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED ||
1491 requested_data_access == FFA_DATA_ACCESS_RO) {
1492 ffa_set_data_access_attr(&permissions,
1493 FFA_DATA_ACCESS_RO);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001494 break;
1495 }
1496 dlog_verbose(
1497 "Invalid data access requested; sender specified "
1498 "permissions %#x but receiver requested %#x.\n",
1499 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001500 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001501 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001502 case FFA_DATA_ACCESS_RESERVED:
1503 panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be "
Andrew Walbrana65a1322020-04-06 19:32:32 +01001504 "checked before this point.");
1505 }
1506 switch (sent_instruction_access) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001507 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
1508 case FFA_INSTRUCTION_ACCESS_X:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001509 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001510 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
1511 requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) {
1512 ffa_set_instruction_access_attr(
1513 &permissions, FFA_INSTRUCTION_ACCESS_X);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001514 break;
1515 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001516 case FFA_INSTRUCTION_ACCESS_NX:
Andrew Walbrana65a1322020-04-06 19:32:32 +01001517 if (requested_instruction_access ==
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001518 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED ||
1519 requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) {
1520 ffa_set_instruction_access_attr(
1521 &permissions, FFA_INSTRUCTION_ACCESS_NX);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001522 break;
1523 }
1524 dlog_verbose(
1525 "Invalid instruction access requested; sender "
Andrew Walbranf07f04d2020-05-01 18:09:00 +01001526 "specified permissions %#x but receiver requested "
1527 "%#x.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01001528 sent_permissions, requested_permissions);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001529 ret = ffa_error(FFA_DENIED);
Andrew Walbrana65a1322020-04-06 19:32:32 +01001530 goto out;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001531 case FFA_INSTRUCTION_ACCESS_RESERVED:
1532 panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should "
Andrew Walbrana65a1322020-04-06 19:32:32 +01001533 "be checked before this point.");
1534 }
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001535 memory_to_attributes = ffa_memory_permissions_to_mode(permissions);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001536
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001537 composite = ffa_memory_region_get_composite(memory_region, 0);
1538 ret = ffa_retrieve_memory(to_locked, composite->constituents,
1539 composite->constituent_count,
1540 memory_to_attributes, share_state->share_func,
1541 false, page_pool);
1542 if (ret.func != FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001543 goto out;
1544 }
1545
1546 /*
1547 * Copy response to RX buffer of caller and deliver the message. This
1548 * must be done before the share_state is (possibly) freed.
1549 */
Andrew Walbrana65a1322020-04-06 19:32:32 +01001550 /* TODO: combine attributes from sender and request. */
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001551 response_length = ffa_retrieved_memory_region_init(
Andrew Walbrana65a1322020-04-06 19:32:32 +01001552 to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE,
1553 memory_region->sender, memory_region->attributes,
1554 memory_region->flags, handle, to_locked.vm->id, permissions,
1555 composite->constituents, composite->constituent_count);
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001556 to_locked.vm->mailbox.recv_size = response_length;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001557 to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001558 to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001559 to_locked.vm->mailbox.state = MAILBOX_STATE_READ;
1560
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001561 if (share_state->share_func == FFA_MEM_DONATE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001562 /*
1563 * Memory that has been donated can't be relinquished, so no
1564 * need to keep the share state around.
1565 */
1566 share_state_free(share_states, share_state, page_pool);
1567 dlog_verbose("Freed share state for donate.\n");
1568 } else {
1569 share_state->retrieved[0] = true;
1570 }
1571
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001572 ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32,
Andrew Walbran130a8ae2020-05-15 16:27:15 +01001573 .arg1 = response_length,
1574 .arg2 = response_length};
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001575
1576out:
1577 share_states_unlock(&share_states);
1578 dump_share_states();
1579 return ret;
1580}
1581
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001582struct ffa_value ffa_memory_relinquish(
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001583 struct vm_locked from_locked,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001584 struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001585{
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001586 ffa_memory_handle_t handle = relinquish_request->handle;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001587 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001588 struct ffa_memory_share_state *share_state;
1589 struct ffa_memory_region *memory_region;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001590 bool clear;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001591 struct ffa_composite_memory_region *composite;
1592 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001593
Andrew Walbrana65a1322020-04-06 19:32:32 +01001594 if (relinquish_request->endpoint_count != 1) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001595 dlog_verbose(
Andrew Walbrana65a1322020-04-06 19:32:32 +01001596 "Stream endpoints not supported (got %d endpoints on "
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001597 "FFA_MEM_RELINQUISH, expected 1).\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001598 relinquish_request->endpoint_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001599 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001600 }
1601
Andrew Walbrana65a1322020-04-06 19:32:32 +01001602 if (relinquish_request->endpoints[0] != from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001603 dlog_verbose(
1604 "VM ID %d in relinquish message doesn't match calling "
1605 "VM ID %d.\n",
Andrew Walbrana65a1322020-04-06 19:32:32 +01001606 relinquish_request->endpoints[0], from_locked.vm->id);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001607 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001608 }
1609
1610 dump_share_states();
1611
1612 share_states = share_states_lock();
1613 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001614 dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001615 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001616 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001617 goto out;
1618 }
1619
1620 memory_region = share_state->memory_region;
1621 CHECK(memory_region != NULL);
1622
Andrew Walbrana65a1322020-04-06 19:32:32 +01001623 if (memory_region->receivers[0].receiver_permissions.receiver !=
1624 from_locked.vm->id) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001625 dlog_verbose(
1626 "VM ID %d tried to relinquish memory region with "
1627 "handle %#x but receiver was %d.\n",
1628 from_locked.vm->id, handle,
Andrew Walbrana65a1322020-04-06 19:32:32 +01001629 memory_region->receivers[0]
1630 .receiver_permissions.receiver);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001631 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001632 goto out;
1633 }
1634
1635 if (!share_state->retrieved[0]) {
1636 dlog_verbose(
1637 "Memory with handle %#x not yet retrieved, can't "
1638 "relinquish.\n",
1639 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001640 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001641 goto out;
1642 }
1643
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001644 clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001645
1646 /*
1647 * Clear is not allowed for memory that was shared, as the original
1648 * sender still has access to the memory.
1649 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001650 if (clear && share_state->share_func == FFA_MEM_SHARE_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001651 dlog_verbose("Memory which was shared can't be cleared.\n");
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001652 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001653 goto out;
1654 }
1655
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001656 composite = ffa_memory_region_get_composite(memory_region, 0);
1657 ret = ffa_relinquish_memory(from_locked, composite->constituents,
1658 composite->constituent_count, page_pool,
1659 clear);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001660
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001661 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001662 /*
1663 * Mark memory handle as not retrieved, so it can be reclaimed
1664 * (or retrieved again).
1665 */
1666 share_state->retrieved[0] = false;
1667 }
1668
1669out:
1670 share_states_unlock(&share_states);
1671 dump_share_states();
1672 return ret;
1673}
1674
1675/**
1676 * Validates that the reclaim transition is allowed for the given handle,
1677 * updates the page table of the reclaiming VM, and frees the internal state
1678 * associated with the handle.
1679 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001680struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked,
1681 ffa_memory_handle_t handle, bool clear,
1682 struct mpool *page_pool)
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001683{
1684 struct share_states_locked share_states;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001685 struct ffa_memory_share_state *share_state;
1686 struct ffa_memory_region *memory_region;
1687 struct ffa_composite_memory_region *composite;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001688 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001689 struct ffa_value ret;
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001690
1691 dump_share_states();
1692
1693 share_states = share_states_lock();
1694 if (!get_share_state(share_states, handle, &share_state)) {
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001695 dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n",
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001696 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001697 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001698 goto out;
1699 }
1700
1701 memory_region = share_state->memory_region;
1702 CHECK(memory_region != NULL);
1703
1704 if (to_locked.vm->id != memory_region->sender) {
1705 dlog_verbose(
1706 "VM %d attempted to reclaim memory handle %#x "
1707 "originally sent by VM %d.\n",
1708 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001709 ret = ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001710 goto out;
1711 }
1712
1713 if (share_state->retrieved[0]) {
1714 dlog_verbose(
1715 "Tried to reclaim memory handle %#x that has not been "
1716 "relinquished.\n",
1717 handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001718 ret = ffa_error(FFA_DENIED);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001719 goto out;
1720 }
1721
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001722 composite = ffa_memory_region_get_composite(memory_region, 0);
1723 ret = ffa_retrieve_memory(to_locked, composite->constituents,
1724 composite->constituent_count,
1725 memory_to_attributes, FFA_MEM_RECLAIM_32,
1726 clear, page_pool);
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001727
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001728 if (ret.func == FFA_SUCCESS_32) {
Andrew Walbran5de9c3d2020-02-10 13:35:29 +00001729 share_state_free(share_states, share_state, page_pool);
1730 dlog_verbose("Freed share state after successful reclaim.\n");
1731 }
1732
1733out:
1734 share_states_unlock(&share_states);
1735 return ret;
Jose Marinho09b1db82019-08-08 09:16:59 +01001736}
Andrew Walbran290b0c92020-02-03 16:37:14 +00001737
1738/**
1739 * Validates that the reclaim transition is allowed for the given memory region
1740 * and updates the page table of the reclaiming VM.
1741 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001742struct ffa_value ffa_memory_tee_reclaim(struct vm_locked to_locked,
1743 ffa_memory_handle_t handle,
1744 struct ffa_memory_region *memory_region,
1745 bool clear, struct mpool *page_pool)
Andrew Walbran290b0c92020-02-03 16:37:14 +00001746{
1747 uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001748 struct ffa_composite_memory_region *composite;
Andrew Walbran290b0c92020-02-03 16:37:14 +00001749
1750 if (memory_region->receiver_count != 1) {
1751 /* Only one receiver supported by Hafnium for now. */
1752 dlog_verbose(
1753 "Multiple recipients not supported (got %d, expected "
1754 "1).\n",
1755 memory_region->receiver_count);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001756 return ffa_error(FFA_NOT_SUPPORTED);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001757 }
1758
1759 if (memory_region->handle != handle) {
1760 dlog_verbose(
1761 "Got memory region handle %#x from TEE but requested "
1762 "handle %#x.\n",
1763 memory_region->handle, handle);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001764 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001765 }
1766
1767 /* The original sender must match the caller. */
1768 if (to_locked.vm->id != memory_region->sender) {
1769 dlog_verbose(
1770 "VM %d attempted to reclaim memory handle %#x "
1771 "originally sent by VM %d.\n",
1772 to_locked.vm->id, handle, memory_region->sender);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001773 return ffa_error(FFA_INVALID_PARAMETERS);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001774 }
1775
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001776 composite = ffa_memory_region_get_composite(memory_region, 0);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001777
1778 /*
1779 * Forward the request to the TEE and then map the memory back into the
1780 * caller's stage-2 page table.
1781 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001782 return ffa_tee_reclaim_memory(to_locked, handle,
1783 composite->constituents,
1784 composite->constituent_count,
1785 memory_to_attributes, clear, page_pool);
Andrew Walbran290b0c92020-02-03 16:37:14 +00001786}