[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> |
---|
[29] | 29 | |
---|
| 30 | #include <memcpy.h> |
---|
| 31 | #include <thread.h> |
---|
| 32 | #include <string.h> |
---|
| 33 | #include <process.h> |
---|
| 34 | #include <printk.h> |
---|
| 35 | #include <vmm.h> |
---|
| 36 | #include <core.h> |
---|
| 37 | #include <cluster.h> |
---|
| 38 | |
---|
| 39 | static void gdt_create(); |
---|
| 40 | static void idt_create(); |
---|
| 41 | void cpu_attach(); |
---|
| 42 | |
---|
[44] | 43 | size_t mytest __in_kdata = 0; |
---|
[32] | 44 | |
---|
| 45 | struct multiboot_info mb_info __in_kdata; |
---|
| 46 | char mb_loader_name[PAGE_SIZE] __in_kdata; |
---|
| 47 | uint8_t mb_mmap[PAGE_SIZE] __in_kdata; |
---|
| 48 | |
---|
[29] | 49 | /* -------------------------------------------------------------------------- */ |
---|
| 50 | |
---|
[32] | 51 | static void |
---|
| 52 | dump_memmap() |
---|
| 53 | { |
---|
| 54 | size_t mmap_length = mb_info.mi_mmap_length; |
---|
| 55 | uint8_t *mmap_addr = (uint8_t *)&mb_mmap; |
---|
| 56 | size_t i; |
---|
| 57 | |
---|
| 58 | if (!(mb_info.mi_flags & MULTIBOOT_INFO_HAS_MMAP)) |
---|
[46] | 59 | x86_panic("No mmap"); |
---|
[32] | 60 | |
---|
| 61 | i = 0; |
---|
| 62 | while (i < mmap_length) { |
---|
| 63 | struct multiboot_mmap *mm; |
---|
| 64 | |
---|
| 65 | mm = (struct multiboot_mmap *)(mmap_addr + i); |
---|
| 66 | |
---|
| 67 | x86_printf("-> [%Z, %Z] %s\n", mm->mm_base_addr, |
---|
| 68 | mm->mm_base_addr + mm->mm_length, |
---|
| 69 | (mm->mm_type == 1) ? "ram" : "rsv" ); |
---|
| 70 | |
---|
| 71 | i += mm->mm_size + 4; |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | |
---|
[29] | 75 | void init_x86_64(paddr_t firstpa) |
---|
| 76 | { |
---|
| 77 | x86_printf("[+] init_x86_64 called\n"); |
---|
| 78 | |
---|
| 79 | /* Create the global structures */ |
---|
| 80 | gdt_create(); |
---|
| 81 | idt_create(); |
---|
| 82 | |
---|
| 83 | /* Attach cpu0 */ |
---|
| 84 | cpu_attach(); |
---|
| 85 | x86_printf("[+] cpu_attach called\n"); |
---|
| 86 | |
---|
[47] | 87 | x86_printf("[+] bootloader: '%s'\n", mb_loader_name); |
---|
[32] | 88 | |
---|
| 89 | dump_memmap(); |
---|
| 90 | x86_printf("[+] dump finished\n"); |
---|
| 91 | |
---|
[35] | 92 | hal_gpt_init(firstpa); |
---|
| 93 | x86_printf("[+] hal_gpt_init called\n"); |
---|
[29] | 94 | |
---|
[35] | 95 | hal_acpi_init(); |
---|
| 96 | x86_printf("[+] hal_acpi_init called\n"); |
---|
| 97 | |
---|
[45] | 98 | hal_gpt_bootstrap_reset(); |
---|
| 99 | x86_printf("[+] hal_gpt_bootstrap_reset called\n"); |
---|
| 100 | |
---|
[46] | 101 | hal_lapic_init(); |
---|
| 102 | x86_printf("[+] hal_lapic_init called\n"); |
---|
[45] | 103 | |
---|
[46] | 104 | hal_tls_init_cpu0(); |
---|
| 105 | x86_printf("[+] hal_tls_init_cpu0 called\n"); |
---|
| 106 | |
---|
[44] | 107 | x86_printf("-> mytest = %z\n", mytest); |
---|
[61] | 108 | size_t *myptr = (void *)CLUSTER_MIN_VA(0) + XPTR(0, &mytest); |
---|
[44] | 109 | *myptr = 1; |
---|
| 110 | x86_printf("-> mytest = %z\n", mytest); |
---|
[35] | 111 | |
---|
[29] | 112 | int m = 0; |
---|
| 113 | int v = 1 / m; |
---|
| 114 | |
---|
[35] | 115 | char *buf = NULL; |
---|
| 116 | *buf = (char)0x01; |
---|
| 117 | |
---|
[29] | 118 | x86_printf("ALIVE!\n"); |
---|
| 119 | |
---|
| 120 | while (1); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | /* -------------------------------------------------------------------------- */ |
---|
| 124 | |
---|
| 125 | uint8_t gdtstore[PAGE_SIZE] __in_kdata; |
---|
| 126 | uint8_t idtstore[PAGE_SIZE] __in_kdata; |
---|
| 127 | struct tss cpu0_tss __in_kdata; |
---|
| 128 | uint8_t cpu0_intr_stack[STKSIZE] __in_kdata; |
---|
| 129 | uint8_t cpu0_dbfl_stack[STKSIZE] __in_kdata; |
---|
| 130 | uint8_t cpu0_nmfl_stack[STKSIZE] __in_kdata; |
---|
| 131 | |
---|
| 132 | static void |
---|
| 133 | setregion(struct region_descriptor *rd, void *base, uint16_t limit) |
---|
| 134 | { |
---|
| 135 | rd->rd_limit = limit; |
---|
| 136 | rd->rd_base = (uint64_t)base; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | /* -------------------------------------------------------------------------- */ |
---|
| 140 | |
---|
| 141 | static void |
---|
| 142 | gdt_set_memseg(struct gdt_memseg *sd, void *base, size_t limit, |
---|
| 143 | int type, int dpl, int gran, int is64) |
---|
| 144 | { |
---|
| 145 | sd->sd_lolimit = (unsigned)limit; |
---|
| 146 | sd->sd_lobase = (unsigned long)base; |
---|
| 147 | sd->sd_type = type; |
---|
| 148 | sd->sd_dpl = dpl; |
---|
| 149 | sd->sd_p = 1; |
---|
| 150 | sd->sd_hilimit = (unsigned)limit >> 16; |
---|
| 151 | sd->sd_avl = 0; |
---|
| 152 | sd->sd_long = is64; |
---|
| 153 | sd->sd_def32 = 0; |
---|
| 154 | sd->sd_gran = gran; |
---|
| 155 | sd->sd_hibase = (unsigned long)base >> 24; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | static void |
---|
| 159 | gdt_set_sysseg(struct gdt_sysseg *sd, void *base, size_t limit, |
---|
| 160 | int type, int dpl, int gran) |
---|
| 161 | { |
---|
| 162 | memset(sd, 0, sizeof *sd); |
---|
| 163 | sd->sd_lolimit = (unsigned)limit; |
---|
| 164 | sd->sd_lobase = (uint64_t)base; |
---|
| 165 | sd->sd_type = type; |
---|
| 166 | sd->sd_dpl = dpl; |
---|
| 167 | sd->sd_p = 1; |
---|
| 168 | sd->sd_hilimit = (unsigned)limit >> 16; |
---|
| 169 | sd->sd_gran = gran; |
---|
| 170 | sd->sd_hibase = (uint64_t)base >> 24; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | static void gdt_create() |
---|
| 174 | { |
---|
| 175 | memset(&gdtstore, 0, PAGE_SIZE); |
---|
| 176 | |
---|
| 177 | /* Flat segments */ |
---|
| 178 | gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_KCODE_SEL), 0, |
---|
| 179 | 0xfffff, SDT_MEMERA, SEL_KPL, 1, 1); |
---|
| 180 | gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_KDATA_SEL), 0, |
---|
| 181 | 0xfffff, SDT_MEMRWA, SEL_KPL, 1, 1); |
---|
| 182 | gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_UCODE_SEL), 0, |
---|
| 183 | 0xfffff, SDT_MEMERA, SEL_UPL, 1, 1); |
---|
| 184 | gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_UDATA_SEL), 0, |
---|
| 185 | 0xfffff, SDT_MEMRWA, SEL_UPL, 1, 1); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | void cpu_load_gdt() |
---|
| 189 | { |
---|
| 190 | struct region_descriptor region; |
---|
| 191 | setregion(®ion, &gdtstore, PAGE_SIZE - 1); |
---|
| 192 | lgdt(®ion); |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | /* -------------------------------------------------------------------------- */ |
---|
| 196 | |
---|
| 197 | static void |
---|
| 198 | idt_set_seg(struct idt_seg *seg, void *func, int ist, int type, int dpl, int sel) |
---|
| 199 | { |
---|
| 200 | seg->gd_looffset = (uint64_t)func & 0xffff; |
---|
| 201 | seg->gd_selector = sel; |
---|
| 202 | seg->gd_ist = ist; |
---|
| 203 | seg->gd_type = type; |
---|
| 204 | seg->gd_dpl = dpl; |
---|
| 205 | seg->gd_p = 1; |
---|
| 206 | seg->gd_hioffset = (uint64_t)func >> 16; |
---|
| 207 | seg->gd_zero = 0; |
---|
| 208 | seg->gd_xx1 = 0; |
---|
| 209 | seg->gd_xx2 = 0; |
---|
| 210 | seg->gd_xx3 = 0; |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | static void idt_create() |
---|
| 214 | { |
---|
[45] | 215 | extern uint64_t x86_traps[], x86_intrs[]; |
---|
[29] | 216 | struct idt_seg *idt; |
---|
| 217 | size_t i; |
---|
| 218 | |
---|
| 219 | idt = (struct idt_seg *)&idtstore; |
---|
[45] | 220 | |
---|
| 221 | /* General exceptions */ |
---|
| 222 | for (i = CPUVEC_MIN; i < CPUVEC_MAX; i++) { |
---|
| 223 | idt_set_seg(&idt[i], (void *)x86_traps[i - CPUVEC_MIN], 0, |
---|
| 224 | SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL)); |
---|
[29] | 225 | } |
---|
[45] | 226 | |
---|
| 227 | /* LAPIC interrupts */ |
---|
| 228 | for (i = LAPICVEC_MIN; i < LAPICVEC_MAX; i++) { |
---|
| 229 | idt_set_seg(&idt[i], (void *)x86_intrs[i - LAPICVEC_MIN], 0, |
---|
| 230 | SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL)); |
---|
| 231 | } |
---|
[29] | 232 | } |
---|
| 233 | |
---|
| 234 | void cpu_load_idt() |
---|
| 235 | { |
---|
| 236 | struct region_descriptor region; |
---|
| 237 | setregion(®ion, &idtstore, PAGE_SIZE - 1); |
---|
| 238 | lidt(®ion); |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | /* -------------------------------------------------------------------------- */ |
---|
| 242 | |
---|
| 243 | /* |
---|
| 244 | * The gdt bitmap must be per-cluster. |
---|
| 245 | */ |
---|
| 246 | int tss_alloc(struct tss *tss) |
---|
| 247 | { |
---|
| 248 | int slot; |
---|
| 249 | |
---|
| 250 | /* Once we have proper SMP support, we will change that */ |
---|
| 251 | slot = GDT_CPU0TSS_SEL; |
---|
| 252 | |
---|
| 253 | gdt_set_sysseg(GDT_ADDR_SYS(gdtstore, slot), tss, |
---|
| 254 | sizeof(*tss) - 1, SDT_SYS386TSS, SEL_KPL, 0); |
---|
| 255 | |
---|
| 256 | return GDT_DYNAM_SEL(slot, SEL_KPL); |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | void cpu_create_tss() |
---|
| 260 | { |
---|
| 261 | struct tss *tss = &cpu0_tss; |
---|
| 262 | int sel; |
---|
| 263 | |
---|
| 264 | /* Create the tss */ |
---|
| 265 | memset(tss, 0, sizeof(*tss)); |
---|
| 266 | tss->tss_iobase = IOMAP_INVALOFF << 16; |
---|
| 267 | tss->tss_ist[0] = (uint64_t)cpu0_intr_stack + STKSIZE; |
---|
| 268 | tss->tss_ist[1] = (uint64_t)cpu0_dbfl_stack + STKSIZE; |
---|
| 269 | tss->tss_ist[2] = (uint64_t)cpu0_nmfl_stack + STKSIZE; |
---|
| 270 | sel = tss_alloc(tss); |
---|
| 271 | |
---|
| 272 | /* Load it */ |
---|
| 273 | ltr(sel); |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | /* -------------------------------------------------------------------------- */ |
---|
| 277 | |
---|
| 278 | void cpu_attach() |
---|
| 279 | { |
---|
| 280 | cpu_load_gdt(); |
---|
| 281 | cpu_load_idt(); |
---|
| 282 | cpu_create_tss(); |
---|
| 283 | } |
---|
| 284 | |
---|