[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> |
---|
| 24 | #include <hal_segmentation.h> |
---|
| 25 | |
---|
| 26 | #include <memcpy.h> |
---|
| 27 | #include <thread.h> |
---|
| 28 | #include <string.h> |
---|
| 29 | #include <process.h> |
---|
| 30 | #include <printk.h> |
---|
| 31 | #include <vmm.h> |
---|
| 32 | #include <core.h> |
---|
| 33 | #include <cluster.h> |
---|
| 34 | |
---|
| 35 | void x86_printf(char *s, ...); |
---|
| 36 | |
---|
| 37 | static void gdt_create(); |
---|
| 38 | static void idt_create(); |
---|
| 39 | void cpu_attach(); |
---|
| 40 | |
---|
| 41 | /* -------------------------------------------------------------------------- */ |
---|
| 42 | |
---|
| 43 | void init_x86_64(paddr_t firstpa) |
---|
| 44 | { |
---|
| 45 | x86_printf("[+] init_x86_64 called\n"); |
---|
| 46 | |
---|
| 47 | extern uint64_t __kernel_end; |
---|
| 48 | x86_printf("__kernel_end: %p\n", (void *)&__kernel_end); |
---|
| 49 | |
---|
| 50 | /* Create the global structures */ |
---|
| 51 | gdt_create(); |
---|
| 52 | x86_printf("[+] gdt_create called\n"); |
---|
| 53 | |
---|
| 54 | idt_create(); |
---|
| 55 | x86_printf("[+] idt_create called\n"); |
---|
| 56 | |
---|
| 57 | /* Attach cpu0 */ |
---|
| 58 | cpu_attach(); |
---|
| 59 | x86_printf("[+] cpu_attach called\n"); |
---|
| 60 | |
---|
| 61 | char *buf = NULL; |
---|
| 62 | *buf = (char)0x01; |
---|
| 63 | |
---|
| 64 | int m = 0; |
---|
| 65 | int v = 1 / m; |
---|
| 66 | |
---|
| 67 | x86_printf("ALIVE!\n"); |
---|
| 68 | |
---|
| 69 | while (1); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | /* -------------------------------------------------------------------------- */ |
---|
| 73 | |
---|
| 74 | uint8_t gdtstore[PAGE_SIZE] __in_kdata; |
---|
| 75 | uint8_t idtstore[PAGE_SIZE] __in_kdata; |
---|
| 76 | struct tss cpu0_tss __in_kdata; |
---|
| 77 | uint8_t cpu0_intr_stack[STKSIZE] __in_kdata; |
---|
| 78 | uint8_t cpu0_dbfl_stack[STKSIZE] __in_kdata; |
---|
| 79 | uint8_t cpu0_nmfl_stack[STKSIZE] __in_kdata; |
---|
| 80 | |
---|
| 81 | static void |
---|
| 82 | setregion(struct region_descriptor *rd, void *base, uint16_t limit) |
---|
| 83 | { |
---|
| 84 | rd->rd_limit = limit; |
---|
| 85 | rd->rd_base = (uint64_t)base; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | /* -------------------------------------------------------------------------- */ |
---|
| 89 | |
---|
| 90 | static void |
---|
| 91 | gdt_set_memseg(struct gdt_memseg *sd, void *base, size_t limit, |
---|
| 92 | int type, int dpl, int gran, int is64) |
---|
| 93 | { |
---|
| 94 | sd->sd_lolimit = (unsigned)limit; |
---|
| 95 | sd->sd_lobase = (unsigned long)base; |
---|
| 96 | sd->sd_type = type; |
---|
| 97 | sd->sd_dpl = dpl; |
---|
| 98 | sd->sd_p = 1; |
---|
| 99 | sd->sd_hilimit = (unsigned)limit >> 16; |
---|
| 100 | sd->sd_avl = 0; |
---|
| 101 | sd->sd_long = is64; |
---|
| 102 | sd->sd_def32 = 0; |
---|
| 103 | sd->sd_gran = gran; |
---|
| 104 | sd->sd_hibase = (unsigned long)base >> 24; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | static void |
---|
| 108 | gdt_set_sysseg(struct gdt_sysseg *sd, void *base, size_t limit, |
---|
| 109 | int type, int dpl, int gran) |
---|
| 110 | { |
---|
| 111 | memset(sd, 0, sizeof *sd); |
---|
| 112 | sd->sd_lolimit = (unsigned)limit; |
---|
| 113 | sd->sd_lobase = (uint64_t)base; |
---|
| 114 | sd->sd_type = type; |
---|
| 115 | sd->sd_dpl = dpl; |
---|
| 116 | sd->sd_p = 1; |
---|
| 117 | sd->sd_hilimit = (unsigned)limit >> 16; |
---|
| 118 | sd->sd_gran = gran; |
---|
| 119 | sd->sd_hibase = (uint64_t)base >> 24; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | static void gdt_create() |
---|
| 123 | { |
---|
| 124 | memset(&gdtstore, 0, PAGE_SIZE); |
---|
| 125 | |
---|
| 126 | /* Flat segments */ |
---|
| 127 | gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_KCODE_SEL), 0, |
---|
| 128 | 0xfffff, SDT_MEMERA, SEL_KPL, 1, 1); |
---|
| 129 | gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_KDATA_SEL), 0, |
---|
| 130 | 0xfffff, SDT_MEMRWA, SEL_KPL, 1, 1); |
---|
| 131 | gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_UCODE_SEL), 0, |
---|
| 132 | 0xfffff, SDT_MEMERA, SEL_UPL, 1, 1); |
---|
| 133 | gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_UDATA_SEL), 0, |
---|
| 134 | 0xfffff, SDT_MEMRWA, SEL_UPL, 1, 1); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | void cpu_load_gdt() |
---|
| 138 | { |
---|
| 139 | struct region_descriptor region; |
---|
| 140 | setregion(®ion, &gdtstore, PAGE_SIZE - 1); |
---|
| 141 | lgdt(®ion); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | /* -------------------------------------------------------------------------- */ |
---|
| 145 | |
---|
| 146 | static void |
---|
| 147 | idt_set_seg(struct idt_seg *seg, void *func, int ist, int type, int dpl, int sel) |
---|
| 148 | { |
---|
| 149 | seg->gd_looffset = (uint64_t)func & 0xffff; |
---|
| 150 | seg->gd_selector = sel; |
---|
| 151 | seg->gd_ist = ist; |
---|
| 152 | seg->gd_type = type; |
---|
| 153 | seg->gd_dpl = dpl; |
---|
| 154 | seg->gd_p = 1; |
---|
| 155 | seg->gd_hioffset = (uint64_t)func >> 16; |
---|
| 156 | seg->gd_zero = 0; |
---|
| 157 | seg->gd_xx1 = 0; |
---|
| 158 | seg->gd_xx2 = 0; |
---|
| 159 | seg->gd_xx3 = 0; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | static void idt_create() |
---|
| 163 | { |
---|
| 164 | extern uint64_t x86_traps[]; |
---|
| 165 | struct idt_seg *idt; |
---|
| 166 | size_t i; |
---|
| 167 | |
---|
| 168 | idt = (struct idt_seg *)&idtstore; |
---|
| 169 | for (i = 0; i < NCPUIDT; i++) { |
---|
| 170 | idt_set_seg(&idt[i], x86_traps[i], 0, SDT_SYS386IGT, |
---|
| 171 | SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL)); |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | void cpu_load_idt() |
---|
| 176 | { |
---|
| 177 | struct region_descriptor region; |
---|
| 178 | setregion(®ion, &idtstore, PAGE_SIZE - 1); |
---|
| 179 | lidt(®ion); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | /* -------------------------------------------------------------------------- */ |
---|
| 183 | |
---|
| 184 | /* |
---|
| 185 | * The gdt bitmap must be per-cluster. |
---|
| 186 | */ |
---|
| 187 | int tss_alloc(struct tss *tss) |
---|
| 188 | { |
---|
| 189 | int slot; |
---|
| 190 | |
---|
| 191 | /* Once we have proper SMP support, we will change that */ |
---|
| 192 | slot = GDT_CPU0TSS_SEL; |
---|
| 193 | |
---|
| 194 | gdt_set_sysseg(GDT_ADDR_SYS(gdtstore, slot), tss, |
---|
| 195 | sizeof(*tss) - 1, SDT_SYS386TSS, SEL_KPL, 0); |
---|
| 196 | |
---|
| 197 | return GDT_DYNAM_SEL(slot, SEL_KPL); |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | void cpu_create_tss() |
---|
| 201 | { |
---|
| 202 | struct tss *tss = &cpu0_tss; |
---|
| 203 | int sel; |
---|
| 204 | |
---|
| 205 | /* Create the tss */ |
---|
| 206 | memset(tss, 0, sizeof(*tss)); |
---|
| 207 | tss->tss_iobase = IOMAP_INVALOFF << 16; |
---|
| 208 | tss->tss_ist[0] = (uint64_t)cpu0_intr_stack + STKSIZE; |
---|
| 209 | tss->tss_ist[1] = (uint64_t)cpu0_dbfl_stack + STKSIZE; |
---|
| 210 | tss->tss_ist[2] = (uint64_t)cpu0_nmfl_stack + STKSIZE; |
---|
| 211 | sel = tss_alloc(tss); |
---|
| 212 | |
---|
| 213 | /* Load it */ |
---|
| 214 | ltr(sel); |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | /* -------------------------------------------------------------------------- */ |
---|
| 218 | |
---|
| 219 | void cpu_attach() |
---|
| 220 | { |
---|
| 221 | cpu_load_gdt(); |
---|
| 222 | cpu_load_idt(); |
---|
| 223 | cpu_create_tss(); |
---|
| 224 | } |
---|
| 225 | |
---|