From 2d9b9144e2b1f46f2f65e44f66fac0de58882da1 Mon Sep 17 00:00:00 2001 From: antifallobst Date: Sun, 12 Feb 2023 09:44:59 +0100 Subject: [PATCH] feature (kernel): Implemented cpu state dumping on panic --- kernel/src/utils/panic.c | 47 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/kernel/src/utils/panic.c b/kernel/src/utils/panic.c index 8a826a7..cf18a4f 100644 --- a/kernel/src/utils/panic.c +++ b/kernel/src/utils/panic.c @@ -17,9 +17,54 @@ #include "utils/logger.h" #include "utils/core.h" +void panic_log_paging_info(cpu_state_T* state) { + log(LOG_NONE, "Paging Info:"); + log(LOG_NONE, " Page Map: 0x%x\n", state->cr3); +} + +void panic_log_eflags(cpu_state_T* state) { + if (state->flags) { + log(LOG_NONE, "CPU Flags:"); + if (state->flags & (1 << CPU_FLAG_CARRY)) { log(LOG_NONE, " Carry"); } + if (state->flags & (1 << CPU_FLAG_PARITY)) { log(LOG_NONE, " Parity"); } + if (state->flags & (1 << CPU_FLAG_AUXILIARY)) { log(LOG_NONE, " Auxiliary"); } + if (state->flags & (1 << CPU_FLAG_ZERO)) { log(LOG_NONE, " Zero"); } + if (state->flags & (1 << CPU_FLAG_SIGN)) { log(LOG_NONE, " Sign"); } + if (state->flags & (1 << CPU_FLAG_TRAP)) { log(LOG_NONE, " Trap"); } + if (state->flags & (1 << CPU_FLAG_INTERRUPT_ENABLE)) { log(LOG_NONE, " Interrupt Enable"); } + if (state->flags & (1 << CPU_FLAG_DIRECTION)) { log(LOG_NONE, " Direction"); } + if (state->flags & (1 << CPU_FLAG_OVERFLOW)) { log(LOG_NONE, " Overflow"); } + if (state->flags & (1 << CPU_FLAG_IO_PRIVILEGE_0)) { log(LOG_NONE, " IO Privilege 0"); } + if (state->flags & (1 << CPU_FLAG_IO_PRIVILEGE_1)) { log(LOG_NONE, " IO Privilege 1"); } + if (state->flags & (1 << CPU_FLAG_NESTED_TASK)) { log(LOG_NONE, " Nested Task"); } + if (state->flags & (1 << CPU_FLAG_RESUME)) { log(LOG_NONE, " Resume"); } + if (state->flags & (1 << CPU_FLAG_VIRTUAL_8086)) { log(LOG_NONE, " Virtual 8086"); } + if (state->flags & (1 << CPU_FLAG_ALIGNMENT_CHECK)) { log(LOG_NONE, " Alignment Check"); } + if (state->flags & (1 << CPU_FLAG_VIRTUAL_INTERRUPT)) { log(LOG_NONE, " Virtual Interrupt"); } + if (state->flags & (1 << CPU_FLAG_VIRTUAL_INTERRUPT_PENDING)) { log(LOG_NONE, " Virtual Interrupt Pending"); } + if (state->flags & (1 << CPU_FLAG_CPUID)) { log(LOG_NONE, " CPUID"); } + + log(LOG_NONE, ""); // newline + } +} + +void panic_log_registers(cpu_state_T* state) { + log(LOG_NONE, "CPU Registers:"); + log(LOG_NONE, " RIP: 0x%x RAX: 0x%x RBX: 0x%x", state->rip, state->rax, state->rbx); + log(LOG_NONE, " RCX: 0x%x RDX: 0x%x RSI: 0x%x", state->rcx, state->rdx, state->rsi); + log(LOG_NONE, " RDI: 0x%x RBP: 0x%x RSP: 0x%x\n", state->rdi, state->rbp, state->rsp); +} + void panic(cpu_state_T* state, string_t message) { log(LOG_ERROR, "!=====[ KERNEL PANIC ]=====!"); - log(LOG_ERROR, "Error Message:", message); + log(LOG_NONE, "Interrupt ID: 0x%xb", state->interrupt_id); + log(LOG_NONE, "Error Code: 0b%.32b", state->error_code); + log(LOG_NONE, "Error Message: %s\n", message); + + + panic_log_paging_info(state); + panic_log_eflags(state); + panic_log_registers(state); CORE_HALT_FOREVER } \ No newline at end of file