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_multiboot.h> |
---|
25 | #include <hal_segmentation.h> |
---|
26 | #include <hal_acpi.h> |
---|
27 | #include <hal_apic.h> |
---|
28 | #include <hal_internal.h> |
---|
29 | #include <hal_remote.h> |
---|
30 | #include <hal_irqmask.h> |
---|
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> |
---|
40 | #include <chdev.h> |
---|
41 | |
---|
42 | #include <boot_info.h> |
---|
43 | |
---|
44 | void kernel_init(boot_info_t *info); |
---|
45 | |
---|
46 | static void gdt_create(); |
---|
47 | static void idt_create(); |
---|
48 | void cpu_attach(); |
---|
49 | |
---|
50 | size_t mytest __in_kdata = 0; |
---|
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 | |
---|
56 | /* -------------------------------------------------------------------------- */ |
---|
57 | |
---|
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)) |
---|
66 | x86_panic("No mmap"); |
---|
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 | |
---|
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 paddr_t pa_avail; |
---|
112 | extern vaddr_t va_avail; |
---|
113 | |
---|
114 | extern uint64_t __kernel_data_start; |
---|
115 | extern uint64_t __kernel_end; |
---|
116 | |
---|
117 | size_t pa_offset = pa_avail - 0; |
---|
118 | size_t va_offset = va_avail - CLUSTER_MIN_VA(0); |
---|
119 | |
---|
120 | memset(info, 0, sizeof(boot_info_t)); |
---|
121 | |
---|
122 | info->signature = 0; |
---|
123 | |
---|
124 | info->paddr_width = 0; |
---|
125 | info->x_width = 1; |
---|
126 | info->y_width = 1; |
---|
127 | info->x_size = 1; |
---|
128 | info->y_size = 1; |
---|
129 | info->io_cxy = 0; |
---|
130 | |
---|
131 | info->ext_dev_nr = 1; |
---|
132 | init_bootinfo_txt(&info->ext_dev[0]); |
---|
133 | |
---|
134 | info->cxy = 0; |
---|
135 | info->cores_nr = 1; |
---|
136 | init_bootinfo_core(&info->core[0]); |
---|
137 | |
---|
138 | info->rsvd_nr = 0; |
---|
139 | /* rsvd XXX */ |
---|
140 | |
---|
141 | /* dev_ XXX */ |
---|
142 | |
---|
143 | info->pages_offset = MAX(pa_offset, va_offset); |
---|
144 | info->pages_nr = 0; /* XXX */ |
---|
145 | |
---|
146 | info->kernel_code_start = (intptr_t)(KERNTEXTOFF - KERNBASE); |
---|
147 | info->kernel_code_end = (intptr_t)(&__kernel_data_start - KERNBASE) - 1; |
---|
148 | info->kernel_data_start = (intptr_t)(&__kernel_data_start - KERNBASE); |
---|
149 | info->kernel_code_end = (intptr_t)(&__kernel_end - KERNBASE) - 1; |
---|
150 | } |
---|
151 | |
---|
152 | void init_x86_64(paddr_t firstpa) |
---|
153 | { |
---|
154 | boot_info_t btinfo; |
---|
155 | |
---|
156 | x86_printf("[+] init_x86_64 called\n"); |
---|
157 | |
---|
158 | /* Create the global structures */ |
---|
159 | gdt_create(); |
---|
160 | idt_create(); |
---|
161 | |
---|
162 | /* Attach cpu0 */ |
---|
163 | cpu_attach(); |
---|
164 | x86_printf("[+] cpu_attach called\n"); |
---|
165 | |
---|
166 | x86_printf("[+] bootloader: '%s'\n", mb_loader_name); |
---|
167 | |
---|
168 | dump_memmap(); |
---|
169 | x86_printf("[+] dump finished\n"); |
---|
170 | |
---|
171 | hal_gpt_init(firstpa); |
---|
172 | x86_printf("[+] hal_gpt_init called\n"); |
---|
173 | |
---|
174 | hal_acpi_init(); |
---|
175 | x86_printf("[+] hal_acpi_init called\n"); |
---|
176 | |
---|
177 | hal_gpt_bootstrap_reset(); |
---|
178 | x86_printf("[+] hal_gpt_bootstrap_reset called\n"); |
---|
179 | |
---|
180 | hal_apic_init(); |
---|
181 | x86_printf("[+] hal_apic_init called\n"); |
---|
182 | |
---|
183 | hal_tls_init_cpu0(); |
---|
184 | x86_printf("[+] hal_tls_init_cpu0 called\n"); |
---|
185 | |
---|
186 | x86_printf("-> mytest = %z\n", mytest); |
---|
187 | void *hoho = &init_x86_64; |
---|
188 | xptr_t myptr = XPTR(0, &mytest); |
---|
189 | |
---|
190 | hal_remote_spt(myptr, hoho); |
---|
191 | x86_printf("-> mytest = %Z\n", hal_remote_lpt(myptr)); |
---|
192 | |
---|
193 | init_bootinfo(&btinfo); |
---|
194 | |
---|
195 | reg_t dummy; |
---|
196 | hal_enable_irq(&dummy); |
---|
197 | |
---|
198 | kernel_init(&btinfo); |
---|
199 | |
---|
200 | x86_printf("[+] kernel_init called\n"); |
---|
201 | |
---|
202 | while (1); |
---|
203 | |
---|
204 | // void x86_stop(); |
---|
205 | // x86_stop(); |
---|
206 | |
---|
207 | int m = 0; |
---|
208 | int v = 1 / m; |
---|
209 | |
---|
210 | char *buf = NULL; |
---|
211 | *buf = (char)0x01; |
---|
212 | |
---|
213 | x86_printf("ALIVE!\n"); |
---|
214 | |
---|
215 | while (1); |
---|
216 | } |
---|
217 | |
---|
218 | /* -------------------------------------------------------------------------- */ |
---|
219 | |
---|
220 | uint8_t gdtstore[PAGE_SIZE] __in_kdata; |
---|
221 | uint8_t idtstore[PAGE_SIZE] __in_kdata; |
---|
222 | struct tss cpu0_tss __in_kdata; |
---|
223 | uint8_t cpu0_intr_stack[STKSIZE] __in_kdata; |
---|
224 | uint8_t cpu0_dbfl_stack[STKSIZE] __in_kdata; |
---|
225 | uint8_t cpu0_nmfl_stack[STKSIZE] __in_kdata; |
---|
226 | |
---|
227 | static void |
---|
228 | setregion(struct region_descriptor *rd, void *base, uint16_t limit) |
---|
229 | { |
---|
230 | rd->rd_limit = limit; |
---|
231 | rd->rd_base = (uint64_t)base; |
---|
232 | } |
---|
233 | |
---|
234 | /* -------------------------------------------------------------------------- */ |
---|
235 | |
---|
236 | static void |
---|
237 | gdt_set_memseg(struct gdt_memseg *sd, void *base, size_t limit, |
---|
238 | int type, int dpl, int gran, int is64) |
---|
239 | { |
---|
240 | sd->sd_lolimit = (unsigned)limit; |
---|
241 | sd->sd_lobase = (unsigned long)base; |
---|
242 | sd->sd_type = type; |
---|
243 | sd->sd_dpl = dpl; |
---|
244 | sd->sd_p = 1; |
---|
245 | sd->sd_hilimit = (unsigned)limit >> 16; |
---|
246 | sd->sd_avl = 0; |
---|
247 | sd->sd_long = is64; |
---|
248 | sd->sd_def32 = 0; |
---|
249 | sd->sd_gran = gran; |
---|
250 | sd->sd_hibase = (unsigned long)base >> 24; |
---|
251 | } |
---|
252 | |
---|
253 | static void |
---|
254 | gdt_set_sysseg(struct gdt_sysseg *sd, void *base, size_t limit, |
---|
255 | int type, int dpl, int gran) |
---|
256 | { |
---|
257 | memset(sd, 0, sizeof *sd); |
---|
258 | sd->sd_lolimit = (unsigned)limit; |
---|
259 | sd->sd_lobase = (uint64_t)base; |
---|
260 | sd->sd_type = type; |
---|
261 | sd->sd_dpl = dpl; |
---|
262 | sd->sd_p = 1; |
---|
263 | sd->sd_hilimit = (unsigned)limit >> 16; |
---|
264 | sd->sd_gran = gran; |
---|
265 | sd->sd_hibase = (uint64_t)base >> 24; |
---|
266 | } |
---|
267 | |
---|
268 | static void gdt_create() |
---|
269 | { |
---|
270 | memset(&gdtstore, 0, PAGE_SIZE); |
---|
271 | |
---|
272 | /* Flat segments */ |
---|
273 | gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_KCODE_SEL), 0, |
---|
274 | 0xfffff, SDT_MEMERA, SEL_KPL, 1, 1); |
---|
275 | gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_KDATA_SEL), 0, |
---|
276 | 0xfffff, SDT_MEMRWA, SEL_KPL, 1, 1); |
---|
277 | gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_UCODE_SEL), 0, |
---|
278 | 0xfffff, SDT_MEMERA, SEL_UPL, 1, 1); |
---|
279 | gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_UDATA_SEL), 0, |
---|
280 | 0xfffff, SDT_MEMRWA, SEL_UPL, 1, 1); |
---|
281 | } |
---|
282 | |
---|
283 | void cpu_load_gdt() |
---|
284 | { |
---|
285 | struct region_descriptor region; |
---|
286 | setregion(®ion, &gdtstore, PAGE_SIZE - 1); |
---|
287 | lgdt(®ion); |
---|
288 | } |
---|
289 | |
---|
290 | /* -------------------------------------------------------------------------- */ |
---|
291 | |
---|
292 | static void |
---|
293 | idt_set_seg(struct idt_seg *seg, void *func, int ist, int type, int dpl, int sel) |
---|
294 | { |
---|
295 | seg->gd_looffset = (uint64_t)func & 0xffff; |
---|
296 | seg->gd_selector = sel; |
---|
297 | seg->gd_ist = ist; |
---|
298 | seg->gd_type = type; |
---|
299 | seg->gd_dpl = dpl; |
---|
300 | seg->gd_p = 1; |
---|
301 | seg->gd_hioffset = (uint64_t)func >> 16; |
---|
302 | seg->gd_zero = 0; |
---|
303 | seg->gd_xx1 = 0; |
---|
304 | seg->gd_xx2 = 0; |
---|
305 | seg->gd_xx3 = 0; |
---|
306 | } |
---|
307 | |
---|
308 | static void idt_create() |
---|
309 | { |
---|
310 | extern uint64_t x86_traps[], x86_intrs[], x86_rsvd; |
---|
311 | struct idt_seg *idt; |
---|
312 | size_t i; |
---|
313 | |
---|
314 | idt = (struct idt_seg *)&idtstore; |
---|
315 | |
---|
316 | /* First, put a dead entry */ |
---|
317 | for (i = 0; i < NIDT; i++) { |
---|
318 | idt_set_seg(&idt[i], (void *)&x86_rsvd, 0, |
---|
319 | SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL)); |
---|
320 | } |
---|
321 | |
---|
322 | /* General exceptions */ |
---|
323 | for (i = CPUVEC_MIN; i < CPUVEC_MAX; i++) { |
---|
324 | idt_set_seg(&idt[i], (void *)x86_traps[i - CPUVEC_MIN], 0, |
---|
325 | SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL)); |
---|
326 | } |
---|
327 | |
---|
328 | /* LAPIC interrupts */ |
---|
329 | for (i = LAPICVEC_MIN; i < LAPICVEC_MAX; i++) { |
---|
330 | idt_set_seg(&idt[i], (void *)x86_intrs[i - LAPICVEC_MIN], 0, |
---|
331 | SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL)); |
---|
332 | } |
---|
333 | } |
---|
334 | |
---|
335 | void cpu_load_idt() |
---|
336 | { |
---|
337 | struct region_descriptor region; |
---|
338 | setregion(®ion, &idtstore, PAGE_SIZE - 1); |
---|
339 | lidt(®ion); |
---|
340 | } |
---|
341 | |
---|
342 | /* -------------------------------------------------------------------------- */ |
---|
343 | |
---|
344 | /* |
---|
345 | * The gdt bitmap must be per-cluster. |
---|
346 | */ |
---|
347 | int tss_alloc(struct tss *tss) |
---|
348 | { |
---|
349 | int slot; |
---|
350 | |
---|
351 | /* Once we have proper SMP support, we will change that */ |
---|
352 | slot = GDT_CPU0TSS_SEL; |
---|
353 | |
---|
354 | gdt_set_sysseg(GDT_ADDR_SYS(gdtstore, slot), tss, |
---|
355 | sizeof(*tss) - 1, SDT_SYS386TSS, SEL_KPL, 0); |
---|
356 | |
---|
357 | return GDT_DYNAM_SEL(slot, SEL_KPL); |
---|
358 | } |
---|
359 | |
---|
360 | void cpu_create_tss() |
---|
361 | { |
---|
362 | struct tss *tss = &cpu0_tss; |
---|
363 | int sel; |
---|
364 | |
---|
365 | /* Create the tss */ |
---|
366 | memset(tss, 0, sizeof(*tss)); |
---|
367 | tss->tss_iobase = IOMAP_INVALOFF << 16; |
---|
368 | tss->tss_ist[0] = (uint64_t)cpu0_intr_stack + STKSIZE; |
---|
369 | tss->tss_ist[1] = (uint64_t)cpu0_dbfl_stack + STKSIZE; |
---|
370 | tss->tss_ist[2] = (uint64_t)cpu0_nmfl_stack + STKSIZE; |
---|
371 | sel = tss_alloc(tss); |
---|
372 | |
---|
373 | /* Load it */ |
---|
374 | ltr(sel); |
---|
375 | } |
---|
376 | |
---|
377 | /* -------------------------------------------------------------------------- */ |
---|
378 | |
---|
379 | void cpu_attach() |
---|
380 | { |
---|
381 | cpu_load_gdt(); |
---|
382 | cpu_load_idt(); |
---|
383 | cpu_create_tss(); |
---|
384 | } |
---|
385 | |
---|