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