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

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

Parse RSDP->RSDT->MADT, and get the LAPIC PA.

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