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

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

improve the APIC implementation

File size: 11.0 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_acpi.h>
27#include <hal_apic.h>
28#include <hal_internal.h>
29#include <hal_remote.h>
30#include <hal_irqmask.h>
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>
40#include <chdev.h>
41
42#include <boot_info.h>
43
44void kernel_init(boot_info_t *info);
45
46static void gdt_create();
47static void idt_create();
48void cpu_attach();
49
50size_t mytest __in_kdata = 0;
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
56/* -------------------------------------------------------------------------- */
57
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))
66                x86_panic("No mmap");
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
82/* -------------------------------------------------------------------------- */
83
84static void init_bootinfo_icu(boot_device_t *dev)
85{
86        extern uint32_t hwi_baseidx;
87        extern uint32_t wti_baseidx;
88        extern uint32_t pti_baseidx;
89        extern size_t ioapic_pins;
90
91        memset(dev, 0, sizeof(boot_device_t));
92
93        dev->base = 0;
94        dev->type = (DEV_FUNC_ICU << 16) | IMPL_ICU_XCU;
95        dev->channels = 1;
96
97        /*
98         * Give 20% of the pins to HWI, 80% to WTI.
99         */
100        dev->param0 = (ioapic_pins * 20) / 100;  /* hwi_nr */
101        dev->param1 = ioapic_pins - dev->param0; /* wti_nr */
102
103        /*
104         * We always set 1 for pti_nr. On x86, timer interrupts are handled by
105         * LAPIC, which is per-cpu and not global. Therefore, we always have one
106         * timer for each CPU, and its IRQ number is faked to 0.
107         */
108        dev->param2 = 1; /* pti_nr */
109
110        dev->param3 = 0;
111
112        /* Set the base idx for the XCU driver */
113        hwi_baseidx = 0;
114        wti_baseidx = dev->param0;
115        pti_baseidx = 0xFFFFFFFF;
116
117#ifdef NOTYET
118    uint32_t    irqs;                 /*! number of input IRQs                              */
119    boot_irq_t  irq[32];              /*! array of input IRQS (PIC and ICU only)            */
120#endif
121}
122
123static size_t init_bootinfo_pages_nr()
124{
125        size_t mmap_length = mb_info.mi_mmap_length;
126        uint8_t *mmap_addr = (uint8_t *)&mb_mmap;
127        paddr_t maxpa, pa;
128        size_t i;
129
130        i = 0;
131        maxpa = 0;
132        while (i < mmap_length) {
133                struct multiboot_mmap *mm;
134
135                mm = (struct multiboot_mmap *)(mmap_addr + i);
136
137                if (mm->mm_type == 1) {
138                        pa = mm->mm_base_addr + mm->mm_length;
139                        if (pa > maxpa)
140                                maxpa = pa;
141                }
142
143                i += mm->mm_size + 4;
144        }
145
146        return (maxpa / PAGE_SIZE);
147}
148
149static size_t init_bootinfo_rsvd(boot_rsvd_t *rsvd)
150{
151        size_t mmap_length = mb_info.mi_mmap_length;
152        uint8_t *mmap_addr = (uint8_t *)&mb_mmap;
153        size_t i, rsvd_nr;
154
155        memset(rsvd, 0, sizeof(boot_rsvd_t) * CONFIG_PPM_MAX_RSVD);
156
157        i = 0, rsvd_nr = 0;
158        while (i < mmap_length) {
159                struct multiboot_mmap *mm;
160
161                mm = (struct multiboot_mmap *)(mmap_addr + i);
162
163                if (mm->mm_type != 1) {
164                        rsvd[rsvd_nr].first_page =
165                            rounddown(mm->mm_base_addr, PAGE_SIZE) / PAGE_SIZE;
166                        rsvd[rsvd_nr].npages =
167                            roundup(mm->mm_length, PAGE_SIZE) / PAGE_SIZE;
168                        rsvd_nr++;
169                        if (rsvd_nr == CONFIG_PPM_MAX_RSVD)
170                                x86_panic("too many memory holes");
171                }
172
173                i += mm->mm_size + 4;
174        }
175
176        return rsvd_nr;
177}
178
179static void init_bootinfo_core(boot_core_t *core)
180{
181        memset(core, 0, sizeof(boot_core_t));
182
183        core->gid = hal_lapic_gid();
184        core->lid = 0;
185        core->cxy = 0;
186}
187
188static void init_bootinfo_txt(boot_device_t *dev)
189{
190        memset(dev, 0, sizeof(boot_device_t));
191
192        dev->base = 0xB8000;
193        dev->type = (DEV_FUNC_TXT << 16) | IMPL_TXT_X86;
194        dev->channels = 1;
195        dev->param0 = 0;
196        dev->param1 = 0;
197        dev->param2 = 0;
198        dev->param3 = 0;
199
200#ifdef NOTYET
201    uint32_t    irqs;    /*! number of input IRQs                    */
202    boot_irq_t  irq[32]; /*! array of input IRQS (PIC and ICU only)  */
203#endif
204}
205
206static void init_bootinfo(boot_info_t *info)
207{
208        size_t offset;
209
210        extern uint64_t __kernel_data_start;
211        extern uint64_t __kernel_end;
212
213        memset(info, 0, sizeof(boot_info_t));
214
215        info->signature = 0;
216
217        info->paddr_width = 0;
218        info->x_width = 1;
219        info->y_width = 1;
220        info->x_size = 1;
221        info->y_size = 1;
222        info->io_cxy = 0;
223
224        info->ext_dev_nr = 1;
225        init_bootinfo_txt(&info->ext_dev[0]);
226
227        info->cxy = 0;
228        info->cores_nr = 1;
229        init_bootinfo_core(&info->core[0]);
230
231        info->rsvd_nr = init_bootinfo_rsvd((boot_rsvd_t *)&info->rsvd);
232
233        init_bootinfo_icu(&info->dev_icu);
234        /* TODO: dev_mmc */
235        /* TODO: dev_dma */
236
237        offset = hal_gpt_bootstrap_uniformize();
238        info->pages_offset = offset / PAGE_SIZE;
239        info->pages_nr = init_bootinfo_pages_nr();
240
241        info->kernel_code_start = (intptr_t)(KERNTEXTOFF - KERNBASE);
242        info->kernel_code_end = (intptr_t)(&__kernel_data_start - KERNBASE) - 1;
243        info->kernel_data_start = (intptr_t)(&__kernel_data_start - KERNBASE);
244        info->kernel_code_end = (intptr_t)(&__kernel_end - KERNBASE) - 1;
245}
246
247void init_x86_64(paddr_t firstpa)
248{
249        boot_info_t btinfo;
250
251        x86_printf("[+] init_x86_64 called\n");
252
253        /* Create the global structures */
254        gdt_create();
255        idt_create();
256
257        /* Attach cpu0 */
258        cpu_attach();
259        x86_printf("[+] cpu_attach called\n");
260
261        x86_printf("[+] bootloader: '%s'\n", mb_loader_name);
262
263        dump_memmap();
264        x86_printf("[+] dump finished\n");
265
266        hal_gpt_init(firstpa);
267        x86_printf("[+] hal_gpt_init called\n");
268
269        hal_acpi_init();
270        x86_printf("[+] hal_acpi_init called\n");
271
272        hal_gpt_bootstrap_reset();
273        x86_printf("[+] hal_gpt_bootstrap_reset called\n");
274
275        hal_apic_init();
276        x86_printf("[+] hal_apic_init called\n");
277
278        hal_tls_init_cpu0();
279        x86_printf("[+] hal_tls_init_cpu0 called\n");
280
281        x86_printf("-> mytest = %z\n", mytest);
282        void *hoho = &init_x86_64;
283        xptr_t myptr = XPTR(0, &mytest);
284
285        hal_remote_spt(myptr, hoho);
286        x86_printf("-> mytest = %Z\n", hal_remote_lpt(myptr));
287
288        init_bootinfo(&btinfo);
289
290        reg_t dummy;
291        hal_enable_irq(&dummy);
292
293        kernel_init(&btinfo);
294
295        x86_printf("[+] kernel_init called\n");
296
297        while (1);
298
299//      void x86_stop();
300//      x86_stop();
301
302        int m = 0;
303        int v = 1 / m;
304
305        char *buf = NULL;
306        *buf = (char)0x01;
307
308        x86_printf("ALIVE!\n");
309
310        while (1);
311}
312
313/* -------------------------------------------------------------------------- */
314
315uint8_t gdtstore[PAGE_SIZE] __in_kdata;
316uint8_t idtstore[PAGE_SIZE] __in_kdata;
317struct tss cpu0_tss __in_kdata;
318uint8_t cpu0_intr_stack[STKSIZE] __in_kdata;
319uint8_t cpu0_dbfl_stack[STKSIZE] __in_kdata;
320uint8_t cpu0_nmfl_stack[STKSIZE] __in_kdata;
321
322static void
323setregion(struct region_descriptor *rd, void *base, uint16_t limit)
324{
325        rd->rd_limit = limit;
326        rd->rd_base = (uint64_t)base;
327}
328
329/* -------------------------------------------------------------------------- */
330
331static void
332gdt_set_memseg(struct gdt_memseg *sd, void *base, size_t limit,
333        int type, int dpl, int gran, int is64)
334{
335        sd->sd_lolimit = (unsigned)limit;
336        sd->sd_lobase = (unsigned long)base;
337        sd->sd_type = type;
338        sd->sd_dpl = dpl;
339        sd->sd_p = 1;
340        sd->sd_hilimit = (unsigned)limit >> 16;
341        sd->sd_avl = 0;
342        sd->sd_long = is64;
343        sd->sd_def32 = 0;
344        sd->sd_gran = gran;
345        sd->sd_hibase = (unsigned long)base >> 24;
346}
347
348static void
349gdt_set_sysseg(struct gdt_sysseg *sd, void *base, size_t limit,
350        int type, int dpl, int gran)
351{
352        memset(sd, 0, sizeof *sd);
353        sd->sd_lolimit = (unsigned)limit;
354        sd->sd_lobase = (uint64_t)base;
355        sd->sd_type = type;
356        sd->sd_dpl = dpl;
357        sd->sd_p = 1;
358        sd->sd_hilimit = (unsigned)limit >> 16;
359        sd->sd_gran = gran;
360        sd->sd_hibase = (uint64_t)base >> 24;
361}
362
363static void gdt_create()
364{
365        memset(&gdtstore, 0, PAGE_SIZE);
366
367        /* Flat segments */
368        gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_KCODE_SEL), 0,
369            0xfffff, SDT_MEMERA, SEL_KPL, 1, 1);
370        gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_KDATA_SEL), 0,
371            0xfffff, SDT_MEMRWA, SEL_KPL, 1, 1);
372        gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_UCODE_SEL), 0,
373            0xfffff, SDT_MEMERA, SEL_UPL, 1, 1);
374        gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_UDATA_SEL), 0,
375            0xfffff, SDT_MEMRWA, SEL_UPL, 1, 1);
376}
377
378void cpu_load_gdt()
379{
380        struct region_descriptor region;
381        setregion(&region, &gdtstore, PAGE_SIZE - 1);
382        lgdt(&region);
383}
384
385/* -------------------------------------------------------------------------- */
386
387static void
388idt_set_seg(struct idt_seg *seg, void *func, int ist, int type, int dpl, int sel)
389{
390        seg->gd_looffset = (uint64_t)func & 0xffff;
391        seg->gd_selector = sel;
392        seg->gd_ist = ist;
393        seg->gd_type = type;
394        seg->gd_dpl = dpl;
395        seg->gd_p = 1;
396        seg->gd_hioffset = (uint64_t)func >> 16;
397        seg->gd_zero = 0;
398        seg->gd_xx1 = 0;
399        seg->gd_xx2 = 0;
400        seg->gd_xx3 = 0;
401}
402
403static void idt_create()
404{
405        extern uint64_t x86_traps[], x86_intrs[], x86_rsvd;
406        struct idt_seg *idt;
407        size_t i;
408
409        idt = (struct idt_seg *)&idtstore;
410
411        /* First, put a dead entry */
412        for (i = 0; i < NIDT; i++) {
413                idt_set_seg(&idt[i], (void *)&x86_rsvd, 0,
414                    SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL));
415        }
416
417        /* General exceptions */
418        for (i = CPUVEC_MIN; i < CPUVEC_MAX; i++) {
419                idt_set_seg(&idt[i], (void *)x86_traps[i - CPUVEC_MIN], 0,
420                    SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL));
421        }
422
423        /* LAPIC interrupts */
424        for (i = LAPICVEC_MIN; i < LAPICVEC_MAX; i++) {
425                idt_set_seg(&idt[i], (void *)x86_intrs[i - LAPICVEC_MIN], 0,
426                    SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL));
427        }
428}
429
430void cpu_load_idt()
431{
432        struct region_descriptor region;
433        setregion(&region, &idtstore, PAGE_SIZE - 1);
434        lidt(&region);
435}
436
437/* -------------------------------------------------------------------------- */
438
439/*
440 * The gdt bitmap must be per-cluster.
441 */
442int tss_alloc(struct tss *tss)
443{
444        int slot;
445
446        /* Once we have proper SMP support, we will change that */
447        slot = GDT_CPU0TSS_SEL;
448
449        gdt_set_sysseg(GDT_ADDR_SYS(gdtstore, slot), tss,
450            sizeof(*tss) - 1, SDT_SYS386TSS, SEL_KPL, 0);
451
452        return GDT_DYNAM_SEL(slot, SEL_KPL);
453}
454
455void cpu_create_tss()
456{
457        struct tss *tss = &cpu0_tss;
458        int sel;
459
460        /* Create the tss */
461        memset(tss, 0, sizeof(*tss));
462        tss->tss_iobase = IOMAP_INVALOFF << 16;
463        tss->tss_ist[0] = (uint64_t)cpu0_intr_stack + STKSIZE;
464        tss->tss_ist[1] = (uint64_t)cpu0_dbfl_stack + STKSIZE;
465        tss->tss_ist[2] = (uint64_t)cpu0_nmfl_stack + STKSIZE;
466        sel = tss_alloc(tss);
467
468        /* Load it */
469        ltr(sel);
470}
471
472/* -------------------------------------------------------------------------- */
473
474void cpu_attach()
475{
476        cpu_load_gdt();
477        cpu_load_idt();
478        cpu_create_tss();
479}
480
Note: See TracBrowser for help on using the repository browser.