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

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

start moving the APIC into the XCU driver

File size: 10.4 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_remote.h>
30#include <hal_irqmask.h>
31
32#include <memcpy.h>
33#include <thread.h>
34#include <string.h>
35#include <process.h>
36#include <printk.h>
37#include <vmm.h>
38#include <core.h>
39#include <cluster.h>
40#include <chdev.h>
41
42#include <boot_info.h>
43
44void kernel_init(boot_info_t *info);
45
46static void gdt_create();
47static void idt_create();
48void cpu_attach();
49
50size_t mytest __in_kdata = 0;
51
52struct multiboot_info mb_info __in_kdata;
53char mb_loader_name[PAGE_SIZE] __in_kdata;
54uint8_t mb_mmap[PAGE_SIZE] __in_kdata;
55
56/* -------------------------------------------------------------------------- */
57
58static void
59dump_memmap()
60{
61        size_t mmap_length = mb_info.mi_mmap_length;
62        uint8_t *mmap_addr = (uint8_t *)&mb_mmap;
63        size_t i;
64
65        if (!(mb_info.mi_flags & MULTIBOOT_INFO_HAS_MMAP))
66                x86_panic("No mmap");
67
68        i = 0;
69        while (i < mmap_length) {
70                struct multiboot_mmap *mm;
71
72                mm = (struct multiboot_mmap *)(mmap_addr + i);
73
74                x86_printf("-> [%Z, %Z] %s\n", mm->mm_base_addr,
75                    mm->mm_base_addr + mm->mm_length,
76                    (mm->mm_type == 1) ? "ram" : "rsv" );
77
78                i += mm->mm_size + 4;
79        }
80}
81
82/* -------------------------------------------------------------------------- */
83
84static void init_bootinfo_icu(boot_device_t *dev)
85{
86        memset(dev, 0, sizeof(boot_device_t));
87
88        dev->base = NULL; /* XXX */
89        dev->type = (DEV_FUNC_ICU << 16) | IMPL_ICU_XCU;
90        dev->channels = 1;
91        dev->param0 = 0;
92        dev->param1 = 0;
93        dev->param2 = 0;
94        dev->param3 = 0;
95
96#ifdef NOTYET
97    uint32_t    irqs;                 /*! number of input IRQs                              */
98    boot_irq_t  irq[32];              /*! array of input IRQS (PIC and ICU only)            */
99#endif
100}
101
102static size_t init_bootinfo_pages_nr()
103{
104        size_t mmap_length = mb_info.mi_mmap_length;
105        uint8_t *mmap_addr = (uint8_t *)&mb_mmap;
106        paddr_t maxpa, pa;
107        size_t i;
108
109        i = 0;
110        maxpa = 0;
111        while (i < mmap_length) {
112                struct multiboot_mmap *mm;
113
114                mm = (struct multiboot_mmap *)(mmap_addr + i);
115
116                if (mm->mm_type == 1) {
117                        pa = mm->mm_base_addr + mm->mm_length;
118                        if (pa > maxpa)
119                                maxpa = pa;
120                }
121
122                i += mm->mm_size + 4;
123        }
124
125        return (maxpa / PAGE_SIZE);
126}
127
128static size_t init_bootinfo_rsvd(boot_rsvd_t *rsvd)
129{
130        size_t mmap_length = mb_info.mi_mmap_length;
131        uint8_t *mmap_addr = (uint8_t *)&mb_mmap;
132        size_t i, rsvd_nr;
133
134        memset(rsvd, 0, sizeof(boot_rsvd_t));
135
136        i = 0, rsvd_nr = 0;
137        while (i < mmap_length) {
138                struct multiboot_mmap *mm;
139
140                mm = (struct multiboot_mmap *)(mmap_addr + i);
141
142                if (mm->mm_type != 1) {
143                        rsvd[rsvd_nr].first_page =
144                            rounddown(mm->mm_base_addr, PAGE_SIZE) / PAGE_SIZE;
145                        rsvd[rsvd_nr].npages =
146                            roundup(mm->mm_length, PAGE_SIZE) / PAGE_SIZE;
147                        rsvd_nr++;
148                        if (rsvd_nr == CONFIG_PPM_MAX_RSVD)
149                                x86_panic("too many memory holes");
150                }
151
152                i += mm->mm_size + 4;
153        }
154
155        return rsvd_nr;
156}
157
158static void init_bootinfo_core(boot_core_t *core)
159{
160        memset(core, 0, sizeof(boot_core_t));
161
162        core->gid = hal_lapic_gid();
163        core->lid = 0;
164        core->cxy = 0;
165}
166
167static void init_bootinfo_txt(boot_device_t *dev)
168{
169        memset(dev, 0, sizeof(boot_device_t));
170
171        dev->base = 0xB8000;
172        dev->type = (DEV_FUNC_TXT << 16) | IMPL_TXT_X86;
173        dev->channels = 1;
174        dev->param0 = 0;
175        dev->param1 = 0;
176        dev->param2 = 0;
177        dev->param3 = 0;
178
179#ifdef NOTYET
180    uint32_t    irqs;    /*! number of input IRQs                    */
181    boot_irq_t  irq[32]; /*! array of input IRQS (PIC and ICU only)  */
182#endif
183}
184
185static void init_bootinfo(boot_info_t *info)
186{
187        size_t offset;
188
189        extern uint64_t __kernel_data_start;
190        extern uint64_t __kernel_end;
191
192        memset(info, 0, sizeof(boot_info_t));
193
194        info->signature = 0;
195
196        info->paddr_width = 0;
197        info->x_width = 1;
198        info->y_width = 1;
199        info->x_size = 1;
200        info->y_size = 1;
201        info->io_cxy = 0;
202
203        info->ext_dev_nr = 1;
204        init_bootinfo_txt(&info->ext_dev[0]);
205
206        info->cxy = 0;
207        info->cores_nr = 1;
208        init_bootinfo_core(&info->core[0]);
209
210        info->rsvd_nr = init_bootinfo_rsvd(&info->rsvd);
211
212        init_bootinfo_icu(&info->dev_icu);
213        /* TODO: dev_mmc */
214        /* TODO: dev_dma */
215
216        offset = hal_gpt_bootstrap_uniformize();
217        info->pages_offset = offset / PAGE_SIZE;
218        info->pages_nr = init_bootinfo_pages_nr();
219
220        info->kernel_code_start = (intptr_t)(KERNTEXTOFF - KERNBASE);
221        info->kernel_code_end = (intptr_t)(&__kernel_data_start - KERNBASE) - 1;
222        info->kernel_data_start = (intptr_t)(&__kernel_data_start - KERNBASE);
223        info->kernel_code_end = (intptr_t)(&__kernel_end - KERNBASE) - 1;
224}
225
226void init_x86_64(paddr_t firstpa)
227{
228        boot_info_t btinfo;
229
230        x86_printf("[+] init_x86_64 called\n");
231
232        /* Create the global structures */
233        gdt_create();
234        idt_create();
235
236        /* Attach cpu0 */
237        cpu_attach();
238        x86_printf("[+] cpu_attach called\n");
239
240        x86_printf("[+] bootloader: '%s'\n", mb_loader_name);
241
242        dump_memmap();
243        x86_printf("[+] dump finished\n");
244
245        hal_gpt_init(firstpa);
246        x86_printf("[+] hal_gpt_init called\n");
247
248        hal_acpi_init();
249        x86_printf("[+] hal_acpi_init called\n");
250
251        hal_gpt_bootstrap_reset();
252        x86_printf("[+] hal_gpt_bootstrap_reset called\n");
253
254        hal_apic_init();
255        x86_printf("[+] hal_apic_init called\n");
256
257        hal_tls_init_cpu0();
258        x86_printf("[+] hal_tls_init_cpu0 called\n");
259
260        x86_printf("-> mytest = %z\n", mytest);
261        void *hoho = &init_x86_64;
262        xptr_t myptr = XPTR(0, &mytest);
263
264        hal_remote_spt(myptr, hoho);
265        x86_printf("-> mytest = %Z\n", hal_remote_lpt(myptr));
266
267        init_bootinfo(&btinfo);
268
269        reg_t dummy;
270        hal_enable_irq(&dummy);
271
272        kernel_init(&btinfo);
273
274        x86_printf("[+] kernel_init called\n");
275
276        while (1);
277
278//      void x86_stop();
279//      x86_stop();
280
281        int m = 0;
282        int v = 1 / m;
283
284        char *buf = NULL;
285        *buf = (char)0x01;
286
287        x86_printf("ALIVE!\n");
288
289        while (1);
290}
291
292/* -------------------------------------------------------------------------- */
293
294uint8_t gdtstore[PAGE_SIZE] __in_kdata;
295uint8_t idtstore[PAGE_SIZE] __in_kdata;
296struct tss cpu0_tss __in_kdata;
297uint8_t cpu0_intr_stack[STKSIZE] __in_kdata;
298uint8_t cpu0_dbfl_stack[STKSIZE] __in_kdata;
299uint8_t cpu0_nmfl_stack[STKSIZE] __in_kdata;
300
301static void
302setregion(struct region_descriptor *rd, void *base, uint16_t limit)
303{
304        rd->rd_limit = limit;
305        rd->rd_base = (uint64_t)base;
306}
307
308/* -------------------------------------------------------------------------- */
309
310static void
311gdt_set_memseg(struct gdt_memseg *sd, void *base, size_t limit,
312        int type, int dpl, int gran, int is64)
313{
314        sd->sd_lolimit = (unsigned)limit;
315        sd->sd_lobase = (unsigned long)base;
316        sd->sd_type = type;
317        sd->sd_dpl = dpl;
318        sd->sd_p = 1;
319        sd->sd_hilimit = (unsigned)limit >> 16;
320        sd->sd_avl = 0;
321        sd->sd_long = is64;
322        sd->sd_def32 = 0;
323        sd->sd_gran = gran;
324        sd->sd_hibase = (unsigned long)base >> 24;
325}
326
327static void
328gdt_set_sysseg(struct gdt_sysseg *sd, void *base, size_t limit,
329        int type, int dpl, int gran)
330{
331        memset(sd, 0, sizeof *sd);
332        sd->sd_lolimit = (unsigned)limit;
333        sd->sd_lobase = (uint64_t)base;
334        sd->sd_type = type;
335        sd->sd_dpl = dpl;
336        sd->sd_p = 1;
337        sd->sd_hilimit = (unsigned)limit >> 16;
338        sd->sd_gran = gran;
339        sd->sd_hibase = (uint64_t)base >> 24;
340}
341
342static void gdt_create()
343{
344        memset(&gdtstore, 0, PAGE_SIZE);
345
346        /* Flat segments */
347        gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_KCODE_SEL), 0,
348            0xfffff, SDT_MEMERA, SEL_KPL, 1, 1);
349        gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_KDATA_SEL), 0,
350            0xfffff, SDT_MEMRWA, SEL_KPL, 1, 1);
351        gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_UCODE_SEL), 0,
352            0xfffff, SDT_MEMERA, SEL_UPL, 1, 1);
353        gdt_set_memseg(GDT_ADDR_MEM(gdtstore, GDT_UDATA_SEL), 0,
354            0xfffff, SDT_MEMRWA, SEL_UPL, 1, 1);
355}
356
357void cpu_load_gdt()
358{
359        struct region_descriptor region;
360        setregion(&region, &gdtstore, PAGE_SIZE - 1);
361        lgdt(&region);
362}
363
364/* -------------------------------------------------------------------------- */
365
366static void
367idt_set_seg(struct idt_seg *seg, void *func, int ist, int type, int dpl, int sel)
368{
369        seg->gd_looffset = (uint64_t)func & 0xffff;
370        seg->gd_selector = sel;
371        seg->gd_ist = ist;
372        seg->gd_type = type;
373        seg->gd_dpl = dpl;
374        seg->gd_p = 1;
375        seg->gd_hioffset = (uint64_t)func >> 16;
376        seg->gd_zero = 0;
377        seg->gd_xx1 = 0;
378        seg->gd_xx2 = 0;
379        seg->gd_xx3 = 0;
380}
381
382static void idt_create()
383{
384        extern uint64_t x86_traps[], x86_intrs[], x86_rsvd;
385        struct idt_seg *idt;
386        size_t i;
387
388        idt = (struct idt_seg *)&idtstore;
389
390        /* First, put a dead entry */
391        for (i = 0; i < NIDT; i++) {
392                idt_set_seg(&idt[i], (void *)&x86_rsvd, 0,
393                    SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL));
394        }
395
396        /* General exceptions */
397        for (i = CPUVEC_MIN; i < CPUVEC_MAX; i++) {
398                idt_set_seg(&idt[i], (void *)x86_traps[i - CPUVEC_MIN], 0,
399                    SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL));
400        }
401
402        /* LAPIC interrupts */
403        for (i = LAPICVEC_MIN; i < LAPICVEC_MAX; i++) {
404                idt_set_seg(&idt[i], (void *)x86_intrs[i - LAPICVEC_MIN], 0,
405                    SDT_SYS386IGT, SEL_KPL, GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL));
406        }
407}
408
409void cpu_load_idt()
410{
411        struct region_descriptor region;
412        setregion(&region, &idtstore, PAGE_SIZE - 1);
413        lidt(&region);
414}
415
416/* -------------------------------------------------------------------------- */
417
418/*
419 * The gdt bitmap must be per-cluster.
420 */
421int tss_alloc(struct tss *tss)
422{
423        int slot;
424
425        /* Once we have proper SMP support, we will change that */
426        slot = GDT_CPU0TSS_SEL;
427
428        gdt_set_sysseg(GDT_ADDR_SYS(gdtstore, slot), tss,
429            sizeof(*tss) - 1, SDT_SYS386TSS, SEL_KPL, 0);
430
431        return GDT_DYNAM_SEL(slot, SEL_KPL);
432}
433
434void cpu_create_tss()
435{
436        struct tss *tss = &cpu0_tss;
437        int sel;
438
439        /* Create the tss */
440        memset(tss, 0, sizeof(*tss));
441        tss->tss_iobase = IOMAP_INVALOFF << 16;
442        tss->tss_ist[0] = (uint64_t)cpu0_intr_stack + STKSIZE;
443        tss->tss_ist[1] = (uint64_t)cpu0_dbfl_stack + STKSIZE;
444        tss->tss_ist[2] = (uint64_t)cpu0_nmfl_stack + STKSIZE;
445        sel = tss_alloc(tss);
446
447        /* Load it */
448        ltr(sel);
449}
450
451/* -------------------------------------------------------------------------- */
452
453void cpu_attach()
454{
455        cpu_load_gdt();
456        cpu_load_idt();
457        cpu_create_tss();
458}
459
Note: See TracBrowser for help on using the repository browser.