Virtual memory & paging
[lunaix-os.git] / lunaix-os / arch / x86 / boot.S
1 #define __ASM__ 1
2 #include <arch/x86/boot/multiboot.h>
3
4 #define MB_FLAGS    MULTIBOOT_MEMORY_INFO | MULTIBOOT_PAGE_ALIGN
5 #define KPG_SIZE    24*1024
6
7 .section .multiboot
8     .long MULTIBOOT_MAGIC
9     .long MB_FLAGS
10     .long CHECKSUM(MB_FLAGS)
11
12 .section .bss
13     .global mb_info
14     .align 16
15     /* 为Multiboot info struct 预留空间 */
16     mb_info:
17         .skip 4096
18     /* 根据System V ABI,栈地址必须16字节对齐 */
19     /* 这里只是一个临时栈,在_hhk_init里面我们会初始化内核专用栈 */
20     stack_bottom:
21         .skip 16318, 0
22     stack_top:
23
24
25 /* 
26     1 page directory, 
27     5 page tables:
28         1. Mapping reserved area and hhk_init
29         2-5. Remapping the kernels
30 */
31
32 .section .kpg
33     .global _k_ptd
34     _k_ptd:
35         .skip KPG_SIZE, 0
36
37
38 .section .hhk_init
39     .global start_
40     .type start_, @function     /* Optional, this just give the 
41                                  * linker more knowledge about the label 
42                                  */
43     start_: 
44         movl $stack_top, %esp
45
46         subl $16, %esp
47
48         /* 
49             将咱们的 multiboot_info 挪个地儿,就是上述预留的空间里
50             而后在_hhk_init里,我们会对所有的高半核初始化代码(arch/x86下的所有)进行Identity map
51             这样,我们能够保证当分页与虚拟地址开启后,我们的内核能够访问到multiboot info table
52          */
53
54         movl $mb_info, 4(%esp)
55         movl %ebx, (%esp)
56         call _save_multiboot_info
57
58         /*
59             _hhk_init用来初始化我们高半核:
60                     1. 设置GDT表
61                     2. 设置IDT表(暂时,以后会拿到 kernel_init 里面)
62                     3. 初始化最简单的PTD与PT(重新映射我们的内核至3GiB处,以及对相应的地方进行Identity Map)
63         */
64
65         movl $(KPG_SIZE), 4(%esp)
66         movl $(_k_ptd - 0xC0000000), (%esp)    /* PTD物理地址 */
67         call _hhk_init
68
69         /*
70             基本的映射定义好了,我们可以放心的打开分页了
71             我们只需要把PTD的基地址加载进CR3就好了。
72         */
73
74         /* 加载PTD基地址(物理地址) */
75         movl (%esp), %eax
76         andl $0xfffff000, %eax      # 有点多余,但写上还算明白一点
77         movl %eax, %cr3
78
79         /* 开启分页与地址转换 (CR0.PG=1) */
80         movl %cr0, %eax
81         orl $0x80000000, %eax
82         movl %eax, %cr0
83
84         addl $16, %esp
85
86         /* 进入高半核! */
87         pushl $hhk_entry_
88         ret