File tree 3 files changed +162
-0
lines changed
3 files changed +162
-0
lines changed Original file line number Diff line number Diff line change
1
+ global start
2
+ extern long_mode_start
3
+
4
+ section .text
5
+ bits 32
6
+ start:
7
+ mov esp , stack_top
8
+
9
+ call check_multiboot
10
+ call check_cpuid
11
+ call check_long_mode
12
+
13
+ call setup_page_tables
14
+ call enable_paging
15
+
16
+ lgdt [ gdt64.pointer ]
17
+ jmp gdt64.code_segment:long_mode_start
18
+
19
+ hlt
20
+
21
+ check_multiboot:
22
+ cmp eax , 0x36d76289
23
+ jne .no_multiboot
24
+ ret
25
+ .no_multiboot:
26
+ mov al , "M"
27
+ jmp error
28
+
29
+ check_cpuid:
30
+ pushfd
31
+ pop eax
32
+ mov ecx , eax
33
+ xor eax , 1 << 21
34
+ push eax
35
+ popfd
36
+ pushfd
37
+ pop eax
38
+ push ecx
39
+ popfd
40
+ cmp eax , ecx
41
+ je .no_cpuid
42
+ ret
43
+ .no_cpuid:
44
+ mov al , "C"
45
+ jmp error
46
+
47
+ check_long_mode:
48
+ mov eax , 0x80000000
49
+ cpuid
50
+ cmp eax , 0x80000001
51
+ jb .no_long_mode
52
+
53
+ mov eax , 0x80000001
54
+ cpuid
55
+ test edx , 1 << 29
56
+ jz .no_long_mode
57
+
58
+ ret
59
+ .no_long_mode:
60
+ mov al , "L"
61
+ jmp error
62
+
63
+ setup_page_tables:
64
+ mov eax , page_table_l3
65
+ or eax , 0b11 ; present, writable
66
+ mov [ page_table_l4 ], eax
67
+
68
+ mov eax , page_table_l2
69
+ or eax , 0b11 ; present, writable
70
+ mov [ page_table_l3 ], eax
71
+
72
+ mov ecx , 0 ; counter
73
+ . loop :
74
+
75
+ mov eax , 0x200000 ; 2MiB
76
+ mul ecx
77
+ or eax , 0b10000011 ; present, writable, huge page
78
+ mov [ page_table_l2 + ecx * 8 ], eax
79
+
80
+ inc ecx ; increment counter
81
+ cmp ecx , 512 ; checks if the whole table is mapped
82
+ jne . loop ; if not, continue
83
+
84
+ ret
85
+
86
+ enable_paging:
87
+ ; pass page table location to cpu
88
+ mov eax , page_table_l4
89
+ mov cr3 , eax
90
+
91
+ ; enable PAE
92
+ mov eax , cr4
93
+ or eax , 1 << 5
94
+ mov cr4 , eax
95
+
96
+ ; enable long mode
97
+ mov ecx , 0xC0000080
98
+ rdmsr
99
+ or eax , 1 << 8
100
+ wrmsr
101
+
102
+ ; enable paging
103
+ mov eax , cr0
104
+ or eax , 1 << 31
105
+ mov cr0 , eax
106
+
107
+ ret
108
+
109
+ error:
110
+ ; print "ERR: X" where X is the error code
111
+ mov dword [ 0xb8000 ], 0x4f524f45
112
+ mov dword [ 0xb8004 ], 0x4f3a4f52
113
+ mov dword [ 0xb8008 ], 0x4f204f20
114
+ mov byte [ 0xb800a ], al
115
+ hlt
116
+
117
+ section .bss
118
+ align 4096
119
+ page_table_l4:
120
+ resb 4096
121
+ page_table_l3:
122
+ resb 4096
123
+ page_table_l2:
124
+ resb 4096
125
+ stack_bottom:
126
+ resb 4096 * 4
127
+ stack_top:
128
+
129
+ section .rodata
130
+ gdt64:
131
+ dq 0 ; zero entry
132
+ .code_segment: equ $ - gdt64
133
+ dq ( 1 << 43 ) | ( 1 << 44 ) | ( 1 << 47 ) | ( 1 << 53 ) ; code segment
134
+ .pointer:
135
+ dw $ - gdt64 - 1 ; length
136
+ dq gdt64 ; address
Original file line number Diff line number Diff line change
1
+ global long_mode_start
2
+ extern kernel_main
3
+
4
+ section .text
5
+ bits 64
6
+ long_mode_start:
7
+ mov ax , 0
8
+ mov ss , ax
9
+ mov ds , ax
10
+ mov es , ax
11
+ mov fs , ax
12
+ mov gs , ax
13
+
14
+ call kernel_main
15
+ hlt
Original file line number Diff line number Diff line change
1
+ section .multiboot_header
2
+ header_start:
3
+ dd 0xe85250d6
4
+ dd 0
5
+ dd header_end - header_start
6
+ dd 0x100000000 - ( 0xe85250d6 + 0 + (header_end - header_start))
7
+
8
+ dw 0
9
+ dw 0
10
+ dd 8
11
+ header_end:
You can’t perform that action at this time.
0 commit comments