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