Skip to content

Commit

Permalink
feat(kernel): wip serial logger
Browse files Browse the repository at this point in the history
  • Loading branch information
d0p1s4m4 committed Jul 5, 2024
1 parent 031e09b commit 4167629
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 24 deletions.
5 changes: 3 additions & 2 deletions boot/loader/loader.asm
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,9 @@ common32:
mov eax, STPDBOOT_MAGIC
mov ebx, boot_structure

mov eax, 0xC0000000
jmp eax
mov ecx, 0xC0000000
jmp ecx

hang:
hlt
jmp $
Expand Down
2 changes: 2 additions & 0 deletions kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ INSTALL = install
KERNEL = vmstupid.sys
SRCS = kernel.asm \
const.inc \
klog.inc \
dev/vga_console.inc \
mm/mm.inc

.PHONY: all
Expand Down
2 changes: 2 additions & 0 deletions kernel/const.inc
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ CR4_PKE = 0x0400000
CR4_CET = 0x0800000
CR4_PKS = 0x1000000

CR = 0x0D
LF = 0x0A
22 changes: 16 additions & 6 deletions kernel/dev/vga_console.inc
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
struc console
{
.buffer db 80*25 dup (?)
.lineno db ?
.colno db ?
}
VGA_COLUMNS = 80
VGA_LINES = 25
VGA_BASE = 0xC03B0000

vga_lineno db 0
vga_colno db 0


;; Function: vga_console_clear
vga_console_clear:
mov ecx, VGA_COLUMNS*VGA_LINES
xor al, al
mov edi, VGA_BASE
rep stosb
ret

10 changes: 10 additions & 0 deletions kernel/init.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
struc TSS {
.link dw ?
.link_h dw ?
.esp0 dd ?
.ss0 dw ?
.ss0_h dw ?
.esp1 dd ?
.ss1 dw ?
}
37 changes: 27 additions & 10 deletions kernel/kernel.asm
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
;; File: kernel.asm
format binary

include 'const.inc'

org KBASE
Expand All @@ -9,9 +11,6 @@
db 'STPDKRNL'
db 32 dup(0)

include 'klog.inc'
include 'mm/mm.inc'

;; Function: kmain
;;
;; Parameters:
Expand All @@ -20,24 +19,42 @@ db 32 dup(0)
;; EBX - Boot structure address
;;
kmain:
xchg bx, bx
mov [0xC03B0000], dword 0x08690948
mov [0xC03B0004], dword 0x05690648
; TODO: interupt, vmm
mov esp, stack_top
cmp eax, STPDBOOT_MAGIC
jne .halt

; init memory manager
; init idt, gdt
; copy boot structure
xchg bx, bx
call vga_console_clear

mov [0xC03B0000], dword 0x08740953
mov [0xC03B0004], dword 0x05700675
mov [0xC03B0008], dword 0x03640469
mov [0xC03B000C], dword 0x0153024F


;KLOG_INIT

;KLOG "kernel alive"
mov esi, szMsgKernelAlive
call klog

.halt:
hlt
jmp $

_edata:
include 'klog.inc'
include 'dev/vga_console.inc'
include 'mm/mm.inc'

szMsgKernelAlive db "Kernel is alive", 0

; BSS
align 4
stack_bottom:
rb 0x4000
stack_top:

_end:
dd 0x0
93 changes: 87 additions & 6 deletions kernel/klog.inc
Original file line number Diff line number Diff line change
@@ -1,11 +1,92 @@
CMOS_ADDRESS = 0x70
CMOS_DATA = 0x71

CMOS_REG_SECOND = 0x00
CMOS_REG_MINUTE = 0x02
CMOS_REG_HOUR = 0x04

COM1 = 0x3F8

klog_print:
mov dx, COM1
@@:
lodsb
or al, al
jz @f
out dx, al
jmp @b
@@:
ret

klog_print_date:
clc
@@:
mov al, 0x0A
out CMOS_ADDRESS, al
in al, CMOS_DATA
and al, 0x80
jnz @b

mov al, CMOS_REG_HOUR
out CMOS_ADDRESS, al
in al, CMOS_DATA

mov ah, al
shr ah, 4
and ah, 0xF
and al, 0xF

add ah, 0x30
add al, 0x30
mov [szTime + 1], ah
mov [szTime + 2], al

mov al, CMOS_REG_MINUTE
out CMOS_ADDRESS, al
in al, CMOS_DATA

mov ah, al
shr ah, 4
and ah, 0xF
and al, 0xF

add ah, 0x30
add al, 0x30
mov [szTime + 4], ah
mov [szTime + 5], al

mov al, CMOS_REG_SECOND
out CMOS_ADDRESS, al
in al, CMOS_DATA

mov ah, al
shr ah, 4
and ah, 0xF
and al, 0xF

add ah, 0x30
add al, 0x30

mov [szTime + 7], ah
mov [szTime + 8], al


push esi
mov esi, szTime
call klog_print
pop esi

ret

macro KLOG_INIT {
}
;; Function: klog
;; Output kernel log
klog:
call klog_print_date

call klog_print

mov esi, szCRLF
call klog_print
ret

macro KLOG msg {
out 0xe9, al
}
szTime db '[00:00:00] ', 0
szCRLF db CR, LF, 0

0 comments on commit 4167629

Please sign in to comment.