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 | * |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style |
| 5 | * license that can be found in the LICENSE file or at |
| 6 | * https://opensource.org/licenses/BSD-3-Clause. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 9 | #include "hf/mm.h" |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 10 | |
| 11 | #include <stdatomic.h> |
| 12 | #include <stdint.h> |
| 13 | |
Maksims Svecovs | 134b8f9 | 2022-03-04 15:14:09 +0000 | [diff] [blame] | 14 | #include "hf/arch/init.h" |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 15 | #include "hf/arch/mm.h" |
Maksims Svecovs | 134b8f9 | 2022-03-04 15:14:09 +0000 | [diff] [blame] | 16 | |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 17 | #include "hf/check.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 18 | #include "hf/dlog.h" |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 19 | #include "hf/ffa.h" |
Andrew Scull | 5991ec9 | 2018-10-08 14:55:02 +0100 | [diff] [blame] | 20 | #include "hf/layout.h" |
Andrew Walbran | 4869936 | 2019-05-20 14:38:00 +0100 | [diff] [blame] | 21 | #include "hf/plat/console.h" |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 22 | #include "hf/static_assert.h" |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 23 | |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 24 | /** |
| 25 | * This file has functions for managing the level 1 and 2 page tables used by |
| 26 | * Hafnium. There is a level 1 mapping used by Hafnium itself to access memory, |
| 27 | * and then a level 2 mapping per VM. The design assumes that all page tables |
| 28 | * contain only 1-1 mappings, aligned on the block boundaries. |
| 29 | */ |
| 30 | |
Wedson Almeida Filho | b2c159e | 2018-10-25 13:27:47 +0100 | [diff] [blame] | 31 | /* |
| 32 | * For stage 2, the input is an intermediate physical addresses rather than a |
| 33 | * virtual address so: |
| 34 | */ |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 35 | static_assert( |
| 36 | sizeof(ptable_addr_t) == sizeof(uintpaddr_t), |
| 37 | "Currently, the same code manages the stage 1 and stage 2 page tables " |
| 38 | "which only works if the virtual and intermediate physical addresses " |
| 39 | "are the same size. It looks like that assumption might not be holding " |
| 40 | "so we need to check that everything is going to be ok."); |
| 41 | |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 42 | static struct mm_ptable ptable; |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 43 | static struct spinlock ptable_lock; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 44 | |
Andrew Scull | da24197 | 2019-01-05 18:17:48 +0000 | [diff] [blame] | 45 | static bool mm_stage2_invalidate = false; |
| 46 | |
| 47 | /** |
| 48 | * After calling this function, modifications to stage-2 page tables will use |
| 49 | * break-before-make and invalidate the TLB for the affected range. |
| 50 | */ |
| 51 | void mm_vm_enable_invalidation(void) |
| 52 | { |
| 53 | mm_stage2_invalidate = true; |
| 54 | } |
| 55 | |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 56 | /** |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 57 | * Get the page table from the physical address. |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 58 | */ |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 59 | 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] | 60 | { |
| 61 | return ptr_from_va(va_from_pa(pa)); |
| 62 | } |
| 63 | |
| 64 | /** |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 65 | * Rounds an address down to a page boundary. |
| 66 | */ |
| 67 | static ptable_addr_t mm_round_down_to_page(ptable_addr_t addr) |
| 68 | { |
| 69 | return addr & ~((ptable_addr_t)(PAGE_SIZE - 1)); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Rounds an address up to a page boundary. |
| 74 | */ |
| 75 | static ptable_addr_t mm_round_up_to_page(ptable_addr_t addr) |
| 76 | { |
| 77 | return mm_round_down_to_page(addr + PAGE_SIZE - 1); |
| 78 | } |
| 79 | |
| 80 | /** |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 81 | * Calculates the size of the address space represented by a page table entry at |
| 82 | * the given level. |
| 83 | */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 84 | static size_t mm_entry_size(mm_level_t level) |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 85 | { |
Andrew Scull | 78d6fd9 | 2018-09-06 15:08:36 +0100 | [diff] [blame] | 86 | return UINT64_C(1) << (PAGE_BITS + level * PAGE_LEVEL_BITS); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
Andrew Scull | cae4557 | 2018-12-13 15:46:30 +0000 | [diff] [blame] | 90 | * Gets the address of the start of the next block of the given size. The size |
| 91 | * must be a power of two. |
| 92 | */ |
| 93 | static ptable_addr_t mm_start_of_next_block(ptable_addr_t addr, |
| 94 | size_t block_size) |
| 95 | { |
| 96 | return (addr + block_size) & ~(block_size - 1); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Gets the physical address of the start of the next block of the given size. |
| 101 | * The size must be a power of two. |
| 102 | */ |
| 103 | static paddr_t mm_pa_start_of_next_block(paddr_t pa, size_t block_size) |
| 104 | { |
| 105 | return pa_init((pa_addr(pa) + block_size) & ~(block_size - 1)); |
| 106 | } |
| 107 | |
| 108 | /** |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 109 | * For a given address, calculates the maximum (plus one) address that can be |
| 110 | * represented by the same table at the given level. |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 111 | */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 112 | static ptable_addr_t mm_level_end(ptable_addr_t addr, mm_level_t level) |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 113 | { |
| 114 | size_t offset = PAGE_BITS + (level + 1) * PAGE_LEVEL_BITS; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 115 | |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 116 | return ((addr >> offset) + 1) << offset; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 120 | * For a given address, calculates the index at which its entry is stored in a |
| 121 | * table at the given level. |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 122 | */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 123 | static size_t mm_index(ptable_addr_t addr, mm_level_t level) |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 124 | { |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 125 | ptable_addr_t v = addr >> (PAGE_BITS + level * PAGE_LEVEL_BITS); |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 126 | |
Andrew Scull | 78d6fd9 | 2018-09-06 15:08:36 +0100 | [diff] [blame] | 127 | return v & ((UINT64_C(1) << PAGE_LEVEL_BITS) - 1); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 131 | * Allocates a new page table. |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 132 | */ |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 133 | static struct mm_page_table *mm_alloc_page_tables(size_t count, |
| 134 | struct mpool *ppool) |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 135 | { |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 136 | if (count == 1) { |
| 137 | return mpool_alloc(ppool); |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 140 | return mpool_alloc_contiguous(ppool, count, count); |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 144 | * Returns the maximum level in the page table given the flags. |
| 145 | */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 146 | static mm_level_t mm_max_level(struct mm_flags flags) |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 147 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 148 | return flags.stage1 ? arch_mm_stage1_max_level() |
| 149 | : arch_mm_stage2_max_level(); |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Returns the number of root-level tables given the flags. |
| 154 | */ |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 155 | static uint8_t mm_root_table_count(struct mm_flags flags) |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 156 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 157 | return flags.stage1 ? arch_mm_stage1_root_table_count() |
| 158 | : arch_mm_stage2_root_table_count(); |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 162 | * Invalidates the TLB for the given address range. |
| 163 | */ |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 164 | static void mm_invalidate_tlb(const struct mm_ptable *ptable, |
| 165 | ptable_addr_t begin, ptable_addr_t end, |
| 166 | struct mm_flags flags, bool non_secure) |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 167 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 168 | if (flags.stage1) { |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 169 | arch_mm_invalidate_stage1_range(ptable->id, va_init(begin), |
Raghu Krishnamurthy | 8fdd6df | 2021-02-03 18:30:59 -0800 | [diff] [blame] | 170 | va_init(end)); |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 171 | } else { |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 172 | arch_mm_invalidate_stage2_range(ptable->id, ipa_init(begin), |
Olivier Deprez | 6f40037 | 2022-03-07 09:31:08 +0100 | [diff] [blame] | 173 | ipa_init(end), non_secure); |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Frees all page-table-related memory associated with the given pte at the |
| 179 | * given level, including any subtables recursively. |
| 180 | */ |
Daniel Boulby | 8adf748 | 2021-09-22 15:12:44 +0100 | [diff] [blame] | 181 | // NOLINTNEXTLINE(misc-no-recursion) |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 182 | static void mm_free_page_pte(pte_t pte, mm_level_t level, struct mpool *ppool) |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 183 | { |
| 184 | struct mm_page_table *table; |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 185 | |
| 186 | if (!arch_mm_pte_is_table(pte, level)) { |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | /* Recursively free any subtables. */ |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 191 | table = mm_page_table_from_pa(arch_mm_table_from_pte(pte, level)); |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 192 | for (size_t i = 0; i < MM_PTE_PER_PAGE; ++i) { |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 193 | mm_free_page_pte(table->entries[i], level - 1, ppool); |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* Free the table itself. */ |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 197 | mpool_free(ppool, table); |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /** |
David Brazdil | 711fbe9 | 2019-08-06 13:39:58 +0100 | [diff] [blame] | 201 | * Returns the first address which cannot be encoded in page tables given by |
| 202 | * `flags`. It is the exclusive end of the address space created by the tables. |
| 203 | */ |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 204 | ptable_addr_t mm_ptable_addr_space_end(struct mm_flags flags) |
David Brazdil | 711fbe9 | 2019-08-06 13:39:58 +0100 | [diff] [blame] | 205 | { |
| 206 | return mm_root_table_count(flags) * |
| 207 | mm_entry_size(mm_max_level(flags) + 1); |
| 208 | } |
| 209 | |
| 210 | /** |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 211 | * Initialises the given page table. |
| 212 | */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 213 | bool mm_ptable_init(struct mm_ptable *ptable, mm_asid_t id, |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 214 | struct mm_flags flags, struct mpool *ppool) |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 215 | { |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 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 | |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 224 | for (size_t i = 0; i < root_table_count; i++) { |
| 225 | for (size_t j = 0; j < MM_PTE_PER_PAGE; j++) { |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 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 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 235 | ptable->root = pa_init((uintpaddr_t)tables); |
| 236 | ptable->id = id; |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 237 | return true; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Frees all memory associated with the give page table. |
| 242 | */ |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 243 | static void mm_ptable_fini(const struct mm_ptable *ptable, |
| 244 | struct mm_flags flags, struct mpool *ppool) |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 245 | { |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 246 | struct mm_page_table *tables = mm_page_table_from_pa(ptable->root); |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 247 | mm_level_t level = mm_max_level(flags); |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 248 | uint8_t root_table_count = mm_root_table_count(flags); |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 249 | |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 250 | for (size_t i = 0; i < root_table_count; ++i) { |
| 251 | for (size_t j = 0; j < MM_PTE_PER_PAGE; ++j) { |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 252 | mm_free_page_pte(tables[i].entries[j], level, ppool); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | mpool_add_chunk(ppool, tables, |
| 257 | sizeof(struct mm_page_table) * root_table_count); |
| 258 | } |
| 259 | |
| 260 | /** |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 261 | * 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] | 262 | * are valid, it performs a break-before-make sequence where it first writes an |
| 263 | * invalid value to the PTE, flushes the TLB, then writes the actual new value. |
| 264 | * This is to prevent cases where CPUs have different 'valid' values in their |
| 265 | * TLBs, which may result in issues for example in cache coherency. |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 266 | */ |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 267 | static void mm_replace_entry(const struct mm_ptable *ptable, |
| 268 | ptable_addr_t begin, pte_t *pte, pte_t new_pte, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 269 | mm_level_t level, struct mm_flags flags, |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 270 | bool non_secure, struct mpool *ppool) |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 271 | { |
| 272 | pte_t v = *pte; |
| 273 | |
| 274 | /* |
| 275 | * 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] | 276 | * present and the TLB is being invalidated. |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 277 | */ |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 278 | if ((flags.stage1 || mm_stage2_invalidate) && |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 279 | arch_mm_pte_is_valid(v, level)) { |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 280 | *pte = arch_mm_absent_pte(level); |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 281 | mm_invalidate_tlb(ptable, begin, begin + mm_entry_size(level), |
| 282 | flags, non_secure); |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /* Assign the new pte. */ |
| 286 | *pte = new_pte; |
| 287 | |
| 288 | /* Free pages that aren't in use anymore. */ |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 289 | mm_free_page_pte(v, level, ppool); |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /** |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 293 | * Populates the provided page table entry with a reference to another table if |
| 294 | * needed, that is, if it does not yet point to another table. |
| 295 | * |
| 296 | * Returns a pointer to the table the entry now points to. |
| 297 | */ |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 298 | static struct mm_page_table *mm_populate_table_pte( |
| 299 | const struct mm_ptable *ptable, ptable_addr_t begin, pte_t *pte, |
| 300 | mm_level_t level, struct mm_flags flags, bool non_secure, |
| 301 | struct mpool *ppool) |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 302 | { |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 303 | struct mm_page_table *ntable; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 304 | pte_t v = *pte; |
| 305 | pte_t new_pte; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 306 | size_t inc; |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 307 | mm_level_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) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 317 | dlog_error("Failed to allocate memory for page table\n"); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 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. */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 333 | for (size_t i = 0; i < MM_PTE_PER_PAGE; i++) { |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 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. */ |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 342 | mm_replace_entry(ptable, begin, pte, |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 343 | arch_mm_table_pte(level, pa_init((uintpaddr_t)ntable)), |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 344 | level, flags, non_secure, ppool); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 345 | |
| 346 | return ntable; |
| 347 | } |
| 348 | |
| 349 | /** |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 350 | * 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] | 351 | * physical range using the provided (architecture-specific) attributes. Or if |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 352 | * `flags.unmap` is set, unmap the given range instead. |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 353 | * |
| 354 | * This function calls itself recursively if it needs to update additional |
| 355 | * levels, but the recursion is bound by the maximum number of levels in a page |
| 356 | * table. |
| 357 | */ |
Daniel Boulby | 8adf748 | 2021-09-22 15:12:44 +0100 | [diff] [blame] | 358 | // NOLINTNEXTLINE(misc-no-recursion) |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 359 | static bool mm_map_level(struct mm_ptable *ptable, ptable_addr_t begin, |
| 360 | ptable_addr_t end, paddr_t pa, mm_attr_t attrs, |
| 361 | struct mm_page_table *child_table, mm_level_t level, |
| 362 | struct mm_flags flags, struct mpool *ppool) |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 363 | { |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 364 | pte_t *pte = &child_table->entries[mm_index(begin, level)]; |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 365 | ptable_addr_t level_end = mm_level_end(begin, level); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 366 | size_t entry_size = mm_entry_size(level); |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 367 | bool commit = flags.commit; |
| 368 | bool unmap = flags.unmap; |
| 369 | bool non_secure = ((attrs & (1ULL << 57)) != 0); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 370 | |
Andrew Scull | 265ada9 | 2018-07-30 15:19:01 +0100 | [diff] [blame] | 371 | /* Cap end so that we don't go over the current level max. */ |
| 372 | if (end > level_end) { |
| 373 | end = level_end; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 374 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 375 | |
| 376 | /* Fill each entry in the table. */ |
Andrew Scull | 265ada9 | 2018-07-30 15:19:01 +0100 | [diff] [blame] | 377 | while (begin < end) { |
Andrew Walbran | 6324fc9 | 2018-10-03 11:46:43 +0100 | [diff] [blame] | 378 | if (unmap ? !arch_mm_pte_is_present(*pte, level) |
| 379 | : arch_mm_pte_is_block(*pte, level) && |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 380 | arch_mm_pte_attrs(*pte, level) == attrs) { |
Andrew Walbran | 6324fc9 | 2018-10-03 11:46:43 +0100 | [diff] [blame] | 381 | /* |
| 382 | * If the entry is already mapped with the right |
| 383 | * attributes, or already absent in the case of |
| 384 | * unmapping, no need to do anything; carry on to the |
| 385 | * next entry. |
| 386 | */ |
| 387 | } else if ((end - begin) >= entry_size && |
| 388 | (unmap || arch_mm_is_block_allowed(level)) && |
| 389 | (begin & (entry_size - 1)) == 0) { |
| 390 | /* |
| 391 | * If the entire entry is within the region we want to |
| 392 | * map, map/unmap the whole entry. |
| 393 | */ |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 394 | if (commit) { |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 395 | pte_t new_pte = |
| 396 | unmap ? arch_mm_absent_pte(level) |
| 397 | : arch_mm_block_pte(level, pa, |
| 398 | attrs); |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 399 | mm_replace_entry(ptable, begin, pte, new_pte, |
| 400 | level, flags, non_secure, |
| 401 | ppool); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 402 | } |
| 403 | } else { |
Andrew Walbran | 6324fc9 | 2018-10-03 11:46:43 +0100 | [diff] [blame] | 404 | /* |
| 405 | * If the entry is already a subtable get it; otherwise |
| 406 | * replace it with an equivalent subtable and get that. |
| 407 | */ |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 408 | struct mm_page_table *nt = |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 409 | mm_populate_table_pte(ptable, begin, pte, level, |
| 410 | flags, non_secure, ppool); |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 411 | if (nt == NULL) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 412 | return false; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 413 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 414 | |
Andrew Walbran | 6324fc9 | 2018-10-03 11:46:43 +0100 | [diff] [blame] | 415 | /* |
| 416 | * Recurse to map/unmap the appropriate entries within |
| 417 | * the subtable. |
| 418 | */ |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 419 | if (!mm_map_level(ptable, begin, end, pa, attrs, nt, |
| 420 | level - 1, flags, ppool)) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 421 | return false; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 422 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 423 | } |
| 424 | |
Andrew Scull | cae4557 | 2018-12-13 15:46:30 +0000 | [diff] [blame] | 425 | begin = mm_start_of_next_block(begin, entry_size); |
| 426 | pa = mm_pa_start_of_next_block(pa, entry_size); |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 427 | pte++; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | return true; |
| 431 | } |
| 432 | |
| 433 | /** |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 434 | * Updates the page table from the root to map the given address range to a |
| 435 | * physical range using the provided (architecture-specific) attributes. Or if |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 436 | * `flags.unmap` is set, unmap the given range instead. |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 437 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 438 | static bool mm_map_root(struct mm_ptable *ptable, ptable_addr_t begin, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 439 | ptable_addr_t end, mm_attr_t attrs, |
| 440 | mm_level_t root_level, struct mm_flags flags, |
| 441 | struct mpool *ppool) |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 442 | { |
| 443 | size_t root_table_size = mm_entry_size(root_level); |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 444 | struct mm_page_table *child_table = &mm_page_table_from_pa( |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 445 | ptable->root)[mm_index(begin, root_level)]; |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 446 | |
| 447 | while (begin < end) { |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 448 | if (!mm_map_level(ptable, begin, end, pa_init(begin), attrs, |
| 449 | child_table, root_level - 1, flags, ppool)) { |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 450 | return false; |
| 451 | } |
Andrew Scull | cae4557 | 2018-12-13 15:46:30 +0000 | [diff] [blame] | 452 | begin = mm_start_of_next_block(begin, root_table_size); |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 453 | child_table++; |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | return true; |
| 457 | } |
| 458 | |
| 459 | /** |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 460 | * 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] | 461 | * or not mapped into the address space with the architecture-agnostic mode |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 462 | * provided. Only commits the change if `flags.commit` is set. |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 463 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 464 | static bool mm_ptable_identity_map(struct mm_ptable *ptable, paddr_t pa_begin, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 465 | paddr_t pa_end, mm_attr_t attrs, |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 466 | struct mm_flags flags, struct mpool *ppool) |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 467 | { |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 468 | mm_level_t root_level = mm_max_level(flags) + 1; |
David Brazdil | 711fbe9 | 2019-08-06 13:39:58 +0100 | [diff] [blame] | 469 | ptable_addr_t ptable_end = mm_ptable_addr_space_end(flags); |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 470 | ptable_addr_t end = mm_round_up_to_page(pa_addr(pa_end)); |
| 471 | 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] | 472 | |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 473 | /* |
Andrew Scull | f825293 | 2019-04-04 13:51:22 +0100 | [diff] [blame] | 474 | * Assert condition to communicate the API constraint of mm_max_level(), |
| 475 | * that isn't encoded in the types, to the static analyzer. |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 476 | */ |
Daniel Boulby | a2f8c66 | 2021-11-26 17:52:53 +0000 | [diff] [blame] | 477 | assert(root_level >= 2); |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 478 | |
| 479 | /* Cap end to stay within the bounds of the page table. */ |
| 480 | if (end > ptable_end) { |
| 481 | end = ptable_end; |
| 482 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 483 | |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 484 | if (!mm_map_root(ptable, begin, end, attrs, root_level, flags, ppool)) { |
Andrew Walbran | 58a6e54 | 2019-11-19 14:23:15 +0000 | [diff] [blame] | 485 | return false; |
| 486 | } |
| 487 | |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 488 | /* |
| 489 | * All TLB invalidations must be complete already if any entries were |
| 490 | * replaced by mm_replace_entry. Sync all page table writes so that code |
| 491 | * following this can use them. |
| 492 | */ |
| 493 | arch_mm_sync_table_writes(); |
Andrew Walbran | 58a6e54 | 2019-11-19 14:23:15 +0000 | [diff] [blame] | 494 | |
| 495 | return true; |
| 496 | } |
| 497 | |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 498 | /* |
| 499 | * Prepares the given page table for the given address mapping such that it |
| 500 | * will be able to commit the change without failure. It does so by ensuring |
| 501 | * the smallest granularity needed is available. This remains valid provided |
Fuad Tabba | 9dc276f | 2020-07-16 09:29:32 +0100 | [diff] [blame] | 502 | * subsequent operations do not decrease the granularity. |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 503 | * |
| 504 | * In particular, multiple calls to this function will result in the |
| 505 | * corresponding calls to commit the changes to succeed. |
| 506 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 507 | static bool mm_ptable_identity_prepare(struct mm_ptable *ptable, |
| 508 | paddr_t pa_begin, paddr_t pa_end, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 509 | mm_attr_t attrs, struct mm_flags flags, |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 510 | struct mpool *ppool) |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 511 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 512 | flags.commit = false; |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 513 | return mm_ptable_identity_map(ptable, pa_begin, pa_end, attrs, flags, |
| 514 | ppool); |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | /** |
| 518 | * Commits the given address mapping to the page table assuming the operation |
| 519 | * cannot fail. `mm_ptable_identity_prepare` must used correctly before this to |
| 520 | * ensure this condition. |
| 521 | * |
| 522 | * Without the table being properly prepared, the commit may only partially |
| 523 | * complete if it runs out of memory resulting in an inconsistent state that |
| 524 | * isn't handled. |
| 525 | * |
| 526 | * Since the non-failure assumtion is used in the reasoning about the atomicity |
| 527 | * of higher level memory operations, any detected violations result in a panic. |
| 528 | * |
| 529 | * TODO: remove ppool argument to be sure no changes are made. |
| 530 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 531 | static void mm_ptable_identity_commit(struct mm_ptable *ptable, |
| 532 | paddr_t pa_begin, paddr_t pa_end, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 533 | mm_attr_t attrs, struct mm_flags flags, |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 534 | struct mpool *ppool) |
| 535 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 536 | flags.commit = true; |
| 537 | CHECK(mm_ptable_identity_map(ptable, pa_begin, pa_end, attrs, flags, |
| 538 | ppool)); |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Andrew Walbran | 58a6e54 | 2019-11-19 14:23:15 +0000 | [diff] [blame] | 541 | /** |
| 542 | * Updates the given table such that the given physical address range is mapped |
| 543 | * or not mapped into the address space with the architecture-agnostic mode |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 544 | * provided. |
| 545 | * |
| 546 | * The page table is updated using the separate prepare and commit stages so |
| 547 | * that, on failure, a partial update of the address space cannot happen. The |
| 548 | * table may be left with extra internal tables but the address space is |
| 549 | * unchanged. |
Andrew Walbran | 58a6e54 | 2019-11-19 14:23:15 +0000 | [diff] [blame] | 550 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 551 | static bool mm_ptable_identity_update(struct mm_ptable *ptable, |
| 552 | paddr_t pa_begin, paddr_t pa_end, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 553 | mm_attr_t attrs, struct mm_flags flags, |
Andrew Walbran | 58a6e54 | 2019-11-19 14:23:15 +0000 | [diff] [blame] | 554 | struct mpool *ppool) |
| 555 | { |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 556 | if (!mm_ptable_identity_prepare(ptable, pa_begin, pa_end, attrs, flags, |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 557 | ppool)) { |
| 558 | return false; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 559 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 560 | |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 561 | mm_ptable_identity_commit(ptable, pa_begin, pa_end, attrs, flags, |
| 562 | ppool); |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 563 | |
| 564 | return true; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 565 | } |
| 566 | |
Karl Meakin | c88ad41 | 2025-02-11 16:04:49 +0000 | [diff] [blame] | 567 | static void mm_dump_entries(const pte_t *entries, mm_level_t level, |
| 568 | uint32_t indent); |
| 569 | |
| 570 | static void mm_dump_block_entry(pte_t entry, mm_level_t level, uint32_t indent) |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 571 | { |
Karl Meakin | c88ad41 | 2025-02-11 16:04:49 +0000 | [diff] [blame] | 572 | mm_attr_t attrs = arch_mm_pte_attrs(entry, level); |
| 573 | paddr_t addr = arch_mm_block_from_pte(entry, level); |
| 574 | |
| 575 | if (arch_mm_pte_is_valid(entry, level)) { |
| 576 | if (level == 0) { |
| 577 | dlog("page {\n"); |
| 578 | } else { |
| 579 | dlog("block {\n"); |
| 580 | } |
| 581 | } else { |
| 582 | dlog("invalid_block {\n"); |
| 583 | } |
| 584 | |
| 585 | indent += 1; |
| 586 | { |
| 587 | dlog_indent(indent, ".addr = %#016lx\n", pa_addr(addr)); |
| 588 | dlog_indent(indent, ".attrs = %#016lx\n", attrs); |
| 589 | } |
| 590 | indent -= 1; |
| 591 | dlog_indent(indent, "}"); |
| 592 | } |
| 593 | |
| 594 | // NOLINTNEXTLINE(misc-no-recursion) |
| 595 | static void mm_dump_table_entry(pte_t entry, mm_level_t level, uint32_t indent) |
| 596 | { |
| 597 | dlog("table {\n"); |
| 598 | indent += 1; |
| 599 | { |
| 600 | mm_attr_t attrs = arch_mm_pte_attrs(entry, level); |
| 601 | paddr_t addr = arch_mm_table_from_pte(entry, level); |
| 602 | const struct mm_page_table *child_table = |
| 603 | mm_page_table_from_pa(addr); |
| 604 | |
| 605 | dlog_indent(indent, ".pte = %#016lx,\n", entry); |
| 606 | dlog_indent(indent, ".attrs = %#016lx,\n", attrs); |
| 607 | dlog_indent(indent, ".addr = %#016lx,\n", pa_addr(addr)); |
| 608 | dlog_indent(indent, ".entries = "); |
| 609 | mm_dump_entries(child_table->entries, level - 1, indent); |
| 610 | dlog(",\n"); |
| 611 | } |
| 612 | indent -= 1; |
| 613 | dlog_indent(indent, "}"); |
| 614 | } |
| 615 | |
| 616 | // NOLINTNEXTLINE(misc-no-recursion) |
| 617 | static void mm_dump_entry(pte_t entry, mm_level_t level, uint32_t indent) |
| 618 | { |
| 619 | switch (arch_mm_pte_type(entry, level)) { |
| 620 | case PTE_TYPE_ABSENT: |
| 621 | dlog("absent {}"); |
| 622 | break; |
| 623 | case PTE_TYPE_INVALID_BLOCK: |
| 624 | case PTE_TYPE_VALID_BLOCK: { |
| 625 | mm_dump_block_entry(entry, level, indent); |
| 626 | break; |
| 627 | } |
| 628 | case PTE_TYPE_TABLE: { |
| 629 | mm_dump_table_entry(entry, level, indent); |
| 630 | break; |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | // NOLINTNEXTLINE(misc-no-recursion) |
| 636 | static void mm_dump_entries(const pte_t *entries, mm_level_t level, |
| 637 | uint32_t indent) |
| 638 | { |
| 639 | dlog("{\n"); |
| 640 | indent += 1; |
| 641 | |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 642 | for (size_t i = 0; i < MM_PTE_PER_PAGE; i++) { |
Karl Meakin | c88ad41 | 2025-02-11 16:04:49 +0000 | [diff] [blame] | 643 | pte_t entry = entries[i]; |
Karl Meakin | 100b0b2 | 2025-02-08 00:59:25 +0000 | [diff] [blame] | 644 | |
| 645 | if (arch_mm_pte_is_absent(entry, level)) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 646 | continue; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 647 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 648 | |
Karl Meakin | c88ad41 | 2025-02-11 16:04:49 +0000 | [diff] [blame] | 649 | dlog_indent(indent, "[level = %u, index = %zu] = ", level, i); |
| 650 | mm_dump_entry(entry, level, indent); |
| 651 | dlog(",\n"); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 652 | } |
Karl Meakin | c88ad41 | 2025-02-11 16:04:49 +0000 | [diff] [blame] | 653 | |
| 654 | indent -= 1; |
| 655 | dlog_indent(indent, "}"); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | /** |
Wedson Almeida Filho | ac8ad01 | 2018-12-17 18:00:29 +0000 | [diff] [blame] | 659 | * Writes the given table to the debug log. |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 660 | */ |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 661 | static void mm_ptable_dump(const struct mm_ptable *ptable, |
| 662 | struct mm_flags flags) |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 663 | { |
Karl Meakin | c88ad41 | 2025-02-11 16:04:49 +0000 | [diff] [blame] | 664 | struct mm_page_table *root_tables = mm_page_table_from_pa(ptable->root); |
| 665 | mm_level_t root_level = mm_max_level(flags) + 1; |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 666 | uint8_t root_table_count = mm_root_table_count(flags); |
Karl Meakin | c88ad41 | 2025-02-11 16:04:49 +0000 | [diff] [blame] | 667 | uint32_t indent = 0; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 668 | |
Karl Meakin | c88ad41 | 2025-02-11 16:04:49 +0000 | [diff] [blame] | 669 | dlog_indent(indent, "mm_ptable {\n"); |
| 670 | indent += 1; |
| 671 | { |
| 672 | dlog_indent(indent, ".stage = %s,\n", |
| 673 | flags.stage1 ? "stage1" : "stage2"); |
| 674 | dlog_indent(indent, ".id = %hu,\n", ptable->id); |
| 675 | dlog_indent(indent, ".root_tables = {\n"); |
| 676 | |
| 677 | indent += 1; |
| 678 | { |
| 679 | for (size_t i = 0; i < root_table_count; ++i) { |
| 680 | dlog_indent( |
| 681 | indent, |
| 682 | "[level = %u, index = %zu].entries = ", |
| 683 | root_level, i); |
| 684 | mm_dump_entries(root_tables[i].entries, |
| 685 | root_level - 1, indent); |
| 686 | dlog(",\n"); |
| 687 | } |
| 688 | } |
| 689 | indent -= 1; |
| 690 | dlog_indent(indent, "},\n"); |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 691 | } |
Karl Meakin | c88ad41 | 2025-02-11 16:04:49 +0000 | [diff] [blame] | 692 | indent -= 1; |
| 693 | dlog_indent(indent, "}\n"); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | /** |
Wedson Almeida Filho | ac8ad01 | 2018-12-17 18:00:29 +0000 | [diff] [blame] | 697 | * Given the table PTE entries all have identical attributes, returns the single |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 698 | * entry with which it can be replaced. |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 699 | */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 700 | static pte_t mm_merge_table_pte(pte_t table_pte, mm_level_t level) |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 701 | { |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 702 | struct mm_page_table *table; |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 703 | mm_attr_t block_attrs; |
| 704 | mm_attr_t table_attrs; |
| 705 | mm_attr_t combined_attrs; |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 706 | paddr_t block_address; |
| 707 | |
Andrew Scull | b6b9b56 | 2018-12-21 14:41:35 +0000 | [diff] [blame] | 708 | table = mm_page_table_from_pa(arch_mm_table_from_pte(table_pte, level)); |
| 709 | |
| 710 | if (!arch_mm_pte_is_present(table->entries[0], level - 1)) { |
Andrew Scull | b6b9b56 | 2018-12-21 14:41:35 +0000 | [diff] [blame] | 711 | return arch_mm_absent_pte(level); |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 712 | } |
| 713 | |
Andrew Scull | b6b9b56 | 2018-12-21 14:41:35 +0000 | [diff] [blame] | 714 | /* Might not be possible to merge the table into a single block. */ |
| 715 | if (!arch_mm_is_block_allowed(level)) { |
| 716 | return table_pte; |
| 717 | } |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 718 | |
Andrew Scull | b6b9b56 | 2018-12-21 14:41:35 +0000 | [diff] [blame] | 719 | /* Replace table with a single block, with equivalent attributes. */ |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 720 | block_attrs = arch_mm_pte_attrs(table->entries[0], level - 1); |
Andrew Scull | b6b9b56 | 2018-12-21 14:41:35 +0000 | [diff] [blame] | 721 | table_attrs = arch_mm_pte_attrs(table_pte, level); |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 722 | combined_attrs = |
| 723 | arch_mm_combine_table_entry_attrs(table_attrs, block_attrs); |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 724 | 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] | 725 | |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 726 | return arch_mm_block_pte(level, block_address, combined_attrs); |
| 727 | } |
| 728 | |
| 729 | /** |
Wedson Almeida Filho | ac8ad01 | 2018-12-17 18:00:29 +0000 | [diff] [blame] | 730 | * Defragments the given PTE by recursively replacing any tables with blocks or |
Andrew Scull | b6b9b56 | 2018-12-21 14:41:35 +0000 | [diff] [blame] | 731 | * absent entries where possible. |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 732 | */ |
Daniel Boulby | 8adf748 | 2021-09-22 15:12:44 +0100 | [diff] [blame] | 733 | // NOLINTNEXTLINE(misc-no-recursion) |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 734 | static void mm_ptable_defrag_entry(struct mm_ptable *ptable, |
| 735 | ptable_addr_t base_addr, pte_t *entry, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 736 | mm_level_t level, struct mm_flags flags, |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 737 | bool non_secure, struct mpool *ppool) |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 738 | { |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 739 | struct mm_page_table *child_table; |
Andrew Scull | 12122ce | 2019-11-19 14:21:07 +0000 | [diff] [blame] | 740 | bool mergeable; |
| 741 | bool base_present; |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 742 | mm_attr_t base_attrs; |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 743 | pte_t new_entry; |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 744 | |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 745 | if (!arch_mm_pte_is_table(*entry, level)) { |
| 746 | return; |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 747 | } |
| 748 | |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 749 | child_table = |
| 750 | mm_page_table_from_pa(arch_mm_table_from_pte(*entry, level)); |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 751 | |
Andrew Scull | 12122ce | 2019-11-19 14:21:07 +0000 | [diff] [blame] | 752 | /* Defrag the first entry in the table and use it as the base entry. */ |
| 753 | static_assert(MM_PTE_PER_PAGE >= 1, "There must be at least one PTE."); |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 754 | |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 755 | mm_ptable_defrag_entry(ptable, base_addr, &(child_table->entries[0]), |
| 756 | level - 1, flags, non_secure, ppool); |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 757 | |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 758 | base_present = |
| 759 | arch_mm_pte_is_present(child_table->entries[0], level - 1); |
| 760 | base_attrs = arch_mm_pte_attrs(child_table->entries[0], level - 1); |
Andrew Scull | 12122ce | 2019-11-19 14:21:07 +0000 | [diff] [blame] | 761 | |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 762 | /* |
Andrew Scull | 12122ce | 2019-11-19 14:21:07 +0000 | [diff] [blame] | 763 | * Defrag the remaining entries in the table and check whether they are |
| 764 | * compatible with the base entry meaning the table can be merged into a |
| 765 | * block entry. It assumes addresses are contiguous due to identity |
| 766 | * mapping. |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 767 | */ |
Andrew Scull | 12122ce | 2019-11-19 14:21:07 +0000 | [diff] [blame] | 768 | mergeable = true; |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 769 | for (size_t i = 1; i < MM_PTE_PER_PAGE; ++i) { |
Andrew Scull | 12122ce | 2019-11-19 14:21:07 +0000 | [diff] [blame] | 770 | bool present; |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 771 | ptable_addr_t block_addr = |
| 772 | base_addr + (i * mm_entry_size(level - 1)); |
Andrew Scull | 12122ce | 2019-11-19 14:21:07 +0000 | [diff] [blame] | 773 | |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 774 | mm_ptable_defrag_entry(ptable, block_addr, |
| 775 | &(child_table->entries[i]), level - 1, |
| 776 | flags, non_secure, ppool); |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 777 | |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 778 | present = arch_mm_pte_is_present(child_table->entries[i], |
| 779 | level - 1); |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 780 | |
Andrew Scull | 12122ce | 2019-11-19 14:21:07 +0000 | [diff] [blame] | 781 | if (present != base_present) { |
| 782 | mergeable = false; |
| 783 | continue; |
| 784 | } |
| 785 | |
| 786 | if (!present) { |
| 787 | continue; |
| 788 | } |
| 789 | |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 790 | if (!arch_mm_pte_is_block(child_table->entries[i], level - 1)) { |
Andrew Scull | 12122ce | 2019-11-19 14:21:07 +0000 | [diff] [blame] | 791 | mergeable = false; |
| 792 | continue; |
| 793 | } |
| 794 | |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 795 | if (arch_mm_pte_attrs(child_table->entries[i], level - 1) != |
Andrew Scull | 12122ce | 2019-11-19 14:21:07 +0000 | [diff] [blame] | 796 | base_attrs) { |
| 797 | mergeable = false; |
| 798 | continue; |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 799 | } |
| 800 | } |
Andrew Scull | b6b9b56 | 2018-12-21 14:41:35 +0000 | [diff] [blame] | 801 | |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 802 | if (!mergeable) { |
| 803 | return; |
Andrew Scull | 12122ce | 2019-11-19 14:21:07 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 806 | new_entry = mm_merge_table_pte(*entry, level); |
| 807 | if (*entry != new_entry) { |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 808 | mm_replace_entry(ptable, base_addr, entry, (uintptr_t)new_entry, |
| 809 | level, flags, non_secure, ppool); |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 810 | } |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | /** |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 814 | * Defragments the given page table by converting page table references to |
| 815 | * blocks whenever possible. |
| 816 | */ |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 817 | static void mm_ptable_defrag(struct mm_ptable *ptable, struct mm_flags flags, |
| 818 | bool non_secure, struct mpool *ppool) |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 819 | { |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 820 | struct mm_page_table *tables = mm_page_table_from_pa(ptable->root); |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 821 | mm_level_t level = mm_max_level(flags); |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 822 | uint8_t root_table_count = mm_root_table_count(flags); |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 823 | ptable_addr_t block_addr = 0; |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 824 | |
| 825 | /* |
| 826 | * Loop through each entry in the table. If it points to another table, |
| 827 | * check if that table can be replaced by a block or an absent entry. |
| 828 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 829 | for (size_t i = 0; i < root_table_count; ++i) { |
| 830 | for (size_t j = 0; j < MM_PTE_PER_PAGE; ++j) { |
Karl Meakin | 00dbf1b | 2025-02-07 17:58:39 +0000 | [diff] [blame^] | 831 | mm_ptable_defrag_entry(ptable, block_addr, |
| 832 | &(tables[i].entries[j]), level, |
| 833 | flags, non_secure, ppool); |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 834 | block_addr = mm_start_of_next_block( |
| 835 | block_addr, mm_entry_size(level)); |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 836 | } |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 837 | } |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 838 | |
| 839 | arch_mm_sync_table_writes(); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | /** |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 843 | * Gets the attributes applied to the given range of stage-2 addresses at the |
| 844 | * given level. |
| 845 | * |
| 846 | * The `got_attrs` argument is initially passed as false until `attrs` contains |
| 847 | * attributes of the memory region at which point it is passed as true. |
| 848 | * |
| 849 | * The value returned in `attrs` is only valid if the function returns true. |
| 850 | * |
| 851 | * 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] | 852 | */ |
Daniel Boulby | 8adf748 | 2021-09-22 15:12:44 +0100 | [diff] [blame] | 853 | // NOLINTNEXTLINE(misc-no-recursion) |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 854 | static bool mm_ptable_get_attrs_level(const struct mm_page_table *table, |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 855 | ptable_addr_t begin, ptable_addr_t end, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 856 | mm_level_t level, bool got_attrs, |
| 857 | mm_attr_t *attrs) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 858 | { |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 859 | const pte_t *pte = &table->entries[mm_index(begin, level)]; |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 860 | ptable_addr_t level_end = mm_level_end(begin, level); |
| 861 | size_t entry_size = mm_entry_size(level); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 862 | |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 863 | /* Cap end so that we don't go over the current level max. */ |
| 864 | if (end > level_end) { |
| 865 | end = level_end; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 866 | } |
| 867 | |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 868 | /* Check that each entry is owned. */ |
| 869 | while (begin < end) { |
| 870 | if (arch_mm_pte_is_table(*pte, level)) { |
| 871 | if (!mm_ptable_get_attrs_level( |
| 872 | mm_page_table_from_pa( |
| 873 | arch_mm_table_from_pte(*pte, |
| 874 | level)), |
| 875 | begin, end, level - 1, got_attrs, attrs)) { |
| 876 | return false; |
| 877 | } |
| 878 | got_attrs = true; |
| 879 | } else { |
| 880 | if (!got_attrs) { |
| 881 | *attrs = arch_mm_pte_attrs(*pte, level); |
| 882 | got_attrs = true; |
| 883 | } else if (arch_mm_pte_attrs(*pte, level) != *attrs) { |
| 884 | return false; |
| 885 | } |
| 886 | } |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 887 | |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 888 | begin = mm_start_of_next_block(begin, entry_size); |
| 889 | pte++; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 890 | } |
| 891 | |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 892 | /* The entry is a valid block. */ |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 893 | return got_attrs; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | /** |
Raghu Krishnamurthy | 2323d72 | 2021-02-12 22:55:38 -0800 | [diff] [blame] | 897 | * Gets the attributes applied to the given range of addresses in the page |
| 898 | * tables. |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 899 | * |
| 900 | * The value returned in `attrs` is only valid if the function returns true. |
| 901 | * |
| 902 | * 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] | 903 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 904 | static bool mm_get_attrs(const struct mm_ptable *ptable, ptable_addr_t begin, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 905 | ptable_addr_t end, mm_attr_t *attrs, |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 906 | struct mm_flags flags) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 907 | { |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 908 | mm_level_t max_level = mm_max_level(flags); |
| 909 | mm_level_t root_level = max_level + 1; |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 910 | size_t root_table_size = mm_entry_size(root_level); |
| 911 | ptable_addr_t ptable_end = |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 912 | mm_root_table_count(flags) * mm_entry_size(root_level); |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 913 | struct mm_page_table *table; |
| 914 | bool got_attrs = false; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 915 | |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 916 | begin = mm_round_down_to_page(begin); |
| 917 | end = mm_round_up_to_page(end); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 918 | |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 919 | /* Fail if the addresses are out of range. */ |
| 920 | if (end > ptable_end) { |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 921 | return false; |
| 922 | } |
| 923 | |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 924 | table = &mm_page_table_from_pa( |
| 925 | ptable->root)[mm_index(begin, root_level)]; |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 926 | while (begin < end) { |
| 927 | if (!mm_ptable_get_attrs_level(table, begin, end, max_level, |
| 928 | got_attrs, attrs)) { |
| 929 | return false; |
| 930 | } |
| 931 | |
| 932 | got_attrs = true; |
| 933 | begin = mm_start_of_next_block(begin, root_table_size); |
| 934 | table++; |
| 935 | } |
| 936 | |
| 937 | return got_attrs; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 938 | } |
| 939 | |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 940 | bool mm_vm_init(struct mm_ptable *ptable, mm_asid_t id, struct mpool *ppool) |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 941 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 942 | return mm_ptable_init(ptable, id, (struct mm_flags){0}, ppool); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 943 | } |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 944 | |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 945 | void mm_vm_fini(const struct mm_ptable *ptable, struct mpool *ppool) |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 946 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 947 | mm_ptable_fini(ptable, (struct mm_flags){0}, ppool); |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | /** |
Andrew Scull | 73b8954 | 2019-11-20 17:31:26 +0000 | [diff] [blame] | 951 | * Selects flags to pass to the page table manipulation operation based on the |
| 952 | * mapping mode. |
| 953 | */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 954 | static struct mm_flags mm_mode_to_flags(mm_mode_t mode) |
Andrew Scull | 73b8954 | 2019-11-20 17:31:26 +0000 | [diff] [blame] | 955 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 956 | struct mm_flags flags = {0}; |
| 957 | |
Andrew Scull | 73b8954 | 2019-11-20 17:31:26 +0000 | [diff] [blame] | 958 | if ((mode & MM_MODE_UNMAPPED_MASK) == MM_MODE_UNMAPPED_MASK) { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 959 | flags.unmap = true; |
Andrew Scull | 73b8954 | 2019-11-20 17:31:26 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 962 | return flags; |
Andrew Scull | 73b8954 | 2019-11-20 17:31:26 +0000 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | /** |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 966 | * See `mm_ptable_identity_prepare`. |
| 967 | * |
Raghu Krishnamurthy | 43fe93a | 2021-01-31 16:38:38 -0800 | [diff] [blame] | 968 | * This must be called before `mm_identity_commit` for the same mapping. |
| 969 | * |
| 970 | * Returns true on success, or false if the update would fail. |
| 971 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 972 | bool mm_identity_prepare(struct mm_ptable *ptable, paddr_t begin, paddr_t end, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 973 | mm_mode_t mode, struct mpool *ppool) |
Raghu Krishnamurthy | 43fe93a | 2021-01-31 16:38:38 -0800 | [diff] [blame] | 974 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 975 | struct mm_flags flags = mm_mode_to_flags(mode); |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 976 | |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 977 | flags.stage1 = true; |
Raghu Krishnamurthy | 43fe93a | 2021-01-31 16:38:38 -0800 | [diff] [blame] | 978 | |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 979 | return mm_ptable_identity_prepare(ptable, begin, end, |
Raghu Krishnamurthy | 43fe93a | 2021-01-31 16:38:38 -0800 | [diff] [blame] | 980 | arch_mm_mode_to_stage1_attrs(mode), |
| 981 | flags, ppool); |
| 982 | } |
| 983 | |
| 984 | /** |
| 985 | * See `mm_ptable_identity_commit`. |
| 986 | * |
| 987 | * `mm_identity_prepare` must be called before this for the same mapping. |
| 988 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 989 | void *mm_identity_commit(struct mm_ptable *ptable, paddr_t begin, paddr_t end, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 990 | mm_mode_t mode, struct mpool *ppool) |
Raghu Krishnamurthy | 43fe93a | 2021-01-31 16:38:38 -0800 | [diff] [blame] | 991 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 992 | struct mm_flags flags = mm_mode_to_flags(mode); |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 993 | |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 994 | flags.stage1 = true; |
Raghu Krishnamurthy | 43fe93a | 2021-01-31 16:38:38 -0800 | [diff] [blame] | 995 | |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 996 | mm_ptable_identity_commit(ptable, begin, end, |
Raghu Krishnamurthy | 43fe93a | 2021-01-31 16:38:38 -0800 | [diff] [blame] | 997 | arch_mm_mode_to_stage1_attrs(mode), flags, |
| 998 | ppool); |
| 999 | return ptr_from_va(va_from_pa(begin)); |
| 1000 | } |
| 1001 | |
| 1002 | /** |
| 1003 | * See `mm_ptable_identity_prepare`. |
| 1004 | * |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 1005 | * This must be called before `mm_vm_identity_commit` for the same mapping. |
Andrew Walbran | 8ec2b9f | 2019-11-25 15:05:40 +0000 | [diff] [blame] | 1006 | * |
| 1007 | * Returns true on success, or false if the update would fail. |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 1008 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 1009 | bool mm_vm_identity_prepare(struct mm_ptable *ptable, paddr_t begin, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 1010 | paddr_t end, mm_mode_t mode, struct mpool *ppool) |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 1011 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 1012 | struct mm_flags flags = mm_mode_to_flags(mode); |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 1013 | |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 1014 | return mm_ptable_identity_prepare(ptable, begin, end, |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 1015 | arch_mm_mode_to_stage2_attrs(mode), |
| 1016 | flags, ppool); |
| 1017 | } |
| 1018 | |
| 1019 | /** |
| 1020 | * See `mm_ptable_identity_commit`. |
| 1021 | * |
| 1022 | * `mm_vm_identity_prepare` must be called before this for the same mapping. |
| 1023 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 1024 | void mm_vm_identity_commit(struct mm_ptable *ptable, paddr_t begin, paddr_t end, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 1025 | mm_mode_t mode, struct mpool *ppool, ipaddr_t *ipa) |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 1026 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 1027 | struct mm_flags flags = mm_mode_to_flags(mode); |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 1028 | |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 1029 | mm_ptable_identity_commit(ptable, begin, end, |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 1030 | arch_mm_mode_to_stage2_attrs(mode), flags, |
| 1031 | ppool); |
| 1032 | |
| 1033 | if (ipa != NULL) { |
| 1034 | *ipa = ipa_from_pa(begin); |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | /** |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 1039 | * Updates a VM's page table such that the given physical address range is |
| 1040 | * mapped in the address space at the corresponding address range in the |
Andrew Scull | fe636b1 | 2018-07-30 14:15:54 +0100 | [diff] [blame] | 1041 | * architecture-agnostic mode provided. |
Andrew Walbran | 8ec2b9f | 2019-11-25 15:05:40 +0000 | [diff] [blame] | 1042 | * |
| 1043 | * mm_vm_defrag should always be called after a series of page table updates, |
| 1044 | * whether they succeed or fail. This is because on failure extra page table |
| 1045 | * entries may have been allocated and then not used, while on success it may be |
| 1046 | * possible to compact the page table by merging several entries into a block. |
| 1047 | * |
| 1048 | * Returns true on success, or false if the update failed and no changes were |
| 1049 | * made. |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1050 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 1051 | bool mm_vm_identity_map(struct mm_ptable *ptable, paddr_t begin, paddr_t end, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 1052 | mm_mode_t mode, struct mpool *ppool, ipaddr_t *ipa) |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1053 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 1054 | struct mm_flags flags = mm_mode_to_flags(mode); |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 1055 | bool success = mm_ptable_identity_update( |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 1056 | ptable, begin, end, arch_mm_mode_to_stage2_attrs(mode), flags, |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 1057 | ppool); |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 1058 | |
| 1059 | if (success && ipa != NULL) { |
| 1060 | *ipa = ipa_from_pa(begin); |
| 1061 | } |
| 1062 | |
| 1063 | return success; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | /** |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 1067 | * Updates the VM's table such that the given physical address range has no |
| 1068 | * connection to the VM. |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 1069 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 1070 | bool mm_vm_unmap(struct mm_ptable *ptable, paddr_t begin, paddr_t end, |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 1071 | struct mpool *ppool) |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 1072 | { |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 1073 | mm_mode_t mode = MM_MODE_UNMAPPED_MASK; |
Andrew Scull | 73b8954 | 2019-11-20 17:31:26 +0000 | [diff] [blame] | 1074 | |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 1075 | return mm_vm_identity_map(ptable, begin, end, mode, ppool, NULL); |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | /** |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 1079 | * Write the given page table of a VM to the debug log. |
| 1080 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 1081 | void mm_vm_dump(const struct mm_ptable *ptable) |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 1082 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 1083 | mm_ptable_dump(ptable, (struct mm_flags){0}); |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | /** |
Raghu Krishnamurthy | 7ad3d14 | 2021-03-28 00:47:35 -0700 | [diff] [blame] | 1087 | * Defragments a stage1 page table. |
| 1088 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 1089 | void mm_stage1_defrag(struct mm_ptable *ptable, struct mpool *ppool) |
Raghu Krishnamurthy | 7ad3d14 | 2021-03-28 00:47:35 -0700 | [diff] [blame] | 1090 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 1091 | mm_ptable_defrag(ptable, (struct mm_flags){.stage1 = true}, false, |
| 1092 | ppool); |
Raghu Krishnamurthy | 7ad3d14 | 2021-03-28 00:47:35 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | /** |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 1096 | * Defragments the VM page table. |
| 1097 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 1098 | void mm_vm_defrag(struct mm_ptable *ptable, struct mpool *ppool, |
| 1099 | bool non_secure) |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 1100 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 1101 | mm_ptable_defrag(ptable, (struct mm_flags){0}, non_secure, ppool); |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | /** |
Fuad Tabba | 9dc276f | 2020-07-16 09:29:32 +0100 | [diff] [blame] | 1105 | * Gets the mode of the given range of intermediate physical addresses if they |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 1106 | * are mapped with the same mode. |
| 1107 | * |
| 1108 | * 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] | 1109 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 1110 | bool mm_vm_get_mode(const struct mm_ptable *ptable, ipaddr_t begin, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 1111 | ipaddr_t end, mm_mode_t *mode) |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 1112 | { |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 1113 | mm_attr_t attrs; |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 1114 | bool ret; |
| 1115 | |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 1116 | ret = mm_get_attrs(ptable, ipa_addr(begin), ipa_addr(end), &attrs, |
| 1117 | (struct mm_flags){0}); |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 1118 | if (ret) { |
| 1119 | *mode = arch_mm_stage2_attrs_to_mode(attrs); |
| 1120 | } |
| 1121 | |
| 1122 | return ret; |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 1123 | } |
| 1124 | |
Raghu Krishnamurthy | 2323d72 | 2021-02-12 22:55:38 -0800 | [diff] [blame] | 1125 | /** |
| 1126 | * Gets the mode of the given range of virtual addresses if they |
| 1127 | * are mapped with the same mode. |
| 1128 | * |
| 1129 | * Returns true if the range is mapped with the same mode and false otherwise. |
| 1130 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 1131 | bool mm_get_mode(const struct mm_ptable *ptable, vaddr_t begin, vaddr_t end, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 1132 | mm_mode_t *mode) |
Raghu Krishnamurthy | 2323d72 | 2021-02-12 22:55:38 -0800 | [diff] [blame] | 1133 | { |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 1134 | mm_attr_t attrs; |
Raghu Krishnamurthy | 2323d72 | 2021-02-12 22:55:38 -0800 | [diff] [blame] | 1135 | bool ret; |
| 1136 | |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 1137 | ret = mm_get_attrs(ptable, va_addr(begin), va_addr(end), &attrs, |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 1138 | (struct mm_flags){.stage1 = true}); |
Raghu Krishnamurthy | 2323d72 | 2021-02-12 22:55:38 -0800 | [diff] [blame] | 1139 | if (ret) { |
| 1140 | *mode = arch_mm_stage1_attrs_to_mode(attrs); |
| 1141 | } |
| 1142 | |
| 1143 | return ret; |
| 1144 | } |
| 1145 | |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1146 | static struct mm_stage1_locked mm_stage1_lock_unsafe(void) |
| 1147 | { |
| 1148 | return (struct mm_stage1_locked){.ptable = &ptable}; |
| 1149 | } |
| 1150 | |
Raghu Krishnamurthy | d3ab8c3 | 2021-02-10 19:11:30 -0800 | [diff] [blame] | 1151 | struct mm_stage1_locked mm_lock_ptable_unsafe(struct mm_ptable *ptable) |
| 1152 | { |
| 1153 | return (struct mm_stage1_locked){.ptable = ptable}; |
| 1154 | } |
| 1155 | |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1156 | struct mm_stage1_locked mm_lock_stage1(void) |
| 1157 | { |
| 1158 | sl_lock(&ptable_lock); |
| 1159 | return mm_stage1_lock_unsafe(); |
| 1160 | } |
| 1161 | |
| 1162 | void mm_unlock_stage1(struct mm_stage1_locked *lock) |
| 1163 | { |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 1164 | CHECK(lock->ptable == &ptable); |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1165 | sl_unlock(&ptable_lock); |
| 1166 | lock->ptable = NULL; |
| 1167 | } |
| 1168 | |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 1169 | /** |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 1170 | * Updates the hypervisor page table such that the given physical address range |
| 1171 | * is mapped into the address space at the corresponding address range in the |
| 1172 | * architecture-agnostic mode provided. |
| 1173 | */ |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1174 | void *mm_identity_map(struct mm_stage1_locked stage1_locked, paddr_t begin, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 1175 | paddr_t end, mm_mode_t mode, struct mpool *ppool) |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 1176 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 1177 | struct mm_flags flags = mm_mode_to_flags(mode); |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 1178 | |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 1179 | flags.stage1 = true; |
Andrew Scull | 73b8954 | 2019-11-20 17:31:26 +0000 | [diff] [blame] | 1180 | |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1181 | if (mm_ptable_identity_update(stage1_locked.ptable, begin, end, |
Andrew Scull | 73b8954 | 2019-11-20 17:31:26 +0000 | [diff] [blame] | 1182 | arch_mm_mode_to_stage1_attrs(mode), flags, |
| 1183 | ppool)) { |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 1184 | return ptr_from_va(va_from_pa(begin)); |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | return NULL; |
| 1188 | } |
| 1189 | |
| 1190 | /** |
| 1191 | * Updates the hypervisor table such that the given physical address range is |
| 1192 | * not mapped in the address space. |
| 1193 | */ |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1194 | bool mm_unmap(struct mm_stage1_locked stage1_locked, paddr_t begin, paddr_t end, |
| 1195 | struct mpool *ppool) |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1196 | { |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 1197 | mm_mode_t mode = MM_MODE_UNMAPPED_MASK; |
Andrew Scull | 73b8954 | 2019-11-20 17:31:26 +0000 | [diff] [blame] | 1198 | |
| 1199 | return mm_identity_map(stage1_locked, begin, end, mode, ppool); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | /** |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1203 | * Defragments the hypervisor page table. |
| 1204 | */ |
| 1205 | void mm_defrag(struct mm_stage1_locked stage1_locked, struct mpool *ppool) |
| 1206 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 1207 | mm_ptable_defrag(stage1_locked.ptable, |
| 1208 | (struct mm_flags){.stage1 = true}, false, ppool); |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | /** |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1212 | * Initialises memory management for the hypervisor itself. |
| 1213 | */ |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 1214 | bool mm_init(struct mpool *ppool) |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1215 | { |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1216 | /* Locking is not enabled yet so fake it, */ |
| 1217 | struct mm_stage1_locked stage1_locked = mm_stage1_lock_unsafe(); |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 1218 | struct mm_flags flags = {.stage1 = true}; |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1219 | |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 1220 | dlog_info("text: %#lx - %#lx\n", pa_addr(layout_text_begin()), |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 1221 | pa_addr(layout_text_end())); |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 1222 | dlog_info("rodata: %#lx - %#lx\n", pa_addr(layout_rodata_begin()), |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 1223 | pa_addr(layout_rodata_end())); |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 1224 | dlog_info("data: %#lx - %#lx\n", pa_addr(layout_data_begin()), |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 1225 | pa_addr(layout_data_end())); |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 1226 | dlog_info("stacks: %#lx - %#lx\n", pa_addr(layout_stacks_begin()), |
Maksims Svecovs | 134b8f9 | 2022-03-04 15:14:09 +0000 | [diff] [blame] | 1227 | pa_addr(layout_stacks_end())); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1228 | |
Raghu Krishnamurthy | 0132b51 | 2021-02-03 14:13:26 -0800 | [diff] [blame] | 1229 | /* ASID 0 is reserved for use by the hypervisor. */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 1230 | if (!mm_ptable_init(&ptable, 0, flags, ppool)) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 1231 | dlog_error("Unable to allocate memory for page table.\n"); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1232 | return false; |
| 1233 | } |
| 1234 | |
Arunachalam Ganapathy | 0f0f706 | 2022-01-26 17:09:53 +0000 | [diff] [blame] | 1235 | /* Initialize arch_mm before calling below mapping routines */ |
| 1236 | if (!arch_mm_init(ptable.root)) { |
| 1237 | return false; |
| 1238 | } |
| 1239 | |
Andrew Walbran | 4869936 | 2019-05-20 14:38:00 +0100 | [diff] [blame] | 1240 | /* Let console driver map pages for itself. */ |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1241 | plat_console_mm_init(stage1_locked, ppool); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1242 | |
| 1243 | /* Map each section. */ |
Raghu Krishnamurthy | 472a882 | 2022-10-04 21:28:59 -0700 | [diff] [blame] | 1244 | CHECK(mm_identity_map(stage1_locked, layout_text_begin(), |
| 1245 | layout_text_end(), MM_MODE_X, ppool) != NULL); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1246 | |
Raghu Krishnamurthy | 472a882 | 2022-10-04 21:28:59 -0700 | [diff] [blame] | 1247 | CHECK(mm_identity_map(stage1_locked, layout_rodata_begin(), |
| 1248 | layout_rodata_end(), MM_MODE_R, ppool) != NULL); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1249 | |
Raghu Krishnamurthy | 472a882 | 2022-10-04 21:28:59 -0700 | [diff] [blame] | 1250 | CHECK(mm_identity_map(stage1_locked, layout_data_begin(), |
| 1251 | layout_data_end(), MM_MODE_R | MM_MODE_W, |
| 1252 | ppool) != NULL); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1253 | |
Maksims Svecovs | 134b8f9 | 2022-03-04 15:14:09 +0000 | [diff] [blame] | 1254 | /* Arch-specific stack mapping. */ |
Raghu Krishnamurthy | 472a882 | 2022-10-04 21:28:59 -0700 | [diff] [blame] | 1255 | CHECK(arch_stack_mm_init(stage1_locked, ppool)); |
Maksims Svecovs | 134b8f9 | 2022-03-04 15:14:09 +0000 | [diff] [blame] | 1256 | |
Arunachalam Ganapathy | 0f0f706 | 2022-01-26 17:09:53 +0000 | [diff] [blame] | 1257 | return true; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1258 | } |