From 1cb5c671f7a0c77787d3d6d3ba7f922dafc5d44d Mon Sep 17 00:00:00 2001 From: Curle Date: Mon, 9 Nov 2020 18:39:34 +0000 Subject: [PATCH] Add basic stack unwinding functionality --- CMakeLists.txt | 3 ++- chroma/inc/kernel/chroma.h | 1 + chroma/inc/kernel/system/stack.h | 15 +++++++++++++++ chroma/system/memory/stack.c | 29 +++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 chroma/inc/kernel/system/stack.h create mode 100644 chroma/system/memory/stack.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 7b15e04..14cc4bd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ SET(src_files ${CMAKE_SOURCE_DIR}/chroma/system/rw.c ${CMAKE_SOURCE_DIR}/chroma/system/serial.c ${CMAKE_SOURCE_DIR}/chroma/system/pci.c + ${CMAKE_SOURCE_DIR}/chroma/system/memory/stack.c ${CMAKE_SOURCE_DIR}/chroma/system/memory/paging.c ${CMAKE_SOURCE_DIR}/chroma/system/memory/abstract_allocator.c ${CMAKE_SOURCE_DIR}/chroma/system/memory/physmem.c @@ -53,5 +54,5 @@ set_property(SOURCE ${src_no_sse} PROPERTY COMPILE_FLAGS -mgeneral-regs-only) add_executable(kernel) target_sources(kernel PUBLIC ${src_preamble} PUBLIC ${src_files} PUBLIC ${src_no_sse} PUBLIC ${lib_files} PUBLIC ${CMAKE_SOURCE_DIR}/font.o PUBLIC ${src_epilogue}) -target_compile_options(kernel PRIVATE -ffreestanding -O2 -Wall -Wextra -Wall -Werror -pedantic -fPIC -fno-exceptions ) +target_compile_options(kernel PRIVATE -ffreestanding -O0 -Wall -Wextra -Wall -Werror -pedantic -fPIC -fno-exceptions -fno-omit-frame-pointer -mno-red-zone -fno-stack-protector ) target_link_options(kernel PRIVATE -T linker.ld -ffreestanding -O2 -nostdlib -nostartfiles -lgcc) diff --git a/chroma/inc/kernel/chroma.h b/chroma/inc/kernel/chroma.h index 5186271..8ec5f92 100644 --- a/chroma/inc/kernel/chroma.h +++ b/chroma/inc/kernel/chroma.h @@ -18,6 +18,7 @@ #include #include #include +#include //Removed cause "wacky copyrighted stuff" //#include diff --git a/chroma/inc/kernel/system/stack.h b/chroma/inc/kernel/system/stack.h new file mode 100644 index 0000000..14bedec --- /dev/null +++ b/chroma/inc/kernel/system/stack.h @@ -0,0 +1,15 @@ +#pragma once +#include +#include + +/************************ + *** Team Kitty, 2020 *** + *** Chroma *** + ***********************/ + +typedef struct stackframe { + struct stackframe* rbp; + size_t rip; +} stackframe_t; + +void StackTrace(size_t cycles); \ No newline at end of file diff --git a/chroma/system/memory/stack.c b/chroma/system/memory/stack.c new file mode 100644 index 0000000..45010a0 --- /dev/null +++ b/chroma/system/memory/stack.c @@ -0,0 +1,29 @@ +#include +/************************ + *** Team Kitty, 2020 *** + *** Chroma *** + ***********************/ + +/* + * This file aims to implement stack unwinding + * to trace faulty functions. + * + * I was in the middle of debugging a jump to null + * when i started creating this, so there will be a + * lot of functionality here left over from that + * initial goal, probably... + * + * //TODO: Rework this to allow unwinding function parameters on call. + */ + +void StackTrace(size_t cycles) { + struct stackframe* stack; + + __asm__ __volatile__ ("mov %%rbp, %[dest]" : [dest] "=r" (stack) : :); + SerialPrintf("[Trace] Beginning stack trace. RBP is currently 0x%p\r\n", stack); + for(size_t frame = 0; stack != 0 && frame < cycles; ++frame) { + SerialPrintf("[Trace] 0x%p \r\n", stack->rip); + stack = stack->rbp; + } + SerialPrintf("[Trace] Stack trace over.\r\n"); +} \ No newline at end of file