source: trunk/hal/x86_64/core/hal_init.c @ 71

Last change on this file since 71 was 70, checked in by max@…, 7 years ago

start constructing the bootinfo structure

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