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

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

Use IMPL_TXT_RS2.

File size: 15.6 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        /* COM1 */
201        dev->irq[IRQ_COM1].dev_type = (DEV_FUNC_TXT << 16) | IMPL_TXT_RS2;
202        dev->irq[IRQ_COM1].channel = 0;
203        dev->irq[IRQ_COM1].is_rx = 0;
204        dev->irq[IRQ_COM1].valid = 1;
205
206        /* ATA */
207        dev->irq[IRQ_ATA0].dev_type = (DEV_FUNC_IOC << 16) | IMPL_IOC_BDV;
208        dev->irq[IRQ_ATA0].channel = 0;
209        dev->irq[IRQ_ATA0].is_rx = 0;
210        dev->irq[IRQ_ATA0].valid = 1;
211}
212
213static void init_bootinfo_txt(boot_device_t *dev)
214{
215        memset(dev, 0, sizeof(boot_device_t));
216
217        dev->base = 0;
218        dev->type = (DEV_FUNC_TXT << 16) | IMPL_TXT_RS2;
219        dev->channels = 1;
220        dev->param0 = 0;
221        dev->param1 = 0;
222        dev->param2 = 0;
223        dev->param3 = 0;
224}
225
226static void init_bootinfo(boot_info_t *info)
227{
228        size_t offset;
229
230        extern uint64_t __kernel_data_start;
231        extern uint64_t __kernel_end;
232
233        memset(info, 0, sizeof(boot_info_t));
234
235        info->signature = 0;
236
237        info->paddr_width = 0;
238        info->x_width = 1;
239        info->y_width = 1;
240        info->x_size = 1;
241        info->y_size = 1;
242        info->io_cxy = 0;
243
244        info->ext_dev_nr = 3;
245        init_bootinfo_txt(&info->ext_dev[0]);
246        init_bootinfo_pic(&info->ext_dev[1]);
247        init_bootinfo_ioc(&info->ext_dev[2]);
248
249        info->cxy = 0;
250        info->cores_nr = ncpu;
251        init_bootinfo_core((boot_core_t *)&info->core);
252
253        info->rsvd_nr = init_bootinfo_rsvd((boot_rsvd_t *)&info->rsvd);
254
255        /* TODO: dev_mmc */
256        /* TODO: dev_dma */
257
258        offset = hal_gpt_bootstrap_uniformize();
259        info->pages_offset = offset / PAGE_SIZE;
260        info->pages_nr = init_bootinfo_pages_nr();
261
262        info->kernel_code_start = (intptr_t)(KERNTEXTOFF - KERNBASE);
263        info->kernel_code_end = (intptr_t)(&__kernel_data_start - KERNBASE) - 1;
264        info->kernel_data_start = (intptr_t)(&__kernel_data_start - KERNBASE);
265        info->kernel_code_end = (intptr_t)(&__kernel_end - KERNBASE) - 1;
266}
267
268/* -------------------------------------------------------------------------- */
269
270static uint32_t cpuN_booted __in_kdata;
271
272void start_secondary_cpus()
273{
274        pt_entry_t flags = PG_V | PG_KW;
275        extern vaddr_t cpuN_boot_trampoline;
276        extern vaddr_t cpuN_boot_trampoline_end;
277        extern paddr_t smp_L4pa;
278        extern vaddr_t smp_stkva;
279        extern paddr_t L4paddr;
280        size_t i, sz;
281
282        smp_L4pa = L4paddr;
283
284        /* map the SMP trampoline (identity) */
285        vaddr_t trampva = (vaddr_t)SMP_TRAMPOLINE_PA;
286        hal_gpt_maptree_area(trampva, trampva + PAGE_SIZE);
287        hal_gpt_enter(trampva, SMP_TRAMPOLINE_PA, flags);
288
289        /* copy it */
290        sz = (size_t)&cpuN_boot_trampoline_end - (size_t)&cpuN_boot_trampoline;
291        memcpy((void *)trampva, (void *)&cpuN_boot_trampoline, sz);
292
293        for (i = 0; i < CONFIG_MAX_LOCAL_CORES; i++) {
294                if (i == 0 || !cpudata[i].valid) {
295                        continue;
296                }
297
298                smp_stkva = (vaddr_t)cpudata[i].boot_stack + STKSIZE;
299
300                cpuN_booted = 0;
301                boot_cpuN(i, SMP_TRAMPOLINE_PA);
302                while (!hal_atomic_cas(&cpuN_booted, 1, 0)) {
303                        /* wait */
304                }
305        }
306
307        // XXX: unmap the trampoline
308}
309
310void init_x86_64_cpuN()
311{
312        lid_t lid;
313
314        cli();
315
316        lid = hal_lapic_gid();
317
318        cpu_attach(lid);
319        x86_printf("[cpu%z] cpu_attach called\n", (uint64_t)lid);
320
321        cpu_tls_init(lid);
322        x86_printf("[cpu%z] cput_tls_init called\n", (uint64_t)lid);
323
324        cpu_lapic_init();
325        x86_printf("[cpu%z] cpu_lapic_init called\n", (uint64_t)lid);
326
327        cpuN_booted = 1;
328
329        if (lid == 1) {
330                hal_ioapic_disable_irq(IRQ_KEYBOARD);
331                hal_ioapic_bind_irq(IRQ_KEYBOARD, IOAPIC_KEYBOARD_VECTOR, 1);
332                hal_ioapic_enable_irq(IRQ_KEYBOARD);
333        }
334
335        kernel_init(&btinfo);
336
337        reg_t dummy;
338        hal_enable_irq(&dummy);
339
340        while (1);
341}
342
343/* -------------------------------------------------------------------------- */
344
345static void apic_map()
346{
347        extern vaddr_t lapic_va, ioapic_va;
348        extern paddr_t lapic_pa, ioapic_pa;
349
350        lapic_va = hal_gpt_bootstrap_valloc(1); // XXX: should be shared
351        hal_gpt_enter(lapic_va, lapic_pa, PG_V|PG_KW|PG_NX|PG_N);
352
353        ioapic_va = hal_gpt_bootstrap_valloc(1); // XXX: should be shared
354        hal_gpt_enter(ioapic_va, ioapic_pa, PG_V|PG_KW|PG_NX|PG_N);
355}
356
357void init_x86_64(paddr_t firstpa)
358{
359        cli();
360
361        /* Initialize the serial port */
362        hal_com_init_early();
363
364        x86_printf("[+] init_x86_64 called\n");
365
366        /* Create the global structures */
367        gdt_create();
368        idt_create();
369
370        /* Identify the features of the cpu */
371        cpu_identify();
372
373        /* Attach cpu0 */
374        cpu_attach(0);
375        x86_printf("[+] cpu_attach called\n");
376
377        x86_printf("[+] bootloader: '%s'\n", mb_loader_name);
378
379        dump_memmap();
380        x86_printf("[+] dump finished\n");
381
382        hal_gpt_init(firstpa);
383        x86_printf("[+] hal_gpt_init called\n");
384
385        hal_acpi_init();
386        x86_printf("[+] hal_acpi_init called\n");
387
388        hal_gpt_bootstrap_reset();
389        x86_printf("[+] hal_gpt_bootstrap_reset called\n");
390
391        apic_map();
392        x86_printf("[+] apic_map called\n");
393
394        hal_apic_init();
395        cpu_lapic_init();
396        x86_printf("[+] hal_apic_init called\n");
397
398        cpu_tls_init(0);
399        x86_printf("[+] cput_tls_init called\n");
400
401        mytest = 0;
402        x86_printf("-> mytest = %z\n", mytest);
403        void *hoho = &init_x86_64;
404        xptr_t myptr = XPTR(0, &mytest);
405
406        hal_remote_spt(myptr, hoho);
407        x86_printf("-> mytest = %Z\n", hal_remote_lpt(myptr));
408
409        init_bootinfo(&btinfo);
410
411        start_secondary_cpus();
412
413        kernel_init(&btinfo);
414
415        x86_printf("[+] kernel_init called\n");
416
417        reg_t dummy;
418        hal_enable_irq(&dummy);
419/*
420        void *ptr;
421
422        khm_t *khm = &LOCAL_CLUSTER->khm;
423        ptr = khm_alloc(khm, 10);
424        memset(ptr, 0, 10);
425        khm_free(ptr);
426
427
428        kcm_t *kcm = &LOCAL_CLUSTER->kcm;
429        ptr = kcm_alloc(kcm);
430        memset(ptr, 0, 1);
431        kcm_free(ptr);
432
433        ptr = ppm_alloc_pages(1);
434        ppm_free_pages(ptr);
435*/
436        while (1);
437
438//      void x86_stop();
439//      x86_stop();
440}
441
442/* -------------------------------------------------------------------------- */
443
444void cpu_activate(uint32_t gid)
445{
446        cpudata[gid].valid = true;
447}
448
449static void
450setregion(struct region_descriptor *rd, void *base, uint16_t limit)
451{
452        rd->rd_limit = limit;
453        rd->rd_base = (uint64_t)base;
454}
455
456/* -------------------------------------------------------------------------- */
457
458static void
459gdt_set_memseg(struct gdt_memseg *sd, void *base, size_t limit,
460        int type, int dpl, int gran, int is64)
461{
462        sd->sd_lolimit = (unsigned)limit;
463        sd->sd_lobase = (unsigned long)base;
464        sd->sd_type = type;
465        sd->sd_dpl = dpl;
466        sd->sd_p = 1;
467        sd->sd_hilimit = (unsigned)limit >> 16;
468        sd->sd_avl = 0;
469        sd->sd_long = is64;
470        sd->sd_def32 = 0;
471        sd->sd_gran = gran;
472        sd->sd_hibase = (unsigned long)base >> 24;
473}
474
475static void
476gdt_set_sysseg(struct gdt_sysseg *sd, void *base, size_t limit,
477        int type, int dpl, int gran)
478{
479        memset(sd, 0, sizeof *sd);
480        sd->sd_lolimit = (unsigned)limit;
481        sd->sd_lobase = (uint64_t)base;
482        sd->sd_type = type;
483        sd->sd_dpl = dpl;
484        sd->sd_p = 1;
485        sd->sd_hilimit = (unsigned)limit >> 16;
486        sd->sd_gran = gran;
487        sd->sd_hibase = (uint64_t)base >> 24;
488}
489
490static void gdt_create()
491{
492        memset(&gdtstore, 0, PAGE_SIZE);
493
494        /* Flat segments */
495        gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_KCODE_SEL), 0,
496            0xfffff, SDT_MEMERA, SEL_KPL, 1, 1);
497        gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_KDATA_SEL), 0,
498            0xfffff, SDT_MEMRWA, SEL_KPL, 1, 1);
499        gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_UCODE_SEL), 0,
500            0xfffff, SDT_MEMERA, SEL_UPL, 1, 1);
501        gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_UDATA_SEL), 0,
502            0xfffff, SDT_MEMRWA, SEL_UPL, 1, 1);
503}
504
505void cpu_load_gdt()
506{
507        struct region_descriptor region;
508        setregion(&region, &gdtstore, PAGE_SIZE - 1);
509        lgdt(&region);
510}
511
512/* -------------------------------------------------------------------------- */
513
514struct {
515        bool_t busy[256];
516} idt_bitmap __in_kdata;
517
518int idt_slot_alloc()
519{
520        size_t i;
521
522        for (i = 0; i < 256; i++) {
523                if (!idt_bitmap.busy[i])
524                        break;
525        }
526        if (i == 256) {
527                return -1;
528        }
529
530        idt_bitmap.busy[i] = true;
531        return (int)i;
532}
533
534void idt_slot_free(int slot)
535{
536        idt_bitmap.busy[slot] = false;
537}
538
539static void
540idt_set_seg(struct idt_seg *seg, void *func, int ist, int type, int dpl, int sel)
541{
542        seg->gd_looffset = (uint64_t)func & 0xffff;
543        seg->gd_selector = sel;
544        seg->gd_ist = ist;
545        seg->gd_type = type;
546        seg->gd_dpl = dpl;
547        seg->gd_p = 1;
548        seg->gd_hioffset = (uint64_t)func >> 16;
549        seg->gd_zero = 0;
550        seg->gd_xx1 = 0;
551        seg->gd_xx2 = 0;
552        seg->gd_xx3 = 0;
553}
554
555static void idt_create()
556{
557        extern uint64_t x86_traps[], x86_intrs[], x86_rsvd;
558        struct idt_seg *idt;
559        size_t i;
560
561        memset(&idt_bitmap, 0, sizeof(idt_bitmap));
562        idt = (struct idt_seg *)&idtstore;
563
564        /* First, put a dead entry */
565        for (i = 0; i < NIDT; i++) {
566                idt_set_seg(&idt[i], (void *)&x86_rsvd, 0,
567                    SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL));
568        }
569
570        /* General exceptions */
571        for (i = CPUVEC_MIN; i < CPUVEC_MAX; i++) {
572                idt_set_seg(&idt[i], (void *)x86_traps[i - CPUVEC_MIN], 0,
573                    SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL));
574                idt_bitmap.busy[i] = true;
575        }
576
577        /* Dynamically configured interrupts */
578        for (i = DYNVEC_MIN; i < DYNVEC_MAX; i++) {
579                idt_set_seg(&idt[i], (void *)x86_intrs[i - DYNVEC_MIN], 0,
580                    SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL));
581                idt_bitmap.busy[i] = true;
582        }
583}
584
585void cpu_load_idt()
586{
587        struct region_descriptor region;
588        setregion(&region, &idtstore, PAGE_SIZE - 1);
589        lidt(&region);
590}
591
592/* -------------------------------------------------------------------------- */
593
594int tss_alloc(struct tss *tss, size_t lid)
595{
596        int slot;
597
598        slot = GDT_CPUTSS_SEL + lid;
599
600        gdt_set_sysseg(GDT_ADDR_SYS(gdtstore, slot), tss,
601            sizeof(*tss) - 1, SDT_SYS386TSS, SEL_KPL, 0);
602
603        return GDT_DYNAM_SEL(slot, SEL_KPL);
604}
605
606void cpu_create_tss(size_t lid)
607{
608        percpu_archdata_t *data = &cpudata[lid];
609        struct tss *tss = &data->tss;
610        int sel;
611
612        /* Create the tss */
613        memset(tss, 0, sizeof(*tss));
614
615        /* tss->tss_rsp0 */
616        tss->tss_ist[0] = (uint64_t)data->intr_stack[lid] + STKSIZE;
617        tss->tss_ist[1] = (uint64_t)data->dbfl_stack[lid] + STKSIZE;
618        tss->tss_ist[2] = (uint64_t)data->nmfl_stack[lid] + STKSIZE;
619        tss->tss_iobase = IOMAP_INVALOFF << 16;
620        sel = tss_alloc(tss, lid);
621
622        /* Load it */
623        ltr(sel);
624}
625
626/* -------------------------------------------------------------------------- */
627
628void cpu_tls_init(size_t lid)
629{
630        percpu_archdata_t *data = &cpudata[lid];
631        tls_t *cputls = &data->tls;
632
633        memset(cputls, 0, sizeof(tls_t));
634
635        cputls->tls_self = cputls;
636        cputls->tls_gid = hal_lapic_gid();
637        cputls->tls_lid = lid;
638        cputls->tls_intr = INTRS_DISABLED;
639
640        wrmsr(MSR_FSBASE, 0);
641        wrmsr(MSR_GSBASE, (uint64_t)cputls);
642        wrmsr(MSR_KERNELGSBASE, 0);
643}
644
645/* -------------------------------------------------------------------------- */
646
647uint64_t cpu_features[4] __in_kdata;
648
649void cpu_identify()
650{
651        /*
652         * desc[0] = eax
653         * desc[1] = ebx
654         * desc[2] = ecx
655         * desc[3] = edx
656         */
657        uint32_t desc[4];
658        char vendor[13];
659        size_t lvl;
660
661        /*
662         * Get information from the standard cpuid leafs
663         */
664        cpuid(0, 0, (uint32_t *)&desc);
665
666        lvl = (uint64_t)desc[0];
667        x86_printf("-> cpuid standard level: %z\n", lvl);
668
669        memcpy(vendor + 0, &desc[1], sizeof(uint32_t));
670        memcpy(vendor + 8, &desc[2], sizeof(uint32_t));
671        memcpy(vendor + 4, &desc[3], sizeof(uint32_t));
672        vendor[12] = '\0';
673        x86_printf("-> CPU vendor: '%s'\n", vendor);
674
675        if (lvl >= 1) {
676                cpuid(1, 0, (uint32_t *)&desc);
677                cpu_features[0] = desc[3];
678                cpu_features[1] = desc[2];
679        }
680
681        /*
682         * Get information from the extended cpuid leafs
683         */
684        cpuid(0x80000000, 0, desc);
685
686        lvl = (uint64_t)desc[0];
687        x86_printf("-> cpuid extended level: %Z\n", lvl);
688}
689
690/* -------------------------------------------------------------------------- */
691
692void cpu_attach(size_t lid)
693{
694        /* Per-cluster structures */
695        cpu_load_gdt();
696        cpu_load_idt();
697
698        /* Per-cpu structures */
699        cpu_create_tss(lid);
700
701        if (cpu_features[0] & CPUID_PSE) {
702                lcr4(rcr4() | CR4_PSE);
703                tlbflushg();
704        } else {
705                /*
706                 * amd64 supports PSE by default, if it's not here we have a
707                 * problem
708                 */
709                x86_panic("PSE not supported");
710        }
711}
712
Note: See TracBrowser for help on using the repository browser.