From bcea9313266aa0ab453dec93af391eebee70c649 Mon Sep 17 00:00:00 2001 From: Fabian Freyer Date: Thu, 22 Feb 2018 20:20:03 +0100 Subject: [PATCH] tests: allow test kernels to print to serial --- tests/data/multiboot/Makefile | 3 ++- tests/data/multiboot/libc.c | 22 +++++++++++++++++++++- tests/data/multiboot/libc.h | 18 ++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/tests/data/multiboot/Makefile b/tests/data/multiboot/Makefile index 2e89b56..53dea42 100644 --- a/tests/data/multiboot/Makefile +++ b/tests/data/multiboot/Makefile @@ -1,5 +1,6 @@ CCFLAGS=-m32 -Wall -Wextra -Werror -fno-stack-protector -nostdinc -fno-builtin ASFLAGS=-m32 +OUTPUT=COM1 UNAME_S!=uname -s LIBGCC!=$(CC) ${CCFLAGS} -print-libgcc-file-name @@ -27,7 +28,7 @@ modules.elf: start.o modules.o libc.o $(LD) $(LDFLAGS) -o $@ $> $(LIBGCC) .c.o: - $(CC) $(CCFLAGS) -c -o $@ $< + $(CC) -DOUTPUT=$(COM1) $(CCFLAGS) -c -o $@ $< .S.o: $(CC) $(ASFLAGS) -c -o $@ $< diff --git a/tests/data/multiboot/libc.c b/tests/data/multiboot/libc.c index 43502f5..2cc4414 100644 --- a/tests/data/multiboot/libc.c +++ b/tests/data/multiboot/libc.c @@ -22,6 +22,8 @@ #include "libc.h" +static char serial_set_up = 0; + void* memcpy(void *dest, const void *src, int n) { char *d = dest; @@ -46,7 +48,25 @@ void* memset(void *b, int c, unsigned len) static void print_char(char c) { - outb(0xe9, c); + #if (OUTPUT != DEBUGCON) + /* Check if serial is set up */ + if (!serial_set_up) { + outb(OUTPUT + 1, 0x00); // Disable all interrupts + outb(OUTPUT + 3, 0x80); // Enable DLAB (set baud rate divisor) + outb(OUTPUT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud + outb(OUTPUT + 1, 0x00); // (hi byte) + outb(OUTPUT + 3, 0x03); // 8 bits, no parity, one stop bit + outb(OUTPUT + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold + outb(OUTPUT + 4, 0x0B); // IRQs enabled, RTS/DSR set + serial_set_up = 1; + } + /* Check if transmit is empty */ + while ( (inb(OUTPUT + 5) & 0x20) == 0 ); + if ( c == '\n' ) + print_char('\r'); + #endif + + outb(OUTPUT, c); } static void print_str(char *s) diff --git a/tests/data/multiboot/libc.h b/tests/data/multiboot/libc.h index b16cf64..d9b943c 100644 --- a/tests/data/multiboot/libc.h +++ b/tests/data/multiboot/libc.h @@ -23,6 +23,17 @@ #ifndef LIBC_H #define LIBC_H +/* Serial Ports */ +#define DEBUGCON 0xe9 +#define COM1 0x3f8 +#define COM2 0x2F8 +#define COM3 0x3E8 +#define COM4 0x2E8 + +#ifndef OUTPUT +#define OUTPUT DEBUGCON +#endif + /* Integer types */ typedef unsigned long long uint64_t; @@ -53,6 +64,13 @@ static inline void outb(uint16_t port, uint8_t data) asm volatile ("outb %0, %1" : : "a" (data), "Nd" (port)); } +static inline uint8_t inb(uint16_t port) +{ + uint8_t ret; + asm volatile ( "inb %1, %0" : "=a"(ret) : "Nd"(port) ); + return ret; +} + /* Misc functions */