chore: remove old klog.inc and rename klog.new.inc

This commit is contained in:
d0p1 🏳️‍⚧️ 2025-01-03 14:58:43 +01:00
parent 567a9388d8
commit dbf5dd9a91
4 changed files with 179 additions and 399 deletions

View file

@ -118,7 +118,7 @@ kmain:
call klog call klog
jmp .halt jmp .halt
include 'klog.new.inc' include 'klog.inc'
include 'dev/console.inc' include 'dev/console.inc'
include 'dev/dev.inc' include 'dev/dev.inc'
include 'mm/bootstrap.inc' include 'mm/bootstrap.inc'

View file

@ -1,59 +1,81 @@
;; File: klog.inc ;; File: klog.inc
;; Kernel logging utilities ;; Kernel logging utilities
;;
COM1 = 0x3F8 COM1 = 0x3F8
;; Function: klog_print ;; Function: _klog_print_str
;; ;;
;; In: ;; In: EAX - Null-terminated string to print
;; ESI - Null-terminated string to print _klog_print_str:
klog_print: push ebp
mov ebp, esp
push esi
mov dx, COM1 mov dx, COM1
mov esi, eax
@@: @@:
lodsb lodsb
or al, al or al, al
jz @f jz @f
out dx, al out dx, al
pusha pusha
call cga_putc call cga_putc
popa popa
jmp @b jmp @b
@@: @@:
pop esi
leave
ret ret
;; Function: klog_print_integer ;; Function: _klog_print_int
;; ;;
;; In: ;; In:
;; EDI - number ;; EAX - number
;; _klog_print_int:
klog_print_integer: test eax, eax
test edi, edi jns @f
js .print_minus ; TODO print '-'
mov ecx, eax
mov dx, COM1
mov al, '-'
out dx, al
pusha
call cga_putc
popa
mov eax, ecx
@@:
mov ecx, eax
sar ecx, 31
xor ecx, eax
sub ecx, eax
mov ecx, eax
call _klog_print_unsigned
.print_minus:
ret ret
;; Function: klog_print_unsigned ;; Function: _klog_print_unsigned
;; ;;
;; In: ;; In:
;; EDI - number ;; EAX - number
;; _klog_print_unsigned:
klog_print_unsigned:
push edx
push ebx push ebx
xor ecx, ecx
mov eax, edi
mov ebx, 10 mov ebx, 10
.loop_calc: xor ecx, ecx
@@:
xor edx, edx xor edx, edx
div ebx div ebx
push edx push edx
inc ecx inc ecx
or eax, eax or eax, eax
jnz .loop_calc jnz @b
.loop_print:
@@:
pop eax pop eax
add al, '0' add al, 0
mov dx, COM1 mov dx, COM1
out dx, al out dx, al
pusha pusha
@ -61,17 +83,65 @@ klog_print_unsigned:
popa popa
dec ecx dec ecx
or cl, cl or cl, cl
jnz .loop_print jnz @b
pop ebx pop ebx
pop edx
ret ret
;; Function: klog_print_time ;; Function: _klog_print_hex
;; ;;
klog_print_time: ;; In:
;; EAX - number
_klog_print_hex:
push ebp
mov ebp, esp
push edi
mov edi, eax
mov eax, szHexPrefix
call _klog_print_str
or edi, edi
jz .print_zero
xor cl, cl
.begin:
cmp cl, 8
je .print_number
rol edi, 4
mov eax, edi
and eax, 0xF
mov al, byte [sDigit + eax]
mov [szKlogBuffer + ecx], al
inc cl
jmp .begin
.print_zero:
mov al, '0'
mov dx, COM1
out dx, al
pusha
call cga_putc
popa
jmp .end
.print_number:
mov [szKlogBuffer + ecx], byte 0
mov eax, szKlogBuffer
call _klog_print_str
.end:
pop edi
leave
ret
;; Function: _klog_print_time
;;
_klog_print_time:
; wait CMOS ready
@@: @@:
mov al, 0x0A mov al, 0x0A
out CMOS_COMMAND, al out CMOS_COMMAND, al
@ -118,116 +188,118 @@ klog_print_time:
add ah, 0x30 add ah, 0x30
add al, 0x30 add al, 0x30
mov [szTime + 7], ah mov [szTime + 7], ah
mov [szTime + 8], al mov [szTime + 8], al
mov eax, szTime
call _klog_print_str
push esi
mov esi, szTime
call klog_print
pop esi
ret
klog_print_hex:
push esi
mov esi, szHexPrefix
call klog_print
pop esi
or edi, edi
jz .print_zero
push esi
mov esi, szKlogBuffer
xor cl, cl
.loop:
cmp cl, 8
je .print_number
rol edi, 4
mov eax, edi
and eax, 0xF
mov al, byte [sDigit + eax]
mov [esi], al
inc esi
inc cl
jmp .loop
.print_zero:
mov al, '0'
out dx, al
pusha
call cga_putc
popa
jmp .end
.print_number:
mov [esi], byte 0
mov esi, szKlogBuffer
call klog_print
pop esi
.end:
ret ret
;; Function: klog ;; Function: klog
;;
;; Output kernel log ;; Output kernel log
;;
;; In:
;; EAX - null-terminated string
;; STACK[X] - var_arg
klog: klog:
call klog_print_time push ebp
mov ebp, esp
.loop: push esi
mov esi, eax
; display log time
call _klog_print_time
mov ecx, 4
.begin:
mov al, [esi] mov al, [esi]
or al, al or al, al
jz .end jz .end
; search for formating
cmp al, '%' cmp al, '%'
jne .putchar jne .putchar
; read next char
inc esi inc esi
mov al, [esi] mov al, [esi]
; check if another '%'
cmp al, '%' cmp al, '%'
je .putchar je .putchar
; check if string format
cmp al, 's' cmp al, 's'
jne .check_x jne @f
mov edi, esi ; print string
pop eax mov eax, [ebp + ecx]
pop esi push ecx
push eax call _klog_print_str
call klog_print pop ecx
mov esi, edi add ecx, 4
jmp .next jmp .next
.check_x:
; check if hex format
@@:
cmp al, 'x' cmp al, 'x'
jne .check_d jne @f
pop eax
pop edi mov eax, [ebp + ecx]
push eax push ecx
call klog_print_hex call _klog_print_hex
pop ecx
add ecx, 4
jmp .next jmp .next
.check_d:
; check if int format
@@:
cmp al, 'd' cmp al, 'd'
jne .check_u jne @f
pop eax
pop edi mov eax, [ebp + ecx]
push eax push ecx
call klog_print_integer call _klog_print_int
pop ecx
add ecx, 4
jmp .next jmp .next
.check_u:
; check if unsigned
@@:
cmp al, 'u' cmp al, 'u'
jne .unknown_format jne @f
pop eax
pop edi mov eax, [ebp + ecx]
push eax push ecx
call klog_print_unsigned call _klog_print_unsigned
pop ecx
add ecx, 4
jmp .next jmp .next
.unknown_format:
; unknown format
@@:
mov al, '?' mov al, '?'
.putchar:
.putchar: ; XXX: fix this mess
mov dx, COM1
out dx, al out dx, al
pusha pusha
call cga_putc call cga_putc
popa popa
.next: .next:
inc esi inc esi
jmp .loop jmp .begin
.end: .end:
mov esi, szCRLF mov eax, szCRLF
call klog_print call _klog_print_str
leave
ret ret
szTime db '[00:00:00] ', 0 szTime db '[00:00:00] ', 0

