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