-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.asm
More file actions
415 lines (336 loc) · 8.4 KB
/
kernel.asm
File metadata and controls
415 lines (336 loc) · 8.4 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
org 0x8000
bits 16
real_mode: ; real mode
; clearing system registers
cli
xor ax, ax
mov ds, ax
mov es, ax
mov ss, ax
xor dl, dl
; clear screen
mov ax, 0x03
int 0x10
; set bright white color
mov al, 0
mov ah, 0x09
mov bh, 0x0
mov bl, 0x0F
mov cx, 0x0EFE
int 0x10
; enabling access to memory more then 1MB
in al, 0x92
or al, 2
out 0x92, al
lgdt [gdt_descriptor]
; enabling protected mode
mov eax, cr0
or eax, 1
mov cr0, eax
jmp 0x08:protected_mode
bits 32
protected_mode:
; configuring system registers
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
mov edi, 0x1000 ; offset to write Page table
mov ecx, 0x1000 ; size of the Page table
xor eax, eax
cld
rep stosd
; Page Map Level 4 address
mov edi, 0x1000
mov dword [edi], 0x2003
; Page Directory Pointer Table address
mov edi, 0x2000
mov dword [edi], 0x3003
; Page Directory address
mov edi, 0x3000
mov dword [edi], 0x0083
; Page Map Level 4 address
mov edi, 0x1000
mov cr3, edi
xor edi, edi
; enabling Physical Address Extension
mov eax, cr4
or eax, 0x20
mov cr4, eax
; enabling long mode using ESSR MSR
mov ecx, 0xC0000080
rdmsr
bts eax, 8
wrmsr
; enabling paging
mov eax, cr0
bts eax, 31
mov cr0, eax
jmp 0x18:long_mode
bits 64
long_mode:
; clearing system and regular registers
xor al, al
xor ax, ax
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
xor rax, rax
xor rcx, rcx
xor rdx, rdx
xor rbx, rbx
xor rbp, rbp
xor rsi, rsi
xor rdi, rdi
main_code:
call wait_key
test al, al
jz main_code ; jump if zero
call check_needed_symbols
inc WORD [cursor_pos]
cmp WORD [cursor_pos], 254
dec WORD [cursor_pos]
jae main_code
mov ah, 0x0F
mov rcx, 0x0B8000
mov dx, WORD [cursor_pos]
add dx, dx
add cx, dx
mov [rcx], al
call move_cursor
mov rbx, 6 ; controls how many time to wait before getting new characters
call delay
jmp main_code
delay:
.convert:
imul rbx, 150000 ; lower number -> low delay, higher number -> high delay
.loop:
in al, 0x60 ; accepting input and then discarding it, so input data doesn't accumulate
test rbx, rbx
jz .end
dec rbx
jmp .loop
.end:
ret
wait_key:
mov rbx, 3 ; controls how much time to wait before asking if new character inputted
call delay
in al, 0x64
test al, 2
jnz wait_key
in al, 0x60
test al, 0b10000000
jnz wait_key
movzx rax, al
mov al, [scan_to_ascii + rax]
ret
move_cursor:
push rax
push rdx
inc WORD [cursor_pos]
mov dx, 0x3D4
mov al, 0x0F
out dx, al
mov dx, 0x3D5
mov ax, WORD [cursor_pos] ; position from the left
out dx, ax
pop rdx
pop rax
ret
check_needed_symbols:
push rcx
push rdx
push rbx
push rsi
push rdi
push rax
; minimum 0x0B8000
; maximum 0x0B8EFE
xor rdx, rdx
mov ch, 0x0F
mov cl, 0
cmp al, 8
je .backspace_char
cmp al, 13
jne .exit1
.check_command_name:
mov rax, 0x0B8000
mov dx, [rax]
test dx, dx
jz .condition_call
xor rax, rax
xor rdx, rdx
.correct_name:
mov al, [YOUR_COMMAND_NAME + rdx]
mov rcx, 0x0B8000
add rcx, rdx
add rcx, rdx
mov ah, [rcx]
.correct_name1:
cmp al, ah
jz .correct_name2
jmp .correct_name3
.correct_name2:
call .test_next
jmp .correct_name
.correct_name3:
cmp al, ah
jne .condition_call
inc rdx
jmp .correct_name
.condition_call:
call clear_all_chars
pop rax
pop rdi
pop rsi
pop rbx
pop rdx
pop rcx
pop rbp ; clear out the return address
jmp main_code
.test_next:
push rcx
push rax
inc rdx
mov al, [YOUR_COMMAND_NAME + rdx]
mov rcx, 0x0B8000
add rcx, rdx
add rcx, rdx
mov ah, [rcx]
cmp al, ah
jz .execute_command
pop rax
pop rcx
ret
.execute_command:
call clear_all_chars
call main_function
mov rbx, 6
call delay
pop rax
pop rdi
pop rsi
pop rbx
pop rdx
pop rcx
pop rbp ; clear out the return address
jmp main_code
.backspace_char:
cmp al, 8
jne .exit1
cmp WORD [cursor_pos], 0 ; if backspace is pressed we discard this symbol instead of printing trash
je .exit2
mov rax, 0x0B8000
mov dx, WORD [cursor_pos]
add dx, dx
sub dx, 2
add ax, dx
mov [rax], cl
dec WORD [cursor_pos]
mov dx, 0x3D4
mov al, 0x0F
out dx, al
mov dx, 0x3D5
mov ax, WORD [cursor_pos] ; new position of the cursor
out dx, ax
pop rax
pop rdi
pop rsi
pop rbx
pop rdx
pop rcx
pop rbp ; clear out the return address
jmp main_code
.exit1:
pop rax
pop rdi
pop rsi
pop rbx
pop rdx
pop rcx
ret
.exit2:
pop rax
pop rdi
pop rsi
pop rbx
pop rdx
pop rcx
pop rbp ; clear out the return address
jmp main_code
clear_all_chars:
push rcx
push rdx
push rax
mov rax, 0x0B8000
mov ch, 0x0F
mov cl, 0
.clear_loop:
cmp rax, 0x0B8EFE
jae .clear_exit
mov [rax], cl
add rax, 2
jmp .clear_loop
.clear_exit:
mov WORD [cursor_pos], 0
mov dx, 0x3D4
mov al, 0x0F
out dx, al
mov dx, 0x3D5
mov al, 0 ; new position of the cursor
out dx, al
pop rax
pop rdx
pop rcx
ret
scan_to_ascii:
db 0, 0 ; Error, Esc
db '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '='
db 8, 0 ; Backspace, Tab
db 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']'
db 13, 0 ; Enter, Left ctrl
db 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', "'", '`'
db 0 ; Left shift
db '\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
db 0, 0, 0 ; Right shift, PrintScreen, Left alt
db ' '
db 0 ; Capslock
gdt_start:
; Explanation for GDT tables, Comming from the end:
; 0x00CF9A000000FFFF:
;
; Limit[15..0] = 0xFFFF - blocks size
; Base[15..0] = 0x0000
; Base[23..16] = 0x00
; Access = 0x9A -> 10011010
; P=1 (segment exists and can be used), DPL=00 (ring0), S=1 (not a system descriptor), Type=1010 (1 bit - execute, bit 2 - allows to call from unpriviliged levels, bit 3 - readable, bit 4 - accessed )
; Flags + Limit[19..16] = 0xCF -> 11001111
; G=1 (limit * 4KB), D/B=1 (32 bit instructions), L=0 (32 bit code), Limit[19..16] = 1111
; Base[31..24] = 0x00
;
; 0x00209A0000000000:
;
; Limit[15..0] = 0x0000 (ignored)
; Base[15..0] = 0x0000
; Base[23..16] = 0x00
; Access = 0x9A -> 10011010
; P=1 (segment exists and can be used), DPL=00 (ring0), S=1 (not a system descriptor), Type=1010 (1 bit - execute, bit 2 - allows to call from unpriviliged levels, bit 3 - readable, bit 4 - accessed )
; Flags + Limit[19..16] = 0x20 -> 00100000
; G=0(limit * 0), D/B=0(16 bit instructions, needed for x64 mode), L=1 (64 bit code), Limit[19..16] = 0
;
dq 0x0000000000000000 ; NULL
dq 0x00CF9A000000FFFF ; 32-bit code descriptor (exec/read).
dq 0x00CF92000000FFFF ; 32-bit data descriptor (read/write).
dq 0x00209A0000000000 ; 64-bit code descriptor (exec/read).
dq 0x0000920000000000 ; 64-bit data descriptor (read/write).
gdt_end:
gdt_descriptor:
dw gdt_end - gdt_start - 1
dd gdt_start
%include "example.asm"
cursor_pos dw 0
times 4096-($-$$) db 0 ; SIZE (NOTE THAT IF YOU NEED TO CHANGE SIZE ALSO CHANGE AMOUNT OF SECTORS TO READ IN BOOTLOADER)