J-Alves | 6665225 | 2022-07-06 09:49:51 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022 The Hafnium Authors. |
| 3 | * |
| 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. |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * The maximum number of memory sharing handles which may be active at once. A |
| 11 | * DONATE handle is active from when it is sent to when it is retrieved; a SHARE |
| 12 | * or LEND handle is active from when it is sent to when it is reclaimed. |
| 13 | */ |
J-Alves | 460d36c | 2023-10-12 17:02:15 +0100 | [diff] [blame] | 14 | #pragma once |
| 15 | |
J-Alves | 6665225 | 2022-07-06 09:49:51 +0100 | [diff] [blame] | 16 | #define MAX_MEM_SHARES 100 |
| 17 | |
J-Alves | d15905d | 2023-02-20 11:52:37 +0000 | [diff] [blame] | 18 | #include <stdbool.h> |
| 19 | #include <stdint.h> |
| 20 | |
| 21 | #include "hf/check.h" |
| 22 | #include "hf/ffa_memory.h" |
| 23 | #include "hf/mpool.h" |
| 24 | #include "hf/vm.h" |
| 25 | |
| 26 | #include "vmapi/hf/ffa.h" |
| 27 | |
J-Alves | 6665225 | 2022-07-06 09:49:51 +0100 | [diff] [blame] | 28 | /** |
| 29 | * The maximum number of fragments into which a memory sharing message may be |
| 30 | * broken. |
| 31 | */ |
| 32 | #define MAX_FRAGMENTS 20 |
| 33 | |
| 34 | static_assert(sizeof(struct ffa_memory_region_constituent) % 16 == 0, |
| 35 | "struct ffa_memory_region_constituent must be a multiple of 16 " |
| 36 | "bytes long."); |
| 37 | static_assert(sizeof(struct ffa_composite_memory_region) % 16 == 0, |
| 38 | "struct ffa_composite_memory_region must be a multiple of 16 " |
| 39 | "bytes long."); |
| 40 | static_assert(sizeof(struct ffa_memory_region_attributes) == 4, |
| 41 | "struct ffa_memory_region_attributes must be 4 bytes long."); |
| 42 | static_assert(sizeof(struct ffa_memory_access) % 16 == 0, |
| 43 | "struct ffa_memory_access must be a multiple of 16 bytes long."); |
| 44 | static_assert(sizeof(struct ffa_memory_region) % 16 == 0, |
| 45 | "struct ffa_memory_region must be a multiple of 16 bytes long."); |
| 46 | static_assert(sizeof(struct ffa_mem_relinquish) % 16 == 0, |
| 47 | "struct ffa_mem_relinquish must be a multiple of 16 " |
| 48 | "bytes long."); |
Demi Marie Obenour | d467741 | 2023-02-03 20:35:12 -0500 | [diff] [blame] | 49 | static_assert(sizeof(((struct ffa_memory_region){0}).receiver_count == 4), |
| 50 | "struct ffa_memory_region::receiver_count must be 4 bytes long"); |
J-Alves | 6665225 | 2022-07-06 09:49:51 +0100 | [diff] [blame] | 51 | |
| 52 | struct ffa_memory_share_state { |
| 53 | /** |
| 54 | * The memory region being shared, or NULL if this share state is |
| 55 | * unallocated. |
| 56 | */ |
| 57 | struct ffa_memory_region *memory_region; |
| 58 | |
| 59 | struct ffa_memory_region_constituent *fragments[MAX_FRAGMENTS]; |
| 60 | |
| 61 | /** The number of constituents in each fragment. */ |
| 62 | uint32_t fragment_constituent_counts[MAX_FRAGMENTS]; |
| 63 | |
| 64 | /** |
| 65 | * The number of valid elements in the `fragments` and |
| 66 | * `fragment_constituent_counts` arrays. |
| 67 | */ |
| 68 | uint32_t fragment_count; |
| 69 | |
| 70 | /** |
| 71 | * The FF-A function used for sharing the memory. Must be one of |
| 72 | * FFA_MEM_DONATE_32, FFA_MEM_LEND_32 or FFA_MEM_SHARE_32 if the |
| 73 | * share state is allocated, or 0. |
| 74 | */ |
| 75 | uint32_t share_func; |
| 76 | |
| 77 | /** |
| 78 | * The sender's original mode before invoking the FF-A function for |
| 79 | * sharing the memory. |
| 80 | * This is used to reset the original configuration when sender invokes |
| 81 | * FFA_MEM_RECLAIM_32. |
| 82 | */ |
| 83 | uint32_t sender_orig_mode; |
| 84 | |
| 85 | /** |
| 86 | * True if all the fragments of this sharing request have been sent and |
| 87 | * Hafnium has updated the sender page table accordingly. |
| 88 | */ |
| 89 | bool sending_complete; |
| 90 | |
| 91 | /** |
| 92 | * How many fragments of the memory region each recipient has retrieved |
| 93 | * so far. The order of this array matches the order of the endpoint |
| 94 | * memory access descriptors in the memory region descriptor. Any |
| 95 | * entries beyond the receiver_count will always be 0. |
| 96 | */ |
| 97 | uint32_t retrieved_fragment_count[MAX_MEM_SHARE_RECIPIENTS]; |
| 98 | |
| 99 | /** |
| 100 | * Field for the SPMC to keep track of how many fragments of the memory |
| 101 | * region the hypervisor has managed to retrieve, using a |
| 102 | * `hypervisor retrieve request`, as defined by FF-A v1.1 EAC0 |
| 103 | * specification. |
| 104 | */ |
| 105 | uint32_t hypervisor_fragment_count; |
J-Alves | 460d36c | 2023-10-12 17:02:15 +0100 | [diff] [blame] | 106 | |
| 107 | /* |
| 108 | * Record whether memory has been protected through the platform |
| 109 | * specific means. Used for linking map action on memory send to memory |
| 110 | * action on reclaim. I.e. if as a result of memory lend/donate the |
| 111 | * memory has been protected, this will be used to reset memory's state, |
| 112 | * by unprotecting on reclaim when the sender reestablishes its |
| 113 | * ownership and exclusive access. |
| 114 | */ |
| 115 | bool memory_protected; |
J-Alves | 6665225 | 2022-07-06 09:49:51 +0100 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | /** |
J-Alves | 0a83dc2 | 2023-05-05 09:50:37 +0100 | [diff] [blame] | 119 | * Actions that can be taken by function `ffa_region_group_identity_map`. |
| 120 | */ |
| 121 | enum ffa_map_action { |
| 122 | MAP_ACTION_CHECK, |
| 123 | MAP_ACTION_CHECK_PROTECT, |
| 124 | MAP_ACTION_COMMIT, |
| 125 | MAP_ACTION_COMMIT_UNPROTECT, |
| 126 | MAP_ACTION_MAX, |
| 127 | }; |
| 128 | |
| 129 | /** |
J-Alves | 6665225 | 2022-07-06 09:49:51 +0100 | [diff] [blame] | 130 | * Encapsulates the set of share states while the `share_states_lock` is held. |
| 131 | */ |
| 132 | struct share_states_locked { |
| 133 | struct ffa_memory_share_state *share_states; |
| 134 | }; |
| 135 | |
Karl Meakin | 52cdfe7 | 2023-06-30 14:49:10 +0100 | [diff] [blame] | 136 | struct ffa_memory_share_state *allocate_share_state( |
| 137 | struct share_states_locked share_states, uint32_t share_func, |
| 138 | struct ffa_memory_region *memory_region, uint32_t fragment_length, |
| 139 | ffa_memory_handle_t handle); |
J-Alves | 6665225 | 2022-07-06 09:49:51 +0100 | [diff] [blame] | 140 | struct share_states_locked share_states_lock(void); |
| 141 | void share_states_unlock(struct share_states_locked *share_states); |
Karl Meakin | 4a2854a | 2023-06-30 16:26:52 +0100 | [diff] [blame] | 142 | struct ffa_memory_share_state *get_share_state( |
| 143 | struct share_states_locked share_states, ffa_memory_handle_t handle); |
J-Alves | fdd2927 | 2022-07-19 13:16:31 +0100 | [diff] [blame] | 144 | void share_state_free(struct share_states_locked share_states, |
| 145 | struct ffa_memory_share_state *share_state, |
| 146 | struct mpool *page_pool); |
| 147 | uint32_t share_state_next_fragment_offset( |
| 148 | struct share_states_locked share_states, |
| 149 | struct ffa_memory_share_state *share_state); |
| 150 | /** Checks whether the given share state has been fully sent. */ |
| 151 | bool share_state_sending_complete(struct share_states_locked share_states, |
| 152 | struct ffa_memory_share_state *share_state); |
J-Alves | 6665225 | 2022-07-06 09:49:51 +0100 | [diff] [blame] | 153 | void dump_share_states(void); |
| 154 | |
J-Alves | 6665225 | 2022-07-06 09:49:51 +0100 | [diff] [blame] | 155 | struct ffa_value ffa_memory_send_validate( |
| 156 | struct vm_locked from_locked, struct ffa_memory_region *memory_region, |
| 157 | uint32_t memory_share_length, uint32_t fragment_length, |
| 158 | uint32_t share_func); |
| 159 | struct ffa_value ffa_send_check_update( |
| 160 | struct vm_locked from_locked, |
| 161 | struct ffa_memory_region_constituent **fragments, |
| 162 | uint32_t *fragment_constituent_counts, uint32_t fragment_count, |
J-Alves | 460d36c | 2023-10-12 17:02:15 +0100 | [diff] [blame] | 163 | uint32_t composite_total_page_count, uint32_t share_func, |
J-Alves | 8f11cde | 2022-12-21 16:18:22 +0000 | [diff] [blame] | 164 | struct ffa_memory_access *receivers, uint32_t receivers_count, |
J-Alves | 460d36c | 2023-10-12 17:02:15 +0100 | [diff] [blame] | 165 | struct mpool *page_pool, bool clear, uint32_t *orig_from_mode_ret, |
| 166 | bool *memory_protected); |
J-Alves | fdd2927 | 2022-07-19 13:16:31 +0100 | [diff] [blame] | 167 | struct ffa_value ffa_memory_send_complete( |
| 168 | struct vm_locked from_locked, struct share_states_locked share_states, |
| 169 | struct ffa_memory_share_state *share_state, struct mpool *page_pool, |
| 170 | uint32_t *orig_from_mode_ret); |
| 171 | struct ffa_value ffa_memory_send_continue_validate( |
| 172 | struct share_states_locked share_states, ffa_memory_handle_t handle, |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 173 | struct ffa_memory_share_state **share_state_ret, ffa_id_t from_vm_id, |
J-Alves | fdd2927 | 2022-07-19 13:16:31 +0100 | [diff] [blame] | 174 | struct mpool *page_pool); |
J-Alves | fc19b37 | 2022-07-06 12:17:35 +0100 | [diff] [blame] | 175 | struct ffa_value ffa_retrieve_check_transition( |
| 176 | struct vm_locked to, uint32_t share_func, |
| 177 | struct ffa_memory_region_constituent **fragments, |
| 178 | uint32_t *fragment_constituent_counts, uint32_t fragment_count, |
J-Alves | fd20605 | 2023-05-22 16:45:00 +0100 | [diff] [blame^] | 179 | uint32_t memory_to_attributes, uint32_t *to_mode, bool memory_protected, |
| 180 | enum ffa_map_action *map_action); |
| 181 | |
J-Alves | b5084cf | 2022-07-06 14:20:12 +0100 | [diff] [blame] | 182 | struct ffa_value ffa_retrieve_check_update( |
J-Alves | 2648338 | 2023-04-20 12:01:49 +0100 | [diff] [blame] | 183 | struct vm_locked to_locked, |
J-Alves | b5084cf | 2022-07-06 14:20:12 +0100 | [diff] [blame] | 184 | struct ffa_memory_region_constituent **fragments, |
| 185 | uint32_t *fragment_constituent_counts, uint32_t fragment_count, |
J-Alves | 2648338 | 2023-04-20 12:01:49 +0100 | [diff] [blame] | 186 | uint32_t sender_orig_mode, uint32_t share_func, bool clear, |
J-Alves | 460d36c | 2023-10-12 17:02:15 +0100 | [diff] [blame] | 187 | struct mpool *page_pool, uint32_t *response_mode, |
| 188 | bool memory_protected); |
J-Alves | cf6253e | 2024-01-03 13:48:48 +0000 | [diff] [blame] | 189 | struct ffa_value ffa_region_group_identity_map( |
J-Alves | fdd2927 | 2022-07-19 13:16:31 +0100 | [diff] [blame] | 190 | struct vm_locked vm_locked, |
| 191 | struct ffa_memory_region_constituent **fragments, |
| 192 | const uint32_t *fragment_constituent_counts, uint32_t fragment_count, |
J-Alves | cf6253e | 2024-01-03 13:48:48 +0000 | [diff] [blame] | 193 | uint32_t mode, struct mpool *ppool, enum ffa_map_action action, |
| 194 | bool *memory_protected); |
J-Alves | fdd2927 | 2022-07-19 13:16:31 +0100 | [diff] [blame] | 195 | bool memory_region_receivers_from_other_world( |
| 196 | struct ffa_memory_region *memory_region); |