Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Hafnium Authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 17 | #include "hf/spci_memory.h" |
| 18 | |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 19 | #include "hf/api.h" |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 20 | #include "hf/check.h" |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 21 | #include "hf/dlog.h" |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 22 | #include "hf/mpool.h" |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 23 | #include "hf/spci_internal.h" |
| 24 | #include "hf/std.h" |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 25 | #include "hf/vm.h" |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 26 | |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 27 | struct spci_mem_transitions { |
| 28 | uint32_t orig_from_mode; |
| 29 | uint32_t orig_to_mode; |
| 30 | uint32_t from_mode; |
| 31 | uint32_t to_mode; |
| 32 | }; |
| 33 | |
| 34 | /* TODO: Add device attributes: GRE, cacheability, shareability. */ |
| 35 | static inline uint32_t spci_memory_attrs_to_mode(uint16_t memory_attributes) |
| 36 | { |
| 37 | uint32_t mode = 0; |
| 38 | |
| 39 | switch (spci_get_memory_access_attr(memory_attributes)) { |
| 40 | case SPCI_MEMORY_RO_NX: |
| 41 | mode = MM_MODE_R; |
| 42 | break; |
| 43 | case SPCI_MEMORY_RO_X: |
| 44 | mode = MM_MODE_R | MM_MODE_X; |
| 45 | break; |
| 46 | case SPCI_MEMORY_RW_NX: |
| 47 | mode = MM_MODE_R | MM_MODE_W; |
| 48 | break; |
| 49 | case SPCI_MEMORY_RW_X: |
| 50 | mode = MM_MODE_R | MM_MODE_W | MM_MODE_X; |
| 51 | break; |
| 52 | } |
| 53 | |
| 54 | return mode; |
| 55 | } |
| 56 | |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 57 | /** |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 58 | * Obtain the next mode to apply to the two VMs. |
| 59 | * |
Jose Marinho | 7fbbf2e | 2019-08-05 13:19:58 +0100 | [diff] [blame] | 60 | * Returns true iff a state transition was found. |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 61 | */ |
| 62 | static bool spci_msg_get_next_state( |
| 63 | const struct spci_mem_transitions *transitions, |
| 64 | uint32_t transition_count, uint32_t memory_to_attributes, |
Andrew Walbran | 1281ed4 | 2019-10-22 17:23:40 +0100 | [diff] [blame] | 65 | uint32_t orig_from_mode, uint32_t orig_to_mode, uint32_t *from_mode, |
| 66 | uint32_t *to_mode) |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 67 | { |
| 68 | const uint32_t state_mask = |
| 69 | MM_MODE_INVALID | MM_MODE_UNOWNED | MM_MODE_SHARED; |
| 70 | const uint32_t orig_from_state = orig_from_mode & state_mask; |
| 71 | |
| 72 | for (uint32_t index = 0; index < transition_count; index++) { |
| 73 | uint32_t table_orig_from_mode = |
| 74 | transitions[index].orig_from_mode; |
| 75 | uint32_t table_orig_to_mode = transitions[index].orig_to_mode; |
| 76 | |
| 77 | if (((orig_from_state) == table_orig_from_mode) && |
| 78 | ((orig_to_mode & state_mask) == table_orig_to_mode)) { |
| 79 | *to_mode = transitions[index].to_mode | |
| 80 | memory_to_attributes; |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 81 | |
| 82 | *from_mode = transitions[index].from_mode | |
| 83 | (~state_mask & orig_from_mode); |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 84 | |
| 85 | return true; |
| 86 | } |
| 87 | } |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Verify that all pages have the same mode, that the starting mode |
| 93 | * constitutes a valid state and obtain the next mode to apply |
| 94 | * to the two VMs. |
| 95 | * |
| 96 | * Returns: |
| 97 | * The error code false indicates that: |
| 98 | * 1) a state transition was not found; |
| 99 | * 2) the pages being shared do not have the same mode within the <to> |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 100 | * or <from> VMs; |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 101 | * 3) The beginning and end IPAs are not page aligned; |
| 102 | * 4) The requested share type was not handled. |
| 103 | * Success is indicated by true. |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 104 | */ |
Andrew Walbran | f4b51af | 2020-02-03 14:44:54 +0000 | [diff] [blame] | 105 | static bool spci_msg_check_transition( |
| 106 | struct vm *to, struct vm *from, uint32_t share_func, |
| 107 | uint32_t *orig_from_mode, |
| 108 | struct spci_memory_region_constituent *constituents, |
| 109 | uint32_t constituent_count, uint32_t memory_to_attributes, |
| 110 | uint32_t *from_mode, uint32_t *to_mode) |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 111 | { |
Andrew Walbran | 1281ed4 | 2019-10-22 17:23:40 +0100 | [diff] [blame] | 112 | uint32_t orig_to_mode; |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 113 | const struct spci_mem_transitions *mem_transition_table; |
| 114 | uint32_t transition_table_size; |
Jose Marinho | 7fbbf2e | 2019-08-05 13:19:58 +0100 | [diff] [blame] | 115 | uint32_t i; |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 116 | |
| 117 | /* |
| 118 | * TODO: Transition table does not currently consider the multiple |
| 119 | * shared case. |
| 120 | */ |
| 121 | static const struct spci_mem_transitions donate_transitions[] = { |
| 122 | { |
| 123 | /* 1) {O-EA, !O-NA} -> {!O-NA, O-EA} */ |
| 124 | .orig_from_mode = 0, |
| 125 | .orig_to_mode = MM_MODE_INVALID | MM_MODE_UNOWNED, |
| 126 | .from_mode = MM_MODE_INVALID | MM_MODE_UNOWNED, |
| 127 | .to_mode = 0, |
| 128 | }, |
| 129 | { |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 130 | /* |
| 131 | * Duplicate of 1) in order to cater for an alternative |
| 132 | * representation of !O-NA: |
| 133 | * (INVALID | UNOWNED | SHARED) and (INVALID | UNOWNED) |
| 134 | * are both alternate representations of !O-NA. |
| 135 | */ |
| 136 | /* 4) {O-EA, !O-NA} -> {!O-NA, O-EA} */ |
| 137 | .orig_from_mode = 0, |
| 138 | .orig_to_mode = MM_MODE_INVALID | MM_MODE_UNOWNED | |
| 139 | MM_MODE_SHARED, |
| 140 | .from_mode = MM_MODE_INVALID | MM_MODE_UNOWNED | |
| 141 | MM_MODE_SHARED, |
| 142 | .to_mode = 0, |
| 143 | }, |
| 144 | }; |
Jose Marinho | 56c2573 | 2019-05-20 09:48:53 +0100 | [diff] [blame] | 145 | |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 146 | static const uint32_t size_donate_transitions = |
| 147 | ARRAY_SIZE(donate_transitions); |
| 148 | |
Andrew Walbran | 648fc3e | 2019-10-22 16:23:05 +0100 | [diff] [blame] | 149 | /* |
| 150 | * This data structure holds the allowed state transitions for the |
| 151 | * "lend" state machine. In this state machine the owner keeps ownership |
| 152 | * but loses access to the lent pages. |
| 153 | */ |
| 154 | static const struct spci_mem_transitions lend_transitions[] = { |
Jose Marinho | 56c2573 | 2019-05-20 09:48:53 +0100 | [diff] [blame] | 155 | { |
Andrew Walbran | 648fc3e | 2019-10-22 16:23:05 +0100 | [diff] [blame] | 156 | /* 1) {O-EA, !O-NA} -> {O-NA, !O-EA} */ |
| 157 | .orig_from_mode = 0, |
| 158 | .orig_to_mode = MM_MODE_INVALID | MM_MODE_UNOWNED | |
| 159 | MM_MODE_SHARED, |
| 160 | .from_mode = MM_MODE_INVALID, |
| 161 | .to_mode = MM_MODE_UNOWNED, |
Jose Marinho | 56c2573 | 2019-05-20 09:48:53 +0100 | [diff] [blame] | 162 | }, |
| 163 | { |
Andrew Walbran | 648fc3e | 2019-10-22 16:23:05 +0100 | [diff] [blame] | 164 | /* |
| 165 | * Duplicate of 1) in order to cater for an alternative |
| 166 | * representation of !O-NA: |
| 167 | * (INVALID | UNOWNED | SHARED) and (INVALID | UNOWNED) |
| 168 | * are both alternate representations of !O-NA. |
| 169 | */ |
| 170 | /* 2) {O-EA, !O-NA} -> {O-NA, !O-EA} */ |
| 171 | .orig_from_mode = 0, |
| 172 | .orig_to_mode = MM_MODE_INVALID | MM_MODE_UNOWNED, |
| 173 | .from_mode = MM_MODE_INVALID, |
| 174 | .to_mode = MM_MODE_UNOWNED, |
Jose Marinho | 56c2573 | 2019-05-20 09:48:53 +0100 | [diff] [blame] | 175 | }, |
| 176 | }; |
| 177 | |
Andrew Walbran | 648fc3e | 2019-10-22 16:23:05 +0100 | [diff] [blame] | 178 | static const uint32_t size_lend_transitions = |
| 179 | ARRAY_SIZE(lend_transitions); |
Jose Marinho | 56c2573 | 2019-05-20 09:48:53 +0100 | [diff] [blame] | 180 | |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 181 | /* |
Andrew Walbran | 648fc3e | 2019-10-22 16:23:05 +0100 | [diff] [blame] | 182 | * This data structure holds the allowed state transitions for the |
| 183 | * "share" state machine. In this state machine the owner keeps the |
| 184 | * shared pages mapped on its stage2 table and keeps access as well. |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 185 | */ |
Andrew Walbran | 648fc3e | 2019-10-22 16:23:05 +0100 | [diff] [blame] | 186 | static const struct spci_mem_transitions share_transitions[] = { |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 187 | { |
| 188 | /* 1) {O-EA, !O-NA} -> {O-SA, !O-SA} */ |
| 189 | .orig_from_mode = 0, |
| 190 | .orig_to_mode = MM_MODE_INVALID | MM_MODE_UNOWNED | |
| 191 | MM_MODE_SHARED, |
| 192 | .from_mode = MM_MODE_SHARED, |
| 193 | .to_mode = MM_MODE_UNOWNED | MM_MODE_SHARED, |
| 194 | }, |
| 195 | { |
| 196 | /* |
| 197 | * Duplicate of 1) in order to cater for an alternative |
| 198 | * representation of !O-NA: |
| 199 | * (INVALID | UNOWNED | SHARED) and (INVALID | UNOWNED) |
| 200 | * are both alternate representations of !O-NA. |
| 201 | */ |
| 202 | /* 2) {O-EA, !O-NA} -> {O-SA, !O-SA} */ |
| 203 | .orig_from_mode = 0, |
| 204 | .orig_to_mode = MM_MODE_INVALID | MM_MODE_UNOWNED, |
| 205 | .from_mode = MM_MODE_SHARED, |
| 206 | .to_mode = MM_MODE_UNOWNED | MM_MODE_SHARED, |
| 207 | }, |
| 208 | }; |
| 209 | |
Andrew Walbran | 648fc3e | 2019-10-22 16:23:05 +0100 | [diff] [blame] | 210 | static const uint32_t size_share_transitions = |
| 211 | ARRAY_SIZE(share_transitions); |
| 212 | |
| 213 | static const struct spci_mem_transitions relinquish_transitions[] = { |
| 214 | { |
| 215 | /* 1) {!O-EA, O-NA} -> {!O-NA, O-EA} */ |
| 216 | .orig_from_mode = MM_MODE_UNOWNED, |
| 217 | .orig_to_mode = MM_MODE_INVALID, |
| 218 | .from_mode = MM_MODE_INVALID | MM_MODE_UNOWNED | |
| 219 | MM_MODE_SHARED, |
| 220 | .to_mode = 0, |
| 221 | }, |
| 222 | { |
| 223 | /* 2) {!O-SA, O-SA} -> {!O-NA, O-EA} */ |
| 224 | .orig_from_mode = MM_MODE_UNOWNED | MM_MODE_SHARED, |
| 225 | .orig_to_mode = MM_MODE_SHARED, |
| 226 | .from_mode = MM_MODE_INVALID | MM_MODE_UNOWNED | |
| 227 | MM_MODE_SHARED, |
| 228 | .to_mode = 0, |
| 229 | }, |
| 230 | }; |
| 231 | |
| 232 | static const uint32_t size_relinquish_transitions = |
| 233 | ARRAY_SIZE(relinquish_transitions); |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 234 | |
Andrew Walbran | f4b51af | 2020-02-03 14:44:54 +0000 | [diff] [blame] | 235 | if (constituent_count == 0) { |
Jose Marinho | 7fbbf2e | 2019-08-05 13:19:58 +0100 | [diff] [blame] | 236 | /* |
| 237 | * Fail if there are no constituents. Otherwise |
| 238 | * spci_msg_get_next_state would get an unitialised |
| 239 | * *orig_from_mode and orig_to_mode. |
| 240 | */ |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 241 | return false; |
| 242 | } |
| 243 | |
Andrew Walbran | f4b51af | 2020-02-03 14:44:54 +0000 | [diff] [blame] | 244 | for (i = 0; i < constituent_count; ++i) { |
Andrew Walbran | d88ee92 | 2020-01-15 18:13:21 +0000 | [diff] [blame] | 245 | ipaddr_t begin = |
| 246 | ipa_init(spci_memory_region_constituent_get_address( |
| 247 | &constituents[i])); |
Jose Marinho | 7fbbf2e | 2019-08-05 13:19:58 +0100 | [diff] [blame] | 248 | size_t size = constituents[i].page_count * PAGE_SIZE; |
| 249 | ipaddr_t end = ipa_add(begin, size); |
| 250 | uint32_t current_from_mode; |
| 251 | uint32_t current_to_mode; |
| 252 | |
| 253 | /* Fail if addresses are not page-aligned. */ |
| 254 | if (!is_aligned(ipa_addr(begin), PAGE_SIZE) || |
| 255 | !is_aligned(ipa_addr(end), PAGE_SIZE)) { |
| 256 | return false; |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * Ensure that this constituent memory range is all mapped with |
| 261 | * the same mode. |
| 262 | */ |
| 263 | if (!mm_vm_get_mode(&from->ptable, begin, end, |
| 264 | ¤t_from_mode) || |
| 265 | !mm_vm_get_mode(&to->ptable, begin, end, |
| 266 | ¤t_to_mode)) { |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | * Ensure that all constituents are mapped with the same mode. |
| 272 | */ |
| 273 | if (i == 0) { |
| 274 | *orig_from_mode = current_from_mode; |
| 275 | orig_to_mode = current_to_mode; |
| 276 | } else if (current_from_mode != *orig_from_mode || |
| 277 | current_to_mode != orig_to_mode) { |
| 278 | return false; |
| 279 | } |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 280 | } |
| 281 | |
Andrew Scull | b5f49e0 | 2019-10-02 13:20:47 +0100 | [diff] [blame] | 282 | /* Ensure the address range is normal memory and not a device. */ |
| 283 | if (*orig_from_mode & MM_MODE_D) { |
| 284 | return false; |
| 285 | } |
| 286 | |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 287 | switch (share_func) { |
| 288 | case SPCI_MEM_DONATE_32: |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 289 | mem_transition_table = donate_transitions; |
| 290 | transition_table_size = size_donate_transitions; |
| 291 | break; |
| 292 | |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 293 | case SPCI_MEM_LEND_32: |
Andrew Walbran | 648fc3e | 2019-10-22 16:23:05 +0100 | [diff] [blame] | 294 | mem_transition_table = lend_transitions; |
| 295 | transition_table_size = size_lend_transitions; |
| 296 | break; |
| 297 | |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 298 | case SPCI_MEM_SHARE_32: |
Andrew Walbran | 648fc3e | 2019-10-22 16:23:05 +0100 | [diff] [blame] | 299 | mem_transition_table = share_transitions; |
| 300 | transition_table_size = size_share_transitions; |
| 301 | break; |
| 302 | |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 303 | case HF_SPCI_MEM_RELINQUISH: |
Jose Marinho | 56c2573 | 2019-05-20 09:48:53 +0100 | [diff] [blame] | 304 | mem_transition_table = relinquish_transitions; |
| 305 | transition_table_size = size_relinquish_transitions; |
| 306 | break; |
| 307 | |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 308 | default: |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | return spci_msg_get_next_state(mem_transition_table, |
| 313 | transition_table_size, |
| 314 | memory_to_attributes, *orig_from_mode, |
| 315 | orig_to_mode, from_mode, to_mode); |
| 316 | } |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 317 | |
| 318 | /** |
| 319 | * Updates a VM's page table such that the given set of physical address ranges |
| 320 | * are mapped in the address space at the corresponding address ranges, in the |
| 321 | * mode provided. |
| 322 | * |
| 323 | * If commit is false, the page tables will be allocated from the mpool but no |
| 324 | * mappings will actually be updated. This function must always be called first |
| 325 | * with commit false to check that it will succeed before calling with commit |
| 326 | * true, to avoid leaving the page table in a half-updated state. To make a |
| 327 | * series of changes atomically you can call them all with commit false before |
| 328 | * calling them all with commit true. |
| 329 | * |
| 330 | * mm_vm_defrag should always be called after a series of page table updates, |
| 331 | * whether they succeed or fail. |
| 332 | * |
| 333 | * Returns true on success, or false if the update failed and no changes were |
| 334 | * made to memory mappings. |
| 335 | */ |
| 336 | static bool spci_region_group_identity_map( |
Andrew Walbran | f4b51af | 2020-02-03 14:44:54 +0000 | [diff] [blame] | 337 | struct vm_locked vm_locked, |
| 338 | struct spci_memory_region_constituent *constituents, |
| 339 | uint32_t constituent_count, int mode, struct mpool *ppool, bool commit) |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 340 | { |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 341 | /* Iterate over the memory region constituents. */ |
Andrew Walbran | f4b51af | 2020-02-03 14:44:54 +0000 | [diff] [blame] | 342 | for (uint32_t index = 0; index < constituent_count; index++) { |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 343 | size_t size = constituents[index].page_count * PAGE_SIZE; |
Andrew Walbran | d88ee92 | 2020-01-15 18:13:21 +0000 | [diff] [blame] | 344 | paddr_t pa_begin = pa_from_ipa( |
| 345 | ipa_init(spci_memory_region_constituent_get_address( |
| 346 | &constituents[index]))); |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 347 | paddr_t pa_end = pa_add(pa_begin, size); |
| 348 | |
| 349 | if (commit) { |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 350 | vm_identity_commit(vm_locked, pa_begin, pa_end, mode, |
| 351 | ppool, NULL); |
| 352 | } else if (!vm_identity_prepare(vm_locked, pa_begin, pa_end, |
| 353 | mode, ppool)) { |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 354 | return false; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | return true; |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Clears a region of physical memory by overwriting it with zeros. The data is |
| 363 | * flushed from the cache so the memory has been cleared across the system. |
| 364 | */ |
| 365 | static bool clear_memory(paddr_t begin, paddr_t end, struct mpool *ppool) |
| 366 | { |
| 367 | /* |
Fuad Tabba | ed294af | 2019-12-20 10:43:01 +0000 | [diff] [blame] | 368 | * 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] | 369 | * global mapping of the whole range. Such an approach will limit |
| 370 | * the changes to stage-1 tables and will allow only local |
| 371 | * invalidation. |
| 372 | */ |
| 373 | bool ret; |
| 374 | struct mm_stage1_locked stage1_locked = mm_lock_stage1(); |
| 375 | void *ptr = |
| 376 | mm_identity_map(stage1_locked, begin, end, MM_MODE_W, ppool); |
| 377 | size_t size = pa_difference(begin, end); |
| 378 | |
| 379 | if (!ptr) { |
| 380 | /* TODO: partial defrag of failed range. */ |
| 381 | /* Recover any memory consumed in failed mapping. */ |
| 382 | mm_defrag(stage1_locked, ppool); |
| 383 | goto fail; |
| 384 | } |
| 385 | |
| 386 | memset_s(ptr, size, 0, size); |
| 387 | arch_mm_flush_dcache(ptr, size); |
| 388 | mm_unmap(stage1_locked, begin, end, ppool); |
| 389 | |
| 390 | ret = true; |
| 391 | goto out; |
| 392 | |
| 393 | fail: |
| 394 | ret = false; |
| 395 | |
| 396 | out: |
| 397 | mm_unlock_stage1(&stage1_locked); |
| 398 | |
| 399 | return ret; |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Clears a region of physical memory by overwriting it with zeros. The data is |
| 404 | * flushed from the cache so the memory has been cleared across the system. |
| 405 | */ |
Andrew Walbran | f4b51af | 2020-02-03 14:44:54 +0000 | [diff] [blame] | 406 | static bool spci_clear_memory_constituents( |
| 407 | struct spci_memory_region_constituent *constituents, |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 408 | uint32_t constituent_count, struct mpool *page_pool) |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 409 | { |
| 410 | struct mpool local_page_pool; |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 411 | struct mm_stage1_locked stage1_locked; |
| 412 | bool ret = false; |
| 413 | |
| 414 | /* |
| 415 | * Create a local pool so any freed memory can't be used by another |
| 416 | * thread. This is to ensure each constituent that is mapped can be |
| 417 | * unmapped again afterwards. |
| 418 | */ |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 419 | mpool_init_with_fallback(&local_page_pool, page_pool); |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 420 | |
| 421 | /* Iterate over the memory region constituents. */ |
Andrew Walbran | f4b51af | 2020-02-03 14:44:54 +0000 | [diff] [blame] | 422 | for (uint32_t i = 0; i < constituent_count; ++i) { |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 423 | size_t size = constituents[i].page_count * PAGE_SIZE; |
Andrew Walbran | d88ee92 | 2020-01-15 18:13:21 +0000 | [diff] [blame] | 424 | paddr_t begin = pa_from_ipa( |
| 425 | ipa_init(spci_memory_region_constituent_get_address( |
| 426 | &constituents[i]))); |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 427 | paddr_t end = pa_add(begin, size); |
| 428 | |
| 429 | if (!clear_memory(begin, end, &local_page_pool)) { |
| 430 | /* |
| 431 | * api_clear_memory will defrag on failure, so no need |
| 432 | * to do it here. |
| 433 | */ |
| 434 | goto out; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | * Need to defrag after clearing, as it may have added extra mappings to |
| 440 | * the stage 1 page table. |
| 441 | */ |
| 442 | stage1_locked = mm_lock_stage1(); |
| 443 | mm_defrag(stage1_locked, &local_page_pool); |
| 444 | mm_unlock_stage1(&stage1_locked); |
| 445 | |
| 446 | ret = true; |
| 447 | |
| 448 | out: |
| 449 | mpool_fini(&local_page_pool); |
| 450 | return ret; |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Shares memory from the calling VM with another. The memory can be shared in |
| 455 | * different modes. |
| 456 | * |
| 457 | * This function requires the calling context to hold the <to> and <from> locks. |
| 458 | * |
| 459 | * Returns: |
| 460 | * In case of error one of the following values is returned: |
| 461 | * 1) SPCI_INVALID_PARAMETERS - The endpoint provided parameters were |
| 462 | * erroneous; |
| 463 | * 2) SPCI_NO_MEMORY - Hafnium did not have sufficient memory to complete |
| 464 | * the request. |
| 465 | * Success is indicated by SPCI_SUCCESS. |
| 466 | */ |
| 467 | static struct spci_value spci_share_memory( |
| 468 | struct vm_locked to_locked, struct vm_locked from_locked, |
Andrew Walbran | f4b51af | 2020-02-03 14:44:54 +0000 | [diff] [blame] | 469 | struct spci_memory_region_constituent *constituents, |
| 470 | uint32_t constituent_count, uint32_t memory_to_attributes, |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 471 | uint32_t share_func, struct mpool *page_pool, bool clear) |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 472 | { |
| 473 | struct vm *to = to_locked.vm; |
| 474 | struct vm *from = from_locked.vm; |
| 475 | uint32_t orig_from_mode; |
| 476 | uint32_t from_mode; |
| 477 | uint32_t to_mode; |
| 478 | struct mpool local_page_pool; |
| 479 | struct spci_value ret; |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 480 | |
| 481 | /* |
Andrew Walbran | d88ee92 | 2020-01-15 18:13:21 +0000 | [diff] [blame] | 482 | * Make sure constituents are properly aligned to a 32-bit boundary. If |
| 483 | * not we would get alignment faults trying to read (32-bit) values. |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 484 | */ |
Andrew Walbran | d88ee92 | 2020-01-15 18:13:21 +0000 | [diff] [blame] | 485 | if (!is_aligned(constituents, 4)) { |
Andrew Walbran | 280f95e | 2020-02-19 16:29:34 +0000 | [diff] [blame] | 486 | dlog_verbose("Constituents not aligned.\n"); |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 487 | return spci_error(SPCI_INVALID_PARAMETERS); |
| 488 | } |
| 489 | |
| 490 | /* Disallow reflexive shares as this suggests an error in the VM. */ |
| 491 | if (to == from) { |
Andrew Walbran | 280f95e | 2020-02-19 16:29:34 +0000 | [diff] [blame] | 492 | dlog_verbose("Reflexive share.\n"); |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 493 | return spci_error(SPCI_INVALID_PARAMETERS); |
| 494 | } |
| 495 | |
| 496 | /* |
| 497 | * Check if the state transition is lawful for both VMs involved |
| 498 | * in the memory exchange, ensure that all constituents of a memory |
| 499 | * region being shared are at the same state. |
| 500 | */ |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 501 | if (!spci_msg_check_transition(to, from, share_func, &orig_from_mode, |
Andrew Walbran | f4b51af | 2020-02-03 14:44:54 +0000 | [diff] [blame] | 502 | constituents, constituent_count, |
| 503 | memory_to_attributes, &from_mode, |
| 504 | &to_mode)) { |
Andrew Walbran | 280f95e | 2020-02-19 16:29:34 +0000 | [diff] [blame] | 505 | dlog_verbose("Invalid transition.\n"); |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 506 | return spci_error(SPCI_INVALID_PARAMETERS); |
| 507 | } |
| 508 | |
| 509 | /* |
| 510 | * Create a local pool so any freed memory can't be used by another |
| 511 | * thread. This is to ensure the original mapping can be restored if the |
| 512 | * clear fails. |
| 513 | */ |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 514 | mpool_init_with_fallback(&local_page_pool, page_pool); |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 515 | |
| 516 | /* |
| 517 | * First reserve all required memory for the new page table entries in |
| 518 | * both sender and recipient page tables without committing, to make |
| 519 | * sure the entire operation will succeed without exhausting the page |
| 520 | * pool. |
| 521 | */ |
Andrew Walbran | f4b51af | 2020-02-03 14:44:54 +0000 | [diff] [blame] | 522 | if (!spci_region_group_identity_map(from_locked, constituents, |
| 523 | constituent_count, from_mode, |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 524 | page_pool, false) || |
Andrew Walbran | f4b51af | 2020-02-03 14:44:54 +0000 | [diff] [blame] | 525 | !spci_region_group_identity_map(to_locked, constituents, |
| 526 | constituent_count, to_mode, |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 527 | page_pool, false)) { |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 528 | /* TODO: partial defrag of failed range. */ |
| 529 | ret = spci_error(SPCI_NO_MEMORY); |
| 530 | goto out; |
| 531 | } |
| 532 | |
| 533 | /* |
| 534 | * First update the mapping for the sender so there is no overlap with |
| 535 | * the recipient. This won't allocate because the transaction was |
| 536 | * already prepared above, but may free pages in the case that a whole |
| 537 | * block is being unmapped that was previously partially mapped. |
| 538 | */ |
Andrew Walbran | f4b51af | 2020-02-03 14:44:54 +0000 | [diff] [blame] | 539 | CHECK(spci_region_group_identity_map(from_locked, constituents, |
| 540 | constituent_count, from_mode, |
| 541 | &local_page_pool, true)); |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 542 | |
| 543 | /* Clear the memory so no VM or device can see the previous contents. */ |
Andrew Walbran | f4b51af | 2020-02-03 14:44:54 +0000 | [diff] [blame] | 544 | if (clear && !spci_clear_memory_constituents( |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 545 | constituents, constituent_count, page_pool)) { |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 546 | /* |
| 547 | * On failure, roll back by returning memory to the sender. This |
| 548 | * may allocate pages which were previously freed into |
| 549 | * `local_page_pool` by the call above, but will never allocate |
| 550 | * more pages than that so can never fail. |
| 551 | */ |
Andrew Walbran | f4b51af | 2020-02-03 14:44:54 +0000 | [diff] [blame] | 552 | CHECK(spci_region_group_identity_map( |
| 553 | from_locked, constituents, constituent_count, |
| 554 | orig_from_mode, &local_page_pool, true)); |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 555 | |
| 556 | ret = spci_error(SPCI_NO_MEMORY); |
| 557 | goto out; |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * Complete the transfer by mapping the memory into the recipient. This |
| 562 | * won't allocate because the transaction was already prepared above, so |
| 563 | * it doesn't need to use the `local_page_pool`. |
| 564 | */ |
Andrew Walbran | f4b51af | 2020-02-03 14:44:54 +0000 | [diff] [blame] | 565 | CHECK(spci_region_group_identity_map(to_locked, constituents, |
| 566 | constituent_count, to_mode, |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 567 | page_pool, true)); |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 568 | |
| 569 | ret = (struct spci_value){.func = SPCI_SUCCESS_32}; |
| 570 | |
| 571 | out: |
| 572 | mpool_fini(&local_page_pool); |
| 573 | |
| 574 | /* |
| 575 | * Tidy up the page tables by reclaiming failed mappings (if there was |
| 576 | * an error) or merging entries into blocks where possible (on success). |
| 577 | */ |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 578 | mm_vm_defrag(&to->ptable, page_pool); |
| 579 | mm_vm_defrag(&from->ptable, page_pool); |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 580 | |
| 581 | return ret; |
| 582 | } |
| 583 | |
| 584 | /** |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 585 | * Validates a call to donate, lend or share memory and then updates the stage-2 |
| 586 | * page tables. Specifically, check if the message length and number of memory |
| 587 | * region constituents match, and if the transition is valid for the type of |
| 588 | * memory sending operation. |
| 589 | * |
| 590 | * Assumes that the caller has already found and locked both VMs and ensured |
| 591 | * that the destination RX buffer is available, and copied the memory region |
| 592 | * descriptor from the sender's TX buffer to a trusted internal buffer. |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 593 | */ |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 594 | struct spci_value spci_memory_send(struct vm_locked to_locked, |
| 595 | struct vm_locked from_locked, |
| 596 | struct spci_memory_region *memory_region, |
| 597 | uint32_t memory_share_size, |
| 598 | uint32_t share_func, struct mpool *page_pool) |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 599 | { |
| 600 | uint32_t memory_to_attributes; |
| 601 | uint32_t attributes_size; |
| 602 | uint32_t constituents_size; |
Andrew Walbran | f4b51af | 2020-02-03 14:44:54 +0000 | [diff] [blame] | 603 | struct spci_memory_region_constituent *constituents; |
| 604 | uint32_t constituent_count = memory_region->constituent_count; |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 605 | |
| 606 | /* |
| 607 | * Ensure the number of constituents are within the memory |
| 608 | * bounds. |
| 609 | */ |
| 610 | attributes_size = sizeof(struct spci_memory_region_attributes) * |
| 611 | memory_region->attribute_count; |
| 612 | constituents_size = sizeof(struct spci_memory_region_constituent) * |
Andrew Walbran | f4b51af | 2020-02-03 14:44:54 +0000 | [diff] [blame] | 613 | constituent_count; |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 614 | if (memory_region->constituent_offset < |
| 615 | sizeof(struct spci_memory_region) + attributes_size || |
| 616 | memory_share_size != |
| 617 | memory_region->constituent_offset + constituents_size) { |
| 618 | return spci_error(SPCI_INVALID_PARAMETERS); |
| 619 | } |
| 620 | |
Andrew Walbran | e28f4a2 | 2019-12-24 15:45:36 +0000 | [diff] [blame] | 621 | /* The sender must match the message sender. */ |
| 622 | if (memory_region->sender != from_locked.vm->id) { |
| 623 | return spci_error(SPCI_INVALID_PARAMETERS); |
| 624 | } |
| 625 | |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 626 | /* We only support a single recipient. */ |
| 627 | if (memory_region->attribute_count != 1) { |
Andrew Walbran | e908c4a | 2019-12-02 17:13:47 +0000 | [diff] [blame] | 628 | return spci_error(SPCI_NOT_SUPPORTED); |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 629 | } |
| 630 | |
Andrew Walbran | cfe6164 | 2019-12-02 15:34:06 +0000 | [diff] [blame] | 631 | /* The recipient must match the message recipient. */ |
| 632 | if (memory_region->attributes[0].receiver != to_locked.vm->id) { |
| 633 | return spci_error(SPCI_INVALID_PARAMETERS); |
| 634 | } |
| 635 | |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 636 | switch (share_func) { |
| 637 | case SPCI_MEM_DONATE_32: |
| 638 | case SPCI_MEM_LEND_32: |
| 639 | case SPCI_MEM_SHARE_32: |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 640 | memory_to_attributes = spci_memory_attrs_to_mode( |
| 641 | memory_region->attributes[0].memory_attributes); |
| 642 | break; |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 643 | case HF_SPCI_MEM_RELINQUISH: |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 644 | memory_to_attributes = MM_MODE_R | MM_MODE_W | MM_MODE_X; |
| 645 | break; |
| 646 | default: |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 647 | dlog_error("Invalid memory sharing message.\n"); |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 648 | return spci_error(SPCI_INVALID_PARAMETERS); |
| 649 | } |
| 650 | |
Andrew Walbran | f4b51af | 2020-02-03 14:44:54 +0000 | [diff] [blame] | 651 | constituents = spci_memory_region_get_constituents(memory_region); |
| 652 | return spci_share_memory( |
| 653 | to_locked, from_locked, constituents, constituent_count, |
Andrew Walbran | 475c145 | 2020-02-07 13:22:22 +0000 | [diff] [blame] | 654 | memory_to_attributes, share_func, page_pool, |
Andrew Walbran | f4b51af | 2020-02-03 14:44:54 +0000 | [diff] [blame] | 655 | memory_region->flags & SPCI_MEMORY_REGION_FLAG_CLEAR); |
Jose Marinho | 09b1db8 | 2019-08-08 09:16:59 +0100 | [diff] [blame] | 656 | } |