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