Kernel: Improve kernel panic message and rename it 'panic'->'Panic'

This commit is contained in:
Bananymous 2023-01-09 14:56:20 +02:00
parent 531f470132
commit efd8638a96
19 changed files with 91 additions and 64 deletions

View File

@ -5,8 +5,8 @@
#include <string.h> #include <string.h>
#if defined(__is_kernel) #if defined(__is_kernel)
#include <kernel/panic.h> #include <kernel/Panic.h>
#define MUST(error) ({ auto e = error; if (e.IsError()) Kernel::panic("{}", e.GetError()); e.Value(); }) #define MUST(error) ({ auto e = error; if (e.IsError()) Kernel::Panic("{}", e.GetError()); e.Value(); })
#else #else
#error "NOT IMPLEMENTED" #error "NOT IMPLEMENTED"
#endif #endif

View File

@ -32,6 +32,7 @@ BUILDDIR=$(abspath build)
KERNEL_OBJS= \ KERNEL_OBJS= \
$(KERNEL_ARCH_OBJS) \ $(KERNEL_ARCH_OBJS) \
kernel/build_libc.o \ kernel/build_libc.o \
kernel/font.o \
kernel/Input.o \ kernel/Input.o \
kernel/kernel.o \ kernel/kernel.o \
kernel/kmalloc.o \ kernel/kmalloc.o \
@ -41,6 +42,7 @@ kernel/RTC.o \
kernel/Serial.o \ kernel/Serial.o \
kernel/Shell.o \ kernel/Shell.o \
kernel/SSP.o \ kernel/SSP.o \
kernel/TTY.o \
icxxabi.o \ icxxabi.o \
OBJS= \ OBJS= \
@ -81,7 +83,7 @@ $(ARCHDIR)/crtbegin.o $(ARCHDIR)/crtend.o:
$(CC) -MD -c $< -o $(BUILDDIR)/$@ $(CFLAGS) $(CC) -MD -c $< -o $(BUILDDIR)/$@ $(CFLAGS)
always: always:
mkdir -p $(BUILDDIR)/arch/i386 mkdir -p $(BUILDDIR)/$(ARCHDIR)
mkdir -p $(BUILDDIR)/kernel mkdir -p $(BUILDDIR)/kernel
clean: clean:

View File

@ -4,7 +4,7 @@
#include <kernel/IO.h> #include <kernel/IO.h>
#include <kernel/kprint.h> #include <kernel/kprint.h>
#include <kernel/Paging.h> #include <kernel/Paging.h>
#include <kernel/panic.h> #include <kernel/Panic.h>
#include <kernel/PIC.h> #include <kernel/PIC.h>
#include <kernel/Serial.h> #include <kernel/Serial.h>

View File

