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

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

Launch the secondary CPUs. For now, they all say hello and enter
an infinite loop.

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