-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkernel_bootstrap.nas
120 lines (106 loc) · 3.89 KB
/
kernel_bootstrap.nas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
; This is the kernel bootstrap file
; As the kernel_main is written in C, so some of the bootstrap functions are not easy to express in the kernel_main so this bootstrap
; is where any boostrap operation need to be done before going into the full function kernel_main
GLOBAL kernel_bootstrap_main
EXTERN kernel_main
KERN_BASE_VIR_ADDR EQU 0x80000000
KERN_BASE_PHY_ADDR EQU 0x08000000
STACK_TMP_PHY_ADDR EQU 0x300000
STACK_VIR_ADDR EQU (KERN_BASE_VIR_ADDR + 0x300000)
CODE_PHY_ADDR EQU (KERN_BASE_PHY_ADDR + 0x100000)
CODE_VIR_ADDR EQU (KERN_BASE_VIR_ADDR + 0x100000)
CODE_TMP_PHY_ADDR EQU 0xa000
CODE_SIZE EQU 0x96000
MEM_PAGE_SIZE EQU 0x1000
PAGE_DIR_ADDR EQU (KERN_BASE_PHY_ADDR + 0x400000)
PAGE_DIR_KERN_LINEAR_START_INDEX EQU 0x200
PAGE_TAB_0_ADDR EQU (PAGE_DIR_ADDR + 0x1000)
PAGE_TAB_200_ADDR EQU (PAGE_DIR_ADDR + 0x1000 * 40h)
PAGE_TAB_201_ADDR EQU (PAGE_DIR_ADDR + 0x1000 * 41h)
section .text
; We are here, so we shall happy, because now we are in protect mode with more than 1MB to be addressable
; The only pity thing is that as it's currently in 32bit protect mode it cannot use 16bit BIOS interrupt anymore.
kernel_bootstrap_main:
; If eip < 1M, then this means that the kernel is still in < 1MB RAM so we need to move it
mov esp, STACK_TMP_PHY_ADDR
call .get_eip
.get_eip:
pop eax
cmp eax, CODE_VIR_ADDR
jae .jump_to_kernal_main
.move_kernel: ; move kernel to 1M
cld ; Setup the move direction
mov ecx, CODE_SIZE / 4
mov eax, CODE_TMP_PHY_ADDR
mov esi, eax
mov eax, CODE_PHY_ADDR
mov edi, eax
rep movsd
; Start to initialize the paging, but this is the very simple one, just to make sure that the kernel
; can run from the 2GB virtual address, and then kernel run into the kernel_main a full paging mechanism
; will be built up
.init_paging_directory:
; First, mark all 1024 page directory entry to 0x00000000
cld
xor eax, eax
mov edi, PAGE_DIR_ADDR
mov ecx, 1024
rep stosd
; Setup the page_dir_entry, which map the [0, 256MB) to [0, 256MB)
mov ebx, (PAGE_TAB_0_ADDR & 0xFFFFF000) | 0xF
xor ecx, ecx
mov esi, PAGE_DIR_ADDR
.loop_set_identity_mapping:
mov dword [esi + ecx * 4], ebx
add ebx, MEM_PAGE_SIZE
inc ecx
cmp ecx, 40h
jb .loop_set_identity_mapping
; Setup the page_dir_entry[200H to 220H], which map the [2GB, 2GB+128MB] to [128MB, 128+128MB)
mov ebx, (PAGE_TAB_200_ADDR & 0xFFFFF000) | 0x3
mov ecx, PAGE_DIR_KERN_LINEAR_START_INDEX
mov esi, PAGE_DIR_ADDR
.loop_set_kernel_mapping:
mov dword [esi + ecx * 4], ebx
add ebx, MEM_PAGE_SIZE
inc ecx
cmp ecx, PAGE_DIR_KERN_LINEAR_START_INDEX + 20h
jb .loop_set_kernel_mapping
.init_paging_identity_mapping_for_0_to_256MB
.init_paging_0_tables:
mov esi, PAGE_TAB_0_ADDR
xor ecx, ecx
mov ebx, 0xF
.init_paging_0h_to_40h_tables_loop:
cmp ecx, 1024 * 40h
jae .init_paging_200_tables
mov dword [esi + ecx * 4], ebx;
add ebx, MEM_PAGE_SIZE
inc ecx
jmp .init_paging_0h_to_40h_tables_loop
.init_paging_200_tables:
mov esi, PAGE_TAB_200_ADDR
xor ecx, ecx
mov ebx, KERN_BASE_PHY_ADDR | 0x3
.init_paging_200h_to_240h_tables_loop:
cmp ecx, 1024 * 40h
jae .enable_paging
mov dword [esi + ecx * 4], ebx;
add ebx, MEM_PAGE_SIZE
inc ecx
jmp .init_paging_200h_to_240h_tables_loop
.enable_paging:
mov eax, PAGE_DIR_ADDR
mov cr3, eax
mov eax, cr0
or eax, 0x80000001
mov cr0, eax
.jump_new_kernel_addr ; After kernel moving and paging enable, then need to jump to the virtual address of the kernel
mov esp, STACK_VIR_ADDR ; Reinitialize the stack point to virtual memory address
mov eax, CODE_VIR_ADDR
push eax
ret
.jump_to_kernal_main:
MOV esp, STACK_VIR_ADDR ; Initial the stack pointer
SUB esp, 4
JMP kernel_main