@ -1,6 +1,6 @@
#include <kernel/APIC.h> #include <kernel/APIC.h>
#include <kernel/IDT.h> #include <kernel/IDT.h>
#include <kernel/panic.h> #include <kernel/Panic.h>
#include <kernel/kprint.h> #include <kernel/kprint.h>
#include <kernel/Serial.h> #include <kernel/Serial.h>
@ -54,7 +54,7 @@ static void (*s_irq_handlers[0xFF])() { nullptr };
kprintln("eax=0x{8H}, ebx=0x{8H}, ecx=0x{8H}, edx=0x{8H}", eax, ebx, ecx, edx); \ kprintln("eax=0x{8H}, ebx=0x{8H}, ecx=0x{8H}, edx=0x{8H}", eax, ebx, ecx, edx); \
kprintln("esp=0x{8H}, ebp=0x{8H}", esp, ebp); \ kprintln("esp=0x{8H}, ebp=0x{8H}", esp, ebp); \
kprintln("CR0=0x{8H} CR2=0x{8H} CR3=0x{8H} CR4=0x{8H}", cr0, cr2, cr3, cr4); \ kprintln("CR0=0x{8H} CR2=0x{8H} CR3=0x{8H} CR4=0x{8H}", cr0, cr2, cr3, cr4); \
Kernel::panic(msg); \ Kernel::Panic(msg); \
} }
#define INTERRUPT_HANDLER_ERR(i, msg) \ #define INTERRUPT_HANDLER_ERR(i, msg) \
@ -76,7 +76,7 @@ static void (*s_irq_handlers[0xFF])() { nullptr };
kprintln("eax=0x{8H}, ebx=0x{8H}, ecx=0x{8H}, edx=0x{8H}", eax, ebx, ecx, edx); \ kprintln("eax=0x{8H}, ebx=0x{8H}, ecx=0x{8H}, edx=0x{8H}", eax, ebx, ecx, edx); \
kprintln("esp=0x{8H}, ebp=0x{8H}", esp, ebp); \ kprintln("esp=0x{8H}, ebp=0x{8H}", esp, ebp); \
kprintln("CR0=0x{8H} CR2=0x{8H} CR3=0x{8H} CR4=0x{8H}", cr0, cr2, cr3, cr4); \ kprintln("CR0=0x{8H} CR2=0x{8H} CR3=0x{8H} CR4=0x{8H}", cr0, cr2, cr3, cr4); \
Kernel::panic(msg " (error code: 0x{8H})", error_code); \ Kernel::Panic(msg " (error code: 0x{8H})", error_code); \
} }
INTERRUPT_HANDLER____(0x00, "Division Error") INTERRUPT_HANDLER____(0x00, "Division Error")
@ -142,7 +142,7 @@ found:
if (s_irq_handlers[irq]) if (s_irq_handlers[irq])
s_irq_handlers[irq](); s_irq_handlers[irq]();
else else
Kernel::panic("no handler for irq 0x{2H}\n", irq); Kernel::Panic("no handler for irq 0x{2H}\n", irq);
APIC::EOI(irq); APIC::EOI(irq);
} }
@ -176,7 +176,7 @@ namespace IDT
static void unimplemented_trap() static void unimplemented_trap()
{ {
Kernel::panic("Unhandeled IRQ"); Kernel::Panic("Unhandeled IRQ");
} }
static void register_interrupt_handler(uint8_t index, void (*f)()) static void register_interrupt_handler(uint8_t index, void (*f)())

View File

@ -1,6 +1,6 @@
#include <kernel/CPUID.h> #include <kernel/CPUID.h>
#include <kernel/Paging.h> #include <kernel/Paging.h>
#include <kernel/panic.h> #include <kernel/Panic.h>
#include <string.h> #include <string.h>
@ -58,8 +58,6 @@ namespace Paging
:: "r" (s_page_directory_pointer_table) :: "r" (s_page_directory_pointer_table)
: "eax" : "eax"
); );
dprintln("Paging enabled");
} }
void MapPage(uintptr_t address) void MapPage(uintptr_t address)

View File

@ -3,7 +3,7 @@
#include <kernel/kmalloc.h> #include <kernel/kmalloc.h>
#include <kernel/multiboot.h> #include <kernel/multiboot.h>
#include <kernel/Paging.h> #include <kernel/Paging.h>
#include <kernel/panic.h> #include <kernel/Panic.h>
#include <kernel/Serial.h> #include <kernel/Serial.h>
#include <kernel/VESA.h> #include <kernel/VESA.h>
@ -26,6 +26,7 @@ namespace VESA
static uint32_t s_width = 0; static uint32_t s_width = 0;
static uint32_t s_height = 0; static uint32_t s_height = 0;
static uint8_t s_mode = 0; static uint8_t s_mode = 0;
static bool s_initialized = false;
static uint32_t s_terminal_width = 0; static uint32_t s_terminal_width = 0;
static uint32_t s_terminal_height = 0; static uint32_t s_terminal_height = 0;
@ -82,6 +83,11 @@ namespace VESA
return s_terminal_height; return s_terminal_height;
} }
bool IsInitialized()
{
return s_initialized;
}
bool Initialize() bool Initialize()
{ {
if (!(s_multiboot_info->flags & MULTIBOOT_FLAGS_FRAMEBUFFER)) if (!(s_multiboot_info->flags & MULTIBOOT_FLAGS_FRAMEBUFFER))
@ -132,6 +138,7 @@ namespace VESA
SetCursorPositionImpl(0, 0, Color::BRIGHT_WHITE); SetCursorPositionImpl(0, 0, Color::BRIGHT_WHITE);
ClearImpl(Color::BLACK); ClearImpl(Color::BLACK);
s_initialized = true;
return true; return true;
} }

