[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 | |
---|
[35] | 38 | #define VA_SIGN_MASK 0xffff000000000000 |
---|
| 39 | #define VA_SIGN_POS(va) ((va) & ~VA_SIGN_MASK) |
---|
| 40 | |
---|
| 41 | #define pl1_i(VA) (((VA_SIGN_POS(VA)) & L1_FRAME) >> L1_SHIFT) |
---|
| 42 | #define pl2_i(VA) (((VA_SIGN_POS(VA)) & L2_FRAME) >> L2_SHIFT) |
---|
| 43 | #define pl3_i(VA) (((VA_SIGN_POS(VA)) & L3_FRAME) >> L3_SHIFT) |
---|
| 44 | #define pl4_i(VA) (((VA_SIGN_POS(VA)) & L4_FRAME) >> L4_SHIFT) |
---|
| 45 | |
---|
[44] | 46 | extern vaddr_t __kernel_end; |
---|
| 47 | size_t kimg_size __in_kdata = 0; |
---|
| 48 | |
---|
[35] | 49 | paddr_t pa_avail __in_kdata = 0; |
---|
| 50 | vaddr_t va_avail __in_kdata = 0; |
---|
| 51 | vaddr_t tmpva __in_kdata = (KERNBASE + NKL2_KIMG_ENTRIES * NBPD_L2); |
---|
| 52 | |
---|
| 53 | paddr_t hal_gpt_bootstrap_palloc(size_t npages) |
---|
| 54 | { |
---|
| 55 | paddr_t pa = pa_avail; |
---|
| 56 | pa_avail += npages * PAGE_SIZE; |
---|
| 57 | return pa; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | vaddr_t hal_gpt_bootstrap_valloc(size_t npages) |
---|
| 61 | { |
---|
| 62 | vaddr_t va = va_avail; |
---|
| 63 | va_avail += npages * PAGE_SIZE; |
---|
| 64 | return va; |
---|
| 65 | } |
---|
| 66 | |
---|
[45] | 67 | /* |
---|
| 68 | * Reset the bootstrap VA we've used in cluster0 so far. After this |
---|
| 69 | * function, cluster0's heap is empty. |
---|
| 70 | */ |
---|
| 71 | void hal_gpt_bootstrap_reset() |
---|
| 72 | { |
---|
[114] | 73 | // XXX: will be revisited later |
---|
| 74 | // size_t npages = (va_avail - (CLUSTER_MIN_VA(0) + KERNEL_VA_SIZE)) / PAGE_SIZE; |
---|
| 75 | // hal_gpt_leave_range(CLUSTER_MIN_VA(0) + KERNEL_VA_SIZE, npages); |
---|
| 76 | // va_avail = CLUSTER_MIN_VA(0) + KERNEL_VA_SIZE; |
---|
[45] | 77 | } |
---|
| 78 | |
---|
[83] | 79 | void hal_gpt_enter(vaddr_t va, paddr_t pa, pt_entry_t flags) |
---|
[35] | 80 | { |
---|
[45] | 81 | XASSERT(va % PAGE_SIZE == 0); |
---|
| 82 | XASSERT(pa % PAGE_SIZE == 0); |
---|
| 83 | XASSERT(va == tmpva || PTE_BASE[pl1_i(va)] == 0); |
---|
[83] | 84 | PTE_BASE[pl1_i(va)] = (pa & PG_FRAME) | flags; |
---|
[79] | 85 | invlpg(va); |
---|
[35] | 86 | } |
---|
| 87 | |
---|
[39] | 88 | void hal_gpt_enter_range(vaddr_t va, paddr_t pa, size_t n) |
---|
| 89 | { |
---|
[83] | 90 | pt_entry_t flags = PG_V | PG_KW | PG_NX; |
---|
[39] | 91 | size_t i; |
---|
| 92 | for (i = 0; i < n; i++) { |
---|
[83] | 93 | hal_gpt_enter(va + i * PAGE_SIZE, pa + i * PAGE_SIZE, flags); |
---|
[39] | 94 | } |
---|
| 95 | } |
---|
| 96 | |
---|
[45] | 97 | void hal_gpt_leave(vaddr_t va) |
---|
| 98 | { |
---|
| 99 | XASSERT(va % PAGE_SIZE == 0); |
---|
| 100 | XASSERT(PTE_BASE[pl1_i(va)] != 0); |
---|
| 101 | PTE_BASE[pl1_i(va)] = 0; |
---|
[79] | 102 | invlpg(va); |
---|
[45] | 103 | } |
---|
| 104 | |
---|
| 105 | void hal_gpt_leave_range(vaddr_t va, size_t n) |
---|
| 106 | { |
---|
| 107 | size_t i; |
---|
| 108 | for (i = 0; i < n; i++) { |
---|
| 109 | hal_gpt_leave(va + i * PAGE_SIZE); |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | |
---|
[35] | 113 | /* |
---|
| 114 | * Create a page tree that can map va_start->va_end. The caller can then |
---|
| 115 | * enter these addresses to physical locations. |
---|
| 116 | * |
---|
| 117 | * This functions is a bit complicated, and may need to be revisited. |
---|
| 118 | */ |
---|
| 119 | void hal_gpt_maptree_area(vaddr_t va_start, vaddr_t va_end) |
---|
| 120 | { |
---|
[112] | 121 | pt_entry_t flags = PG_V | PG_KW | PG_NX; |
---|
[35] | 122 | size_t L4start, L4end, nL4e; |
---|
| 123 | size_t L3start, L3end, nL3e; |
---|
| 124 | size_t L2start, L2end, nL2e; |
---|
| 125 | paddr_t L3page, L2page, L1page; |
---|
| 126 | paddr_t pa; |
---|
| 127 | size_t i, npa; |
---|
| 128 | pt_entry_t *pde; |
---|
| 129 | |
---|
| 130 | /* Allocate L3 */ |
---|
| 131 | L4start = pl4_i(va_start); |
---|
| 132 | L4end = pl4_i(va_end); |
---|
| 133 | nL4e = (L4end - L4start + 1); |
---|
| 134 | L3page = hal_gpt_bootstrap_palloc(nL4e); |
---|
| 135 | |
---|
| 136 | /* Allocate L2 */ |
---|
| 137 | L3start = pl3_i(va_start); |
---|
| 138 | L3end = pl3_i(va_end); |
---|
| 139 | nL3e = (L3end - L3start + 1); |
---|
| 140 | L2page = hal_gpt_bootstrap_palloc(nL3e); |
---|
| 141 | |
---|
| 142 | /* Allocate L1 */ |
---|
| 143 | L2start = pl2_i(va_start); |
---|
| 144 | L2end = pl2_i(va_end); |
---|
| 145 | nL2e = (L2end - L2start + 1); |
---|
| 146 | L1page = hal_gpt_bootstrap_palloc(nL2e); |
---|
| 147 | |
---|
| 148 | /* Zero out L1 */ |
---|
| 149 | for (i = 0; i < nL2e; i++) { |
---|
| 150 | pa = L1page + i * PAGE_SIZE; |
---|
[83] | 151 | hal_gpt_enter(tmpva, pa, flags); |
---|
[35] | 152 | |
---|
| 153 | memset((void *)tmpva, 0, PAGE_SIZE); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | /* Zero out L2 */ |
---|
| 157 | for (i = 0; i < nL3e; i++) { |
---|
| 158 | pa = L2page + i * PAGE_SIZE; |
---|
[83] | 159 | hal_gpt_enter(tmpva, pa, flags); |
---|
[35] | 160 | |
---|
| 161 | memset((void *)tmpva, 0, PAGE_SIZE); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | /* Zero out L3 */ |
---|
| 165 | for (i = 0; i < nL4e; i++) { |
---|
| 166 | pa = L3page + i * PAGE_SIZE; |
---|
[83] | 167 | hal_gpt_enter(tmpva, pa, flags); |
---|
[35] | 168 | |
---|
| 169 | memset((void *)tmpva, 0, PAGE_SIZE); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | /* Create L2, linked to L1 */ |
---|
| 173 | npa = (L2start / NPDPG) * PAGE_SIZE; |
---|
| 174 | for (i = L2start; i <= L2end; i++) { |
---|
| 175 | pa = (paddr_t)&(((pt_entry_t *)L2page)[i]); |
---|
| 176 | pa -= npa; /* shift on the left */ |
---|
| 177 | pa &= PG_FRAME; /* rounddown to a page boundary */ |
---|
[83] | 178 | hal_gpt_enter(tmpva, pa, flags); |
---|
[35] | 179 | |
---|
| 180 | pde = (pt_entry_t *)tmpva; |
---|
| 181 | pa = L1page + (i - L2start) * PAGE_SIZE; |
---|
| 182 | pde[i % NPDPG] = (pa & PG_FRAME) | PG_V | PG_KW; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | /* Create L3, linked to L2 */ |
---|
| 186 | npa = (L3start / NPDPG) * PAGE_SIZE; |
---|
| 187 | for (i = L3start; i <= L3end; i++) { |
---|
| 188 | pa = (paddr_t)&(((pt_entry_t *)L3page)[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 = L2page + (i - L3start) * PAGE_SIZE; |
---|
| 195 | pde[i % NPDPG] = (pa & PG_FRAME) | PG_V | PG_KW; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | /* Link L3 into L4 */ |
---|
| 199 | for (i = 0; i < nL4e; i++) { |
---|
| 200 | pa = L3page + i * PAGE_SIZE; |
---|
| 201 | L4_BASE[L4start + i] = (pa & PG_FRAME) | PG_V | PG_KW; |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | void hal_gpt_init(paddr_t firstpa) |
---|
| 206 | { |
---|
[112] | 207 | paddr_t kimg_min_pa, kimg_max_pa; |
---|
[44] | 208 | |
---|
| 209 | /* Initialize global values */ |
---|
[35] | 210 | pa_avail = firstpa; |
---|
[47] | 211 | va_avail = CLUSTER_MIN_VA(0) + KERNEL_VA_SIZE; |
---|
[44] | 212 | kimg_size = ((uint64_t)&__kernel_end - KERNBASE); |
---|
| 213 | XASSERT(kimg_size % PAGE_SIZE == 0); |
---|
| 214 | |
---|
[112] | 215 | kimg_min_pa = 0; |
---|
| 216 | kimg_max_pa = kimg_min_pa + kimg_size; |
---|
| 217 | |
---|
[44] | 218 | /* Create cluster0's heap entry. */ |
---|
[47] | 219 | hal_gpt_maptree_area(CLUSTER_MIN_VA(0), CLUSTER_MAX_VA(0)); |
---|
[44] | 220 | |
---|
| 221 | /* Manually enter cluster0's kimg */ |
---|
[112] | 222 | hal_gpt_enter_range(CLUSTER_MIN_VA(0), kimg_min_pa, kimg_size / PAGE_SIZE); |
---|
| 223 | |
---|
| 224 | /* Manually enter cluster0's heap */ |
---|
| 225 | /* |
---|
| 226 | hal_gpt_enter_range(CLUSTER_MIN_VA(0) + kimg_size, kimg_max_pa, |
---|
| 227 | (CLUSTER_VA_SIZE - kimg_size) / PAGE_SIZE); |
---|
| 228 | */ |
---|
[35] | 229 | } |
---|
| 230 | |
---|
| 231 | /* -------------------------------------------------------------------------- */ |
---|
| 232 | |
---|
[25] | 233 | /**************************************************************************************** |
---|
| 234 | * These global variables defines the masks for the Generic Page Table Entry attributes, |
---|
| 235 | * and must be defined in all GPT implementation. |
---|
| 236 | ***************************************************************************************/ |
---|
| 237 | |
---|
| 238 | uint32_t GPT_MAPPED; |
---|
| 239 | uint32_t GPT_SMALL; |
---|
| 240 | uint32_t GPT_READABLE; |
---|
| 241 | uint32_t GPT_WRITABLE; |
---|
| 242 | uint32_t GPT_EXECUTABLE; |
---|
| 243 | uint32_t GPT_CACHABLE; |
---|
| 244 | uint32_t GPT_USER; |
---|
| 245 | uint32_t GPT_DIRTY; |
---|
| 246 | uint32_t GPT_ACCESSED; |
---|
| 247 | uint32_t GPT_GLOBAL; |
---|
| 248 | uint32_t GPT_COW; |
---|
| 249 | uint32_t GPT_SWAP; |
---|
| 250 | uint32_t GPT_LOCKED; |
---|
| 251 | |
---|
| 252 | error_t hal_gpt_create( gpt_t * gpt ) |
---|
| 253 | { |
---|
[48] | 254 | x86_panic((char *)__func__); |
---|
[25] | 255 | return 0; |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | void hal_gpt_destroy( gpt_t * gpt ) |
---|
| 259 | { |
---|
[48] | 260 | x86_panic((char *)__func__); |
---|
[25] | 261 | } |
---|
| 262 | |
---|
| 263 | void hal_gpt_print( gpt_t * gpt ) |
---|
| 264 | { |
---|
[48] | 265 | x86_panic((char *)__func__); |
---|
[25] | 266 | } |
---|
| 267 | |
---|
| 268 | error_t hal_gpt_set_pte( gpt_t * gpt, |
---|
| 269 | vpn_t vpn, |
---|
| 270 | ppn_t ppn, |
---|
| 271 | uint32_t attr ) |
---|
| 272 | { |
---|
[48] | 273 | x86_panic((char *)__func__); |
---|
[25] | 274 | return 0; |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | void hal_gpt_get_pte( gpt_t * gpt, |
---|
| 278 | vpn_t vpn, |
---|
| 279 | uint32_t * attr, |
---|
| 280 | ppn_t * ppn ) |
---|
| 281 | { |
---|
[48] | 282 | x86_panic((char *)__func__); |
---|
[25] | 283 | } |
---|
| 284 | |
---|
| 285 | void hal_gpt_reset_pte( gpt_t * gpt, |
---|
| 286 | vpn_t vpn ) |
---|
| 287 | { |
---|
[48] | 288 | x86_panic((char *)__func__); |
---|
[25] | 289 | } |
---|
| 290 | |
---|
| 291 | error_t hal_gpt_lock_pte( gpt_t * gpt, |
---|
| 292 | vpn_t vpn ) |
---|
| 293 | { |
---|
[48] | 294 | x86_panic((char *)__func__); |
---|
[25] | 295 | return 0; |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | error_t hal_gpt_unlock_pte( gpt_t * gpt, |
---|
| 299 | vpn_t vpn ) |
---|
| 300 | { |
---|
[48] | 301 | x86_panic((char *)__func__); |
---|
[25] | 302 | return 0; |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | error_t hal_gpt_copy( gpt_t * dst_gpt, |
---|
| 306 | gpt_t * src_gpt, |
---|
| 307 | bool_t cow ) |
---|
| 308 | { |
---|
[48] | 309 | x86_panic((char *)__func__); |
---|
[25] | 310 | return 0; |
---|
| 311 | } |
---|
| 312 | |
---|