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