source: trunk/hal/x86_64/hal_init.c @ 45

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

Add some code for LAPIC; far from complete, but a good start.

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