[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> |
---|
[72] | 29 | #include <hal_remote.h> |
---|
[99] | 30 | #include <hal_irqmask.h> |
---|
[29] | 31 | |
---|
| 32 | #include <memcpy.h> |
---|
| 33 | #include <thread.h> |
---|
| 34 | #include <string.h> |
---|
| 35 | #include <process.h> |
---|
| 36 | #include <printk.h> |
---|
| 37 | #include <vmm.h> |
---|
| 38 | #include <core.h> |
---|
| 39 | #include <cluster.h> |
---|
[70] | 40 | #include <chdev.h> |
---|
[29] | 41 | |
---|
[70] | 42 | #include <boot_info.h> |
---|
| 43 | |
---|
[81] | 44 | void kernel_init(boot_info_t *info); |
---|
| 45 | |
---|
[29] | 46 | static void gdt_create(); |
---|
| 47 | static void idt_create(); |
---|
| 48 | void cpu_attach(); |
---|
| 49 | |
---|
[44] | 50 | size_t mytest __in_kdata = 0; |
---|
[32] | 51 | |
---|
| 52 | struct multiboot_info mb_info __in_kdata; |
---|
| 53 | char mb_loader_name[PAGE_SIZE] __in_kdata; |
---|
| 54 | uint8_t mb_mmap[PAGE_SIZE] __in_kdata; |
---|
| 55 | |
---|
[29] | 56 | /* -------------------------------------------------------------------------- */ |
---|
| 57 | |
---|
[32] | 58 | static void |
---|
| 59 | dump_memmap() |
---|
| 60 | { |
---|
| 61 | size_t mmap_length = mb_info.mi_mmap_length; |
---|
| 62 | uint8_t *mmap_addr = (uint8_t *)&mb_mmap; |
---|
| 63 | size_t i; |
---|
| 64 | |
---|
| 65 | if (!(mb_info.mi_flags & MULTIBOOT_INFO_HAS_MMAP)) |
---|
[46] | 66 | x86_panic("No mmap"); |
---|
[32] | 67 | |
---|
| 68 | i = 0; |
---|
| 69 | while (i < mmap_length) { |
---|
| 70 | struct multiboot_mmap *mm; |
---|
| 71 | |
---|
| 72 | mm = (struct multiboot_mmap *)(mmap_addr + i); |
---|
| 73 | |
---|
| 74 | x86_printf("-> [%Z, %Z] %s\n", mm->mm_base_addr, |
---|
| 75 | mm->mm_base_addr + mm->mm_length, |
---|
| 76 | (mm->mm_type == 1) ? "ram" : "rsv" ); |
---|
| 77 | |
---|
| 78 | i += mm->mm_size + 4; |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | |
---|
[70] | 82 | static void init_bootinfo_core(boot_core_t *core) |
---|
| 83 | { |
---|
| 84 | memset(core, 0, sizeof(boot_core_t)); |
---|
| 85 | |
---|
| 86 | core->gid = hal_lapic_gid(); |
---|
| 87 | core->lid = 0; |
---|
| 88 | core->cxy = 0; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | static void init_bootinfo_txt(boot_device_t *dev) |
---|
| 92 | { |
---|
| 93 | memset(dev, 0, sizeof(boot_device_t)); |
---|
| 94 | |
---|
| 95 | dev->base = 0xB8000; |
---|
| 96 | dev->type = (DEV_FUNC_TXT << 16) | IMPL_TXT_X86; |
---|
| 97 | dev->channels = 1; |
---|
| 98 | dev->param0 = 0; |
---|
| 99 | dev->param1 = 0; |
---|
| 100 | dev->param2 = 0; |
---|
| 101 | dev->param3 = 0; |
---|
| 102 | |
---|
| 103 | #ifdef NOTYET |
---|
| 104 | uint32_t irqs; /*! number of input IRQs */ |
---|
| 105 | boot_irq_t irq[32]; /*! array of input IRQS (PIC and ICU only) */ |
---|
| 106 | #endif |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | static void init_bootinfo(boot_info_t *info) |
---|
| 110 | { |
---|
| 111 | extern uint64_t __kernel_data_start; |
---|
| 112 | extern uint64_t __kernel_end; |
---|
| 113 | |
---|
| 114 | memset(info, 0, sizeof(boot_info_t)); |
---|
| 115 | |
---|
| 116 | info->signature = 0; |
---|
| 117 | |
---|
| 118 | info->paddr_width = 0; |
---|
| 119 | info->x_width = 1; |
---|
| 120 | info->y_width = 1; |
---|
| 121 | info->x_size = 1; |
---|
| 122 | info->y_size = 1; |
---|
| 123 | info->io_cxy = 0; |
---|
| 124 | |
---|
| 125 | info->ext_dev_nr = 1; |
---|
| 126 | init_bootinfo_txt(&info->ext_dev[0]); |
---|
| 127 | |
---|
| 128 | info->cxy = 0; |
---|
| 129 | info->cores_nr = 1; |
---|
| 130 | init_bootinfo_core(&info->core[0]); |
---|
| 131 | |
---|
| 132 | info->rsvd_nr = 0; |
---|
| 133 | /* rsvd XXX */ |
---|
| 134 | |
---|
| 135 | /* dev_ XXX */ |
---|
| 136 | |
---|
| 137 | info->pages_offset = 0; |
---|
| 138 | info->pages_nr = 0; |
---|
| 139 | |
---|
| 140 | info->kernel_code_start = (intptr_t)(KERNTEXTOFF - KERNBASE); |
---|
| 141 | info->kernel_code_end = (intptr_t)(&__kernel_data_start - KERNBASE) - 1; |
---|
| 142 | info->kernel_data_start = (intptr_t)(&__kernel_data_start - KERNBASE); |
---|
| 143 | info->kernel_code_end = (intptr_t)(&__kernel_end - KERNBASE) - 1; |
---|
| 144 | } |
---|
| 145 | |
---|
[29] | 146 | void init_x86_64(paddr_t firstpa) |
---|
| 147 | { |
---|
[70] | 148 | boot_info_t btinfo; |
---|
| 149 | |
---|
[29] | 150 | x86_printf("[+] init_x86_64 called\n"); |
---|
| 151 | |
---|
| 152 | /* Create the global structures */ |
---|
| 153 | gdt_create(); |
---|
| 154 | idt_create(); |
---|
| 155 | |
---|
| 156 | /* Attach cpu0 */ |
---|
| 157 | cpu_attach(); |
---|
| 158 | x86_printf("[+] cpu_attach called\n"); |
---|
| 159 | |
---|
[47] | 160 | x86_printf("[+] bootloader: '%s'\n", mb_loader_name); |
---|
[32] | 161 | |
---|
| 162 | dump_memmap(); |
---|
| 163 | x86_printf("[+] dump finished\n"); |
---|
| 164 | |
---|
[35] | 165 | hal_gpt_init(firstpa); |
---|
| 166 | x86_printf("[+] hal_gpt_init called\n"); |
---|
[29] | 167 | |
---|
[35] | 168 | hal_acpi_init(); |
---|
| 169 | x86_printf("[+] hal_acpi_init called\n"); |
---|
| 170 | |
---|
[45] | 171 | hal_gpt_bootstrap_reset(); |
---|
| 172 | x86_printf("[+] hal_gpt_bootstrap_reset called\n"); |
---|
| 173 | |
---|
[82] | 174 | hal_apic_init(); |
---|
| 175 | x86_printf("[+] hal_apic_init called\n"); |
---|
[45] | 176 | |
---|
[46] | 177 | hal_tls_init_cpu0(); |
---|
| 178 | x86_printf("[+] hal_tls_init_cpu0 called\n"); |
---|
| 179 | |
---|
[44] | 180 | x86_printf("-> mytest = %z\n", mytest); |
---|
[94] | 181 | void *hoho = &init_x86_64; |
---|
[74] | 182 | xptr_t myptr = XPTR(0, &mytest); |
---|
[35] | 183 | |
---|
[94] | 184 | hal_remote_spt(myptr, hoho); |
---|
| 185 | x86_printf("-> mytest = %Z\n", hal_remote_lpt(myptr)); |
---|
| 186 | |
---|
[99] | 187 | init_bootinfo(&btinfo); |
---|
[94] | 188 | |
---|
[99] | 189 | reg_t dummy; |
---|
| 190 | hal_enable_irq(&dummy); |
---|
| 191 | |
---|
[70] | 192 | kernel_init(&btinfo); |
---|
[99] | 193 | |
---|
[70] | 194 | x86_printf("[+] kernel_init called\n"); |
---|
| 195 | |
---|
[99] | 196 | while (1); |
---|
| 197 | |
---|
[82] | 198 | // void x86_stop(); |
---|
| 199 | // x86_stop(); |
---|
[80] | 200 | |
---|
[29] | 201 | int m = 0; |
---|
| 202 | int v = 1 / m; |
---|
| 203 | |
---|
[35] | 204 | char *buf = NULL; |
---|
| 205 | *buf = (char)0x01; |
---|
| 206 | |
---|
[29] | 207 | x86_printf("ALIVE!\n"); |
---|
| 208 | |
---|
| 209 | while (1); |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | /* -------------------------------------------------------------------------- */ |
---|
| 213 | |
---|
| 214 | uint8_t gdtstore[PAGE_SIZE] __in_kdata; |
---|
| 215 | uint8_t idtstore[PAGE_SIZE] __in_kdata; |
---|
| 216 | struct tss cpu0_tss __in_kdata; |
---|
| 217 | uint8_t cpu0_intr_stack[STKSIZE] __in_kdata; |
---|
| 218 | uint8_t cpu0_dbfl_stack[STKSIZE] __in_kdata; |
---|
| 219 | uint8_t cpu0_nmfl_stack[STKSIZE] __in_kdata; |
---|
| 220 | |
---|
| 221 | static void |
---|
| 222 | setregion(struct region_descriptor *rd, void *base, uint16_t limit) |
---|
| 223 | { |
---|
| 224 | rd->rd_limit = limit; |
---|
| 225 | rd->rd_base = (uint64_t)base; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | /* -------------------------------------------------------------------------- */ |
---|
| 229 | |
---|
| 230 | static void |
---|
| 231 | gdt_set_memseg(struct gdt_memseg *sd, void *base, size_t limit, |
---|
| 232 | int type, int dpl, int gran, int is64) |
---|
| 233 | { |
---|
| 234 | sd->sd_lolimit = (unsigned)limit; |
---|
| 235 | sd->sd_lobase = (unsigned long)base; |
---|
| 236 | sd->sd_type = type; |
---|
| 237 | sd->sd_dpl = dpl; |
---|
| 238 | sd->sd_p = 1; |
---|
| 239 | sd->sd_hilimit = (unsigned)limit >> 16; |
---|
| 240 | sd->sd_avl = 0; |
---|
| 241 | sd->sd_long = is64; |
---|
| 242 | sd->sd_def32 = 0; |
---|
| 243 | sd->sd_gran = gran; |
---|
| 244 | sd->sd_hibase = (unsigned long)base >> 24; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | static void |
---|
| 248 | gdt_set_sysseg(struct gdt_sysseg *sd, void *base, size_t limit, |
---|
| 249 | int type, int dpl, int gran) |
---|
| 250 | { |
---|
| 251 | memset(sd, 0, sizeof *sd); |
---|
| 252 | sd->sd_lolimit = (unsigned)limit; |
---|
| 253 | sd->sd_lobase = (uint64_t)base; |
---|
| 254 | sd->sd_type = type; |
---|
| 255 | sd->sd_dpl = dpl; |
---|
| 256 | sd->sd_p = 1; |
---|
| 257 | sd->sd_hilimit = (unsigned)limit >> 16; |
---|
| 258 | sd->sd_gran = gran; |
---|
| 259 | sd->sd_hibase = (uint64_t)base >> 24; |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | static void gdt_create() |
---|
| 263 | { |
---|
| 264 | memset(&gdtstore, 0, PAGE_SIZE); |
---|
| 265 | |
---|
| 266 | /* Flat segments */ |
---|
| 267 | gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_KCODE_SEL), 0, |
---|
| 268 | 0xfffff, SDT_MEMERA, SEL_KPL, 1, 1); |
---|
| 269 | gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_KDATA_SEL), 0, |
---|
| 270 | 0xfffff, SDT_MEMRWA, SEL_KPL, 1, 1); |
---|
| 271 | gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_UCODE_SEL), 0, |
---|
| 272 | 0xfffff, SDT_MEMERA, SEL_UPL, 1, 1); |
---|
| 273 | gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_UDATA_SEL), 0, |
---|
| 274 | 0xfffff, SDT_MEMRWA, SEL_UPL, 1, 1); |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | void cpu_load_gdt() |
---|
| 278 | { |
---|
| 279 | struct region_descriptor region; |
---|
| 280 | setregion(®ion, &gdtstore, PAGE_SIZE - 1); |
---|
| 281 | lgdt(®ion); |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | /* -------------------------------------------------------------------------- */ |
---|
| 285 | |
---|
| 286 | static void |
---|
| 287 | idt_set_seg(struct idt_seg *seg, void *func, int ist, int type, int dpl, int sel) |
---|
| 288 | { |
---|
| 289 | seg->gd_looffset = (uint64_t)func & 0xffff; |
---|
| 290 | seg->gd_selector = sel; |
---|
| 291 | seg->gd_ist = ist; |
---|
| 292 | seg->gd_type = type; |
---|
| 293 | seg->gd_dpl = dpl; |
---|
| 294 | seg->gd_p = 1; |
---|
| 295 | seg->gd_hioffset = (uint64_t)func >> 16; |
---|
| 296 | seg->gd_zero = 0; |
---|
| 297 | seg->gd_xx1 = 0; |
---|
| 298 | seg->gd_xx2 = 0; |
---|
| 299 | seg->gd_xx3 = 0; |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | static void idt_create() |
---|
| 303 | { |
---|
[80] | 304 | extern uint64_t x86_traps[], x86_intrs[], x86_rsvd; |
---|
[29] | 305 | struct idt_seg *idt; |
---|
| 306 | size_t i; |
---|
| 307 | |
---|
| 308 | idt = (struct idt_seg *)&idtstore; |
---|
[45] | 309 | |
---|
[80] | 310 | /* First, put a dead entry */ |
---|
| 311 | for (i = 0; i < NIDT; i++) { |
---|
| 312 | idt_set_seg(&idt[i], (void *)&x86_rsvd, 0, |
---|
| 313 | SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL)); |
---|
| 314 | } |
---|
| 315 | |
---|
[45] | 316 | /* General exceptions */ |
---|
| 317 | for (i = CPUVEC_MIN; i < CPUVEC_MAX; i++) { |
---|
| 318 | idt_set_seg(&idt[i], (void *)x86_traps[i - CPUVEC_MIN], 0, |
---|
| 319 | SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL)); |
---|
[29] | 320 | } |
---|
[45] | 321 | |
---|
| 322 | /* LAPIC interrupts */ |
---|
| 323 | for (i = LAPICVEC_MIN; i < LAPICVEC_MAX; i++) { |
---|
| 324 | idt_set_seg(&idt[i], (void *)x86_intrs[i - LAPICVEC_MIN], 0, |
---|
| 325 | SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL)); |
---|
| 326 | } |
---|
[29] | 327 | } |
---|
| 328 | |
---|
| 329 | void cpu_load_idt() |
---|
| 330 | { |
---|
| 331 | struct region_descriptor region; |
---|
| 332 | setregion(®ion, &idtstore, PAGE_SIZE - 1); |
---|
| 333 | lidt(®ion); |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | /* -------------------------------------------------------------------------- */ |
---|
| 337 | |
---|
| 338 | /* |
---|
| 339 | * The gdt bitmap must be per-cluster. |
---|
| 340 | */ |
---|
| 341 | int tss_alloc(struct tss *tss) |
---|
| 342 | { |
---|
| 343 | int slot; |
---|
| 344 | |
---|
| 345 | /* Once we have proper SMP support, we will change that */ |
---|
| 346 | slot = GDT_CPU0TSS_SEL; |
---|
| 347 | |
---|
| 348 | gdt_set_sysseg(GDT_ADDR_SYS(gdtstore, slot), tss, |
---|
| 349 | sizeof(*tss) - 1, SDT_SYS386TSS, SEL_KPL, 0); |
---|
| 350 | |
---|
| 351 | return GDT_DYNAM_SEL(slot, SEL_KPL); |
---|
| 352 | } |
---|
| 353 | |
---|
| 354 | void cpu_create_tss() |
---|
| 355 | { |
---|
| 356 | struct tss *tss = &cpu0_tss; |
---|
| 357 | int sel; |
---|
| 358 | |
---|
| 359 | /* Create the tss */ |
---|
| 360 | memset(tss, 0, sizeof(*tss)); |
---|
| 361 | tss->tss_iobase = IOMAP_INVALOFF << 16; |
---|
| 362 | tss->tss_ist[0] = (uint64_t)cpu0_intr_stack + STKSIZE; |
---|
| 363 | tss->tss_ist[1] = (uint64_t)cpu0_dbfl_stack + STKSIZE; |
---|
| 364 | tss->tss_ist[2] = (uint64_t)cpu0_nmfl_stack + STKSIZE; |
---|
| 365 | sel = tss_alloc(tss); |
---|
| 366 | |
---|
| 367 | /* Load it */ |
---|
| 368 | ltr(sel); |
---|
| 369 | } |
---|
| 370 | |
---|
| 371 | /* -------------------------------------------------------------------------- */ |
---|
| 372 | |
---|
| 373 | void cpu_attach() |
---|
| 374 | { |
---|
| 375 | cpu_load_gdt(); |
---|
| 376 | cpu_load_idt(); |
---|
| 377 | cpu_create_tss(); |
---|
| 378 | } |
---|
| 379 | |
---|