View File

@ -7,9 +7,7 @@ KERNEL_ARCH_OBJS= \
$(ARCHDIR)/APIC.o \ $(ARCHDIR)/APIC.o \
$(ARCHDIR)/boot.o \ $(ARCHDIR)/boot.o \
$(ARCHDIR)/CPUID.o \ $(ARCHDIR)/CPUID.o \
$(ARCHDIR)/font.o \
$(ARCHDIR)/GDT.o \ $(ARCHDIR)/GDT.o \
$(ARCHDIR)/IDT.o \ $(ARCHDIR)/IDT.o \
$(ARCHDIR)/Paging.o \ $(ARCHDIR)/Paging.o \
$(ARCHDIR)/TTY.o \
$(ARCHDIR)/VESA.o \ $(ARCHDIR)/VESA.o \

View File

@ -0,0 +1,31 @@
#pragma once
#include <kernel/kprint.h>
#include <kernel/Serial.h>
#include <kernel/VESA.h>
#define Panic(...) PanicImpl(__FILE__, __LINE__, __VA_ARGS__)
namespace Kernel
{
template<typename... Args>
__attribute__((__noreturn__))
static void PanicImpl(const char* file, int line, const char* message, Args... args)
{
if (VESA::IsInitialized())
{
kprint("\e[31mKernel panic at {}:{}\n", file, line);
kprint(message, args...);
kprint("\e[m\n");
}
else
{
derrorln("Kernel panic at {}:{}", file, line);
derrorln(message, args...);
}
asm volatile("cli; hlt");
__builtin_unreachable();
}
}

View File

@ -26,6 +26,7 @@ namespace VESA
}; };
bool Initialize(); bool Initialize();
bool IsInitialized();
void PutBitmapAt(const uint8_t*, uint32_t, uint32_t, Color); void PutBitmapAt(const uint8_t*, uint32_t, uint32_t, Color);
void PutBitmapAt(const uint8_t*, uint32_t, uint32_t, Color, Color); void PutBitmapAt(const uint8_t*, uint32_t, uint32_t, Color, Color);
void PutCharAt(uint16_t, uint32_t, uint32_t, Color, Color); void PutCharAt(uint16_t, uint32_t, uint32_t, Color, Color);

View File

@ -1,19 +0,0 @@
#pragma once
#include <kernel/kprint.h>
namespace Kernel
{
template<typename... Args>
__attribute__((__noreturn__))
static void panic(const char* message, Args... args)
{
kprint("\e[31mKernel panic: ");
kprint(message, args...);
kprint("\e[m\n");
asm volatile("cli; hlt");
__builtin_unreachable();
}
}

View File

