diff --git a/kernel/Makefile b/kernel/Makefile index 4151de2eea..2fc2b330d0 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -34,6 +34,7 @@ $(KERNEL_ARCH_OBJS) \ kernel/APIC.o \ kernel/build_libc.o \ kernel/CPUID.o \ +kernel/Debug.o \ kernel/font.o \ kernel/Input.o \ kernel/InterruptController.o \ diff --git a/kernel/arch/i386/MMU.cpp b/kernel/arch/i386/MMU.cpp index 1061922202..593b826c06 100644 --- a/kernel/arch/i386/MMU.cpp +++ b/kernel/arch/i386/MMU.cpp @@ -1,7 +1,7 @@ #include +#include #include #include -#include #include diff --git a/kernel/include/kernel/Debug.h b/kernel/include/kernel/Debug.h new file mode 100644 index 0000000000..c85386f40e --- /dev/null +++ b/kernel/include/kernel/Debug.h @@ -0,0 +1,32 @@ +#pragma once + +#include +#include + +#define dprintln(...) \ + do { \ + BAN::Formatter::print(Debug::putchar, "[{5}.{3}] {}:{}: ", PIT::ms_since_boot() / 1000, PIT::ms_since_boot() % 1000, __FILE__, __LINE__); \ + BAN::Formatter::print(Debug::putchar, __VA_ARGS__); \ + BAN::Formatter::print(Debug::putchar, "\r\n"); \ + } while(false) + +#define dwarnln(...) \ + do { \ + BAN::Formatter::print(Debug::putchar, "\e[33m"); \ + dprintln(__VA_ARGS__); \ + BAN::Formatter::print(Debug::putchar, "\e[m"); \ + } while(false) + +#define derrorln(...) \ + do { \ + BAN::Formatter::print(Debug::putchar, "\e[31m"); \ + dprintln(__VA_ARGS__); \ + BAN::Formatter::print(Debug::putchar, "\e[m"); \ + } while(false) + +#define BOCHS_BREAK() asm volatile("xchgw %bx, %bx") + +namespace Debug +{ + void putchar(char); +} \ No newline at end of file diff --git a/kernel/include/kernel/Panic.h b/kernel/include/kernel/Panic.h index dadb4b3579..61bc260812 100644 --- a/kernel/include/kernel/Panic.h +++ b/kernel/include/kernel/Panic.h @@ -1,8 +1,7 @@ #pragma once +#include #include -#include -#include #define Panic(...) PanicImpl(__FILE__, __LINE__, __VA_ARGS__) @@ -15,15 +14,9 @@ namespace Kernel __attribute__((__noreturn__)) static void PanicImpl(const char* file, int line, const char* message, Args... args) { - derrorln("Kernel panic at {}:{}", file, line); + kprintln("\e[31mKernel panic at {}:{}\e[m", file, line); derrorln(message, args...); dump_stacktrace(); - if (TTY::IsInitialized()) - { - kprint("\e[31mKernel panic at {}:{}\n", file, line); - kprint(message, args...); - kprint("\e[m\n"); - } asm volatile("cli"); for (;;) asm volatile("hlt"); diff --git a/kernel/include/kernel/Serial.h b/kernel/include/kernel/Serial.h index 2e953c1a93..296d69d2a8 100644 --- a/kernel/include/kernel/Serial.h +++ b/kernel/include/kernel/Serial.h @@ -1,36 +1,11 @@ #pragma once -#include -#include - -#define dprintln(...) \ - do { \ - BAN::Formatter::print(Serial::serial_putc, "[{5}.{3}] {}:{}: ", PIT::ms_since_boot() / 1000, PIT::ms_since_boot() % 1000, __FILE__, __LINE__); \ - BAN::Formatter::print(Serial::serial_putc, __VA_ARGS__); \ - BAN::Formatter::print(Serial::serial_putc, "\r\n"); \ - } while(false) - -#define dwarnln(...) \ - do { \ - BAN::Formatter::print(Serial::serial_putc, "\e[33m"); \ - dprintln(__VA_ARGS__); \ - BAN::Formatter::print(Serial::serial_putc, "\e[m"); \ - } while(false) - -#define derrorln(...) \ - do { \ - BAN::Formatter::print(Serial::serial_putc, "\e[31m"); \ - dprintln(__VA_ARGS__); \ - BAN::Formatter::print(Serial::serial_putc, "\e[m"); \ - } while(false) - -#define BOCHS_BREAK() asm volatile("xchgw %bx, %bx") - namespace Serial { - void initialize(); + void Initialize(); + bool IsInitialized(); - void serial_putc(char); + void putchar(char); } \ No newline at end of file diff --git a/kernel/include/kernel/TTY.h b/kernel/include/kernel/TTY.h index 69ff3b3174..932d0089f1 100644 --- a/kernel/include/kernel/TTY.h +++ b/kernel/include/kernel/TTY.h @@ -1,7 +1,6 @@ #pragma once #include -#include class TTY { diff --git a/kernel/kernel/APIC.cpp b/kernel/kernel/APIC.cpp index 091761d785..f362b7ac5b 100644 --- a/kernel/kernel/APIC.cpp +++ b/kernel/kernel/APIC.cpp @@ -1,10 +1,9 @@ #include +#include #include #include #include -#include #include -#include #include diff --git a/kernel/kernel/Debug.cpp b/kernel/kernel/Debug.cpp new file mode 100644 index 0000000000..11b01fcf66 --- /dev/null +++ b/kernel/kernel/Debug.cpp @@ -0,0 +1,16 @@ +#include +#include +#include + +namespace Debug +{ + + void putchar(char ch) + { + if (Serial::IsInitialized()) + return Serial::putchar(ch); + if (TTY::IsInitialized()) + return TTY::PutCharCurrent(ch); + } + +} \ No newline at end of file diff --git a/kernel/kernel/Input.cpp b/kernel/kernel/Input.cpp index 0e982c9d19..cea1dc21b3 100644 --- a/kernel/kernel/Input.cpp +++ b/kernel/kernel/Input.cpp @@ -1,11 +1,11 @@ #include +#include #include #include #include #include #include #include -#include #include diff --git a/kernel/kernel/Panic.cpp b/kernel/kernel/Panic.cpp index 30270bc672..cc95b4a02d 100644 --- a/kernel/kernel/Panic.cpp +++ b/kernel/kernel/Panic.cpp @@ -12,10 +12,10 @@ namespace Kernel }; stackframe* frame = (stackframe*)__builtin_frame_address(0); - BAN::Formatter::print(Serial::serial_putc, "\e[36mStack trace:\r\n"); + BAN::Formatter::print(Debug::putchar, "\e[36mStack trace:\r\n"); while (frame) { - BAN::Formatter::print(Serial::serial_putc, " {}\r\n", (void*)frame->eip); + BAN::Formatter::print(Debug::putchar, " {}\r\n", (void*)frame->eip); frame = frame->ebp; } } diff --git a/kernel/kernel/Serial.cpp b/kernel/kernel/Serial.cpp index 4e1656139e..5a1a3a1ad7 100644 --- a/kernel/kernel/Serial.cpp +++ b/kernel/kernel/Serial.cpp @@ -1,5 +1,5 @@ +#include #include -#include #define COM1_PORT 0x3f8 @@ -8,7 +8,7 @@ namespace Serial static bool s_initialized = false; - void initialize() + void Initialize() { IO::outb(COM1_PORT + 1, 0x00); // Disable all interrupts IO::outb(COM1_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor) @@ -30,14 +30,18 @@ namespace Serial s_initialized = true; } - int is_transmit_empty() { + bool IsInitialized() + { + return s_initialized; + } + + static int is_transmit_empty() { return IO::inb(COM1_PORT + 5) & 0x20; } - void serial_putc(char c) + void putchar(char c) { - if (!s_initialized) - return; + ASSERT(s_initialized); while (is_transmit_empty() == 0); IO::outb(COM1_PORT, c); } diff --git a/kernel/kernel/Shell.cpp b/kernel/kernel/Shell.cpp index 985fa87ae9..1323a62a32 100644 --- a/kernel/kernel/Shell.cpp +++ b/kernel/kernel/Shell.cpp @@ -2,14 +2,11 @@ #include #include #include -#include #include #include #include #include -#include #include -#include #include diff --git a/kernel/kernel/TTY.cpp b/kernel/kernel/TTY.cpp index 6615336e8f..0d29b45645 100644 --- a/kernel/kernel/TTY.cpp +++ b/kernel/kernel/TTY.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include diff --git a/kernel/kernel/VesaTerminalDriver.cpp b/kernel/kernel/VesaTerminalDriver.cpp index b3b6f4096a..08c4a86139 100644 --- a/kernel/kernel/VesaTerminalDriver.cpp +++ b/kernel/kernel/VesaTerminalDriver.cpp @@ -1,7 +1,7 @@ #include +#include #include #include -#include #include extern const struct bitmap_font font; diff --git a/kernel/kernel/kernel.cpp b/kernel/kernel/kernel.cpp index a884034aaf..4f0cd24c26 100644 --- a/kernel/kernel/kernel.cpp +++ b/kernel/kernel/kernel.cpp @@ -1,3 +1,4 @@ +#include #include #include #include diff --git a/kernel/kernel/kmalloc.cpp b/kernel/kernel/kmalloc.cpp index 2cfee304ca..5a175f7f07 100644 --- a/kernel/kernel/kmalloc.cpp +++ b/kernel/kernel/kmalloc.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include