Skip to content

Commit

Permalink
kernel: Don't call x86_32 directly in the panic code.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Sep 24, 2020
1 parent d3ec260 commit e0a64e7
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 50 deletions.
6 changes: 6 additions & 0 deletions arch/Arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ TimeStamp arch_get_time();
__no_return void arch_reboot();

__no_return void arch_shutdown();

void arch_panic_dump();

void arch_dump_stack_frame(void *stackframe);

void arch_backtrace();
29 changes: 1 addition & 28 deletions arch/x86_32/Interrupts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,6 @@ static const char *_exception_messages[32] = {
"Reserved",
};

void interrupts_dump_stackframe(InterruptStackFrame *stackframe)
{
printf("\tCS=%04x DS=%04x ES=%04x FS=%04x GS=%04x\n", stackframe->cs, stackframe->ds, stackframe->es, stackframe->fs, stackframe->gs);
printf("\tEAX=%08x EBX=%08x ECX=%08x EDX=%08x\n", stackframe->eax, stackframe->ebx, stackframe->ecx, stackframe->edx);
printf("\tEDI=%08x ESI=%08x EBP=%08x ESP=%08x\n", stackframe->edi, stackframe->esi, stackframe->ebp, stackframe->esp);
printf("\tINT=%08x ERR=%08x EIP=%08x FLG=%08x\n", stackframe->intno, stackframe->err, stackframe->eip, stackframe->eflags);

printf("\tCR0=%08x CR2=%08x CR3=%08x CR4=%08x\n", CR0(), CR2(), CR3(), CR4());
}

struct Stackframe
{
struct Stackframe *ebp;
uint32_t eip;
};

void backtrace(uint32_t ebp)
{
Stackframe *stackframe = reinterpret_cast<Stackframe *>(ebp);

while (stackframe)
{
stream_format(log_stream, "\t%08x\n", stackframe->eip);
stackframe = stackframe->ebp;
}
}

extern "C" uint32_t interrupts_handler(uintptr_t esp, InterruptStackFrame stackframe)
{
if (stackframe.intno < 32)
Expand All @@ -92,7 +65,7 @@ extern "C" uint32_t interrupts_handler(uintptr_t esp, InterruptStackFrame stackf
CR2());

task_dump(scheduler_running());
backtrace(stackframe.ebp);
arch_dump_stack_frame(reinterpret_cast<void *>(&stackframe));

task_exit(-1);
}
Expand Down
49 changes: 49 additions & 0 deletions arch/x86_32/x86_32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

#include "arch/x86/COM.h"
#include "arch/x86_32/ACPI.h"
#include "arch/x86_32/CPUID.h"
#include "arch/x86_32/FPU.h"
#include "arch/x86_32/GDT.h"
#include "arch/x86_32/IDT.h"
#include "arch/x86_32/Interrupts.h"
#include "arch/x86_32/LAPIC.h"
#include "arch/x86_32/PIC.h"
#include "arch/x86_32/PIT.h"
Expand Down Expand Up @@ -129,3 +131,50 @@ __no_return void arch_shutdown()
logger_error("Failled to shutdown: Halting!");
system_stop();
}

void arch_panic_dump()
{
cpuid_dump();
}

struct Stackframe
{
struct Stackframe *ebp;
uint32_t eip;
};

void backtrace_internal(uint32_t ebp)
{
bool empty = true;
Stackframe *stackframe = reinterpret_cast<Stackframe *>(ebp);

while (stackframe)
{
empty = false;
stream_format(log_stream, "\t%08x\n", stackframe->eip);
stackframe = stackframe->ebp;
}

if (empty)
stream_format(log_stream, "\t[EMPTY]\n");
}

