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