Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google LLC |
| 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 Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 17 | #include "hf/mm.h" |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 18 | |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 19 | #include <assert.h> |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 20 | #include <stdatomic.h> |
| 21 | #include <stdint.h> |
| 22 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 23 | #include "hf/alloc.h" |
| 24 | #include "hf/dlog.h" |
Andrew Scull | 5991ec9 | 2018-10-08 14:55:02 +0100 | [diff] [blame] | 25 | #include "hf/layout.h" |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 26 | |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 27 | /** |
| 28 | * This file has functions for managing the level 1 and 2 page tables used by |
| 29 | * Hafnium. There is a level 1 mapping used by Hafnium itself to access memory, |
| 30 | * and then a level 2 mapping per VM. The design assumes that all page tables |
| 31 | * contain only 1-1 mappings, aligned on the block boundaries. |
| 32 | */ |
| 33 | |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 34 | /* The type of addresses stored in the page table. */ |
| 35 | typedef uintvaddr_t ptable_addr_t; |
| 36 | |
Wedson Almeida Filho | b2c159e | 2018-10-25 13:27:47 +0100 | [diff] [blame] | 37 | /* |
| 38 | * For stage 2, the input is an intermediate physical addresses rather than a |
| 39 | * virtual address so: |
| 40 | */ |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 41 | static_assert( |
| 42 | sizeof(ptable_addr_t) == sizeof(uintpaddr_t), |
| 43 | "Currently, the same code manages the stage 1 and stage 2 page tables " |
| 44 | "which only works if the virtual and intermediate physical addresses " |
| 45 | "are the same size. It looks like that assumption might not be holding " |
| 46 | "so we need to check that everything is going to be ok."); |
| 47 | |
Andrew Scull | 4f170f5 | 2018-07-19 12:58:20 +0100 | [diff] [blame] | 48 | /* Keep macro alignment */ |
| 49 | /* clang-format off */ |
| 50 | |
Andrew Scull | f2f948e | 2018-10-22 18:39:28 +0100 | [diff] [blame] | 51 | #define MAP_FLAG_NOSYNC 0x01 |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 52 | #define MAP_FLAG_COMMIT 0x02 |
Andrew Walbran | 6324fc9 | 2018-10-03 11:46:43 +0100 | [diff] [blame] | 53 | #define MAP_FLAG_UNMAP 0x04 |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 54 | #define MAP_FLAG_NOBBM 0x08 |
| 55 | #define MAP_FLAG_STAGE1 0x10 |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 56 | |
Andrew Scull | 4f170f5 | 2018-07-19 12:58:20 +0100 | [diff] [blame] | 57 | /* clang-format on */ |
| 58 | |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 59 | static struct mm_ptable ptable; |
| 60 | |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 61 | /** |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 62 | * Get the page table from the physical address. |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 63 | */ |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 64 | static struct mm_page_table *mm_page_table_from_pa(paddr_t pa) |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 65 | { |
| 66 | return ptr_from_va(va_from_pa(pa)); |
| 67 | } |
| 68 | |
| 69 | /** |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 70 | * Rounds an address down to a page boundary. |
| 71 | */ |
| 72 | static ptable_addr_t mm_round_down_to_page(ptable_addr_t addr) |
| 73 | { |
| 74 | return addr & ~((ptable_addr_t)(PAGE_SIZE - 1)); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Rounds an address up to a page boundary. |
| 79 | */ |
| 80 | static ptable_addr_t mm_round_up_to_page(ptable_addr_t addr) |
| 81 | { |
| 82 | return mm_round_down_to_page(addr + PAGE_SIZE - 1); |
| 83 | } |
| 84 | |
| 85 | /** |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 86 | * Calculates the size of the address space represented by a page table entry at |
| 87 | * the given level. |
| 88 | */ |
Andrew Scull | e982771 | 2018-10-19 14:54:20 +0100 | [diff] [blame] | 89 | static size_t mm_entry_size(uint8_t level) |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 90 | { |
Andrew Scull | 78d6fd9 | 2018-09-06 15:08:36 +0100 | [diff] [blame] | 91 | return UINT64_C(1) << (PAGE_BITS + level * PAGE_LEVEL_BITS); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
Andrew Scull | cae4557 | 2018-12-13 15:46:30 +0000 | [diff] [blame^] | 95 | * Gets the address of the start of the next block of the given size. The size |
| 96 | * must be a power of two. |
| 97 | */ |
| 98 | static ptable_addr_t mm_start_of_next_block(ptable_addr_t addr, |
| 99 | size_t block_size) |
| 100 | { |
| 101 | return (addr + block_size) & ~(block_size - 1); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Gets the physical address of the start of the next block of the given size. |
| 106 | * The size must be a power of two. |
| 107 | */ |
| 108 | static paddr_t mm_pa_start_of_next_block(paddr_t pa, size_t block_size) |
| 109 | { |
| 110 | return pa_init((pa_addr(pa) + block_size) & ~(block_size - 1)); |
| 111 | } |
| 112 | |
| 113 | /** |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 114 | * For a given address, calculates the maximum (plus one) address that can be |
| 115 | * represented by the same table at the given level. |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 116 | */ |
Andrew Scull | e982771 | 2018-10-19 14:54:20 +0100 | [diff] [blame] | 117 | static ptable_addr_t mm_level_end(ptable_addr_t addr, uint8_t level) |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 118 | { |
| 119 | size_t offset = PAGE_BITS + (level + 1) * PAGE_LEVEL_BITS; |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 120 | return ((addr >> offset) + 1) << offset; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 124 | * For a given address, calculates the index at which its entry is stored in a |
| 125 | * table at the given level. |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 126 | */ |
Andrew Scull | e982771 | 2018-10-19 14:54:20 +0100 | [diff] [blame] | 127 | static size_t mm_index(ptable_addr_t addr, uint8_t level) |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 128 | { |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 129 | ptable_addr_t v = addr >> (PAGE_BITS + level * PAGE_LEVEL_BITS); |
Andrew Scull | 78d6fd9 | 2018-09-06 15:08:36 +0100 | [diff] [blame] | 130 | return v & ((UINT64_C(1) << PAGE_LEVEL_BITS) - 1); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 134 | * Allocate a new page table. |
| 135 | */ |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 136 | static struct mm_page_table *mm_alloc_page_tables(size_t count, bool nosync) |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 137 | { |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 138 | size_t size_and_align = count * sizeof(struct mm_page_table); |
Andrew Scull | f2f948e | 2018-10-22 18:39:28 +0100 | [diff] [blame] | 139 | if (nosync) { |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 140 | return halloc_aligned_nosync(size_and_align, size_and_align); |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 143 | return halloc_aligned(size_and_align, size_and_align); |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /** |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 147 | * Invalidates the TLB for the given address range. |
| 148 | */ |
| 149 | static void mm_invalidate_tlb(ptable_addr_t begin, ptable_addr_t end, |
| 150 | bool stage1) |
| 151 | { |
| 152 | if (stage1) { |
| 153 | arch_mm_invalidate_stage1_range(va_init(begin), va_init(end)); |
| 154 | } else { |
| 155 | arch_mm_invalidate_stage2_range(ipa_init(begin), ipa_init(end)); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Frees all page-table-related memory associated with the given pte at the |
| 161 | * given level, including any subtables recursively. |
| 162 | */ |
| 163 | static void mm_free_page_pte(pte_t pte, uint8_t level) |
| 164 | { |
| 165 | struct mm_page_table *table; |
| 166 | uint64_t i; |
| 167 | |
| 168 | if (!arch_mm_pte_is_table(pte, level)) { |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | /* Recursively free any subtables. */ |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 173 | table = mm_page_table_from_pa(arch_mm_table_from_pte(pte, level)); |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 174 | for (i = 0; i < MM_PTE_PER_PAGE; ++i) { |
| 175 | mm_free_page_pte(table->entries[i], level - 1); |
| 176 | } |
| 177 | |
| 178 | /* Free the table itself. */ |
| 179 | hfree(table); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Replaces a page table entry with the given value. If both old and new values |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 184 | * are valid, it performs a break-before-make sequence where it first writes an |
| 185 | * invalid value to the PTE, flushes the TLB, then writes the actual new value. |
| 186 | * This is to prevent cases where CPUs have different 'valid' values in their |
| 187 | * TLBs, which may result in issues for example in cache coherency. |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 188 | */ |
| 189 | static void mm_replace_entry(ptable_addr_t begin, pte_t *pte, pte_t new_pte, |
| 190 | uint8_t level, int flags) |
| 191 | { |
| 192 | pte_t v = *pte; |
| 193 | |
| 194 | /* |
| 195 | * We need to do the break-before-make sequence if both values are |
| 196 | * present, and if it hasn't been inhibited by the NOBBM flag. |
| 197 | */ |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 198 | if (!(flags & MAP_FLAG_NOBBM) && arch_mm_pte_is_valid(v, level) && |
| 199 | arch_mm_pte_is_valid(new_pte, level)) { |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 200 | *pte = arch_mm_absent_pte(level); |
| 201 | mm_invalidate_tlb(begin, begin + mm_entry_size(level), |
| 202 | flags & MAP_FLAG_STAGE1); |
| 203 | } |
| 204 | |
| 205 | /* Assign the new pte. */ |
| 206 | *pte = new_pte; |
| 207 | |
| 208 | /* Free pages that aren't in use anymore. */ |
| 209 | mm_free_page_pte(v, level); |
| 210 | } |
| 211 | |
| 212 | /** |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 213 | * Populates the provided page table entry with a reference to another table if |
| 214 | * needed, that is, if it does not yet point to another table. |
| 215 | * |
| 216 | * Returns a pointer to the table the entry now points to. |
| 217 | */ |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 218 | static struct mm_page_table *mm_populate_table_pte(ptable_addr_t begin, |
| 219 | pte_t *pte, uint8_t level, |
| 220 | int flags) |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 221 | { |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 222 | struct mm_page_table *ntable; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 223 | pte_t v = *pte; |
| 224 | pte_t new_pte; |
| 225 | size_t i; |
| 226 | size_t inc; |
Andrew Scull | e982771 | 2018-10-19 14:54:20 +0100 | [diff] [blame] | 227 | uint8_t level_below = level - 1; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 228 | |
| 229 | /* Just return pointer to table if it's already populated. */ |
Andrew Scull | 78d6fd9 | 2018-09-06 15:08:36 +0100 | [diff] [blame] | 230 | if (arch_mm_pte_is_table(v, level)) { |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 231 | return mm_page_table_from_pa(arch_mm_table_from_pte(v, level)); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 232 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 233 | |
| 234 | /* Allocate a new table. */ |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 235 | ntable = mm_alloc_page_tables(1, flags & MAP_FLAG_NOSYNC); |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 236 | if (ntable == NULL) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 237 | dlog("Failed to allocate memory for page table\n"); |
| 238 | return NULL; |
| 239 | } |
| 240 | |
| 241 | /* Determine template for new pte and its increment. */ |
Andrew Scull | 78d6fd9 | 2018-09-06 15:08:36 +0100 | [diff] [blame] | 242 | if (arch_mm_pte_is_block(v, level)) { |
Andrew Scull | 78d6fd9 | 2018-09-06 15:08:36 +0100 | [diff] [blame] | 243 | inc = mm_entry_size(level_below); |
| 244 | new_pte = arch_mm_block_pte(level_below, |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 245 | arch_mm_block_from_pte(v, level), |
| 246 | arch_mm_pte_attrs(v, level)); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 247 | } else { |
Andrew Scull | 78d6fd9 | 2018-09-06 15:08:36 +0100 | [diff] [blame] | 248 | inc = 0; |
Andrew Walbran | 1b99f9d | 2018-10-03 17:54:40 +0100 | [diff] [blame] | 249 | new_pte = arch_mm_absent_pte(level_below); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /* Initialise entries in the new table. */ |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 253 | for (i = 0; i < MM_PTE_PER_PAGE; i++) { |
| 254 | ntable->entries[i] = new_pte; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 255 | new_pte += inc; |
| 256 | } |
| 257 | |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 258 | /* Ensure initialisation is visible before updating the pte. */ |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 259 | atomic_thread_fence(memory_order_release); |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 260 | |
| 261 | /* Replace the pte entry, doing a break-before-make if needed. */ |
| 262 | mm_replace_entry(begin, pte, |
| 263 | arch_mm_table_pte(level, pa_init((uintpaddr_t)ntable)), |
| 264 | level, flags); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 265 | |
| 266 | return ntable; |
| 267 | } |
| 268 | |
| 269 | /** |
Andrew Walbran | 6324fc9 | 2018-10-03 11:46:43 +0100 | [diff] [blame] | 270 | * Returns whether all entries in this table are absent. |
| 271 | */ |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 272 | static bool mm_page_table_is_empty(struct mm_page_table *table, uint8_t level) |
Andrew Walbran | 6324fc9 | 2018-10-03 11:46:43 +0100 | [diff] [blame] | 273 | { |
| 274 | uint64_t i; |
| 275 | |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 276 | for (i = 0; i < MM_PTE_PER_PAGE; ++i) { |
| 277 | if (arch_mm_pte_is_present(table->entries[i], level)) { |
Andrew Walbran | 6324fc9 | 2018-10-03 11:46:43 +0100 | [diff] [blame] | 278 | return false; |
| 279 | } |
| 280 | } |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 281 | |
Andrew Walbran | 6324fc9 | 2018-10-03 11:46:43 +0100 | [diff] [blame] | 282 | return true; |
| 283 | } |
| 284 | |
| 285 | /** |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 286 | * Updates the page table at the given level to map the given address range to a |
Andrew Walbran | 6324fc9 | 2018-10-03 11:46:43 +0100 | [diff] [blame] | 287 | * physical range using the provided (architecture-specific) attributes. Or if |
| 288 | * MAP_FLAG_UNMAP is set, unmap the given range instead. |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 289 | * |
| 290 | * This function calls itself recursively if it needs to update additional |
| 291 | * levels, but the recursion is bound by the maximum number of levels in a page |
| 292 | * table. |
| 293 | */ |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 294 | static bool mm_map_level(ptable_addr_t begin, ptable_addr_t end, paddr_t pa, |
Andrew Scull | e982771 | 2018-10-19 14:54:20 +0100 | [diff] [blame] | 295 | uint64_t attrs, struct mm_page_table *table, |
| 296 | uint8_t level, int flags) |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 297 | { |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 298 | pte_t *pte = &table->entries[mm_index(begin, level)]; |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 299 | ptable_addr_t level_end = mm_level_end(begin, level); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 300 | size_t entry_size = mm_entry_size(level); |
| 301 | bool commit = flags & MAP_FLAG_COMMIT; |
Andrew Walbran | 6324fc9 | 2018-10-03 11:46:43 +0100 | [diff] [blame] | 302 | bool unmap = flags & MAP_FLAG_UNMAP; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 303 | |
Andrew Scull | 265ada9 | 2018-07-30 15:19:01 +0100 | [diff] [blame] | 304 | /* Cap end so that we don't go over the current level max. */ |
| 305 | if (end > level_end) { |
| 306 | end = level_end; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 307 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 308 | |
| 309 | /* Fill each entry in the table. */ |
Andrew Scull | 265ada9 | 2018-07-30 15:19:01 +0100 | [diff] [blame] | 310 | while (begin < end) { |
Andrew Walbran | 6324fc9 | 2018-10-03 11:46:43 +0100 | [diff] [blame] | 311 | if (unmap ? !arch_mm_pte_is_present(*pte, level) |
| 312 | : arch_mm_pte_is_block(*pte, level) && |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 313 | arch_mm_pte_attrs(*pte, level) == attrs) { |
Andrew Walbran | 6324fc9 | 2018-10-03 11:46:43 +0100 | [diff] [blame] | 314 | /* |
| 315 | * If the entry is already mapped with the right |
| 316 | * attributes, or already absent in the case of |
| 317 | * unmapping, no need to do anything; carry on to the |
| 318 | * next entry. |
| 319 | */ |
| 320 | } else if ((end - begin) >= entry_size && |
| 321 | (unmap || arch_mm_is_block_allowed(level)) && |
| 322 | (begin & (entry_size - 1)) == 0) { |
| 323 | /* |
| 324 | * If the entire entry is within the region we want to |
| 325 | * map, map/unmap the whole entry. |
| 326 | */ |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 327 | if (commit) { |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 328 | pte_t new_pte = |
| 329 | unmap ? arch_mm_absent_pte(level) |
| 330 | : arch_mm_block_pte(level, pa, |
| 331 | attrs); |
| 332 | mm_replace_entry(begin, pte, new_pte, level, |
| 333 | flags); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 334 | } |
| 335 | } else { |
Andrew Walbran | 6324fc9 | 2018-10-03 11:46:43 +0100 | [diff] [blame] | 336 | /* |
| 337 | * If the entry is already a subtable get it; otherwise |
| 338 | * replace it with an equivalent subtable and get that. |
| 339 | */ |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 340 | struct mm_page_table *nt = |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 341 | mm_populate_table_pte(begin, pte, level, flags); |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 342 | if (nt == NULL) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 343 | return false; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 344 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 345 | |
Andrew Walbran | 6324fc9 | 2018-10-03 11:46:43 +0100 | [diff] [blame] | 346 | /* |
| 347 | * Recurse to map/unmap the appropriate entries within |
| 348 | * the subtable. |
| 349 | */ |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 350 | if (!mm_map_level(begin, end, pa, attrs, nt, level - 1, |
| 351 | flags)) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 352 | return false; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 353 | } |
Andrew Walbran | 6324fc9 | 2018-10-03 11:46:43 +0100 | [diff] [blame] | 354 | |
| 355 | /* |
| 356 | * If the subtable is now empty, replace it with an |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 357 | * absent entry at this level. We never need to do |
| 358 | * break-before-makes here because we are assigning |
| 359 | * an absent value. |
Andrew Walbran | 6324fc9 | 2018-10-03 11:46:43 +0100 | [diff] [blame] | 360 | */ |
| 361 | if (commit && unmap && |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 362 | mm_page_table_is_empty(nt, level - 1)) { |
Andrew Walbran | 6324fc9 | 2018-10-03 11:46:43 +0100 | [diff] [blame] | 363 | pte_t v = *pte; |
| 364 | *pte = arch_mm_absent_pte(level); |
Andrew Walbran | 6324fc9 | 2018-10-03 11:46:43 +0100 | [diff] [blame] | 365 | mm_free_page_pte(v, level); |
| 366 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 367 | } |
| 368 | |
Andrew Scull | cae4557 | 2018-12-13 15:46:30 +0000 | [diff] [blame^] | 369 | begin = mm_start_of_next_block(begin, entry_size); |
| 370 | pa = mm_pa_start_of_next_block(pa, entry_size); |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 371 | pte++; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | return true; |
| 375 | } |
| 376 | |
| 377 | /** |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 378 | * Updates the page table from the root to map the given address range to a |
| 379 | * physical range using the provided (architecture-specific) attributes. Or if |
| 380 | * MAP_FLAG_UNMAP is set, unmap the given range instead. |
| 381 | */ |
| 382 | static bool mm_map_root(struct mm_ptable *t, ptable_addr_t begin, |
| 383 | ptable_addr_t end, uint64_t attrs, uint8_t root_level, |
| 384 | int flags) |
| 385 | { |
| 386 | size_t root_table_size = mm_entry_size(root_level); |
| 387 | struct mm_page_table *table = |
| 388 | &mm_page_table_from_pa(t->root)[mm_index(begin, root_level)]; |
| 389 | |
| 390 | while (begin < end) { |
| 391 | if (!mm_map_level(begin, end, pa_init(begin), attrs, table, |
| 392 | root_level - 1, flags)) { |
| 393 | return false; |
| 394 | } |
Andrew Scull | cae4557 | 2018-12-13 15:46:30 +0000 | [diff] [blame^] | 395 | begin = mm_start_of_next_block(begin, root_table_size); |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 396 | table++; |
| 397 | } |
| 398 | |
| 399 | return true; |
| 400 | } |
| 401 | |
| 402 | /** |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 403 | * Updates the given table such that the given physical address range is mapped |
Andrew Scull | a6da834 | 2018-11-01 12:29:49 +0000 | [diff] [blame] | 404 | * or not mapped into the address space with the architecture-agnostic mode |
| 405 | * provided. |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 406 | */ |
Andrew Scull | a6da834 | 2018-11-01 12:29:49 +0000 | [diff] [blame] | 407 | static bool mm_ptable_identity_update(struct mm_ptable *t, paddr_t pa_begin, |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 408 | paddr_t pa_end, int mode) |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 409 | { |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 410 | uint64_t attrs = arch_mm_mode_to_attrs(mode); |
Andrew Scull | a6da834 | 2018-11-01 12:29:49 +0000 | [diff] [blame] | 411 | int flags = (mode & MM_MODE_NOSYNC ? MAP_FLAG_NOSYNC : 0) | |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 412 | (mode & MM_MODE_NOINVALIDATE ? MAP_FLAG_NOBBM : 0) | |
| 413 | (mode & MM_MODE_STAGE1 ? MAP_FLAG_STAGE1 : 0) | |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 414 | (mode & MM_MODE_INVALID && mode & MM_MODE_UNOWNED |
| 415 | ? MAP_FLAG_UNMAP |
| 416 | : 0); |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 417 | uint8_t root_level = arch_mm_max_level(mode) + 1; |
| 418 | ptable_addr_t ptable_end = |
| 419 | arch_mm_root_table_count(mode) * mm_entry_size(root_level); |
| 420 | ptable_addr_t end = mm_round_up_to_page(pa_addr(pa_end)); |
| 421 | ptable_addr_t begin = pa_addr(arch_mm_clear_pa(pa_begin)); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 422 | |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 423 | /* |
| 424 | * TODO: replace with assertions that the max level will be greater than |
| 425 | * 0 and less than 255 so wrapping will not be a problem and will not |
| 426 | * lead to subsequent overflows. |
| 427 | */ |
| 428 | if (root_level == 0 || root_level == 1) { |
| 429 | return false; |
| 430 | } |
| 431 | |
| 432 | /* Cap end to stay within the bounds of the page table. */ |
| 433 | if (end > ptable_end) { |
| 434 | end = ptable_end; |
| 435 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 436 | |
| 437 | /* |
| 438 | * Do it in two steps to prevent leaving the table in a halfway updated |
| 439 | * state. In such a two-step implementation, the table may be left with |
| 440 | * extra internal tables, but no different mapping on failure. |
| 441 | */ |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 442 | if (!mm_map_root(t, begin, end, attrs, root_level, flags) || |
| 443 | !mm_map_root(t, begin, end, attrs, root_level, |
| 444 | flags | MAP_FLAG_COMMIT)) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 445 | return false; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 446 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 447 | |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 448 | /* Invalidate the tlb. */ |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 449 | if (!(mode & MM_MODE_NOINVALIDATE)) { |
| 450 | mm_invalidate_tlb(begin, end, (mode & MM_MODE_STAGE1) != 0); |
| 451 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 452 | |
| 453 | return true; |
| 454 | } |
| 455 | |
| 456 | /** |
Andrew Scull | a6da834 | 2018-11-01 12:29:49 +0000 | [diff] [blame] | 457 | * Updates the given table such that the given physical address range is mapped |
| 458 | * into the address space with the architecture-agnostic mode provided. |
| 459 | */ |
| 460 | static bool mm_ptable_identity_map(struct mm_ptable *t, paddr_t pa_begin, |
| 461 | paddr_t pa_end, int mode) |
| 462 | { |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 463 | return mm_ptable_identity_update(t, pa_begin, pa_end, mode); |
Andrew Scull | a6da834 | 2018-11-01 12:29:49 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | /** |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 467 | * Updates the given table such that the given physical address range is not |
| 468 | * mapped into the address space. |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 469 | */ |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 470 | static bool mm_ptable_unmap(struct mm_ptable *t, paddr_t pa_begin, |
| 471 | paddr_t pa_end, int mode) |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 472 | { |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 473 | return mm_ptable_identity_update( |
| 474 | t, pa_begin, pa_end, mode | MM_MODE_UNOWNED | MM_MODE_INVALID); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | /** |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 478 | * Writes the given table to the debug log, calling itself recursively to |
| 479 | * write sub-tables. |
| 480 | */ |
Andrew Scull | e982771 | 2018-10-19 14:54:20 +0100 | [diff] [blame] | 481 | static void mm_dump_table_recursive(struct mm_page_table *table, uint8_t level, |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 482 | int max_level) |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 483 | { |
| 484 | uint64_t i; |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 485 | for (i = 0; i < MM_PTE_PER_PAGE; i++) { |
| 486 | if (!arch_mm_pte_is_present(table->entries[i], level)) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 487 | continue; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 488 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 489 | |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 490 | dlog("%*s%x: %x\n", 4 * (max_level - level), "", i, |
| 491 | table->entries[i]); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 492 | |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 493 | if (arch_mm_pte_is_table(table->entries[i], level)) { |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 494 | mm_dump_table_recursive( |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 495 | mm_page_table_from_pa(arch_mm_table_from_pte( |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 496 | table->entries[i], level)), |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 497 | level - 1, max_level); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * Write the given table to the debug log. |
| 504 | */ |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 505 | void mm_ptable_dump(struct mm_ptable *t, int mode) |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 506 | { |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 507 | struct mm_page_table *tables = mm_page_table_from_pa(t->root); |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 508 | int max_level = arch_mm_max_level(mode); |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 509 | uint8_t root_table_count = arch_mm_root_table_count(mode); |
| 510 | uint8_t i; |
| 511 | for (i = 0; i < root_table_count; ++i) { |
| 512 | mm_dump_table_recursive(&tables[i], max_level, max_level); |
| 513 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | /** |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 517 | * Given that `entry` is a subtable but its entries are all absent, return the |
| 518 | * absent entry with which it can be replaced. Note that `entry` will no longer |
| 519 | * be valid after calling this function as the subtable will have been freed. |
| 520 | */ |
Andrew Scull | e982771 | 2018-10-19 14:54:20 +0100 | [diff] [blame] | 521 | static pte_t mm_table_pte_to_absent(pte_t entry, uint8_t level) |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 522 | { |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 523 | struct mm_page_table *table = |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 524 | mm_page_table_from_pa(arch_mm_table_from_pte(entry, level)); |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 525 | |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 526 | /* |
| 527 | * Free the subtable. This is safe to do directly (rather than |
| 528 | * using mm_free_page_pte) because we know by this point that it |
| 529 | * doesn't have any subtables of its own. |
| 530 | */ |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 531 | hfree(table); |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 532 | |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 533 | /* Replace subtable with a single absent entry. */ |
| 534 | return arch_mm_absent_pte(level); |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * Given that `entry` is a subtable and its entries are all identical, return |
| 539 | * the single block entry with which it can be replaced if possible. Note that |
| 540 | * `entry` will no longer be valid after calling this function as the subtable |
| 541 | * may have been freed. |
| 542 | */ |
Andrew Scull | e982771 | 2018-10-19 14:54:20 +0100 | [diff] [blame] | 543 | static pte_t mm_table_pte_to_block(pte_t entry, uint8_t level) |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 544 | { |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 545 | struct mm_page_table *table; |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 546 | uint64_t block_attrs; |
| 547 | uint64_t table_attrs; |
| 548 | uint64_t combined_attrs; |
| 549 | paddr_t block_address; |
| 550 | |
| 551 | if (!arch_mm_is_block_allowed(level)) { |
| 552 | return entry; |
| 553 | } |
| 554 | |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 555 | table = mm_page_table_from_pa(arch_mm_table_from_pte(entry, level)); |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 556 | /* |
| 557 | * Replace subtable with a single block, with equivalent |
| 558 | * attributes. |
| 559 | */ |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 560 | block_attrs = arch_mm_pte_attrs(table->entries[0], level - 1); |
| 561 | table_attrs = arch_mm_pte_attrs(entry, level); |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 562 | combined_attrs = |
| 563 | arch_mm_combine_table_entry_attrs(table_attrs, block_attrs); |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 564 | block_address = arch_mm_block_from_pte(table->entries[0], level - 1); |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 565 | /* Free the subtable. */ |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 566 | hfree(table); |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 567 | /* |
| 568 | * We can assume that the block is aligned properly |
| 569 | * because all virtual addresses are aligned by |
| 570 | * definition, and we have a 1-1 mapping from virtual to |
| 571 | * physical addresses. |
| 572 | */ |
| 573 | return arch_mm_block_pte(level, block_address, combined_attrs); |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * Defragment the given ptable entry by recursively replacing any tables with |
| 578 | * block or absent entries where possible. |
| 579 | */ |
Andrew Scull | e982771 | 2018-10-19 14:54:20 +0100 | [diff] [blame] | 580 | static pte_t mm_ptable_defrag_entry(pte_t entry, uint8_t level) |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 581 | { |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 582 | struct mm_page_table *table; |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 583 | uint64_t i; |
| 584 | uint64_t attrs; |
| 585 | bool identical_blocks_so_far = true; |
| 586 | bool all_absent_so_far = true; |
| 587 | |
| 588 | if (!arch_mm_pte_is_table(entry, level)) { |
| 589 | return entry; |
| 590 | } |
| 591 | |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 592 | table = mm_page_table_from_pa(arch_mm_table_from_pte(entry, level)); |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 593 | |
| 594 | /* |
| 595 | * Check if all entries are blocks with the same flags or are all |
| 596 | * absent. |
| 597 | */ |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 598 | attrs = arch_mm_pte_attrs(table->entries[0], level); |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 599 | for (i = 0; i < MM_PTE_PER_PAGE; ++i) { |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 600 | /* |
| 601 | * First try to defrag the entry, in case it is a subtable. |
| 602 | */ |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 603 | table->entries[i] = |
| 604 | mm_ptable_defrag_entry(table->entries[i], level - 1); |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 605 | |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 606 | if (arch_mm_pte_is_present(table->entries[i], level - 1)) { |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 607 | all_absent_so_far = false; |
| 608 | } |
| 609 | |
| 610 | /* |
| 611 | * If the entry is a block, check that the flags are the same as |
| 612 | * what we have so far. |
| 613 | */ |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 614 | if (!arch_mm_pte_is_block(table->entries[i], level - 1) || |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 615 | arch_mm_pte_attrs(table->entries[i], level) != attrs) { |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 616 | identical_blocks_so_far = false; |
| 617 | } |
| 618 | } |
| 619 | if (identical_blocks_so_far) { |
| 620 | return mm_table_pte_to_block(entry, level); |
| 621 | } |
| 622 | if (all_absent_so_far) { |
| 623 | return mm_table_pte_to_absent(entry, level); |
| 624 | } |
| 625 | return entry; |
| 626 | } |
| 627 | |
| 628 | /** |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 629 | * Defragments the given page table by converting page table references to |
| 630 | * blocks whenever possible. |
| 631 | */ |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 632 | void mm_ptable_defrag(struct mm_ptable *t, int mode) |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 633 | { |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 634 | struct mm_page_table *tables = mm_page_table_from_pa(t->root); |
Andrew Scull | e982771 | 2018-10-19 14:54:20 +0100 | [diff] [blame] | 635 | uint8_t level = arch_mm_max_level(mode); |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 636 | uint8_t root_table_count = arch_mm_root_table_count(mode); |
| 637 | uint8_t i; |
| 638 | uint64_t j; |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 639 | |
| 640 | /* |
| 641 | * Loop through each entry in the table. If it points to another table, |
| 642 | * check if that table can be replaced by a block or an absent entry. |
| 643 | */ |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 644 | for (i = 0; i < root_table_count; ++i) { |
| 645 | for (j = 0; j < MM_PTE_PER_PAGE; ++j) { |
| 646 | tables[i].entries[j] = mm_ptable_defrag_entry( |
| 647 | tables[i].entries[j], level); |
| 648 | } |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 649 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /** |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 653 | * Determines if the given address is valid in the address space of the given |
| 654 | * page table by recursively traversing all levels of the page table. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 655 | */ |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 656 | static bool mm_is_mapped_recursive(struct mm_page_table *table, |
Andrew Scull | e982771 | 2018-10-19 14:54:20 +0100 | [diff] [blame] | 657 | ptable_addr_t addr, uint8_t level) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 658 | { |
| 659 | pte_t pte; |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 660 | ptable_addr_t va_level_end = mm_level_end(addr, level); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 661 | |
| 662 | /* It isn't mapped if it doesn't fit in the table. */ |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 663 | if (addr >= va_level_end) { |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 664 | return false; |
| 665 | } |
| 666 | |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 667 | pte = table->entries[mm_index(addr, level)]; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 668 | |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 669 | if (!arch_mm_pte_is_valid(pte, level)) { |
| 670 | return false; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 671 | } |
| 672 | |
Andrew Scull | 78d6fd9 | 2018-09-06 15:08:36 +0100 | [diff] [blame] | 673 | if (arch_mm_pte_is_table(pte, level)) { |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 674 | return mm_is_mapped_recursive( |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 675 | mm_page_table_from_pa( |
| 676 | arch_mm_table_from_pte(pte, level)), |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 677 | addr, level - 1); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 678 | } |
| 679 | |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 680 | /* The entry is a valid block. */ |
| 681 | return true; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /** |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 685 | * Determines if the given address is valid in the address space of the given |
| 686 | * page table. |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 687 | */ |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 688 | static bool mm_ptable_is_mapped(struct mm_ptable *t, ptable_addr_t addr, |
| 689 | int mode) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 690 | { |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 691 | struct mm_page_table *tables = mm_page_table_from_pa(t->root); |
Andrew Scull | e982771 | 2018-10-19 14:54:20 +0100 | [diff] [blame] | 692 | uint8_t level = arch_mm_max_level(mode); |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 693 | size_t index; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 694 | |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 695 | addr = mm_round_down_to_page(addr); |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 696 | index = mm_index(addr, level + 1); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 697 | |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 698 | if (index >= arch_mm_root_table_count(mode)) { |
| 699 | return false; |
| 700 | } |
| 701 | |
| 702 | return mm_is_mapped_recursive(&tables[index], addr, level); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | /** |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 706 | * Initialises the given page table. |
| 707 | */ |
Andrew Scull | 8c3a63a | 2018-09-20 13:38:34 +0100 | [diff] [blame] | 708 | bool mm_ptable_init(struct mm_ptable *t, int mode) |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 709 | { |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 710 | uint8_t i; |
| 711 | size_t j; |
| 712 | struct mm_page_table *tables; |
| 713 | uint8_t root_table_count = arch_mm_root_table_count(mode); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 714 | |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 715 | tables = mm_alloc_page_tables(root_table_count, mode & MM_MODE_NOSYNC); |
| 716 | if (tables == NULL) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 717 | return false; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 718 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 719 | |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 720 | for (i = 0; i < root_table_count; i++) { |
| 721 | for (j = 0; j < MM_PTE_PER_PAGE; j++) { |
| 722 | tables[i].entries[j] = |
| 723 | arch_mm_absent_pte(arch_mm_max_level(mode)); |
| 724 | } |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 725 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 726 | |
Andrew Scull | 265ada9 | 2018-07-30 15:19:01 +0100 | [diff] [blame] | 727 | /* TODO: halloc could return a virtual or physical address if mm not |
| 728 | * enabled? */ |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 729 | t->root = pa_init((uintpaddr_t)tables); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 730 | |
| 731 | return true; |
| 732 | } |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 733 | |
| 734 | /** |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 735 | * Frees all memory associated with the give page table. |
| 736 | */ |
| 737 | void mm_ptable_fini(struct mm_ptable *t, int mode) |
| 738 | { |
| 739 | struct mm_page_table *tables = mm_page_table_from_pa(t->root); |
| 740 | uint8_t level = arch_mm_max_level(mode); |
| 741 | uint8_t root_table_count = arch_mm_root_table_count(mode); |
| 742 | uint8_t i; |
| 743 | uint64_t j; |
| 744 | |
| 745 | for (i = 0; i < root_table_count; ++i) { |
| 746 | for (j = 0; j < MM_PTE_PER_PAGE; ++j) { |
| 747 | mm_free_page_pte(tables[i].entries[j], level); |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | hfree(tables); |
| 752 | } |
| 753 | |
| 754 | /** |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 755 | * Updates a VM's page table such that the given physical address range is |
| 756 | * mapped in the address space at the corresponding address range in the |
Andrew Scull | fe636b1 | 2018-07-30 14:15:54 +0100 | [diff] [blame] | 757 | * architecture-agnostic mode provided. |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 758 | */ |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 759 | bool mm_vm_identity_map(struct mm_ptable *t, paddr_t begin, paddr_t end, |
| 760 | int mode, ipaddr_t *ipa) |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 761 | { |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 762 | bool success = |
| 763 | mm_ptable_identity_map(t, begin, end, mode & ~MM_MODE_STAGE1); |
| 764 | |
| 765 | if (success && ipa != NULL) { |
| 766 | *ipa = ipa_from_pa(begin); |
| 767 | } |
| 768 | |
| 769 | return success; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | /** |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 773 | * Updates the VM's table such that the given physical address range is not |
| 774 | * mapped in the address space. |
| 775 | */ |
| 776 | bool mm_vm_unmap(struct mm_ptable *t, paddr_t begin, paddr_t end, int mode) |
| 777 | { |
| 778 | return mm_ptable_unmap(t, begin, end, mode & ~MM_MODE_STAGE1); |
| 779 | } |
| 780 | |
| 781 | /** |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 782 | * Unmaps the hypervisor pages from the given page table. |
| 783 | */ |
| 784 | bool mm_vm_unmap_hypervisor(struct mm_ptable *t, int mode) |
| 785 | { |
| 786 | /* TODO: If we add pages dynamically, they must be included here too. */ |
| 787 | return mm_vm_unmap(t, layout_text_begin(), layout_text_end(), mode) && |
| 788 | mm_vm_unmap(t, layout_rodata_begin(), layout_rodata_end(), |
| 789 | mode) && |
| 790 | mm_vm_unmap(t, layout_data_begin(), layout_data_end(), mode); |
| 791 | } |
| 792 | |
| 793 | /** |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 794 | * Checks whether the given intermediate physical addess is mapped in the given |
| 795 | * page table of a VM. |
| 796 | */ |
| 797 | bool mm_vm_is_mapped(struct mm_ptable *t, ipaddr_t ipa, int mode) |
| 798 | { |
| 799 | return mm_ptable_is_mapped(t, ipa_addr(ipa), mode & ~MM_MODE_STAGE1); |
| 800 | } |
| 801 | |
| 802 | /** |
| 803 | * Translates an intermediate physical address to a physical address. Addresses |
| 804 | * are currently identity mapped so this is a simple type convertion. Returns |
| 805 | * true if the address was mapped in the table and the address was converted. |
| 806 | */ |
| 807 | bool mm_vm_translate(struct mm_ptable *t, ipaddr_t ipa, paddr_t *pa) |
| 808 | { |
| 809 | bool mapped = mm_vm_is_mapped(t, ipa, 0); |
| 810 | |
| 811 | if (mapped) { |
| 812 | *pa = pa_init(ipa_addr(ipa)); |
| 813 | } |
| 814 | |
| 815 | return mapped; |
| 816 | } |
| 817 | |
| 818 | /** |
| 819 | * Updates the hypervisor page table such that the given physical address range |
| 820 | * is mapped into the address space at the corresponding address range in the |
| 821 | * architecture-agnostic mode provided. |
| 822 | */ |
| 823 | void *mm_identity_map(paddr_t begin, paddr_t end, int mode) |
| 824 | { |
| 825 | if (mm_ptable_identity_map(&ptable, begin, end, |
| 826 | mode | MM_MODE_STAGE1)) { |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 827 | return ptr_from_va(va_from_pa(begin)); |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | return NULL; |
| 831 | } |
| 832 | |
| 833 | /** |
| 834 | * Updates the hypervisor table such that the given physical address range is |
| 835 | * not mapped in the address space. |
| 836 | */ |
| 837 | bool mm_unmap(paddr_t begin, paddr_t end, int mode) |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 838 | { |
| 839 | return mm_ptable_unmap(&ptable, begin, end, mode | MM_MODE_STAGE1); |
| 840 | } |
| 841 | |
| 842 | /** |
| 843 | * Initialises memory management for the hypervisor itself. |
| 844 | */ |
| 845 | bool mm_init(void) |
| 846 | { |
Andrew Scull | cb0a741 | 2018-11-06 17:28:14 +0000 | [diff] [blame] | 847 | dlog_nosync("text: 0x%x - 0x%x\n", pa_addr(layout_text_begin()), |
| 848 | pa_addr(layout_text_end())); |
| 849 | dlog_nosync("rodata: 0x%x - 0x%x\n", pa_addr(layout_rodata_begin()), |
| 850 | pa_addr(layout_rodata_end())); |
| 851 | dlog_nosync("data: 0x%x - 0x%x\n", pa_addr(layout_data_begin()), |
| 852 | pa_addr(layout_data_end())); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 853 | |
Andrew Scull | 8c3a63a | 2018-09-20 13:38:34 +0100 | [diff] [blame] | 854 | if (!mm_ptable_init(&ptable, MM_MODE_NOSYNC | MM_MODE_STAGE1)) { |
Andrew Scull | cb0a741 | 2018-11-06 17:28:14 +0000 | [diff] [blame] | 855 | dlog_nosync("Unable to allocate memory for page table.\n"); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 856 | return false; |
| 857 | } |
| 858 | |
| 859 | /* Map page for uart. */ |
| 860 | /* TODO: We may not want to map this. */ |
Andrew Scull | 24e032f | 2018-10-15 17:18:12 +0100 | [diff] [blame] | 861 | mm_ptable_identity_map(&ptable, pa_init(PL011_BASE), |
| 862 | pa_add(pa_init(PL011_BASE), PAGE_SIZE), |
| 863 | MM_MODE_R | MM_MODE_W | MM_MODE_D | |
| 864 | MM_MODE_NOSYNC | MM_MODE_STAGE1); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 865 | |
| 866 | /* Map each section. */ |
Andrew Scull | 5991ec9 | 2018-10-08 14:55:02 +0100 | [diff] [blame] | 867 | mm_identity_map(layout_text_begin(), layout_text_end(), |
Andrew Scull | fe636b1 | 2018-07-30 14:15:54 +0100 | [diff] [blame] | 868 | MM_MODE_X | MM_MODE_NOSYNC); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 869 | |
Andrew Scull | 5991ec9 | 2018-10-08 14:55:02 +0100 | [diff] [blame] | 870 | mm_identity_map(layout_rodata_begin(), layout_rodata_end(), |
Andrew Scull | fe636b1 | 2018-07-30 14:15:54 +0100 | [diff] [blame] | 871 | MM_MODE_R | MM_MODE_NOSYNC); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 872 | |
Andrew Scull | 5991ec9 | 2018-10-08 14:55:02 +0100 | [diff] [blame] | 873 | mm_identity_map(layout_data_begin(), layout_data_end(), |
Andrew Scull | fe636b1 | 2018-07-30 14:15:54 +0100 | [diff] [blame] | 874 | MM_MODE_R | MM_MODE_W | MM_MODE_NOSYNC); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 875 | |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 876 | return arch_mm_init(ptable.root, true); |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | bool mm_cpu_init(void) |
| 880 | { |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 881 | return arch_mm_init(ptable.root, false); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | /** |
| 885 | * Defragments the hypervisor page table. |
| 886 | */ |
| 887 | void mm_defrag(void) |
| 888 | { |
| 889 | mm_ptable_defrag(&ptable, MM_MODE_STAGE1); |
| 890 | } |