| 1 | /* |
|---|
| 2 | * hal_init.c - C initialization procedure for x86. |
|---|
| 3 | * |
|---|
| 4 | * Copyright (c) 2017 Maxime Villard |
|---|
| 5 | * |
|---|
| 6 | * This file is part of ALMOS-MKH. |
|---|
| 7 | * |
|---|
| 8 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
|---|
| 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 | * |
|---|
| 12 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
|---|
| 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> |
|---|
| 23 | #include <hal_boot.h> |
|---|
| 24 | #include <hal_multiboot.h> |
|---|
| 25 | #include <hal_segmentation.h> |
|---|
| 26 | #include <hal_acpi.h> |
|---|
| 27 | #include <hal_apic.h> |
|---|
| 28 | #include <hal_internal.h> |
|---|
| 29 | #include <hal_register.h> |
|---|
| 30 | |
|---|
| 31 | #include <hal_remote.h> |
|---|
| 32 | #include <hal_irqmask.h> |
|---|
| 33 | |
|---|
| 34 | #include <memcpy.h> |
|---|
| 35 | #include <thread.h> |
|---|
| 36 | #include <string.h> |
|---|
| 37 | #include <process.h> |
|---|
| 38 | #include <printk.h> |
|---|
| 39 | #include <vmm.h> |
|---|
| 40 | #include <core.h> |
|---|
| 41 | #include <cluster.h> |
|---|
| 42 | #include <chdev.h> |
|---|
| 43 | |
|---|
| 44 | #include <boot_info.h> |
|---|
| 45 | |
|---|
| 46 | void kernel_init(boot_info_t *info); |
|---|
| 47 | |
|---|
| 48 | static void gdt_create(); |
|---|
| 49 | static void idt_create(); |
|---|
| 50 | void cpu_tls_init(size_t lid); |
|---|
| 51 | void cpu_identify(); |
|---|
| 52 | void cpu_attach(size_t lid); |
|---|
| 53 | |
|---|
| 54 | size_t mytest __in_kdata = 0; |
|---|
| 55 | |
|---|
| 56 | struct multiboot_info mb_info __in_kdata; |
|---|
| 57 | char mb_loader_name[PAGE_SIZE] __in_kdata; |
|---|
| 58 | uint8_t mb_mmap[PAGE_SIZE] __in_kdata; |
|---|
| 59 | |
|---|
| 60 | size_t ncpu __in_kdata = 0; |
|---|
| 61 | static boot_info_t btinfo __in_kdata; |
|---|
| 62 | |
|---|
| 63 | /* x86-specific per-cluster structures */ |
|---|
| 64 | uint8_t gdtstore[PAGE_SIZE] __in_kdata; |
|---|
| 65 | uint8_t idtstore[PAGE_SIZE] __in_kdata; |
|---|
| 66 | |
|---|
| 67 | /* x86-specific per-cpu structures */ |
|---|
| 68 | typedef struct { |
|---|
| 69 | bool_t valid; |
|---|
| 70 | struct tss tss; |
|---|
| 71 | struct tls tls; |
|---|
| 72 | uint8_t boot_stack[STKSIZE]; |
|---|
| 73 | uint8_t intr_stack[STKSIZE]; |
|---|
| 74 | uint8_t dbfl_stack[STKSIZE]; |
|---|
| 75 | uint8_t nmfl_stack[STKSIZE]; |
|---|
| 76 | } percpu_archdata_t; |
|---|
| 77 | percpu_archdata_t cpudata[CONFIG_MAX_LOCAL_CORES] __in_kdata; |
|---|
| 78 | |
|---|
| 79 | /* -------------------------------------------------------------------------- */ |
|---|
| 80 | |
|---|
| 81 | static void |
|---|
| 82 | dump_memmap() |
|---|
| 83 | { |
|---|
| 84 | size_t mmap_length = mb_info.mi_mmap_length; |
|---|
| 85 | uint8_t *mmap_addr = (uint8_t *)&mb_mmap; |
|---|
| 86 | size_t i; |
|---|
| 87 | |
|---|
| 88 | if (!(mb_info.mi_flags & MULTIBOOT_INFO_HAS_MMAP)) |
|---|
| 89 | x86_panic("No mmap"); |
|---|
| 90 | |
|---|
| 91 | i = 0; |
|---|
| 92 | while (i < mmap_length) { |
|---|
| 93 | struct multiboot_mmap *mm; |
|---|
| 94 | |
|---|
| 95 | mm = (struct multiboot_mmap *)(mmap_addr + i); |
|---|
| 96 | |
|---|
| 97 | x86_printf("-> [%Z, %Z] %s\n", mm->mm_base_addr, |
|---|
| 98 | mm->mm_base_addr + mm->mm_length, |
|---|
| 99 | (mm->mm_type == 1) ? "ram" : "rsv" ); |
|---|
| 100 | |
|---|
| 101 | i += mm->mm_size + 4; |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | /* -------------------------------------------------------------------------- */ |
|---|
| 106 | |
|---|
| 107 | static size_t init_bootinfo_pages_nr() |
|---|
| 108 | { |
|---|
| 109 | size_t mmap_length = mb_info.mi_mmap_length; |
|---|
| 110 | uint8_t *mmap_addr = (uint8_t *)&mb_mmap; |
|---|
| 111 | paddr_t maxpa, pa; |
|---|
| 112 | size_t i; |
|---|
| 113 | |
|---|
| 114 | i = 0; |
|---|
| 115 | maxpa = 0; |
|---|
| 116 | while (i < mmap_length) { |
|---|
| 117 | struct multiboot_mmap *mm; |
|---|
| 118 | |
|---|
| 119 | mm = (struct multiboot_mmap *)(mmap_addr + i); |
|---|
| 120 | |
|---|
| 121 | if (mm->mm_type == 1) { |
|---|
| 122 | pa = mm->mm_base_addr + mm->mm_length; |
|---|
| 123 | if (pa > maxpa) |
|---|
| 124 | maxpa = pa; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | i += mm->mm_size + 4; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | return (maxpa / PAGE_SIZE); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | static size_t init_bootinfo_rsvd(boot_rsvd_t *rsvd) |
|---|
| 134 | { |
|---|
| 135 | size_t mmap_length = mb_info.mi_mmap_length; |
|---|
| 136 | uint8_t *mmap_addr = (uint8_t *)&mb_mmap; |
|---|
| 137 | size_t i, rsvd_nr; |
|---|
| 138 | |
|---|
| 139 | memset(rsvd, 0, sizeof(boot_rsvd_t) * CONFIG_PPM_MAX_RSVD); |
|---|
| 140 | |
|---|
| 141 | i = 0, rsvd_nr = 0; |
|---|
| 142 | while (i < mmap_length) { |
|---|
| 143 | struct multiboot_mmap *mm; |
|---|
| 144 | |
|---|
| 145 | mm = (struct multiboot_mmap *)(mmap_addr + i); |
|---|
| 146 | |
|---|
| 147 | if (mm->mm_type != 1) { |
|---|
| 148 | rsvd[rsvd_nr].first_page = |
|---|
| 149 | rounddown(mm->mm_base_addr, PAGE_SIZE) / PAGE_SIZE; |
|---|
| 150 | rsvd[rsvd_nr].npages = |
|---|
| 151 | roundup(mm->mm_length, PAGE_SIZE) / PAGE_SIZE; |
|---|
| 152 | rsvd_nr++; |
|---|
| 153 | if (rsvd_nr == CONFIG_PPM_MAX_RSVD) |
|---|
| 154 | x86_panic("too many memory holes"); |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | i += mm->mm_size + 4; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | return rsvd_nr; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | static void init_bootinfo_core(boot_core_t *core) |
|---|
| 164 | { |
|---|
| 165 | size_t i; |
|---|
| 166 | |
|---|
| 167 | // XXX: not necessarily contiguous |
|---|
| 168 | for (i = 0; i < ncpu; i++) { |
|---|
| 169 | memset(&core[i], 0, sizeof(boot_core_t)); |
|---|
| 170 | |
|---|
| 171 | core[i].gid = i; |
|---|
| 172 | core[i].lid = i; |
|---|
| 173 | core[i].cxy = 0; |
|---|
| 174 | } |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | static void init_bootinfo_ioc(boot_device_t *dev) |
|---|
| 178 | { |
|---|
| 179 | memset(dev, 0, sizeof(boot_device_t)); |
|---|
| 180 | |
|---|
| 181 | dev->base = 0; |
|---|
| 182 | dev->type = (DEV_FUNC_IOC << 16) | IMPL_IOC_BDV; |
|---|
| 183 | dev->channels = 1; |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | static void init_bootinfo_pic(boot_device_t *dev) |
|---|
| 187 | { |
|---|
| 188 | memset(dev, 0, sizeof(boot_device_t)); |
|---|
| 189 | |
|---|
| 190 | dev->base = 0; |
|---|
| 191 | dev->type = (DEV_FUNC_PIC << 16) | IMPL_PIC_SCL; |
|---|
| 192 | dev->channels = 1; |
|---|
| 193 | dev->param0 = 0; |
|---|
| 194 | dev->param1 = 0; |
|---|
| 195 | dev->param2 = 0; |
|---|
| 196 | dev->param3 = 0; |
|---|
| 197 | |
|---|
| 198 | dev->irqs = 16; |
|---|
| 199 | |
|---|
| 200 | /* COM1 */ |
|---|
| 201 | dev->irq[IRQ_COM1].dev_type = (DEV_FUNC_TXT << 16) | IMPL_TXT_TTY; |
|---|
| 202 | dev->irq[IRQ_COM1].channel = 0; |
|---|
| 203 | dev->irq[IRQ_COM1].is_rx = 0; |
|---|
| 204 | dev->irq[IRQ_COM1].valid = 1; |
|---|
| 205 | |
|---|
| 206 | /* ATA */ |
|---|
| 207 | dev->irq[IRQ_ATA0].dev_type = (DEV_FUNC_IOC << 16) | IMPL_IOC_BDV; |
|---|
| 208 | dev->irq[IRQ_ATA0].channel = 0; |
|---|
| 209 | dev->irq[IRQ_ATA0].is_rx = 0; |
|---|
| 210 | dev->irq[IRQ_ATA0].valid = 1; |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | static void init_bootinfo_txt(boot_device_t *dev) |
|---|
| 214 | { |
|---|
| 215 | memset(dev, 0, sizeof(boot_device_t)); |
|---|
| 216 | |
|---|
| 217 | dev->base = 0; |
|---|
| 218 | dev->type = (DEV_FUNC_TXT << 16) | IMPL_TXT_TTY; |
|---|
| 219 | dev->channels = 1; |
|---|
| 220 | dev->param0 = 0; |
|---|
| 221 | dev->param1 = 0; |
|---|
| 222 | dev->param2 = 0; |
|---|
| 223 | dev->param3 = 0; |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | static void init_bootinfo(boot_info_t *info) |
|---|
| 227 | { |
|---|
| 228 | size_t offset; |
|---|
| 229 | |
|---|
| 230 | extern uint64_t __kernel_data_start; |
|---|
| 231 | extern uint64_t __kernel_end; |
|---|
| 232 | |
|---|
| 233 | memset(info, 0, sizeof(boot_info_t)); |
|---|
| 234 | |
|---|
| 235 | info->signature = 0; |
|---|
| 236 | |
|---|
| 237 | info->paddr_width = 0; |
|---|
| 238 | info->x_width = 1; |
|---|
| 239 | info->y_width = 1; |
|---|
| 240 | info->x_size = 1; |
|---|
| 241 | info->y_size = 1; |
|---|
| 242 | info->io_cxy = 0; |
|---|
| 243 | |
|---|
| 244 | info->ext_dev_nr = 3; |
|---|
| 245 | init_bootinfo_txt(&info->ext_dev[0]); |
|---|
| 246 | init_bootinfo_pic(&info->ext_dev[1]); |
|---|
| 247 | init_bootinfo_ioc(&info->ext_dev[2]); |
|---|
| 248 | |
|---|
| 249 | info->cxy = 0; |
|---|
| 250 | info->cores_nr = ncpu; |
|---|
| 251 | init_bootinfo_core((boot_core_t *)&info->core); |
|---|
| 252 | |
|---|
| 253 | info->rsvd_nr = init_bootinfo_rsvd((boot_rsvd_t *)&info->rsvd); |
|---|
| 254 | |
|---|
| 255 | /* TODO: dev_icu */ |
|---|
| 256 | /* TODO: dev_mmc */ |
|---|
| 257 | /* TODO: dev_dma */ |
|---|
| 258 | |
|---|
| 259 | offset = hal_gpt_bootstrap_uniformize(); |
|---|
| 260 | info->pages_offset = offset / PAGE_SIZE; |
|---|
| 261 | info->pages_nr = init_bootinfo_pages_nr(); |
|---|
| 262 | |
|---|
| 263 | info->kernel_code_start = (intptr_t)(KERNTEXTOFF - KERNBASE); |
|---|
| 264 | info->kernel_code_end = (intptr_t)(&__kernel_data_start - KERNBASE) - 1; |
|---|
| 265 | info->kernel_data_start = (intptr_t)(&__kernel_data_start - KERNBASE); |
|---|
| 266 | info->kernel_code_end = (intptr_t)(&__kernel_end - KERNBASE) - 1; |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | /* -------------------------------------------------------------------------- */ |
|---|
| 270 | |
|---|
| 271 | static uint32_t cpuN_booted __in_kdata; |
|---|
| 272 | |
|---|
| 273 | void start_secondary_cpus() |
|---|
| 274 | { |
|---|
| 275 | pt_entry_t flags = PG_V | PG_KW; |
|---|
| 276 | extern vaddr_t cpuN_boot_trampoline; |
|---|
| 277 | extern vaddr_t cpuN_boot_trampoline_end; |
|---|
| 278 | extern paddr_t smp_L4pa; |
|---|
| 279 | extern vaddr_t smp_stkva; |
|---|
| 280 | extern paddr_t L4paddr; |
|---|
| 281 | size_t i, sz; |
|---|
| 282 | |
|---|
| 283 | smp_L4pa = L4paddr; |
|---|
| 284 | |
|---|
| 285 | /* map the SMP trampoline (identity) */ |
|---|
| 286 | vaddr_t trampva = (vaddr_t)SMP_TRAMPOLINE_PA; |
|---|
| 287 | hal_gpt_maptree_area(trampva, trampva + PAGE_SIZE); |
|---|
| 288 | hal_gpt_enter(trampva, SMP_TRAMPOLINE_PA, flags); |
|---|
| 289 | |
|---|
| 290 | /* copy it */ |
|---|
| 291 | sz = (size_t)&cpuN_boot_trampoline_end - (size_t)&cpuN_boot_trampoline; |
|---|
| 292 | memcpy((void *)trampva, (void *)&cpuN_boot_trampoline, sz); |
|---|
| 293 | |
|---|
| 294 | for (i = 0; i < CONFIG_MAX_LOCAL_CORES; i++) { |
|---|
| 295 | if (i == 0 || !cpudata[i].valid) { |
|---|
| 296 | continue; |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | smp_stkva = (vaddr_t)cpudata[i].boot_stack + STKSIZE; |
|---|
| 300 | |
|---|
| 301 | cpuN_booted = 0; |
|---|
| 302 | boot_cpuN(i, SMP_TRAMPOLINE_PA); |
|---|
| 303 | while (!hal_atomic_cas(&cpuN_booted, 1, 0)) { |
|---|
| 304 | /* wait */ |
|---|
| 305 | } |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | // XXX: unmap the trampoline |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | void init_x86_64_cpuN() |
|---|
| 312 | { |
|---|
| 313 | lid_t lid; |
|---|
| 314 | |
|---|
| 315 | cli(); |
|---|
| 316 | |
|---|
| 317 | lid = hal_lapic_gid(); |
|---|
| 318 | |
|---|
| 319 | cpu_attach(lid); |
|---|
| 320 | x86_printf("[cpu%z] cpu_attach called\n", (uint64_t)lid); |
|---|
| 321 | |
|---|
| 322 | cpu_tls_init(lid); |
|---|
| 323 | x86_printf("[cpu%z] cput_tls_init called\n", (uint64_t)lid); |
|---|
| 324 | |
|---|
| 325 | cpu_lapic_init(); |
|---|
| 326 | x86_printf("[cpu%z] cpu_lapic_init called\n", (uint64_t)lid); |
|---|
| 327 | |
|---|
| 328 | cpuN_booted = 1; |
|---|
| 329 | |
|---|
| 330 | if (lid == 1) { |
|---|
| 331 | hal_ioapic_disable_irq(IRQ_KEYBOARD); |
|---|
| 332 | hal_ioapic_bind_irq(IRQ_KEYBOARD, IOAPIC_KEYBOARD_VECTOR, 1); |
|---|
| 333 | hal_ioapic_enable_irq(IRQ_KEYBOARD); |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | kernel_init(&btinfo); |
|---|
| 337 | |
|---|
| 338 | reg_t dummy; |
|---|
| 339 | hal_enable_irq(&dummy); |
|---|
| 340 | |
|---|
| 341 | while (1); |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | /* -------------------------------------------------------------------------- */ |
|---|
| 345 | |
|---|
| 346 | static void apic_map() |
|---|
| 347 | { |
|---|
| 348 | extern vaddr_t lapic_va, ioapic_va; |
|---|
| 349 | extern paddr_t lapic_pa, ioapic_pa; |
|---|
| 350 | |
|---|
| 351 | lapic_va = hal_gpt_bootstrap_valloc(1); // XXX: should be shared |
|---|
| 352 | hal_gpt_enter(lapic_va, lapic_pa, PG_V|PG_KW|PG_NX|PG_N); |
|---|
| 353 | |
|---|
| 354 | ioapic_va = hal_gpt_bootstrap_valloc(1); // XXX: should be shared |
|---|
| 355 | hal_gpt_enter(ioapic_va, ioapic_pa, PG_V|PG_KW|PG_NX|PG_N); |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | void init_x86_64(paddr_t firstpa) |
|---|
| 359 | { |
|---|
| 360 | cli(); |
|---|
| 361 | |
|---|
| 362 | /* Initialize the serial port */ |
|---|
| 363 | hal_com_init_early(); |
|---|
| 364 | |
|---|
| 365 | x86_printf("[+] init_x86_64 called\n"); |
|---|
| 366 | |
|---|
| 367 | /* Create the global structures */ |
|---|
| 368 | gdt_create(); |
|---|
| 369 | idt_create(); |
|---|
| 370 | |
|---|
| 371 | /* Identify the features of the cpu */ |
|---|
| 372 | cpu_identify(); |
|---|
| 373 | |
|---|
| 374 | /* Attach cpu0 */ |
|---|
| 375 | cpu_attach(0); |
|---|
| 376 | x86_printf("[+] cpu_attach called\n"); |
|---|
| 377 | |
|---|
| 378 | x86_printf("[+] bootloader: '%s'\n", mb_loader_name); |
|---|
| 379 | |
|---|
| 380 | dump_memmap(); |
|---|
| 381 | x86_printf("[+] dump finished\n"); |
|---|
| 382 | |
|---|
| 383 | hal_gpt_init(firstpa); |
|---|
| 384 | x86_printf("[+] hal_gpt_init called\n"); |
|---|
| 385 | |
|---|
| 386 | hal_acpi_init(); |
|---|
| 387 | x86_printf("[+] hal_acpi_init called\n"); |
|---|
| 388 | |
|---|
| 389 | hal_gpt_bootstrap_reset(); |
|---|
| 390 | x86_printf("[+] hal_gpt_bootstrap_reset called\n"); |
|---|
| 391 | |
|---|
| 392 | apic_map(); |
|---|
| 393 | x86_printf("[+] apic_map called\n"); |
|---|
| 394 | |
|---|
| 395 | hal_apic_init(); |
|---|
| 396 | cpu_lapic_init(); |
|---|
| 397 | x86_printf("[+] hal_apic_init called\n"); |
|---|
| 398 | |
|---|
| 399 | cpu_tls_init(0); |
|---|
| 400 | x86_printf("[+] cput_tls_init called\n"); |
|---|
| 401 | |
|---|
| 402 | mytest = 0; |
|---|
| 403 | x86_printf("-> mytest = %z\n", mytest); |
|---|
| 404 | void *hoho = &init_x86_64; |
|---|
| 405 | xptr_t myptr = XPTR(0, &mytest); |
|---|
| 406 | |
|---|
| 407 | hal_remote_spt(myptr, hoho); |
|---|
| 408 | x86_printf("-> mytest = %Z\n", hal_remote_lpt(myptr)); |
|---|
| 409 | |
|---|
| 410 | init_bootinfo(&btinfo); |
|---|
| 411 | |
|---|
| 412 | start_secondary_cpus(); |
|---|
| 413 | |
|---|
| 414 | kernel_init(&btinfo); |
|---|
| 415 | |
|---|
| 416 | x86_printf("[+] kernel_init called\n"); |
|---|
| 417 | |
|---|
| 418 | reg_t dummy; |
|---|
| 419 | hal_enable_irq(&dummy); |
|---|
| 420 | /* |
|---|
| 421 | void *ptr; |
|---|
| 422 | |
|---|
| 423 | khm_t *khm = &LOCAL_CLUSTER->khm; |
|---|
| 424 | ptr = khm_alloc(khm, 10); |
|---|
| 425 | memset(ptr, 0, 10); |
|---|
| 426 | khm_free(ptr); |
|---|
| 427 | |
|---|
| 428 | |
|---|
| 429 | kcm_t *kcm = &LOCAL_CLUSTER->kcm; |
|---|
| 430 | ptr = kcm_alloc(kcm); |
|---|
| 431 | memset(ptr, 0, 1); |
|---|
| 432 | kcm_free(ptr); |
|---|
| 433 | |
|---|
| 434 | ptr = ppm_alloc_pages(1); |
|---|
| 435 | ppm_free_pages(ptr); |
|---|
| 436 | */ |
|---|
| 437 | while (1); |
|---|
| 438 | |
|---|
| 439 | // void x86_stop(); |
|---|
| 440 | // x86_stop(); |
|---|
| 441 | } |
|---|
| 442 | |
|---|
| 443 | /* -------------------------------------------------------------------------- */ |
|---|
| 444 | |
|---|
| 445 | void cpu_activate(uint32_t gid) |
|---|
| 446 | { |
|---|
| 447 | cpudata[gid].valid = true; |
|---|
| 448 | } |
|---|
| 449 | |
|---|
| 450 | static void |
|---|
| 451 | setregion(struct region_descriptor *rd, void *base, uint16_t limit) |
|---|
| 452 | { |
|---|
| 453 | rd->rd_limit = limit; |
|---|
| 454 | rd->rd_base = (uint64_t)base; |
|---|
| 455 | } |
|---|
| 456 | |
|---|
| 457 | /* -------------------------------------------------------------------------- */ |
|---|
| 458 | |
|---|
| 459 | static void |
|---|
| 460 | gdt_set_memseg(struct gdt_memseg *sd, void *base, size_t limit, |
|---|
| 461 | int type, int dpl, int gran, int is64) |
|---|
| 462 | { |
|---|
| 463 | sd->sd_lolimit = (unsigned)limit; |
|---|
| 464 | sd->sd_lobase = (unsigned long)base; |
|---|
| 465 | sd->sd_type = type; |
|---|
| 466 | sd->sd_dpl = dpl; |
|---|
| 467 | sd->sd_p = 1; |
|---|
| 468 | sd->sd_hilimit = (unsigned)limit >> 16; |
|---|
| 469 | sd->sd_avl = 0; |
|---|
| 470 | sd->sd_long = is64; |
|---|
| 471 | sd->sd_def32 = 0; |
|---|
| 472 | sd->sd_gran = gran; |
|---|
| 473 | sd->sd_hibase = (unsigned long)base >> 24; |
|---|
| 474 | } |
|---|
| 475 | |
|---|
| 476 | static void |
|---|
| 477 | gdt_set_sysseg(struct gdt_sysseg *sd, void *base, size_t limit, |
|---|
| 478 | int type, int dpl, int gran) |
|---|
| 479 | { |
|---|
| 480 | memset(sd, 0, sizeof *sd); |
|---|
| 481 | sd->sd_lolimit = (unsigned)limit; |
|---|
| 482 | sd->sd_lobase = (uint64_t)base; |
|---|
| 483 | sd->sd_type = type; |
|---|
| 484 | sd->sd_dpl = dpl; |
|---|
| 485 | sd->sd_p = 1; |
|---|
| 486 | sd->sd_hilimit = (unsigned)limit >> 16; |
|---|
| 487 | sd->sd_gran = gran; |
|---|
| 488 | sd->sd_hibase = (uint64_t)base >> 24; |
|---|
| 489 | } |
|---|
| 490 | |
|---|
| 491 | static void gdt_create() |
|---|
| 492 | { |
|---|
| 493 | memset(&gdtstore, 0, PAGE_SIZE); |
|---|
| 494 | |
|---|
| 495 | /* Flat segments */ |
|---|
| 496 | gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_KCODE_SEL), 0, |
|---|
| 497 | 0xfffff, SDT_MEMERA, SEL_KPL, 1, 1); |
|---|
| 498 | gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_KDATA_SEL), 0, |
|---|
| 499 | 0xfffff, SDT_MEMRWA, SEL_KPL, 1, 1); |
|---|
| 500 | gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_UCODE_SEL), 0, |
|---|
| 501 | 0xfffff, SDT_MEMERA, SEL_UPL, 1, 1); |
|---|
| 502 | gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_UDATA_SEL), 0, |
|---|
| 503 | 0xfffff, SDT_MEMRWA, SEL_UPL, 1, 1); |
|---|
| 504 | } |
|---|
| 505 | |
|---|
| 506 | void cpu_load_gdt() |
|---|
| 507 | { |
|---|
| 508 | struct region_descriptor region; |
|---|
| 509 | setregion(®ion, &gdtstore, PAGE_SIZE - 1); |
|---|
| 510 | lgdt(®ion); |
|---|
| 511 | } |
|---|
| 512 | |
|---|
| 513 | /* -------------------------------------------------------------------------- */ |
|---|
| 514 | |
|---|
| 515 | struct { |
|---|
| 516 | bool_t busy[256]; |
|---|
| 517 | } idt_bitmap __in_kdata; |
|---|
| 518 | |
|---|
| 519 | int idt_slot_alloc() |
|---|
| 520 | { |
|---|
| 521 | size_t i; |
|---|
| 522 | |
|---|
| 523 | for (i = 0; i < 256; i++) { |
|---|
| 524 | if (!idt_bitmap.busy[i]) |
|---|
| 525 | break; |
|---|
| 526 | } |
|---|
| 527 | if (i == 256) { |
|---|
| 528 | return -1; |
|---|
| 529 | } |
|---|
| 530 | |
|---|
| 531 | idt_bitmap.busy[i] = true; |
|---|
| 532 | return (int)i; |
|---|
| 533 | } |
|---|
| 534 | |
|---|
| 535 | void idt_slot_free(int slot) |
|---|
| 536 | { |
|---|
| 537 | idt_bitmap.busy[slot] = false; |
|---|
| 538 | } |
|---|
| 539 | |
|---|
| 540 | static void |
|---|
| 541 | idt_set_seg(struct idt_seg *seg, void *func, int ist, int type, int dpl, int sel) |
|---|
| 542 | { |
|---|
| 543 | seg->gd_looffset = (uint64_t)func & 0xffff; |
|---|
| 544 | seg->gd_selector = sel; |
|---|
| 545 | seg->gd_ist = ist; |
|---|
| 546 | seg->gd_type = type; |
|---|
| 547 | seg->gd_dpl = dpl; |
|---|
| 548 | seg->gd_p = 1; |
|---|
| 549 | seg->gd_hioffset = (uint64_t)func >> 16; |
|---|
| 550 | seg->gd_zero = 0; |
|---|
| 551 | seg->gd_xx1 = 0; |
|---|
| 552 | seg->gd_xx2 = 0; |
|---|
| 553 | seg->gd_xx3 = 0; |
|---|
| 554 | } |
|---|
| 555 | |
|---|
| 556 | static void idt_create() |
|---|
| 557 | { |
|---|
| 558 | extern uint64_t x86_traps[], x86_intrs[], x86_rsvd; |
|---|
| 559 | struct idt_seg *idt; |
|---|
| 560 | size_t i; |
|---|
| 561 | |
|---|
| 562 | memset(&idt_bitmap, 0, sizeof(idt_bitmap)); |
|---|
| 563 | idt = (struct idt_seg *)&idtstore; |
|---|
| 564 | |
|---|
| 565 | /* First, put a dead entry */ |
|---|
| 566 | for (i = 0; i < NIDT; i++) { |
|---|
| 567 | idt_set_seg(&idt[i], (void *)&x86_rsvd, 0, |
|---|
| 568 | SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL)); |
|---|
| 569 | } |
|---|
| 570 | |
|---|
| 571 | /* General exceptions */ |
|---|
| 572 | for (i = CPUVEC_MIN; i < CPUVEC_MAX; i++) { |
|---|
| 573 | idt_set_seg(&idt[i], (void *)x86_traps[i - CPUVEC_MIN], 0, |
|---|
| 574 | SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL)); |
|---|
| 575 | idt_bitmap.busy[i] = true; |
|---|
| 576 | } |
|---|
| 577 | |
|---|
| 578 | /* Dynamically configured interrupts */ |
|---|
| 579 | for (i = DYNVEC_MIN; i < DYNVEC_MAX; i++) { |
|---|
| 580 | idt_set_seg(&idt[i], (void *)x86_intrs[i - DYNVEC_MIN], 0, |
|---|
| 581 | SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL)); |
|---|
| 582 | idt_bitmap.busy[i] = true; |
|---|
| 583 | } |
|---|
| 584 | } |
|---|
| 585 | |
|---|
| 586 | void cpu_load_idt() |
|---|
| 587 | { |
|---|
| 588 | struct region_descriptor region; |
|---|
| 589 | setregion(®ion, &idtstore, PAGE_SIZE - 1); |
|---|
| 590 | lidt(®ion); |
|---|
| 591 | } |
|---|
| 592 | |
|---|
| 593 | /* -------------------------------------------------------------------------- */ |
|---|
| 594 | |
|---|
| 595 | int tss_alloc(struct tss *tss, size_t lid) |
|---|
| 596 | { |
|---|
| 597 | int slot; |
|---|
| 598 | |
|---|
| 599 | slot = GDT_CPUTSS_SEL + lid; |
|---|
| 600 | |
|---|
| 601 | gdt_set_sysseg(GDT_ADDR_SYS(gdtstore, slot), tss, |
|---|
| 602 | sizeof(*tss) - 1, SDT_SYS386TSS, SEL_KPL, 0); |
|---|
| 603 | |
|---|
| 604 | return GDT_DYNAM_SEL(slot, SEL_KPL); |
|---|
| 605 | } |
|---|
| 606 | |
|---|
| 607 | void cpu_create_tss(size_t lid) |
|---|
| 608 | { |
|---|
| 609 | percpu_archdata_t *data = &cpudata[lid]; |
|---|
| 610 | struct tss *tss = &data->tss; |
|---|
| 611 | int sel; |
|---|
| 612 | |
|---|
| 613 | /* Create the tss */ |
|---|
| 614 | memset(tss, 0, sizeof(*tss)); |
|---|
| 615 | |
|---|
| 616 | /* tss->tss_rsp0 */ |
|---|
| 617 | tss->tss_ist[0] = (uint64_t)data->intr_stack[lid] + STKSIZE; |
|---|
| 618 | tss->tss_ist[1] = (uint64_t)data->dbfl_stack[lid] + STKSIZE; |
|---|
| 619 | tss->tss_ist[2] = (uint64_t)data->nmfl_stack[lid] + STKSIZE; |
|---|
| 620 | tss->tss_iobase = IOMAP_INVALOFF << 16; |
|---|
| 621 | sel = tss_alloc(tss, lid); |
|---|
| 622 | |
|---|
| 623 | /* Load it */ |
|---|
| 624 | ltr(sel); |
|---|
| 625 | } |
|---|
| 626 | |
|---|
| 627 | /* -------------------------------------------------------------------------- */ |
|---|
| 628 | |
|---|
| 629 | void cpu_tls_init(size_t lid) |
|---|
| 630 | { |
|---|
| 631 | percpu_archdata_t *data = &cpudata[lid]; |
|---|
| 632 | tls_t *cputls = &data->tls; |
|---|
| 633 | |
|---|
| 634 | memset(cputls, 0, sizeof(tls_t)); |
|---|
| 635 | |
|---|
| 636 | cputls->tls_self = cputls; |
|---|
| 637 | cputls->tls_gid = hal_lapic_gid(); |
|---|
| 638 | cputls->tls_lid = lid; |
|---|
| 639 | cputls->tls_intr = INTRS_DISABLED; |
|---|
| 640 | |
|---|
| 641 | wrmsr(MSR_FSBASE, 0); |
|---|
| 642 | wrmsr(MSR_GSBASE, (uint64_t)cputls); |
|---|
| 643 | wrmsr(MSR_KERNELGSBASE, 0); |
|---|
| 644 | } |
|---|
| 645 | |
|---|
| 646 | /* -------------------------------------------------------------------------- */ |
|---|
| 647 | |
|---|
| 648 | uint64_t cpu_features[4] __in_kdata; |
|---|
| 649 | |
|---|
| 650 | void cpu_identify() |
|---|
| 651 | { |
|---|
| 652 | /* |
|---|
| 653 | * desc[0] = eax |
|---|
| 654 | * desc[1] = ebx |
|---|
| 655 | * desc[2] = ecx |
|---|
| 656 | * desc[3] = edx |
|---|
| 657 | */ |
|---|
| 658 | uint32_t desc[4]; |
|---|
| 659 | char vendor[13]; |
|---|
| 660 | size_t lvl; |
|---|
| 661 | |
|---|
| 662 | /* |
|---|
| 663 | * Get information from the standard cpuid leafs |
|---|
| 664 | */ |
|---|
| 665 | cpuid(0, 0, (uint32_t *)&desc); |
|---|
| 666 | |
|---|
| 667 | lvl = (uint64_t)desc[0]; |
|---|
| 668 | x86_printf("-> cpuid standard level: %z\n", lvl); |
|---|
| 669 | |
|---|
| 670 | memcpy(vendor + 0, &desc[1], sizeof(uint32_t)); |
|---|
| 671 | memcpy(vendor + 8, &desc[2], sizeof(uint32_t)); |
|---|
| 672 | memcpy(vendor + 4, &desc[3], sizeof(uint32_t)); |
|---|
| 673 | vendor[12] = '\0'; |
|---|
| 674 | x86_printf("-> CPU vendor: '%s'\n", vendor); |
|---|
| 675 | |
|---|
| 676 | if (lvl >= 1) { |
|---|
| 677 | cpuid(1, 0, (uint32_t *)&desc); |
|---|
| 678 | cpu_features[0] = desc[3]; |
|---|
| 679 | cpu_features[1] = desc[2]; |
|---|
| 680 | } |
|---|
| 681 | |
|---|
| 682 | /* |
|---|
| 683 | * Get information from the extended cpuid leafs |
|---|
| 684 | */ |
|---|
| 685 | cpuid(0x80000000, 0, desc); |
|---|
| 686 | |
|---|
| 687 | lvl = (uint64_t)desc[0]; |
|---|
| 688 | x86_printf("-> cpuid extended level: %Z\n", lvl); |
|---|
| 689 | } |
|---|
| 690 | |
|---|
| 691 | /* -------------------------------------------------------------------------- */ |
|---|
| 692 | |
|---|
| 693 | void cpu_attach(size_t lid) |
|---|
| 694 | { |
|---|
| 695 | /* Per-cluster structures */ |
|---|
| 696 | cpu_load_gdt(); |
|---|
| 697 | cpu_load_idt(); |
|---|
| 698 | |
|---|
| 699 | /* Per-cpu structures */ |
|---|
| 700 | cpu_create_tss(lid); |
|---|
| 701 | |
|---|
| 702 | if (cpu_features[0] & CPUID_PSE) { |
|---|
| 703 | lcr4(rcr4() | CR4_PSE); |
|---|
| 704 | tlbflushg(); |
|---|
| 705 | } else { |
|---|
| 706 | /* |
|---|
| 707 | * amd64 supports PSE by default, if it's not here we have a |
|---|
| 708 | * problem |
|---|
| 709 | */ |
|---|
| 710 | x86_panic("PSE not supported"); |
|---|
| 711 | } |
|---|
| 712 | } |
|---|
| 713 | |
|---|