diff options
author | Ian Moffett <ian@osmora.org> | 2024-01-17 20:59:53 -0500 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2024-01-17 20:59:53 -0500 |
commit | 469f601e210a74bb08408be894c9ec84701d6715 (patch) | |
tree | 41e8b517fceddbf581ac2333a82bb39f21899647 /sys | |
parent | 09f3fd474c0175170c177cdcb372034eda86c70f (diff) |
kernel: Add serial logging support
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'sys')
-rw-r--r-- | sys/arch/amd64/amd64/machdep.c | 12 | ||||
-rw-r--r-- | sys/arch/amd64/amd64/uart.c | 98 | ||||
-rw-r--r-- | sys/include/arch/amd64/uart.h | 40 | ||||
-rw-r--r-- | sys/include/sys/machdep.h | 1 | ||||
-rw-r--r-- | sys/kern/kern_syslog.c | 10 |
5 files changed, 161 insertions, 0 deletions
diff --git a/sys/arch/amd64/amd64/machdep.c b/sys/arch/amd64/amd64/machdep.c index ec1d48f..f5b6365 100644 --- a/sys/arch/amd64/amd64/machdep.c +++ b/sys/arch/amd64/amd64/machdep.c @@ -37,6 +37,7 @@ #include <machine/tss.h> #include <machine/spectre.h> #include <machine/cpu.h> +#include <machine/uart.h> #include <firmware/acpi/acpi.h> __MODULE_NAME("machdep"); @@ -80,6 +81,16 @@ processor_halt(void) __ASMV("cli; hlt"); } + +/* + * Send char to serial for debugging purposes. + */ +void +serial_dbgch(char c) +{ + uart8250_write(c); +} + /* * Things set up before processor_init() call... */ @@ -90,6 +101,7 @@ pre_init(void) * These are critical things that need to be set up as soon as possible * way before the processor_init() call. */ + uart8250_try_init(); interrupts_init(); gdt_load(&g_gdtr); } diff --git a/sys/arch/amd64/amd64/uart.c b/sys/arch/amd64/amd64/uart.c new file mode 100644 index 0000000..0bf0236 --- /dev/null +++ b/sys/arch/amd64/amd64/uart.c @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/types.h> +#include <sys/cdefs.h> +#include <machine/uart.h> +#include <machine/io.h> + +#define UART_COM1 0x3F8 + +#define UART_PORTNO(OFFSET) (UART_COM1 + 1) + +static bool +uart8250_transmit_empty(void) +{ + return __TEST(UART_PORTNO(5), __BIT(5)); +} + +void +uart8250_write(char byte) +{ + while (!uart8250_transmit_empty()); + outb(UART_COM1, byte); +} + +int +uart8250_try_init(void) +{ + /* Disable interrutps */ + outb(UART_PORTNO(1), 0x00); + + /* Enable DLAB to set divisor latches */ + outb(UART_PORTNO(3), 0x80); + + /* Set to 38400 baud via divisor latches (DLL and DLH)*/ + outb(UART_PORTNO(0), 0x03); + outb(UART_PORTNO(1), 0x00); + + /* + * Set Data Word Length to 8 bits... + * + * XXX: Our write does not preserve the DLAB bit... + * We want to clear it to make the baud latches + * readonly + */ + outb(UART_PORTNO(3), 0x03); + + /* + * We want FIFO to be enabled, and want to clear the + * TX/RX queues. We also want to set the interrupt + * watermark at 14 bytes. + */ + outb(UART_PORTNO(2), 0xC7); + + /* + * Enable auxiliary output 2 (used as interrupt line) and + * mark data terminal ready. + */ + outb(UART_PORTNO(4), 0x0B); + + /* Enable interrupts */ + outb(UART_PORTNO(1), 0x01); + + /* Put chip in loopback mode... test chip w/ test byte */ + outb(UART_PORTNO(4), 0x1E); + outb(UART_PORTNO(0), 0xAE); + if (inb(UART_PORTNO(0) != 0xAE)) { + /* Not the same byte, something is wrong */ + return 1; + } + return 0; +} diff --git a/sys/include/arch/amd64/uart.h b/sys/include/arch/amd64/uart.h new file mode 100644 index 0000000..a49e626 --- /dev/null +++ b/sys/include/arch/amd64/uart.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* UART 8250 init logic */ + +#ifndef _UART_H_ +#define _UART_H_ + +#include <sys/types.h> + +int uart8250_try_init(void); +void uart8250_write(char byte); + +#endif diff --git a/sys/include/sys/machdep.h b/sys/include/sys/machdep.h index f494f73..5964369 100644 --- a/sys/include/sys/machdep.h +++ b/sys/include/sys/machdep.h @@ -38,6 +38,7 @@ __weak void processor_init(void); __weak void pre_init(void); __weak void processor_halt(void); +__weak void serial_dbgch(char c); #endif /* defined(_KERNEL) */ #endif /* !_SYS_MACHDEP_H_ */ diff --git a/sys/kern/kern_syslog.c b/sys/kern/kern_syslog.c index ebc5512..84943b3 100644 --- a/sys/kern/kern_syslog.c +++ b/sys/kern/kern_syslog.c @@ -29,6 +29,7 @@ #include <sys/syslog.h> #include <sys/tty.h> +#include <sys/machdep.h> #include <string.h> static struct tty syslog_tty; @@ -39,6 +40,15 @@ static bool is_conlog_init = false; static void syslog_write(const char *s, size_t len) { +#if defined(__SERIAL_DEBUG) + size_t tmp_len = len; + const char *tmp_s = s; + + while (tmp_len--) { + serial_dbgch(*tmp_s++); + } +#endif /* defined(__SERIAL_DEBUG) */ + if (is_conlog_init) { tty_write(&syslog_tty, s, len); } |