View file

@ -1,292 +0,0 @@
;; File: klog.inc
;; Kernel logging utilities
;;
COM1 = 0x3F8
;; Function: _klog_print
;;
;; In: EAX - Null-terminated string to print
_klog_print_str:
push ebp
mov ebp, esp
push esi
mov dx, COM1
mov esi, eax
@@:
lodsb
or al, al
jz @f
out dx, al
pusha
call cga_putc
popa
jmp @b
@@:
pop esi
leave
ret
;; Function: _klog_print_int
;;
;; In:
;; EAX - number
_klog_print_int:
test eax, eax
js .print_minus
.print_minus:
ret
;; Function: _klog_print_unsigned
;;
;; In:
;; EAX - number
_klog_print_unsigned:
push ebx
mov ebx, 10
xor ecx, ecx
@@:
xor edx, edx
div ebx
push edx
inc ecx
or eax, eax
jnz @b
@@:
pop eax
add al, 0
mov dx, COM1
out dx, al
pusha
call cga_putc
popa
dec ecx
or cl, cl
jnz @b
pop ebx
ret
;; Function: _klog_print_hex
;;
;; In:
;; EAX - number
_klog_print_hex:
push ebp
mov ebp, esp
push edi
mov edi, eax
mov eax, szHexPrefix
call _klog_print_str
or edi, edi
jz .print_zero
xor cl, cl
.begin:
cmp cl, 8
je .print_number
rol edi, 4
mov eax, edi
and eax, 0xF
mov al, byte [sDigit + eax]
mov [szKlogBuffer + ecx], al
inc cl
jmp .begin
.print_zero:
mov al, '0'
mov dx, COM1
out dx, al
pusha
call cga_putc
popa
jmp .end
.print_number:
mov [szKlogBuffer + ecx], byte 0
mov eax, szKlogBuffer
call _klog_print_str
.end:
pop edi
leave
ret
;; Function: _klog_print_time
;;
_klog_print_time:
; wait CMOS ready
@@:
mov al, 0x0A
out CMOS_COMMAND, al
in al, CMOS_DATA
and al, 0x80
jnz @b
mov al, CMOS_REG_HOUR
out CMOS_COMMAND, 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_COMMAND, 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_COMMAND, 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
mov eax, szTime
call _klog_print_str
ret
;; Function: klog
;;
;; Output kernel log
;;
;; In:
;; EAX - null-terminated string
;; STACK[X] - var_arg
klog:
push ebp
mov ebp, esp
push esi
mov esi, eax
; display log time
call _klog_print_time
mov ecx, 4
.begin:
mov al, [esi]
or al, al
jz .end
; search for formating
cmp al, '%'
jne .putchar
; read next char
inc esi
mov al, [esi]
; check if another '%'
cmp al, '%'
je .putchar
; check if string format
cmp al, 's'
jne @f
; print string
mov eax, [ebp + ecx]
push ecx
call _klog_print_str
pop ecx
add ecx, 4
jmp .next
; check if hex format
@@:
cmp al, 'x'
jne @f
mov eax, [ebp + ecx]
push ecx
call _klog_print_hex
pop ecx
add ecx, 4
jmp .next
; check if int format
@@:
cmp al, 'd'
jne @f
mov eax, [ebp + ecx]
push ecx
call _klog_print_int
pop ecx
add ecx, 4
jmp .next
; check if unsigned
@@:
cmp al, 'u'
jne @f
mov eax, [ebp + ecx]
push ecx
call _klog_print_unsigned
pop ecx
add ecx, 4
jmp .next
; unknown format
@@:
mov al, '?'
.putchar: ; XXX: fix this mess
mov dx, COM1
out dx, al
pusha
call cga_putc
popa
.next:
inc esi
jmp .begin
.end:
mov eax, szCRLF
call _klog_print_str
leave
ret
szTime db '[00:00:00] ', 0
szCRLF db CR, LF, 0
sDigit db '0123456789ABCDEF'
szHexPrefix db '0x', 0
szKlogBuffer db '00000000', 0

View file

@ -98,7 +98,7 @@ pmm_free:
;; Out: ;; Out:
;; EAX - return -1 on error ;; EAX - return -1 on error
pmm_init: pmm_init:
mov esi, szMsgMmInit mov eax, szMsgMmInit
call klog call klog
mov eax, kend mov eax, kend