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