-
Notifications
You must be signed in to change notification settings - Fork 3
/
loader.asm
133 lines (104 loc) · 2.15 KB
/
loader.asm
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
121
122
123
124
125
126
127
128
129
130
131
132
133
bits 32
section .bss
STACK_SIZE equ 60000 ; 4096 * 1024 * 1024 + 400
align 4
global stack_top
global stack_end
stack_end:
resb STACK_SIZE
stack_top:
section .multiboot
MB1_MAGIC equ 0x1BADB002 ; MB 2 - 0xe85250d6
MB1_FLAGS_MEMINFO equ 1<<1
MB1_FLAGS_MBALIGN equ 1<<0
MB1_FLAGS_GRAPHICS equ 1<<2
MB1_FLAGS equ MB1_FLAGS_MBALIGN \
| MB1_FLAGS_MEMINFO \
| MB1_FLAGS_GRAPHICS
align 4
multiboot_header:
dd MB1_MAGIC
dd MB1_FLAGS
dd -(MB1_MAGIC + MB1_FLAGS)
dd 0
dd 0
dd 0
dd 0
dd 0
dd 0
dd 0
dd 0
dd 24
multiboot_header_end:
section .bootstrap.data
align 4096
VM_BASE equ 0xC0000000
PDE_INDEX equ (VM_BASE >> 22)
global lowerkrnl_page_directory
lowerkrnl_page_directory:
dd 0x00000083
times(PDE_INDEX - 1) dd 0
dd 0x00000083
times(1024 - PDE_INDEX - 1) dd 0
section .bootstrap.text
align 4
PSE_BIT equ 0x00000010
PG_BIT equ 0x80000000
global _loader
; _loader equ lower_loader - VM_BASE
_loader equ lower_loader
global lower_loader
lower_loader:
cli
mov ax, ds
mov es, ax
mov ss, ax
cld
; Update current page directory and prepare for jump
mov ecx, (lowerkrnl_page_directory - VM_BASE)
mov cr3, ecx
; Enable 4mb pages
mov ecx, cr4;
or ecx, PSE_BIT
mov cr4, ecx
; Enable paging
mov ecx, cr0
or ecx, PG_BIT
mov cr0, ecx
; ; Just jumping to higher_loader is a relative jump, so it will just increase eip.
; ; But we want eip to change to 0xc0000000 based addresses.
; ; So we're using doing jump here
lea ecx, [higher_loader]
jmp ecx
higher_loader:
; Unmap first 4mb page, because we don't need it anymore
; mov dword[lowerkrnl_page_directory], 0
; invlpg[0]
mov esp, stack_top
finit
; extern set_up_gdt
; call set_up_gdt
extern x86_gdt_init
call x86_gdt_init
push ebx
push eax
mov eax, 0x1
cpuid
test edx, 1<<25
jz .noSSE
; SSE setup
mov eax, cr0
and ax, 0xFFFB
or ax, 0x2
mov cr0, eax
mov eax, cr4
or ax, 3 << 9
mov cr4, eax
.noSSE:
xor ebp, ebp
extern kmain
call kmain
cli
.stop:
hlt
jmp .stop