From eb934b266052965b9c480d11d6866a994f30731b Mon Sep 17 00:00:00 2001 From: John Hodge Date: Wed, 21 Apr 2010 21:11:03 +0800 Subject: [PATCH] Added kernel panic routines to output to the screen on kpanic --- Kernel/arch/x86/Makefile | 2 +- Kernel/arch/x86/errors.c | 1 + Kernel/arch/x86/kpanic.c | 55 +++++++++++++++++++++++++++++++++++++++ Kernel/arch/x86/mm_virt.c | 2 ++ Kernel/debug.c | 29 ++++++++++++++++----- Kernel/include/acess.h | 11 ++++---- 6 files changed, 88 insertions(+), 12 deletions(-) create mode 100644 Kernel/arch/x86/kpanic.c diff --git a/Kernel/arch/x86/Makefile b/Kernel/arch/x86/Makefile index 708c8dc1..c52e369b 100644 --- a/Kernel/arch/x86/Makefile +++ b/Kernel/arch/x86/Makefile @@ -32,4 +32,4 @@ endif A_OBJ = start.ao main.o lib.o desctab.ao errors.o irq.o A_OBJ += mm_phys.o mm_virt.o A_OBJ += proc.o time.o vm8086.o -#A_OBJ += gdb_stub.o +A_OBJ += kpanic.o diff --git a/Kernel/arch/x86/errors.c b/Kernel/arch/x86/errors.c index 31c385fc..f5391832 100644 --- a/Kernel/arch/x86/errors.c +++ b/Kernel/arch/x86/errors.c @@ -66,6 +66,7 @@ void ErrorHandler(tRegs *Regs) return ; } + Debug_KernelPanic(); Warning("CPU Error %i - %s, Code: 0x%x", Regs->int_num, csaERROR_NAMES[Regs->int_num], Regs->err_code); Warning(" CS:EIP = 0x%04x:%08x", Regs->cs, Regs->eip); diff --git a/Kernel/arch/x86/kpanic.c b/Kernel/arch/x86/kpanic.c new file mode 100644 index 00000000..505f698a --- /dev/null +++ b/Kernel/arch/x86/kpanic.c @@ -0,0 +1,55 @@ +/* + * Acess 2 Kernel + * By John Hodge (thePowersGang) + * - x86 Kernel Panic Handler + */ + +#include + +#define FB ((Uint16 *)(KERNEL_BASE|0xB8000)) + + int giKP_Pos = 0; + +/** + * \brief Sets the screen mode for a kernel panic + */ +void KernelPanic_SetMode() +{ + int i; + // Restore VGA 0xB8000 text mode + + // Clear Screen + for( i = 0; i < 80*25; i++ ) + { + FB[i] = 0x4F00; + } +} + +void KernelPanic_PutChar(char Ch) +{ + if( giKP_Pos > 80*25 ) return ; + switch(Ch) + { + case '\t': + do { + FB[giKP_Pos] &= 0xFF00; + FB[giKP_Pos++] |= ' '; + } while(giKP_Pos & 7); + break; + + case '\n': + giKP_Pos += 80; + case '\r': + giKP_Pos -= giKP_Pos % 80; + break; + + default: + if(' ' <= Ch && Ch < 0x7F) + { + FB[giKP_Pos] &= 0xFF00; + FB[giKP_Pos] |= Ch; + } + giKP_Pos ++; + break; + } +} diff --git a/Kernel/arch/x86/mm_virt.c b/Kernel/arch/x86/mm_virt.c index cf77b875..db89cc14 100644 --- a/Kernel/arch/x86/mm_virt.c +++ b/Kernel/arch/x86/mm_virt.c @@ -214,6 +214,8 @@ void MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs) return ; } + Debug_KernelPanic(); + // -- Check Error Code -- if(ErrorCode & 8) Warning("Reserved Bits Trashed!"); diff --git a/Kernel/debug.c b/Kernel/debug.c index e8e181d5..ac9d6179 100644 --- a/Kernel/debug.c +++ b/Kernel/debug.c @@ -11,13 +11,16 @@ #define GDB_SERIAL_PORT 0x2F8 // === IMPORTS === -extern void Threads_Dump(); +extern void Threads_Dump(void); +extern void KernelPanic_SetMode(void); +extern void KernelPanic_PutChar(char Ch); // === GLOBALS === int gDebug_Level = 0; int giDebug_KTerm = -1; int gbDebug_SerialSetup = 0; int gbGDB_SerialSetup = 0; + int gbDebug_IsKPanic = 0; // === CODE === int putDebugChar(char ch) @@ -75,11 +78,16 @@ static void Debug_Putchar(char ch) outb(SERIAL_PORT, ch); #endif - if(gbInPutChar) return ; - gbInPutChar = 1; - if(giDebug_KTerm != -1) - VFS_Write(giDebug_KTerm, 1, &ch); - gbInPutChar = 0; + if( !gbDebug_IsKPanic ) + { + if(gbInPutChar) return ; + gbInPutChar = 1; + if(giDebug_KTerm != -1) + VFS_Write(giDebug_KTerm, 1, &ch); + gbInPutChar = 0; + } + else + KernelPanic_PutChar(ch); } static void Debug_Puts(char *Str) @@ -224,6 +232,12 @@ void Debug_Fmt(const char *format, va_list *args) } } +void Debug_KernelPanic() +{ + gbDebug_IsKPanic = 1; + KernelPanic_SetMode(); +} + /** * \fn void LogF(char *Msg, ...) */ @@ -262,6 +276,9 @@ void Warning(char *Fmt, ...) void Panic(char *Fmt, ...) { va_list args; + + Debug_KernelPanic(); + Debug_Puts("Panic: "); va_start(args, Fmt); Debug_Fmt(Fmt, &args); diff --git a/Kernel/include/acess.h b/Kernel/include/acess.h index db2dd964..09c4f517 100644 --- a/Kernel/include/acess.h +++ b/Kernel/include/acess.h @@ -115,11 +115,12 @@ extern void Log_Debug(char *Ident, char *Message, ...); * \name Debugging and Errors * \{ */ -extern void Panic(char *Msg, ...); -extern void Warning(char *Msg, ...); -extern void Log(char *Fmt, ...); -extern void LogV(char *Fmt, va_list Args); -extern void LogF(char *Fmt, ...); +extern void Debug_KernelPanic(); //!< Initiate a kernel panic +extern void Panic(char *Msg, ...); //!< Print a panic message (initiates a kernel panic) +extern void Warning(char *Msg, ...); //!< Print a warning message +extern void LogF(char *Fmt, ...); //!< Print a log message without a trailing newline +extern void Log(char *Fmt, ...); //!< Print a log message +extern void LogV(char *Fmt, va_list Args); //!< va_list Log message extern void Debug_Enter(char *FuncName, char *ArgTypes, ...); extern void Debug_Log(char *FuncName, char *Fmt, ...); extern void Debug_Leave(char *FuncName, char RetType, ...); -- 2.20.1