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