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

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

complete hal_ppm_init, to take into account the reserved areas

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