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

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

Sync with TSAR.

File size: 15.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_acpi.h>
27#include <hal_apic.h>
28#include <hal_internal.h>
29#include <hal_register.h>
30
31#include <hal_remote.h>
32#include <hal_irqmask.h>
33
34#include <memcpy.h>
35#include <thread.h>
36#include <string.h>
37#include <process.h>
38#include <printk.h>
39#include <vmm.h>
40#include <core.h>
41#include <cluster.h>
42#include <chdev.h>
43
44#include <boot_info.h>
45
46void kernel_init(boot_info_t *info);
47
48static void gdt_create();
49static void idt_create();
50void cpu_tls_init(size_t lid);
51void cpu_identify();
52void cpu_attach(size_t lid);
53
54size_t mytest __in_kdata = 0;
55
56struct multiboot_info mb_info __in_kdata;
57char mb_loader_name[PAGE_SIZE] __in_kdata;
58uint8_t mb_mmap[PAGE_SIZE] __in_kdata;
59
60size_t ncpu __in_kdata = 0;
61static boot_info_t btinfo __in_kdata;
62
63/* x86-specific per-cluster structures */
64uint8_t gdtstore[PAGE_SIZE] __in_kdata;
65uint8_t idtstore[PAGE_SIZE] __in_kdata;
66
67/* x86-specific per-cpu structures */
68typedef struct {
69        bool_t valid;
70        struct tss tss;
71        struct tls tls;
72        uint8_t boot_stack[STKSIZE];
73        uint8_t intr_stack[STKSIZE];
74        uint8_t dbfl_stack[STKSIZE];
75        uint8_t nmfl_stack[STKSIZE];
76} percpu_archdata_t;
77percpu_archdata_t cpudata[CONFIG_MAX_LOCAL_CORES] __in_kdata;
78
79/* -------------------------------------------------------------------------- */
80
81static void
82dump_memmap()
83{
84        size_t mmap_length = mb_info.mi_mmap_length;
85        uint8_t *mmap_addr = (uint8_t *)&mb_mmap;
86        size_t i;
87
88        if (!(mb_info.mi_flags & MULTIBOOT_INFO_HAS_MMAP))
89                x86_panic("No mmap");
90
91        i = 0;
92        while (i < mmap_length) {
93                struct multiboot_mmap *mm;
94
95                mm = (struct multiboot_mmap *)(mmap_addr + i);
96
97                x86_printf("-> [%Z, %Z] %s\n", mm->mm_base_addr,
98                    mm->mm_base_addr + mm->mm_length,
99                    (mm->mm_type == 1) ? "ram" : "rsv" );
100
101                i += mm->mm_size + 4;
102        }
103}
104
105/* -------------------------------------------------------------------------- */
106
107static size_t init_bootinfo_pages_nr()
108{
109        size_t mmap_length = mb_info.mi_mmap_length;
110        uint8_t *mmap_addr = (uint8_t *)&mb_mmap;
111        paddr_t maxpa, pa;
112        size_t i;
113
114        i = 0;
115        maxpa = 0;
116        while (i < mmap_length) {
117                struct multiboot_mmap *mm;
118
119                mm = (struct multiboot_mmap *)(mmap_addr + i);
120
121                if (mm->mm_type == 1) {
122                        pa = mm->mm_base_addr + mm->mm_length;
123                        if (pa > maxpa)
124                                maxpa = pa;
125                }
126
127                i += mm->mm_size + 4;
128        }
129
130        return (maxpa / PAGE_SIZE);
131}
132
133static size_t init_bootinfo_rsvd(boot_rsvd_t *rsvd)
134{
135        size_t mmap_length = mb_info.mi_mmap_length;
136        uint8_t *mmap_addr = (uint8_t *)&mb_mmap;
137        size_t i, rsvd_nr;
138
139        memset(rsvd, 0, sizeof(boot_rsvd_t) * CONFIG_PPM_MAX_RSVD);
140
141        i = 0, rsvd_nr = 0;
142        while (i < mmap_length) {
143                struct multiboot_mmap *mm;
144
145                mm = (struct multiboot_mmap *)(mmap_addr + i);
146
147                if (mm->mm_type != 1) {
148                        rsvd[rsvd_nr].first_page =
149                            rounddown(mm->mm_base_addr, PAGE_SIZE) / PAGE_SIZE;
150                        rsvd[rsvd_nr].npages =
151                            roundup(mm->mm_length, PAGE_SIZE) / PAGE_SIZE;
152                        rsvd_nr++;
153                        if (rsvd_nr == CONFIG_PPM_MAX_RSVD)
154                                x86_panic("too many memory holes");
155                }
156
157                i += mm->mm_size + 4;
158        }
159
160        return rsvd_nr;
161}
162
163static void init_bootinfo_core(boot_core_t *core)
164{
165        size_t i;
166
167        // XXX: not necessarily contiguous
168        for (i = 0; i < ncpu; i++) {
169                memset(&core[i], 0, sizeof(boot_core_t));
170
171                core[i].gid = i;
172                core[i].lid = i;
173                core[i].cxy = 0;
174        }
175}
176
177static void init_bootinfo_ioc(boot_device_t *dev)
178{
179        memset(dev, 0, sizeof(boot_device_t));
180
181        dev->base = 0;
182        dev->type = (DEV_FUNC_IOC << 16) | IMPL_IOC_BDV;
183        dev->channels = 1;
184}
185
186static void init_bootinfo_pic(boot_device_t *dev)
187{
188        memset(dev, 0, sizeof(boot_device_t));
189
190        dev->base = 0;
191        dev->type = (DEV_FUNC_PIC << 16) | IMPL_PIC_I86;
192        dev->channels = 1;
193        dev->param0 = 0;
194        dev->param1 = 0;
195        dev->param2 = 0;
196        dev->param3 = 0;
197
198        dev->irqs = 16;
199
200        /* ATA */
201        dev->irq[IRQ_ATA0].dev_type = (DEV_FUNC_IOC << 16) | IMPL_IOC_BDV;
202        dev->irq[IRQ_ATA0].channel = 0;
203        dev->irq[IRQ_ATA0].is_rx = 0;
204        dev->irq[IRQ_ATA0].valid = 1;
205}
206
207static void init_bootinfo_txt(boot_device_t *dev)
208{
209        memset(dev, 0, sizeof(boot_device_t));
210
211        dev->base = 0;
212        dev->type = (DEV_FUNC_TXT << 16) | IMPL_TXT_RS2;
213        dev->channels = 4;
214        dev->param0 = 0;
215        dev->param1 = 0;
216        dev->param2 = 0;
217        dev->param3 = 0;
218}
219
220static void init_bootinfo(boot_info_t *info)
221{
222        size_t offset;
223
224        memset(info, 0, sizeof(boot_info_t));
225
226        info->signature = 0;
227
228        info->paddr_width = 0;
229        info->x_width = 1;
230        info->y_width = 1;
231        info->x_size = 1;
232        info->y_size = 1;
233        info->io_cxy = 0;
234
235        info->ext_dev_nr = 3;
236        init_bootinfo_txt(&info->ext_dev[0]);
237        init_bootinfo_pic(&info->ext_dev[1]);
238        init_bootinfo_ioc(&info->ext_dev[2]);
239
240        info->cxy = 0;
241        info->cores_nr = ncpu;
242        init_bootinfo_core((boot_core_t *)&info->core);
243
244        info->rsvd_nr = init_bootinfo_rsvd((boot_rsvd_t *)&info->rsvd);
245
246        /* TODO: dev_mmc */
247        /* TODO: dev_dma */
248
249        offset = hal_gpt_bootstrap_uniformize();
250        info->pages_offset = offset / PAGE_SIZE;
251        info->pages_nr = init_bootinfo_pages_nr();
252}
253
254/* -------------------------------------------------------------------------- */
255
256static uint32_t cpuN_booted __in_kdata;
257
258void start_secondary_cpus()
259{
260        pt_entry_t flags = PG_V | PG_KW;
261        extern vaddr_t cpuN_boot_trampoline;
262        extern vaddr_t cpuN_boot_trampoline_end;
263        extern paddr_t smp_L4pa;
264        extern vaddr_t smp_stkva;
265        extern paddr_t L4paddr;
266        size_t i, sz;
267
268        smp_L4pa = L4paddr;
269
270        /* map the SMP trampoline (identity) */
271        vaddr_t trampva = (vaddr_t)SMP_TRAMPOLINE_PA;
272        hal_gpt_maptree_area(trampva, trampva + PAGE_SIZE);
273        hal_gpt_enter(trampva, SMP_TRAMPOLINE_PA, flags);
274
275        /* copy it */
276        sz = (size_t)&cpuN_boot_trampoline_end - (size_t)&cpuN_boot_trampoline;
277        memcpy((void *)trampva, (void *)&cpuN_boot_trampoline, sz);
278
279        for (i = 0; i < CONFIG_MAX_LOCAL_CORES; i++) {
280                if (i == 0 || !cpudata[i].valid) {
281                        continue;
282                }
283
284                smp_stkva = ((vaddr_t)&cpudata[i].boot_stack + STKSIZE) & ~0xF;
285
286                cpuN_booted = 0;
287                boot_cpuN(i, SMP_TRAMPOLINE_PA);
288                while (!hal_atomic_cas(&cpuN_booted, 1, 0)) {
289                        /* wait */
290                }
291        }
292
293        // XXX: unmap the trampoline
294}
295
296void init_x86_64_cpuN()
297{
298        lid_t lid;
299
300        cli();
301
302        lid = hal_lapic_gid();
303
304        cpu_attach(lid);
305        x86_printf("[cpu%z] cpu_attach called\n", (uint64_t)lid);
306
307        cpu_tls_init(lid);
308        x86_printf("[cpu%z] cput_tls_init called\n", (uint64_t)lid);
309
310        cpu_lapic_init();
311        x86_printf("[cpu%z] cpu_lapic_init called\n", (uint64_t)lid);
312
313        cpuN_booted = 1;
314
315        if (lid == 1) {
316                hal_ioapic_disable_irq(IRQ_KEYBOARD);
317                hal_ioapic_bind_irq(IRQ_KEYBOARD, IOAPIC_KEYBOARD_VECTOR, 1);
318                hal_ioapic_enable_irq(IRQ_KEYBOARD);
319        }
320
321        kernel_init(&btinfo);
322
323        reg_t dummy;
324        hal_enable_irq(&dummy);
325
326        while (1);
327}
328
329/* -------------------------------------------------------------------------- */
330
331static void apic_map()
332{
333        extern vaddr_t lapic_va, ioapic_va;
334        extern paddr_t lapic_pa, ioapic_pa;
335
336        lapic_va = hal_gpt_bootstrap_valloc(1); // XXX: should be shared
337        hal_gpt_enter(lapic_va, lapic_pa, PG_V|PG_KW|PG_NX|PG_N);
338
339        ioapic_va = hal_gpt_bootstrap_valloc(1); // XXX: should be shared
340        hal_gpt_enter(ioapic_va, ioapic_pa, PG_V|PG_KW|PG_NX|PG_N);
341}
342
343void init_x86_64(paddr_t firstpa)
344{
345        cli();
346
347        /* Initialize the serial port */
348        hal_com_init_early();
349
350        x86_printf("[+] init_x86_64 called\n");
351
352        /* Create the global structures */
353        gdt_create();
354        idt_create();
355
356        /* Identify the features of the cpu */
357        cpu_identify();
358
359        /* Attach cpu0 */
360        cpu_attach(0);
361        x86_printf("[+] cpu_attach called\n");
362
363        x86_printf("[+] bootloader: '%s'\n", mb_loader_name);
364
365        dump_memmap();
366        x86_printf("[+] dump finished\n");
367
368        hal_gpt_init(firstpa);
369        x86_printf("[+] hal_gpt_init called\n");
370
371        hal_acpi_init();
372        x86_printf("[+] hal_acpi_init called\n");
373
374        hal_gpt_bootstrap_reset();
375        x86_printf("[+] hal_gpt_bootstrap_reset called\n");
376
377        apic_map();
378        x86_printf("[+] apic_map called\n");
379
380        hal_apic_init();
381        cpu_lapic_init();
382        x86_printf("[+] hal_apic_init called\n");
383
384        cpu_tls_init(0);
385        x86_printf("[+] cput_tls_init called\n");
386
387        mytest = 0;
388        x86_printf("-> mytest = %z\n", mytest);
389        void *hoho = &init_x86_64;
390        xptr_t myptr = XPTR(0, &mytest);
391
392        hal_remote_spt(myptr, hoho);
393        x86_printf("-> mytest = %Z\n", hal_remote_lpt(myptr));
394
395        init_bootinfo(&btinfo);
396
397        start_secondary_cpus();
398
399        kernel_init(&btinfo);
400
401        x86_printf("[+] kernel_init called\n");
402
403        reg_t dummy;
404        hal_enable_irq(&dummy);
405
406        while (1);
407
408/*
409        void *ptr;
410
411        khm_t *khm = &LOCAL_CLUSTER->khm;
412        ptr = khm_alloc(khm, 10);
413        memset(ptr, 0, 10);
414        khm_free(ptr);
415
416
417        kcm_t *kcm = &LOCAL_CLUSTER->kcm;
418        ptr = kcm_alloc(kcm);
419        memset(ptr, 0, 1);
420        kcm_free(ptr);
421
422        ptr = ppm_alloc_pages(1);
423        ppm_free_pages(ptr);
424*/
425
426
427//      void x86_stop();
428//      x86_stop();
429}
430
431/* -------------------------------------------------------------------------- */
432
433void cpu_activate(uint32_t gid)
434{
435        cpudata[gid].valid = true;
436}
437
438static void
439setregion(struct region_descriptor *rd, void *base, uint16_t limit)
440{
441        rd->rd_limit = limit;
442        rd->rd_base = (uint64_t)base;
443}
444
445/* -------------------------------------------------------------------------- */
446
447static void
448gdt_set_memseg(struct gdt_memseg *sd, void *base, size_t limit,
449        int type, int dpl, int gran, int is64)
450{
451        sd->sd_lolimit = (unsigned)limit;
452        sd->sd_lobase = (unsigned long)base;
453        sd->sd_type = type;
454        sd->sd_dpl = dpl;
455        sd->sd_p = 1;
456        sd->sd_hilimit = (unsigned)limit >> 16;
457        sd->sd_avl = 0;
458        sd->sd_long = is64;
459        sd->sd_def32 = 0;
460        sd->sd_gran = gran;
461        sd->sd_hibase = (unsigned long)base >> 24;
462}
463
464static void
465gdt_set_sysseg(struct gdt_sysseg *sd, void *base, size_t limit,
466        int type, int dpl, int gran)
467{
468        memset(sd, 0, sizeof *sd);
469        sd->sd_lolimit = (unsigned)limit;
470        sd->sd_lobase = (uint64_t)base;
471        sd->sd_type = type;
472        sd->sd_dpl = dpl;
473        sd->sd_p = 1;
474        sd->sd_hilimit = (unsigned)limit >> 16;
475        sd->sd_gran = gran;
476        sd->sd_hibase = (uint64_t)base >> 24;
477}
478
479static void gdt_create()
480{
481        memset(&gdtstore, 0, PAGE_SIZE);
482
483        /* Flat segments */
484        gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_KCODE_SEL), 0,
485            0xfffff, SDT_MEMERA, SEL_KPL, 1, 1);
486        gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_KDATA_SEL), 0,
487            0xfffff, SDT_MEMRWA, SEL_KPL, 1, 1);
488        gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_UCODE_SEL), 0,
489            0xfffff, SDT_MEMERA, SEL_UPL, 1, 1);
490        gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_UDATA_SEL), 0,
491            0xfffff, SDT_MEMRWA, SEL_UPL, 1, 1);
492}
493
494void cpu_load_gdt()
495{
496        struct region_descriptor region;
497        setregion(&region, &gdtstore, PAGE_SIZE - 1);
498        lgdt(&region);
499}
500
501/* -------------------------------------------------------------------------- */
502
503struct {
504        bool_t busy[256];
505} idt_bitmap __in_kdata;
506
507int idt_slot_alloc()
508{
509        size_t i;
510
511        for (i = 0; i < 256; i++) {
512                if (!idt_bitmap.busy[i])
513                        break;
514        }
515        if (i == 256) {
516                return -1;
517        }
518
519        idt_bitmap.busy[i] = true;
520        return (int)i;
521}
522
523void idt_slot_free(int slot)
524{
525        idt_bitmap.busy[slot] = false;
526}
527
528static void
529idt_set_seg(struct idt_seg *seg, void *func, int ist, int type, int dpl, int sel)
530{
531        seg->gd_looffset = (uint64_t)func & 0xffff;
532        seg->gd_selector = sel;
533        seg->gd_ist = ist;
534        seg->gd_type = type;
535        seg->gd_dpl = dpl;
536        seg->gd_p = 1;
537        seg->gd_hioffset = (uint64_t)func >> 16;
538        seg->gd_zero = 0;
539        seg->gd_xx1 = 0;
540        seg->gd_xx2 = 0;
541        seg->gd_xx3 = 0;
542}
543
544static void idt_create()
545{
546        extern uint64_t x86_traps[], x86_intrs[], x86_rsvd;
547        struct idt_seg *idt;
548        size_t i;
549        int ist;
550
551        memset(&idt_bitmap, 0, sizeof(idt_bitmap));
552        idt = (struct idt_seg *)&idtstore;
553
554        /* First, put a dead entry */
555        for (i = 0; i < NIDT; i++) {
556                idt_set_seg(&idt[i], (void *)&x86_rsvd, 0,
557                    SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL));
558        }
559
560        /* General exceptions */
561        for (i = CPUVEC_MIN; i < CPUVEC_MAX; i++) {
562                if (i == 2) { /* NMI */
563                        ist = 3;
564                } else if (i == 8) { /* Double Fault */
565                        ist = 2;
566                } else {
567                        ist = 0;
568                }
569                idt_set_seg(&idt[i], (void *)x86_traps[i - CPUVEC_MIN], ist,
570                    SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL));
571                idt_bitmap.busy[i] = true;
572        }
573
574        /* Dynamically configured interrupts */
575        for (i = DYNVEC_MIN; i < DYNVEC_MAX; i++) {
576                idt_set_seg(&idt[i], (void *)x86_intrs[i - DYNVEC_MIN], 0,
577                    SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL));
578                idt_bitmap.busy[i] = true;
579        }
580}
581
582void cpu_load_idt()
583{
584        struct region_descriptor region;
585        setregion(&region, &idtstore, PAGE_SIZE - 1);
586        lidt(&region);
587}
588
589/* -------------------------------------------------------------------------- */
590
591int tss_alloc(struct tss *tss, size_t lid)
592{
593        int slot;
594
595        slot = GDT_CPUTSS_SEL + lid;
596
597        gdt_set_sysseg(GDT_ADDR_SYS(gdtstore, slot), tss,
598            sizeof(*tss) - 1, SDT_SYS386TSS, SEL_KPL, 0);
599
600        return GDT_DYNAM_SEL(slot, SEL_KPL);
601}
602
603void cpu_create_tss(size_t lid)
604{
605        percpu_archdata_t *data = &cpudata[lid];
606        struct tss *tss = &data->tss;
607        int sel;
608
609        /* Create the tss */
610        memset(tss, 0, sizeof(*tss));
611
612        /* tss->tss_rsp0 */
613        tss->tss_ist[0] = ((uint64_t)&data->intr_stack + STKSIZE) & ~0xF;
614        tss->tss_ist[1] = ((uint64_t)&data->dbfl_stack + STKSIZE) & ~0xF;
615        tss->tss_ist[2] = ((uint64_t)&data->nmfl_stack + STKSIZE) & ~0xF;
616        tss->tss_iobase = IOMAP_INVALOFF << 16;
617        sel = tss_alloc(tss, lid);
618
619        /* Load it */
620        ltr(sel);
621}
622
623/* -------------------------------------------------------------------------- */
624
625void cpu_tls_init(size_t lid)
626{
627        percpu_archdata_t *data = &cpudata[lid];
628        tls_t *cputls = &data->tls;
629
630        memset(cputls, 0, sizeof(tls_t));
631
632        cputls->tls_self = cputls;
633        cputls->tls_gid = hal_lapic_gid();
634        cputls->tls_lid = lid;
635        cputls->tls_intr = INTRS_DISABLED;
636
637        wrmsr(MSR_FSBASE, 0);
638        wrmsr(MSR_GSBASE, (uint64_t)cputls);
639        wrmsr(MSR_KERNELGSBASE, 0);
640}
641
642/* -------------------------------------------------------------------------- */
643
644uint64_t cpu_features[4] __in_kdata;
645
646void cpu_identify()
647{
648        /*
649         * desc[0] = eax
650         * desc[1] = ebx
651         * desc[2] = ecx
652         * desc[3] = edx
653         */
654        uint32_t desc[4];
655        char vendor[13];
656        size_t lvl;
657
658        /*
659         * Get information from the standard cpuid leafs
660         */
661        cpuid(0, 0, (uint32_t *)&desc);
662
663        lvl = (uint64_t)desc[0];
664        x86_printf("-> cpuid standard level: %z\n", lvl);
665
666        memcpy(vendor + 0, &desc[1], sizeof(uint32_t));
667        memcpy(vendor + 8, &desc[2], sizeof(uint32_t));
668        memcpy(vendor + 4, &desc[3], sizeof(uint32_t));
669        vendor[12] = '\0';
670        x86_printf("-> CPU vendor: '%s'\n", vendor);
671
672        if (lvl >= 1) {
673                cpuid(1, 0, (uint32_t *)&desc);
674                cpu_features[0] = desc[3];
675                cpu_features[1] = desc[2];
676        }
677
678        /*
679         * Get information from the extended cpuid leafs
680         */
681        cpuid(0x80000000, 0, desc);
682
683        lvl = (uint64_t)desc[0];
684        x86_printf("-> cpuid extended level: %Z\n", lvl);
685}
686
687/* -------------------------------------------------------------------------- */
688
689void cpu_attach(size_t lid)
690{
691        /* Per-cluster structures */
692        cpu_load_gdt();
693        cpu_load_idt();
694
695        /* Per-cpu structures */
696        cpu_create_tss(lid);
697
698        if (cpu_features[0] & CPUID_PSE) {
699                lcr4(rcr4() | CR4_PSE);
700                tlbflushg();
701        } else {
702                /*
703                 * amd64 supports PSE by default, if it's not here we have a
704                 * problem
705                 */
706                x86_panic("PSE not supported");
707        }
708}
709
Note: See TracBrowser for help on using the repository browser.