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

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

Add support for context switch - not tested yet, due to some other bugs in
the mapper. This cswitch is similar to that of TSAR.

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