[29] | 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> |
---|
[32] | 24 | #include <hal_multiboot.h> |
---|
[29] | 25 | #include <hal_segmentation.h> |
---|
[45] | 26 | #include <hal_acpi.h> |
---|
[82] | 27 | #include <hal_apic.h> |
---|
[35] | 28 | #include <hal_internal.h> |
---|
[166] | 29 | #include <hal_register.h> |
---|
| 30 | |
---|
[72] | 31 | #include <hal_remote.h> |
---|
[99] | 32 | #include <hal_irqmask.h> |
---|
[29] | 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> |
---|
[70] | 42 | #include <chdev.h> |
---|
[29] | 43 | |
---|
[70] | 44 | #include <boot_info.h> |
---|
| 45 | |
---|
[81] | 46 | void kernel_init(boot_info_t *info); |
---|
| 47 | |
---|
[29] | 48 | static void gdt_create(); |
---|
| 49 | static void idt_create(); |
---|
[168] | 50 | void cpu_tls_init(size_t lid); |
---|
[166] | 51 | void cpu_identify(); |
---|
[29] | 52 | void cpu_attach(); |
---|
| 53 | |
---|
[44] | 54 | size_t mytest __in_kdata = 0; |
---|
[32] | 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 | |
---|
[29] | 60 | /* -------------------------------------------------------------------------- */ |
---|
| 61 | |
---|
[32] | 62 | static void |
---|
| 63 | dump_memmap() |
---|
| 64 | { |
---|
| 65 | size_t mmap_length = mb_info.mi_mmap_length; |
---|
| 66 | uint8_t *mmap_addr = (uint8_t *)&mb_mmap; |
---|
| 67 | size_t i; |
---|
| 68 | |
---|
| 69 | if (!(mb_info.mi_flags & MULTIBOOT_INFO_HAS_MMAP)) |
---|
[46] | 70 | x86_panic("No mmap"); |
---|
[32] | 71 | |
---|
| 72 | i = 0; |
---|
| 73 | while (i < mmap_length) { |
---|
| 74 | struct multiboot_mmap *mm; |
---|
| 75 | |
---|
| 76 | mm = (struct multiboot_mmap *)(mmap_addr + i); |
---|
| 77 | |
---|
| 78 | x86_printf("-> [%Z, %Z] %s\n", mm->mm_base_addr, |
---|
| 79 | mm->mm_base_addr + mm->mm_length, |
---|
| 80 | (mm->mm_type == 1) ? "ram" : "rsv" ); |
---|
| 81 | |
---|
| 82 | i += mm->mm_size + 4; |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | |
---|
[135] | 86 | /* -------------------------------------------------------------------------- */ |
---|
| 87 | |
---|
| 88 | static void init_bootinfo_icu(boot_device_t *dev) |
---|
| 89 | { |
---|
[137] | 90 | extern uint32_t hwi_baseidx; |
---|
| 91 | extern uint32_t wti_baseidx; |
---|
| 92 | extern uint32_t pti_baseidx; |
---|
| 93 | extern size_t ioapic_pins; |
---|
| 94 | |
---|
[135] | 95 | memset(dev, 0, sizeof(boot_device_t)); |
---|
| 96 | |
---|
[137] | 97 | dev->base = 0; |
---|
[135] | 98 | dev->type = (DEV_FUNC_ICU << 16) | IMPL_ICU_XCU; |
---|
| 99 | dev->channels = 1; |
---|
[137] | 100 | |
---|
[138] | 101 | #if NOTYET |
---|
[137] | 102 | /* |
---|
| 103 | * Give 20% of the pins to HWI, 80% to WTI. |
---|
| 104 | */ |
---|
| 105 | dev->param0 = (ioapic_pins * 20) / 100; /* hwi_nr */ |
---|
| 106 | dev->param1 = ioapic_pins - dev->param0; /* wti_nr */ |
---|
[138] | 107 | #else |
---|
| 108 | dev->param0 = 1; /* hwi_nr */ |
---|
| 109 | dev->param1 = 1; /* wti_nr */ |
---|
| 110 | #endif |
---|
[137] | 111 | |
---|
| 112 | /* |
---|
| 113 | * We always set 1 for pti_nr. On x86, timer interrupts are handled by |
---|
| 114 | * LAPIC, which is per-cpu and not global. Therefore, we always have one |
---|
| 115 | * timer for each CPU, and its IRQ number is faked to 0. |
---|
| 116 | */ |
---|
| 117 | dev->param2 = 1; /* pti_nr */ |
---|
| 118 | |
---|
[135] | 119 | dev->param3 = 0; |
---|
| 120 | |
---|
[137] | 121 | /* Set the base idx for the XCU driver */ |
---|
| 122 | hwi_baseidx = 0; |
---|
| 123 | wti_baseidx = dev->param0; |
---|
| 124 | pti_baseidx = 0xFFFFFFFF; |
---|
| 125 | |
---|
[135] | 126 | #ifdef NOTYET |
---|
| 127 | uint32_t irqs; /*! number of input IRQs */ |
---|
| 128 | boot_irq_t irq[32]; /*! array of input IRQS (PIC and ICU only) */ |
---|
| 129 | #endif |
---|
| 130 | } |
---|
| 131 | |
---|
[119] | 132 | static size_t init_bootinfo_pages_nr() |
---|
| 133 | { |
---|
| 134 | size_t mmap_length = mb_info.mi_mmap_length; |
---|
| 135 | uint8_t *mmap_addr = (uint8_t *)&mb_mmap; |
---|
| 136 | paddr_t maxpa, pa; |
---|
| 137 | size_t i; |
---|
| 138 | |
---|
| 139 | i = 0; |
---|
| 140 | maxpa = 0; |
---|
| 141 | while (i < mmap_length) { |
---|
| 142 | struct multiboot_mmap *mm; |
---|
| 143 | |
---|
| 144 | mm = (struct multiboot_mmap *)(mmap_addr + i); |
---|
| 145 | |
---|
| 146 | if (mm->mm_type == 1) { |
---|
| 147 | pa = mm->mm_base_addr + mm->mm_length; |
---|
| 148 | if (pa > maxpa) |
---|
| 149 | maxpa = pa; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | i += mm->mm_size + 4; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | return (maxpa / PAGE_SIZE); |
---|
| 156 | } |
---|
| 157 | |
---|
[116] | 158 | static size_t init_bootinfo_rsvd(boot_rsvd_t *rsvd) |
---|
| 159 | { |
---|
| 160 | size_t mmap_length = mb_info.mi_mmap_length; |
---|
| 161 | uint8_t *mmap_addr = (uint8_t *)&mb_mmap; |
---|
| 162 | size_t i, rsvd_nr; |
---|
| 163 | |
---|
[137] | 164 | memset(rsvd, 0, sizeof(boot_rsvd_t) * CONFIG_PPM_MAX_RSVD); |
---|
[116] | 165 | |
---|
| 166 | i = 0, rsvd_nr = 0; |
---|
| 167 | while (i < mmap_length) { |
---|
| 168 | struct multiboot_mmap *mm; |
---|
| 169 | |
---|
| 170 | mm = (struct multiboot_mmap *)(mmap_addr + i); |
---|
| 171 | |
---|
[119] | 172 | if (mm->mm_type != 1) { |
---|
| 173 | rsvd[rsvd_nr].first_page = |
---|
| 174 | rounddown(mm->mm_base_addr, PAGE_SIZE) / PAGE_SIZE; |
---|
| 175 | rsvd[rsvd_nr].npages = |
---|
| 176 | roundup(mm->mm_length, PAGE_SIZE) / PAGE_SIZE; |
---|
| 177 | rsvd_nr++; |
---|
| 178 | if (rsvd_nr == CONFIG_PPM_MAX_RSVD) |
---|
| 179 | x86_panic("too many memory holes"); |
---|
| 180 | } |
---|
[116] | 181 | |
---|
| 182 | i += mm->mm_size + 4; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | return rsvd_nr; |
---|
| 186 | } |
---|
| 187 | |
---|
[70] | 188 | static void init_bootinfo_core(boot_core_t *core) |
---|
| 189 | { |
---|
| 190 | memset(core, 0, sizeof(boot_core_t)); |
---|
| 191 | |
---|
| 192 | core->gid = hal_lapic_gid(); |
---|
| 193 | core->lid = 0; |
---|
| 194 | core->cxy = 0; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | static void init_bootinfo_txt(boot_device_t *dev) |
---|
| 198 | { |
---|
| 199 | memset(dev, 0, sizeof(boot_device_t)); |
---|
| 200 | |
---|
| 201 | dev->base = 0xB8000; |
---|
| 202 | dev->type = (DEV_FUNC_TXT << 16) | IMPL_TXT_X86; |
---|
| 203 | dev->channels = 1; |
---|
| 204 | dev->param0 = 0; |
---|
| 205 | dev->param1 = 0; |
---|
| 206 | dev->param2 = 0; |
---|
| 207 | dev->param3 = 0; |
---|
| 208 | |
---|
| 209 | #ifdef NOTYET |
---|
| 210 | uint32_t irqs; /*! number of input IRQs */ |
---|
| 211 | boot_irq_t irq[32]; /*! array of input IRQS (PIC and ICU only) */ |
---|
| 212 | #endif |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | static void init_bootinfo(boot_info_t *info) |
---|
| 216 | { |
---|
[116] | 217 | size_t offset; |
---|
[114] | 218 | |
---|
[70] | 219 | extern uint64_t __kernel_data_start; |
---|
| 220 | extern uint64_t __kernel_end; |
---|
| 221 | |
---|
| 222 | memset(info, 0, sizeof(boot_info_t)); |
---|
| 223 | |
---|
| 224 | info->signature = 0; |
---|
| 225 | |
---|
| 226 | info->paddr_width = 0; |
---|
| 227 | info->x_width = 1; |
---|
| 228 | info->y_width = 1; |
---|
| 229 | info->x_size = 1; |
---|
| 230 | info->y_size = 1; |
---|
| 231 | info->io_cxy = 0; |
---|
| 232 | |
---|
| 233 | info->ext_dev_nr = 1; |
---|
| 234 | init_bootinfo_txt(&info->ext_dev[0]); |
---|
| 235 | |
---|
| 236 | info->cxy = 0; |
---|
| 237 | info->cores_nr = 1; |
---|
| 238 | init_bootinfo_core(&info->core[0]); |
---|
| 239 | |
---|
[137] | 240 | info->rsvd_nr = init_bootinfo_rsvd((boot_rsvd_t *)&info->rsvd); |
---|
[70] | 241 | |
---|
[135] | 242 | init_bootinfo_icu(&info->dev_icu); |
---|
| 243 | /* TODO: dev_mmc */ |
---|
| 244 | /* TODO: dev_dma */ |
---|
| 245 | |
---|
[116] | 246 | offset = hal_gpt_bootstrap_uniformize(); |
---|
| 247 | info->pages_offset = offset / PAGE_SIZE; |
---|
[119] | 248 | info->pages_nr = init_bootinfo_pages_nr(); |
---|
[70] | 249 | |
---|
| 250 | info->kernel_code_start = (intptr_t)(KERNTEXTOFF - KERNBASE); |
---|
| 251 | info->kernel_code_end = (intptr_t)(&__kernel_data_start - KERNBASE) - 1; |
---|
| 252 | info->kernel_data_start = (intptr_t)(&__kernel_data_start - KERNBASE); |
---|
| 253 | info->kernel_code_end = (intptr_t)(&__kernel_end - KERNBASE) - 1; |
---|
| 254 | } |
---|
| 255 | |
---|
[166] | 256 | /* -------------------------------------------------------------------------- */ |
---|
| 257 | |
---|
[29] | 258 | void init_x86_64(paddr_t firstpa) |
---|
| 259 | { |
---|
[70] | 260 | boot_info_t btinfo; |
---|
| 261 | |
---|
[154] | 262 | /* Initialize the serial port */ |
---|
| 263 | hal_com_init_early(); |
---|
| 264 | |
---|
[29] | 265 | x86_printf("[+] init_x86_64 called\n"); |
---|
| 266 | |
---|
| 267 | /* Create the global structures */ |
---|
| 268 | gdt_create(); |
---|
| 269 | idt_create(); |
---|
| 270 | |
---|
[166] | 271 | /* Identify the features of the cpu */ |
---|
| 272 | cpu_identify(); |
---|
| 273 | |
---|
[29] | 274 | /* Attach cpu0 */ |
---|
[162] | 275 | cpu_attach(0); |
---|
[29] | 276 | x86_printf("[+] cpu_attach called\n"); |
---|
| 277 | |
---|
[47] | 278 | x86_printf("[+] bootloader: '%s'\n", mb_loader_name); |
---|
[32] | 279 | |
---|
| 280 | dump_memmap(); |
---|
| 281 | x86_printf("[+] dump finished\n"); |
---|
| 282 | |
---|
[35] | 283 | hal_gpt_init(firstpa); |
---|
| 284 | x86_printf("[+] hal_gpt_init called\n"); |
---|
[29] | 285 | |
---|
[35] | 286 | hal_acpi_init(); |
---|
| 287 | x86_printf("[+] hal_acpi_init called\n"); |
---|
| 288 | |
---|
[45] | 289 | hal_gpt_bootstrap_reset(); |
---|
| 290 | x86_printf("[+] hal_gpt_bootstrap_reset called\n"); |
---|
| 291 | |
---|
[82] | 292 | hal_apic_init(); |
---|
| 293 | x86_printf("[+] hal_apic_init called\n"); |
---|
[45] | 294 | |
---|
[168] | 295 | cpu_tls_init(0); |
---|
| 296 | x86_printf("[+] cput_tls_init called\n"); |
---|
[46] | 297 | |
---|
[44] | 298 | x86_printf("-> mytest = %z\n", mytest); |
---|
[94] | 299 | void *hoho = &init_x86_64; |
---|
[74] | 300 | xptr_t myptr = XPTR(0, &mytest); |
---|
[35] | 301 | |
---|
[94] | 302 | hal_remote_spt(myptr, hoho); |
---|
| 303 | x86_printf("-> mytest = %Z\n", hal_remote_lpt(myptr)); |
---|
| 304 | |
---|
[99] | 305 | init_bootinfo(&btinfo); |
---|
[94] | 306 | |
---|
[99] | 307 | reg_t dummy; |
---|
| 308 | hal_enable_irq(&dummy); |
---|
| 309 | |
---|
[152] | 310 | while (1); |
---|
| 311 | |
---|
[70] | 312 | kernel_init(&btinfo); |
---|
[99] | 313 | |
---|
[70] | 314 | x86_printf("[+] kernel_init called\n"); |
---|
| 315 | |
---|
[99] | 316 | while (1); |
---|
| 317 | |
---|
[82] | 318 | // void x86_stop(); |
---|
| 319 | // x86_stop(); |
---|
[29] | 320 | } |
---|
| 321 | |
---|
| 322 | /* -------------------------------------------------------------------------- */ |
---|
| 323 | |
---|
[165] | 324 | /* x86-specific per-cluster structures */ |
---|
[29] | 325 | uint8_t gdtstore[PAGE_SIZE] __in_kdata; |
---|
| 326 | uint8_t idtstore[PAGE_SIZE] __in_kdata; |
---|
| 327 | |
---|
[165] | 328 | /* x86-specific per-cpu structures */ |
---|
| 329 | typedef struct { |
---|
| 330 | struct tss tss; |
---|
[168] | 331 | struct tls tls; |
---|
[165] | 332 | uint8_t intr_stack[STKSIZE]; |
---|
| 333 | uint8_t dbfl_stack[STKSIZE]; |
---|
| 334 | uint8_t nmfl_stack[STKSIZE]; |
---|
| 335 | } percpu_archdata_t; |
---|
| 336 | percpu_archdata_t cpudata[CONFIG_MAX_LOCAL_CORES] __in_kdata; |
---|
| 337 | |
---|
[29] | 338 | static void |
---|
| 339 | setregion(struct region_descriptor *rd, void *base, uint16_t limit) |
---|
| 340 | { |
---|
| 341 | rd->rd_limit = limit; |
---|
| 342 | rd->rd_base = (uint64_t)base; |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | /* -------------------------------------------------------------------------- */ |
---|
| 346 | |
---|
| 347 | static void |
---|
| 348 | gdt_set_memseg(struct gdt_memseg *sd, void *base, size_t limit, |
---|
| 349 | int type, int dpl, int gran, int is64) |
---|
| 350 | { |
---|
| 351 | sd->sd_lolimit = (unsigned)limit; |
---|
| 352 | sd->sd_lobase = (unsigned long)base; |
---|
| 353 | sd->sd_type = type; |
---|
| 354 | sd->sd_dpl = dpl; |
---|
| 355 | sd->sd_p = 1; |
---|
| 356 | sd->sd_hilimit = (unsigned)limit >> 16; |
---|
| 357 | sd->sd_avl = 0; |
---|
| 358 | sd->sd_long = is64; |
---|
| 359 | sd->sd_def32 = 0; |
---|
| 360 | sd->sd_gran = gran; |
---|
| 361 | sd->sd_hibase = (unsigned long)base >> 24; |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | static void |
---|
| 365 | gdt_set_sysseg(struct gdt_sysseg *sd, void *base, size_t limit, |
---|
| 366 | int type, int dpl, int gran) |
---|
| 367 | { |
---|
| 368 | memset(sd, 0, sizeof *sd); |
---|
| 369 | sd->sd_lolimit = (unsigned)limit; |
---|
| 370 | sd->sd_lobase = (uint64_t)base; |
---|
| 371 | sd->sd_type = type; |
---|
| 372 | sd->sd_dpl = dpl; |
---|
| 373 | sd->sd_p = 1; |
---|
| 374 | sd->sd_hilimit = (unsigned)limit >> 16; |
---|
| 375 | sd->sd_gran = gran; |
---|
| 376 | sd->sd_hibase = (uint64_t)base >> 24; |
---|
| 377 | } |
---|
| 378 | |
---|
| 379 | static void gdt_create() |
---|
| 380 | { |
---|
| 381 | memset(&gdtstore, 0, PAGE_SIZE); |
---|
| 382 | |
---|
| 383 | /* Flat segments */ |
---|
| 384 | gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_KCODE_SEL), 0, |
---|
| 385 | 0xfffff, SDT_MEMERA, SEL_KPL, 1, 1); |
---|
| 386 | gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_KDATA_SEL), 0, |
---|
| 387 | 0xfffff, SDT_MEMRWA, SEL_KPL, 1, 1); |
---|
| 388 | gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_UCODE_SEL), 0, |
---|
| 389 | 0xfffff, SDT_MEMERA, SEL_UPL, 1, 1); |
---|
| 390 | gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_UDATA_SEL), 0, |
---|
| 391 | 0xfffff, SDT_MEMRWA, SEL_UPL, 1, 1); |
---|
| 392 | } |
---|
| 393 | |
---|
| 394 | void cpu_load_gdt() |
---|
| 395 | { |
---|
| 396 | struct region_descriptor region; |
---|
| 397 | setregion(®ion, &gdtstore, PAGE_SIZE - 1); |
---|
| 398 | lgdt(®ion); |
---|
| 399 | } |
---|
| 400 | |
---|
| 401 | /* -------------------------------------------------------------------------- */ |
---|
| 402 | |
---|
| 403 | static void |
---|
| 404 | idt_set_seg(struct idt_seg *seg, void *func, int ist, int type, int dpl, int sel) |
---|
| 405 | { |
---|
| 406 | seg->gd_looffset = (uint64_t)func & 0xffff; |
---|
| 407 | seg->gd_selector = sel; |
---|
| 408 | seg->gd_ist = ist; |
---|
| 409 | seg->gd_type = type; |
---|
| 410 | seg->gd_dpl = dpl; |
---|
| 411 | seg->gd_p = 1; |
---|
| 412 | seg->gd_hioffset = (uint64_t)func >> 16; |
---|
| 413 | seg->gd_zero = 0; |
---|
| 414 | seg->gd_xx1 = 0; |
---|
| 415 | seg->gd_xx2 = 0; |
---|
| 416 | seg->gd_xx3 = 0; |
---|
| 417 | } |
---|
| 418 | |
---|
| 419 | static void idt_create() |
---|
| 420 | { |
---|
[80] | 421 | extern uint64_t x86_traps[], x86_intrs[], x86_rsvd; |
---|
[29] | 422 | struct idt_seg *idt; |
---|
| 423 | size_t i; |
---|
| 424 | |
---|
| 425 | idt = (struct idt_seg *)&idtstore; |
---|
[45] | 426 | |
---|
[80] | 427 | /* First, put a dead entry */ |
---|
| 428 | for (i = 0; i < NIDT; i++) { |
---|
| 429 | idt_set_seg(&idt[i], (void *)&x86_rsvd, 0, |
---|
| 430 | SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL)); |
---|
| 431 | } |
---|
| 432 | |
---|
[45] | 433 | /* General exceptions */ |
---|
| 434 | for (i = CPUVEC_MIN; i < CPUVEC_MAX; i++) { |
---|
| 435 | idt_set_seg(&idt[i], (void *)x86_traps[i - CPUVEC_MIN], 0, |
---|
| 436 | SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL)); |
---|
[29] | 437 | } |
---|
[45] | 438 | |
---|
[138] | 439 | /* Dynamically configured interrupts */ |
---|
| 440 | for (i = DYNVEC_MIN; i < DYNVEC_MAX; i++) { |
---|
| 441 | idt_set_seg(&idt[i], (void *)x86_intrs[i - DYNVEC_MIN], 0, |
---|
[45] | 442 | SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL)); |
---|
| 443 | } |
---|
[29] | 444 | } |
---|
| 445 | |
---|
| 446 | void cpu_load_idt() |
---|
| 447 | { |
---|
| 448 | struct region_descriptor region; |
---|
| 449 | setregion(®ion, &idtstore, PAGE_SIZE - 1); |
---|
| 450 | lidt(®ion); |
---|
| 451 | } |
---|
| 452 | |
---|
| 453 | /* -------------------------------------------------------------------------- */ |
---|
| 454 | |
---|
[164] | 455 | int tss_alloc(struct tss *tss, size_t lid) |
---|
[29] | 456 | { |
---|
| 457 | int slot; |
---|
| 458 | |
---|
[164] | 459 | slot = GDT_CPUTSS_SEL + lid; |
---|
[29] | 460 | |
---|
| 461 | gdt_set_sysseg(GDT_ADDR_SYS(gdtstore, slot), tss, |
---|
| 462 | sizeof(*tss) - 1, SDT_SYS386TSS, SEL_KPL, 0); |
---|
| 463 | |
---|
| 464 | return GDT_DYNAM_SEL(slot, SEL_KPL); |
---|
| 465 | } |
---|
| 466 | |
---|
[162] | 467 | void cpu_create_tss(size_t lid) |
---|
[29] | 468 | { |
---|
[165] | 469 | percpu_archdata_t *data = &cpudata[lid]; |
---|
| 470 | struct tss *tss = &data->tss; |
---|
[29] | 471 | int sel; |
---|
| 472 | |
---|
| 473 | /* Create the tss */ |
---|
| 474 | memset(tss, 0, sizeof(*tss)); |
---|
[162] | 475 | |
---|
| 476 | /* tss->tss_rsp0 */ |
---|
[165] | 477 | tss->tss_ist[0] = (uint64_t)data->intr_stack[lid] + STKSIZE; |
---|
| 478 | tss->tss_ist[1] = (uint64_t)data->dbfl_stack[lid] + STKSIZE; |
---|
| 479 | tss->tss_ist[2] = (uint64_t)data->nmfl_stack[lid] + STKSIZE; |
---|
[29] | 480 | tss->tss_iobase = IOMAP_INVALOFF << 16; |
---|
[164] | 481 | sel = tss_alloc(tss, lid); |
---|
[29] | 482 | |
---|
| 483 | /* Load it */ |
---|
| 484 | ltr(sel); |
---|
| 485 | } |
---|
| 486 | |
---|
| 487 | /* -------------------------------------------------------------------------- */ |
---|
| 488 | |
---|
[168] | 489 | void cpu_tls_init(size_t lid) |
---|
| 490 | { |
---|
| 491 | percpu_archdata_t *data = &cpudata[lid]; |
---|
| 492 | tls_t *cputls = &data->tls; |
---|
| 493 | |
---|
| 494 | memset(cputls, 0, sizeof(tls_t)); |
---|
| 495 | |
---|
| 496 | cputls->tls_self = cputls; |
---|
| 497 | cputls->tls_gid = hal_lapic_gid(); |
---|
| 498 | cputls->tls_lid = lid; |
---|
| 499 | |
---|
| 500 | wrmsr(MSR_FSBASE, 0); |
---|
| 501 | wrmsr(MSR_GSBASE, (uint64_t)cputls); |
---|
| 502 | wrmsr(MSR_KERNELGSBASE, 0); |
---|
| 503 | } |
---|
| 504 | |
---|
| 505 | /* -------------------------------------------------------------------------- */ |
---|
| 506 | |
---|
[166] | 507 | uint64_t cpu_features[4] __in_kdata; |
---|
| 508 | |
---|
| 509 | void cpu_identify() |
---|
| 510 | { |
---|
| 511 | /* |
---|
| 512 | * desc[0] = eax |
---|
| 513 | * desc[1] = ebx |
---|
| 514 | * desc[2] = ecx |
---|
| 515 | * desc[3] = edx |
---|
| 516 | */ |
---|
| 517 | uint32_t desc[4]; |
---|
| 518 | char vendor[13]; |
---|
| 519 | size_t lvl; |
---|
| 520 | |
---|
| 521 | /* |
---|
| 522 | * Get information from the standard cpuid leafs |
---|
| 523 | */ |
---|
| 524 | cpuid(0, 0, (uint32_t *)&desc); |
---|
| 525 | |
---|
| 526 | lvl = (uint64_t)desc[0]; |
---|
| 527 | x86_printf("-> cpuid standard level: %z\n", lvl); |
---|
| 528 | |
---|
| 529 | memcpy(vendor + 0, &desc[1], sizeof(uint32_t)); |
---|
| 530 | memcpy(vendor + 8, &desc[2], sizeof(uint32_t)); |
---|
| 531 | memcpy(vendor + 4, &desc[3], sizeof(uint32_t)); |
---|
| 532 | vendor[12] = '\0'; |
---|
| 533 | x86_printf("-> CPU vendor: '%s'\n", vendor); |
---|
| 534 | |
---|
| 535 | if (lvl >= 1) { |
---|
| 536 | cpuid(1, 0, (uint32_t *)&desc); |
---|
| 537 | cpu_features[0] = desc[3]; |
---|
| 538 | cpu_features[1] = desc[2]; |
---|
| 539 | } |
---|
| 540 | |
---|
| 541 | /* |
---|
| 542 | * Get information from the extended cpuid leafs |
---|
| 543 | */ |
---|
| 544 | cpuid(0x80000000, 0, desc); |
---|
| 545 | |
---|
| 546 | lvl = (uint64_t)desc[0]; |
---|
| 547 | x86_printf("-> cpuid extended level: %Z\n", lvl); |
---|
| 548 | } |
---|
| 549 | |
---|
| 550 | /* -------------------------------------------------------------------------- */ |
---|
| 551 | |
---|
[162] | 552 | void cpu_attach(size_t lid) |
---|
[29] | 553 | { |
---|
[168] | 554 | /* Per-cluster structures */ |
---|
[29] | 555 | cpu_load_gdt(); |
---|
| 556 | cpu_load_idt(); |
---|
[168] | 557 | |
---|
| 558 | /* Per-cpu structures */ |
---|
[162] | 559 | cpu_create_tss(lid); |
---|
[166] | 560 | |
---|
| 561 | if (cpu_features[0] & CPUID_PSE) { |
---|
| 562 | lcr4(rcr4() | CR4_PSE); |
---|
| 563 | tlbflushg(); |
---|
| 564 | } else { |
---|
| 565 | /* |
---|
| 566 | * amd64 supports PSE by default, if it's not here we have a |
---|
| 567 | * problem |
---|
| 568 | */ |
---|
| 569 | x86_panic("PSE not supported"); |
---|
| 570 | } |
---|
[29] | 571 | } |
---|
| 572 | |
---|