stos+amd64: Add serial logging + trace facility

Signed-off-by: Chloe M. <chloe@mensia.org>
This commit is contained in:
Chloe M.
2026-06-22 01:39:29 +00:00
parent 70dcf7938f
commit aec7ddb4a2
6 changed files with 183 additions and 3 deletions
+14 -3
View File
@@ -12,23 +12,34 @@ include ../../../mk/stos.mk
ASMFILES = $(shell find . -name "*.S")
ASMOFILES = $(ASMFILES:.S=.S.o)
CFILES = $(shell find . -name "*.c")
OFILES = $(CFILES:.c=.o)
MISC_OFILES = $(ASMOFILES)
MISC_OFILES += $(OFILES)
MISC_OFILES += $(shell find $(ST_PROJECT_ROOT)/paw/spkg/ -name "*.o")
MISC_OFILES += $(shell find $(ST_PROJECT_ROOT)/paw/stos/ -name "*.o" | grep -v "arch")
KERNEL_PATH = \
$(ST_PROJECT_ROOT)/artifacts/stoskrnl.sys
CFLAGS = \
$(SYS_CFLAGS)
CFLAGS = \
$(SYS_CFLAGS) \
-I../../target \
-I../../head/ \
-I$(ST_PROJECT_ROOT)/sdk/head
.PHONY: all
all: bin
.PHONY: bin
bin: $(ASMOFILES)
bin: $(OFILES) $(ASMOFILES)
$(PROMPT) "LD" $(KERNEL_PATH)
$(SYS_LD) -Tdevel/link.ld $(MISC_OFILES) -o $(KERNEL_PATH)
%.o: %.c
$(PROMPT) "CC" $<
$(SYS_CC) -c $< $(CFLAGS) -o $@
%.S.o: %.S
$(PROMPT) "AS" $<
$(SYS_CC) -c $< $(CFLAGS) -o $@
+105
View File
@@ -0,0 +1,105 @@
/*
* Copyright (c) 2026, Chloe M.
* Provided under the BSD-3 clause.
*
* Description: serial interface
* Author: Chloe M.
*/
#include <hal/serial.h>
#include <machine/pio.h>
#include <stdef.h>
/* Channel port numbers */
#define UART_COM1 0x3F8
#define UART_COM2 0x2F8
#define UART_COM3 0x3E8
#define UART_COM4 0x2E8
#define UART_COM5 0x5F8
#define UART_COM6 0x4F8
#define UART_COM7 0x5E8
#define UART_COM8 0x4E8
/* Register offsets */
#define UART_REG(OFFSET) (UART_COM1 + OFFSET)
#define UART_REG_FCR UART_REG(2) /* FIFO Control Register */
#define UART_REG_LCR UART_REG(3) /* Line Control Register */
#define UART_REG_MCR UART_REG(4) /* MODEM Control Register */
#define UART_REG_LSR UART_REG(5) /* Line Status Register */
#define UART_REG_MSR UART_REG(6) /* MODEM Status Register */
#define UART_REG_SR UART_REG(7) /* Scratch Register */
/* Registers when LCR.DLAB=0 */
#define UART_REG_RBR UART_REG(0) /* Receiver Buffer Register */
#define UART_REG_THR UART_REG(0) /* Transmitter Holding Register */
#define UART_REG_IER UART_REG(1) /* Interrupt Enable Register */
/* Registers when LCR.DLAB=1 */
#define UART_REG_DLL UART_REG(0) /* Divisor Latch Low */
#define UART_REG_DLH UART_REG(1) /* Divisor Latch High */
#define UART_LCR_WLS0 BIT(0) /* Word Length Select Bit 0 */
#define UART_LCR_WLS1 BIT(1) /* Word Length Select Bit 1 */
#define UART_LCR_DLAB BIT(7) /* Divisor Latch Access Bit*/
#define UART_MCR_DTR BIT(0) /* Data Terminal Ready*/
#define UART_MCR_LOOP BIT(4) /* Loop */
#define UART_LSR_THRE BIT(5) /* Transmitter Holding Register */
#define UART_DIVISOR(RATE) (115200 / RATE)
static inline BOOLEAN
UartTxEmpty(VOID)
{
return ISSET(UART_REG_LSR, UART_LSR_THRE) != 0;
}
VOID
HalSerialWrite(const CHAR *Data, USIZE Length)
{
if (Data == NULL) {
return;
}
while (!UartTxEmpty()) {
ASMV("pause");
}
for (USIZE Index = 0; Index < Length; ++Index) {
MdOutb(UART_REG_THR, Data[Index]);
}
}
ST_STATUS
HalSerialInit(VOID)
{
/* Disable interrupts */
MdOutb(UART_REG_IER, 0x00);
/* Set DLAB to set baud rate */
MdOutb(UART_REG_LCR, UART_LCR_DLAB);
/* Set speed to 57600 baud */
MdOutb(UART_REG_DLL, UART_DIVISOR(57600));
MdOutb(UART_REG_IER, 0x00);
/* Set word size to 8 bits and clear DLAB */
MdOutb(UART_REG_LCR, UART_LCR_WLS0 | UART_LCR_WLS1);
/* Disable FIFOs for now... (TODO: Use them) */
MdOutb(UART_REG_FCR, 0x00);
/* Test chip in loopback mode */
MdOutb(UART_REG_MCR, UART_MCR_LOOP);
MdOutb(UART_REG_THR, 0xF0);
if (MdInb(UART_REG_RBR != 0xF0))
return -1;
/*
* Mark the data terminal ready and clear
* loopback mode.
*/
MdOutb(UART_REG_MCR, UART_MCR_DTR);
return 0;
}
+17
View File
@@ -0,0 +1,17 @@
/*
* Copyright (c) 2026, Chloe M.
* Provided under the BSD-3 clause.
*
* Description: Trace facility
* Author: Chloe M.
*/
#ifndef _EX_TRACE_H_
#define _EX_TRACE_H_ 1
#include <lib/printf.h>
#define TRACE printf
#define FmtPrintf vsnprintf
#endif /* !_EX_TRACE_H_ */
+27
View File
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2026, Chloe M.
* Provided under the BSD-3 clause.
*
* Description: HAL serial interface
* Author: Chloe M.
*/
#ifndef _HAL_SERIAL_H_
#define _HAL_SERIAL_H_ 1
#include <stapi/status.h>
/*
* Initialize the serial driver
*/
ST_STATUS HalSerialInit(VOID);
/*
* Transmit data over serial
*
* @Data: Data to transmit
* @Length: Length of data to transmit
*/
VOID HalSerialWrite(const CHAR *Data, USIZE Length);
#endif /* !_HAL_SERIAL_H_ */
+17
View File
@@ -6,9 +6,26 @@
* Author: Chloe M.
*/
#include <ex/trace.h>
#include <hal/serial.h>
#include <stdef.h>
/*
* Display a boot banner
*/
static VOID
BootBanner(VOID)
{
TRACE("Machine has been turned on~\n");
TRACE("Booting SystemPaw3 !! <3\n");
}
VOID
KiKernelEntry(VOID)
{
/* Initialize the serial driver */
HalSerialInit();
/* Write the boot banner */
BootBanner();
}
+3
View File
@@ -6,7 +6,10 @@
* Author: Chloe M.
*/
#include <hal/serial.h>
void
_putchar(char c)
{
HalSerialWrite(&c, 1);
}