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 | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 164 | static void mm_invalidate_tlb(ptable_addr_t begin, ptable_addr_t end, |
| 165 | struct mm_flags flags, bool non_secure, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 166 | mm_asid_t id) |
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) { |
Raghu Krishnamurthy | 8fdd6df | 2021-02-03 18:30:59 -0800 | [diff] [blame] | 169 | arch_mm_invalidate_stage1_range(id, va_init(begin), |
| 170 | va_init(end)); |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 171 | } else { |
Raghu Krishnamurthy | 8fdd6df | 2021-02-03 18:30:59 -0800 | [diff] [blame] | 172 | arch_mm_invalidate_stage2_range(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 | */ |
| 267 | static void mm_replace_entry(ptable_addr_t begin, pte_t *pte, pte_t new_pte, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 268 | mm_level_t level, struct mm_flags flags, |
| 269 | bool non_secure, struct mpool *ppool, mm_asid_t id) |
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 | */ |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 277 | if ((flags.stage1 || mm_stage2_invalidate) && |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 278 | arch_mm_pte_is_valid(v, level)) { |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 279 | *pte = arch_mm_absent_pte(level); |
Raghu Krishnamurthy | 8fdd6df | 2021-02-03 18:30:59 -0800 | [diff] [blame] | 280 | mm_invalidate_tlb(begin, begin + mm_entry_size(level), flags, |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 281 | non_secure, id); |
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 | */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 297 | static struct mm_page_table *mm_populate_table_pte(ptable_addr_t begin, |
| 298 | pte_t *pte, mm_level_t level, |
| 299 | struct mm_flags flags, |
| 300 | bool non_secure, |
| 301 | struct mpool *ppool, |
| 302 | mm_asid_t id) |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 303 | { |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 304 | struct mm_page_table *ntable; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 305 | pte_t v = *pte; |
| 306 | pte_t new_pte; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 307 | size_t inc; |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 308 | mm_level_t level_below = level - 1; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 309 | |
| 310 | /* Just return pointer to table if it's already populated. */ |
Andrew Scull | 78d6fd9 | 2018-09-06 15:08:36 +0100 | [diff] [blame] | 311 | if (arch_mm_pte_is_table(v, level)) { |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 312 | 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] | 313 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 314 | |
| 315 | /* Allocate a new table. */ |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 316 | ntable = mm_alloc_page_tables(1, ppool); |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 317 | if (ntable == NULL) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 318 | dlog_error("Failed to allocate memory for page table\n"); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 319 | return NULL; |
| 320 | } |
| 321 | |
| 322 | /* Determine template for new pte and its increment. */ |
Andrew Scull | 78d6fd9 | 2018-09-06 15:08:36 +0100 | [diff] [blame] | 323 | if (arch_mm_pte_is_block(v, level)) { |
Andrew Scull | 78d6fd9 | 2018-09-06 15:08:36 +0100 | [diff] [blame] | 324 | inc = mm_entry_size(level_below); |
| 325 | new_pte = arch_mm_block_pte(level_below, |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 326 | arch_mm_block_from_pte(v, level), |
| 327 | arch_mm_pte_attrs(v, level)); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 328 | } else { |
Andrew Scull | 78d6fd9 | 2018-09-06 15:08:36 +0100 | [diff] [blame] | 329 | inc = 0; |
Andrew Walbran | 1b99f9d | 2018-10-03 17:54:40 +0100 | [diff] [blame] | 330 | new_pte = arch_mm_absent_pte(level_below); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /* Initialise entries in the new table. */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 334 | for (size_t i = 0; i < MM_PTE_PER_PAGE; i++) { |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 335 | ntable->entries[i] = new_pte; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 336 | new_pte += inc; |
| 337 | } |
| 338 | |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 339 | /* Ensure initialisation is visible before updating the pte. */ |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 340 | atomic_thread_fence(memory_order_release); |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 341 | |
| 342 | /* Replace the pte entry, doing a break-before-make if needed. */ |
| 343 | mm_replace_entry(begin, pte, |
| 344 | arch_mm_table_pte(level, pa_init((uintpaddr_t)ntable)), |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 345 | level, flags, non_secure, ppool, id); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 346 | |
| 347 | return ntable; |
| 348 | } |
| 349 | |
| 350 | /** |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 351 | * 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] | 352 | * physical range using the provided (architecture-specific) attributes. Or if |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 353 | * `flags.unmap` is set, unmap the given range instead. |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 354 | * |
| 355 | * This function calls itself recursively if it needs to update additional |
| 356 | * levels, but the recursion is bound by the maximum number of levels in a page |
| 357 | * table. |
| 358 | */ |
Daniel Boulby | 8adf748 | 2021-09-22 15:12:44 +0100 | [diff] [blame] | 359 | // NOLINTNEXTLINE(misc-no-recursion) |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 360 | static bool mm_map_level(ptable_addr_t begin, ptable_addr_t end, paddr_t pa, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 361 | mm_attr_t attrs, struct mm_page_table *table, |
| 362 | mm_level_t level, struct mm_flags flags, |
| 363 | struct mpool *ppool, mm_asid_t id) |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 364 | { |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 365 | pte_t *pte = &table->entries[mm_index(begin, level)]; |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 366 | ptable_addr_t level_end = mm_level_end(begin, level); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 367 | size_t entry_size = mm_entry_size(level); |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 368 | bool commit = flags.commit; |
| 369 | bool unmap = flags.unmap; |
| 370 | bool non_secure = ((attrs & (1ULL << 57)) != 0); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 371 | |
Andrew Scull | 265ada9 | 2018-07-30 15:19:01 +0100 | [diff] [blame] | 372 | /* Cap end so that we don't go over the current level max. */ |
| 373 | if (end > level_end) { |
| 374 | end = level_end; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 375 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 376 | |
| 377 | /* Fill each entry in the table. */ |
Andrew Scull | 265ada9 | 2018-07-30 15:19:01 +0100 | [diff] [blame] | 378 | while (begin < end) { |
Andrew Walbran | 6324fc9 | 2018-10-03 11:46:43 +0100 | [diff] [blame] | 379 | if (unmap ? !arch_mm_pte_is_present(*pte, level) |
| 380 | : arch_mm_pte_is_block(*pte, level) && |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 381 | arch_mm_pte_attrs(*pte, level) == attrs) { |
Andrew Walbran | 6324fc9 | 2018-10-03 11:46:43 +0100 | [diff] [blame] | 382 | /* |
| 383 | * If the entry is already mapped with the right |
| 384 | * attributes, or already absent in the case of |
| 385 | * unmapping, no need to do anything; carry on to the |
| 386 | * next entry. |
| 387 | */ |
| 388 | } else if ((end - begin) >= entry_size && |
| 389 | (unmap || arch_mm_is_block_allowed(level)) && |
| 390 | (begin & (entry_size - 1)) == 0) { |
| 391 | /* |
| 392 | * If the entire entry is within the region we want to |
| 393 | * map, map/unmap the whole entry. |
| 394 | */ |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 395 | if (commit) { |
Wedson Almeida Filho | 7c91323 | 2018-11-23 18:20:29 +0000 | [diff] [blame] | 396 | pte_t new_pte = |
| 397 | unmap ? arch_mm_absent_pte(level) |
| 398 | : arch_mm_block_pte(level, pa, |
| 399 | attrs); |
| 400 | mm_replace_entry(begin, pte, new_pte, level, |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 401 | flags, non_secure, ppool, id); |
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 = |
| 409 | mm_populate_table_pte(begin, pte, level, flags, |
| 410 | non_secure, ppool, id); |
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 | */ |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 419 | if (!mm_map_level(begin, end, pa, attrs, nt, level - 1, |
Raghu Krishnamurthy | 8fdd6df | 2021-02-03 18:30:59 -0800 | [diff] [blame] | 420 | flags, ppool, id)) { |
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 | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 444 | struct mm_page_table *table = &mm_page_table_from_pa( |
| 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) { |
| 448 | if (!mm_map_level(begin, end, pa_init(begin), attrs, table, |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 449 | root_level - 1, flags, ppool, ptable->id)) { |
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); |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 453 | table++; |
| 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 | |
| 567 | /** |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 568 | * Writes the given table to the debug log, calling itself recursively to |
| 569 | * write sub-tables. |
| 570 | */ |
Daniel Boulby | 8adf748 | 2021-09-22 15:12:44 +0100 | [diff] [blame] | 571 | // NOLINTNEXTLINE(misc-no-recursion) |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 572 | static void mm_dump_table_recursive(const struct mm_page_table *ptable, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 573 | mm_level_t level, mm_level_t max_level) |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 574 | { |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 575 | for (size_t i = 0; i < MM_PTE_PER_PAGE; i++) { |
| 576 | if (!arch_mm_pte_is_present(ptable->entries[i], level)) { |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 577 | continue; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 578 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 579 | |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 580 | dlog("%*s%lx: %lx\n", 4 * (max_level - level), "", i, |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 581 | ptable->entries[i]); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 582 | |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 583 | if (arch_mm_pte_is_table(ptable->entries[i], level)) { |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 584 | mm_dump_table_recursive( |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 585 | mm_page_table_from_pa(arch_mm_table_from_pte( |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 586 | ptable->entries[i], level)), |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 587 | level - 1, max_level); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | /** |
Wedson Almeida Filho | ac8ad01 | 2018-12-17 18:00:29 +0000 | [diff] [blame] | 593 | * Writes the given table to the debug log. |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 594 | */ |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 595 | static void mm_ptable_dump(const struct mm_ptable *ptable, |
| 596 | struct mm_flags flags) |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 597 | { |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 598 | struct mm_page_table *tables = mm_page_table_from_pa(ptable->root); |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 599 | mm_level_t max_level = mm_max_level(flags); |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 600 | uint8_t root_table_count = mm_root_table_count(flags); |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 601 | |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 602 | for (size_t i = 0; i < root_table_count; ++i) { |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 603 | mm_dump_table_recursive(&tables[i], max_level, max_level); |
| 604 | } |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | /** |
Wedson Almeida Filho | ac8ad01 | 2018-12-17 18:00:29 +0000 | [diff] [blame] | 608 | * Given the table PTE entries all have identical attributes, returns the single |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 609 | * entry with which it can be replaced. |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 610 | */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 611 | 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] | 612 | { |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 613 | struct mm_page_table *table; |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 614 | mm_attr_t block_attrs; |
| 615 | mm_attr_t table_attrs; |
| 616 | mm_attr_t combined_attrs; |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 617 | paddr_t block_address; |
| 618 | |
Andrew Scull | b6b9b56 | 2018-12-21 14:41:35 +0000 | [diff] [blame] | 619 | table = mm_page_table_from_pa(arch_mm_table_from_pte(table_pte, level)); |
| 620 | |
| 621 | if (!arch_mm_pte_is_present(table->entries[0], level - 1)) { |
Andrew Scull | b6b9b56 | 2018-12-21 14:41:35 +0000 | [diff] [blame] | 622 | return arch_mm_absent_pte(level); |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 623 | } |
| 624 | |
Andrew Scull | b6b9b56 | 2018-12-21 14:41:35 +0000 | [diff] [blame] | 625 | /* Might not be possible to merge the table into a single block. */ |
| 626 | if (!arch_mm_is_block_allowed(level)) { |
| 627 | return table_pte; |
| 628 | } |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 629 | |
Andrew Scull | b6b9b56 | 2018-12-21 14:41:35 +0000 | [diff] [blame] | 630 | /* Replace table with a single block, with equivalent attributes. */ |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 631 | block_attrs = arch_mm_pte_attrs(table->entries[0], level - 1); |
Andrew Scull | b6b9b56 | 2018-12-21 14:41:35 +0000 | [diff] [blame] | 632 | table_attrs = arch_mm_pte_attrs(table_pte, level); |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 633 | combined_attrs = |
| 634 | arch_mm_combine_table_entry_attrs(table_attrs, block_attrs); |
Andrew Scull | 3681b8d | 2018-12-12 14:22:59 +0000 | [diff] [blame] | 635 | 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] | 636 | |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 637 | return arch_mm_block_pte(level, block_address, combined_attrs); |
| 638 | } |
| 639 | |
| 640 | /** |
Wedson Almeida Filho | ac8ad01 | 2018-12-17 18:00:29 +0000 | [diff] [blame] | 641 | * Defragments the given PTE by recursively replacing any tables with blocks or |
Andrew Scull | b6b9b56 | 2018-12-21 14:41:35 +0000 | [diff] [blame] | 642 | * absent entries where possible. |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 643 | */ |
Daniel Boulby | 8adf748 | 2021-09-22 15:12:44 +0100 | [diff] [blame] | 644 | // NOLINTNEXTLINE(misc-no-recursion) |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 645 | static void mm_ptable_defrag_entry(ptable_addr_t base_addr, pte_t *entry, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 646 | mm_level_t level, struct mm_flags flags, |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 647 | bool non_secure, struct mpool *ppool, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 648 | mm_asid_t id) |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 649 | { |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 650 | struct mm_page_table *table; |
Andrew Scull | 12122ce | 2019-11-19 14:21:07 +0000 | [diff] [blame] | 651 | bool mergeable; |
| 652 | bool base_present; |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 653 | mm_attr_t base_attrs; |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 654 | pte_t new_entry; |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 655 | |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 656 | if (!arch_mm_pte_is_table(*entry, level)) { |
| 657 | return; |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 658 | } |
| 659 | |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 660 | 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] | 661 | |
Andrew Scull | 12122ce | 2019-11-19 14:21:07 +0000 | [diff] [blame] | 662 | /* Defrag the first entry in the table and use it as the base entry. */ |
| 663 | 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] | 664 | |
| 665 | mm_ptable_defrag_entry(base_addr, &(table->entries[0]), level - 1, |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 666 | flags, non_secure, ppool, id); |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 667 | |
Andrew Scull | 12122ce | 2019-11-19 14:21:07 +0000 | [diff] [blame] | 668 | base_present = arch_mm_pte_is_present(table->entries[0], level - 1); |
| 669 | base_attrs = arch_mm_pte_attrs(table->entries[0], level - 1); |
| 670 | |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 671 | /* |
Andrew Scull | 12122ce | 2019-11-19 14:21:07 +0000 | [diff] [blame] | 672 | * Defrag the remaining entries in the table and check whether they are |
| 673 | * compatible with the base entry meaning the table can be merged into a |
| 674 | * block entry. It assumes addresses are contiguous due to identity |
| 675 | * mapping. |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 676 | */ |
Andrew Scull | 12122ce | 2019-11-19 14:21:07 +0000 | [diff] [blame] | 677 | mergeable = true; |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 678 | for (size_t i = 1; i < MM_PTE_PER_PAGE; ++i) { |
Andrew Scull | 12122ce | 2019-11-19 14:21:07 +0000 | [diff] [blame] | 679 | bool present; |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 680 | ptable_addr_t block_addr = |
| 681 | base_addr + (i * mm_entry_size(level - 1)); |
Andrew Scull | 12122ce | 2019-11-19 14:21:07 +0000 | [diff] [blame] | 682 | |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 683 | mm_ptable_defrag_entry(block_addr, &(table->entries[i]), |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 684 | level - 1, flags, non_secure, ppool, id); |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 685 | |
Andrew Scull | 12122ce | 2019-11-19 14:21:07 +0000 | [diff] [blame] | 686 | present = arch_mm_pte_is_present(table->entries[i], level - 1); |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 687 | |
Andrew Scull | 12122ce | 2019-11-19 14:21:07 +0000 | [diff] [blame] | 688 | if (present != base_present) { |
| 689 | mergeable = false; |
| 690 | continue; |
| 691 | } |
| 692 | |
| 693 | if (!present) { |
| 694 | continue; |
| 695 | } |
| 696 | |
| 697 | if (!arch_mm_pte_is_block(table->entries[i], level - 1)) { |
| 698 | mergeable = false; |
| 699 | continue; |
| 700 | } |
| 701 | |
| 702 | if (arch_mm_pte_attrs(table->entries[i], level - 1) != |
| 703 | base_attrs) { |
| 704 | mergeable = false; |
| 705 | continue; |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 706 | } |
| 707 | } |
Andrew Scull | b6b9b56 | 2018-12-21 14:41:35 +0000 | [diff] [blame] | 708 | |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 709 | if (!mergeable) { |
| 710 | return; |
Andrew Scull | 12122ce | 2019-11-19 14:21:07 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 713 | new_entry = mm_merge_table_pte(*entry, level); |
| 714 | if (*entry != new_entry) { |
Olivier Deprez | 6f40037 | 2022-03-07 09:31:08 +0100 | [diff] [blame] | 715 | mm_replace_entry(base_addr, entry, (uintptr_t)new_entry, level, |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 716 | flags, non_secure, ppool, id); |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 717 | } |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | /** |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 721 | * Defragments the given page table by converting page table references to |
| 722 | * blocks whenever possible. |
| 723 | */ |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 724 | static void mm_ptable_defrag(struct mm_ptable *ptable, struct mm_flags flags, |
| 725 | bool non_secure, struct mpool *ppool) |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 726 | { |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 727 | struct mm_page_table *tables = mm_page_table_from_pa(ptable->root); |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 728 | mm_level_t level = mm_max_level(flags); |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 729 | uint8_t root_table_count = mm_root_table_count(flags); |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 730 | ptable_addr_t block_addr = 0; |
Andrew Walbran | 2400ed2 | 2018-09-27 14:45:58 +0100 | [diff] [blame] | 731 | |
| 732 | /* |
| 733 | * Loop through each entry in the table. If it points to another table, |
| 734 | * check if that table can be replaced by a block or an absent entry. |
| 735 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 736 | for (size_t i = 0; i < root_table_count; ++i) { |
| 737 | for (size_t j = 0; j < MM_PTE_PER_PAGE; ++j) { |
| 738 | mm_ptable_defrag_entry( |
| 739 | block_addr, &(tables[i].entries[j]), level, |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 740 | flags, non_secure, ppool, ptable->id); |
Raghu Krishnamurthy | c1012d6 | 2021-01-24 19:19:31 -0800 | [diff] [blame] | 741 | block_addr = mm_start_of_next_block( |
| 742 | block_addr, mm_entry_size(level)); |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 743 | } |
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 | |
| 746 | arch_mm_sync_table_writes(); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | /** |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 750 | * Gets the attributes applied to the given range of stage-2 addresses at the |
| 751 | * given level. |
| 752 | * |
| 753 | * The `got_attrs` argument is initially passed as false until `attrs` contains |
| 754 | * attributes of the memory region at which point it is passed as true. |
| 755 | * |
| 756 | * The value returned in `attrs` is only valid if the function returns true. |
| 757 | * |
| 758 | * 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] | 759 | */ |
Daniel Boulby | 8adf748 | 2021-09-22 15:12:44 +0100 | [diff] [blame] | 760 | // NOLINTNEXTLINE(misc-no-recursion) |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 761 | 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] | 762 | ptable_addr_t begin, ptable_addr_t end, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 763 | mm_level_t level, bool got_attrs, |
| 764 | mm_attr_t *attrs) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 765 | { |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 766 | const pte_t *pte = &table->entries[mm_index(begin, level)]; |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 767 | ptable_addr_t level_end = mm_level_end(begin, level); |
| 768 | size_t entry_size = mm_entry_size(level); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 769 | |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 770 | /* Cap end so that we don't go over the current level max. */ |
| 771 | if (end > level_end) { |
| 772 | end = level_end; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 773 | } |
| 774 | |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 775 | /* Check that each entry is owned. */ |
| 776 | while (begin < end) { |
| 777 | if (arch_mm_pte_is_table(*pte, level)) { |
| 778 | if (!mm_ptable_get_attrs_level( |
| 779 | mm_page_table_from_pa( |
| 780 | arch_mm_table_from_pte(*pte, |
| 781 | level)), |
| 782 | begin, end, level - 1, got_attrs, attrs)) { |
| 783 | return false; |
| 784 | } |
| 785 | got_attrs = true; |
| 786 | } else { |
| 787 | if (!got_attrs) { |
| 788 | *attrs = arch_mm_pte_attrs(*pte, level); |
| 789 | got_attrs = true; |
| 790 | } else if (arch_mm_pte_attrs(*pte, level) != *attrs) { |
| 791 | return false; |
| 792 | } |
| 793 | } |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 794 | |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 795 | begin = mm_start_of_next_block(begin, entry_size); |
| 796 | pte++; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 797 | } |
| 798 | |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 799 | /* The entry is a valid block. */ |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 800 | return got_attrs; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | /** |
Raghu Krishnamurthy | 2323d72 | 2021-02-12 22:55:38 -0800 | [diff] [blame] | 804 | * Gets the attributes applied to the given range of addresses in the page |
| 805 | * tables. |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 806 | * |
| 807 | * The value returned in `attrs` is only valid if the function returns true. |
| 808 | * |
| 809 | * 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] | 810 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 811 | 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] | 812 | ptable_addr_t end, mm_attr_t *attrs, |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 813 | struct mm_flags flags) |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 814 | { |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 815 | mm_level_t max_level = mm_max_level(flags); |
| 816 | mm_level_t root_level = max_level + 1; |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 817 | size_t root_table_size = mm_entry_size(root_level); |
| 818 | ptable_addr_t ptable_end = |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 819 | mm_root_table_count(flags) * mm_entry_size(root_level); |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 820 | struct mm_page_table *table; |
| 821 | bool got_attrs = false; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 822 | |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 823 | begin = mm_round_down_to_page(begin); |
| 824 | end = mm_round_up_to_page(end); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 825 | |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 826 | /* Fail if the addresses are out of range. */ |
| 827 | if (end > ptable_end) { |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 828 | return false; |
| 829 | } |
| 830 | |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 831 | table = &mm_page_table_from_pa( |
| 832 | ptable->root)[mm_index(begin, root_level)]; |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 833 | while (begin < end) { |
| 834 | if (!mm_ptable_get_attrs_level(table, begin, end, max_level, |
| 835 | got_attrs, attrs)) { |
| 836 | return false; |
| 837 | } |
| 838 | |
| 839 | got_attrs = true; |
| 840 | begin = mm_start_of_next_block(begin, root_table_size); |
| 841 | table++; |
| 842 | } |
| 843 | |
| 844 | return got_attrs; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 845 | } |
| 846 | |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 847 | 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] | 848 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 849 | return mm_ptable_init(ptable, id, (struct mm_flags){0}, ppool); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 850 | } |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 851 | |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 852 | void mm_vm_fini(const struct mm_ptable *ptable, struct mpool *ppool) |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 853 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 854 | mm_ptable_fini(ptable, (struct mm_flags){0}, ppool); |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | /** |
Andrew Scull | 73b8954 | 2019-11-20 17:31:26 +0000 | [diff] [blame] | 858 | * Selects flags to pass to the page table manipulation operation based on the |
| 859 | * mapping mode. |
| 860 | */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 861 | static struct mm_flags mm_mode_to_flags(mm_mode_t mode) |
Andrew Scull | 73b8954 | 2019-11-20 17:31:26 +0000 | [diff] [blame] | 862 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 863 | struct mm_flags flags = {0}; |
| 864 | |
Andrew Scull | 73b8954 | 2019-11-20 17:31:26 +0000 | [diff] [blame] | 865 | if ((mode & MM_MODE_UNMAPPED_MASK) == MM_MODE_UNMAPPED_MASK) { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 866 | flags.unmap = true; |
Andrew Scull | 73b8954 | 2019-11-20 17:31:26 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 869 | return flags; |
Andrew Scull | 73b8954 | 2019-11-20 17:31:26 +0000 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | /** |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 873 | * See `mm_ptable_identity_prepare`. |
| 874 | * |
Raghu Krishnamurthy | 43fe93a | 2021-01-31 16:38:38 -0800 | [diff] [blame] | 875 | * This must be called before `mm_identity_commit` for the same mapping. |
| 876 | * |
| 877 | * Returns true on success, or false if the update would fail. |
| 878 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 879 | 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] | 880 | mm_mode_t mode, struct mpool *ppool) |
Raghu Krishnamurthy | 43fe93a | 2021-01-31 16:38:38 -0800 | [diff] [blame] | 881 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 882 | struct mm_flags flags = mm_mode_to_flags(mode); |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 883 | |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 884 | flags.stage1 = true; |
Raghu Krishnamurthy | 43fe93a | 2021-01-31 16:38:38 -0800 | [diff] [blame] | 885 | |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 886 | return mm_ptable_identity_prepare(ptable, begin, end, |
Raghu Krishnamurthy | 43fe93a | 2021-01-31 16:38:38 -0800 | [diff] [blame] | 887 | arch_mm_mode_to_stage1_attrs(mode), |
| 888 | flags, ppool); |
| 889 | } |
| 890 | |
| 891 | /** |
| 892 | * See `mm_ptable_identity_commit`. |
| 893 | * |
| 894 | * `mm_identity_prepare` must be called before this for the same mapping. |
| 895 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 896 | 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] | 897 | mm_mode_t mode, struct mpool *ppool) |
Raghu Krishnamurthy | 43fe93a | 2021-01-31 16:38:38 -0800 | [diff] [blame] | 898 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 899 | struct mm_flags flags = mm_mode_to_flags(mode); |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 900 | |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 901 | flags.stage1 = true; |
Raghu Krishnamurthy | 43fe93a | 2021-01-31 16:38:38 -0800 | [diff] [blame] | 902 | |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 903 | mm_ptable_identity_commit(ptable, begin, end, |
Raghu Krishnamurthy | 43fe93a | 2021-01-31 16:38:38 -0800 | [diff] [blame] | 904 | arch_mm_mode_to_stage1_attrs(mode), flags, |
| 905 | ppool); |
| 906 | return ptr_from_va(va_from_pa(begin)); |
| 907 | } |
| 908 | |
| 909 | /** |
| 910 | * See `mm_ptable_identity_prepare`. |
| 911 | * |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 912 | * 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] | 913 | * |
| 914 | * Returns true on success, or false if the update would fail. |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 915 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 916 | bool mm_vm_identity_prepare(struct mm_ptable *ptable, paddr_t begin, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 917 | paddr_t end, mm_mode_t mode, struct mpool *ppool) |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 918 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 919 | struct mm_flags flags = mm_mode_to_flags(mode); |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 920 | |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 921 | return mm_ptable_identity_prepare(ptable, begin, end, |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 922 | arch_mm_mode_to_stage2_attrs(mode), |
| 923 | flags, ppool); |
| 924 | } |
| 925 | |
| 926 | /** |
| 927 | * See `mm_ptable_identity_commit`. |
| 928 | * |
| 929 | * `mm_vm_identity_prepare` must be called before this for the same mapping. |
| 930 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 931 | 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] | 932 | mm_mode_t mode, struct mpool *ppool, ipaddr_t *ipa) |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 933 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 934 | struct mm_flags flags = mm_mode_to_flags(mode); |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 935 | |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 936 | mm_ptable_identity_commit(ptable, begin, end, |
Andrew Scull | 4e83cef | 2019-11-19 14:17:54 +0000 | [diff] [blame] | 937 | arch_mm_mode_to_stage2_attrs(mode), flags, |
| 938 | ppool); |
| 939 | |
| 940 | if (ipa != NULL) { |
| 941 | *ipa = ipa_from_pa(begin); |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | /** |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 946 | * Updates a VM's page table such that the given physical address range is |
| 947 | * mapped in the address space at the corresponding address range in the |
Andrew Scull | fe636b1 | 2018-07-30 14:15:54 +0100 | [diff] [blame] | 948 | * architecture-agnostic mode provided. |
Andrew Walbran | 8ec2b9f | 2019-11-25 15:05:40 +0000 | [diff] [blame] | 949 | * |
| 950 | * mm_vm_defrag should always be called after a series of page table updates, |
| 951 | * whether they succeed or fail. This is because on failure extra page table |
| 952 | * entries may have been allocated and then not used, while on success it may be |
| 953 | * possible to compact the page table by merging several entries into a block. |
| 954 | * |
| 955 | * Returns true on success, or false if the update failed and no changes were |
| 956 | * made. |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 957 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 958 | 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] | 959 | mm_mode_t mode, struct mpool *ppool, ipaddr_t *ipa) |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 960 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 961 | struct mm_flags flags = mm_mode_to_flags(mode); |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 962 | bool success = mm_ptable_identity_update( |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 963 | ptable, begin, end, arch_mm_mode_to_stage2_attrs(mode), flags, |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 964 | ppool); |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 965 | |
| 966 | if (success && ipa != NULL) { |
| 967 | *ipa = ipa_from_pa(begin); |
| 968 | } |
| 969 | |
| 970 | return success; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | /** |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 974 | * Updates the VM's table such that the given physical address range has no |
| 975 | * connection to the VM. |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 976 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 977 | 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] | 978 | struct mpool *ppool) |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 979 | { |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 980 | mm_mode_t mode = MM_MODE_UNMAPPED_MASK; |
Andrew Scull | 73b8954 | 2019-11-20 17:31:26 +0000 | [diff] [blame] | 981 | |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 982 | return mm_vm_identity_map(ptable, begin, end, mode, ppool, NULL); |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | /** |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 986 | * Write the given page table of a VM to the debug log. |
| 987 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 988 | void mm_vm_dump(const struct mm_ptable *ptable) |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 989 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 990 | mm_ptable_dump(ptable, (struct mm_flags){0}); |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | /** |
Raghu Krishnamurthy | 7ad3d14 | 2021-03-28 00:47:35 -0700 | [diff] [blame] | 994 | * Defragments a stage1 page table. |
| 995 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 996 | void mm_stage1_defrag(struct mm_ptable *ptable, struct mpool *ppool) |
Raghu Krishnamurthy | 7ad3d14 | 2021-03-28 00:47:35 -0700 | [diff] [blame] | 997 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 998 | mm_ptable_defrag(ptable, (struct mm_flags){.stage1 = true}, false, |
| 999 | ppool); |
Raghu Krishnamurthy | 7ad3d14 | 2021-03-28 00:47:35 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | /** |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 1003 | * Defragments the VM page table. |
| 1004 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 1005 | void mm_vm_defrag(struct mm_ptable *ptable, struct mpool *ppool, |
| 1006 | bool non_secure) |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 1007 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 1008 | mm_ptable_defrag(ptable, (struct mm_flags){0}, non_secure, ppool); |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | /** |
Fuad Tabba | 9dc276f | 2020-07-16 09:29:32 +0100 | [diff] [blame] | 1012 | * 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] | 1013 | * are mapped with the same mode. |
| 1014 | * |
| 1015 | * 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] | 1016 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 1017 | 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] | 1018 | ipaddr_t end, mm_mode_t *mode) |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 1019 | { |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 1020 | mm_attr_t attrs; |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 1021 | bool ret; |
| 1022 | |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 1023 | ret = mm_get_attrs(ptable, ipa_addr(begin), ipa_addr(end), &attrs, |
| 1024 | (struct mm_flags){0}); |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 1025 | if (ret) { |
| 1026 | *mode = arch_mm_stage2_attrs_to_mode(attrs); |
| 1027 | } |
| 1028 | |
| 1029 | return ret; |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 1030 | } |
| 1031 | |
Raghu Krishnamurthy | 2323d72 | 2021-02-12 22:55:38 -0800 | [diff] [blame] | 1032 | /** |
| 1033 | * Gets the mode of the given range of virtual addresses if they |
| 1034 | * are mapped with the same mode. |
| 1035 | * |
| 1036 | * Returns true if the range is mapped with the same mode and false otherwise. |
| 1037 | */ |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 1038 | 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] | 1039 | mm_mode_t *mode) |
Raghu Krishnamurthy | 2323d72 | 2021-02-12 22:55:38 -0800 | [diff] [blame] | 1040 | { |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 1041 | mm_attr_t attrs; |
Raghu Krishnamurthy | 2323d72 | 2021-02-12 22:55:38 -0800 | [diff] [blame] | 1042 | bool ret; |
| 1043 | |
Karl Meakin | d64aaf8 | 2025-02-08 01:12:55 +0000 | [diff] [blame] | 1044 | ret = mm_get_attrs(ptable, va_addr(begin), va_addr(end), &attrs, |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 1045 | (struct mm_flags){.stage1 = true}); |
Raghu Krishnamurthy | 2323d72 | 2021-02-12 22:55:38 -0800 | [diff] [blame] | 1046 | if (ret) { |
| 1047 | *mode = arch_mm_stage1_attrs_to_mode(attrs); |
| 1048 | } |
| 1049 | |
| 1050 | return ret; |
| 1051 | } |
| 1052 | |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1053 | static struct mm_stage1_locked mm_stage1_lock_unsafe(void) |
| 1054 | { |
| 1055 | return (struct mm_stage1_locked){.ptable = &ptable}; |
| 1056 | } |
| 1057 | |
Raghu Krishnamurthy | d3ab8c3 | 2021-02-10 19:11:30 -0800 | [diff] [blame] | 1058 | struct mm_stage1_locked mm_lock_ptable_unsafe(struct mm_ptable *ptable) |
| 1059 | { |
| 1060 | return (struct mm_stage1_locked){.ptable = ptable}; |
| 1061 | } |
| 1062 | |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1063 | struct mm_stage1_locked mm_lock_stage1(void) |
| 1064 | { |
| 1065 | sl_lock(&ptable_lock); |
| 1066 | return mm_stage1_lock_unsafe(); |
| 1067 | } |
| 1068 | |
| 1069 | void mm_unlock_stage1(struct mm_stage1_locked *lock) |
| 1070 | { |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 1071 | CHECK(lock->ptable == &ptable); |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1072 | sl_unlock(&ptable_lock); |
| 1073 | lock->ptable = NULL; |
| 1074 | } |
| 1075 | |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 1076 | /** |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 1077 | * Updates the hypervisor page table such that the given physical address range |
| 1078 | * is mapped into the address space at the corresponding address range in the |
| 1079 | * architecture-agnostic mode provided. |
| 1080 | */ |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1081 | 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] | 1082 | paddr_t end, mm_mode_t mode, struct mpool *ppool) |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 1083 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 1084 | struct mm_flags flags = mm_mode_to_flags(mode); |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 1085 | |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 1086 | flags.stage1 = true; |
Andrew Scull | 73b8954 | 2019-11-20 17:31:26 +0000 | [diff] [blame] | 1087 | |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1088 | if (mm_ptable_identity_update(stage1_locked.ptable, begin, end, |
Andrew Scull | 73b8954 | 2019-11-20 17:31:26 +0000 | [diff] [blame] | 1089 | arch_mm_mode_to_stage1_attrs(mode), flags, |
| 1090 | ppool)) { |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 1091 | return ptr_from_va(va_from_pa(begin)); |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | return NULL; |
| 1095 | } |
| 1096 | |
| 1097 | /** |
| 1098 | * Updates the hypervisor table such that the given physical address range is |
| 1099 | * not mapped in the address space. |
| 1100 | */ |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1101 | bool mm_unmap(struct mm_stage1_locked stage1_locked, paddr_t begin, paddr_t end, |
| 1102 | struct mpool *ppool) |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1103 | { |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 1104 | mm_mode_t mode = MM_MODE_UNMAPPED_MASK; |
Andrew Scull | 73b8954 | 2019-11-20 17:31:26 +0000 | [diff] [blame] | 1105 | |
| 1106 | return mm_identity_map(stage1_locked, begin, end, mode, ppool); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | /** |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1110 | * Defragments the hypervisor page table. |
| 1111 | */ |
| 1112 | void mm_defrag(struct mm_stage1_locked stage1_locked, struct mpool *ppool) |
| 1113 | { |
Karl Meakin | 1fd4b82 | 2025-02-01 17:13:47 +0000 | [diff] [blame] | 1114 | mm_ptable_defrag(stage1_locked.ptable, |
| 1115 | (struct mm_flags){.stage1 = true}, false, ppool); |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | /** |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1119 | * Initialises memory management for the hypervisor itself. |
| 1120 | */ |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 1121 | bool mm_init(struct mpool *ppool) |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1122 | { |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1123 | /* Locking is not enabled yet so fake it, */ |
| 1124 | struct mm_stage1_locked stage1_locked = mm_stage1_lock_unsafe(); |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 1125 | struct mm_flags flags = {.stage1 = true}; |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1126 | |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 1127 | dlog_info("text: %#lx - %#lx\n", pa_addr(layout_text_begin()), |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 1128 | pa_addr(layout_text_end())); |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 1129 | dlog_info("rodata: %#lx - %#lx\n", pa_addr(layout_rodata_begin()), |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 1130 | pa_addr(layout_rodata_end())); |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 1131 | dlog_info("data: %#lx - %#lx\n", pa_addr(layout_data_begin()), |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 1132 | pa_addr(layout_data_end())); |
Karl Meakin | e8937d9 | 2024-03-19 16:04:25 +0000 | [diff] [blame] | 1133 | dlog_info("stacks: %#lx - %#lx\n", pa_addr(layout_stacks_begin()), |
Maksims Svecovs | 134b8f9 | 2022-03-04 15:14:09 +0000 | [diff] [blame] | 1134 | pa_addr(layout_stacks_end())); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1135 | |
Raghu Krishnamurthy | 0132b51 | 2021-02-03 14:13:26 -0800 | [diff] [blame] | 1136 | /* ASID 0 is reserved for use by the hypervisor. */ |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 1137 | if (!mm_ptable_init(&ptable, 0, flags, ppool)) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 1138 | dlog_error("Unable to allocate memory for page table.\n"); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1139 | return false; |
| 1140 | } |
| 1141 | |
Arunachalam Ganapathy | 0f0f706 | 2022-01-26 17:09:53 +0000 | [diff] [blame] | 1142 | /* Initialize arch_mm before calling below mapping routines */ |
| 1143 | if (!arch_mm_init(ptable.root)) { |
| 1144 | return false; |
| 1145 | } |
| 1146 | |
Andrew Walbran | 4869936 | 2019-05-20 14:38:00 +0100 | [diff] [blame] | 1147 | /* Let console driver map pages for itself. */ |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 1148 | plat_console_mm_init(stage1_locked, ppool); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1149 | |
| 1150 | /* Map each section. */ |
Raghu Krishnamurthy | 472a882 | 2022-10-04 21:28:59 -0700 | [diff] [blame] | 1151 | CHECK(mm_identity_map(stage1_locked, layout_text_begin(), |
| 1152 | layout_text_end(), MM_MODE_X, ppool) != NULL); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1153 | |
Raghu Krishnamurthy | 472a882 | 2022-10-04 21:28:59 -0700 | [diff] [blame] | 1154 | CHECK(mm_identity_map(stage1_locked, layout_rodata_begin(), |
| 1155 | layout_rodata_end(), MM_MODE_R, ppool) != NULL); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1156 | |
Raghu Krishnamurthy | 472a882 | 2022-10-04 21:28:59 -0700 | [diff] [blame] | 1157 | CHECK(mm_identity_map(stage1_locked, layout_data_begin(), |
| 1158 | layout_data_end(), MM_MODE_R | MM_MODE_W, |
| 1159 | ppool) != NULL); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1160 | |
Maksims Svecovs | 134b8f9 | 2022-03-04 15:14:09 +0000 | [diff] [blame] | 1161 | /* Arch-specific stack mapping. */ |
Raghu Krishnamurthy | 472a882 | 2022-10-04 21:28:59 -0700 | [diff] [blame] | 1162 | CHECK(arch_stack_mm_init(stage1_locked, ppool)); |
Maksims Svecovs | 134b8f9 | 2022-03-04 15:14:09 +0000 | [diff] [blame] | 1163 | |
Arunachalam Ganapathy | 0f0f706 | 2022-01-26 17:09:53 +0000 | [diff] [blame] | 1164 | return true; |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 1165 | } |