StupidOS/kernel/klog.inc

310 lines
3.6 KiB
PHP
Raw Normal View History

2024-07-07 13:48:22 +00:00
;; File: klog.inc
;; Kernel logging utilities
;;
2024-07-07 13:48:22 +00:00
2024-07-05 12:23:58 +00:00
COM1 = 0x3F8
;; Function: _klog_print_str
2024-07-07 13:48:22 +00:00
;;
;; In: EAX - Null-terminated string to print
_klog_print_str:
push ebp
mov ebp, esp
push esi
2024-07-05 12:23:58 +00:00
mov dx, COM1
mov esi, eax
2024-07-05 12:23:58 +00:00
@@:
lodsb
or al, al
jz @f
out dx, al
2024-07-15 06:32:59 +00:00
pusha
call cga_putc
popa
2024-07-05 12:23:58 +00:00
jmp @b
@@:
pop esi
leave
2024-07-05 12:23:58 +00:00
ret
;; Function: _klog_print_int
2024-07-22 08:29:10 +00:00
;;
;; In:
;; EAX - number
_klog_print_int:
test eax, eax
jns @f
; 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
2024-07-22 08:29:10 +00:00
ret
;; Function: _klog_print_unsigned
2024-07-22 08:29:10 +00:00
;;
;; In:
;; EAX - number
_klog_print_unsigned:
2024-07-22 08:29:10 +00:00
push ebx
mov ebx, 10
xor ecx, ecx
@@:
2024-07-22 08:29:10 +00:00
xor edx, edx
div ebx
push edx
inc ecx
or eax, eax
jnz @b
@@:
2024-07-22 08:29:10 +00:00
pop eax
add al, 0
2024-07-22 08:29:10 +00:00
mov dx, COM1
out dx, al
pusha
call cga_putc
popa
dec ecx
or cl, cl
jnz @b
2024-07-22 08:29:10 +00:00
pop ebx
2024-07-22 08:29:10 +00:00
ret
;; Function: _klog_print_hex
2024-07-07 13:48:22 +00:00
;;
;; 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
2024-07-05 12:23:58 +00:00
@@:
mov al, 0x0A
2024-07-14 16:48:36 +00:00
out CMOS_COMMAND, al
2024-07-05 12:23:58 +00:00
in al, CMOS_DATA
and al, 0x80
jnz @b
mov al, CMOS_REG_HOUR
2024-07-14 16:48:36 +00:00
out CMOS_COMMAND, al
2024-07-05 12:23:58 +00:00
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
2024-07-14 16:48:36 +00:00
out CMOS_COMMAND, al
2024-07-05 12:23:58 +00:00
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
2024-07-14 16:48:36 +00:00
out CMOS_COMMAND, al
2024-07-05 12:23:58 +00:00
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
2024-07-05 12:23:58 +00:00
ret
2024-07-05 12:23:58 +00:00
;; Function: klog
;;
2024-07-05 12:23:58 +00:00
;; Output kernel log
;;
;; In:
;; EAX - null-terminated string
;; STACK[X] - var_arg
2024-07-05 12:23:58 +00:00
klog:
push ebp
mov ebp, esp
push esi
mov esi, eax
; display log time
call _klog_print_time
2024-07-05 12:23:58 +00:00
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
@@:
2024-07-22 08:29:10 +00:00
cmp al, 'd'
jne @f
mov eax, [ebp + ecx]
push ecx
call _klog_print_int
pop ecx
add ecx, 4
2024-07-22 08:29:10 +00:00
jmp .next
; check if unsigned
@@:
2024-07-22 08:29:10 +00:00
cmp al, 'u'
jne @f
mov eax, [ebp + ecx]
push ecx
call _klog_print_unsigned
pop ecx
add ecx, 4
2024-07-22 08:29:10 +00:00
jmp .next
; unknown format
@@:
mov al, '?'
.putchar: ; XXX: fix this mess
mov dx, COM1
out dx, al
2024-07-15 06:32:59 +00:00
pusha
call cga_putc
popa
.next:
inc esi
jmp .begin
.end:
mov eax, szCRLF
call _klog_print_str
leave
2024-07-05 12:23:58 +00:00
ret
2024-05-02 11:34:27 +00:00
szTime db '[00:00:00] ', 0
szCRLF db CR, LF, 0
sDigit db '0123456789ABCDEF'
szHexPrefix db '0x', 0
szKlogBuffer db '00000000', 0