@ -307,7 +307,7 @@ namespace Input
} }
else else
{ {
Kernel::panic("Unknown target"); Kernel::Panic("Unknown target");
} }
} }
@ -317,20 +317,20 @@ namespace Input
{ {
auto& command = s_command_queue.Front(); auto& command = s_command_queue.Front();
if (command.target != TARGET_KEYBOARD && command.target != TARGET_MOUSE) if (command.target != TARGET_KEYBOARD && command.target != TARGET_MOUSE)
Kernel::panic("Undefined target for command 0x{2H}", command.command); Kernel::Panic("Undefined target for command 0x{2H}", command.command);
if (command._sent == 0 && command._ack == 0) if (command._sent == 0 && command._ack == 0)
{ {
command._sent++; command._sent++;
if (!i8042_command(command.target, command.command)) if (!i8042_command(command.target, command.command))
Kernel::panic("PS/2 command oof {}, 0x{2H}", command.target, command.command); Kernel::Panic("PS/2 command oof {}, 0x{2H}", command.target, command.command);
} }
if (command._sent == 1 && command._ack == 1 && command.has_data) if (command._sent == 1 && command._ack == 1 && command.has_data)
{ {
command._sent++; command._sent++;
if (!i8042_command(command.target, command.data)) if (!i8042_command(command.target, command.data))
Kernel::panic("PS/2 data oof {}, 0x{2H}", command.target, command.data); Kernel::Panic("PS/2 data oof {}, 0x{2H}", command.target, command.data);
} }
if (command._sent > 0 && PIT::ms_since_boot() > s_command_sent + 1000) if (command._sent > 0 && PIT::ms_since_boot() > s_command_sent + 1000)
@ -349,14 +349,14 @@ namespace Input
{ {
case I8042_KB_RESET: case I8042_KB_RESET:
if (s_command_response[0] != I8042_KB_SELF_TEST_PASS) if (s_command_response[0] != I8042_KB_SELF_TEST_PASS)
Kernel::panic("PS/2 Keyboard self test failed"); Kernel::Panic("PS/2 Keyboard self test failed");
break; break;
case I8042_KB_SET_SCAN_CODE_SET: case I8042_KB_SET_SCAN_CODE_SET:
break; break;
case I8042_KB_SET_LEDS: case I8042_KB_SET_LEDS:
break; break;
default: default:
Kernel::panic("PS/2 Keyboard unhandled command"); Kernel::Panic("PS/2 Keyboard unhandled command");
} }
} }
else if (command.target == TARGET_MOUSE) else if (command.target == TARGET_MOUSE)
@ -365,16 +365,16 @@ namespace Input
{ {
case I8042_MOUSE_RESET: case I8042_MOUSE_RESET:
if (s_command_response[0] != I8042_MOUSE_SELF_TEST_PASS) if (s_command_response[0] != I8042_MOUSE_SELF_TEST_PASS)
Kernel::panic("PS/2 Mouse self test failed"); Kernel::Panic("PS/2 Mouse self test failed");
if (s_command_response[1] != 0x00) if (s_command_response[1] != 0x00)
Kernel::panic("PS/2 Mouse invalid byte sent after self test"); Kernel::Panic("PS/2 Mouse invalid byte sent after self test");
break; break;
case I8042_MOUSE_ENABLE: case I8042_MOUSE_ENABLE:
break; break;
case I8042_MOUSE_DISABLE: case I8042_MOUSE_DISABLE:
break; break;
default: default:
Kernel::panic("PS/2 Mouse unhandled command"); Kernel::Panic("PS/2 Mouse unhandled command");
} }
} }

View File

@ -1,4 +1,4 @@
#include <kernel/panic.h> #include <kernel/Panic.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
@ -16,7 +16,7 @@ __BEGIN_DECLS
__attribute__((noreturn)) __attribute__((noreturn))
void __stack_chk_fail(void) void __stack_chk_fail(void)
{ {
Kernel::panic("Stack smashing detected"); Kernel::Panic("Stack smashing detected");
abort(); abort();
} }

View File

@ -1,5 +1,5 @@
#include <kernel/kmalloc.h> #include <kernel/kmalloc.h>
#include <kernel/panic.h> #include <kernel/Panic.h>
#include <kernel/Serial.h> #include <kernel/Serial.h>
#include <kernel/TTY.h> #include <kernel/TTY.h>
#include <kernel/VESA.h> #include <kernel/VESA.h>
@ -242,7 +242,7 @@ void TTY::HandleAnsiEscape(uint16_t ch)
void TTY::RenderFromBuffer(uint32_t x, uint32_t y) void TTY::RenderFromBuffer(uint32_t x, uint32_t y)
{ {
if (x >= m_width || y >= m_height) if (x >= m_width || y >= m_height)
Kernel::panic("invalid render from buffer, {} {}", x, y); Kernel::Panic("invalid render from buffer, {} {}", x, y);
const auto& cell = m_buffer[y * m_width + x]; const auto& cell = m_buffer[y * m_width + x];
VESA::PutCharAt(cell.character, x, y, cell.foreground, cell.background); VESA::PutCharAt(cell.character, x, y, cell.foreground, cell.background);
} }

