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

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

Update. We need to use two separate arrays: one for the heap and one for
the kimg. The size of a heap entry is configurable, but that of the kimg
is not (fixed by mcmodel).

As a result, we also need to use two XPTRs: XPTR_HEAP and XPTR_KIMG. For
now we only declare XPTR_KIMG, and are not using it yet.

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