-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.asm
More file actions
547 lines (481 loc) · 9.1 KB
/
kernel.asm
File metadata and controls
547 lines (481 loc) · 9.1 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
use16
org 0x7C00
BootSector:
jmp beginBoot
times (0x100 - ($ - $$)) db 0x00
drvNum: db 0x00
beginBoot:
; the bios provides boot drive on dl, preserve it
mov [drvNum], dl
; set segment offset to give us more address space
mov ax, 0x7E0
mov es, ax
mov ah, 0x02 ; command to read sectors into memory
mov al, 128 ; read 128 sectors
mov dl, [drvNum] ; drive number
mov ch, 0 ; cylinder number
mov dh, 0 ; head number
mov cl, 2
mov bx, 0 ; load sector to this address
int 0x13
; reset segment
mov ax, 0x0000
mov es, ax
jmp 0x0000:sys_stage2
; pad bootsector and add magic bytes
times ((0x200 - 2) - ($ - $$)) db 0x00
dw 0xAA55
sys_stage2:
jmp BeginInitSystem
; scratch space for the vesa struct
STRUCTPTR:
times 512 db 0x00
SysStartMsg:
db "Starting System...", 13, 10, 0
PrintMessage:
mov ebx, SysStartMsg
mov ah, 0x0E
ContMessage:
mov al, [ebx]
cmp al, 0
je EndMessage
inc ebx
int 0x10
jmp ContMessage
EndMessage:
ret
BeginInitSystem:
call PrintMessage
jmp InitializeGraphics
VideoMemPtr: dd 0x00000000
InitializeGraphics:
; get information about mode 0x0112
mov ax, 0x4F01
mov cx, 0x0112
mov di, STRUCTPTR
int 0x10
; get pointer from received information
mov eax, STRUCTPTR
add eax, 40 ; pointer number at offset 40
mov ebx, [eax]
mov [VideoMemPtr], ebx
; enter the mode
mov ax, 0x4F02 ; vesa mode
mov bx, 0x0112 ; 640x480x24
int 0x10
enterProtMode:
cli
lgdt [gdt_descriptor]
mov eax, cr0
or eax, 0b00000001 ; set "protected mode enable" flag
mov cr0, eax
jmp CODE_SEG:BeginKernel32 ; set CS register and jump to kernel
hlt
ret
[bits 32]
; set up the Global Descriptor Table
; the GDT determines the current ring level and permission levels for memory ranges,
; such as read, write, and code execute.
gdt_start:
gdt_null:
dd 0x0
dd 0x0
gdt_code:
dw 0xFFFF
dw 0x0
db 0x0
db 10011010b
db 11001111b
db 0x0
gdt_data:
dw 0xFFFF
dw 0x0
db 0x0
db 10010010b
db 11001111b
db 0x0
gdt_end:
gdt_descriptor:
dw gdt_end - gdt_start
dd gdt_start
CODE_SEG equ gdt_code - gdt_start
DATA_SEG equ gdt_data - gdt_start
IDT_table:
times 2048 db 0x00
IDT_ptr_high: dd 0x00000000
IDT_ptr_low: dd 0x00000000
BeginKernel32:
mov ax, DATA_SEG
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
;ebp was observed to start at 0x00006EF0 (varies)
;increments by 4 on protected mode, 2 on real mode
; program the timer interrupt to about 60 ticks per second
mov ax, 1193181 / 60
out 0x40, al
mov al, ah
out 0x40, al
; remap PIC
; move interrupts from 0-15 to 32-47
; exceptions are now at 0-15
mov al, 0x11
out 0x20, al
mov al, 0x11
out 0xA0, al
mov al, 0x20
out 0x21, al
mov al, 0x28
out 0xA1, al
mov al, 0x04
out 0x21, al
mov al, 0x02
out 0xA1, al
mov al, 0x01
out 0x21, al
mov al, 0x01
out 0xA1, al
mov al, 0x00
out 0x21, al
mov al, 0x00
out 0xA1, al
%macro InterruptDesc 2 ; %1: function, %2: offset
mov eax, %1
and eax, 0xFFFF
mov [0 + IDT_table + %2 * 8], ax ;; offset_lowerbits
mov eax, 0x08
mov [2 + IDT_table + %2 * 8], ax ;; selector
mov eax, 0
mov [4 + IDT_table + %2 * 8], al ;; zero
mov eax, 0x8E
mov [5 + IDT_table + %2 * 8], al ;; type_attr
mov eax, %1
and eax, 0xFFFF0000
shr eax, 16
mov [6 + IDT_table + %2 * 8], ax ;; offset_higherbits
%endmacro
InterruptDesc addr_exception, 0
InterruptDesc addr_exception, 1
InterruptDesc addr_exception, 2
InterruptDesc addr_exception, 3
InterruptDesc addr_exception, 4
InterruptDesc addr_exception, 5
InterruptDesc addr_exception, 6
InterruptDesc addr_exception, 7
InterruptDesc addr_exception, 8
InterruptDesc addr_exception, 9
InterruptDesc addr_exception, 10
InterruptDesc addr_exception, 11
InterruptDesc addr_exception, 12
InterruptDesc addr_exception, 13
InterruptDesc addr_exception, 14
InterruptDesc addr_exception, 15
InterruptDesc addr_irq0, 32 ; timer interrupt
InterruptDesc addr_irq1, 33 ; keyboard
InterruptDesc addr_irq2, 34
InterruptDesc addr_irq3, 35
InterruptDesc addr_irq4, 36
InterruptDesc addr_irq5, 37
InterruptDesc addr_irq6, 38
InterruptDesc addr_irq7, 39
InterruptDesc addr_irq8, 40
InterruptDesc addr_irq9, 41
InterruptDesc addr_irq10, 42
InterruptDesc addr_irq11, 43
InterruptDesc addr_irq12, 44 ; ps/2
InterruptDesc addr_irq13, 45
InterruptDesc addr_irq14, 46
InterruptDesc addr_irq15, 47
jmp beginIDT
; ps/2 mouse packet data
tempIrq12AL: db 0x00
Packet_PS2Mouse: db 0x00, 0x00, 0x00, 0x00, 0x00
Packet_PS2Pos: db 0x00
EventReady: db 0x00
KernIntTablePtr: dd 0x00000000
KernelSetIRQ:
mov eax, [esp + 4]
mov [KernIntTablePtr], eax
sti
ret
; *** Exceptions ***
; for now, just make all of them shut down the system
addr_exception:
call blankScreen
jmp EndKernel
; *** Interrupts ***
addr_irq0:
pusha
mov eax, [KernIntTablePtr]
call [eax]
popa
iret
addr_irq1:
pusha
mov eax, [KernIntTablePtr]
call [eax + 4]
popa
iret
addr_irq2:
pusha
mov eax, [KernIntTablePtr]
call [eax + 8]
popa
iret
addr_irq3:
pusha
mov eax, [KernIntTablePtr]
call [eax + 12]
popa
iret
addr_irq4:
pusha
mov eax, [KernIntTablePtr]
call [eax + 16]
popa
iret
addr_irq5:
pusha
mov eax, [KernIntTablePtr]
call [eax + 20]
popa
iret
addr_irq6:
pusha
mov eax, [KernIntTablePtr]
call [eax + 24]
popa
iret
addr_irq7:
pusha
mov eax, [KernIntTablePtr]
call [eax + 28]
popa
iret
addr_irq8:
pusha
mov eax, [KernIntTablePtr]
call [eax + 32]
popa
iret
addr_irq9:
pusha
mov eax, [KernIntTablePtr]
call [eax + 36]
popa
iret
addr_irq10:
pusha
mov eax, [KernIntTablePtr]
call [eax + 40]
popa
iret
addr_irq11:
pusha
mov eax, [KernIntTablePtr]
call [eax + 44]
popa
iret
addr_irq12:
pusha
mov eax, [KernIntTablePtr]
call [eax + 48]
popa
iret
addr_irq13:
pusha
mov eax, [KernIntTablePtr]
call [eax + 52]
popa
iret
addr_irq14:
pusha
mov eax, [KernIntTablePtr]
call [eax + 56]
popa
iret
addr_irq15:
pusha
mov eax, [KernIntTablePtr]
call [eax + 60]
popa
iret
KernelYieldEvent:
;pusha
;KernelYieldLoop:
hlt
;popa
ret
beginIDT:
; load idt
mov eax, IDT_table
and eax, 0xFFFF
shl eax, 16
add eax, 2048
mov [IDT_ptr_high], eax
mov eax, IDT_table
shr eax, 16
mov [IDT_ptr_low], eax
mov eax, IDT_ptr_high
lidt [eax]
jmp BeginKernel
blankScreen:
mov eax, [VideoMemPtr]
mov ecx, 0
mov bl, 255
blankScreenLoop:
mov [eax], bl
inc eax
inc ecx
cmp ecx, 640 * 480 * 3
jl blankScreenLoop
ret
reloc_memBase: dd 0x00100000
reloc_memBss: dd 0x00000000
reloc_memGlobVar: dd 0x00000000
reloc_kernelEntry: dd 0x00000000
; Initialize memory region with zeros
; ebx: starting addr
; ecx: buffer size
memRegion_init:
add ecx, ebx
memRegInitLoop:
mov byte [ebx], 0x00
inc ebx
cmp ebx, ecx
jl memRegInitLoop
ret
; runtime relocation
processKernelReloc:
mov eax, codeRelocationData
kernRelocLoop:
mov bl, [eax]
add eax, 1
cmp bl, 1 ; data
je kreloc_data
cmp bl, 2 ; rdata
je kreloc_rdata
cmp bl, 3 ; bss
je kreloc_bss
cmp bl, 4 ; glob
je kreloc_glob
cmp bl, 5 ; text
je kreloc_text
cmp bl, 6 ; bssSize
je kreloc_bssSize
cmp bl, 7 ; globVarSize
je kreloc_globVarSize
cmp bl, 8 ; entry
je kreloc_entry
jmp kernRelocStop
kreloc_data:
mov ecx, [eax]
add ecx, kernelBytecodeStart
mov edx, ecx
mov ecx, [ecx]
add ecx, codeDataSection
mov [edx], ecx
add eax, 4
jmp kernRelocLoop
kreloc_rdata:
mov ecx, [eax]
add ecx, kernelBytecodeStart
mov edx, ecx
mov ecx, [ecx]
add ecx, codeReadonlyData
mov [edx], ecx
add eax, 4
jmp kernRelocLoop
kreloc_bss:
mov ecx, [eax]
add ecx, kernelBytecodeStart
mov edx, ecx
mov ecx, [ecx]
add ecx, [reloc_memBss]
mov [edx], ecx
add eax, 4
jmp kernRelocLoop
kreloc_glob:
mov ecx, [eax]
add ecx, kernelBytecodeStart
mov edx, ecx
mov ecx, [ecx]
add ecx, [reloc_memGlobVar]
add eax, 4
mov ebx, [eax]
add ecx, ebx
mov [edx], ecx
add eax, 4
jmp kernRelocLoop
kreloc_text:
mov ecx, [eax]
add ecx, kernelBytecodeStart
mov edx, ecx
mov ecx, [ecx]
add ecx, kernelBytecodeStart
mov [edx], ecx
add eax, 4
jmp kernRelocLoop
kreloc_bssSize:
; set BSS ptr to memory base and increment memory base by size of BSS
mov ecx, [reloc_memBase]
mov [reloc_memBss], ecx
mov ebx, [eax]
add ecx, ebx
mov [reloc_memBase], ecx
; initialize region
mov ebx, [reloc_memBss]
mov ecx, [eax]
call memRegion_init
add eax, 4
jmp kernRelocLoop
kreloc_globVarSize:
mov ecx, [reloc_memBase]
mov [reloc_memGlobVar], ecx
mov ebx, [eax]
add ecx, ebx
mov [reloc_memBase], ecx
; initialize region
mov ebx, [reloc_memGlobVar]
mov ecx, [eax]
call memRegion_init
add eax, 4
jmp kernRelocLoop
kreloc_entry:
mov ecx, [eax]
mov [reloc_kernelEntry], ecx
add eax, 4
jmp kernRelocLoop
kernRelocStop:
ret
;;;;;;;;;;;;;;;; Kernel Data ;;;;;;;;;;;;;;;;
codeRelocationData:
incbin "./build/reloc.bin"
codeDataSection:
incbin "./build/exec_data.bin"
codeReadonlyData:
incbin "./build/exec_rdata.bin"
kernelBytecode:
call processKernelReloc
mov eax, [VideoMemPtr]
push KernelSetIRQ
push KernelYieldEvent
push eax
mov eax, kernelBytecodeStart
add eax, [reloc_kernelEntry]
call eax
jmp kernelBytecodeEnd
kernelBytecodeStart:
incbin "./build/exec_code.bin"
kernelBytecodeEnd:
jmp EndKernel
BeginKernel:
jmp kernelBytecode
EndKernel:
cli
EndKernelLoop:
hlt
jmp EndKernelLoop
times (0x200000 - ($ - $$)) db 0x00