-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.asm
More file actions
174 lines (149 loc) · 5.07 KB
/
kernel.asm
File metadata and controls
174 lines (149 loc) · 5.07 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
;
; FlingOS™ Getting Started tutorials
; Copyright (C) 2015 Edward Nutting
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License along
; with this program; if not, write to the Free Software Foundation, Inc.,
; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
;
BITS 32
GLOBAL _Kernel_Start:function
EXTERN main
SECTION .text
; BEGIN - Multiboot Header
MultibootSignature dd 464367618
MultibootFlags dd 3
MultibootChecksum dd -464367621
MultibootGraphicsRuntime_VbeModeInfoAddr dd 2147483647
MultibootGraphicsRuntime_VbeControlInfoAddr dd 2147483647
MultibootGraphicsRuntime_VbeMode dd 2147483647
MultibootInfo_Memory_High dd 0
MultibootInfo_Memory_Low dd 0
; END - Multiboot Header
MultibootInfo_Structure dd 0
; BEGIN - Kernel stack space allocation
Kernel_Stack_End:
TIMES 65535 db 0
Kernel_Stack_Start:
; END - Kernel stack space allocation
; BEGIN - GDT allocations
; This is the GDT table pre-filled with the entries we want
GDT_Contents:
; I have a suspicion that the order of the items in the GDT matters
; Code and data selectors first then TSS
db 0, 0, 0, 0, 0, 0, 0, 0 ; Offset: 0 - Null selector - required
db 255, 255, 0, 0, 0, 0x9A, 0xCF, 0 ; Offset: 8 - KM Code selector - covers the entire 4GiB address range
db 255, 255, 0, 0, 0, 0x92, 0xCF, 0 ; Offset: 16 - KM Data selector - covers the entire 4GiB address range
db 255, 255, 0, 0, 0, 0xFA, 0xCF, 0 ; Offset: 24 - UM Code selector - covers the entire 4GiB address range
db 255, 255, 0, 0, 0, 0xF2, 0xCF, 0 ; Offset: 32 - UM Data selector - covers the entire 4GiB address range
; Size - Change iff adding/removing rows from GDT contents
; Size = Total bytes in GDT - 1
GDT_Pointer db 39, 0, 0, 0, 0, 0
; END - GDT allocations
_Kernel_Start:
cli
; MultiBoot compliant loader provides info in registers:
; EBX = MultiBoot Info Structure Pointer
; EAX = 0x2BADBOO2
; BEGIN - Multiboot Info
mov dword ECX, 0x2BADB002
cmp ECX, EAX
jne HandleNoMultiboot
mov dword [MultibootInfo_Structure], EBX
add dword EBX, 0x4
mov dword EAX, [EBX]
mov dword [MultibootInfo_Memory_Low], EAX
add dword EBX, 0x4
mov dword EAX, [EBX]
mov dword [MultibootInfo_Memory_High], EAX
; END - Multiboot Info
; BEGIN - Set Screen Colour
mov dword EAX, 0x2F ; Colour: 0x2- = Green background, 0x-F = White foreground
mov dword EBX, 0xB8000 ; Display Memory address
mov dword ECX, 2000 ; 80 x 25 Characters - VGA Text-mode Display size
.ColourOutput:
mov byte [EBX], 0
mov byte [EBX+1], AL
add EBX, 2
loop .ColourOutput
; END - Set Screen Colour
; BEGIN - Switch to protected mode
mov dword EAX, CR0
or EAX, 1
mov dword CR0, EAX
; END - Switch to protected mode
; BEGIN - Set Screen Colour
mov dword EAX, 0x3F ; Colour: 0x3- = Aqua background, 0x-F = White foreground
mov dword EBX, 0xB8000 ; Display Memory address
mov dword ECX, 2000 ; 80 x 25 Characters - VGA Text-mode Display size
.ColourOutput2:
mov byte [EBX], 0
mov byte [EBX+1], AL
add EBX, 2
loop .ColourOutput2
; END - Set Screen Colour
; BEGIN - Set stack pointer
mov dword ESP, Kernel_Stack_Start
; END - Set stack pointer
; BEGIN - Set Screen Colour
mov dword EAX, 0x4F ; Colour: 0x4- = Red background, 0x-F = White foreground
mov dword EBX, 0xB8000 ; Display Memory address
mov dword ECX, 2000 ; 80 x 25 Characters - VGA Text-mode Display size
.ColourOutput3:
mov byte [EBX], 0
mov byte [EBX+1], AL
add EBX, 2
loop .ColourOutput3
; END - Set Screen Colour
; BEGIN - Tell CPU about GDT
mov dword [GDT_Pointer + 2], GDT_Contents
mov dword EAX, GDT_Pointer
lgdt [EAX]
; Set data segments
mov dword EAX, 0x10
mov word DS, EAX
mov word ES, EAX
mov word FS, EAX
mov word GS, EAX
mov word SS, EAX
; Force reload of code segment
jmp 8:Boot_FlushCsGDT
Boot_FlushCsGDT:
; END - Tell CPU about GDT
; BEGIN - Set Screen Colour
mov dword EAX, 0x5F ; Colour: 0x5- = Purple background, 0x-F = White foreground
mov dword EBX, 0xB8000 ; Display Memory address
mov dword ECX, 2000 ; 80 x 25 Characters - VGA Text-mode Display size
.ColourOutput4:
mov byte [EBX], 0
mov byte [EBX+1], AL
add EBX, 2
loop .ColourOutput4
; END - Set Screen Colour
call main
jmp Halt
HandleNoMultiboot:
; BEGIN - Set Screen Colour
mov dword EAX, 0x4F ; Colour: 0x4- = Red background, 0x-F = White foreground
mov dword EBX, 0xB8000 ; Display Memory address
mov dword ECX, 2000 ; 80 x 25 Characters - VGA Text-mode Display size
.ColourOutput:
mov byte [EBX], 0
mov byte [EBX+1], AL
add EBX, 2
loop .ColourOutput
; END - Set Screen Colour
Halt:
cli ; Stop maskable interrupts
hlt ; Halt the processor
jmp Halt ; If non-maskable interrupt occurs, re-halt