void arch_dump_stack_frame(void *sf)
{
auto stackframe = reinterpret_cast<InterruptStackFrame *>(sf);

printf("\tCS=%04x DS=%04x ES=%04x FS=%04x GS=%04x\n", stackframe->cs, stackframe->ds, stackframe->es, stackframe->fs, stackframe->gs);
printf("\tEAX=%08x EBX=%08x ECX=%08x EDX=%08x\n", stackframe->eax, stackframe->ebx, stackframe->ecx, stackframe->edx);
printf("\tEDI=%08x ESI=%08x EBP=%08x ESP=%08x\n", stackframe->edi, stackframe->esi, stackframe->ebp, stackframe->esp);
printf("\tINT=%08x ERR=%08x EIP=%08x FLG=%08x\n", stackframe->intno, stackframe->err, stackframe->eip, stackframe->eflags);

printf("\tCR0=%08x CR2=%08x CR3=%08x CR4=%08x\n", CR0(), CR2(), CR3(), CR4());

printf("\n\tBacktrace:\n");
backtrace_internal(stackframe->ebp);
}

void arch_backtrace()
{
backtrace_internal(EBP());
}
15 changes: 15 additions & 0 deletions arch/x86_64/x86_64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,18 @@ __no_return void arch_shutdown()
{
ASSERT_NOT_REACHED();
}

void arch_panic_dump()
{
ASSERT_NOT_REACHED();
}

void arch_dump_stack_frame(void *stackframe)
{
ASSERT_NOT_REACHED();
}

void arch_backtrace()
{
ASSERT_NOT_REACHED();
}
4 changes: 2 additions & 2 deletions kernel/.build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ KERNEL_CXXFLAGS = \
-fno-exceptions \
-ffreestanding \
-nostdlib \
-DCONFIG_KEYBOARD_LAYOUT=\""${CONFIG_KEYBOARD_LAYOUT}"\"
-DCONFIG_KEYBOARD_LAYOUT=\""${CONFIG_KEYBOARD_LAYOUT}"\"

OBJECTS += $(KERNEL_OBJECTS)

Expand All @@ -64,4 +64,4 @@ $(BUILD_DIRECTORY)/arch/%.s.o: arch/%.s
$(KERNEL_BINARY): $(KERNEL_OBJECTS)
$(DIRECTORY_GUARD)
@echo [KERNEL] [LD] $(KERNEL_BINARY)
@$(CXX) $(LDFLAGS) -T arch/x86_32/link.ld -o $@ -ffreestanding $^ -nostdlib -lgcc
@$(CXX) $(LDFLAGS) -T arch/$(BUILD_ARCH)/link.ld -o $@ -ffreestanding $^ -nostdlib -lgcc
26 changes: 6 additions & 20 deletions kernel/system/Panic.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#include <libsystem/thread/Atomic.h>

/* XXX: we should not depend on X86 directly */
#include "arch/x86_32/CPUID.h"
#include "arch/x86_32/Interrupts.h"

#include "arch/Arch.h"
#include "kernel/graphics/EarlyConsole.h"
#include "kernel/graphics/Font.h"
#include "kernel/scheduling/Scheduler.h"
Expand Down Expand Up @@ -52,8 +49,6 @@ static const char *const witty_comments[] = {
static bool has_panic = false;
static bool nested_panic = false;

void backtrace(uint32_t ebp);

void system_panic_internal(
__SOURCE_LOCATION__ location,
void *stackframe,
Expand Down Expand Up @@ -109,25 +104,16 @@ void system_panic_internal(
printf("\n");
}

printf("\n\tStackframe:\n");
if (stackframe)
{
printf("\n\tContext:\n");
interrupts_dump_stackframe((InterruptStackFrame *)stackframe);

printf("\n\tBacktrace:\n");
backtrace(((InterruptStackFrame *)stackframe)->ebp);
printf("\n");
arch_dump_stack_frame(stackframe);
}
else
{
uint32_t ebp;
asm("movl %%ebp,%0"
: "=r"(ebp)::);

printf("\n\tBacktrace:\n");
backtrace(ebp);
printf("\n");
arch_backtrace();
}
printf("\n");

memory_dump();

Expand All @@ -136,7 +122,7 @@ void system_panic_internal(
if (!nested_panic)
{
task_dump(scheduler_running());
cpuid_dump();
arch_panic_dump();
}

printf("\n");
Expand Down

0 comments on commit e0a64e7

Please sign in to comment.