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

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

map the LAPIC and IOAPIC in a separate function

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