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

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

update

File size: 15.4 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        extern uint64_t __kernel_data_start;
225        extern uint64_t __kernel_end;
226
227        memset(info, 0, sizeof(boot_info_t));
228
229        info->signature = 0;
230
231        info->paddr_width = 0;
232        info->x_width = 1;
233        info->y_width = 1;
234        info->x_size = 1;
235        info->y_size = 1;
236        info->io_cxy = 0;
237
238        info->ext_dev_nr = 3;
239        init_bootinfo_txt(&info->ext_dev[0]);
240        init_bootinfo_pic(&info->ext_dev[1]);
241        init_bootinfo_ioc(&info->ext_dev[2]);
242
243        info->cxy = 0;
244        info->cores_nr = ncpu;
245        init_bootinfo_core((boot_core_t *)&info->core);
246
247        info->rsvd_nr = init_bootinfo_rsvd((boot_rsvd_t *)&info->rsvd);
248
249        /* TODO: dev_mmc */
250        /* TODO: dev_dma */
251
252        offset = hal_gpt_bootstrap_uniformize();
253        info->pages_offset = offset / PAGE_SIZE;
254        info->pages_nr = init_bootinfo_pages_nr();
255
256        info->kernel_code_start = (intptr_t)(KERNTEXTOFF - KERNBASE);
257        info->kernel_code_end = (intptr_t)(&__kernel_data_start - KERNBASE) - 1;
258        info->kernel_data_start = (intptr_t)(&__kernel_data_start - KERNBASE);
259        info->kernel_code_end = (intptr_t)(&__kernel_end - KERNBASE) - 1;
260}
261
262/* -------------------------------------------------------------------------- */
263
264static uint32_t cpuN_booted __in_kdata;
265
266void start_secondary_cpus()
267{
268        pt_entry_t flags = PG_V | PG_KW;
269        extern vaddr_t cpuN_boot_trampoline;
270        extern vaddr_t cpuN_boot_trampoline_end;
271        extern paddr_t smp_L4pa;
272        extern vaddr_t smp_stkva;
273        extern paddr_t L4paddr;
274        size_t i, sz;
275
276        smp_L4pa = L4paddr;
277
278        /* map the SMP trampoline (identity) */
279        vaddr_t trampva = (vaddr_t)SMP_TRAMPOLINE_PA;
280        hal_gpt_maptree_area(trampva, trampva + PAGE_SIZE);
281        hal_gpt_enter(trampva, SMP_TRAMPOLINE_PA, flags);
282
283        /* copy it */
284        sz = (size_t)&cpuN_boot_trampoline_end - (size_t)&cpuN_boot_trampoline;
285        memcpy((void *)trampva, (void *)&cpuN_boot_trampoline, sz);
286
287        for (i = 0; i < CONFIG_MAX_LOCAL_CORES; i++) {
288                if (i == 0 || !cpudata[i].valid) {
289                        continue;
290                }
291
292                smp_stkva = (vaddr_t)cpudata[i].boot_stack + STKSIZE;
293
294                cpuN_booted = 0;
295                boot_cpuN(i, SMP_TRAMPOLINE_PA);
296                while (!hal_atomic_cas(&cpuN_booted, 1, 0)) {
297                        /* wait */
298                }
299        }
300
301        // XXX: unmap the trampoline
302}
303
304void init_x86_64_cpuN()
305{
306        lid_t lid;
307
308        cli();
309
310        lid = hal_lapic_gid();
311
312        cpu_attach(lid);
313        x86_printf("[cpu%z] cpu_attach called\n", (uint64_t)lid);
314
315        cpu_tls_init(lid);
316        x86_printf("[cpu%z] cput_tls_init called\n", (uint64_t)lid);
317
318        cpu_lapic_init();
319        x86_printf("[cpu%z] cpu_lapic_init called\n", (uint64_t)lid);
320
321        cpuN_booted = 1;
322
323        if (lid == 1) {
324                hal_ioapic_disable_irq(IRQ_KEYBOARD);
325                hal_ioapic_bind_irq(IRQ_KEYBOARD, IOAPIC_KEYBOARD_VECTOR, 1);
326                hal_ioapic_enable_irq(IRQ_KEYBOARD);
327        }
328
329        kernel_init(&btinfo);
330
331        reg_t dummy;
332        hal_enable_irq(&dummy);
333
334        while (1);
335}
336
337/* -------------------------------------------------------------------------- */
338
339static void apic_map()
340{
341        extern vaddr_t lapic_va, ioapic_va;
342        extern paddr_t lapic_pa, ioapic_pa;
343
344        lapic_va = hal_gpt_bootstrap_valloc(1); // XXX: should be shared
345        hal_gpt_enter(lapic_va, lapic_pa, PG_V|PG_KW|PG_NX|PG_N);
346
347        ioapic_va = hal_gpt_bootstrap_valloc(1); // XXX: should be shared
348        hal_gpt_enter(ioapic_va, ioapic_pa, PG_V|PG_KW|PG_NX|PG_N);
349}
350
351void init_x86_64(paddr_t firstpa)
352{
353        cli();
354
355        /* Initialize the serial port */
356        hal_com_init_early();
357
358        x86_printf("[+] init_x86_64 called\n");
359
360        /* Create the global structures */
361        gdt_create();
362        idt_create();
363
364        /* Identify the features of the cpu */
365        cpu_identify();
366
367        /* Attach cpu0 */
368        cpu_attach(0);
369        x86_printf("[+] cpu_attach called\n");
370
371        x86_printf("[+] bootloader: '%s'\n", mb_loader_name);
372
373        dump_memmap();
374        x86_printf("[+] dump finished\n");
375
376        hal_gpt_init(firstpa);
377        x86_printf("[+] hal_gpt_init called\n");
378
379        hal_acpi_init();
380        x86_printf("[+] hal_acpi_init called\n");
381
382        hal_gpt_bootstrap_reset();
383        x86_printf("[+] hal_gpt_bootstrap_reset called\n");
384
385        apic_map();
386        x86_printf("[+] apic_map called\n");
387
388        hal_apic_init();
389        cpu_lapic_init();
390        x86_printf("[+] hal_apic_init called\n");
391
392        cpu_tls_init(0);
393        x86_printf("[+] cput_tls_init called\n");
394
395        mytest = 0;
396        x86_printf("-> mytest = %z\n", mytest);
397        void *hoho = &init_x86_64;
398        xptr_t myptr = XPTR(0, &mytest);
399
400        hal_remote_spt(myptr, hoho);
401        x86_printf("-> mytest = %Z\n", hal_remote_lpt(myptr));
402
403        init_bootinfo(&btinfo);
404
405        start_secondary_cpus();
406
407        kernel_init(&btinfo);
408
409        x86_printf("[+] kernel_init called\n");
410
411        reg_t dummy;
412        hal_enable_irq(&dummy);
413
414        while (1);
415
416/*
417        void *ptr;
418
419        khm_t *khm = &LOCAL_CLUSTER->khm;
420        ptr = khm_alloc(khm, 10);
421        memset(ptr, 0, 10);
422        khm_free(ptr);
423
424
425        kcm_t *kcm = &LOCAL_CLUSTER->kcm;
426        ptr = kcm_alloc(kcm);
427        memset(ptr, 0, 1);
428        kcm_free(ptr);
429
430        ptr = ppm_alloc_pages(1);
431        ppm_free_pages(ptr);
432*/
433
434
435//      void x86_stop();
436//      x86_stop();
437}
438
439/* -------------------------------------------------------------------------- */
440
441void cpu_activate(uint32_t gid)
442{
443        cpudata[gid].valid = true;
444}
445
446static void
447setregion(struct region_descriptor *rd, void *base, uint16_t limit)
448{
449        rd->rd_limit = limit;
450        rd->rd_base = (uint64_t)base;
451}
452
453/* -------------------------------------------------------------------------- */
454
455static void
456gdt_set_memseg(struct gdt_memseg *sd, void *base, size_t limit,
457        int type, int dpl, int gran, int is64)
458{
459        sd->sd_lolimit = (unsigned)limit;
460        sd->sd_lobase = (unsigned long)base;
461        sd->sd_type = type;
462        sd->sd_dpl = dpl;
463        sd->sd_p = 1;
464        sd->sd_hilimit = (unsigned)limit >> 16;
465        sd->sd_avl = 0;
466        sd->sd_long = is64;
467        sd->sd_def32 = 0;
468        sd->sd_gran = gran;
469        sd->sd_hibase = (unsigned long)base >> 24;
470}
471
472static void
473gdt_set_sysseg(struct gdt_sysseg *sd, void *base, size_t limit,
474        int type, int dpl, int gran)
475{
476        memset(sd, 0, sizeof *sd);
477        sd->sd_lolimit = (unsigned)limit;
478        sd->sd_lobase = (uint64_t)base;
479        sd->sd_type = type;
480        sd->sd_dpl = dpl;
481        sd->sd_p = 1;
482        sd->sd_hilimit = (unsigned)limit >> 16;
483        sd->sd_gran = gran;
484        sd->sd_hibase = (uint64_t)base >> 24;
485}
486
487static void gdt_create()
488{
489        memset(&gdtstore, 0, PAGE_SIZE);
490
491        /* Flat segments */
492        gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_KCODE_SEL), 0,
493            0xfffff, SDT_MEMERA, SEL_KPL, 1, 1);
494        gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_KDATA_SEL), 0,
495            0xfffff, SDT_MEMRWA, SEL_KPL, 1, 1);
496        gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_UCODE_SEL), 0,
497            0xfffff, SDT_MEMERA, SEL_UPL, 1, 1);
498        gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_UDATA_SEL), 0,
499            0xfffff, SDT_MEMRWA, SEL_UPL, 1, 1);
500}
501
502void cpu_load_gdt()
503{
504        struct region_descriptor region;
505        setregion(&region, &gdtstore, PAGE_SIZE - 1);
506        lgdt(&region);
507}
508
509/* -------------------------------------------------------------------------- */
510
511struct {
512        bool_t busy[256];
513} idt_bitmap __in_kdata;
514
515int idt_slot_alloc()
516{
517        size_t i;
518
519        for (i = 0; i < 256; i++) {
520                if (!idt_bitmap.busy[i])
521                        break;
522        }
523        if (i == 256) {
524                return -1;
525        }
526
527        idt_bitmap.busy[i] = true;
528        return (int)i;
529}
530
531void idt_slot_free(int slot)
532{
533        idt_bitmap.busy[slot] = false;
534}
535
536static void
537idt_set_seg(struct idt_seg *seg, void *func, int ist, int type, int dpl, int sel)
538{
539        seg->gd_looffset = (uint64_t)func & 0xffff;
540        seg->gd_selector = sel;
541        seg->gd_ist = ist;
542        seg->gd_type = type;
543        seg->gd_dpl = dpl;
544        seg->gd_p = 1;
545        seg->gd_hioffset = (uint64_t)func >> 16;
546        seg->gd_zero = 0;
547        seg->gd_xx1 = 0;
548        seg->gd_xx2 = 0;
549        seg->gd_xx3 = 0;
550}
551
552static void idt_create()
553{
554        extern uint64_t x86_traps[], x86_intrs[], x86_rsvd;
555        struct idt_seg *idt;
556        size_t i;
557
558        memset(&idt_bitmap, 0, sizeof(idt_bitmap));
559        idt = (struct idt_seg *)&idtstore;
560
561        /* First, put a dead entry */
562        for (i = 0; i < NIDT; i++) {
563                idt_set_seg(&idt[i], (void *)&x86_rsvd, 0,
564                    SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL));
565        }
566
567        /* General exceptions */
568        for (i = CPUVEC_MIN; i < CPUVEC_MAX; i++) {
569                idt_set_seg(&idt[i], (void *)x86_traps[i - CPUVEC_MIN], 0,
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[lid] + STKSIZE;
614        tss->tss_ist[1] = (uint64_t)data->dbfl_stack[lid] + STKSIZE;
615        tss->tss_ist[2] = (uint64_t)data->nmfl_stack[lid] + STKSIZE;
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.