refactor: one more step towards arch-agnostic design
[lunaix-os.git] / lunaix-os / arch / i386 / boot / boot.S
1 #define __ASM__ 1
2 #include <sys/boot/multiboot.h>
3
4 #define MB_FLAGS    (MULTIBOOT_MEMORY_INFO | MULTIBOOT_PAGE_ALIGN)
5 #define KPG_SIZE    10*4096
6
7 .section .multiboot
8     .long MULTIBOOT_MAGIC
9     .long MB_FLAGS
10     .long CHECKSUM(MB_FLAGS)
11
12 .section .boot.bss
13     /* 根据System V ABI,栈地址必须16字节对齐 */
14     /* 这里只是一个临时栈,在_hhk_init里面我们会初始化内核专用栈 */
15     .align 16
16     stack_bottom:
17         .skip 4096, 0
18     __stack_top:
19
20
21 /* 
22     1 page directory, 
23     9 page tables:
24         1. Mapping reserved area and hhk_init
25         2-9. Remapping the kernels
26 */
27
28 .section .kpg
29     .global _k_ptd
30     _k_ptd:
31         .skip KPG_SIZE, 0
32
33
34 .section .boot.text
35     .global start_
36     .type start_, @function     /* Optional, this just give the 
37                                  * linker more knowledge about the label 
38                                  */
39     start_: 
40         cld
41         # 确保屏蔽所有外中断,直到我们准备好PIC为止
42         cli
43         movl $__stack_top, %esp
44
45         subl $16, %esp
46
47         /* 
48             parse multiboot struct into arch-agnostic boot info struct
49          */
50
51         movl %ebx, (%esp)
52         call mb_parse
53
54         /*
55             kpg_init用来初始化内核页表:
56                 1. 初始化最简单的PD与PT(重新映射我们的内核至3GiB处,以及对相应的地方进行Identity Map)
57         */
58
59         movl $(KPG_SIZE), 4(%esp)
60         movl $(_k_ptd - 0xC0000000), (%esp)    /* PTD物理地址 */
61         call kpg_init
62
63         /*
64             基本的映射定义好了,我们可以放心的打开分页了
65             我们只需要把PTD的基地址加载进CR3就好了。
66         */
67
68         /* 加载PTD基地址(物理地址) */
69         movl (%esp), %eax
70         andl $0xfffff000, %eax      # 有点多余,但写上还算明白一点
71         movl %eax, %cr3
72
73         movl %cr0, %eax
74         orl $0x80000000, %eax   /* 开启分页与地址转换 (CR0.PG=1, CR0.WP=0) */
75         andl $0xfffffffb, %eax
76         orl $0x2, %eax          /* 启用x87 FPU (CR0.MP=1, CR0.EM=0) */
77         movl %eax, %cr0
78
79         movl %cr4, %eax
80         orl $0x600, %eax
81         movl %eax, %cr4         /* CR4.OSFXSR=1, CR4.OSXMMEXCPT=1 */
82                                 /* x87 FPU 已配置 */
83
84         addl $16, %esp
85
86         /* 进入高半核! */
87         pushl $hhk_entry_
88         ret