/* * hal_init.c - C initialization procedure for x86. * * Copyright (c) 2017 Maxime Villard * * This file is part of ALMOS-MKH. * * ALMOS-MKH is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by * the Free Software Foundation; version 2.0 of the License. * * ALMOS-MKH is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with ALMOS-MKH.; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include void kernel_init(boot_info_t *info); static void gdt_create(); static void idt_create(); void cpu_tls_init(size_t lid); void cpu_identify(); void cpu_attach(); size_t mytest __in_kdata = 0; struct multiboot_info mb_info __in_kdata; char mb_loader_name[PAGE_SIZE] __in_kdata; uint8_t mb_mmap[PAGE_SIZE] __in_kdata; /* -------------------------------------------------------------------------- */ static void dump_memmap() { size_t mmap_length = mb_info.mi_mmap_length; uint8_t *mmap_addr = (uint8_t *)&mb_mmap; size_t i; if (!(mb_info.mi_flags & MULTIBOOT_INFO_HAS_MMAP)) x86_panic("No mmap"); i = 0; while (i < mmap_length) { struct multiboot_mmap *mm; mm = (struct multiboot_mmap *)(mmap_addr + i); x86_printf("-> [%Z, %Z] %s\n", mm->mm_base_addr, mm->mm_base_addr + mm->mm_length, (mm->mm_type == 1) ? "ram" : "rsv" ); i += mm->mm_size + 4; } } /* -------------------------------------------------------------------------- */ static size_t init_bootinfo_pages_nr() { size_t mmap_length = mb_info.mi_mmap_length; uint8_t *mmap_addr = (uint8_t *)&mb_mmap; paddr_t maxpa, pa; size_t i; i = 0; maxpa = 0; while (i < mmap_length) { struct multiboot_mmap *mm; mm = (struct multiboot_mmap *)(mmap_addr + i); if (mm->mm_type == 1) { pa = mm->mm_base_addr + mm->mm_length; if (pa > maxpa) maxpa = pa; } i += mm->mm_size + 4; } return (maxpa / PAGE_SIZE); } static size_t init_bootinfo_rsvd(boot_rsvd_t *rsvd) { size_t mmap_length = mb_info.mi_mmap_length; uint8_t *mmap_addr = (uint8_t *)&mb_mmap; size_t i, rsvd_nr; memset(rsvd, 0, sizeof(boot_rsvd_t) * CONFIG_PPM_MAX_RSVD); i = 0, rsvd_nr = 0; while (i < mmap_length) { struct multiboot_mmap *mm; mm = (struct multiboot_mmap *)(mmap_addr + i); if (mm->mm_type != 1) { rsvd[rsvd_nr].first_page = rounddown(mm->mm_base_addr, PAGE_SIZE) / PAGE_SIZE; rsvd[rsvd_nr].npages = roundup(mm->mm_length, PAGE_SIZE) / PAGE_SIZE; rsvd_nr++; if (rsvd_nr == CONFIG_PPM_MAX_RSVD) x86_panic("too many memory holes"); } i += mm->mm_size + 4; } return rsvd_nr; } static void init_bootinfo_core(boot_core_t *core) { memset(core, 0, sizeof(boot_core_t)); core->gid = hal_lapic_gid(); core->lid = 0; core->cxy = 0; } static void init_bootinfo_pic(boot_device_t *dev) { memset(dev, 0, sizeof(boot_device_t)); dev->base = 0; dev->type = (DEV_FUNC_PIC << 16) | IMPL_PIC_SCL; dev->channels = 1; dev->param0 = 0; dev->param1 = 0; dev->param2 = 0; dev->param3 = 0; dev->irqs = 5; /* COM1 */ dev->irq[4].dev_type = (DEV_FUNC_TXT << 16) | IMPL_TXT_TTY; dev->irq[4].channel = 0; dev->irq[4].is_rx = 0; dev->irq[4].valid = 1; } static void init_bootinfo_txt(boot_device_t *dev) { memset(dev, 0, sizeof(boot_device_t)); dev->base = 0; dev->type = (DEV_FUNC_TXT << 16) | IMPL_TXT_TTY; dev->channels = 1; dev->param0 = 0; dev->param1 = 0; dev->param2 = 0; dev->param3 = 0; } static void init_bootinfo(boot_info_t *info) { size_t offset; extern uint64_t __kernel_data_start; extern uint64_t __kernel_end; memset(info, 0, sizeof(boot_info_t)); info->signature = 0; info->paddr_width = 0; info->x_width = 1; info->y_width = 1; info->x_size = 1; info->y_size = 1; info->io_cxy = 0; info->ext_dev_nr = 2; init_bootinfo_txt(&info->ext_dev[0]); init_bootinfo_pic(&info->ext_dev[1]); info->cxy = 0; info->cores_nr = 1; init_bootinfo_core(&info->core[0]); info->rsvd_nr = init_bootinfo_rsvd((boot_rsvd_t *)&info->rsvd); /* TODO: dev_icu */ /* TODO: dev_mmc */ /* TODO: dev_dma */ offset = hal_gpt_bootstrap_uniformize(); info->pages_offset = offset / PAGE_SIZE; info->pages_nr = init_bootinfo_pages_nr(); info->kernel_code_start = (intptr_t)(KERNTEXTOFF - KERNBASE); info->kernel_code_end = (intptr_t)(&__kernel_data_start - KERNBASE) - 1; info->kernel_data_start = (intptr_t)(&__kernel_data_start - KERNBASE); info->kernel_code_end = (intptr_t)(&__kernel_end - KERNBASE) - 1; } /* -------------------------------------------------------------------------- */ void init_x86_64(paddr_t firstpa) { boot_info_t btinfo; /* Initialize the serial port */ hal_com_init_early(); x86_printf("[+] init_x86_64 called\n"); /* Create the global structures */ gdt_create(); idt_create(); /* Identify the features of the cpu */ cpu_identify(); /* Attach cpu0 */ cpu_attach(0); x86_printf("[+] cpu_attach called\n"); x86_printf("[+] bootloader: '%s'\n", mb_loader_name); dump_memmap(); x86_printf("[+] dump finished\n"); hal_gpt_init(firstpa); x86_printf("[+] hal_gpt_init called\n"); hal_acpi_init(); x86_printf("[+] hal_acpi_init called\n"); hal_gpt_bootstrap_reset(); x86_printf("[+] hal_gpt_bootstrap_reset called\n"); hal_apic_init(); x86_printf("[+] hal_apic_init called\n"); cpu_tls_init(0); x86_printf("[+] cput_tls_init called\n"); x86_printf("-> mytest = %z\n", mytest); void *hoho = &init_x86_64; xptr_t myptr = XPTR(0, &mytest); hal_remote_spt(myptr, hoho); x86_printf("-> mytest = %Z\n", hal_remote_lpt(myptr)); init_bootinfo(&btinfo); reg_t dummy; hal_enable_irq(&dummy); kernel_init(&btinfo); x86_printf("[+] kernel_init called\n"); /* void *ptr; khm_t *khm = &LOCAL_CLUSTER->khm; ptr = khm_alloc(khm, 10); memset(ptr, 0, 10); khm_free(ptr); kcm_t *kcm = &LOCAL_CLUSTER->kcm; ptr = kcm_alloc(kcm); memset(ptr, 0, 1); kcm_free(ptr); ptr = ppm_alloc_pages(1); ppm_free_pages(ptr); */ while (1); // void x86_stop(); // x86_stop(); } /* -------------------------------------------------------------------------- */ /* x86-specific per-cluster structures */ uint8_t gdtstore[PAGE_SIZE] __in_kdata; uint8_t idtstore[PAGE_SIZE] __in_kdata; /* x86-specific per-cpu structures */ typedef struct { struct tss tss; struct tls tls; uint8_t intr_stack[STKSIZE]; uint8_t dbfl_stack[STKSIZE]; uint8_t nmfl_stack[STKSIZE]; } percpu_archdata_t; percpu_archdata_t cpudata[CONFIG_MAX_LOCAL_CORES] __in_kdata; static void setregion(struct region_descriptor *rd, void *base, uint16_t limit) { rd->rd_limit = limit; rd->rd_base = (uint64_t)base; } /* -------------------------------------------------------------------------- */ static void gdt_set_memseg(struct gdt_memseg *sd, void *base, size_t limit, int type, int dpl, int gran, int is64) { sd->sd_lolimit = (unsigned)limit; sd->sd_lobase = (unsigned long)base; sd->sd_type = type; sd->sd_dpl = dpl; sd->sd_p = 1; sd->sd_hilimit = (unsigned)limit >> 16; sd->sd_avl = 0; sd->sd_long = is64; sd->sd_def32 = 0; sd->sd_gran = gran; sd->sd_hibase = (unsigned long)base >> 24; } static void gdt_set_sysseg(struct gdt_sysseg *sd, void *base, size_t limit, int type, int dpl, int gran) { memset(sd, 0, sizeof *sd); sd->sd_lolimit = (unsigned)limit; sd->sd_lobase = (uint64_t)base; sd->sd_type = type; sd->sd_dpl = dpl; sd->sd_p = 1; sd->sd_hilimit = (unsigned)limit >> 16; sd->sd_gran = gran; sd->sd_hibase = (uint64_t)base >> 24; } static void gdt_create() { memset(&gdtstore, 0, PAGE_SIZE); /* Flat segments */ gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_KCODE_SEL), 0, 0xfffff, SDT_MEMERA, SEL_KPL, 1, 1); gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_KDATA_SEL), 0, 0xfffff, SDT_MEMRWA, SEL_KPL, 1, 1); gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_UCODE_SEL), 0, 0xfffff, SDT_MEMERA, SEL_UPL, 1, 1); gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_UDATA_SEL), 0, 0xfffff, SDT_MEMRWA, SEL_UPL, 1, 1); } void cpu_load_gdt() { struct region_descriptor region; setregion(®ion, &gdtstore, PAGE_SIZE - 1); lgdt(®ion); } /* -------------------------------------------------------------------------- */ static void idt_set_seg(struct idt_seg *seg, void *func, int ist, int type, int dpl, int sel) { seg->gd_looffset = (uint64_t)func & 0xffff; seg->gd_selector = sel; seg->gd_ist = ist; seg->gd_type = type; seg->gd_dpl = dpl; seg->gd_p = 1; seg->gd_hioffset = (uint64_t)func >> 16; seg->gd_zero = 0; seg->gd_xx1 = 0; seg->gd_xx2 = 0; seg->gd_xx3 = 0; } static void idt_create() { extern uint64_t x86_traps[], x86_intrs[], x86_rsvd; struct idt_seg *idt; size_t i; idt = (struct idt_seg *)&idtstore; /* First, put a dead entry */ for (i = 0; i < NIDT; i++) { idt_set_seg(&idt[i], (void *)&x86_rsvd, 0, SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL)); } /* General exceptions */ for (i = CPUVEC_MIN; i < CPUVEC_MAX; i++) { idt_set_seg(&idt[i], (void *)x86_traps[i - CPUVEC_MIN], 0, SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL)); } /* Dynamically configured interrupts */ for (i = DYNVEC_MIN; i < DYNVEC_MAX; i++) { idt_set_seg(&idt[i], (void *)x86_intrs[i - DYNVEC_MIN], 0, SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL)); } } void cpu_load_idt() { struct region_descriptor region; setregion(®ion, &idtstore, PAGE_SIZE - 1); lidt(®ion); } /* -------------------------------------------------------------------------- */ int tss_alloc(struct tss *tss, size_t lid) { int slot; slot = GDT_CPUTSS_SEL + lid; gdt_set_sysseg(GDT_ADDR_SYS(gdtstore, slot), tss, sizeof(*tss) - 1, SDT_SYS386TSS, SEL_KPL, 0); return GDT_DYNAM_SEL(slot, SEL_KPL); } void cpu_create_tss(size_t lid) { percpu_archdata_t *data = &cpudata[lid]; struct tss *tss = &data->tss; int sel; /* Create the tss */ memset(tss, 0, sizeof(*tss)); /* tss->tss_rsp0 */ tss->tss_ist[0] = (uint64_t)data->intr_stack[lid] + STKSIZE; tss->tss_ist[1] = (uint64_t)data->dbfl_stack[lid] + STKSIZE; tss->tss_ist[2] = (uint64_t)data->nmfl_stack[lid] + STKSIZE; tss->tss_iobase = IOMAP_INVALOFF << 16; sel = tss_alloc(tss, lid); /* Load it */ ltr(sel); } /* -------------------------------------------------------------------------- */ void cpu_tls_init(size_t lid) { percpu_archdata_t *data = &cpudata[lid]; tls_t *cputls = &data->tls; memset(cputls, 0, sizeof(tls_t)); cputls->tls_self = cputls; cputls->tls_gid = hal_lapic_gid(); cputls->tls_lid = lid; wrmsr(MSR_FSBASE, 0); wrmsr(MSR_GSBASE, (uint64_t)cputls); wrmsr(MSR_KERNELGSBASE, 0); } /* -------------------------------------------------------------------------- */ uint64_t cpu_features[4] __in_kdata; void cpu_identify() { /* * desc[0] = eax * desc[1] = ebx * desc[2] = ecx * desc[3] = edx */ uint32_t desc[4]; char vendor[13]; size_t lvl; /* * Get information from the standard cpuid leafs */ cpuid(0, 0, (uint32_t *)&desc); lvl = (uint64_t)desc[0]; x86_printf("-> cpuid standard level: %z\n", lvl); memcpy(vendor + 0, &desc[1], sizeof(uint32_t)); memcpy(vendor + 8, &desc[2], sizeof(uint32_t)); memcpy(vendor + 4, &desc[3], sizeof(uint32_t)); vendor[12] = '\0'; x86_printf("-> CPU vendor: '%s'\n", vendor); if (lvl >= 1) { cpuid(1, 0, (uint32_t *)&desc); cpu_features[0] = desc[3]; cpu_features[1] = desc[2]; } /* * Get information from the extended cpuid leafs */ cpuid(0x80000000, 0, desc); lvl = (uint64_t)desc[0]; x86_printf("-> cpuid extended level: %Z\n", lvl); } /* -------------------------------------------------------------------------- */ void cpu_attach(size_t lid) { /* Per-cluster structures */ cpu_load_gdt(); cpu_load_idt(); /* Per-cpu structures */ cpu_create_tss(lid); if (cpu_features[0] & CPUID_PSE) { lcr4(rcr4() | CR4_PSE); tlbflushg(); } else { /* * amd64 supports PSE by default, if it's not here we have a * problem */ x86_panic("PSE not supported"); } }