summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2024-01-17 20:59:53 -0500
committerIan Moffett <ian@osmora.org>2024-01-17 20:59:53 -0500
commit469f601e210a74bb08408be894c9ec84701d6715 (patch)
tree41e8b517fceddbf581ac2333a82bb39f21899647
parent09f3fd474c0175170c177cdcb372034eda86c70f (diff)
kernel: Add serial logging support
Signed-off-by: Ian Moffett <ian@osmora.org>
-rw-r--r--Makefile.in10
-rw-r--r--configure.ac11
-rw-r--r--sys/arch/amd64/amd64/machdep.c12
-rw-r--r--sys/arch/amd64/amd64/uart.c98
-rw-r--r--sys/include/arch/amd64/uart.h40
-rw-r--r--sys/include/sys/machdep.h1
-rw-r--r--sys/kern/kern_syslog.c10
7 files changed, 181 insertions, 1 deletions
diff --git a/Makefile.in b/Makefile.in
index b00c05d..63120db 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -5,6 +5,7 @@
# Feature stuff
###############################
override SPECTRE_MITIGATION = @SPECTRE_MITIGATION@
+override SERIAL_DEBUG = @SERIAL_DEBUG@
ifeq ($(SPECTRE_MITIGATION), 1)
override SPECTRE_FLAGS=-D__SPECTRE_MITIGATION
@@ -12,6 +13,12 @@ else
override SPECTRE_FLAGS=
endif
+ifeq ($(SERIAL_DEBUG), 1)
+ override SERIAL_DEBUG_FLAGS=-D__SERIAL_DEBUG
+else
+ override SERIAL_DEBUG_FLAGS=
+endif
+
###############################
# CFLAGS, QEMU flags + misc
@@ -23,7 +30,8 @@ override KERNEL_DEFINES = -DHYRA_VERSION="\"$(HYRA_VERSION)\""\
-DHYRA_BUILDDATE="\"@HYRA_BUILDDATE@\""\
-DHYRA_BUILDBRANCH="\"@HYRA_BUILDBRANCH@\""\
-DHYRA_ARCH="\"@ARCH@\""\
- $(SPECTRE_FLAGS)
+ $(SPECTRE_FLAGS)\
+ $(SERIAL_DEBUG_FLAGS)
override KERNEL_CFLAGS = @KERNEL_CFLAGS@ $(KERNEL_DEFINES)
override KERNEL_LDFLAGS = -nostdlib -zmax-page-size=0x1000 -static -Tconf/link-$(ARCH).ld
override QEMU_FLAGS = @QEMU_FLAGS@
diff --git a/configure.ac b/configure.ac
index ffb2daf..afe3e34 100644
--- a/configure.ac
+++ b/configure.ac
@@ -21,6 +21,11 @@ AC_ARG_ENABLE([spectre-mitigation],
[AS_HELP_STRING([--enable-spectre-mitigation], [Enable Spectre mitigation (IBRS or similar)])],,
[enable_spectre_mitigation=$enableval])
+# Enable serial debug
+AC_ARG_ENABLE([serial-debug],
+ [AS_HELP_STRING([--enable-serial-debug], [Allow serial debugging])],,
+ [enable_serial_debug=$enableval])
+
if test "x$enable_spectre_mitigation" = "xyes"; then
AC_SUBST(SPECTRE_MITIGATION, [1])
@@ -28,6 +33,12 @@ else
AC_SUBST(SPECTRE_MITIGATION, [0])
fi
+if test "x$enable_serial_debug" = "xyes"; then
+ AC_SUBST(SERIAL_DEBUG, [1])
+else
+ AC_SUBST(SERIAL_DEBUG, [0])
+fi
+
AC_SUBST(HYRA_BUILDDATE, [$HYRA_BUILDDATE])
AC_SUBST(HYRA_BUILDBRANCH, [$HYRA_BUILDBRANCH])
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);
}