Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Hafnium Authors. |
| 3 | * |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style |
| 5 | * license that can be found in the LICENSE file or at |
| 6 | * https://opensource.org/licenses/BSD-3-Clause. |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 9 | #include "hf/ffa_memory.h" |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 10 | |
Federico Recanati | 4fd065d | 2021-12-13 20:06:23 +0100 | [diff] [blame] | 11 | #include "hf/arch/mm.h" |
Olivier Deprez | 112d2b5 | 2020-09-30 07:39:23 +0200 | [diff] [blame] | 12 | #include "hf/arch/other_world.h" |
Olivier Deprez | 55a189e | 2021-06-09 15:45:27 +0200 | [diff] [blame] | 13 | #include "hf/arch/plat/ffa.h" |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 14 | |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 15 | #include "hf/api.h" |
Daniel Boulby | a2f8c66 | 2021-11-26 17:52:53 +0000 | [diff] [blame] | 16 | #include "hf/assert.h" |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 17 | #include "hf/check.h" |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 18 | #include "hf/dlog.h" |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 19 | #include "hf/ffa_internal.h" |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 20 | #include "hf/mpool.h" |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 21 | #include "hf/std.h" |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 22 | #include "hf/vm.h" |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 23 | |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 24 | /** |
| 25 | * The maximum number of memory sharing handles which may be active at once. A |
| 26 | * DONATE handle is active from when it is sent to when it is retrieved; a SHARE |
| 27 | * or LEND handle is active from when it is sent to when it is reclaimed. |
| 28 | */ |
| 29 | #define MAX_MEM_SHARES 100 |
| 30 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 31 | /** |
| 32 | * The maximum number of fragments into which a memory sharing message may be |
| 33 | * broken. |
| 34 | */ |
| 35 | #define MAX_FRAGMENTS 20 |
| 36 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 37 | static_assert(sizeof(struct ffa_memory_region_constituent) % 16 == 0, |
| 38 | "struct ffa_memory_region_constituent must be a multiple of 16 " |
Andrew Walbran | c34c7b2 | 2020-02-28 11:16:59 +0000 | [diff] [blame] | 39 | "bytes long."); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 40 | static_assert(sizeof(struct ffa_composite_memory_region) % 16 == 0, |
| 41 | "struct ffa_composite_memory_region must be a multiple of 16 " |
Andrew Walbran | c34c7b2 | 2020-02-28 11:16:59 +0000 | [diff] [blame] | 42 | "bytes long."); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 43 | static_assert(sizeof(struct ffa_memory_region_attributes) == 4, |
Andrew Walbran | 41890ff | 2020-09-23 15:09:39 +0100 | [diff] [blame] | 44 | "struct ffa_memory_region_attributes must be 4 bytes long."); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 45 | static_assert(sizeof(struct ffa_memory_access) % 16 == 0, |
| 46 | "struct ffa_memory_access must be a multiple of 16 bytes long."); |
| 47 | static_assert(sizeof(struct ffa_memory_region) % 16 == 0, |
| 48 | "struct ffa_memory_region must be a multiple of 16 bytes long."); |
| 49 | static_assert(sizeof(struct ffa_mem_relinquish) % 16 == 0, |
| 50 | "struct ffa_mem_relinquish must be a multiple of 16 " |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 51 | "bytes long."); |
Andrew Walbran | c34c7b2 | 2020-02-28 11:16:59 +0000 | [diff] [blame] | 52 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 53 | struct ffa_memory_share_state { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 54 | /** |
| 55 | * The memory region being shared, or NULL if this share state is |
| 56 | * unallocated. |
| 57 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 58 | struct ffa_memory_region *memory_region; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 59 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 60 | struct ffa_memory_region_constituent *fragments[MAX_FRAGMENTS]; |
| 61 | |
| 62 | /** The number of constituents in each fragment. */ |
| 63 | uint32_t fragment_constituent_counts[MAX_FRAGMENTS]; |
| 64 | |
| 65 | /** |
| 66 | * The number of valid elements in the `fragments` and |
| 67 | * `fragment_constituent_counts` arrays. |
| 68 | */ |
| 69 | uint32_t fragment_count; |
| 70 | |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 71 | /** |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 72 | * The FF-A function used for sharing the memory. Must be one of |
| 73 | * FFA_MEM_DONATE_32, FFA_MEM_LEND_32 or FFA_MEM_SHARE_32 if the |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 74 | * share state is allocated, or 0. |
| 75 | */ |
| 76 | uint32_t share_func; |
| 77 | |
| 78 | /** |
J-Alves | 2a0d288 | 2020-10-29 14:49:50 +0000 | [diff] [blame] | 79 | * The sender's original mode before invoking the FF-A function for |
| 80 | * sharing the memory. |
| 81 | * This is used to reset the original configuration when sender invokes |
| 82 | * FFA_MEM_RECLAIM_32. |
| 83 | */ |
| 84 | uint32_t sender_orig_mode; |
| 85 | |
| 86 | /** |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 87 | * True if all the fragments of this sharing request have been sent and |
| 88 | * Hafnium has updated the sender page table accordingly. |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 89 | */ |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 90 | bool sending_complete; |
| 91 | |
| 92 | /** |
| 93 | * How many fragments of the memory region each recipient has retrieved |
| 94 | * so far. The order of this array matches the order of the endpoint |
| 95 | * memory access descriptors in the memory region descriptor. Any |
| 96 | * entries beyond the receiver_count will always be 0. |
| 97 | */ |
| 98 | uint32_t retrieved_fragment_count[MAX_MEM_SHARE_RECIPIENTS]; |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * Field for the SPMC to keep track of how many fragments of the memory |
| 102 | * region the hypervisor has managed to retrieve, using a |
| 103 | * `hypervisor retrieve request`, as defined by FF-A v1.1 EAC0 |
| 104 | * specification. |
| 105 | */ |
| 106 | uint32_t hypervisor_fragment_count; |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 109 | /** |
| 110 | * Encapsulates the set of share states while the `share_states_lock` is held. |
| 111 | */ |
| 112 | struct share_states_locked { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 113 | struct ffa_memory_share_state *share_states; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | /** |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 117 | * All access to members of a `struct ffa_memory_share_state` must be guarded |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 118 | * by this lock. |
| 119 | */ |
| 120 | static struct spinlock share_states_lock_instance = SPINLOCK_INIT; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 121 | static struct ffa_memory_share_state share_states[MAX_MEM_SHARES]; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 122 | |
| 123 | /** |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 124 | * Buffer for retrieving memory region information from the other world for when |
| 125 | * a region is reclaimed by a VM. Access to this buffer must be guarded by the |
| 126 | * VM lock of the other world VM. |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 127 | */ |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 128 | alignas(PAGE_SIZE) static uint8_t |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 129 | other_world_retrieve_buffer[HF_MAILBOX_SIZE * MAX_FRAGMENTS]; |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 130 | |
| 131 | /** |
J-Alves | 917d2f2 | 2020-10-30 18:39:30 +0000 | [diff] [blame] | 132 | * Extracts the index from a memory handle allocated by Hafnium's current world. |
| 133 | */ |
| 134 | uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle) |
| 135 | { |
| 136 | return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK; |
| 137 | } |
| 138 | |
| 139 | /** |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 140 | * Initialises the next available `struct ffa_memory_share_state` and sets |
| 141 | * `share_state_ret` to a pointer to it. If `handle` is |
| 142 | * `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle, otherwise |
| 143 | * uses the provided handle which is assumed to be globally unique. |
| 144 | * |
| 145 | * Returns true on success or false if none are available. |
| 146 | */ |
| 147 | static bool allocate_share_state( |
| 148 | struct share_states_locked share_states, uint32_t share_func, |
| 149 | struct ffa_memory_region *memory_region, uint32_t fragment_length, |
| 150 | ffa_memory_handle_t handle, |
| 151 | struct ffa_memory_share_state **share_state_ret) |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 152 | { |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 153 | uint64_t i; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 154 | |
Daniel Boulby | a2f8c66 | 2021-11-26 17:52:53 +0000 | [diff] [blame] | 155 | assert(share_states.share_states != NULL); |
| 156 | assert(memory_region != NULL); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 157 | |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 158 | for (i = 0; i < MAX_MEM_SHARES; ++i) { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 159 | if (share_states.share_states[i].share_func == 0) { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 160 | uint32_t j; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 161 | struct ffa_memory_share_state *allocated_state = |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 162 | &share_states.share_states[i]; |
| 163 | struct ffa_composite_memory_region *composite = |
| 164 | ffa_memory_region_get_composite(memory_region, |
| 165 | 0); |
| 166 | |
| 167 | if (handle == FFA_MEMORY_HANDLE_INVALID) { |
J-Alves | ee68c54 | 2020-10-29 17:48:20 +0000 | [diff] [blame] | 168 | memory_region->handle = |
Olivier Deprez | 55a189e | 2021-06-09 15:45:27 +0200 | [diff] [blame] | 169 | plat_ffa_memory_handle_make(i); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 170 | } else { |
J-Alves | ee68c54 | 2020-10-29 17:48:20 +0000 | [diff] [blame] | 171 | memory_region->handle = handle; |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 172 | } |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 173 | allocated_state->share_func = share_func; |
| 174 | allocated_state->memory_region = memory_region; |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 175 | allocated_state->fragment_count = 1; |
| 176 | allocated_state->fragments[0] = composite->constituents; |
| 177 | allocated_state->fragment_constituent_counts[0] = |
| 178 | (fragment_length - |
| 179 | ffa_composite_constituent_offset(memory_region, |
| 180 | 0)) / |
| 181 | sizeof(struct ffa_memory_region_constituent); |
| 182 | allocated_state->sending_complete = false; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 183 | for (j = 0; j < MAX_MEM_SHARE_RECIPIENTS; ++j) { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 184 | allocated_state->retrieved_fragment_count[j] = |
| 185 | 0; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 186 | } |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 187 | if (share_state_ret != NULL) { |
| 188 | *share_state_ret = allocated_state; |
| 189 | } |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 190 | return true; |
| 191 | } |
| 192 | } |
| 193 | |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 194 | return false; |
| 195 | } |
| 196 | |
| 197 | /** Locks the share states lock. */ |
| 198 | struct share_states_locked share_states_lock(void) |
| 199 | { |
| 200 | sl_lock(&share_states_lock_instance); |
| 201 | |
| 202 | return (struct share_states_locked){.share_states = share_states}; |
| 203 | } |
| 204 | |
| 205 | /** Unlocks the share states lock. */ |
| 206 | static void share_states_unlock(struct share_states_locked *share_states) |
| 207 | { |
Daniel Boulby | a2f8c66 | 2021-11-26 17:52:53 +0000 | [diff] [blame] | 208 | assert(share_states->share_states != NULL); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 209 | share_states->share_states = NULL; |
| 210 | sl_unlock(&share_states_lock_instance); |
| 211 | } |
| 212 | |
| 213 | /** |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 214 | * If the given handle is a valid handle for an allocated share state then |
| 215 | * initialises `share_state_ret` to point to the share state and returns true. |
| 216 | * Otherwise returns false. |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 217 | */ |
| 218 | static bool get_share_state(struct share_states_locked share_states, |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 219 | ffa_memory_handle_t handle, |
| 220 | struct ffa_memory_share_state **share_state_ret) |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 221 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 222 | struct ffa_memory_share_state *share_state; |
J-Alves | 917d2f2 | 2020-10-30 18:39:30 +0000 | [diff] [blame] | 223 | uint64_t index; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 224 | |
Daniel Boulby | a2f8c66 | 2021-11-26 17:52:53 +0000 | [diff] [blame] | 225 | assert(share_states.share_states != NULL); |
| 226 | assert(share_state_ret != NULL); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 227 | |
| 228 | /* |
| 229 | * First look for a share_state allocated by us, in which case the |
| 230 | * handle is based on the index. |
| 231 | */ |
Olivier Deprez | 55a189e | 2021-06-09 15:45:27 +0200 | [diff] [blame] | 232 | if (plat_ffa_memory_handle_allocated_by_current_world(handle)) { |
J-Alves | 917d2f2 | 2020-10-30 18:39:30 +0000 | [diff] [blame] | 233 | index = ffa_memory_handle_get_index(handle); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 234 | if (index < MAX_MEM_SHARES) { |
| 235 | share_state = &share_states.share_states[index]; |
| 236 | if (share_state->share_func != 0) { |
| 237 | *share_state_ret = share_state; |
| 238 | return true; |
| 239 | } |
| 240 | } |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 243 | /* Fall back to a linear scan. */ |
| 244 | for (index = 0; index < MAX_MEM_SHARES; ++index) { |
| 245 | share_state = &share_states.share_states[index]; |
J-Alves | ee68c54 | 2020-10-29 17:48:20 +0000 | [diff] [blame] | 246 | if (share_state->memory_region != NULL && |
| 247 | share_state->memory_region->handle == handle && |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 248 | share_state->share_func != 0) { |
| 249 | *share_state_ret = share_state; |
| 250 | return true; |
| 251 | } |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 254 | return false; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /** Marks a share state as unallocated. */ |
| 258 | static void share_state_free(struct share_states_locked share_states, |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 259 | struct ffa_memory_share_state *share_state, |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 260 | struct mpool *page_pool) |
| 261 | { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 262 | uint32_t i; |
| 263 | |
Daniel Boulby | a2f8c66 | 2021-11-26 17:52:53 +0000 | [diff] [blame] | 264 | assert(share_states.share_states != NULL); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 265 | share_state->share_func = 0; |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 266 | share_state->sending_complete = false; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 267 | mpool_free(page_pool, share_state->memory_region); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 268 | /* |
| 269 | * First fragment is part of the same page as the `memory_region`, so it |
| 270 | * doesn't need to be freed separately. |
| 271 | */ |
| 272 | share_state->fragments[0] = NULL; |
| 273 | share_state->fragment_constituent_counts[0] = 0; |
| 274 | for (i = 1; i < share_state->fragment_count; ++i) { |
| 275 | mpool_free(page_pool, share_state->fragments[i]); |
| 276 | share_state->fragments[i] = NULL; |
| 277 | share_state->fragment_constituent_counts[i] = 0; |
| 278 | } |
| 279 | share_state->fragment_count = 0; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 280 | share_state->memory_region = NULL; |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 281 | share_state->hypervisor_fragment_count = 0; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 284 | /** Checks whether the given share state has been fully sent. */ |
| 285 | static bool share_state_sending_complete( |
| 286 | struct share_states_locked share_states, |
| 287 | struct ffa_memory_share_state *share_state) |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 288 | { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 289 | struct ffa_composite_memory_region *composite; |
| 290 | uint32_t expected_constituent_count; |
| 291 | uint32_t fragment_constituent_count_total = 0; |
| 292 | uint32_t i; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 293 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 294 | /* Lock must be held. */ |
Daniel Boulby | a2f8c66 | 2021-11-26 17:52:53 +0000 | [diff] [blame] | 295 | assert(share_states.share_states != NULL); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 296 | |
| 297 | /* |
| 298 | * Share state must already be valid, or it's not possible to get hold |
| 299 | * of it. |
| 300 | */ |
| 301 | CHECK(share_state->memory_region != NULL && |
| 302 | share_state->share_func != 0); |
| 303 | |
| 304 | composite = |
| 305 | ffa_memory_region_get_composite(share_state->memory_region, 0); |
| 306 | expected_constituent_count = composite->constituent_count; |
| 307 | for (i = 0; i < share_state->fragment_count; ++i) { |
| 308 | fragment_constituent_count_total += |
| 309 | share_state->fragment_constituent_counts[i]; |
| 310 | } |
| 311 | dlog_verbose( |
| 312 | "Checking completion: constituent count %d/%d from %d " |
| 313 | "fragments.\n", |
| 314 | fragment_constituent_count_total, expected_constituent_count, |
| 315 | share_state->fragment_count); |
| 316 | |
| 317 | return fragment_constituent_count_total == expected_constituent_count; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Calculates the offset of the next fragment expected for the given share |
| 322 | * state. |
| 323 | */ |
| 324 | static uint32_t share_state_next_fragment_offset( |
| 325 | struct share_states_locked share_states, |
| 326 | struct ffa_memory_share_state *share_state) |
| 327 | { |
| 328 | uint32_t next_fragment_offset; |
| 329 | uint32_t i; |
| 330 | |
| 331 | /* Lock must be held. */ |
Daniel Boulby | a2f8c66 | 2021-11-26 17:52:53 +0000 | [diff] [blame] | 332 | assert(share_states.share_states != NULL); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 333 | |
| 334 | next_fragment_offset = |
| 335 | ffa_composite_constituent_offset(share_state->memory_region, 0); |
| 336 | for (i = 0; i < share_state->fragment_count; ++i) { |
| 337 | next_fragment_offset += |
| 338 | share_state->fragment_constituent_counts[i] * |
| 339 | sizeof(struct ffa_memory_region_constituent); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 342 | return next_fragment_offset; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 345 | static void dump_memory_region(struct ffa_memory_region *memory_region) |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 346 | { |
| 347 | uint32_t i; |
| 348 | |
| 349 | if (LOG_LEVEL < LOG_LEVEL_VERBOSE) { |
| 350 | return; |
| 351 | } |
| 352 | |
Olivier Deprez | 935e1b1 | 2020-12-22 18:01:29 +0100 | [diff] [blame] | 353 | dlog("from VM %#x, attributes %#x, flags %#x, tag %u, to " |
Olivier Deprez | f92e5d4 | 2020-11-13 16:00:54 +0100 | [diff] [blame] | 354 | "%u " |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 355 | "recipients [", |
| 356 | memory_region->sender, memory_region->attributes, |
Olivier Deprez | 935e1b1 | 2020-12-22 18:01:29 +0100 | [diff] [blame] | 357 | memory_region->flags, memory_region->tag, |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 358 | memory_region->receiver_count); |
| 359 | for (i = 0; i < memory_region->receiver_count; ++i) { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 360 | if (i != 0) { |
| 361 | dlog(", "); |
| 362 | } |
Olivier Deprez | f92e5d4 | 2020-11-13 16:00:54 +0100 | [diff] [blame] | 363 | dlog("VM %#x: %#x (offset %u)", |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 364 | memory_region->receivers[i].receiver_permissions.receiver, |
| 365 | memory_region->receivers[i] |
| 366 | .receiver_permissions.permissions, |
| 367 | memory_region->receivers[i] |
| 368 | .composite_memory_region_offset); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 369 | } |
| 370 | dlog("]"); |
| 371 | } |
| 372 | |
| 373 | static void dump_share_states(void) |
| 374 | { |
| 375 | uint32_t i; |
| 376 | |
| 377 | if (LOG_LEVEL < LOG_LEVEL_VERBOSE) { |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | dlog("Current share states:\n"); |
| 382 | sl_lock(&share_states_lock_instance); |
| 383 | for (i = 0; i < MAX_MEM_SHARES; ++i) { |
| 384 | if (share_states[i].share_func != 0) { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 385 | switch (share_states[i].share_func) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 386 | case FFA_MEM_SHARE_32: |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 387 | dlog("SHARE"); |
| 388 | break; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 389 | case FFA_MEM_LEND_32: |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 390 | dlog("LEND"); |
| 391 | break; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 392 | case FFA_MEM_DONATE_32: |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 393 | dlog("DONATE"); |
| 394 | break; |
| 395 | default: |
| 396 | dlog("invalid share_func %#x", |
| 397 | share_states[i].share_func); |
| 398 | } |
Olivier Deprez | 935e1b1 | 2020-12-22 18:01:29 +0100 | [diff] [blame] | 399 | dlog(" %#x (", share_states[i].memory_region->handle); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 400 | dump_memory_region(share_states[i].memory_region); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 401 | if (share_states[i].sending_complete) { |
| 402 | dlog("): fully sent"); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 403 | } else { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 404 | dlog("): partially sent"); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 405 | } |
J-Alves | 2a0d288 | 2020-10-29 14:49:50 +0000 | [diff] [blame] | 406 | dlog(" with %d fragments, %d retrieved, " |
| 407 | " sender's original mode: %#x\n", |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 408 | share_states[i].fragment_count, |
J-Alves | 2a0d288 | 2020-10-29 14:49:50 +0000 | [diff] [blame] | 409 | share_states[i].retrieved_fragment_count[0], |
| 410 | share_states[i].sender_orig_mode); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | sl_unlock(&share_states_lock_instance); |
| 414 | } |
| 415 | |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 416 | /* TODO: Add device attributes: GRE, cacheability, shareability. */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 417 | static inline uint32_t ffa_memory_permissions_to_mode( |
J-Alves | 7cd5eb3 | 2020-10-16 19:06:10 +0100 | [diff] [blame] | 418 | ffa_memory_access_permissions_t permissions, uint32_t default_mode) |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 419 | { |
| 420 | uint32_t mode = 0; |
| 421 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 422 | switch (ffa_get_data_access_attr(permissions)) { |
| 423 | case FFA_DATA_ACCESS_RO: |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 424 | mode = MM_MODE_R; |
| 425 | break; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 426 | case FFA_DATA_ACCESS_RW: |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 427 | mode = MM_MODE_R | MM_MODE_W; |
| 428 | break; |
J-Alves | 7cd5eb3 | 2020-10-16 19:06:10 +0100 | [diff] [blame] | 429 | case FFA_DATA_ACCESS_NOT_SPECIFIED: |
| 430 | mode = (default_mode & (MM_MODE_R | MM_MODE_W)); |
| 431 | break; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 432 | case FFA_DATA_ACCESS_RESERVED: |
| 433 | panic("Tried to convert FFA_DATA_ACCESS_RESERVED."); |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 434 | } |
| 435 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 436 | switch (ffa_get_instruction_access_attr(permissions)) { |
| 437 | case FFA_INSTRUCTION_ACCESS_NX: |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 438 | break; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 439 | case FFA_INSTRUCTION_ACCESS_X: |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 440 | mode |= MM_MODE_X; |
| 441 | break; |
J-Alves | 7cd5eb3 | 2020-10-16 19:06:10 +0100 | [diff] [blame] | 442 | case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED: |
| 443 | mode |= (default_mode & MM_MODE_X); |
| 444 | break; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 445 | case FFA_INSTRUCTION_ACCESS_RESERVED: |
| 446 | panic("Tried to convert FFA_INSTRUCTION_ACCESS_RESVERVED."); |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | return mode; |
| 450 | } |
| 451 | |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 452 | /** |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 453 | * Get the current mode in the stage-2 page table of the given vm of all the |
| 454 | * pages in the given constituents, if they all have the same mode, or return |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 455 | * an appropriate FF-A error if not. |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 456 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 457 | static struct ffa_value constituents_get_mode( |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 458 | struct vm_locked vm, uint32_t *orig_mode, |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 459 | struct ffa_memory_region_constituent **fragments, |
| 460 | const uint32_t *fragment_constituent_counts, uint32_t fragment_count) |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 461 | { |
Jose Marinho | 7fbbf2e | 2019-08-05 13:19:58 +0100 | [diff] [blame] | 462 | uint32_t i; |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 463 | uint32_t j; |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 464 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 465 | if (fragment_count == 0 || fragment_constituent_counts[0] == 0) { |
Jose Marinho | 7fbbf2e | 2019-08-05 13:19:58 +0100 | [diff] [blame] | 466 | /* |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 467 | * Fail if there are no constituents. Otherwise we would get an |
| 468 | * uninitialised *orig_mode. |
Jose Marinho | 7fbbf2e | 2019-08-05 13:19:58 +0100 | [diff] [blame] | 469 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 470 | return ffa_error(FFA_INVALID_PARAMETERS); |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 471 | } |
| 472 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 473 | for (i = 0; i < fragment_count; ++i) { |
| 474 | for (j = 0; j < fragment_constituent_counts[i]; ++j) { |
| 475 | ipaddr_t begin = ipa_init(fragments[i][j].address); |
| 476 | size_t size = fragments[i][j].page_count * PAGE_SIZE; |
| 477 | ipaddr_t end = ipa_add(begin, size); |
| 478 | uint32_t current_mode; |
Jose Marinho | 7fbbf2e | 2019-08-05 13:19:58 +0100 | [diff] [blame] | 479 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 480 | /* Fail if addresses are not page-aligned. */ |
| 481 | if (!is_aligned(ipa_addr(begin), PAGE_SIZE) || |
| 482 | !is_aligned(ipa_addr(end), PAGE_SIZE)) { |
| 483 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 484 | } |
Jose Marinho | 7fbbf2e | 2019-08-05 13:19:58 +0100 | [diff] [blame] | 485 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 486 | /* |
| 487 | * Ensure that this constituent memory range is all |
| 488 | * mapped with the same mode. |
| 489 | */ |
Raghu Krishnamurthy | 785d52f | 2021-02-13 00:02:40 -0800 | [diff] [blame] | 490 | if (!vm_mem_get_mode(vm, begin, end, ¤t_mode)) { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 491 | return ffa_error(FFA_DENIED); |
| 492 | } |
Jose Marinho | 7fbbf2e | 2019-08-05 13:19:58 +0100 | [diff] [blame] | 493 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 494 | /* |
| 495 | * Ensure that all constituents are mapped with the same |
| 496 | * mode. |
| 497 | */ |
| 498 | if (i == 0) { |
| 499 | *orig_mode = current_mode; |
| 500 | } else if (current_mode != *orig_mode) { |
| 501 | dlog_verbose( |
| 502 | "Expected mode %#x but was %#x for %d " |
| 503 | "pages at %#x.\n", |
| 504 | *orig_mode, current_mode, |
| 505 | fragments[i][j].page_count, |
| 506 | ipa_addr(begin)); |
| 507 | return ffa_error(FFA_DENIED); |
| 508 | } |
Jose Marinho | 7fbbf2e | 2019-08-05 13:19:58 +0100 | [diff] [blame] | 509 | } |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 510 | } |
| 511 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 512 | return (struct ffa_value){.func = FFA_SUCCESS_32}; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /** |
| 516 | * Verify that all pages have the same mode, that the starting mode |
| 517 | * constitutes a valid state and obtain the next mode to apply |
| 518 | * to the sending VM. |
| 519 | * |
| 520 | * Returns: |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 521 | * 1) FFA_DENIED if a state transition was not found; |
| 522 | * 2) FFA_DENIED if the pages being shared do not have the same mode within |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 523 | * the <from> VM; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 524 | * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 525 | * aligned; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 526 | * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled. |
| 527 | * Or FFA_SUCCESS on success. |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 528 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 529 | static struct ffa_value ffa_send_check_transition( |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 530 | struct vm_locked from, uint32_t share_func, |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 531 | struct ffa_memory_access *receivers, uint32_t receivers_count, |
| 532 | uint32_t *orig_from_mode, |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 533 | struct ffa_memory_region_constituent **fragments, |
| 534 | uint32_t *fragment_constituent_counts, uint32_t fragment_count, |
| 535 | uint32_t *from_mode) |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 536 | { |
| 537 | const uint32_t state_mask = |
| 538 | MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 539 | struct ffa_value ret; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 540 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 541 | ret = constituents_get_mode(from, orig_from_mode, fragments, |
| 542 | fragment_constituent_counts, |
| 543 | fragment_count); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 544 | if (ret.func != FFA_SUCCESS_32) { |
Olivier Deprez | e7eb168 | 2022-03-16 17:09:03 +0100 | [diff] [blame] | 545 | dlog_verbose("Inconsistent modes.\n"); |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 546 | return ret; |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 547 | } |
| 548 | |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 549 | /* Ensure the address range is normal memory and not a device. */ |
| 550 | if (*orig_from_mode & MM_MODE_D) { |
| 551 | dlog_verbose("Can't share device memory (mode is %#x).\n", |
| 552 | *orig_from_mode); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 553 | return ffa_error(FFA_DENIED); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | /* |
| 557 | * Ensure the sender is the owner and has exclusive access to the |
| 558 | * memory. |
| 559 | */ |
| 560 | if ((*orig_from_mode & state_mask) != 0) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 561 | return ffa_error(FFA_DENIED); |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 562 | } |
| 563 | |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 564 | assert(receivers != NULL && receivers_count > 0U); |
J-Alves | 7cd5eb3 | 2020-10-16 19:06:10 +0100 | [diff] [blame] | 565 | |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 566 | for (uint32_t i = 0U; i < receivers_count; i++) { |
| 567 | ffa_memory_access_permissions_t permissions = |
| 568 | receivers[i].receiver_permissions.permissions; |
| 569 | uint32_t required_from_mode = ffa_memory_permissions_to_mode( |
| 570 | permissions, *orig_from_mode); |
| 571 | |
| 572 | if ((*orig_from_mode & required_from_mode) != |
| 573 | required_from_mode) { |
| 574 | dlog_verbose( |
| 575 | "Sender tried to send memory with permissions " |
| 576 | "which " |
| 577 | "required mode %#x but only had %#x itself.\n", |
| 578 | required_from_mode, *orig_from_mode); |
| 579 | return ffa_error(FFA_DENIED); |
| 580 | } |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /* Find the appropriate new mode. */ |
| 584 | *from_mode = ~state_mask & *orig_from_mode; |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 585 | switch (share_func) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 586 | case FFA_MEM_DONATE_32: |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 587 | *from_mode |= MM_MODE_INVALID | MM_MODE_UNOWNED; |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 588 | break; |
| 589 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 590 | case FFA_MEM_LEND_32: |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 591 | *from_mode |= MM_MODE_INVALID; |
Andrew Walbran | 648fc3e | 2019-10-22 16:23:05 +0100 | [diff] [blame] | 592 | break; |
| 593 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 594 | case FFA_MEM_SHARE_32: |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 595 | *from_mode |= MM_MODE_SHARED; |
Jose Marinho | 56c2573 | 2019-05-20 09:48:53 +0100 | [diff] [blame] | 596 | break; |
| 597 | |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 598 | default: |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 599 | return ffa_error(FFA_INVALID_PARAMETERS); |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 600 | } |
| 601 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 602 | return (struct ffa_value){.func = FFA_SUCCESS_32}; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 605 | static struct ffa_value ffa_relinquish_check_transition( |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 606 | struct vm_locked from, uint32_t *orig_from_mode, |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 607 | struct ffa_memory_region_constituent **fragments, |
| 608 | uint32_t *fragment_constituent_counts, uint32_t fragment_count, |
| 609 | uint32_t *from_mode) |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 610 | { |
| 611 | const uint32_t state_mask = |
| 612 | MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED; |
| 613 | uint32_t orig_from_state; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 614 | struct ffa_value ret; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 615 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 616 | ret = constituents_get_mode(from, orig_from_mode, fragments, |
| 617 | fragment_constituent_counts, |
| 618 | fragment_count); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 619 | if (ret.func != FFA_SUCCESS_32) { |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 620 | return ret; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | /* Ensure the address range is normal memory and not a device. */ |
| 624 | if (*orig_from_mode & MM_MODE_D) { |
| 625 | dlog_verbose("Can't relinquish device memory (mode is %#x).\n", |
| 626 | *orig_from_mode); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 627 | return ffa_error(FFA_DENIED); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | /* |
| 631 | * Ensure the relinquishing VM is not the owner but has access to the |
| 632 | * memory. |
| 633 | */ |
| 634 | orig_from_state = *orig_from_mode & state_mask; |
| 635 | if ((orig_from_state & ~MM_MODE_SHARED) != MM_MODE_UNOWNED) { |
| 636 | dlog_verbose( |
| 637 | "Tried to relinquish memory in state %#x (masked %#x " |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 638 | "but should be %#x).\n", |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 639 | *orig_from_mode, orig_from_state, MM_MODE_UNOWNED); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 640 | return ffa_error(FFA_DENIED); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /* Find the appropriate new mode. */ |
| 644 | *from_mode = (~state_mask & *orig_from_mode) | MM_MODE_UNMAPPED_MASK; |
| 645 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 646 | return (struct ffa_value){.func = FFA_SUCCESS_32}; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | /** |
| 650 | * Verify that all pages have the same mode, that the starting mode |
| 651 | * constitutes a valid state and obtain the next mode to apply |
| 652 | * to the retrieving VM. |
| 653 | * |
| 654 | * Returns: |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 655 | * 1) FFA_DENIED if a state transition was not found; |
| 656 | * 2) FFA_DENIED if the pages being shared do not have the same mode within |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 657 | * the <to> VM; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 658 | * 3) FFA_INVALID_PARAMETERS if the beginning and end IPAs are not page |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 659 | * aligned; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 660 | * 4) FFA_INVALID_PARAMETERS if the requested share type was not handled. |
| 661 | * Or FFA_SUCCESS on success. |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 662 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 663 | static struct ffa_value ffa_retrieve_check_transition( |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 664 | struct vm_locked to, uint32_t share_func, |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 665 | struct ffa_memory_region_constituent **fragments, |
| 666 | uint32_t *fragment_constituent_counts, uint32_t fragment_count, |
| 667 | uint32_t memory_to_attributes, uint32_t *to_mode) |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 668 | { |
| 669 | uint32_t orig_to_mode; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 670 | struct ffa_value ret; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 671 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 672 | ret = constituents_get_mode(to, &orig_to_mode, fragments, |
| 673 | fragment_constituent_counts, |
| 674 | fragment_count); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 675 | if (ret.func != FFA_SUCCESS_32) { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 676 | dlog_verbose("Inconsistent modes.\n"); |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 677 | return ret; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 680 | if (share_func == FFA_MEM_RECLAIM_32) { |
J-Alves | 9256f16 | 2021-12-09 13:18:43 +0000 | [diff] [blame] | 681 | /* |
| 682 | * If the original ffa memory send call has been processed |
| 683 | * successfully, it is expected the orig_to_mode would overlay |
| 684 | * with `state_mask`, as a result of the function |
| 685 | * `ffa_send_check_transition`. |
| 686 | */ |
Daniel Boulby | 9133dad | 2022-04-25 14:38:44 +0100 | [diff] [blame] | 687 | assert((orig_to_mode & (MM_MODE_INVALID | MM_MODE_UNOWNED | |
| 688 | MM_MODE_SHARED)) != 0U); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 689 | } else { |
| 690 | /* |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 691 | * If the retriever is from virtual FF-A instance: |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 692 | * Ensure the retriever has the expected state. We don't care |
| 693 | * about the MM_MODE_SHARED bit; either with or without it set |
| 694 | * are both valid representations of the !O-NA state. |
| 695 | */ |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 696 | if (vm_id_is_current_world(to.vm->id) && |
| 697 | to.vm->id != HF_PRIMARY_VM_ID && |
| 698 | (orig_to_mode & MM_MODE_UNMAPPED_MASK) != |
| 699 | MM_MODE_UNMAPPED_MASK) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 700 | return ffa_error(FFA_DENIED); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | |
| 704 | /* Find the appropriate new mode. */ |
| 705 | *to_mode = memory_to_attributes; |
| 706 | switch (share_func) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 707 | case FFA_MEM_DONATE_32: |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 708 | *to_mode |= 0; |
| 709 | break; |
| 710 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 711 | case FFA_MEM_LEND_32: |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 712 | *to_mode |= MM_MODE_UNOWNED; |
| 713 | break; |
| 714 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 715 | case FFA_MEM_SHARE_32: |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 716 | *to_mode |= MM_MODE_UNOWNED | MM_MODE_SHARED; |
| 717 | break; |
| 718 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 719 | case FFA_MEM_RECLAIM_32: |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 720 | *to_mode |= 0; |
| 721 | break; |
| 722 | |
| 723 | default: |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 724 | dlog_error("Invalid share_func %#x.\n", share_func); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 725 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 728 | return (struct ffa_value){.func = FFA_SUCCESS_32}; |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 729 | } |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 730 | |
| 731 | /** |
| 732 | * Updates a VM's page table such that the given set of physical address ranges |
| 733 | * are mapped in the address space at the corresponding address ranges, in the |
| 734 | * mode provided. |
| 735 | * |
| 736 | * If commit is false, the page tables will be allocated from the mpool but no |
| 737 | * mappings will actually be updated. This function must always be called first |
| 738 | * with commit false to check that it will succeed before calling with commit |
| 739 | * true, to avoid leaving the page table in a half-updated state. To make a |
| 740 | * series of changes atomically you can call them all with commit false before |
| 741 | * calling them all with commit true. |
| 742 | * |
Raghu Krishnamurthy | 7ad3d14 | 2021-03-28 00:47:35 -0700 | [diff] [blame] | 743 | * vm_ptable_defrag should always be called after a series of page table |
| 744 | * updates, whether they succeed or fail. |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 745 | * |
| 746 | * Returns true on success, or false if the update failed and no changes were |
| 747 | * made to memory mappings. |
| 748 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 749 | static bool ffa_region_group_identity_map( |
Andrew Walbran | f4b51af | 2020-02-03 14:44:54 +0000 | [diff] [blame] | 750 | struct vm_locked vm_locked, |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 751 | struct ffa_memory_region_constituent **fragments, |
| 752 | const uint32_t *fragment_constituent_counts, uint32_t fragment_count, |
Daniel Boulby | 4dd3f53 | 2021-09-21 09:57:08 +0100 | [diff] [blame] | 753 | uint32_t mode, struct mpool *ppool, bool commit) |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 754 | { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 755 | uint32_t i; |
| 756 | uint32_t j; |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 757 | |
Raghu Krishnamurthy | 7ad3d14 | 2021-03-28 00:47:35 -0700 | [diff] [blame] | 758 | if (vm_locked.vm->el0_partition) { |
| 759 | mode |= MM_MODE_USER | MM_MODE_NG; |
| 760 | } |
| 761 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 762 | /* Iterate over the memory region constituents within each fragment. */ |
| 763 | for (i = 0; i < fragment_count; ++i) { |
| 764 | for (j = 0; j < fragment_constituent_counts[i]; ++j) { |
| 765 | size_t size = fragments[i][j].page_count * PAGE_SIZE; |
| 766 | paddr_t pa_begin = |
| 767 | pa_from_ipa(ipa_init(fragments[i][j].address)); |
| 768 | paddr_t pa_end = pa_add(pa_begin, size); |
Jens Wiklander | 4f1880c | 2022-10-19 17:00:14 +0200 | [diff] [blame] | 769 | uint32_t pa_bits = |
| 770 | arch_mm_get_pa_bits(arch_mm_get_pa_range()); |
Federico Recanati | 4fd065d | 2021-12-13 20:06:23 +0100 | [diff] [blame] | 771 | |
| 772 | /* |
| 773 | * Ensure the requested region falls into system's PA |
| 774 | * range. |
| 775 | */ |
Jens Wiklander | 4f1880c | 2022-10-19 17:00:14 +0200 | [diff] [blame] | 776 | if (((pa_addr(pa_begin) >> pa_bits) > 0) || |
| 777 | ((pa_addr(pa_end) >> pa_bits) > 0)) { |
Federico Recanati | 4fd065d | 2021-12-13 20:06:23 +0100 | [diff] [blame] | 778 | dlog_error("Region is outside of PA Range\n"); |
| 779 | return false; |
| 780 | } |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 781 | |
| 782 | if (commit) { |
| 783 | vm_identity_commit(vm_locked, pa_begin, pa_end, |
| 784 | mode, ppool, NULL); |
| 785 | } else if (!vm_identity_prepare(vm_locked, pa_begin, |
| 786 | pa_end, mode, ppool)) { |
| 787 | return false; |
| 788 | } |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | |
| 792 | return true; |
| 793 | } |
| 794 | |
| 795 | /** |
| 796 | * Clears a region of physical memory by overwriting it with zeros. The data is |
| 797 | * flushed from the cache so the memory has been cleared across the system. |
| 798 | */ |
J-Alves | 7db3200 | 2021-12-14 14:44:50 +0000 | [diff] [blame] | 799 | static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool, |
| 800 | uint32_t extra_mode_attributes) |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 801 | { |
| 802 | /* |
Fuad Tabba | ed294af | 2019-12-20 10:43:01 +0000 | [diff] [blame] | 803 | * TODO: change this to a CPU local single page window rather than a |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 804 | * global mapping of the whole range. Such an approach will limit |
| 805 | * the changes to stage-1 tables and will allow only local |
| 806 | * invalidation. |
| 807 | */ |
| 808 | bool ret; |
| 809 | struct mm_stage1_locked stage1_locked = mm_lock_stage1(); |
J-Alves | 7db3200 | 2021-12-14 14:44:50 +0000 | [diff] [blame] | 810 | void *ptr = mm_identity_map(stage1_locked, begin, end, |
| 811 | MM_MODE_W | (extra_mode_attributes & |
| 812 | plat_ffa_other_world_mode()), |
| 813 | ppool); |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 814 | size_t size = pa_difference(begin, end); |
| 815 | |
| 816 | if (!ptr) { |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 817 | goto fail; |
| 818 | } |
| 819 | |
| 820 | memset_s(ptr, size, 0, size); |
| 821 | arch_mm_flush_dcache(ptr, size); |
| 822 | mm_unmap(stage1_locked, begin, end, ppool); |
| 823 | |
| 824 | ret = true; |
| 825 | goto out; |
| 826 | |
| 827 | fail: |
| 828 | ret = false; |
| 829 | |
| 830 | out: |
| 831 | mm_unlock_stage1(&stage1_locked); |
| 832 | |
| 833 | return ret; |
| 834 | } |
| 835 | |
| 836 | /** |
| 837 | * Clears a region of physical memory by overwriting it with zeros. The data is |
| 838 | * flushed from the cache so the memory has been cleared across the system. |
| 839 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 840 | static bool ffa_clear_memory_constituents( |
J-Alves | 7db3200 | 2021-12-14 14:44:50 +0000 | [diff] [blame] | 841 | uint32_t security_state_mode, |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 842 | struct ffa_memory_region_constituent **fragments, |
| 843 | const uint32_t *fragment_constituent_counts, uint32_t fragment_count, |
| 844 | struct mpool *page_pool) |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 845 | { |
| 846 | struct mpool local_page_pool; |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 847 | uint32_t i; |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 848 | bool ret = false; |
| 849 | |
| 850 | /* |
| 851 | * Create a local pool so any freed memory can't be used by another |
| 852 | * thread. This is to ensure each constituent that is mapped can be |
| 853 | * unmapped again afterwards. |
| 854 | */ |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 855 | mpool_init_with_fallback(&local_page_pool, page_pool); |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 856 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 857 | /* Iterate over the memory region constituents within each fragment. */ |
| 858 | for (i = 0; i < fragment_count; ++i) { |
| 859 | uint32_t j; |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 860 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 861 | for (j = 0; j < fragment_constituent_counts[j]; ++j) { |
| 862 | size_t size = fragments[i][j].page_count * PAGE_SIZE; |
| 863 | paddr_t begin = |
| 864 | pa_from_ipa(ipa_init(fragments[i][j].address)); |
| 865 | paddr_t end = pa_add(begin, size); |
| 866 | |
J-Alves | 7db3200 | 2021-12-14 14:44:50 +0000 | [diff] [blame] | 867 | if (!clear_memory(begin, end, &local_page_pool, |
| 868 | security_state_mode)) { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 869 | /* |
| 870 | * api_clear_memory will defrag on failure, so |
| 871 | * no need to do it here. |
| 872 | */ |
| 873 | goto out; |
| 874 | } |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 875 | } |
| 876 | } |
| 877 | |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 878 | ret = true; |
| 879 | |
| 880 | out: |
| 881 | mpool_fini(&local_page_pool); |
| 882 | return ret; |
| 883 | } |
| 884 | |
| 885 | /** |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 886 | * Validates and prepares memory to be sent from the calling VM to another. |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 887 | * |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 888 | * This function requires the calling context to hold the <from> VM lock. |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 889 | * |
| 890 | * Returns: |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 891 | * In case of error, one of the following values is returned: |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 892 | * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 893 | * erroneous; |
Andrew Walbran | f07f04d | 2020-05-01 18:09:00 +0100 | [diff] [blame] | 894 | * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete the |
| 895 | * request. |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 896 | * 3) FFA_DENIED - The sender doesn't have sufficient access to send the |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 897 | * memory with the given permissions. |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 898 | * Success is indicated by FFA_SUCCESS. |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 899 | */ |
Andrew Walbran | 996d1d1 | 2020-05-27 14:08:43 +0100 | [diff] [blame] | 900 | static struct ffa_value ffa_send_check_update( |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 901 | struct vm_locked from_locked, |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 902 | struct ffa_memory_region_constituent **fragments, |
| 903 | uint32_t *fragment_constituent_counts, uint32_t fragment_count, |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 904 | uint32_t share_func, struct ffa_memory_access *receivers, |
| 905 | uint32_t receivers_count, struct mpool *page_pool, bool clear, |
| 906 | uint32_t *orig_from_mode_ret) |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 907 | { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 908 | uint32_t i; |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 909 | uint32_t orig_from_mode; |
| 910 | uint32_t from_mode; |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 911 | struct mpool local_page_pool; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 912 | struct ffa_value ret; |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 913 | |
| 914 | /* |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 915 | * Make sure constituents are properly aligned to a 64-bit boundary. If |
| 916 | * not we would get alignment faults trying to read (64-bit) values. |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 917 | */ |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 918 | for (i = 0; i < fragment_count; ++i) { |
| 919 | if (!is_aligned(fragments[i], 8)) { |
| 920 | dlog_verbose("Constituents not aligned.\n"); |
| 921 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 922 | } |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | /* |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 926 | * Check if the state transition is lawful for the sender, ensure that |
| 927 | * all constituents of a memory region being shared are at the same |
| 928 | * state. |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 929 | */ |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 930 | ret = ffa_send_check_transition(from_locked, share_func, receivers, |
| 931 | receivers_count, &orig_from_mode, |
| 932 | fragments, fragment_constituent_counts, |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 933 | fragment_count, &from_mode); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 934 | if (ret.func != FFA_SUCCESS_32) { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 935 | dlog_verbose("Invalid transition for send.\n"); |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 936 | return ret; |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 937 | } |
| 938 | |
Andrew Walbran | 37c574e | 2020-06-03 11:45:46 +0100 | [diff] [blame] | 939 | if (orig_from_mode_ret != NULL) { |
| 940 | *orig_from_mode_ret = orig_from_mode; |
| 941 | } |
| 942 | |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 943 | /* |
| 944 | * Create a local pool so any freed memory can't be used by another |
| 945 | * thread. This is to ensure the original mapping can be restored if the |
| 946 | * clear fails. |
| 947 | */ |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 948 | mpool_init_with_fallback(&local_page_pool, page_pool); |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 949 | |
| 950 | /* |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 951 | * First reserve all required memory for the new page table entries |
| 952 | * without committing, to make sure the entire operation will succeed |
| 953 | * without exhausting the page pool. |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 954 | */ |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 955 | if (!ffa_region_group_identity_map( |
| 956 | from_locked, fragments, fragment_constituent_counts, |
| 957 | fragment_count, from_mode, page_pool, false)) { |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 958 | /* TODO: partial defrag of failed range. */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 959 | ret = ffa_error(FFA_NO_MEMORY); |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 960 | goto out; |
| 961 | } |
| 962 | |
| 963 | /* |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 964 | * Update the mapping for the sender. This won't allocate because the |
| 965 | * transaction was already prepared above, but may free pages in the |
| 966 | * case that a whole block is being unmapped that was previously |
| 967 | * partially mapped. |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 968 | */ |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 969 | CHECK(ffa_region_group_identity_map( |
| 970 | from_locked, fragments, fragment_constituent_counts, |
| 971 | fragment_count, from_mode, &local_page_pool, true)); |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 972 | |
| 973 | /* Clear the memory so no VM or device can see the previous contents. */ |
J-Alves | 7db3200 | 2021-12-14 14:44:50 +0000 | [diff] [blame] | 974 | if (clear && |
| 975 | !ffa_clear_memory_constituents( |
| 976 | plat_ffa_owner_world_mode(from_locked.vm->id), fragments, |
| 977 | fragment_constituent_counts, fragment_count, page_pool)) { |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 978 | /* |
| 979 | * On failure, roll back by returning memory to the sender. This |
| 980 | * may allocate pages which were previously freed into |
| 981 | * `local_page_pool` by the call above, but will never allocate |
| 982 | * more pages than that so can never fail. |
| 983 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 984 | CHECK(ffa_region_group_identity_map( |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 985 | from_locked, fragments, fragment_constituent_counts, |
| 986 | fragment_count, orig_from_mode, &local_page_pool, |
| 987 | true)); |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 988 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 989 | ret = ffa_error(FFA_NO_MEMORY); |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 990 | goto out; |
| 991 | } |
| 992 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 993 | ret = (struct ffa_value){.func = FFA_SUCCESS_32}; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 994 | |
| 995 | out: |
| 996 | mpool_fini(&local_page_pool); |
| 997 | |
| 998 | /* |
| 999 | * Tidy up the page table by reclaiming failed mappings (if there was an |
| 1000 | * error) or merging entries into blocks where possible (on success). |
| 1001 | */ |
Raghu Krishnamurthy | 7ad3d14 | 2021-03-28 00:47:35 -0700 | [diff] [blame] | 1002 | vm_ptable_defrag(from_locked, page_pool); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1003 | |
| 1004 | return ret; |
| 1005 | } |
| 1006 | |
| 1007 | /** |
| 1008 | * Validates and maps memory shared from one VM to another. |
| 1009 | * |
| 1010 | * This function requires the calling context to hold the <to> lock. |
| 1011 | * |
| 1012 | * Returns: |
| 1013 | * In case of error, one of the following values is returned: |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1014 | * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1015 | * erroneous; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1016 | * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1017 | * the request. |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1018 | * Success is indicated by FFA_SUCCESS. |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1019 | */ |
Andrew Walbran | 996d1d1 | 2020-05-27 14:08:43 +0100 | [diff] [blame] | 1020 | static struct ffa_value ffa_retrieve_check_update( |
J-Alves | 7db3200 | 2021-12-14 14:44:50 +0000 | [diff] [blame] | 1021 | struct vm_locked to_locked, ffa_vm_id_t from_id, |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1022 | struct ffa_memory_region_constituent **fragments, |
| 1023 | uint32_t *fragment_constituent_counts, uint32_t fragment_count, |
| 1024 | uint32_t memory_to_attributes, uint32_t share_func, bool clear, |
| 1025 | struct mpool *page_pool) |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1026 | { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1027 | uint32_t i; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1028 | uint32_t to_mode; |
| 1029 | struct mpool local_page_pool; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1030 | struct ffa_value ret; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1031 | |
| 1032 | /* |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1033 | * Make sure constituents are properly aligned to a 64-bit boundary. If |
| 1034 | * not we would get alignment faults trying to read (64-bit) values. |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1035 | */ |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1036 | for (i = 0; i < fragment_count; ++i) { |
| 1037 | if (!is_aligned(fragments[i], 8)) { |
| 1038 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1039 | } |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | /* |
| 1043 | * Check if the state transition is lawful for the recipient, and ensure |
| 1044 | * that all constituents of the memory region being retrieved are at the |
| 1045 | * same state. |
| 1046 | */ |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1047 | ret = ffa_retrieve_check_transition( |
| 1048 | to_locked, share_func, fragments, fragment_constituent_counts, |
| 1049 | fragment_count, memory_to_attributes, &to_mode); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1050 | if (ret.func != FFA_SUCCESS_32) { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1051 | dlog_verbose("Invalid transition for retrieve.\n"); |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1052 | return ret; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | /* |
| 1056 | * Create a local pool so any freed memory can't be used by another |
| 1057 | * thread. This is to ensure the original mapping can be restored if the |
| 1058 | * clear fails. |
| 1059 | */ |
| 1060 | mpool_init_with_fallback(&local_page_pool, page_pool); |
| 1061 | |
| 1062 | /* |
| 1063 | * First reserve all required memory for the new page table entries in |
| 1064 | * the recipient page tables without committing, to make sure the entire |
| 1065 | * operation will succeed without exhausting the page pool. |
| 1066 | */ |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1067 | if (!ffa_region_group_identity_map( |
| 1068 | to_locked, fragments, fragment_constituent_counts, |
| 1069 | fragment_count, to_mode, page_pool, false)) { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1070 | /* TODO: partial defrag of failed range. */ |
| 1071 | dlog_verbose( |
| 1072 | "Insufficient memory to update recipient page " |
| 1073 | "table.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1074 | ret = ffa_error(FFA_NO_MEMORY); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1075 | goto out; |
| 1076 | } |
| 1077 | |
| 1078 | /* Clear the memory so no VM or device can see the previous contents. */ |
J-Alves | 7db3200 | 2021-12-14 14:44:50 +0000 | [diff] [blame] | 1079 | if (clear && |
| 1080 | !ffa_clear_memory_constituents( |
| 1081 | plat_ffa_owner_world_mode(from_id), fragments, |
| 1082 | fragment_constituent_counts, fragment_count, page_pool)) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1083 | ret = ffa_error(FFA_NO_MEMORY); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1084 | goto out; |
| 1085 | } |
| 1086 | |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 1087 | /* |
| 1088 | * Complete the transfer by mapping the memory into the recipient. This |
| 1089 | * won't allocate because the transaction was already prepared above, so |
| 1090 | * it doesn't need to use the `local_page_pool`. |
| 1091 | */ |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1092 | CHECK(ffa_region_group_identity_map( |
| 1093 | to_locked, fragments, fragment_constituent_counts, |
| 1094 | fragment_count, to_mode, page_pool, true)); |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 1095 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1096 | ret = (struct ffa_value){.func = FFA_SUCCESS_32}; |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 1097 | |
| 1098 | out: |
| 1099 | mpool_fini(&local_page_pool); |
| 1100 | |
| 1101 | /* |
Andrew Walbran | f07f04d | 2020-05-01 18:09:00 +0100 | [diff] [blame] | 1102 | * Tidy up the page table by reclaiming failed mappings (if there was an |
| 1103 | * error) or merging entries into blocks where possible (on success). |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 1104 | */ |
Raghu Krishnamurthy | 7ad3d14 | 2021-03-28 00:47:35 -0700 | [diff] [blame] | 1105 | vm_ptable_defrag(to_locked, page_pool); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1106 | |
| 1107 | return ret; |
| 1108 | } |
| 1109 | |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 1110 | /** |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1111 | * Reclaims the given memory from the other world. To do this space is first |
| 1112 | * reserved in the <to> VM's page table, then the reclaim request is sent on to |
| 1113 | * the other world. then (if that is successful) the memory is mapped back into |
| 1114 | * the <to> VM's page table. |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 1115 | * |
| 1116 | * This function requires the calling context to hold the <to> lock. |
| 1117 | * |
| 1118 | * Returns: |
| 1119 | * In case of error, one of the following values is returned: |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1120 | * 1) FFA_INVALID_PARAMETERS - The endpoint provided parameters were |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 1121 | * erroneous; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1122 | * 2) FFA_NO_MEMORY - Hafnium did not have sufficient memory to complete |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 1123 | * the request. |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1124 | * Success is indicated by FFA_SUCCESS. |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 1125 | */ |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1126 | static struct ffa_value ffa_other_world_reclaim_check_update( |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1127 | struct vm_locked to_locked, ffa_memory_handle_t handle, |
| 1128 | struct ffa_memory_region_constituent *constituents, |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 1129 | uint32_t constituent_count, uint32_t memory_to_attributes, bool clear, |
| 1130 | struct mpool *page_pool) |
| 1131 | { |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 1132 | uint32_t to_mode; |
| 1133 | struct mpool local_page_pool; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1134 | struct ffa_value ret; |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1135 | ffa_memory_region_flags_t other_world_flags; |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 1136 | |
| 1137 | /* |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1138 | * Make sure constituents are properly aligned to a 64-bit boundary. If |
| 1139 | * not we would get alignment faults trying to read (64-bit) values. |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 1140 | */ |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1141 | if (!is_aligned(constituents, 8)) { |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 1142 | dlog_verbose("Constituents not aligned.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1143 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | /* |
| 1147 | * Check if the state transition is lawful for the recipient, and ensure |
| 1148 | * that all constituents of the memory region being retrieved are at the |
| 1149 | * same state. |
| 1150 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1151 | ret = ffa_retrieve_check_transition(to_locked, FFA_MEM_RECLAIM_32, |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1152 | &constituents, &constituent_count, |
| 1153 | 1, memory_to_attributes, &to_mode); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1154 | if (ret.func != FFA_SUCCESS_32) { |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 1155 | dlog_verbose("Invalid transition.\n"); |
| 1156 | return ret; |
| 1157 | } |
| 1158 | |
| 1159 | /* |
| 1160 | * Create a local pool so any freed memory can't be used by another |
| 1161 | * thread. This is to ensure the original mapping can be restored if the |
| 1162 | * clear fails. |
| 1163 | */ |
| 1164 | mpool_init_with_fallback(&local_page_pool, page_pool); |
| 1165 | |
| 1166 | /* |
| 1167 | * First reserve all required memory for the new page table entries in |
| 1168 | * the recipient page tables without committing, to make sure the entire |
| 1169 | * operation will succeed without exhausting the page pool. |
| 1170 | */ |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1171 | if (!ffa_region_group_identity_map(to_locked, &constituents, |
| 1172 | &constituent_count, 1, to_mode, |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1173 | page_pool, false)) { |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 1174 | /* TODO: partial defrag of failed range. */ |
| 1175 | dlog_verbose( |
| 1176 | "Insufficient memory to update recipient page " |
| 1177 | "table.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1178 | ret = ffa_error(FFA_NO_MEMORY); |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 1179 | goto out; |
| 1180 | } |
| 1181 | |
| 1182 | /* |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 1183 | * Forward the request to the other world, check if SPMC returned |
| 1184 | * FFA_SUCCESS_32. If not, terminate and return the error to caller |
| 1185 | * VM. |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 1186 | */ |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1187 | other_world_flags = 0; |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 1188 | if (clear) { |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1189 | other_world_flags |= FFA_MEMORY_REGION_FLAG_CLEAR; |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 1190 | } |
Olivier Deprez | 112d2b5 | 2020-09-30 07:39:23 +0200 | [diff] [blame] | 1191 | ret = arch_other_world_call( |
| 1192 | (struct ffa_value){.func = FFA_MEM_RECLAIM_32, |
| 1193 | .arg1 = (uint32_t)handle, |
| 1194 | .arg2 = (uint32_t)(handle >> 32), |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1195 | .arg3 = other_world_flags}); |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 1196 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1197 | if (ret.func != FFA_SUCCESS_32) { |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 1198 | dlog_verbose( |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1199 | "Got %#x (%d) from other world in response to " |
| 1200 | "FFA_MEM_RECLAIM, " |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1201 | "expected FFA_SUCCESS.\n", |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 1202 | ret.func, ret.arg2); |
| 1203 | goto out; |
| 1204 | } |
| 1205 | |
| 1206 | /* |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1207 | * The other world was happy with it, so complete the reclaim by mapping |
| 1208 | * the memory into the recipient. This won't allocate because the |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 1209 | * transaction was already prepared above, so it doesn't need to use the |
| 1210 | * `local_page_pool`. |
| 1211 | */ |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1212 | CHECK(ffa_region_group_identity_map(to_locked, &constituents, |
| 1213 | &constituent_count, 1, to_mode, |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1214 | page_pool, true)); |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 1215 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1216 | ret = (struct ffa_value){.func = FFA_SUCCESS_32}; |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 1217 | |
| 1218 | out: |
| 1219 | mpool_fini(&local_page_pool); |
| 1220 | |
| 1221 | /* |
Andrew Walbran | f07f04d | 2020-05-01 18:09:00 +0100 | [diff] [blame] | 1222 | * Tidy up the page table by reclaiming failed mappings (if there was an |
| 1223 | * error) or merging entries into blocks where possible (on success). |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 1224 | */ |
Raghu Krishnamurthy | 7ad3d14 | 2021-03-28 00:47:35 -0700 | [diff] [blame] | 1225 | vm_ptable_defrag(to_locked, page_pool); |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 1226 | |
| 1227 | return ret; |
| 1228 | } |
| 1229 | |
Andrew Walbran | 996d1d1 | 2020-05-27 14:08:43 +0100 | [diff] [blame] | 1230 | static struct ffa_value ffa_relinquish_check_update( |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1231 | struct vm_locked from_locked, |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1232 | struct ffa_memory_region_constituent **fragments, |
| 1233 | uint32_t *fragment_constituent_counts, uint32_t fragment_count, |
| 1234 | struct mpool *page_pool, bool clear) |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1235 | { |
| 1236 | uint32_t orig_from_mode; |
| 1237 | uint32_t from_mode; |
| 1238 | struct mpool local_page_pool; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1239 | struct ffa_value ret; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1240 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1241 | ret = ffa_relinquish_check_transition( |
| 1242 | from_locked, &orig_from_mode, fragments, |
| 1243 | fragment_constituent_counts, fragment_count, &from_mode); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1244 | if (ret.func != FFA_SUCCESS_32) { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1245 | dlog_verbose("Invalid transition for relinquish.\n"); |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1246 | return ret; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | /* |
| 1250 | * Create a local pool so any freed memory can't be used by another |
| 1251 | * thread. This is to ensure the original mapping can be restored if the |
| 1252 | * clear fails. |
| 1253 | */ |
| 1254 | mpool_init_with_fallback(&local_page_pool, page_pool); |
| 1255 | |
| 1256 | /* |
| 1257 | * First reserve all required memory for the new page table entries |
| 1258 | * without committing, to make sure the entire operation will succeed |
| 1259 | * without exhausting the page pool. |
| 1260 | */ |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1261 | if (!ffa_region_group_identity_map( |
| 1262 | from_locked, fragments, fragment_constituent_counts, |
| 1263 | fragment_count, from_mode, page_pool, false)) { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1264 | /* TODO: partial defrag of failed range. */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1265 | ret = ffa_error(FFA_NO_MEMORY); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1266 | goto out; |
| 1267 | } |
| 1268 | |
| 1269 | /* |
| 1270 | * Update the mapping for the sender. This won't allocate because the |
| 1271 | * transaction was already prepared above, but may free pages in the |
| 1272 | * case that a whole block is being unmapped that was previously |
| 1273 | * partially mapped. |
| 1274 | */ |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1275 | CHECK(ffa_region_group_identity_map( |
| 1276 | from_locked, fragments, fragment_constituent_counts, |
| 1277 | fragment_count, from_mode, &local_page_pool, true)); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1278 | |
| 1279 | /* Clear the memory so no VM or device can see the previous contents. */ |
J-Alves | 7db3200 | 2021-12-14 14:44:50 +0000 | [diff] [blame] | 1280 | if (clear && |
| 1281 | !ffa_clear_memory_constituents( |
| 1282 | plat_ffa_owner_world_mode(from_locked.vm->id), fragments, |
| 1283 | fragment_constituent_counts, fragment_count, page_pool)) { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1284 | /* |
| 1285 | * On failure, roll back by returning memory to the sender. This |
| 1286 | * may allocate pages which were previously freed into |
| 1287 | * `local_page_pool` by the call above, but will never allocate |
| 1288 | * more pages than that so can never fail. |
| 1289 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1290 | CHECK(ffa_region_group_identity_map( |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1291 | from_locked, fragments, fragment_constituent_counts, |
| 1292 | fragment_count, orig_from_mode, &local_page_pool, |
| 1293 | true)); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1294 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1295 | ret = ffa_error(FFA_NO_MEMORY); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1296 | goto out; |
| 1297 | } |
| 1298 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1299 | ret = (struct ffa_value){.func = FFA_SUCCESS_32}; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1300 | |
| 1301 | out: |
| 1302 | mpool_fini(&local_page_pool); |
| 1303 | |
| 1304 | /* |
| 1305 | * Tidy up the page table by reclaiming failed mappings (if there was an |
| 1306 | * error) or merging entries into blocks where possible (on success). |
| 1307 | */ |
Raghu Krishnamurthy | 7ad3d14 | 2021-03-28 00:47:35 -0700 | [diff] [blame] | 1308 | vm_ptable_defrag(from_locked, page_pool); |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 1309 | |
| 1310 | return ret; |
| 1311 | } |
| 1312 | |
| 1313 | /** |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1314 | * Complete a memory sending operation by checking that it is valid, updating |
| 1315 | * the sender page table, and then either marking the share state as having |
| 1316 | * completed sending (on success) or freeing it (on failure). |
| 1317 | * |
| 1318 | * Returns FFA_SUCCESS with the handle encoded, or the relevant FFA_ERROR. |
| 1319 | */ |
| 1320 | static struct ffa_value ffa_memory_send_complete( |
| 1321 | struct vm_locked from_locked, struct share_states_locked share_states, |
Andrew Walbran | 37c574e | 2020-06-03 11:45:46 +0100 | [diff] [blame] | 1322 | struct ffa_memory_share_state *share_state, struct mpool *page_pool, |
| 1323 | uint32_t *orig_from_mode_ret) |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1324 | { |
| 1325 | struct ffa_memory_region *memory_region = share_state->memory_region; |
| 1326 | struct ffa_value ret; |
| 1327 | |
| 1328 | /* Lock must be held. */ |
Daniel Boulby | a2f8c66 | 2021-11-26 17:52:53 +0000 | [diff] [blame] | 1329 | assert(share_states.share_states != NULL); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1330 | |
| 1331 | /* Check that state is valid in sender page table and update. */ |
| 1332 | ret = ffa_send_check_update( |
| 1333 | from_locked, share_state->fragments, |
| 1334 | share_state->fragment_constituent_counts, |
| 1335 | share_state->fragment_count, share_state->share_func, |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 1336 | memory_region->receivers, memory_region->receiver_count, |
Andrew Walbran | 37c574e | 2020-06-03 11:45:46 +0100 | [diff] [blame] | 1337 | page_pool, memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR, |
| 1338 | orig_from_mode_ret); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1339 | if (ret.func != FFA_SUCCESS_32) { |
| 1340 | /* |
| 1341 | * Free share state, it failed to send so it can't be retrieved. |
| 1342 | */ |
| 1343 | dlog_verbose("Complete failed, freeing share state.\n"); |
| 1344 | share_state_free(share_states, share_state, page_pool); |
| 1345 | return ret; |
| 1346 | } |
| 1347 | |
| 1348 | share_state->sending_complete = true; |
| 1349 | dlog_verbose("Marked sending complete.\n"); |
| 1350 | |
J-Alves | ee68c54 | 2020-10-29 17:48:20 +0000 | [diff] [blame] | 1351 | return ffa_mem_success(share_state->memory_region->handle); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | /** |
Federico Recanati | a98603a | 2021-12-20 18:04:03 +0100 | [diff] [blame] | 1355 | * Check that the memory attributes match Hafnium expectations: |
| 1356 | * Normal Memory, Inner shareable, Write-Back Read-Allocate |
| 1357 | * Write-Allocate Cacheable. |
| 1358 | */ |
| 1359 | static struct ffa_value ffa_memory_attributes_validate( |
| 1360 | ffa_memory_access_permissions_t attributes) |
| 1361 | { |
| 1362 | enum ffa_memory_type memory_type; |
| 1363 | enum ffa_memory_cacheability cacheability; |
| 1364 | enum ffa_memory_shareability shareability; |
| 1365 | |
| 1366 | memory_type = ffa_get_memory_type_attr(attributes); |
| 1367 | if (memory_type != FFA_MEMORY_NORMAL_MEM) { |
| 1368 | dlog_verbose("Invalid memory type %#x, expected %#x.\n", |
| 1369 | memory_type, FFA_MEMORY_NORMAL_MEM); |
Federico Recanati | 3d953f3 | 2022-02-17 09:31:29 +0100 | [diff] [blame] | 1370 | return ffa_error(FFA_DENIED); |
Federico Recanati | a98603a | 2021-12-20 18:04:03 +0100 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | cacheability = ffa_get_memory_cacheability_attr(attributes); |
| 1374 | if (cacheability != FFA_MEMORY_CACHE_WRITE_BACK) { |
| 1375 | dlog_verbose("Invalid cacheability %#x, expected %#x.\n", |
| 1376 | cacheability, FFA_MEMORY_CACHE_WRITE_BACK); |
Federico Recanati | 3d953f3 | 2022-02-17 09:31:29 +0100 | [diff] [blame] | 1377 | return ffa_error(FFA_DENIED); |
Federico Recanati | a98603a | 2021-12-20 18:04:03 +0100 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | shareability = ffa_get_memory_shareability_attr(attributes); |
| 1381 | if (shareability != FFA_MEMORY_INNER_SHAREABLE) { |
| 1382 | dlog_verbose("Invalid shareability %#x, expected #%x.\n", |
| 1383 | shareability, FFA_MEMORY_INNER_SHAREABLE); |
Federico Recanati | 3d953f3 | 2022-02-17 09:31:29 +0100 | [diff] [blame] | 1384 | return ffa_error(FFA_DENIED); |
Federico Recanati | a98603a | 2021-12-20 18:04:03 +0100 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | return (struct ffa_value){.func = FFA_SUCCESS_32}; |
| 1388 | } |
| 1389 | |
| 1390 | /** |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1391 | * Check that the given `memory_region` represents a valid memory send request |
| 1392 | * of the given `share_func` type, return the clear flag and permissions via the |
| 1393 | * respective output parameters, and update the permissions if necessary. |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1394 | * |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1395 | * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1396 | * not. |
| 1397 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1398 | static struct ffa_value ffa_memory_send_validate( |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1399 | struct vm_locked from_locked, struct ffa_memory_region *memory_region, |
| 1400 | uint32_t memory_share_length, uint32_t fragment_length, |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 1401 | uint32_t share_func) |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1402 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1403 | struct ffa_composite_memory_region *composite; |
Andrew Walbran | 130a8ae | 2020-05-15 16:27:15 +0100 | [diff] [blame] | 1404 | uint32_t receivers_length; |
Federico Recanati | 872cd69 | 2022-01-05 13:10:10 +0100 | [diff] [blame] | 1405 | uint32_t composite_memory_region_offset; |
Andrew Walbran | 352aa3d | 2020-05-01 17:51:33 +0100 | [diff] [blame] | 1406 | uint32_t constituents_offset; |
Andrew Walbran | 130a8ae | 2020-05-15 16:27:15 +0100 | [diff] [blame] | 1407 | uint32_t constituents_length; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1408 | enum ffa_data_access data_access; |
| 1409 | enum ffa_instruction_access instruction_access; |
Federico Recanati | a98603a | 2021-12-20 18:04:03 +0100 | [diff] [blame] | 1410 | struct ffa_value ret; |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1411 | |
J-Alves | 95df0ef | 2022-12-07 10:09:48 +0000 | [diff] [blame] | 1412 | /* The sender must match the caller. */ |
| 1413 | if ((!vm_id_is_current_world(from_locked.vm->id) && |
| 1414 | vm_id_is_current_world(memory_region->sender)) || |
| 1415 | (vm_id_is_current_world(from_locked.vm->id) && |
| 1416 | memory_region->sender != from_locked.vm->id)) { |
| 1417 | dlog_verbose("Invalid memory sender ID.\n"); |
| 1418 | return ffa_error(FFA_DENIED); |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1419 | } |
| 1420 | |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1421 | /* |
| 1422 | * Ensure that the composite header is within the memory bounds and |
| 1423 | * doesn't overlap the first part of the message. |
| 1424 | */ |
Andrew Walbran | 130a8ae | 2020-05-15 16:27:15 +0100 | [diff] [blame] | 1425 | receivers_length = sizeof(struct ffa_memory_access) * |
| 1426 | memory_region->receiver_count; |
Andrew Walbran | 352aa3d | 2020-05-01 17:51:33 +0100 | [diff] [blame] | 1427 | constituents_offset = |
| 1428 | ffa_composite_constituent_offset(memory_region, 0); |
Federico Recanati | 872cd69 | 2022-01-05 13:10:10 +0100 | [diff] [blame] | 1429 | composite_memory_region_offset = |
| 1430 | memory_region->receivers[0].composite_memory_region_offset; |
| 1431 | if ((composite_memory_region_offset == 0) || |
| 1432 | (composite_memory_region_offset < |
| 1433 | sizeof(struct ffa_memory_region) + receivers_length) || |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1434 | constituents_offset > fragment_length) { |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1435 | dlog_verbose( |
Andrew Walbran | 352aa3d | 2020-05-01 17:51:33 +0100 | [diff] [blame] | 1436 | "Invalid composite memory region descriptor offset " |
| 1437 | "%d.\n", |
| 1438 | memory_region->receivers[0] |
| 1439 | .composite_memory_region_offset); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1440 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1441 | } |
| 1442 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1443 | composite = ffa_memory_region_get_composite(memory_region, 0); |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1444 | |
| 1445 | /* |
Andrew Walbran | f07f04d | 2020-05-01 18:09:00 +0100 | [diff] [blame] | 1446 | * Ensure the number of constituents are within the memory bounds. |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1447 | */ |
Andrew Walbran | 130a8ae | 2020-05-15 16:27:15 +0100 | [diff] [blame] | 1448 | constituents_length = sizeof(struct ffa_memory_region_constituent) * |
| 1449 | composite->constituent_count; |
Andrew Walbran | 352aa3d | 2020-05-01 17:51:33 +0100 | [diff] [blame] | 1450 | if (memory_share_length != constituents_offset + constituents_length) { |
| 1451 | dlog_verbose("Invalid length %d or composite offset %d.\n", |
Andrew Walbran | 130a8ae | 2020-05-15 16:27:15 +0100 | [diff] [blame] | 1452 | memory_share_length, |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1453 | memory_region->receivers[0] |
| 1454 | .composite_memory_region_offset); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1455 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1456 | } |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1457 | if (fragment_length < memory_share_length && |
| 1458 | fragment_length < HF_MAILBOX_SIZE) { |
| 1459 | dlog_warning( |
| 1460 | "Initial fragment length %d smaller than mailbox " |
| 1461 | "size.\n", |
| 1462 | fragment_length); |
| 1463 | } |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1464 | |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1465 | /* |
| 1466 | * Clear is not allowed for memory sharing, as the sender still has |
| 1467 | * access to the memory. |
| 1468 | */ |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1469 | if ((memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) && |
| 1470 | share_func == FFA_MEM_SHARE_32) { |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1471 | dlog_verbose("Memory can't be cleared while being shared.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1472 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | /* No other flags are allowed/supported here. */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1476 | if (memory_region->flags & ~FFA_MEMORY_REGION_FLAG_CLEAR) { |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1477 | dlog_verbose("Invalid flags %#x.\n", memory_region->flags); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1478 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1479 | } |
| 1480 | |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 1481 | /* Check that the permissions are valid, for each specified receiver. */ |
| 1482 | for (uint32_t i = 0U; i < memory_region->receiver_count; i++) { |
| 1483 | ffa_memory_access_permissions_t permissions = |
| 1484 | memory_region->receivers[i] |
| 1485 | .receiver_permissions.permissions; |
| 1486 | ffa_vm_id_t receiver_id = |
| 1487 | memory_region->receivers[i] |
| 1488 | .receiver_permissions.receiver; |
| 1489 | |
| 1490 | if (memory_region->sender == receiver_id) { |
| 1491 | dlog_verbose("Can't share memory with itself.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1492 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1493 | } |
Federico Recanati | 85090c4 | 2021-12-15 13:17:54 +0100 | [diff] [blame] | 1494 | |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 1495 | for (uint32_t j = i + 1; j < memory_region->receiver_count; |
| 1496 | j++) { |
| 1497 | if (receiver_id == |
| 1498 | memory_region->receivers[j] |
| 1499 | .receiver_permissions.receiver) { |
| 1500 | dlog_verbose( |
| 1501 | "Repeated receiver(%x) in memory send " |
| 1502 | "operation.\n", |
| 1503 | memory_region->receivers[j] |
| 1504 | .receiver_permissions.receiver); |
| 1505 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1506 | } |
| 1507 | } |
| 1508 | |
| 1509 | if (composite_memory_region_offset != |
| 1510 | memory_region->receivers[i] |
| 1511 | .composite_memory_region_offset) { |
| 1512 | dlog_verbose( |
| 1513 | "All ffa_memory_access should point to the " |
| 1514 | "same composite memory region offset.\n"); |
| 1515 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1516 | } |
| 1517 | |
| 1518 | data_access = ffa_get_data_access_attr(permissions); |
| 1519 | instruction_access = |
| 1520 | ffa_get_instruction_access_attr(permissions); |
| 1521 | if (data_access == FFA_DATA_ACCESS_RESERVED || |
| 1522 | instruction_access == FFA_INSTRUCTION_ACCESS_RESERVED) { |
| 1523 | dlog_verbose( |
| 1524 | "Reserved value for receiver permissions " |
| 1525 | "%#x.\n", |
| 1526 | permissions); |
| 1527 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1528 | } |
| 1529 | if (instruction_access != |
| 1530 | FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED) { |
| 1531 | dlog_verbose( |
| 1532 | "Invalid instruction access permissions %#x " |
| 1533 | "for sending memory.\n", |
| 1534 | permissions); |
| 1535 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1536 | } |
| 1537 | if (share_func == FFA_MEM_SHARE_32) { |
| 1538 | if (data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) { |
| 1539 | dlog_verbose( |
| 1540 | "Invalid data access permissions %#x " |
| 1541 | "for sharing memory.\n", |
| 1542 | permissions); |
| 1543 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1544 | } |
| 1545 | /* |
| 1546 | * According to section 10.10.3 of the FF-A v1.1 EAC0 |
| 1547 | * spec, NX is required for share operations (but must |
| 1548 | * not be specified by the sender) so set it in the |
| 1549 | * copy that we store, ready to be returned to the |
| 1550 | * retriever. |
| 1551 | */ |
J-Alves | b19731a | 2022-06-20 17:30:33 +0100 | [diff] [blame] | 1552 | if (vm_id_is_current_world(receiver_id)) { |
| 1553 | ffa_set_instruction_access_attr( |
| 1554 | &permissions, |
| 1555 | FFA_INSTRUCTION_ACCESS_NX); |
| 1556 | memory_region->receivers[i] |
| 1557 | .receiver_permissions.permissions = |
| 1558 | permissions; |
| 1559 | } |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 1560 | } |
| 1561 | if (share_func == FFA_MEM_LEND_32 && |
| 1562 | data_access == FFA_DATA_ACCESS_NOT_SPECIFIED) { |
| 1563 | dlog_verbose( |
| 1564 | "Invalid data access permissions %#x for " |
| 1565 | "lending memory.\n", |
| 1566 | permissions); |
| 1567 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1568 | } |
| 1569 | |
| 1570 | if (share_func == FFA_MEM_DONATE_32 && |
| 1571 | data_access != FFA_DATA_ACCESS_NOT_SPECIFIED) { |
| 1572 | dlog_verbose( |
| 1573 | "Invalid data access permissions %#x for " |
| 1574 | "donating memory.\n", |
| 1575 | permissions); |
| 1576 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1577 | } |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1578 | } |
| 1579 | |
Federico Recanati | d937f5e | 2021-12-20 17:38:23 +0100 | [diff] [blame] | 1580 | /* |
J-Alves | 807794e | 2022-06-16 13:42:47 +0100 | [diff] [blame] | 1581 | * If a memory donate or lend with single borrower, the memory type |
| 1582 | * shall not be specified by the sender. |
Federico Recanati | d937f5e | 2021-12-20 17:38:23 +0100 | [diff] [blame] | 1583 | */ |
J-Alves | 807794e | 2022-06-16 13:42:47 +0100 | [diff] [blame] | 1584 | if (share_func == FFA_MEM_DONATE_32 || |
| 1585 | (share_func == FFA_MEM_LEND_32 && |
| 1586 | memory_region->receiver_count == 1)) { |
| 1587 | if (ffa_get_memory_type_attr(memory_region->attributes) != |
| 1588 | FFA_MEMORY_NOT_SPECIFIED_MEM) { |
| 1589 | dlog_verbose( |
| 1590 | "Memory type shall not be specified by " |
| 1591 | "sender.\n"); |
| 1592 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1593 | } |
| 1594 | } else { |
| 1595 | /* |
| 1596 | * Check that sender's memory attributes match Hafnium |
| 1597 | * expectations: Normal Memory, Inner shareable, Write-Back |
| 1598 | * Read-Allocate Write-Allocate Cacheable. |
| 1599 | */ |
| 1600 | ret = ffa_memory_attributes_validate(memory_region->attributes); |
| 1601 | if (ret.func != FFA_SUCCESS_32) { |
| 1602 | return ret; |
| 1603 | } |
Federico Recanati | d937f5e | 2021-12-20 17:38:23 +0100 | [diff] [blame] | 1604 | } |
| 1605 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1606 | return (struct ffa_value){.func = FFA_SUCCESS_32}; |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1607 | } |
| 1608 | |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1609 | /** Forwards a memory send message on to the other world. */ |
| 1610 | static struct ffa_value memory_send_other_world_forward( |
| 1611 | struct vm_locked other_world_locked, ffa_vm_id_t sender_vm_id, |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1612 | uint32_t share_func, struct ffa_memory_region *memory_region, |
| 1613 | uint32_t memory_share_length, uint32_t fragment_length) |
| 1614 | { |
| 1615 | struct ffa_value ret; |
| 1616 | |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1617 | /* Use its own RX buffer. */ |
| 1618 | memcpy_s(other_world_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1619 | memory_region, fragment_length); |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1620 | other_world_locked.vm->mailbox.recv_size = fragment_length; |
| 1621 | other_world_locked.vm->mailbox.recv_sender = sender_vm_id; |
| 1622 | other_world_locked.vm->mailbox.recv_func = share_func; |
| 1623 | other_world_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED; |
Olivier Deprez | 112d2b5 | 2020-09-30 07:39:23 +0200 | [diff] [blame] | 1624 | ret = arch_other_world_call( |
| 1625 | (struct ffa_value){.func = share_func, |
| 1626 | .arg1 = memory_share_length, |
| 1627 | .arg2 = fragment_length}); |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1628 | /* |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1629 | * After the call to the other world completes it must have finished |
| 1630 | * reading its RX buffer, so it is ready for another message. |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1631 | */ |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1632 | other_world_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY; |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1633 | |
| 1634 | return ret; |
| 1635 | } |
| 1636 | |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1637 | /** |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1638 | * Gets the share state for continuing an operation to donate, lend or share |
| 1639 | * memory, and checks that it is a valid request. |
| 1640 | * |
| 1641 | * Returns FFA_SUCCESS if the request was valid, or the relevant FFA_ERROR if |
| 1642 | * not. |
| 1643 | */ |
| 1644 | static struct ffa_value ffa_memory_send_continue_validate( |
| 1645 | struct share_states_locked share_states, ffa_memory_handle_t handle, |
| 1646 | struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id, |
| 1647 | struct mpool *page_pool) |
| 1648 | { |
| 1649 | struct ffa_memory_share_state *share_state; |
| 1650 | struct ffa_memory_region *memory_region; |
| 1651 | |
Daniel Boulby | a2f8c66 | 2021-11-26 17:52:53 +0000 | [diff] [blame] | 1652 | assert(share_state_ret != NULL); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1653 | |
| 1654 | /* |
| 1655 | * Look up the share state by handle and make sure that the VM ID |
| 1656 | * matches. |
| 1657 | */ |
| 1658 | if (!get_share_state(share_states, handle, &share_state)) { |
| 1659 | dlog_verbose( |
| 1660 | "Invalid handle %#x for memory send continuation.\n", |
| 1661 | handle); |
| 1662 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1663 | } |
| 1664 | memory_region = share_state->memory_region; |
| 1665 | |
| 1666 | if (memory_region->sender != from_vm_id) { |
| 1667 | dlog_verbose("Invalid sender %d.\n", memory_region->sender); |
| 1668 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1669 | } |
| 1670 | |
| 1671 | if (share_state->sending_complete) { |
| 1672 | dlog_verbose( |
| 1673 | "Sending of memory handle %#x is already complete.\n", |
| 1674 | handle); |
| 1675 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 1676 | } |
| 1677 | |
| 1678 | if (share_state->fragment_count == MAX_FRAGMENTS) { |
| 1679 | /* |
| 1680 | * Log a warning as this is a sign that MAX_FRAGMENTS should |
| 1681 | * probably be increased. |
| 1682 | */ |
| 1683 | dlog_warning( |
| 1684 | "Too many fragments for memory share with handle %#x; " |
| 1685 | "only %d supported.\n", |
| 1686 | handle, MAX_FRAGMENTS); |
| 1687 | /* Free share state, as it's not possible to complete it. */ |
| 1688 | share_state_free(share_states, share_state, page_pool); |
| 1689 | return ffa_error(FFA_NO_MEMORY); |
| 1690 | } |
| 1691 | |
| 1692 | *share_state_ret = share_state; |
| 1693 | |
| 1694 | return (struct ffa_value){.func = FFA_SUCCESS_32}; |
| 1695 | } |
| 1696 | |
| 1697 | /** |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1698 | * Forwards a memory send continuation message on to the other world. |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1699 | */ |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1700 | static struct ffa_value memory_send_continue_other_world_forward( |
| 1701 | struct vm_locked other_world_locked, ffa_vm_id_t sender_vm_id, |
| 1702 | void *fragment, uint32_t fragment_length, ffa_memory_handle_t handle) |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1703 | { |
| 1704 | struct ffa_value ret; |
| 1705 | |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1706 | memcpy_s(other_world_locked.vm->mailbox.recv, FFA_MSG_PAYLOAD_MAX, |
| 1707 | fragment, fragment_length); |
| 1708 | other_world_locked.vm->mailbox.recv_size = fragment_length; |
| 1709 | other_world_locked.vm->mailbox.recv_sender = sender_vm_id; |
| 1710 | other_world_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32; |
| 1711 | other_world_locked.vm->mailbox.state = MAILBOX_STATE_RECEIVED; |
Olivier Deprez | 112d2b5 | 2020-09-30 07:39:23 +0200 | [diff] [blame] | 1712 | ret = arch_other_world_call( |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1713 | (struct ffa_value){.func = FFA_MEM_FRAG_TX_32, |
| 1714 | .arg1 = (uint32_t)handle, |
| 1715 | .arg2 = (uint32_t)(handle >> 32), |
| 1716 | .arg3 = fragment_length, |
| 1717 | .arg4 = (uint64_t)sender_vm_id << 16}); |
| 1718 | /* |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1719 | * After the call to the other world completes it must have finished |
| 1720 | * reading its RX buffer, so it is ready for another message. |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1721 | */ |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1722 | other_world_locked.vm->mailbox.state = MAILBOX_STATE_EMPTY; |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1723 | |
| 1724 | return ret; |
| 1725 | } |
| 1726 | |
| 1727 | /** |
J-Alves | 95df0ef | 2022-12-07 10:09:48 +0000 | [diff] [blame] | 1728 | * Checks if there is at least one receiver from the other world. |
| 1729 | */ |
| 1730 | static bool memory_region_receivers_from_other_world( |
| 1731 | struct ffa_memory_region *memory_region) |
| 1732 | { |
| 1733 | for (uint32_t i = 0; i < memory_region->receiver_count; i++) { |
| 1734 | ffa_vm_id_t receiver = memory_region->receivers[i] |
| 1735 | .receiver_permissions.receiver; |
| 1736 | if (!vm_id_is_current_world(receiver)) { |
| 1737 | return true; |
| 1738 | } |
| 1739 | } |
| 1740 | return false; |
| 1741 | } |
| 1742 | |
| 1743 | /** |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1744 | * Validates a call to donate, lend or share memory to a non-other world VM and |
| 1745 | * then updates the stage-2 page tables. Specifically, check if the message |
| 1746 | * length and number of memory region constituents match, and if the transition |
| 1747 | * is valid for the type of memory sending operation. |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 1748 | * |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1749 | * Assumes that the caller has already found and locked the sender VM and copied |
| 1750 | * the memory region descriptor from the sender's TX buffer to a freshly |
| 1751 | * allocated page from Hafnium's internal pool. The caller must have also |
| 1752 | * validated that the receiver VM ID is valid. |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1753 | * |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1754 | * This function takes ownership of the `memory_region` passed in and will free |
| 1755 | * it when necessary; it must not be freed by the caller. |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 1756 | */ |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1757 | struct ffa_value ffa_memory_send(struct vm_locked from_locked, |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1758 | struct ffa_memory_region *memory_region, |
Andrew Walbran | 130a8ae | 2020-05-15 16:27:15 +0100 | [diff] [blame] | 1759 | uint32_t memory_share_length, |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1760 | uint32_t fragment_length, uint32_t share_func, |
| 1761 | struct mpool *page_pool) |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 1762 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1763 | struct ffa_value ret; |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1764 | struct share_states_locked share_states; |
| 1765 | struct ffa_memory_share_state *share_state; |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 1766 | |
| 1767 | /* |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1768 | * If there is an error validating the `memory_region` then we need to |
| 1769 | * free it because we own it but we won't be storing it in a share state |
| 1770 | * after all. |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 1771 | */ |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1772 | ret = ffa_memory_send_validate(from_locked, memory_region, |
| 1773 | memory_share_length, fragment_length, |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 1774 | share_func); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1775 | if (ret.func != FFA_SUCCESS_32) { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1776 | mpool_free(page_pool, memory_region); |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1777 | return ret; |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 1778 | } |
| 1779 | |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1780 | /* Set flag for share function, ready to be retrieved later. */ |
| 1781 | switch (share_func) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1782 | case FFA_MEM_SHARE_32: |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1783 | memory_region->flags |= |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1784 | FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE; |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1785 | break; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1786 | case FFA_MEM_LEND_32: |
| 1787 | memory_region->flags |= FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND; |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1788 | break; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1789 | case FFA_MEM_DONATE_32: |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1790 | memory_region->flags |= |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1791 | FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE; |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 1792 | break; |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 1793 | } |
| 1794 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1795 | share_states = share_states_lock(); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1796 | /* |
| 1797 | * Allocate a share state before updating the page table. Otherwise if |
| 1798 | * updating the page table succeeded but allocating the share state |
| 1799 | * failed then it would leave the memory in a state where nobody could |
| 1800 | * get it back. |
| 1801 | */ |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1802 | if (!allocate_share_state(share_states, share_func, memory_region, |
| 1803 | fragment_length, FFA_MEMORY_HANDLE_INVALID, |
| 1804 | &share_state)) { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1805 | dlog_verbose("Failed to allocate share state.\n"); |
| 1806 | mpool_free(page_pool, memory_region); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1807 | ret = ffa_error(FFA_NO_MEMORY); |
| 1808 | goto out; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1811 | if (fragment_length == memory_share_length) { |
| 1812 | /* No more fragments to come, everything fit in one message. */ |
J-Alves | 2a0d288 | 2020-10-29 14:49:50 +0000 | [diff] [blame] | 1813 | ret = ffa_memory_send_complete( |
| 1814 | from_locked, share_states, share_state, page_pool, |
| 1815 | &(share_state->sender_orig_mode)); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1816 | } else { |
| 1817 | ret = (struct ffa_value){ |
| 1818 | .func = FFA_MEM_FRAG_RX_32, |
J-Alves | ee68c54 | 2020-10-29 17:48:20 +0000 | [diff] [blame] | 1819 | .arg1 = (uint32_t)memory_region->handle, |
| 1820 | .arg2 = (uint32_t)(memory_region->handle >> 32), |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1821 | .arg3 = fragment_length}; |
| 1822 | } |
| 1823 | |
| 1824 | out: |
| 1825 | share_states_unlock(&share_states); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1826 | dump_share_states(); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1827 | return ret; |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | /** |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1831 | * Validates a call to donate, lend or share memory to the other world and then |
| 1832 | * updates the stage-2 page tables. Specifically, check if the message length |
| 1833 | * and number of memory region constituents match, and if the transition is |
| 1834 | * valid for the type of memory sending operation. |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1835 | * |
| 1836 | * Assumes that the caller has already found and locked the sender VM and the |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1837 | * other world VM, and copied the memory region descriptor from the sender's TX |
| 1838 | * buffer to a freshly allocated page from Hafnium's internal pool. The caller |
| 1839 | * must have also validated that the receiver VM ID is valid. |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1840 | * |
| 1841 | * This function takes ownership of the `memory_region` passed in and will free |
| 1842 | * it when necessary; it must not be freed by the caller. |
| 1843 | */ |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1844 | struct ffa_value ffa_memory_other_world_send( |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1845 | struct vm_locked from_locked, struct vm_locked to_locked, |
| 1846 | struct ffa_memory_region *memory_region, uint32_t memory_share_length, |
| 1847 | uint32_t fragment_length, uint32_t share_func, struct mpool *page_pool) |
| 1848 | { |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1849 | struct ffa_value ret; |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1850 | |
| 1851 | /* |
| 1852 | * If there is an error validating the `memory_region` then we need to |
| 1853 | * free it because we own it but we won't be storing it in a share state |
| 1854 | * after all. |
| 1855 | */ |
| 1856 | ret = ffa_memory_send_validate(from_locked, memory_region, |
| 1857 | memory_share_length, fragment_length, |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 1858 | share_func); |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1859 | if (ret.func != FFA_SUCCESS_32) { |
| 1860 | goto out; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1863 | if (fragment_length == memory_share_length) { |
| 1864 | /* No more fragments to come, everything fit in one message. */ |
| 1865 | struct ffa_composite_memory_region *composite = |
| 1866 | ffa_memory_region_get_composite(memory_region, 0); |
| 1867 | struct ffa_memory_region_constituent *constituents = |
| 1868 | composite->constituents; |
Andrew Walbran | 37c574e | 2020-06-03 11:45:46 +0100 | [diff] [blame] | 1869 | struct mpool local_page_pool; |
| 1870 | uint32_t orig_from_mode; |
| 1871 | |
| 1872 | /* |
| 1873 | * Use a local page pool so that we can roll back if necessary. |
| 1874 | */ |
| 1875 | mpool_init_with_fallback(&local_page_pool, page_pool); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1876 | |
| 1877 | ret = ffa_send_check_update( |
| 1878 | from_locked, &constituents, |
| 1879 | &composite->constituent_count, 1, share_func, |
J-Alves | 363f572 | 2022-04-25 17:37:37 +0100 | [diff] [blame] | 1880 | memory_region->receivers, memory_region->receiver_count, |
| 1881 | &local_page_pool, |
Andrew Walbran | 37c574e | 2020-06-03 11:45:46 +0100 | [diff] [blame] | 1882 | memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR, |
| 1883 | &orig_from_mode); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1884 | if (ret.func != FFA_SUCCESS_32) { |
Andrew Walbran | 37c574e | 2020-06-03 11:45:46 +0100 | [diff] [blame] | 1885 | mpool_fini(&local_page_pool); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1886 | goto out; |
| 1887 | } |
| 1888 | |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1889 | /* Forward memory send message on to other world. */ |
| 1890 | ret = memory_send_other_world_forward( |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1891 | to_locked, from_locked.vm->id, share_func, |
| 1892 | memory_region, memory_share_length, fragment_length); |
Andrew Walbran | 37c574e | 2020-06-03 11:45:46 +0100 | [diff] [blame] | 1893 | |
| 1894 | if (ret.func != FFA_SUCCESS_32) { |
| 1895 | dlog_verbose( |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1896 | "Other world didn't successfully complete " |
| 1897 | "memory send operation; returned %#x (%d). " |
| 1898 | "Rolling back.\n", |
Andrew Walbran | 37c574e | 2020-06-03 11:45:46 +0100 | [diff] [blame] | 1899 | ret.func, ret.arg2); |
| 1900 | |
| 1901 | /* |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1902 | * The other world failed to complete the send |
| 1903 | * operation, so roll back the page table update for the |
| 1904 | * VM. This can't fail because it won't try to allocate |
| 1905 | * more memory than was freed into the `local_page_pool` |
| 1906 | * by `ffa_send_check_update` in the initial update. |
Andrew Walbran | 37c574e | 2020-06-03 11:45:46 +0100 | [diff] [blame] | 1907 | */ |
| 1908 | CHECK(ffa_region_group_identity_map( |
| 1909 | from_locked, &constituents, |
| 1910 | &composite->constituent_count, 1, |
| 1911 | orig_from_mode, &local_page_pool, true)); |
| 1912 | } |
| 1913 | |
| 1914 | mpool_fini(&local_page_pool); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1915 | } else { |
| 1916 | struct share_states_locked share_states = share_states_lock(); |
| 1917 | ffa_memory_handle_t handle; |
| 1918 | |
| 1919 | /* |
| 1920 | * We need to wait for the rest of the fragments before we can |
| 1921 | * check whether the transaction is valid and unmap the memory. |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1922 | * Call the other world so it can do its initial validation and |
| 1923 | * assign a handle, and allocate a share state to keep what we |
| 1924 | * have so far. |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1925 | */ |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1926 | ret = memory_send_other_world_forward( |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1927 | to_locked, from_locked.vm->id, share_func, |
| 1928 | memory_region, memory_share_length, fragment_length); |
| 1929 | if (ret.func == FFA_ERROR_32) { |
| 1930 | goto out_unlock; |
| 1931 | } else if (ret.func != FFA_MEM_FRAG_RX_32) { |
| 1932 | dlog_warning( |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1933 | "Got %#x from other world in response to %#x " |
| 1934 | "for " |
Olivier Deprez | 701e8bf | 2022-04-06 18:45:18 +0200 | [diff] [blame] | 1935 | "fragment with %d/%d, expected " |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1936 | "FFA_MEM_FRAG_RX.\n", |
| 1937 | ret.func, share_func, fragment_length, |
| 1938 | memory_share_length); |
| 1939 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 1940 | goto out_unlock; |
| 1941 | } |
| 1942 | handle = ffa_frag_handle(ret); |
| 1943 | if (ret.arg3 != fragment_length) { |
| 1944 | dlog_warning( |
| 1945 | "Got unexpected fragment offset %d for " |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1946 | "FFA_MEM_FRAG_RX from other world (expected " |
| 1947 | "%d).\n", |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1948 | ret.arg3, fragment_length); |
| 1949 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 1950 | goto out_unlock; |
| 1951 | } |
| 1952 | if (ffa_frag_sender(ret) != from_locked.vm->id) { |
| 1953 | dlog_warning( |
| 1954 | "Got unexpected sender ID %d for " |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1955 | "FFA_MEM_FRAG_RX from other world (expected " |
| 1956 | "%d).\n", |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1957 | ffa_frag_sender(ret), from_locked.vm->id); |
| 1958 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 1959 | goto out_unlock; |
| 1960 | } |
| 1961 | |
| 1962 | if (!allocate_share_state(share_states, share_func, |
| 1963 | memory_region, fragment_length, |
| 1964 | handle, NULL)) { |
| 1965 | dlog_verbose("Failed to allocate share state.\n"); |
| 1966 | ret = ffa_error(FFA_NO_MEMORY); |
| 1967 | goto out_unlock; |
| 1968 | } |
| 1969 | /* |
| 1970 | * Don't free the memory region fragment, as it has been stored |
| 1971 | * in the share state. |
| 1972 | */ |
| 1973 | memory_region = NULL; |
| 1974 | out_unlock: |
| 1975 | share_states_unlock(&share_states); |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1976 | } |
| 1977 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1978 | out: |
| 1979 | if (memory_region != NULL) { |
| 1980 | mpool_free(page_pool, memory_region); |
| 1981 | } |
| 1982 | dump_share_states(); |
| 1983 | return ret; |
| 1984 | } |
| 1985 | |
| 1986 | /** |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 1987 | * Continues an operation to donate, lend or share memory to a VM from current |
| 1988 | * world. If this is the last fragment then checks that the transition is valid |
| 1989 | * for the type of memory sending operation and updates the stage-2 page tables |
| 1990 | * of the sender. |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 1991 | * |
| 1992 | * Assumes that the caller has already found and locked the sender VM and copied |
| 1993 | * the memory region descriptor from the sender's TX buffer to a freshly |
| 1994 | * allocated page from Hafnium's internal pool. |
| 1995 | * |
| 1996 | * This function takes ownership of the `fragment` passed in; it must not be |
| 1997 | * freed by the caller. |
| 1998 | */ |
| 1999 | struct ffa_value ffa_memory_send_continue(struct vm_locked from_locked, |
| 2000 | void *fragment, |
| 2001 | uint32_t fragment_length, |
| 2002 | ffa_memory_handle_t handle, |
| 2003 | struct mpool *page_pool) |
| 2004 | { |
| 2005 | struct share_states_locked share_states = share_states_lock(); |
| 2006 | struct ffa_memory_share_state *share_state; |
| 2007 | struct ffa_value ret; |
| 2008 | struct ffa_memory_region *memory_region; |
| 2009 | |
| 2010 | ret = ffa_memory_send_continue_validate(share_states, handle, |
| 2011 | &share_state, |
| 2012 | from_locked.vm->id, page_pool); |
| 2013 | if (ret.func != FFA_SUCCESS_32) { |
| 2014 | goto out_free_fragment; |
| 2015 | } |
| 2016 | memory_region = share_state->memory_region; |
| 2017 | |
J-Alves | 95df0ef | 2022-12-07 10:09:48 +0000 | [diff] [blame] | 2018 | if (memory_region_receivers_from_other_world(memory_region)) { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2019 | dlog_error( |
| 2020 | "Got hypervisor-allocated handle for memory send to " |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 2021 | "other world. This should never happen, and indicates " |
| 2022 | "a bug in " |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2023 | "EL3 code.\n"); |
| 2024 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2025 | goto out_free_fragment; |
| 2026 | } |
| 2027 | |
| 2028 | /* Add this fragment. */ |
| 2029 | share_state->fragments[share_state->fragment_count] = fragment; |
| 2030 | share_state->fragment_constituent_counts[share_state->fragment_count] = |
| 2031 | fragment_length / sizeof(struct ffa_memory_region_constituent); |
| 2032 | share_state->fragment_count++; |
| 2033 | |
| 2034 | /* Check whether the memory send operation is now ready to complete. */ |
| 2035 | if (share_state_sending_complete(share_states, share_state)) { |
J-Alves | 2a0d288 | 2020-10-29 14:49:50 +0000 | [diff] [blame] | 2036 | ret = ffa_memory_send_complete( |
| 2037 | from_locked, share_states, share_state, page_pool, |
| 2038 | &(share_state->sender_orig_mode)); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2039 | } else { |
| 2040 | ret = (struct ffa_value){ |
| 2041 | .func = FFA_MEM_FRAG_RX_32, |
| 2042 | .arg1 = (uint32_t)handle, |
| 2043 | .arg2 = (uint32_t)(handle >> 32), |
| 2044 | .arg3 = share_state_next_fragment_offset(share_states, |
| 2045 | share_state)}; |
| 2046 | } |
| 2047 | goto out; |
| 2048 | |
| 2049 | out_free_fragment: |
| 2050 | mpool_free(page_pool, fragment); |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2051 | |
| 2052 | out: |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2053 | share_states_unlock(&share_states); |
Andrew Walbran | 1a86aa9 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2054 | return ret; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2055 | } |
| 2056 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2057 | /** |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 2058 | * Continues an operation to donate, lend or share memory to the other world VM. |
| 2059 | * If this is the last fragment then checks that the transition is valid for the |
| 2060 | * type of memory sending operation and updates the stage-2 page tables of the |
| 2061 | * sender. |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2062 | * |
| 2063 | * Assumes that the caller has already found and locked the sender VM and copied |
| 2064 | * the memory region descriptor from the sender's TX buffer to a freshly |
| 2065 | * allocated page from Hafnium's internal pool. |
| 2066 | * |
| 2067 | * This function takes ownership of the `memory_region` passed in and will free |
| 2068 | * it when necessary; it must not be freed by the caller. |
| 2069 | */ |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 2070 | struct ffa_value ffa_memory_other_world_send_continue( |
| 2071 | struct vm_locked from_locked, struct vm_locked to_locked, |
| 2072 | void *fragment, uint32_t fragment_length, ffa_memory_handle_t handle, |
| 2073 | struct mpool *page_pool) |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2074 | { |
| 2075 | struct share_states_locked share_states = share_states_lock(); |
| 2076 | struct ffa_memory_share_state *share_state; |
| 2077 | struct ffa_value ret; |
| 2078 | struct ffa_memory_region *memory_region; |
| 2079 | |
| 2080 | ret = ffa_memory_send_continue_validate(share_states, handle, |
| 2081 | &share_state, |
| 2082 | from_locked.vm->id, page_pool); |
| 2083 | if (ret.func != FFA_SUCCESS_32) { |
| 2084 | goto out_free_fragment; |
| 2085 | } |
| 2086 | memory_region = share_state->memory_region; |
| 2087 | |
J-Alves | 95df0ef | 2022-12-07 10:09:48 +0000 | [diff] [blame] | 2088 | if (!memory_region_receivers_from_other_world(memory_region)) { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2089 | dlog_error( |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 2090 | "Got SPM-allocated handle for memory send to non-other " |
| 2091 | "world VM. This should never happen, and indicates a " |
| 2092 | "bug.\n"); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2093 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2094 | goto out_free_fragment; |
| 2095 | } |
| 2096 | |
| 2097 | if (to_locked.vm->mailbox.state != MAILBOX_STATE_EMPTY || |
| 2098 | to_locked.vm->mailbox.recv == NULL) { |
| 2099 | /* |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 2100 | * If the other world RX buffer is not available, tell the |
| 2101 | * sender to retry by returning the current offset again. |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2102 | */ |
| 2103 | ret = (struct ffa_value){ |
| 2104 | .func = FFA_MEM_FRAG_RX_32, |
| 2105 | .arg1 = (uint32_t)handle, |
| 2106 | .arg2 = (uint32_t)(handle >> 32), |
| 2107 | .arg3 = share_state_next_fragment_offset(share_states, |
| 2108 | share_state), |
| 2109 | }; |
| 2110 | goto out_free_fragment; |
| 2111 | } |
| 2112 | |
| 2113 | /* Add this fragment. */ |
| 2114 | share_state->fragments[share_state->fragment_count] = fragment; |
| 2115 | share_state->fragment_constituent_counts[share_state->fragment_count] = |
| 2116 | fragment_length / sizeof(struct ffa_memory_region_constituent); |
| 2117 | share_state->fragment_count++; |
| 2118 | |
| 2119 | /* Check whether the memory send operation is now ready to complete. */ |
| 2120 | if (share_state_sending_complete(share_states, share_state)) { |
Andrew Walbran | 37c574e | 2020-06-03 11:45:46 +0100 | [diff] [blame] | 2121 | struct mpool local_page_pool; |
| 2122 | uint32_t orig_from_mode; |
| 2123 | |
| 2124 | /* |
| 2125 | * Use a local page pool so that we can roll back if necessary. |
| 2126 | */ |
| 2127 | mpool_init_with_fallback(&local_page_pool, page_pool); |
| 2128 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2129 | ret = ffa_memory_send_complete(from_locked, share_states, |
Andrew Walbran | 37c574e | 2020-06-03 11:45:46 +0100 | [diff] [blame] | 2130 | share_state, &local_page_pool, |
| 2131 | &orig_from_mode); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2132 | |
| 2133 | if (ret.func == FFA_SUCCESS_32) { |
| 2134 | /* |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 2135 | * Forward final fragment on to the other world so that |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2136 | * it can complete the memory sending operation. |
| 2137 | */ |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 2138 | ret = memory_send_continue_other_world_forward( |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2139 | to_locked, from_locked.vm->id, fragment, |
| 2140 | fragment_length, handle); |
| 2141 | |
| 2142 | if (ret.func != FFA_SUCCESS_32) { |
| 2143 | /* |
| 2144 | * The error will be passed on to the caller, |
| 2145 | * but log it here too. |
| 2146 | */ |
| 2147 | dlog_verbose( |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 2148 | "other world didn't successfully " |
| 2149 | "complete " |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2150 | "memory send operation; returned %#x " |
Andrew Walbran | 37c574e | 2020-06-03 11:45:46 +0100 | [diff] [blame] | 2151 | "(%d). Rolling back.\n", |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2152 | ret.func, ret.arg2); |
Andrew Walbran | 37c574e | 2020-06-03 11:45:46 +0100 | [diff] [blame] | 2153 | |
| 2154 | /* |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 2155 | * The other world failed to complete the send |
Andrew Walbran | 37c574e | 2020-06-03 11:45:46 +0100 | [diff] [blame] | 2156 | * operation, so roll back the page table update |
| 2157 | * for the VM. This can't fail because it won't |
| 2158 | * try to allocate more memory than was freed |
| 2159 | * into the `local_page_pool` by |
| 2160 | * `ffa_send_check_update` in the initial |
| 2161 | * update. |
| 2162 | */ |
| 2163 | CHECK(ffa_region_group_identity_map( |
| 2164 | from_locked, share_state->fragments, |
| 2165 | share_state |
| 2166 | ->fragment_constituent_counts, |
| 2167 | share_state->fragment_count, |
| 2168 | orig_from_mode, &local_page_pool, |
| 2169 | true)); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2170 | } |
Andrew Walbran | 37c574e | 2020-06-03 11:45:46 +0100 | [diff] [blame] | 2171 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2172 | /* Free share state. */ |
| 2173 | share_state_free(share_states, share_state, page_pool); |
| 2174 | } else { |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 2175 | /* Abort sending to other world. */ |
| 2176 | struct ffa_value other_world_ret = |
Olivier Deprez | 112d2b5 | 2020-09-30 07:39:23 +0200 | [diff] [blame] | 2177 | arch_other_world_call((struct ffa_value){ |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2178 | .func = FFA_MEM_RECLAIM_32, |
| 2179 | .arg1 = (uint32_t)handle, |
| 2180 | .arg2 = (uint32_t)(handle >> 32)}); |
| 2181 | |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 2182 | if (other_world_ret.func != FFA_SUCCESS_32) { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2183 | /* |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 2184 | * Nothing we can do if other world doesn't |
| 2185 | * abort properly, just log it. |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2186 | */ |
| 2187 | dlog_verbose( |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 2188 | "other world didn't successfully abort " |
| 2189 | "failed " |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2190 | "memory send operation; returned %#x " |
| 2191 | "(%d).\n", |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 2192 | other_world_ret.func, |
| 2193 | other_world_ret.arg2); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2194 | } |
| 2195 | /* |
| 2196 | * We don't need to free the share state in this case |
| 2197 | * because ffa_memory_send_complete does that already. |
| 2198 | */ |
| 2199 | } |
Andrew Walbran | 37c574e | 2020-06-03 11:45:46 +0100 | [diff] [blame] | 2200 | |
| 2201 | mpool_fini(&local_page_pool); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2202 | } else { |
| 2203 | uint32_t next_fragment_offset = |
| 2204 | share_state_next_fragment_offset(share_states, |
| 2205 | share_state); |
| 2206 | |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 2207 | ret = memory_send_continue_other_world_forward( |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2208 | to_locked, from_locked.vm->id, fragment, |
| 2209 | fragment_length, handle); |
| 2210 | |
| 2211 | if (ret.func != FFA_MEM_FRAG_RX_32 || |
| 2212 | ffa_frag_handle(ret) != handle || |
| 2213 | ret.arg3 != next_fragment_offset || |
| 2214 | ffa_frag_sender(ret) != from_locked.vm->id) { |
| 2215 | dlog_verbose( |
| 2216 | "Got unexpected result from forwarding " |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 2217 | "FFA_MEM_FRAG_TX to other world. %#x (handle " |
| 2218 | "%#x, " |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2219 | "offset %d, sender %d); expected " |
| 2220 | "FFA_MEM_FRAG_RX (handle %#x, offset %d, " |
| 2221 | "sender %d).\n", |
| 2222 | ret.func, ffa_frag_handle(ret), ret.arg3, |
| 2223 | ffa_frag_sender(ret), handle, |
| 2224 | next_fragment_offset, from_locked.vm->id); |
| 2225 | /* Free share state. */ |
| 2226 | share_state_free(share_states, share_state, page_pool); |
| 2227 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2228 | goto out; |
| 2229 | } |
| 2230 | |
| 2231 | ret = (struct ffa_value){.func = FFA_MEM_FRAG_RX_32, |
| 2232 | .arg1 = (uint32_t)handle, |
| 2233 | .arg2 = (uint32_t)(handle >> 32), |
| 2234 | .arg3 = next_fragment_offset}; |
| 2235 | } |
| 2236 | goto out; |
| 2237 | |
| 2238 | out_free_fragment: |
| 2239 | mpool_free(page_pool, fragment); |
| 2240 | |
| 2241 | out: |
| 2242 | share_states_unlock(&share_states); |
| 2243 | return ret; |
| 2244 | } |
| 2245 | |
| 2246 | /** Clean up after the receiver has finished retrieving a memory region. */ |
| 2247 | static void ffa_memory_retrieve_complete( |
| 2248 | struct share_states_locked share_states, |
| 2249 | struct ffa_memory_share_state *share_state, struct mpool *page_pool) |
| 2250 | { |
| 2251 | if (share_state->share_func == FFA_MEM_DONATE_32) { |
| 2252 | /* |
| 2253 | * Memory that has been donated can't be relinquished, |
| 2254 | * so no need to keep the share state around. |
| 2255 | */ |
| 2256 | share_state_free(share_states, share_state, page_pool); |
| 2257 | dlog_verbose("Freed share state for donate.\n"); |
| 2258 | } |
| 2259 | } |
| 2260 | |
J-Alves | 96de29f | 2022-04-26 16:05:24 +0100 | [diff] [blame] | 2261 | /* |
| 2262 | * Gets the receiver's access permissions from 'struct ffa_memory_region' and |
| 2263 | * returns its index in the receiver's array. If receiver's ID doesn't exist |
| 2264 | * in the array, return the region's 'receiver_count'. |
| 2265 | */ |
| 2266 | static uint32_t ffa_memory_region_get_receiver( |
| 2267 | struct ffa_memory_region *memory_region, ffa_vm_id_t receiver) |
| 2268 | { |
| 2269 | struct ffa_memory_access *receivers; |
| 2270 | uint32_t i; |
| 2271 | |
| 2272 | assert(memory_region != NULL); |
| 2273 | |
| 2274 | receivers = memory_region->receivers; |
| 2275 | |
| 2276 | for (i = 0U; i < memory_region->receiver_count; i++) { |
| 2277 | if (receivers[i].receiver_permissions.receiver == receiver) { |
| 2278 | break; |
| 2279 | } |
| 2280 | } |
| 2281 | |
| 2282 | return i; |
| 2283 | } |
| 2284 | |
| 2285 | /** |
| 2286 | * Validates the retrieved permissions against those specified by the lender |
| 2287 | * of memory share operation. Optionally can help set the permissions to be used |
| 2288 | * for the S2 mapping, through the `permissions` argument. |
| 2289 | * Returns true if permissions are valid, false otherwise. |
| 2290 | */ |
| 2291 | static bool ffa_memory_retrieve_is_memory_access_valid( |
| 2292 | enum ffa_data_access sent_data_access, |
| 2293 | enum ffa_data_access requested_data_access, |
| 2294 | enum ffa_instruction_access sent_instruction_access, |
| 2295 | enum ffa_instruction_access requested_instruction_access, |
| 2296 | ffa_memory_access_permissions_t *permissions) |
| 2297 | { |
| 2298 | switch (sent_data_access) { |
| 2299 | case FFA_DATA_ACCESS_NOT_SPECIFIED: |
| 2300 | case FFA_DATA_ACCESS_RW: |
| 2301 | if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED || |
| 2302 | requested_data_access == FFA_DATA_ACCESS_RW) { |
| 2303 | if (permissions != NULL) { |
| 2304 | ffa_set_data_access_attr(permissions, |
| 2305 | FFA_DATA_ACCESS_RW); |
| 2306 | } |
| 2307 | break; |
| 2308 | } |
| 2309 | /* Intentional fall-through. */ |
| 2310 | case FFA_DATA_ACCESS_RO: |
| 2311 | if (requested_data_access == FFA_DATA_ACCESS_NOT_SPECIFIED || |
| 2312 | requested_data_access == FFA_DATA_ACCESS_RO) { |
| 2313 | if (permissions != NULL) { |
| 2314 | ffa_set_data_access_attr(permissions, |
| 2315 | FFA_DATA_ACCESS_RO); |
| 2316 | } |
| 2317 | break; |
| 2318 | } |
| 2319 | dlog_verbose( |
| 2320 | "Invalid data access requested; sender specified " |
| 2321 | "permissions %#x but receiver requested %#x.\n", |
| 2322 | sent_data_access, requested_data_access); |
| 2323 | return false; |
| 2324 | case FFA_DATA_ACCESS_RESERVED: |
| 2325 | panic("Got unexpected FFA_DATA_ACCESS_RESERVED. Should be " |
| 2326 | "checked before this point."); |
| 2327 | } |
| 2328 | |
| 2329 | switch (sent_instruction_access) { |
| 2330 | case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED: |
| 2331 | case FFA_INSTRUCTION_ACCESS_X: |
| 2332 | if (requested_instruction_access == |
| 2333 | FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED || |
| 2334 | requested_instruction_access == FFA_INSTRUCTION_ACCESS_X) { |
| 2335 | if (permissions != NULL) { |
| 2336 | ffa_set_instruction_access_attr( |
| 2337 | permissions, FFA_INSTRUCTION_ACCESS_X); |
| 2338 | } |
| 2339 | break; |
| 2340 | } |
| 2341 | case FFA_INSTRUCTION_ACCESS_NX: |
| 2342 | if (requested_instruction_access == |
| 2343 | FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED || |
| 2344 | requested_instruction_access == FFA_INSTRUCTION_ACCESS_NX) { |
| 2345 | if (permissions != NULL) { |
| 2346 | ffa_set_instruction_access_attr( |
| 2347 | permissions, FFA_INSTRUCTION_ACCESS_NX); |
| 2348 | } |
| 2349 | break; |
| 2350 | } |
| 2351 | dlog_verbose( |
| 2352 | "Invalid instruction access requested; sender " |
| 2353 | "specified permissions %#x but receiver requested " |
| 2354 | "%#x.\n", |
| 2355 | sent_instruction_access, requested_instruction_access); |
| 2356 | return false; |
| 2357 | case FFA_INSTRUCTION_ACCESS_RESERVED: |
| 2358 | panic("Got unexpected FFA_INSTRUCTION_ACCESS_RESERVED. Should " |
| 2359 | "be checked before this point."); |
| 2360 | } |
| 2361 | |
| 2362 | return true; |
| 2363 | } |
| 2364 | |
| 2365 | /** |
| 2366 | * Validate the receivers' permissions in the retrieve request against those |
| 2367 | * specified by the lender. |
| 2368 | * In the `permissions` argument returns the permissions to set at S2 for the |
| 2369 | * caller to the FFA_MEMORY_RETRIEVE_REQ. |
| 2370 | * Returns FFA_SUCCESS if all specified permissions are valid. |
| 2371 | */ |
| 2372 | static struct ffa_value ffa_memory_retrieve_validate_memory_access_list( |
| 2373 | struct ffa_memory_region *memory_region, |
| 2374 | struct ffa_memory_region *retrieve_request, ffa_vm_id_t to_vm_id, |
| 2375 | ffa_memory_access_permissions_t *permissions) |
| 2376 | { |
| 2377 | uint32_t retrieve_receiver_index; |
| 2378 | |
| 2379 | assert(permissions != NULL); |
| 2380 | |
| 2381 | if (retrieve_request->receiver_count != memory_region->receiver_count) { |
| 2382 | dlog_verbose( |
| 2383 | "Retrieve request should contain same list of " |
| 2384 | "borrowers, as specified by the lender.\n"); |
| 2385 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2386 | } |
| 2387 | |
| 2388 | retrieve_receiver_index = retrieve_request->receiver_count; |
| 2389 | |
| 2390 | /* Should be populated with the permissions of the retriever. */ |
| 2391 | *permissions = 0; |
| 2392 | |
| 2393 | for (uint32_t i = 0U; i < retrieve_request->receiver_count; i++) { |
| 2394 | ffa_memory_access_permissions_t sent_permissions; |
| 2395 | struct ffa_memory_access *current_receiver = |
| 2396 | &retrieve_request->receivers[i]; |
| 2397 | ffa_memory_access_permissions_t requested_permissions = |
| 2398 | current_receiver->receiver_permissions.permissions; |
| 2399 | ffa_vm_id_t current_receiver_id = |
| 2400 | current_receiver->receiver_permissions.receiver; |
| 2401 | bool found_to_id = current_receiver_id == to_vm_id; |
| 2402 | |
| 2403 | /* |
| 2404 | * Find the current receiver in the transaction descriptor from |
| 2405 | * sender. |
| 2406 | */ |
| 2407 | uint32_t mem_region_receiver_index = |
| 2408 | ffa_memory_region_get_receiver(memory_region, |
| 2409 | current_receiver_id); |
| 2410 | |
| 2411 | if (mem_region_receiver_index == |
| 2412 | memory_region->receiver_count) { |
| 2413 | dlog_verbose("%s: receiver %x not found\n", __func__, |
| 2414 | current_receiver_id); |
| 2415 | return ffa_error(FFA_DENIED); |
| 2416 | } |
| 2417 | |
| 2418 | sent_permissions = |
| 2419 | memory_region->receivers[mem_region_receiver_index] |
| 2420 | .receiver_permissions.permissions; |
| 2421 | |
| 2422 | if (found_to_id) { |
| 2423 | retrieve_receiver_index = i; |
| 2424 | } |
| 2425 | |
| 2426 | /* |
| 2427 | * Since we are traversing the list of receivers, save the index |
| 2428 | * of the caller. As it needs to be there. |
| 2429 | */ |
| 2430 | |
| 2431 | if (current_receiver->composite_memory_region_offset != 0U) { |
| 2432 | dlog_verbose( |
| 2433 | "Retriever specified address ranges not " |
| 2434 | "supported (got offset %d).\n", |
| 2435 | current_receiver |
| 2436 | ->composite_memory_region_offset); |
| 2437 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2438 | } |
| 2439 | |
| 2440 | /* |
| 2441 | * Check permissions from sender against permissions requested |
| 2442 | * by receiver. |
| 2443 | */ |
| 2444 | if (!ffa_memory_retrieve_is_memory_access_valid( |
| 2445 | ffa_get_data_access_attr(sent_permissions), |
| 2446 | ffa_get_data_access_attr(requested_permissions), |
| 2447 | ffa_get_instruction_access_attr(sent_permissions), |
| 2448 | ffa_get_instruction_access_attr( |
| 2449 | requested_permissions), |
| 2450 | found_to_id ? permissions : NULL)) { |
| 2451 | return ffa_error(FFA_DENIED); |
| 2452 | } |
| 2453 | |
| 2454 | /* |
| 2455 | * Can't request PM to clear memory if only provided with RO |
| 2456 | * permissions. |
| 2457 | */ |
| 2458 | if (found_to_id && |
| 2459 | (ffa_get_data_access_attr(*permissions) == |
| 2460 | FFA_DATA_ACCESS_RO) && |
| 2461 | (retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != |
| 2462 | 0U) { |
| 2463 | dlog_verbose( |
| 2464 | "Receiver has RO permissions can not request " |
| 2465 | "clear.\n"); |
| 2466 | return ffa_error(FFA_DENIED); |
| 2467 | } |
| 2468 | } |
| 2469 | |
| 2470 | if (retrieve_receiver_index == retrieve_request->receiver_count) { |
| 2471 | dlog_verbose( |
| 2472 | "Retrieve request does not contain caller's (%x) " |
| 2473 | "permissions\n", |
| 2474 | to_vm_id); |
| 2475 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 2476 | } |
| 2477 | |
| 2478 | return (struct ffa_value){.func = FFA_SUCCESS_32}; |
| 2479 | } |
| 2480 | |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 2481 | /* |
| 2482 | * According to section 16.4.3 of FF-A v1.1 EAC0 specification, the hypervisor |
| 2483 | * may issue an FFA_MEM_RETRIEVE_REQ to obtain the memory region description |
| 2484 | * of a pending memory sharing operation whose allocator is the SPM, for |
| 2485 | * validation purposes before forwarding an FFA_MEM_RECLAIM call. In doing so |
| 2486 | * the memory region descriptor of the retrieve request must be zeroed with the |
| 2487 | * exception of the sender ID and handle. |
| 2488 | */ |
| 2489 | bool is_ffa_memory_retrieve_borrower_request(struct ffa_memory_region *request, |
| 2490 | struct vm_locked to_locked) |
| 2491 | { |
| 2492 | return to_locked.vm->id == HF_HYPERVISOR_VM_ID && |
| 2493 | request->attributes == 0U && request->flags == 0U && |
| 2494 | request->tag == 0U && request->receiver_count == 0U && |
| 2495 | plat_ffa_memory_handle_allocated_by_current_world( |
| 2496 | request->handle); |
| 2497 | } |
| 2498 | |
| 2499 | /* |
| 2500 | * Helper to reset count of fragments retrieved by the hypervisor. |
| 2501 | */ |
| 2502 | static void ffa_memory_retrieve_complete_from_hyp( |
| 2503 | struct ffa_memory_share_state *share_state) |
| 2504 | { |
| 2505 | if (share_state->hypervisor_fragment_count == |
| 2506 | share_state->fragment_count) { |
| 2507 | share_state->hypervisor_fragment_count = 0; |
| 2508 | } |
| 2509 | } |
| 2510 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2511 | struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked, |
| 2512 | struct ffa_memory_region *retrieve_request, |
Andrew Walbran | 130a8ae | 2020-05-15 16:27:15 +0100 | [diff] [blame] | 2513 | uint32_t retrieve_request_length, |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2514 | struct mpool *page_pool) |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2515 | { |
Andrew Walbran | 130a8ae | 2020-05-15 16:27:15 +0100 | [diff] [blame] | 2516 | uint32_t expected_retrieve_request_length = |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2517 | sizeof(struct ffa_memory_region) + |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 2518 | retrieve_request->receiver_count * |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2519 | sizeof(struct ffa_memory_access); |
| 2520 | ffa_memory_handle_t handle = retrieve_request->handle; |
| 2521 | ffa_memory_region_flags_t transaction_type = |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 2522 | retrieve_request->flags & |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2523 | FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK; |
| 2524 | struct ffa_memory_region *memory_region; |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 2525 | ffa_memory_access_permissions_t permissions = 0; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2526 | uint32_t memory_to_attributes; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2527 | struct share_states_locked share_states; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2528 | struct ffa_memory_share_state *share_state; |
| 2529 | struct ffa_value ret; |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2530 | struct ffa_composite_memory_region *composite; |
| 2531 | uint32_t total_length; |
| 2532 | uint32_t fragment_length; |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 2533 | ffa_vm_id_t receiver_id; |
| 2534 | bool is_send_complete = false; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2535 | |
| 2536 | dump_share_states(); |
| 2537 | |
Andrew Walbran | 130a8ae | 2020-05-15 16:27:15 +0100 | [diff] [blame] | 2538 | if (retrieve_request_length != expected_retrieve_request_length) { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2539 | dlog_verbose( |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2540 | "Invalid length for FFA_MEM_RETRIEVE_REQ, expected %d " |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2541 | "but was %d.\n", |
Andrew Walbran | 130a8ae | 2020-05-15 16:27:15 +0100 | [diff] [blame] | 2542 | expected_retrieve_request_length, |
| 2543 | retrieve_request_length); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2544 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2545 | } |
| 2546 | |
| 2547 | share_states = share_states_lock(); |
| 2548 | if (!get_share_state(share_states, handle, &share_state)) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2549 | dlog_verbose("Invalid handle %#x for FFA_MEM_RETRIEVE_REQ.\n", |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2550 | handle); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2551 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2552 | goto out; |
| 2553 | } |
| 2554 | |
J-Alves | 96de29f | 2022-04-26 16:05:24 +0100 | [diff] [blame] | 2555 | if (!share_state->sending_complete) { |
| 2556 | dlog_verbose( |
| 2557 | "Memory with handle %#x not fully sent, can't " |
| 2558 | "retrieve.\n", |
| 2559 | handle); |
| 2560 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2561 | goto out; |
| 2562 | } |
| 2563 | |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 2564 | memory_region = share_state->memory_region; |
| 2565 | CHECK(memory_region != NULL); |
| 2566 | |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 2567 | receiver_id = to_locked.vm->id; |
J-Alves | 96de29f | 2022-04-26 16:05:24 +0100 | [diff] [blame] | 2568 | |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 2569 | if (!is_ffa_memory_retrieve_borrower_request(retrieve_request, |
| 2570 | to_locked)) { |
| 2571 | uint32_t receiver_index; |
J-Alves | 614d9f4 | 2022-06-28 14:03:10 +0100 | [diff] [blame] | 2572 | /* |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 2573 | * Find receiver index in the receivers list specified by the |
| 2574 | * sender. |
J-Alves | 614d9f4 | 2022-06-28 14:03:10 +0100 | [diff] [blame] | 2575 | */ |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 2576 | receiver_index = ffa_memory_region_get_receiver( |
| 2577 | memory_region, to_locked.vm->id); |
| 2578 | |
| 2579 | if (receiver_index == memory_region->receiver_count) { |
| 2580 | dlog_verbose( |
| 2581 | "Incorrect receiver VM ID %x for " |
| 2582 | "FFA_MEM_RETRIEVE_REQ, " |
| 2583 | "for handle %#x.\n", |
| 2584 | to_locked.vm->id, handle); |
| 2585 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2586 | goto out; |
| 2587 | } |
| 2588 | |
| 2589 | if (share_state->retrieved_fragment_count[receiver_index] != |
| 2590 | 0U) { |
| 2591 | dlog_verbose( |
| 2592 | "Memory with handle %#x already retrieved.\n", |
| 2593 | handle); |
| 2594 | ret = ffa_error(FFA_DENIED); |
| 2595 | goto out; |
| 2596 | } |
| 2597 | |
| 2598 | /* |
| 2599 | * Check that the transaction type expected by the receiver is |
| 2600 | * correct, if it has been specified. |
| 2601 | */ |
| 2602 | if (transaction_type != |
| 2603 | FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED && |
| 2604 | transaction_type != |
| 2605 | (memory_region->flags & |
| 2606 | FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK)) { |
| 2607 | dlog_verbose( |
| 2608 | "Incorrect transaction type %#x for " |
| 2609 | "FFA_MEM_RETRIEVE_REQ, expected %#x for handle " |
| 2610 | "%#x.\n", |
| 2611 | transaction_type, |
| 2612 | memory_region->flags & |
| 2613 | FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK, |
| 2614 | handle); |
| 2615 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2616 | goto out; |
| 2617 | } |
| 2618 | |
| 2619 | if (retrieve_request->sender != memory_region->sender) { |
| 2620 | dlog_verbose( |
| 2621 | "Incorrect sender ID %d for " |
| 2622 | "FFA_MEM_RETRIEVE_REQ, " |
| 2623 | "expected %d for handle %#x.\n", |
| 2624 | retrieve_request->sender, memory_region->sender, |
| 2625 | handle); |
| 2626 | ret = ffa_error(FFA_DENIED); |
| 2627 | goto out; |
| 2628 | } |
| 2629 | |
| 2630 | if (retrieve_request->tag != memory_region->tag) { |
| 2631 | dlog_verbose( |
| 2632 | "Incorrect tag %d for FFA_MEM_RETRIEVE_REQ, " |
| 2633 | "expected " |
| 2634 | "%d for handle %#x.\n", |
| 2635 | retrieve_request->tag, memory_region->tag, |
| 2636 | handle); |
| 2637 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2638 | goto out; |
| 2639 | } |
| 2640 | |
| 2641 | if ((retrieve_request->flags & |
| 2642 | FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID) != 0U) { |
| 2643 | dlog_verbose( |
| 2644 | "Retriever specified 'address range alignment " |
| 2645 | "hint' not supported.\n"); |
| 2646 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2647 | goto out; |
| 2648 | } |
| 2649 | |
| 2650 | if ((retrieve_request->flags & |
| 2651 | FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK) != 0) { |
| 2652 | dlog_verbose( |
| 2653 | "Bits 8-5 must be zero in memory region's " |
| 2654 | "flags (address range alignment hint not " |
| 2655 | "supported).\n"); |
| 2656 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2657 | goto out; |
| 2658 | } |
| 2659 | |
| 2660 | if ((retrieve_request->flags & ~0x7FF) != 0U) { |
| 2661 | dlog_verbose( |
| 2662 | "Bits 31-10 must be zero in memory region's " |
| 2663 | "flags.\n"); |
| 2664 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2665 | goto out; |
| 2666 | } |
| 2667 | |
| 2668 | if (share_state->share_func == FFA_MEM_SHARE_32 && |
| 2669 | (retrieve_request->flags & |
| 2670 | (FFA_MEMORY_REGION_FLAG_CLEAR | |
| 2671 | FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH)) != 0U) { |
| 2672 | dlog_verbose( |
| 2673 | "Memory Share operation can't clean after " |
| 2674 | "relinquish " |
| 2675 | "memory region.\n"); |
| 2676 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2677 | goto out; |
| 2678 | } |
| 2679 | |
| 2680 | /* |
| 2681 | * If the borrower needs the memory to be cleared before mapping |
| 2682 | * to its address space, the sender should have set the flag |
| 2683 | * when calling FFA_MEM_LEND/FFA_MEM_DONATE, else return |
| 2684 | * FFA_DENIED. |
| 2685 | */ |
| 2686 | if ((retrieve_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR) != |
| 2687 | 0U && |
| 2688 | (memory_region->flags & FFA_MEMORY_REGION_FLAG_CLEAR) == |
| 2689 | 0U) { |
| 2690 | dlog_verbose( |
| 2691 | "Borrower needs memory cleared. Sender needs " |
| 2692 | "to set " |
| 2693 | "flag for clearing memory.\n"); |
| 2694 | ret = ffa_error(FFA_DENIED); |
| 2695 | goto out; |
| 2696 | } |
| 2697 | |
| 2698 | ret = ffa_memory_retrieve_validate_memory_access_list( |
| 2699 | memory_region, retrieve_request, receiver_id, |
| 2700 | &permissions); |
J-Alves | 614d9f4 | 2022-06-28 14:03:10 +0100 | [diff] [blame] | 2701 | if (ret.func != FFA_SUCCESS_32) { |
| 2702 | goto out; |
| 2703 | } |
Federico Recanati | a98603a | 2021-12-20 18:04:03 +0100 | [diff] [blame] | 2704 | |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 2705 | if (ffa_get_memory_type_attr(retrieve_request->attributes) != |
| 2706 | FFA_MEMORY_NOT_SPECIFIED_MEM) { |
| 2707 | /* |
| 2708 | * Ensure receiver's attributes are compatible with how |
| 2709 | * Hafnium maps memory: Normal Memory, Inner shareable, |
| 2710 | * Write-Back Read-Allocate Write-Allocate Cacheable. |
| 2711 | */ |
| 2712 | ret = ffa_memory_attributes_validate( |
| 2713 | retrieve_request->attributes); |
| 2714 | if (ret.func != FFA_SUCCESS_32) { |
| 2715 | goto out; |
| 2716 | } |
| 2717 | } |
| 2718 | |
| 2719 | memory_to_attributes = ffa_memory_permissions_to_mode( |
| 2720 | permissions, share_state->sender_orig_mode); |
| 2721 | ret = ffa_retrieve_check_update( |
| 2722 | to_locked, memory_region->sender, |
| 2723 | share_state->fragments, |
| 2724 | share_state->fragment_constituent_counts, |
| 2725 | share_state->fragment_count, memory_to_attributes, |
| 2726 | share_state->share_func, false, page_pool); |
| 2727 | |
| 2728 | if (ret.func != FFA_SUCCESS_32) { |
| 2729 | goto out; |
| 2730 | } |
| 2731 | |
| 2732 | share_state->retrieved_fragment_count[receiver_index] = 1; |
| 2733 | is_send_complete = |
| 2734 | share_state->retrieved_fragment_count[receiver_index] == |
| 2735 | share_state->fragment_count; |
| 2736 | } else { |
| 2737 | if (share_state->hypervisor_fragment_count != 0U) { |
| 2738 | dlog_verbose( |
| 2739 | "Memory with handle %#x already " |
| 2740 | "retrieved by " |
| 2741 | "the hypervisor.\n", |
| 2742 | handle); |
| 2743 | ret = ffa_error(FFA_DENIED); |
| 2744 | goto out; |
| 2745 | } |
| 2746 | |
| 2747 | share_state->hypervisor_fragment_count = 1; |
| 2748 | |
| 2749 | ffa_memory_retrieve_complete_from_hyp(share_state); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2750 | } |
| 2751 | |
| 2752 | /* |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 2753 | * Copy response to RX buffer of caller and deliver the message. |
| 2754 | * This must be done before the share_state is (possibly) freed. |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2755 | */ |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 2756 | /* TODO: combine attributes from sender and request. */ |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2757 | composite = ffa_memory_region_get_composite(memory_region, 0); |
| 2758 | /* |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 2759 | * Constituents which we received in the first fragment should |
| 2760 | * always fit in the first fragment we are sending, because the |
| 2761 | * header is the same size in both cases and we have a fixed |
| 2762 | * message buffer size. So `ffa_retrieved_memory_region_init` |
| 2763 | * should never fail. |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2764 | */ |
| 2765 | CHECK(ffa_retrieved_memory_region_init( |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 2766 | to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE, |
| 2767 | memory_region->sender, memory_region->attributes, |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 2768 | memory_region->flags, handle, receiver_id, permissions, |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2769 | composite->page_count, composite->constituent_count, |
| 2770 | share_state->fragments[0], |
| 2771 | share_state->fragment_constituent_counts[0], &total_length, |
| 2772 | &fragment_length)); |
| 2773 | to_locked.vm->mailbox.recv_size = fragment_length; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2774 | to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2775 | to_locked.vm->mailbox.recv_func = FFA_MEM_RETRIEVE_RESP_32; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2776 | to_locked.vm->mailbox.state = MAILBOX_STATE_READ; |
| 2777 | |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 2778 | if (is_send_complete) { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2779 | ffa_memory_retrieve_complete(share_states, share_state, |
| 2780 | page_pool); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2781 | } |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2782 | ret = (struct ffa_value){.func = FFA_MEM_RETRIEVE_RESP_32, |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2783 | .arg1 = total_length, |
| 2784 | .arg2 = fragment_length}; |
| 2785 | |
| 2786 | out: |
| 2787 | share_states_unlock(&share_states); |
| 2788 | dump_share_states(); |
| 2789 | return ret; |
| 2790 | } |
| 2791 | |
| 2792 | struct ffa_value ffa_memory_retrieve_continue(struct vm_locked to_locked, |
| 2793 | ffa_memory_handle_t handle, |
| 2794 | uint32_t fragment_offset, |
| 2795 | struct mpool *page_pool) |
| 2796 | { |
| 2797 | struct ffa_memory_region *memory_region; |
| 2798 | struct share_states_locked share_states; |
| 2799 | struct ffa_memory_share_state *share_state; |
| 2800 | struct ffa_value ret; |
| 2801 | uint32_t fragment_index; |
| 2802 | uint32_t retrieved_constituents_count; |
| 2803 | uint32_t i; |
| 2804 | uint32_t expected_fragment_offset; |
| 2805 | uint32_t remaining_constituent_count; |
| 2806 | uint32_t fragment_length; |
J-Alves | c7484f1 | 2022-05-13 12:41:14 +0100 | [diff] [blame] | 2807 | uint32_t receiver_index; |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2808 | |
| 2809 | dump_share_states(); |
| 2810 | |
| 2811 | share_states = share_states_lock(); |
| 2812 | if (!get_share_state(share_states, handle, &share_state)) { |
| 2813 | dlog_verbose("Invalid handle %#x for FFA_MEM_FRAG_RX.\n", |
| 2814 | handle); |
| 2815 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2816 | goto out; |
| 2817 | } |
| 2818 | |
| 2819 | memory_region = share_state->memory_region; |
| 2820 | CHECK(memory_region != NULL); |
| 2821 | |
J-Alves | c7484f1 | 2022-05-13 12:41:14 +0100 | [diff] [blame] | 2822 | receiver_index = |
| 2823 | ffa_memory_region_get_receiver(memory_region, to_locked.vm->id); |
| 2824 | |
| 2825 | if (receiver_index == memory_region->receiver_count) { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2826 | dlog_verbose( |
J-Alves | c7484f1 | 2022-05-13 12:41:14 +0100 | [diff] [blame] | 2827 | "Caller of FFA_MEM_FRAG_RX (%x) is not a borrower to " |
| 2828 | "memory sharing transaction (%x)\n", |
| 2829 | to_locked.vm->id, handle); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2830 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2831 | goto out; |
| 2832 | } |
| 2833 | |
| 2834 | if (!share_state->sending_complete) { |
| 2835 | dlog_verbose( |
| 2836 | "Memory with handle %#x not fully sent, can't " |
| 2837 | "retrieve.\n", |
| 2838 | handle); |
| 2839 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2840 | goto out; |
| 2841 | } |
| 2842 | |
J-Alves | c7484f1 | 2022-05-13 12:41:14 +0100 | [diff] [blame] | 2843 | if (share_state->retrieved_fragment_count[receiver_index] == 0 || |
| 2844 | share_state->retrieved_fragment_count[receiver_index] >= |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2845 | share_state->fragment_count) { |
| 2846 | dlog_verbose( |
| 2847 | "Retrieval of memory with handle %#x not yet started " |
| 2848 | "or already completed (%d/%d fragments retrieved).\n", |
J-Alves | c7484f1 | 2022-05-13 12:41:14 +0100 | [diff] [blame] | 2849 | handle, |
| 2850 | share_state->retrieved_fragment_count[receiver_index], |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2851 | share_state->fragment_count); |
| 2852 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2853 | goto out; |
| 2854 | } |
| 2855 | |
J-Alves | c7484f1 | 2022-05-13 12:41:14 +0100 | [diff] [blame] | 2856 | fragment_index = share_state->retrieved_fragment_count[receiver_index]; |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2857 | |
| 2858 | /* |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 2859 | * Check that the given fragment offset is correct by counting |
| 2860 | * how many constituents were in the fragments previously sent. |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2861 | */ |
| 2862 | retrieved_constituents_count = 0; |
| 2863 | for (i = 0; i < fragment_index; ++i) { |
| 2864 | retrieved_constituents_count += |
| 2865 | share_state->fragment_constituent_counts[i]; |
| 2866 | } |
J-Alves | c7484f1 | 2022-05-13 12:41:14 +0100 | [diff] [blame] | 2867 | |
| 2868 | CHECK(memory_region->receiver_count > 0); |
| 2869 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2870 | expected_fragment_offset = |
J-Alves | c7484f1 | 2022-05-13 12:41:14 +0100 | [diff] [blame] | 2871 | ffa_composite_constituent_offset(memory_region, |
| 2872 | receiver_index) + |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2873 | retrieved_constituents_count * |
J-Alves | c7484f1 | 2022-05-13 12:41:14 +0100 | [diff] [blame] | 2874 | sizeof(struct ffa_memory_region_constituent) - |
| 2875 | sizeof(struct ffa_memory_access) * |
| 2876 | (memory_region->receiver_count - 1); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2877 | if (fragment_offset != expected_fragment_offset) { |
| 2878 | dlog_verbose("Fragment offset was %d but expected %d.\n", |
| 2879 | fragment_offset, expected_fragment_offset); |
| 2880 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2881 | goto out; |
| 2882 | } |
| 2883 | |
| 2884 | remaining_constituent_count = ffa_memory_fragment_init( |
| 2885 | to_locked.vm->mailbox.recv, HF_MAILBOX_SIZE, |
| 2886 | share_state->fragments[fragment_index], |
| 2887 | share_state->fragment_constituent_counts[fragment_index], |
| 2888 | &fragment_length); |
| 2889 | CHECK(remaining_constituent_count == 0); |
| 2890 | to_locked.vm->mailbox.recv_size = fragment_length; |
| 2891 | to_locked.vm->mailbox.recv_sender = HF_HYPERVISOR_VM_ID; |
| 2892 | to_locked.vm->mailbox.recv_func = FFA_MEM_FRAG_TX_32; |
| 2893 | to_locked.vm->mailbox.state = MAILBOX_STATE_READ; |
J-Alves | c7484f1 | 2022-05-13 12:41:14 +0100 | [diff] [blame] | 2894 | share_state->retrieved_fragment_count[receiver_index]++; |
| 2895 | if (share_state->retrieved_fragment_count[receiver_index] == |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2896 | share_state->fragment_count) { |
| 2897 | ffa_memory_retrieve_complete(share_states, share_state, |
| 2898 | page_pool); |
| 2899 | } |
| 2900 | |
| 2901 | ret = (struct ffa_value){.func = FFA_MEM_FRAG_TX_32, |
| 2902 | .arg1 = (uint32_t)handle, |
| 2903 | .arg2 = (uint32_t)(handle >> 32), |
| 2904 | .arg3 = fragment_length}; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2905 | |
| 2906 | out: |
| 2907 | share_states_unlock(&share_states); |
| 2908 | dump_share_states(); |
| 2909 | return ret; |
| 2910 | } |
| 2911 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2912 | struct ffa_value ffa_memory_relinquish( |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2913 | struct vm_locked from_locked, |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2914 | struct ffa_mem_relinquish *relinquish_request, struct mpool *page_pool) |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2915 | { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2916 | ffa_memory_handle_t handle = relinquish_request->handle; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2917 | struct share_states_locked share_states; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2918 | struct ffa_memory_share_state *share_state; |
| 2919 | struct ffa_memory_region *memory_region; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2920 | bool clear; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2921 | struct ffa_value ret; |
J-Alves | 8eb1916 | 2022-04-28 10:56:48 +0100 | [diff] [blame] | 2922 | uint32_t receiver_index; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2923 | |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 2924 | if (relinquish_request->endpoint_count != 1) { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2925 | dlog_verbose( |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 2926 | "Stream endpoints not supported (got %d " |
| 2927 | "endpoints on " |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2928 | "FFA_MEM_RELINQUISH, expected 1).\n", |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2929 | relinquish_request->endpoint_count); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2930 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2931 | } |
| 2932 | |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 2933 | if (relinquish_request->endpoints[0] != from_locked.vm->id) { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2934 | dlog_verbose( |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 2935 | "VM ID %d in relinquish message doesn't match " |
| 2936 | "calling " |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2937 | "VM ID %d.\n", |
Andrew Walbran | a65a132 | 2020-04-06 19:32:32 +0100 | [diff] [blame] | 2938 | relinquish_request->endpoints[0], from_locked.vm->id); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2939 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2940 | } |
| 2941 | |
| 2942 | dump_share_states(); |
| 2943 | |
| 2944 | share_states = share_states_lock(); |
| 2945 | if (!get_share_state(share_states, handle, &share_state)) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2946 | dlog_verbose("Invalid handle %#x for FFA_MEM_RELINQUISH.\n", |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2947 | handle); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2948 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2949 | goto out; |
| 2950 | } |
| 2951 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2952 | if (!share_state->sending_complete) { |
| 2953 | dlog_verbose( |
| 2954 | "Memory with handle %#x not fully sent, can't " |
| 2955 | "relinquish.\n", |
| 2956 | handle); |
| 2957 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 2958 | goto out; |
| 2959 | } |
| 2960 | |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2961 | memory_region = share_state->memory_region; |
| 2962 | CHECK(memory_region != NULL); |
| 2963 | |
J-Alves | 8eb1916 | 2022-04-28 10:56:48 +0100 | [diff] [blame] | 2964 | receiver_index = ffa_memory_region_get_receiver(memory_region, |
| 2965 | from_locked.vm->id); |
| 2966 | |
| 2967 | if (receiver_index == memory_region->receiver_count) { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2968 | dlog_verbose( |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 2969 | "VM ID %d tried to relinquish memory region " |
| 2970 | "with " |
J-Alves | 8eb1916 | 2022-04-28 10:56:48 +0100 | [diff] [blame] | 2971 | "handle %#x and it is not a valid borrower.\n", |
| 2972 | from_locked.vm->id, handle); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2973 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2974 | goto out; |
| 2975 | } |
| 2976 | |
J-Alves | 8eb1916 | 2022-04-28 10:56:48 +0100 | [diff] [blame] | 2977 | if (share_state->retrieved_fragment_count[receiver_index] != |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 2978 | share_state->fragment_count) { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2979 | dlog_verbose( |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 2980 | "Memory with handle %#x not yet fully " |
| 2981 | "retrieved, " |
J-Alves | 8eb1916 | 2022-04-28 10:56:48 +0100 | [diff] [blame] | 2982 | "receiver %x can't relinquish.\n", |
| 2983 | handle, from_locked.vm->id); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2984 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2985 | goto out; |
| 2986 | } |
| 2987 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2988 | clear = relinquish_request->flags & FFA_MEMORY_REGION_FLAG_CLEAR; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2989 | |
| 2990 | /* |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 2991 | * Clear is not allowed for memory that was shared, as the |
| 2992 | * original sender still has access to the memory. |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2993 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2994 | if (clear && share_state->share_func == FFA_MEM_SHARE_32) { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2995 | dlog_verbose("Memory which was shared can't be cleared.\n"); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 2996 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 2997 | goto out; |
| 2998 | } |
| 2999 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3000 | ret = ffa_relinquish_check_update( |
| 3001 | from_locked, share_state->fragments, |
| 3002 | share_state->fragment_constituent_counts, |
| 3003 | share_state->fragment_count, page_pool, clear); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3004 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3005 | if (ret.func == FFA_SUCCESS_32) { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3006 | /* |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 3007 | * Mark memory handle as not retrieved, so it can be |
| 3008 | * reclaimed (or retrieved again). |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3009 | */ |
J-Alves | 8eb1916 | 2022-04-28 10:56:48 +0100 | [diff] [blame] | 3010 | share_state->retrieved_fragment_count[receiver_index] = 0; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3011 | } |
| 3012 | |
| 3013 | out: |
| 3014 | share_states_unlock(&share_states); |
| 3015 | dump_share_states(); |
| 3016 | return ret; |
| 3017 | } |
| 3018 | |
| 3019 | /** |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 3020 | * Validates that the reclaim transition is allowed for the given |
| 3021 | * handle, updates the page table of the reclaiming VM, and frees the |
| 3022 | * internal state associated with the handle. |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3023 | */ |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3024 | struct ffa_value ffa_memory_reclaim(struct vm_locked to_locked, |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3025 | ffa_memory_handle_t handle, |
| 3026 | ffa_memory_region_flags_t flags, |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3027 | struct mpool *page_pool) |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3028 | { |
| 3029 | struct share_states_locked share_states; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3030 | struct ffa_memory_share_state *share_state; |
| 3031 | struct ffa_memory_region *memory_region; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3032 | struct ffa_value ret; |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3033 | |
| 3034 | dump_share_states(); |
| 3035 | |
| 3036 | share_states = share_states_lock(); |
| 3037 | if (!get_share_state(share_states, handle, &share_state)) { |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3038 | dlog_verbose("Invalid handle %#x for FFA_MEM_RECLAIM.\n", |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3039 | handle); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3040 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3041 | goto out; |
| 3042 | } |
| 3043 | |
| 3044 | memory_region = share_state->memory_region; |
| 3045 | CHECK(memory_region != NULL); |
| 3046 | |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 3047 | if (vm_id_is_current_world(to_locked.vm->id) && |
| 3048 | to_locked.vm->id != memory_region->sender) { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3049 | dlog_verbose( |
Olivier Deprez | f92e5d4 | 2020-11-13 16:00:54 +0100 | [diff] [blame] | 3050 | "VM %#x attempted to reclaim memory handle %#x " |
| 3051 | "originally sent by VM %#x.\n", |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3052 | to_locked.vm->id, handle, memory_region->sender); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3053 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3054 | goto out; |
| 3055 | } |
| 3056 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3057 | if (!share_state->sending_complete) { |
| 3058 | dlog_verbose( |
| 3059 | "Memory with handle %#x not fully sent, can't " |
| 3060 | "reclaim.\n", |
| 3061 | handle); |
| 3062 | ret = ffa_error(FFA_INVALID_PARAMETERS); |
| 3063 | goto out; |
| 3064 | } |
| 3065 | |
J-Alves | 752236c | 2022-04-28 11:07:47 +0100 | [diff] [blame] | 3066 | for (uint32_t i = 0; i < memory_region->receiver_count; i++) { |
| 3067 | if (share_state->retrieved_fragment_count[i] != 0) { |
| 3068 | dlog_verbose( |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 3069 | "Tried to reclaim memory handle %#x " |
| 3070 | "that has " |
| 3071 | "not been relinquished by all " |
| 3072 | "borrowers(%x).\n", |
J-Alves | 752236c | 2022-04-28 11:07:47 +0100 | [diff] [blame] | 3073 | handle, |
| 3074 | memory_region->receivers[i] |
| 3075 | .receiver_permissions.receiver); |
| 3076 | ret = ffa_error(FFA_DENIED); |
| 3077 | goto out; |
| 3078 | } |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3079 | } |
| 3080 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3081 | ret = ffa_retrieve_check_update( |
J-Alves | 7db3200 | 2021-12-14 14:44:50 +0000 | [diff] [blame] | 3082 | to_locked, memory_region->sender, share_state->fragments, |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3083 | share_state->fragment_constituent_counts, |
J-Alves | 2a0d288 | 2020-10-29 14:49:50 +0000 | [diff] [blame] | 3084 | share_state->fragment_count, share_state->sender_orig_mode, |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3085 | FFA_MEM_RECLAIM_32, flags & FFA_MEM_RECLAIM_CLEAR, page_pool); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3086 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3087 | if (ret.func == FFA_SUCCESS_32) { |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3088 | share_state_free(share_states, share_state, page_pool); |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 3089 | dlog_verbose( |
| 3090 | "Freed share state after successful " |
| 3091 | "reclaim.\n"); |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 3092 | } |
| 3093 | |
| 3094 | out: |
| 3095 | share_states_unlock(&share_states); |
| 3096 | return ret; |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 3097 | } |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 3098 | |
| 3099 | /** |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 3100 | * Validates that the reclaim transition is allowed for the memory |
| 3101 | * region with the given handle which was previously shared with the |
| 3102 | * other world. Tells the other world to mark it as reclaimed, and |
| 3103 | * updates the page table of the reclaiming VM. |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3104 | * |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 3105 | * To do this information about the memory region is first fetched from |
| 3106 | * the other world. |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 3107 | */ |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 3108 | struct ffa_value ffa_memory_other_world_reclaim(struct vm_locked to_locked, |
| 3109 | struct vm_locked from_locked, |
| 3110 | ffa_memory_handle_t handle, |
| 3111 | ffa_memory_region_flags_t flags, |
| 3112 | struct mpool *page_pool) |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 3113 | { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3114 | uint32_t request_length = ffa_memory_lender_retrieve_request_init( |
| 3115 | from_locked.vm->mailbox.recv, handle, to_locked.vm->id); |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 3116 | struct ffa_value other_world_ret; |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3117 | uint32_t length; |
| 3118 | uint32_t fragment_length; |
| 3119 | uint32_t fragment_offset; |
| 3120 | struct ffa_memory_region *memory_region; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3121 | struct ffa_composite_memory_region *composite; |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3122 | uint32_t memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X; |
| 3123 | |
| 3124 | CHECK(request_length <= HF_MAILBOX_SIZE); |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 3125 | CHECK(from_locked.vm->id == HF_OTHER_WORLD_ID); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3126 | |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 3127 | /* Retrieve memory region information from the other world. */ |
| 3128 | other_world_ret = arch_other_world_call( |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3129 | (struct ffa_value){.func = FFA_MEM_RETRIEVE_REQ_32, |
| 3130 | .arg1 = request_length, |
| 3131 | .arg2 = request_length}); |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 3132 | if (other_world_ret.func == FFA_ERROR_32) { |
| 3133 | dlog_verbose("Got error %d from EL3.\n", other_world_ret.arg2); |
| 3134 | return other_world_ret; |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3135 | } |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 3136 | if (other_world_ret.func != FFA_MEM_RETRIEVE_RESP_32) { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3137 | dlog_verbose( |
| 3138 | "Got %#x from EL3, expected FFA_MEM_RETRIEVE_RESP.\n", |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 3139 | other_world_ret.func); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3140 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3141 | } |
| 3142 | |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 3143 | length = other_world_ret.arg1; |
| 3144 | fragment_length = other_world_ret.arg2; |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3145 | |
| 3146 | if (fragment_length > HF_MAILBOX_SIZE || fragment_length > length || |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 3147 | length > sizeof(other_world_retrieve_buffer)) { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3148 | dlog_verbose("Invalid fragment length %d/%d (max %d/%d).\n", |
| 3149 | fragment_length, length, HF_MAILBOX_SIZE, |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 3150 | sizeof(other_world_retrieve_buffer)); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3151 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3152 | } |
| 3153 | |
| 3154 | /* |
| 3155 | * Copy the first fragment of the memory region descriptor to an |
| 3156 | * internal buffer. |
| 3157 | */ |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 3158 | memcpy_s(other_world_retrieve_buffer, |
| 3159 | sizeof(other_world_retrieve_buffer), |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3160 | from_locked.vm->mailbox.send, fragment_length); |
| 3161 | |
| 3162 | /* Fetch the remaining fragments into the same buffer. */ |
| 3163 | fragment_offset = fragment_length; |
| 3164 | while (fragment_offset < length) { |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 3165 | other_world_ret = arch_other_world_call( |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3166 | (struct ffa_value){.func = FFA_MEM_FRAG_RX_32, |
| 3167 | .arg1 = (uint32_t)handle, |
| 3168 | .arg2 = (uint32_t)(handle >> 32), |
| 3169 | .arg3 = fragment_offset}); |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 3170 | if (other_world_ret.func != FFA_MEM_FRAG_TX_32) { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3171 | dlog_verbose( |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 3172 | "Got %#x (%d) from other world in response to " |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3173 | "FFA_MEM_FRAG_RX, expected FFA_MEM_FRAG_TX.\n", |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 3174 | other_world_ret.func, other_world_ret.arg2); |
| 3175 | return other_world_ret; |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3176 | } |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 3177 | if (ffa_frag_handle(other_world_ret) != handle) { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3178 | dlog_verbose( |
| 3179 | "Got FFA_MEM_FRAG_TX for unexpected handle %#x " |
| 3180 | "in response to FFA_MEM_FRAG_RX for handle " |
| 3181 | "%#x.\n", |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 3182 | ffa_frag_handle(other_world_ret), handle); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3183 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3184 | } |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 3185 | if (ffa_frag_sender(other_world_ret) != 0) { |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3186 | dlog_verbose( |
| 3187 | "Got FFA_MEM_FRAG_TX with unexpected sender %d " |
| 3188 | "(expected 0).\n", |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 3189 | ffa_frag_sender(other_world_ret)); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3190 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3191 | } |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 3192 | fragment_length = other_world_ret.arg3; |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3193 | if (fragment_length > HF_MAILBOX_SIZE || |
| 3194 | fragment_offset + fragment_length > length) { |
| 3195 | dlog_verbose( |
| 3196 | "Invalid fragment length %d at offset %d (max " |
| 3197 | "%d).\n", |
| 3198 | fragment_length, fragment_offset, |
| 3199 | HF_MAILBOX_SIZE); |
| 3200 | return ffa_error(FFA_INVALID_PARAMETERS); |
| 3201 | } |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 3202 | memcpy_s(other_world_retrieve_buffer + fragment_offset, |
| 3203 | sizeof(other_world_retrieve_buffer) - fragment_offset, |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3204 | from_locked.vm->mailbox.send, fragment_length); |
| 3205 | |
| 3206 | fragment_offset += fragment_length; |
| 3207 | } |
| 3208 | |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 3209 | memory_region = (struct ffa_memory_region *)other_world_retrieve_buffer; |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 3210 | |
| 3211 | if (memory_region->receiver_count != 1) { |
| 3212 | /* Only one receiver supported by Hafnium for now. */ |
| 3213 | dlog_verbose( |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 3214 | "Multiple recipients not supported (got %d, " |
| 3215 | "expected 1).\n", |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 3216 | memory_region->receiver_count); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3217 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 3218 | } |
| 3219 | |
| 3220 | if (memory_region->handle != handle) { |
| 3221 | dlog_verbose( |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 3222 | "Got memory region handle %#x from other world " |
| 3223 | "but requested handle %#x.\n", |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 3224 | memory_region->handle, handle); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3225 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 3226 | } |
| 3227 | |
| 3228 | /* The original sender must match the caller. */ |
| 3229 | if (to_locked.vm->id != memory_region->sender) { |
| 3230 | dlog_verbose( |
Olivier Deprez | f92e5d4 | 2020-11-13 16:00:54 +0100 | [diff] [blame] | 3231 | "VM %#x attempted to reclaim memory handle %#x " |
| 3232 | "originally sent by VM %#x.\n", |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 3233 | to_locked.vm->id, handle, memory_region->sender); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3234 | return ffa_error(FFA_INVALID_PARAMETERS); |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 3235 | } |
| 3236 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 3237 | composite = ffa_memory_region_get_composite(memory_region, 0); |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 3238 | |
| 3239 | /* |
J-Alves | a9cd7e3 | 2022-07-01 13:49:33 +0100 | [diff] [blame] | 3240 | * Validate that the reclaim transition is allowed for the given |
| 3241 | * memory region, forward the request to the other world and |
| 3242 | * then map the memory back into the caller's stage-2 page |
| 3243 | * table. |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 3244 | */ |
J-Alves | 8505a8a | 2022-06-15 18:10:18 +0100 | [diff] [blame] | 3245 | return ffa_other_world_reclaim_check_update( |
Andrew Walbran | 996d1d1 | 2020-05-27 14:08:43 +0100 | [diff] [blame] | 3246 | to_locked, handle, composite->constituents, |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 3247 | composite->constituent_count, memory_to_attributes, |
| 3248 | flags & FFA_MEM_RECLAIM_CLEAR, page_pool); |
Andrew Walbran | 290b0c9 | 2020-02-03 16:37:14 +0000 | [diff] [blame] | 3249 | } |