[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 |
---|
[234] | 18 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
[25] | 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); |
---|
[319] | 70 | hal_gpt_leave_range(CLUSTER_MIN_VA(0), (KERNTEXTOFF - KERNBASE) / PAGE_SIZE); |
---|
[148] | 71 | |
---|
[116] | 72 | va_avail = CLUSTER_MIN_VA(0) + KERNEL_VA_SIZE; |
---|
[45] | 73 | } |
---|
| 74 | |
---|
[116] | 75 | /* |
---|
| 76 | * Uniformize the PA and VA offsets, and return the value. After this function, |
---|
| 77 | * we are guaranteed to have [VA = PA + constant_offset]. And therefore we can |
---|
| 78 | * only call hal_gpt_bootstrap_valloc, without entering it in a PA. |
---|
| 79 | */ |
---|
| 80 | size_t hal_gpt_bootstrap_uniformize() |
---|
| 81 | { |
---|
| 82 | size_t pa_offset = pa_avail - 0; |
---|
| 83 | size_t va_offset = va_avail - CLUSTER_MIN_VA(0); |
---|
| 84 | |
---|
| 85 | if (pa_offset < va_offset) |
---|
| 86 | pa_avail += (va_offset - pa_offset); |
---|
| 87 | else if (pa_offset > va_offset) |
---|
| 88 | va_avail += (pa_offset - va_offset); |
---|
| 89 | |
---|
| 90 | return MAX(pa_offset, va_offset); |
---|
| 91 | } |
---|
| 92 | |
---|
[83] | 93 | void hal_gpt_enter(vaddr_t va, paddr_t pa, pt_entry_t flags) |
---|
[35] | 94 | { |
---|
[45] | 95 | XASSERT(va % PAGE_SIZE == 0); |
---|
| 96 | XASSERT(pa % PAGE_SIZE == 0); |
---|
[116] | 97 | //XASSERT(va == tmpva || PTE_BASE[pl1_i(va)] == 0); |
---|
[83] | 98 | PTE_BASE[pl1_i(va)] = (pa & PG_FRAME) | flags; |
---|
[79] | 99 | invlpg(va); |
---|
[35] | 100 | } |
---|
| 101 | |
---|
[39] | 102 | void hal_gpt_enter_range(vaddr_t va, paddr_t pa, size_t n) |
---|
| 103 | { |
---|
[83] | 104 | pt_entry_t flags = PG_V | PG_KW | PG_NX; |
---|
[39] | 105 | size_t i; |
---|
| 106 | for (i = 0; i < n; i++) { |
---|
[83] | 107 | hal_gpt_enter(va + i * PAGE_SIZE, pa + i * PAGE_SIZE, flags); |
---|
[39] | 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
[45] | 111 | void hal_gpt_leave(vaddr_t va) |
---|
| 112 | { |
---|
| 113 | XASSERT(va % PAGE_SIZE == 0); |
---|
| 114 | XASSERT(PTE_BASE[pl1_i(va)] != 0); |
---|
| 115 | PTE_BASE[pl1_i(va)] = 0; |
---|
[79] | 116 | invlpg(va); |
---|
[45] | 117 | } |
---|
| 118 | |
---|
| 119 | void hal_gpt_leave_range(vaddr_t va, size_t n) |
---|
| 120 | { |
---|
| 121 | size_t i; |
---|
| 122 | for (i = 0; i < n; i++) { |
---|
| 123 | hal_gpt_leave(va + i * PAGE_SIZE); |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
[35] | 127 | /* |
---|
| 128 | * Create a page tree that can map va_start->va_end. The caller can then |
---|
| 129 | * enter these addresses to physical locations. |
---|
| 130 | * |
---|
[148] | 131 | * This function is a bit complicated, and may need to be revisited. |
---|
[35] | 132 | */ |
---|
| 133 | void hal_gpt_maptree_area(vaddr_t va_start, vaddr_t va_end) |
---|
| 134 | { |
---|
[112] | 135 | pt_entry_t flags = PG_V | PG_KW | PG_NX; |
---|
[35] | 136 | size_t L4start, L4end, nL4e; |
---|
| 137 | size_t L3start, L3end, nL3e; |
---|
| 138 | size_t L2start, L2end, nL2e; |
---|
| 139 | paddr_t L3page, L2page, L1page; |
---|
| 140 | paddr_t pa; |
---|
| 141 | size_t i, npa; |
---|
| 142 | pt_entry_t *pde; |
---|
| 143 | |
---|
| 144 | /* Allocate L3 */ |
---|
| 145 | L4start = pl4_i(va_start); |
---|
| 146 | L4end = pl4_i(va_end); |
---|
| 147 | nL4e = (L4end - L4start + 1); |
---|
| 148 | L3page = hal_gpt_bootstrap_palloc(nL4e); |
---|
| 149 | |
---|
| 150 | /* Allocate L2 */ |
---|
| 151 | L3start = pl3_i(va_start); |
---|
| 152 | L3end = pl3_i(va_end); |
---|
| 153 | nL3e = (L3end - L3start + 1); |
---|
| 154 | L2page = hal_gpt_bootstrap_palloc(nL3e); |
---|
| 155 | |
---|
| 156 | /* Allocate L1 */ |
---|
| 157 | L2start = pl2_i(va_start); |
---|
| 158 | L2end = pl2_i(va_end); |
---|
| 159 | nL2e = (L2end - L2start + 1); |
---|
| 160 | L1page = hal_gpt_bootstrap_palloc(nL2e); |
---|
| 161 | |
---|
| 162 | /* Zero out L1 */ |
---|
| 163 | for (i = 0; i < nL2e; i++) { |
---|
| 164 | pa = L1page + i * PAGE_SIZE; |
---|
[83] | 165 | hal_gpt_enter(tmpva, pa, flags); |
---|
[35] | 166 | |
---|
| 167 | memset((void *)tmpva, 0, PAGE_SIZE); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | /* Zero out L2 */ |
---|
| 171 | for (i = 0; i < nL3e; i++) { |
---|
| 172 | pa = L2page + i * PAGE_SIZE; |
---|
[83] | 173 | hal_gpt_enter(tmpva, pa, flags); |
---|
[35] | 174 | |
---|
| 175 | memset((void *)tmpva, 0, PAGE_SIZE); |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | /* Zero out L3 */ |
---|
| 179 | for (i = 0; i < nL4e; i++) { |
---|
| 180 | pa = L3page + i * PAGE_SIZE; |
---|
[83] | 181 | hal_gpt_enter(tmpva, pa, flags); |
---|
[35] | 182 | |
---|
| 183 | memset((void *)tmpva, 0, PAGE_SIZE); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | /* Create L2, linked to L1 */ |
---|
| 187 | npa = (L2start / NPDPG) * PAGE_SIZE; |
---|
| 188 | for (i = L2start; i <= L2end; i++) { |
---|
| 189 | pa = (paddr_t)&(((pt_entry_t *)L2page)[i]); |
---|
| 190 | pa -= npa; /* shift on the left */ |
---|
| 191 | pa &= PG_FRAME; /* rounddown to a page boundary */ |
---|
[83] | 192 | hal_gpt_enter(tmpva, pa, flags); |
---|
[35] | 193 | |
---|
| 194 | pde = (pt_entry_t *)tmpva; |
---|
| 195 | pa = L1page + (i - L2start) * PAGE_SIZE; |
---|
| 196 | pde[i % NPDPG] = (pa & PG_FRAME) | PG_V | PG_KW; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | /* Create L3, linked to L2 */ |
---|
| 200 | npa = (L3start / NPDPG) * PAGE_SIZE; |
---|
| 201 | for (i = L3start; i <= L3end; i++) { |
---|
| 202 | pa = (paddr_t)&(((pt_entry_t *)L3page)[i]); |
---|
| 203 | pa -= npa; /* shift on the left */ |
---|
| 204 | pa &= PG_FRAME; /* rounddown to a page boundary */ |
---|
[83] | 205 | hal_gpt_enter(tmpva, pa, flags); |
---|
[35] | 206 | |
---|
| 207 | pde = (pt_entry_t *)tmpva; |
---|
| 208 | pa = L2page + (i - L3start) * PAGE_SIZE; |
---|
| 209 | pde[i % NPDPG] = (pa & PG_FRAME) | PG_V | PG_KW; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | /* Link L3 into L4 */ |
---|
| 213 | for (i = 0; i < nL4e; i++) { |
---|
| 214 | pa = L3page + i * PAGE_SIZE; |
---|
| 215 | L4_BASE[L4start + i] = (pa & PG_FRAME) | PG_V | PG_KW; |
---|
| 216 | } |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | void hal_gpt_init(paddr_t firstpa) |
---|
| 220 | { |
---|
[44] | 221 | /* Initialize global values */ |
---|
[35] | 222 | pa_avail = firstpa; |
---|
[47] | 223 | va_avail = CLUSTER_MIN_VA(0) + KERNEL_VA_SIZE; |
---|
[44] | 224 | kimg_size = ((uint64_t)&__kernel_end - KERNBASE); |
---|
| 225 | XASSERT(kimg_size % PAGE_SIZE == 0); |
---|
| 226 | |
---|
[224] | 227 | /* |
---|
| 228 | * Create cluster0's page tree, enter the space, and unmap the area |
---|
| 229 | * below the kernel. |
---|
| 230 | */ |
---|
[148] | 231 | hal_gpt_maptree_area(CLUSTER_MIN_VA(0), CLUSTER_MIN_VA(0) + CLUSTER_PA_SIZE); |
---|
| 232 | hal_gpt_enter_range(CLUSTER_MIN_VA(0), 0, CLUSTER_PA_SIZE / PAGE_SIZE); |
---|
[224] | 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 | |
---|
[225] | 243 | uint32_t GPT_MAPPED; |
---|
| 244 | uint32_t GPT_SMALL; |
---|
| 245 | uint32_t GPT_READABLE; |
---|
| 246 | uint32_t GPT_WRITABLE; |
---|
| 247 | uint32_t GPT_EXECUTABLE; |
---|
| 248 | uint32_t GPT_CACHABLE; |
---|
| 249 | uint32_t GPT_USER; |
---|
| 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; |
---|
[25] | 256 | |
---|
[225] | 257 | error_t hal_gpt_create(gpt_t *gpt) |
---|
[25] | 258 | { |
---|
[225] | 259 | page_t *page; |
---|
[316] | 260 | xptr_t page_xp; |
---|
[225] | 261 | |
---|
| 262 | /* check page size */ |
---|
| 263 | if (CONFIG_PPM_PAGE_SIZE != 4096) { |
---|
| 264 | printk("\n[PANIC] in %s : For x86, the page must be 4 Kbytes\n", __FUNCTION__); |
---|
| 265 | hal_core_sleep(); |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | /* allocate a physical page for L4 */ |
---|
| 269 | kmem_req_t req; |
---|
| 270 | req.type = KMEM_PAGE; |
---|
| 271 | req.size = 1; |
---|
| 272 | req.flags = AF_KERNEL | AF_ZERO; |
---|
| 273 | page = (page_t *)kmem_alloc(&req); |
---|
| 274 | |
---|
| 275 | if (page == NULL) { |
---|
| 276 | printk("\n[ERROR] in %s : cannot allocate physical memory for PT1\n", __FUNCTION__); |
---|
| 277 | return ENOMEM; |
---|
| 278 | } |
---|
| 279 | |
---|
[316] | 280 | /* |
---|
| 281 | * XXX XXX XXX: can kmem_alloc allocate the page in a remote cluster?? |
---|
| 282 | */ |
---|
| 283 | page_xp = XPTR(local_cxy, page); |
---|
| 284 | |
---|
[225] | 285 | /* populate the kernel entries */ |
---|
| 286 | pt_entry_t *L4src, *L4dst; |
---|
| 287 | extern paddr_t L4paddr; // XXX XXX smp |
---|
| 288 | vaddr_t L4vaddr = L4paddr + KERNBASE; // XXX |
---|
| 289 | L4src = (pt_entry_t *)L4vaddr; |
---|
[316] | 290 | L4dst = (pt_entry_t *)ppm_page2base(page_xp); |
---|
[225] | 291 | memcpy(&L4dst[256], &L4src[256], 256 * sizeof(pt_entry_t)); |
---|
[316] | 292 | L4dst[L4_SLOT_PTE] = (ppm_page2ppn(page_xp) >> CONFIG_PPM_PAGE_SHIFT) | |
---|
[225] | 293 | PG_V | PG_KW | PG_NX; |
---|
| 294 | |
---|
| 295 | /* initialize generic page table descriptor */ |
---|
[316] | 296 | gpt->ptr = GET_PTR(ppm_page2base(page_xp)); |
---|
| 297 | gpt->ppn = ppm_page2ppn(page_xp); |
---|
| 298 | gpt->page = GET_PTR(page_xp); |
---|
[225] | 299 | |
---|
| 300 | /* initialize PTE entries attributes masks */ |
---|
| 301 | GPT_MAPPED = PG_V; |
---|
| 302 | GPT_SMALL = 0; |
---|
| 303 | GPT_READABLE = PG_V; |
---|
| 304 | GPT_WRITABLE = PG_RW; |
---|
| 305 | GPT_EXECUTABLE = 0; |
---|
| 306 | GPT_CACHABLE = 0; |
---|
| 307 | GPT_USER = PG_u; |
---|
| 308 | GPT_DIRTY = 0; |
---|
| 309 | GPT_ACCESSED = 0; |
---|
| 310 | GPT_GLOBAL = PG_G; |
---|
| 311 | GPT_COW = 0; |
---|
| 312 | GPT_SWAP = 0; |
---|
| 313 | GPT_LOCKED = 0; |
---|
| 314 | |
---|
[25] | 315 | return 0; |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | void hal_gpt_destroy( gpt_t * gpt ) |
---|
| 319 | { |
---|
[48] | 320 | x86_panic((char *)__func__); |
---|
[25] | 321 | } |
---|
| 322 | |
---|
| 323 | void hal_gpt_print( gpt_t * gpt ) |
---|
| 324 | { |
---|
[48] | 325 | x86_panic((char *)__func__); |
---|
[25] | 326 | } |
---|
| 327 | |
---|
| 328 | error_t hal_gpt_set_pte( gpt_t * gpt, |
---|
| 329 | vpn_t vpn, |
---|
| 330 | ppn_t ppn, |
---|
| 331 | uint32_t attr ) |
---|
| 332 | { |
---|
[48] | 333 | x86_panic((char *)__func__); |
---|
[25] | 334 | return 0; |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | void hal_gpt_get_pte( gpt_t * gpt, |
---|
| 338 | vpn_t vpn, |
---|
| 339 | uint32_t * attr, |
---|
| 340 | ppn_t * ppn ) |
---|
| 341 | { |
---|
[48] | 342 | x86_panic((char *)__func__); |
---|
[25] | 343 | } |
---|
| 344 | |
---|
| 345 | void hal_gpt_reset_pte( gpt_t * gpt, |
---|
| 346 | vpn_t vpn ) |
---|
| 347 | { |
---|
[48] | 348 | x86_panic((char *)__func__); |
---|
[25] | 349 | } |
---|
| 350 | |
---|
| 351 | error_t hal_gpt_lock_pte( gpt_t * gpt, |
---|
| 352 | vpn_t vpn ) |
---|
| 353 | { |
---|
[48] | 354 | x86_panic((char *)__func__); |
---|
[25] | 355 | return 0; |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | error_t hal_gpt_unlock_pte( gpt_t * gpt, |
---|
| 359 | vpn_t vpn ) |
---|
| 360 | { |
---|
[48] | 361 | x86_panic((char *)__func__); |
---|
[25] | 362 | return 0; |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | error_t hal_gpt_copy( gpt_t * dst_gpt, |
---|
| 366 | gpt_t * src_gpt, |
---|
| 367 | bool_t cow ) |
---|
| 368 | { |
---|
[48] | 369 | x86_panic((char *)__func__); |
---|
[25] | 370 | return 0; |
---|
| 371 | } |
---|
| 372 | |
---|