From 8a5608df918f68b0b287ea2c08b5666941692861 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sat, 22 Apr 2023 17:58:51 +0300 Subject: [PATCH] Kernel: d{print,warn,error}ln(...) now has a spinlock --- kernel/include/kernel/Debug.h | 25 +++++++++++++++++++------ kernel/kernel/Debug.cpp | 17 +++++++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/kernel/include/kernel/Debug.h b/kernel/include/kernel/Debug.h index 11ffcde53..d34c0fca1 100644 --- a/kernel/include/kernel/Debug.h +++ b/kernel/include/kernel/Debug.h @@ -5,23 +5,29 @@ #define dprintln(...) \ do { \ + Debug::DebugLock::lock(); \ 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"); \ + Debug::DebugLock::unlock(); \ } while(false) -#define dwarnln(...) \ - do { \ +#define dwarnln(...) \ + do { \ + Debug::DebugLock::lock(); \ BAN::Formatter::print(Debug::putchar, "\e[33m"); \ - dprintln(__VA_ARGS__); \ + dprintln(__VA_ARGS__); \ BAN::Formatter::print(Debug::putchar, "\e[m"); \ + Debug::DebugLock::unlock(); \ } while(false) -#define derrorln(...) \ - do { \ +#define derrorln(...) \ + do { \ + Debug::DebugLock::lock(); \ BAN::Formatter::print(Debug::putchar, "\e[31m"); \ - dprintln(__VA_ARGS__); \ + dprintln(__VA_ARGS__); \ BAN::Formatter::print(Debug::putchar, "\e[m"); \ + Debug::DebugLock::unlock(); \ } while(false) #define BOCHS_BREAK() asm volatile("xchgw %bx, %bx") @@ -30,4 +36,11 @@ namespace Debug { void dump_stack_trace(); void putchar(char); + + class DebugLock + { + public: + static void lock(); + static void unlock(); + }; } \ No newline at end of file diff --git a/kernel/kernel/Debug.cpp b/kernel/kernel/Debug.cpp index 7c3812805..ae88f12b8 100644 --- a/kernel/kernel/Debug.cpp +++ b/kernel/kernel/Debug.cpp @@ -1,5 +1,7 @@ #include +#include #include +#include #include namespace Debug @@ -53,4 +55,19 @@ namespace Debug return Kernel::TTY::putchar_current(ch); } + + static Kernel::RecursiveSpinLock s_debug_lock; + + void DebugLock::lock() + { + if (interrupts_enabled()) + s_debug_lock.lock(); + } + + void DebugLock::unlock() + { + if (interrupts_enabled()) + s_debug_lock.unlock(); + } + } \ No newline at end of file