Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 1 | #include "mm.h" |
| 2 | |
| 3 | #include <stdatomic.h> |
| 4 | #include <stdint.h> |
| 5 | |
| 6 | #include "alloc.h" |
| 7 | #include "dlog.h" |
| 8 | |
Andrew Scull | 4f170f5 | 2018-07-19 12:58:20 +0100 | [diff] [blame] | 9 | /* Keep macro alignment */ |
| 10 | /* clang-format off */ |
| 11 | |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 12 | #define MAP_FLAG_SYNC 0x01 |
| 13 | #define MAP_FLAG_COMMIT 0x02 |
| 14 | |
Andrew Scull | 4f170f5 | 2018-07-19 12:58:20 +0100 | [diff] [blame] | 15 | /* clang-format on */ |
| 16 | |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 17 | /** |
| 18 | * Calculates the size of the address space represented by a page table entry at |
| 19 | * the given level. |
| 20 | */ |
| 21 | static inline size_t mm_entry_size(int level) |
| 22 | { |
| 23 | return 1ull << (PAGE_BITS + level * PAGE_LEVEL_BITS); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * For a given virtual address, calculates the maximum (plus one) address that |
| 28 | * can be represented by the same table at the given level. |
| 29 | */ |
| 30 | static inline vaddr_t mm_level_end(vaddr_t va, int level) |
| 31 | { |
| 32 | size_t offset = PAGE_BITS + (level + 1) * PAGE_LEVEL_BITS; |
| 33 | return ((va >> offset) + 1) << offset; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * For a given virtual address, calculates the index at which its entry is |
| 38 | * stored in a table at the given level. |
| 39 | */ |
| 40 | static inline size_t mm_index(vaddr_t va, int level) |
| 41 | { |
| 42 | vaddr_t v = va >> (PAGE_BITS + level * PAGE_LEVEL_BITS); |
| 43 | return v & ((1ull << PAGE_LEVEL_BITS) - 1); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Populates the provided page table entry with a reference to another table if |
| 48 | * needed, that is, if it does not yet point to another table. |
| 49 | * |
| 50 | * Returns a pointer to the table the entry now points to. |
| 51 | */ |
| 52 | static pte_t *mm_populate_table_pte(pte_t *pte, int level, bool sync_alloc) |
| 53 | { |
| 54 | pte_t *ntable; |
| 55 | pte_t v = *pte; |
| 56 | pte_t new_pte; |
| 57 | size_t i; |
| 58 | size_t inc; |
| 59 | |
| 60 | /* Just return pointer to table if it's already populated. */ |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 61 | if (arch_mm_pte_is_table(v)) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 62 | return arch_mm_pte_to_table(v); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 63 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 64 | |
| 65 | /* Allocate a new table. */ |
| 66 | ntable = (sync_alloc ? halloc_aligned : halloc_aligned_nosync)( |
Andrew Scull | 4f170f5 | 2018-07-19 12:58:20 +0100 | [diff] [blame] | 67 | PAGE_SIZE, PAGE_SIZE); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 68 | if (!ntable) { |
| 69 | dlog("Failed to allocate memory for page table\n"); |
| 70 | return NULL; |
| 71 | } |
| 72 | |
| 73 | /* Determine template for new pte and its increment. */ |
| 74 | if (!arch_mm_pte_is_block(v)) { |
| 75 | inc = 0; |
| 76 | new_pte = arch_mm_absent_pte(); |
| 77 | } else { |
| 78 | inc = mm_entry_size(level - 1); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 79 | if (level == 1) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 80 | new_pte = arch_mm_block_to_page_pte(v); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 81 | } else { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 82 | new_pte = v; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 83 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /* Initialise entries in the new table. */ |
| 87 | for (i = 0; i < PAGE_SIZE / sizeof(paddr_t); i++) { |
| 88 | ntable[i] = new_pte; |
| 89 | new_pte += inc; |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * Ensure initialisation is visible before updating the actual pte, then |
| 94 | * update it. |
| 95 | */ |
| 96 | atomic_thread_fence(memory_order_release); |
| 97 | *pte = arch_mm_pa_to_table_pte((paddr_t)ntable); |
| 98 | |
| 99 | return ntable; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Frees all page-table-related memory associated with the given pte at the |
| 104 | * given level. |
| 105 | */ |
| 106 | static void mm_free_page_pte(pte_t pte, int level, bool sync) |
| 107 | { |
Andrew Scull | 020ae69 | 2018-07-19 16:20:14 +0100 | [diff] [blame^] | 108 | (void)pte; |
| 109 | (void)level; |
| 110 | (void)sync; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 111 | /* TODO: Implement. |
| 112 | if (!arch_mm_pte_is_present(pte) || level < 1) |
| 113 | return; |
| 114 | */ |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Updates the page table at the given level to map the given virtual address |
| 119 | * range to a physical range using the provided (architecture-specific) |
| 120 | * attributes. |
| 121 | * |
| 122 | * This function calls itself recursively if it needs to update additional |
| 123 | * levels, but the recursion is bound by the maximum number of levels in a page |
| 124 | * table. |
| 125 | */ |
| 126 | static bool mm_map_level(vaddr_t va, vaddr_t va_end, paddr_t pa, uint64_t attrs, |
| 127 | pte_t *table, int level, int flags) |
| 128 | { |
| 129 | size_t i = mm_index(va, level); |
| 130 | vaddr_t va_level_end = mm_level_end(va, level); |
| 131 | size_t entry_size = mm_entry_size(level); |
| 132 | bool commit = flags & MAP_FLAG_COMMIT; |
| 133 | bool sync = flags & MAP_FLAG_SYNC; |
| 134 | |
| 135 | /* Cap va_end so that we don't go over the current level max. */ |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 136 | if (va_end > va_level_end) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 137 | va_end = va_level_end; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 138 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 139 | |
| 140 | /* Fill each entry in the table. */ |
| 141 | while (va < va_end) { |
| 142 | if (level == 0) { |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 143 | if (commit) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 144 | table[i] = arch_mm_pa_to_page_pte(pa, attrs); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 145 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 146 | } else if ((va_end - va) >= entry_size && |
| 147 | arch_mm_is_block_allowed(level) && |
| 148 | (va & (entry_size - 1)) == 0) { |
| 149 | if (commit) { |
| 150 | pte_t pte = table[i]; |
| 151 | table[i] = arch_mm_pa_to_block_pte(pa, attrs); |
| 152 | /* TODO: Add barrier. How do we ensure this |
| 153 | * isn't in use by another CPU? Send IPI? */ |
| 154 | mm_free_page_pte(pte, level, sync); |
| 155 | } |
| 156 | } else { |
Andrew Scull | 4f170f5 | 2018-07-19 12:58:20 +0100 | [diff] [blame] | 157 | pte_t *nt = |
| 158 | mm_populate_table_pte(table + i, level, sync); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 159 | if (!nt) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 160 | return false; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 161 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 162 | |
Andrew Scull | 4f170f5 | 2018-07-19 12:58:20 +0100 | [diff] [blame] | 163 | if (!mm_map_level(va, va_end, pa, attrs, nt, level - 1, |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 164 | flags)) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 165 | return false; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 166 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | va = (va + entry_size) & ~(entry_size - 1); |
| 170 | pa = (pa + entry_size) & ~(entry_size - 1); |
| 171 | i++; |
| 172 | } |
| 173 | |
| 174 | return true; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Invalidates the TLB for the given virtual address range. |
| 179 | */ |
| 180 | static void mm_invalidate_tlb(vaddr_t begin, vaddr_t end, bool stage1) |
| 181 | { |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 182 | if (stage1) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 183 | arch_mm_invalidate_stage1_range(begin, end); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 184 | } else { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 185 | arch_mm_invalidate_stage2_range(begin, end); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 186 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Updates the given table such that the given virtual address range is mapped |
| 191 | * to the given physical address range in the architecture-agnostic mode |
| 192 | * provided. |
| 193 | */ |
| 194 | bool mm_ptable_map(struct mm_ptable *t, vaddr_t begin, vaddr_t end, |
| 195 | paddr_t paddr, int mode) |
| 196 | { |
| 197 | uint64_t attrs = arch_mm_mode_to_attrs(mode); |
| 198 | int flags = (mode & MM_MODE_NOSYNC) ? 0 : MAP_FLAG_SYNC; |
| 199 | int level = arch_mm_max_level(&t->arch); |
| 200 | |
| 201 | begin = arch_mm_clear_va(begin); |
| 202 | end = arch_mm_clear_va(end + PAGE_SIZE - 1); |
| 203 | paddr = arch_mm_clear_pa(paddr); |
| 204 | |
| 205 | /* |
| 206 | * Do it in two steps to prevent leaving the table in a halfway updated |
| 207 | * state. In such a two-step implementation, the table may be left with |
| 208 | * extra internal tables, but no different mapping on failure. |
| 209 | */ |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 210 | if (!mm_map_level(begin, end, paddr, attrs, t->table, level, flags)) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 211 | return false; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 212 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 213 | |
| 214 | mm_map_level(begin, end, paddr, attrs, t->table, level, |
| 215 | flags | MAP_FLAG_COMMIT); |
| 216 | |
| 217 | /* Invalidate the tlb. */ |
| 218 | mm_invalidate_tlb(begin, end, (mode & MM_MODE_STAGE1) != 0); |
| 219 | |
| 220 | return true; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Updates the given table such that the given virtual address range is not |
| 225 | * mapped to any physical address. |
| 226 | */ |
| 227 | bool mm_ptable_unmap(struct mm_ptable *t, vaddr_t begin, vaddr_t end, int mode) |
| 228 | { |
| 229 | int flags = (mode & MM_MODE_NOSYNC) ? 0 : MAP_FLAG_SYNC; |
| 230 | int level = arch_mm_max_level(&t->arch); |
| 231 | |
| 232 | begin = arch_mm_clear_va(begin); |
| 233 | end = arch_mm_clear_va(end + PAGE_SIZE - 1); |
| 234 | |
| 235 | /* Also do updates in two steps, similarly to mm_ptable_map. */ |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 236 | if (!mm_map_level(begin, end, begin, 0, t->table, level, flags)) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 237 | return false; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 238 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 239 | |
| 240 | mm_map_level(begin, end, begin, 0, t->table, level, |
| 241 | flags | MAP_FLAG_COMMIT); |
| 242 | |
| 243 | /* Invalidate the tlb. */ |
| 244 | mm_invalidate_tlb(begin, end, (mode & MM_MODE_STAGE1) != 0); |
| 245 | |
| 246 | return true; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Updates the given table such that a single virtual address page is mapped |
| 251 | * to a single physical address page in the provided architecture-agnostic mode. |
| 252 | */ |
| 253 | bool mm_ptable_map_page(struct mm_ptable *t, vaddr_t va, paddr_t pa, int mode) |
| 254 | { |
| 255 | size_t i; |
| 256 | uint64_t attrs = arch_mm_mode_to_attrs(mode); |
| 257 | pte_t *table = t->table; |
| 258 | bool sync = !(mode & MM_MODE_NOSYNC); |
| 259 | |
| 260 | va = arch_mm_clear_va(va); |
| 261 | pa = arch_mm_clear_pa(pa); |
| 262 | |
| 263 | for (i = arch_mm_max_level(&t->arch); i > 0; i--) { |
| 264 | table = mm_populate_table_pte(table + mm_index(va, i), i, sync); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 265 | if (!table) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 266 | return false; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 267 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | i = mm_index(va, 0); |
| 271 | table[i] = arch_mm_pa_to_page_pte(pa, attrs); |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Writes the given table to the debug log, calling itself recursively to |
| 277 | * write sub-tables. |
| 278 | */ |
| 279 | static void mm_dump_table_recursive(pte_t *table, int level, int max_level) |
| 280 | { |
| 281 | uint64_t i; |
| 282 | for (i = 0; i < PAGE_SIZE / sizeof(pte_t); i++) { |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 283 | if (!arch_mm_pte_is_present(table[i])) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 284 | continue; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 285 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 286 | |
| 287 | dlog("%*s%x: %x\n", 4 * (max_level - level), "", i, table[i]); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 288 | if (!level) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 289 | continue; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 290 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 291 | |
| 292 | if (arch_mm_pte_is_table(table[i])) { |
| 293 | mm_dump_table_recursive(arch_mm_pte_to_table(table[i]), |
| 294 | level - 1, max_level); |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Write the given table to the debug log. |
| 301 | */ |
| 302 | void mm_ptable_dump(struct mm_ptable *t) |
| 303 | { |
| 304 | int max_level = arch_mm_max_level(&t->arch); |
| 305 | mm_dump_table_recursive(t->table, max_level, max_level); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Defragments the given page table by converting page table references to |
| 310 | * blocks whenever possible. |
| 311 | */ |
| 312 | void mm_ptable_defrag(struct mm_ptable *t) |
| 313 | { |
| 314 | /* TODO: Implement. */ |
Andrew Scull | 020ae69 | 2018-07-19 16:20:14 +0100 | [diff] [blame^] | 315 | (void)t; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Initialises the given page table. |
| 320 | */ |
| 321 | bool mm_ptable_init(struct mm_ptable *t, int mode) |
| 322 | { |
| 323 | size_t i; |
| 324 | pte_t *table; |
| 325 | |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 326 | if (mode & MM_MODE_NOSYNC) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 327 | table = halloc_aligned_nosync(PAGE_SIZE, PAGE_SIZE); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 328 | } else { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 329 | table = halloc_aligned(PAGE_SIZE, PAGE_SIZE); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 330 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 331 | |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 332 | if (!table) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 333 | return false; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 334 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 335 | |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 336 | for (i = 0; i < PAGE_SIZE / sizeof(pte_t); i++) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 337 | table[i] = arch_mm_absent_pte(); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 338 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 339 | |
| 340 | t->table = table; |
| 341 | arch_mm_ptable_init(&t->arch); |
| 342 | |
| 343 | return true; |
| 344 | } |