From 41676291fea19d9e9e9357b10990762fc4e96512 Mon Sep 17 00:00:00 2001 From: d0p1 Date: Fri, 5 Jul 2024 14:23:58 +0200 Subject: [PATCH] feat(kernel): wip serial logger --- boot/loader/loader.asm | 5 +- kernel/Makefile | 2 + kernel/const.inc | 2 + kernel/dev/vga_console.inc | 22 ++++++--- kernel/init.inc | 10 ++++ kernel/kernel.asm | 37 +++++++++++---- kernel/klog.inc | 93 +++++++++++++++++++++++++++++++++++--- 7 files changed, 147 insertions(+), 24 deletions(-) create mode 100644 kernel/init.inc diff --git a/boot/loader/loader.asm b/boot/loader/loader.asm index 99476fc..8bc4086 100644 --- a/boot/loader/loader.asm +++ b/boot/loader/loader.asm @@ -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 $ diff --git a/kernel/Makefile b/kernel/Makefile index 4e287b6..48dfaef 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -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 diff --git a/kernel/const.inc b/kernel/const.inc index 128a245..95c2c94 100644 --- a/kernel/const.inc +++ b/kernel/const.inc @@ -46,3 +46,5 @@ CR4_PKE = 0x0400000 CR4_CET = 0x0800000 CR4_PKS = 0x1000000 +CR = 0x0D +LF = 0x0A diff --git a/kernel/dev/vga_console.inc b/kernel/dev/vga_console.inc index e667cd7..3ea9646 100644 --- a/kernel/dev/vga_console.inc +++ b/kernel/dev/vga_console.inc @@ -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 + diff --git a/kernel/init.inc b/kernel/init.inc new file mode 100644 index 0000000..79870be --- /dev/null +++ b/kernel/init.inc @@ -0,0 +1,10 @@ +struc TSS { + .link dw ? + .link_h dw ? + .esp0 dd ? + .ss0 dw ? + .ss0_h dw ? + .esp1 dd ? + .ss1 dw ? + +} diff --git a/kernel/kernel.asm b/kernel/kernel.asm index bb0d6e0..647aea1 100644 --- a/kernel/kernel.asm +++ b/kernel/kernel.asm @@ -1,4 +1,6 @@ ;; File: kernel.asm + format binary + include 'const.inc' org KBASE @@ -9,9 +11,6 @@ db 'STPDKRNL' db 32 dup(0) - include 'klog.inc' - include 'mm/mm.inc' - ;; Function: kmain ;; ;; Parameters: @@ -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' - ; BSS +szMsgKernelAlive db "Kernel is alive", 0 + + align 4 +stack_bottom: rb 0x4000 +stack_top: _end: + dd 0x0 diff --git a/kernel/klog.inc b/kernel/klog.inc index e377ef8..d9a9b35 100644 --- a/kernel/klog.inc +++ b/kernel/klog.inc @@ -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 -macro KLOG msg { - out 0xe9, al -} + call klog_print + + mov esi, szCRLF + call klog_print + ret + +szTime db '[00:00:00] ', 0 +szCRLF db CR, LF, 0