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

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

Fix previous. Still don't know where the race comes from...

File size: 15.5 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();
[237]52void cpu_attach(size_t lid);
[29]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
[240]60size_t ncpu __in_kdata = 0;
61static boot_info_t btinfo __in_kdata;
62
[236]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
[29]79/* -------------------------------------------------------------------------- */
80
[32]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))
[46]89                x86_panic("No mmap");
[32]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
[135]105/* -------------------------------------------------------------------------- */
106
[119]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
[116]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
[137]139        memset(rsvd, 0, sizeof(boot_rsvd_t) * CONFIG_PPM_MAX_RSVD);
[116]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
[119]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                }
[116]156
157                i += mm->mm_size + 4;
158        }
159
160        return rsvd_nr;
161}
162
[70]163static void init_bootinfo_core(boot_core_t *core)
164{
[240]165        size_t i;
[70]166
[240]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        }
[70]175}
176
[195]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
[192]186static void init_bootinfo_pic(boot_device_t *dev)
[70]187{
188        memset(dev, 0, sizeof(boot_device_t));
189
[192]190        dev->base = 0;
[252]191        dev->type = (DEV_FUNC_PIC << 16) | IMPL_PIC_I86;
[70]192        dev->channels = 1;
193        dev->param0 = 0;
194        dev->param1 = 0;
195        dev->param2 = 0;
196        dev->param3 = 0;
197
[202]198        dev->irqs = 16;
[192]199
[202]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;
[70]205}
206
[192]207static void init_bootinfo_txt(boot_device_t *dev)
208{
209        memset(dev, 0, sizeof(boot_device_t));
210
211        dev->base = 0;
[254]212        dev->type = (DEV_FUNC_TXT << 16) | IMPL_TXT_RS2;
[255]213        dev->channels = 4;
[192]214        dev->param0 = 0;
215        dev->param1 = 0;
216        dev->param2 = 0;
217        dev->param3 = 0;
218}
219
[70]220static void init_bootinfo(boot_info_t *info)
221{
[116]222        size_t offset;
[114]223
[70]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
[195]238        info->ext_dev_nr = 3;
[70]239        init_bootinfo_txt(&info->ext_dev[0]);
[192]240        init_bootinfo_pic(&info->ext_dev[1]);
[195]241        init_bootinfo_ioc(&info->ext_dev[2]);
[70]242
243        info->cxy = 0;
[240]244        info->cores_nr = ncpu;
245        init_bootinfo_core((boot_core_t *)&info->core);
[70]246
[137]247        info->rsvd_nr = init_bootinfo_rsvd((boot_rsvd_t *)&info->rsvd);
[70]248
[135]249        /* TODO: dev_mmc */
250        /* TODO: dev_dma */
251
[116]252        offset = hal_gpt_bootstrap_uniformize();
253        info->pages_offset = offset / PAGE_SIZE;
[119]254        info->pages_nr = init_bootinfo_pages_nr();
[70]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
[166]262/* -------------------------------------------------------------------------- */
263
[236]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
[283]292                smp_stkva = ((vaddr_t)&cpudata[i].boot_stack + STKSIZE) & ~0xF;
[236]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{
[240]306        lid_t lid;
[237]307
[240]308        cli();
309
310        lid = hal_lapic_gid();
311
[237]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
[236]321        cpuN_booted = 1;
[237]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
[240]329        kernel_init(&btinfo);
330
331        reg_t dummy;
332        hal_enable_irq(&dummy);
333
[236]334        while (1);
335}
336
337/* -------------------------------------------------------------------------- */
338
[199]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
[29]351void init_x86_64(paddr_t firstpa)
352{
[240]353        cli();
[70]354
[154]355        /* Initialize the serial port */
356        hal_com_init_early();
357
[29]358        x86_printf("[+] init_x86_64 called\n");
359
360        /* Create the global structures */
361        gdt_create();
362        idt_create();
363
[166]364        /* Identify the features of the cpu */
365        cpu_identify();
366
[29]367        /* Attach cpu0 */
[162]368        cpu_attach(0);
[29]369        x86_printf("[+] cpu_attach called\n");
370
[47]371        x86_printf("[+] bootloader: '%s'\n", mb_loader_name);
[32]372
373        dump_memmap();
374        x86_printf("[+] dump finished\n");
375
[35]376        hal_gpt_init(firstpa);
377        x86_printf("[+] hal_gpt_init called\n");
[29]378
[35]379        hal_acpi_init();
380        x86_printf("[+] hal_acpi_init called\n");
381
[45]382        hal_gpt_bootstrap_reset();
383        x86_printf("[+] hal_gpt_bootstrap_reset called\n");
384
[199]385        apic_map();
386        x86_printf("[+] apic_map called\n");
387
[82]388        hal_apic_init();
[237]389        cpu_lapic_init();
[82]390        x86_printf("[+] hal_apic_init called\n");
[45]391
[168]392        cpu_tls_init(0);
393        x86_printf("[+] cput_tls_init called\n");
[46]394
[224]395        mytest = 0;
[44]396        x86_printf("-> mytest = %z\n", mytest);
[94]397        void *hoho = &init_x86_64;
[74]398        xptr_t myptr = XPTR(0, &mytest);
[35]399
[94]400        hal_remote_spt(myptr, hoho);
401        x86_printf("-> mytest = %Z\n", hal_remote_lpt(myptr));
402
[99]403        init_bootinfo(&btinfo);
[94]404
[236]405        start_secondary_cpus();
406
[276]407        kernel_init(&btinfo);
408
409        x86_printf("[+] kernel_init called\n");
410
[274]411        reg_t dummy;
412        hal_enable_irq(&dummy);
413
414        while (1);
415
[192]416/*
417        void *ptr;
[70]418
[192]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*/
[99]433
[274]434
[82]435//      void x86_stop();
436//      x86_stop();
[29]437}
438
439/* -------------------------------------------------------------------------- */
440
[235]441void cpu_activate(uint32_t gid)
442{
443        cpudata[gid].valid = true;
444}
445
[29]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
[203]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
[29]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{
[80]554        extern uint64_t x86_traps[], x86_intrs[], x86_rsvd;
[29]555        struct idt_seg *idt;
556        size_t i;
[292]557        int ist;
[29]558
[203]559        memset(&idt_bitmap, 0, sizeof(idt_bitmap));
[29]560        idt = (struct idt_seg *)&idtstore;
[45]561
[80]562        /* First, put a dead entry */
563        for (i = 0; i < NIDT; i++) {
564                idt_set_seg(&idt[i], (void *)&x86_rsvd, 0,
565                    SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL));
566        }
567
[45]568        /* General exceptions */
569        for (i = CPUVEC_MIN; i < CPUVEC_MAX; i++) {
[292]570                if (i == 2) { /* NMI */
571                        ist = 3;
572                } else if (i == 8) { /* Double Fault */
573                        ist = 2;
574                } else {
575                        ist = 0;
576                }
[293]577                idt_set_seg(&idt[i], (void *)x86_traps[i - CPUVEC_MIN], ist,
[45]578                    SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL));
[203]579                idt_bitmap.busy[i] = true;
[29]580        }
[45]581
[138]582        /* Dynamically configured interrupts */
583        for (i = DYNVEC_MIN; i < DYNVEC_MAX; i++) {
584                idt_set_seg(&idt[i], (void *)x86_intrs[i - DYNVEC_MIN], 0,
[45]585                    SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL));
[203]586                idt_bitmap.busy[i] = true;
[45]587        }
[29]588}
589
590void cpu_load_idt()
591{
592        struct region_descriptor region;
593        setregion(&region, &idtstore, PAGE_SIZE - 1);
594        lidt(&region);
595}
596
597/* -------------------------------------------------------------------------- */
598
[164]599int tss_alloc(struct tss *tss, size_t lid)
[29]600{
601        int slot;
602
[164]603        slot = GDT_CPUTSS_SEL + lid;
[29]604
605        gdt_set_sysseg(GDT_ADDR_SYS(gdtstore, slot), tss,
606            sizeof(*tss) - 1, SDT_SYS386TSS, SEL_KPL, 0);
607
608        return GDT_DYNAM_SEL(slot, SEL_KPL);
609}
610
[162]611void cpu_create_tss(size_t lid)
[29]612{
[165]613        percpu_archdata_t *data = &cpudata[lid];
614        struct tss *tss = &data->tss;
[29]615        int sel;
616
617        /* Create the tss */
618        memset(tss, 0, sizeof(*tss));
[162]619
620        /* tss->tss_rsp0 */
[283]621        tss->tss_ist[0] = ((uint64_t)&data->intr_stack + STKSIZE) & ~0xF;
622        tss->tss_ist[1] = ((uint64_t)&data->dbfl_stack + STKSIZE) & ~0xF;
623        tss->tss_ist[2] = ((uint64_t)&data->nmfl_stack + STKSIZE) & ~0xF;
[29]624        tss->tss_iobase = IOMAP_INVALOFF << 16;
[164]625        sel = tss_alloc(tss, lid);
[29]626
627        /* Load it */
628        ltr(sel);
629}
630
631/* -------------------------------------------------------------------------- */
632
[168]633void cpu_tls_init(size_t lid)
634{
635        percpu_archdata_t *data = &cpudata[lid];
636        tls_t *cputls = &data->tls;
637
638        memset(cputls, 0, sizeof(tls_t));
639
640        cputls->tls_self = cputls;
641        cputls->tls_gid = hal_lapic_gid();
642        cputls->tls_lid = lid;
[240]643        cputls->tls_intr = INTRS_DISABLED;
[168]644
645        wrmsr(MSR_FSBASE, 0);
646        wrmsr(MSR_GSBASE, (uint64_t)cputls);
647        wrmsr(MSR_KERNELGSBASE, 0);
648}
649
650/* -------------------------------------------------------------------------- */
651
[166]652uint64_t cpu_features[4] __in_kdata;
653
654void cpu_identify()
655{
656        /*
657         * desc[0] = eax
658         * desc[1] = ebx
659         * desc[2] = ecx
660         * desc[3] = edx
661         */
662        uint32_t desc[4];
663        char vendor[13];
664        size_t lvl;
665
666        /*
667         * Get information from the standard cpuid leafs
668         */
669        cpuid(0, 0, (uint32_t *)&desc);
670
671        lvl = (uint64_t)desc[0];
672        x86_printf("-> cpuid standard level: %z\n", lvl);
673
674        memcpy(vendor + 0, &desc[1], sizeof(uint32_t));
675        memcpy(vendor + 8, &desc[2], sizeof(uint32_t));
676        memcpy(vendor + 4, &desc[3], sizeof(uint32_t));
677        vendor[12] = '\0';
678        x86_printf("-> CPU vendor: '%s'\n", vendor);
679
680        if (lvl >= 1) {
681                cpuid(1, 0, (uint32_t *)&desc);
682                cpu_features[0] = desc[3];
683                cpu_features[1] = desc[2];
684        }
685
686        /*
687         * Get information from the extended cpuid leafs
688         */
689        cpuid(0x80000000, 0, desc);
690
691        lvl = (uint64_t)desc[0];
692        x86_printf("-> cpuid extended level: %Z\n", lvl);
693}
694
695/* -------------------------------------------------------------------------- */
696
[162]697void cpu_attach(size_t lid)
[29]698{
[168]699        /* Per-cluster structures */
[29]700        cpu_load_gdt();
701        cpu_load_idt();
[168]702
703        /* Per-cpu structures */
[162]704        cpu_create_tss(lid);
[166]705
706        if (cpu_features[0] & CPUID_PSE) {
707                lcr4(rcr4() | CR4_PSE);
708                tlbflushg();
709        } else {
710                /*
711                 * amd64 supports PSE by default, if it's not here we have a
712                 * problem
713                 */
714                x86_panic("PSE not supported");
715        }
[29]716}
717
Note: See TracBrowser for help on using the repository browser.