[25] | 1 | /* |
---|
[35] | 2 | * hal_gpt.c - implementation of the Generic Page Table API for x86_64 |
---|
[25] | 3 | * |
---|
[35] | 4 | * Copyright (c) 2017 Maxime Villard |
---|
[25] | 5 | * |
---|
| 6 | * This file is part of ALMOS-MKH. |
---|
| 7 | * |
---|
[35] | 8 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
[25] | 9 | * under the terms of the GNU General Public License as published by |
---|
| 10 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 11 | * |
---|
[35] | 12 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
[25] | 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 15 | * General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with ALMOS-MKH.; if not, write to the Free Software Foundation, |
---|
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #include <hal_types.h> |
---|
[35] | 23 | #include <hal_boot.h> /* XXX */ |
---|
[25] | 24 | #include <hal_gpt.h> |
---|
| 25 | #include <hal_special.h> |
---|
[35] | 26 | #include <hal_internal.h> |
---|
| 27 | |
---|
[25] | 28 | #include <printk.h> |
---|
| 29 | #include <bits.h> |
---|
[35] | 30 | #include <string.h> |
---|
[25] | 31 | #include <process.h> |
---|
| 32 | #include <kmem.h> |
---|
| 33 | #include <thread.h> |
---|
| 34 | #include <cluster.h> |
---|
| 35 | #include <ppm.h> |
---|
| 36 | #include <page.h> |
---|
| 37 | |
---|
[44] | 38 | extern vaddr_t __kernel_end; |
---|
| 39 | size_t kimg_size __in_kdata = 0; |
---|
| 40 | |
---|
[35] | 41 | paddr_t pa_avail __in_kdata = 0; |
---|
| 42 | vaddr_t va_avail __in_kdata = 0; |
---|
| 43 | vaddr_t tmpva __in_kdata = (KERNBASE + NKL2_KIMG_ENTRIES * NBPD_L2); |
---|
| 44 | |
---|
| 45 | paddr_t hal_gpt_bootstrap_palloc(size_t npages) |
---|
| 46 | { |
---|
| 47 | paddr_t pa = pa_avail; |
---|
| 48 | pa_avail += npages * PAGE_SIZE; |
---|
| 49 | return pa; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | vaddr_t hal_gpt_bootstrap_valloc(size_t npages) |
---|
| 53 | { |
---|
| 54 | vaddr_t va = va_avail; |
---|
| 55 | va_avail += npages * PAGE_SIZE; |
---|
| 56 | return va; |
---|
| 57 | } |
---|
| 58 | |
---|
[45] | 59 | /* |
---|
| 60 | * Reset the bootstrap VA we've used in cluster0 so far. After this |
---|
| 61 | * function, cluster0's heap is empty. |
---|
| 62 | */ |
---|
| 63 | void hal_gpt_bootstrap_reset() |
---|
| 64 | { |
---|
[148] | 65 | /* |
---|
| 66 | * Re-enter cluster0's space, because we altered it when mapping the ACPI |
---|
| 67 | * tables. |
---|
| 68 | */ |
---|
| 69 | hal_gpt_enter_range(CLUSTER_MIN_VA(0), 0, CLUSTER_PA_SIZE / PAGE_SIZE); |
---|
| 70 | |
---|
[116] | 71 | va_avail = CLUSTER_MIN_VA(0) + KERNEL_VA_SIZE; |
---|
[45] | 72 | } |
---|
| 73 | |
---|
[116] | 74 | /* |
---|
| 75 | * Uniformize the PA and VA offsets, and return the value. After this function, |
---|
| 76 | * we are guaranteed to have [VA = PA + constant_offset]. And therefore we can |
---|
| 77 | * only call hal_gpt_bootstrap_valloc, without entering it in a PA. |
---|
| 78 | */ |
---|
| 79 | size_t hal_gpt_bootstrap_uniformize() |
---|
| 80 | { |
---|
| 81 | size_t pa_offset = pa_avail - 0; |
---|
| 82 | size_t va_offset = va_avail - CLUSTER_MIN_VA(0); |
---|
| 83 | |
---|
| 84 | if (pa_offset < va_offset) |
---|
| 85 | pa_avail += (va_offset - pa_offset); |
---|
| 86 | else if (pa_offset > va_offset) |
---|
| 87 | va_avail += (pa_offset - va_offset); |
---|
| 88 | |
---|
| 89 | return MAX(pa_offset, va_offset); |
---|
| 90 | } |
---|
| 91 | |
---|
[83] | 92 | void hal_gpt_enter(vaddr_t va, paddr_t pa, pt_entry_t flags) |
---|
[35] | 93 | { |
---|
[45] | 94 | XASSERT(va % PAGE_SIZE == 0); |
---|
| 95 | XASSERT(pa % PAGE_SIZE == 0); |
---|
[116] | 96 | //XASSERT(va == tmpva || PTE_BASE[pl1_i(va)] == 0); |
---|
[83] | 97 | PTE_BASE[pl1_i(va)] = (pa & PG_FRAME) | flags; |
---|
[79] | 98 | invlpg(va); |
---|
[35] | 99 | } |
---|
| 100 | |
---|
[39] | 101 | void hal_gpt_enter_range(vaddr_t va, paddr_t pa, size_t n) |
---|
| 102 | { |
---|
[83] | 103 | pt_entry_t flags = PG_V | PG_KW | PG_NX; |
---|
[39] | 104 | size_t i; |
---|
| 105 | for (i = 0; i < n; i++) { |
---|
[83] | 106 | hal_gpt_enter(va + i * PAGE_SIZE, pa + i * PAGE_SIZE, flags); |
---|
[39] | 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
[45] | 110 | void hal_gpt_leave(vaddr_t va) |
---|
| 111 | { |
---|
| 112 | XASSERT(va % PAGE_SIZE == 0); |
---|
| 113 | XASSERT(PTE_BASE[pl1_i(va)] != 0); |
---|
| 114 | PTE_BASE[pl1_i(va)] = 0; |
---|
[79] | 115 | invlpg(va); |
---|
[45] | 116 | } |
---|
| 117 | |
---|
| 118 | void hal_gpt_leave_range(vaddr_t va, size_t n) |
---|
| 119 | { |
---|
| 120 | size_t i; |
---|
| 121 | for (i = 0; i < n; i++) { |
---|
| 122 | hal_gpt_leave(va + i * PAGE_SIZE); |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | |
---|
[35] | 126 | /* |
---|
| 127 | * Create a page tree that can map va_start->va_end. The caller can then |
---|
| 128 | * enter these addresses to physical locations. |
---|
| 129 | * |
---|
[148] | 130 | * This function is a bit complicated, and may need to be revisited. |
---|
[35] | 131 | */ |
---|
| 132 | void hal_gpt_maptree_area(vaddr_t va_start, vaddr_t va_end) |
---|
| 133 | { |
---|
[112] | 134 | pt_entry_t flags = PG_V | PG_KW | PG_NX; |
---|
[35] | 135 | size_t L4start, L4end, nL4e; |
---|
| 136 | size_t L3start, L3end, nL3e; |
---|
| 137 | size_t L2start, L2end, nL2e; |
---|
| 138 | paddr_t L3page, L2page, L1page; |
---|
| 139 | paddr_t pa; |
---|
| 140 | size_t i, npa; |
---|
| 141 | pt_entry_t *pde; |
---|
| 142 | |
---|
| 143 | /* Allocate L3 */ |
---|
| 144 | L4start = pl4_i(va_start); |
---|
| 145 | L4end = pl4_i(va_end); |
---|
| 146 | nL4e = (L4end - L4start + 1); |
---|
| 147 | L3page = hal_gpt_bootstrap_palloc(nL4e); |
---|
| 148 | |
---|
| 149 | /* Allocate L2 */ |
---|
| 150 | L3start = pl3_i(va_start); |
---|
| 151 | L3end = pl3_i(va_end); |
---|
| 152 | nL3e = (L3end - L3start + 1); |
---|
| 153 | L2page = hal_gpt_bootstrap_palloc(nL3e); |
---|
| 154 | |
---|
| 155 | /* Allocate L1 */ |
---|
| 156 | L2start = pl2_i(va_start); |
---|
| 157 | L2end = pl2_i(va_end); |
---|
| 158 | nL2e = (L2end - L2start + 1); |
---|
| 159 | L1page = hal_gpt_bootstrap_palloc(nL2e); |
---|
| 160 | |
---|
| 161 | /* Zero out L1 */ |
---|
| 162 | for (i = 0; i < nL2e; i++) { |
---|
| 163 | pa = L1page + i * PAGE_SIZE; |
---|
[83] | 164 | hal_gpt_enter(tmpva, pa, flags); |
---|
[35] | 165 | |
---|
| 166 | memset((void *)tmpva, 0, PAGE_SIZE); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | /* Zero out L2 */ |
---|
| 170 | for (i = 0; i < nL3e; i++) { |
---|
| 171 | pa = L2page + i * PAGE_SIZE; |
---|
[83] | 172 | hal_gpt_enter(tmpva, pa, flags); |
---|
[35] | 173 | |
---|
| 174 | memset((void *)tmpva, 0, PAGE_SIZE); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | /* Zero out L3 */ |
---|
| 178 | for (i = 0; i < nL4e; i++) { |
---|
| 179 | pa = L3page + i * PAGE_SIZE; |
---|
[83] | 180 | hal_gpt_enter(tmpva, pa, flags); |
---|
[35] | 181 | |
---|
| 182 | memset((void *)tmpva, 0, PAGE_SIZE); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | /* Create L2, linked to L1 */ |
---|
| 186 | npa = (L2start / NPDPG) * PAGE_SIZE; |
---|
| 187 | for (i = L2start; i <= L2end; i++) { |
---|
| 188 | pa = (paddr_t)&(((pt_entry_t *)L2page)[i]); |
---|
| 189 | pa -= npa; /* shift on the left */ |
---|
| 190 | pa &= PG_FRAME; /* rounddown to a page boundary */ |
---|
[83] | 191 | hal_gpt_enter(tmpva, pa, flags); |
---|
[35] | 192 | |
---|
| 193 | pde = (pt_entry_t *)tmpva; |
---|
| 194 | pa = L1page + (i - L2start) * PAGE_SIZE; |
---|
| 195 | pde[i % NPDPG] = (pa & PG_FRAME) | PG_V | PG_KW; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | /* Create L3, linked to L2 */ |
---|
| 199 | npa = (L3start / NPDPG) * PAGE_SIZE; |
---|
| 200 | for (i = L3start; i <= L3end; i++) { |
---|
| 201 | pa = (paddr_t)&(((pt_entry_t *)L3page)[i]); |
---|
| 202 | pa -= npa; /* shift on the left */ |
---|
| 203 | pa &= PG_FRAME; /* rounddown to a page boundary */ |
---|
[83] | 204 | hal_gpt_enter(tmpva, pa, flags); |
---|
[35] | 205 | |
---|
| 206 | pde = (pt_entry_t *)tmpva; |
---|
| 207 | pa = L2page + (i - L3start) * PAGE_SIZE; |
---|
| 208 | pde[i % NPDPG] = (pa & PG_FRAME) | PG_V | PG_KW; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | /* Link L3 into L4 */ |
---|
| 212 | for (i = 0; i < nL4e; i++) { |
---|
| 213 | pa = L3page + i * PAGE_SIZE; |
---|
| 214 | L4_BASE[L4start + i] = (pa & PG_FRAME) | PG_V | PG_KW; |
---|
| 215 | } |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | void hal_gpt_init(paddr_t firstpa) |
---|
| 219 | { |
---|
[44] | 220 | /* Initialize global values */ |
---|
[35] | 221 | pa_avail = firstpa; |
---|
[47] | 222 | va_avail = CLUSTER_MIN_VA(0) + KERNEL_VA_SIZE; |
---|
[44] | 223 | kimg_size = ((uint64_t)&__kernel_end - KERNBASE); |
---|
| 224 | XASSERT(kimg_size % PAGE_SIZE == 0); |
---|
| 225 | |
---|
[134] | 226 | /* Create cluster0's page tree */ |
---|
[148] | 227 | hal_gpt_maptree_area(CLUSTER_MIN_VA(0), CLUSTER_MIN_VA(0) + CLUSTER_PA_SIZE); |
---|
[44] | 228 | |
---|
[148] | 229 | /* Enter cluster0's space */ |
---|
| 230 | hal_gpt_enter_range(CLUSTER_MIN_VA(0), 0, CLUSTER_PA_SIZE / PAGE_SIZE); |
---|
[112] | 231 | |
---|
[123] | 232 | /* Unmap the area below the kernel */ |
---|
| 233 | hal_gpt_leave_range(CLUSTER_MIN_VA(0), (KERNTEXTOFF - KERNBASE) / PAGE_SIZE); |
---|
[35] | 234 | } |
---|
| 235 | |
---|
| 236 | /* -------------------------------------------------------------------------- */ |
---|
| 237 | |
---|
[25] | 238 | /**************************************************************************************** |
---|
| 239 | * These global variables defines the masks for the Generic Page Table Entry attributes, |
---|
| 240 | * and must be defined in all GPT implementation. |
---|
| 241 | ***************************************************************************************/ |
---|
| 242 | |
---|
| 243 | uint32_t GPT_MAPPED; |
---|
| 244 | uint32_t GPT_SMALL; |
---|
| 245 | uint32_t GPT_READABLE; |
---|
[145] | 246 | uint32_t GPT_WRITABLE; |
---|
[25] | 247 | uint32_t GPT_EXECUTABLE; |
---|
[145] | 248 | uint32_t GPT_CACHABLE; |
---|
| 249 | uint32_t GPT_USER; |
---|
[25] | 250 | uint32_t GPT_DIRTY; |
---|
| 251 | uint32_t GPT_ACCESSED; |
---|
| 252 | uint32_t GPT_GLOBAL; |
---|
| 253 | uint32_t GPT_COW; |
---|
| 254 | uint32_t GPT_SWAP; |
---|
| 255 | uint32_t GPT_LOCKED; |
---|
| 256 | |
---|
| 257 | error_t hal_gpt_create( gpt_t * gpt ) |
---|
| 258 | { |
---|
[48] | 259 | x86_panic((char *)__func__); |
---|
[25] | 260 | return 0; |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | void hal_gpt_destroy( gpt_t * gpt ) |
---|
| 264 | { |
---|
[48] | 265 | x86_panic((char *)__func__); |
---|
[25] | 266 | } |
---|
| 267 | |
---|
| 268 | void hal_gpt_print( gpt_t * gpt ) |
---|
| 269 | { |
---|
[48] | 270 | x86_panic((char *)__func__); |
---|
[25] | 271 | } |
---|
| 272 | |
---|
| 273 | error_t hal_gpt_set_pte( gpt_t * gpt, |
---|
| 274 | vpn_t vpn, |
---|
| 275 | ppn_t ppn, |
---|
| 276 | uint32_t attr ) |
---|
| 277 | { |
---|
[48] | 278 | x86_panic((char *)__func__); |
---|
[25] | 279 | return 0; |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | void hal_gpt_get_pte( gpt_t * gpt, |
---|
| 283 | vpn_t vpn, |
---|
| 284 | uint32_t * attr, |
---|
| 285 | ppn_t * ppn ) |
---|
| 286 | { |
---|
[48] | 287 | x86_panic((char *)__func__); |
---|
[25] | 288 | } |
---|
| 289 | |
---|
| 290 | void hal_gpt_reset_pte( gpt_t * gpt, |
---|
| 291 | vpn_t vpn ) |
---|
| 292 | { |
---|
[48] | 293 | x86_panic((char *)__func__); |
---|
[25] | 294 | } |
---|
| 295 | |
---|
| 296 | error_t hal_gpt_lock_pte( gpt_t * gpt, |
---|
| 297 | vpn_t vpn ) |
---|
| 298 | { |
---|
[48] | 299 | x86_panic((char *)__func__); |
---|
[25] | 300 | return 0; |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | error_t hal_gpt_unlock_pte( gpt_t * gpt, |
---|
| 304 | vpn_t vpn ) |
---|
| 305 | { |
---|
[48] | 306 | x86_panic((char *)__func__); |
---|
[25] | 307 | return 0; |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | error_t hal_gpt_copy( gpt_t * dst_gpt, |
---|
| 311 | gpt_t * src_gpt, |
---|
| 312 | bool_t cow ) |
---|
| 313 | { |
---|
[48] | 314 | x86_panic((char *)__func__); |
---|
[25] | 315 | return 0; |
---|
| 316 | } |
---|
| 317 | |
---|