View File

@ -2,12 +2,10 @@
#include <kernel/GDT.h> #include <kernel/GDT.h>
#include <kernel/IDT.h> #include <kernel/IDT.h>
#include <kernel/Input.h> #include <kernel/Input.h>
#include <kernel/IO.h>
#include <kernel/kmalloc.h> #include <kernel/kmalloc.h>
#include <kernel/kprint.h> #include <kernel/kprint.h>
#include <kernel/multiboot.h> #include <kernel/multiboot.h>
#include <kernel/Paging.h> #include <kernel/Paging.h>
#include <kernel/panic.h>
#include <kernel/PIC.h> #include <kernel/PIC.h>
#include <kernel/PIT.h> #include <kernel/PIT.h>
#include <kernel/RTC.h> #include <kernel/RTC.h>
@ -62,8 +60,15 @@ extern "C" void kernel_main(multiboot_info_t* mbi, uint32_t magic)
return; return;
} }
dprintln("hello from 64 bit protected mode!");
Paging::Initialize(); Paging::Initialize();
dprintln("paging enabled");
return;
s_multiboot_info = mbi; s_multiboot_info = mbi;
if (!VESA::Initialize()) if (!VESA::Initialize())

View File

@ -1,6 +1,6 @@
#include <kernel/multiboot.h> #include <kernel/multiboot.h>
#include <kernel/kmalloc.h> #include <kernel/kmalloc.h>
#include <kernel/panic.h> #include <kernel/Panic.h>
#include <kernel/Serial.h> #include <kernel/Serial.h>
#include <stdint.h> #include <stdint.h>
@ -48,7 +48,7 @@ static bool s_initialized = false;
void kmalloc_initialize() void kmalloc_initialize()
{ {
if (!(s_multiboot_info->flags & (1 << 6))) if (!(s_multiboot_info->flags & (1 << 6)))
Kernel::panic("Kmalloc: Bootloader didn't provide a memory map"); Kernel::Panic("Kmalloc: Bootloader didn't provide a memory map");
// Validate kmalloc memory // Validate kmalloc memory
bool valid = false; bool valid = false;
@ -70,7 +70,7 @@ void kmalloc_initialize()
} }
if (!valid) if (!valid)
Kernel::panic("Kmalloc: Could not find {} MB of memory", (double)(s_kmalloc_eternal_end - s_kmalloc_node_base)); Kernel::Panic("Kmalloc: Could not find {} MB of memory", (double)(s_kmalloc_eternal_end - s_kmalloc_node_base));
s_kmalloc_node_count = 1; s_kmalloc_node_count = 1;
s_kmalloc_node_head = (kmalloc_node*)s_kmalloc_node_base; s_kmalloc_node_head = (kmalloc_node*)s_kmalloc_node_base;
@ -90,7 +90,9 @@ void kmalloc_initialize()
void kmalloc_dump_nodes() void kmalloc_dump_nodes()
{ {
if (!s_initialized) Kernel::panic("kmalloc not initialized!"); if (!s_initialized)
Kernel::Panic("kmalloc not initialized!");
dprintln("Kmalloc memory available {} MB", (float)s_kmalloc_available / MB); dprintln("Kmalloc memory available {} MB", (float)s_kmalloc_available / MB);
dprintln("Kmalloc memory allocated {} MB", (float)s_kmalloc_allocated / MB); dprintln("Kmalloc memory allocated {} MB", (float)s_kmalloc_allocated / MB);
dprintln("Using {}/{} nodes", s_kmalloc_node_count, s_kmalloc_max_nodes); dprintln("Using {}/{} nodes", s_kmalloc_node_count, s_kmalloc_max_nodes);
@ -103,13 +105,14 @@ void kmalloc_dump_nodes()
void* kmalloc_eternal(size_t size) void* kmalloc_eternal(size_t size)
{ {
if (!s_initialized) Kernel::panic("kmalloc not initialized!"); if (!s_initialized)
Kernel::Panic("kmalloc not initialized!");
if (size % ALIGN) if (size % ALIGN)
size += ALIGN - (size % ALIGN); size += ALIGN - (size % ALIGN);
if (s_kmalloc_eternal_ptr % ALIGN) if (s_kmalloc_eternal_ptr % ALIGN)
Kernel::panic("Unaligned ptr in kmalloc_eternal"); Kernel::Panic("Unaligned ptr in kmalloc_eternal");
if (s_kmalloc_eternal_ptr + size > s_kmalloc_eternal_end) if (s_kmalloc_eternal_ptr + size > s_kmalloc_eternal_end)
{ {
@ -124,7 +127,8 @@ void* kmalloc_eternal(size_t size)
void* kmalloc(size_t size) void* kmalloc(size_t size)
{ {
if (!s_initialized) Kernel::panic("kmalloc not initialized!"); if (!s_initialized)
Kernel::Panic("kmalloc not initialized!");
if (size % ALIGN) if (size % ALIGN)
size += ALIGN - (size % ALIGN); size += ALIGN - (size % ALIGN);
@ -155,7 +159,7 @@ void* kmalloc(size_t size)
{ {
valid_node.free = false; valid_node.free = false;
if (valid_node.addr % ALIGN) if (valid_node.addr % ALIGN)
Kernel::panic("Unaligned ptr in kmalloc"); Kernel::Panic("Unaligned ptr in kmalloc");
return (void*)valid_node.addr; return (void*)valid_node.addr;
} }
@ -184,13 +188,13 @@ void* kmalloc(size_t size)
s_kmalloc_available -= size; s_kmalloc_available -= size;
if (valid_node.addr % ALIGN) if (valid_node.addr % ALIGN)
Kernel::panic("Unaligned ptr in kmalloc"); Kernel::Panic("Unaligned ptr in kmalloc");
return (void*)valid_node.addr; return (void*)valid_node.addr;
} }
void kfree(void* addr) void kfree(void* addr)
{ {
if (!s_initialized) Kernel::panic("kmalloc not initialized!"); if (!s_initialized) Kernel::Panic("kmalloc not initialized!");
if (addr == nullptr) if (addr == nullptr)
return; return;

View File

@ -1,7 +1,7 @@
#include <stdio.h> #include <stdio.h>
#if defined(__is_libk) #if defined(__is_libk)
#include <kernel/panic.h> #include <kernel/Panic.h>
#else #else
#include <stdlib.h> #include <stdlib.h>
#endif #endif
@ -9,7 +9,7 @@
int putchar(int c) int putchar(int c)
{ {
#if defined(__is_libk) #if defined(__is_libk)
Kernel::panic("Please use kprint() instead of stdio"); Kernel::Panic("Please use kprint() instead of stdio");
#else #else
abort(); abort();
#endif #endif

View File

@ -2,14 +2,14 @@
#include <stdio.h> #include <stdio.h>
#if defined(__is_libk) #if defined(__is_libk)
#include <kernel/panic.h> #include <kernel/Panic.h>
#endif #endif
__attribute__((__noreturn__)) __attribute__((__noreturn__))
void abort(void) void abort(void)
{ {
#if defined(__is_libk) #if defined(__is_libk)
Kernel::panic("abort()"); Kernel::Panic("abort()");
#else #else
printf("abort()\n"); printf("abort()\n");
#endif #endif