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

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

Update:

  • Introduce a basic VM manager in hal_gpt.c, to create the initial page tree for cluster0.
  • Locate the ACPI RSDP table, which involves entering the PA into cluster0's VA space.

The definitions are still a bit messy, but they will be reorganized later.

File size: 6.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_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        extern uint64_t __kernel_end;
79        x86_printf("__kernel_end: %Z\n", (uint64_t)&__kernel_end);
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
92        x86_printf("[+] bootloader: %s\n", mb_loader_name);
93
94        dump_memmap();
95        x86_printf("[+] dump finished\n");
96
97        hal_gpt_init(firstpa);
98        x86_printf("[+] hal_gpt_init called\n");
99
100        hal_acpi_init();
101        x86_printf("[+] hal_acpi_init called\n");
102
103
104        int m = 0;
105        int v = 1 / m;
106
107        char *buf = NULL;
108        *buf = (char)0x01;
109
110        x86_printf("ALIVE!\n");
111
112        while (1);
113}
114
115/* -------------------------------------------------------------------------- */
116
117uint8_t gdtstore[PAGE_SIZE] __in_kdata;
118uint8_t idtstore[PAGE_SIZE] __in_kdata;
119struct tss cpu0_tss __in_kdata;
120uint8_t cpu0_intr_stack[STKSIZE] __in_kdata;
121uint8_t cpu0_dbfl_stack[STKSIZE] __in_kdata;
122uint8_t cpu0_nmfl_stack[STKSIZE] __in_kdata;
123
124static void
125setregion(struct region_descriptor *rd, void *base, uint16_t limit)
126{
127        rd->rd_limit = limit;
128        rd->rd_base = (uint64_t)base;
129}
130
131/* -------------------------------------------------------------------------- */
132
133static void
134gdt_set_memseg(struct gdt_memseg *sd, void *base, size_t limit,
135        int type, int dpl, int gran, int is64)
136{
137        sd->sd_lolimit = (unsigned)limit;
138        sd->sd_lobase = (unsigned long)base;
139        sd->sd_type = type;
140        sd->sd_dpl = dpl;
141        sd->sd_p = 1;
142        sd->sd_hilimit = (unsigned)limit >> 16;
143        sd->sd_avl = 0;
144        sd->sd_long = is64;
145        sd->sd_def32 = 0;
146        sd->sd_gran = gran;
147        sd->sd_hibase = (unsigned long)base >> 24;
148}
149
150static void
151gdt_set_sysseg(struct gdt_sysseg *sd, void *base, size_t limit,
152        int type, int dpl, int gran)
153{
154        memset(sd, 0, sizeof *sd);
155        sd->sd_lolimit = (unsigned)limit;
156        sd->sd_lobase = (uint64_t)base;
157        sd->sd_type = type;
158        sd->sd_dpl = dpl;
159        sd->sd_p = 1;
160        sd->sd_hilimit = (unsigned)limit >> 16;
161        sd->sd_gran = gran;
162        sd->sd_hibase = (uint64_t)base >> 24;
163}
164
165static void gdt_create()
166{
167        memset(&gdtstore, 0, PAGE_SIZE);
168
169        /* Flat segments */
170        gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_KCODE_SEL), 0,
171            0xfffff, SDT_MEMERA, SEL_KPL, 1, 1);
172        gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_KDATA_SEL), 0,
173            0xfffff, SDT_MEMRWA, SEL_KPL, 1, 1);
174        gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_UCODE_SEL), 0,
175            0xfffff, SDT_MEMERA, SEL_UPL, 1, 1);
176        gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_UDATA_SEL), 0,
177            0xfffff, SDT_MEMRWA, SEL_UPL, 1, 1);
178}
179
180void cpu_load_gdt()
181{
182        struct region_descriptor region;
183        setregion(&region, &gdtstore, PAGE_SIZE - 1);
184        lgdt(&region);
185}
186
187/* -------------------------------------------------------------------------- */
188
189static void
190idt_set_seg(struct idt_seg *seg, void *func, int ist, int type, int dpl, int sel)
191{
192        seg->gd_looffset = (uint64_t)func & 0xffff;
193        seg->gd_selector = sel;
194        seg->gd_ist = ist;
195        seg->gd_type = type;
196        seg->gd_dpl = dpl;
197        seg->gd_p = 1;
198        seg->gd_hioffset = (uint64_t)func >> 16;
199        seg->gd_zero = 0;
200        seg->gd_xx1 = 0;
201        seg->gd_xx2 = 0;
202        seg->gd_xx3 = 0;
203}
204
205static void idt_create()
206{
207        extern uint64_t x86_traps[];
208        struct idt_seg *idt;
209        size_t i;
210
211        idt = (struct idt_seg *)&idtstore;
212        for (i = 0; i < NCPUIDT; i++) {
213                idt_set_seg(&idt[i], (void *)x86_traps[i], 0, SDT_SYS386IGT,
214                    SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL));
215        }
216}
217
218void cpu_load_idt()
219{
220        struct region_descriptor region;
221        setregion(&region, &idtstore, PAGE_SIZE - 1);
222        lidt(&region);
223}
224
225/* -------------------------------------------------------------------------- */
226
227/*
228 * The gdt bitmap must be per-cluster.
229 */
230int tss_alloc(struct tss *tss)
231{
232        int slot;
233
234        /* Once we have proper SMP support, we will change that */
235        slot = GDT_CPU0TSS_SEL;
236
237        gdt_set_sysseg(GDT_ADDR_SYS(gdtstore, slot), tss,
238            sizeof(*tss) - 1, SDT_SYS386TSS, SEL_KPL, 0);
239
240        return GDT_DYNAM_SEL(slot, SEL_KPL);
241}
242
243void cpu_create_tss()
244{
245        struct tss *tss = &cpu0_tss;
246        int sel;
247
248        /* Create the tss */
249        memset(tss, 0, sizeof(*tss));
250        tss->tss_iobase = IOMAP_INVALOFF << 16;
251        tss->tss_ist[0] = (uint64_t)cpu0_intr_stack + STKSIZE;
252        tss->tss_ist[1] = (uint64_t)cpu0_dbfl_stack + STKSIZE;
253        tss->tss_ist[2] = (uint64_t)cpu0_nmfl_stack + STKSIZE;
254        sel = tss_alloc(tss);
255
256        /* Load it */
257        ltr(sel);
258}
259
260/* -------------------------------------------------------------------------- */
261
262void cpu_attach()
263{
264        cpu_load_gdt();
265        cpu_load_idt();
266        cpu_create_tss();
267}
268
Note: See TracBrowser for help on using the repository browser.