A Total Overhaul on the Lunaix's Virtual Memory Model (#26)
[lunaix-os.git] / lunaix-os / arch / i386 / boot / prologue.S
1 /* 高半核入口点 - 0xC0000000 */
2
3 #define __ASM__
4 #include <sys/abi.h>
5
6 .section .bss
7     .align 16
8     .skip 2048, 0
9     __kinit_stack_top:
10     # TODO 
11     # This stack was too small that corrupt the ambient kernel structures.
12     #  (took me days to figure this out!)
13     # We should spent more time to implement a good strategy to detect such
14     #  run-over (we can check these invariants when we trapped in some non-recoverable
15     #  state and provide good feedback to user)
16
17 .section .text
18     .global hhk_entry_
19     hhk_entry_:
20         /*
21             欢迎来到虚拟内存的世界! :D
22          */
23         movl $__kinit_stack_top, %esp
24         andl $stack_alignment, %esp
25         subl $16, %esp
26         /* 
27             最终还是决定将IDT&GDT的初始化和安装放在这里
28             注意:由于已开启分页,GDTR与IDTR里面放的不是物理地址,是线性地址! 
29         */
30         /* 
31             加载 GDT 
32             P.s. 虽然GDT在分页后已变得不重要,甚至可以忽略不作。但为了保持完整性,还是选择加载他
33                     这主要是为了保险起见,让GDTR有一个合法的值,否则多咱的粗心大意,容易出#GP
34         */
35         call _init_gdt
36
37         movl $_gdt, 2(%esp)
38         movw _gdt_limit, %ax
39         movw %ax, (%esp)
40         lgdt (%esp)
41
42         /* 更新段寄存器 */
43         movw $KDATA_SEG, %cx
44         movw %cx, %es
45         movw %cx, %ds
46         movw %cx, %fs
47         movw %cx, %gs
48         movw %cx, %ss
49         
50         /* 更新 CS:EIP */
51         pushw $KCODE_SEG
52         pushl $_after_gdt
53         retf
54
55     _after_gdt:
56         subl $16, %esp
57
58         # 加载 IDT
59         movl $_idt, 2(%esp)
60         movw _idt_limit, %ax
61         movw %ax, (%esp)
62         lidt (%esp)
63
64         # perform arch-specific initialization before diving into kernel
65         call arch_preinit
66
67         /* 加载TSS段选择器 */
68         movw $TSS_SEG, %ax
69         ltr %ax
70
71         movl $bhctx_buffer, (%esp)       # mb_parser.c
72         call kernel_bootstrap
73
74     1:
75         hlt
76         jmp 1b