feature (kernel): Implemented stack-tracing

This commit is contained in:
antifallobst 2023-03-01 21:58:14 +01:00
parent 17156ab824
commit 7a0e3632e7
8 changed files with 146 additions and 1 deletions

View File

@ -46,4 +46,9 @@ typedef struct {
elf_executable_T* elf_executable_create (uint8_t* buffer);
void elf_executable_destruct (elf_executable_T* executable);
// the following stuff is temporary and should be replaced when processes are implemented
#include "boot/boot_info.h"
void elf_init_kernel_exec(boot_info_T* boot_info);
extern elf_executable_T* g_kernel_executable;
#endif //NOX_ELF_H

35
kernel/inc/mm/stack.h Normal file
View File

@ -0,0 +1,35 @@
/*
* Copyright 2023 Antifallobst <antifallobst@systemausfall.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the Software), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef NOX_STACK_H
#define NOX_STACK_H
#include "utils/stdtypes.h"
#include "utils/symbol.h"
#include "drivers/elf/elf.h"
void stack_dump_call_info (uint64_t rip, symbol_T* symbol);
void stack_trace_call_stack (uint64_t rbp);
#endif //NOX_STACK_H

View File

@ -38,4 +38,6 @@ typedef struct {
uint64_t address;
} symbol_T;
symbol_T* symbol_resolve_function_from_rip(symbol_T* symbols, uint64_t num_symbols, uint64_t rip);
#endif //NOX_SYMBOLS_H

View File

@ -26,6 +26,16 @@
#include "utils/memory.h"
#include "utils/logger.h"
// TEMPORARY BLOCK BEGIN
elf_executable_T* g_kernel_executable;
void elf_init_kernel_exec(boot_info_T* boot_info) {
g_kernel_executable = elf_executable_create(boot_info->kernel_file->address);
}
// TEMPORARY BLOCK END
bool elf_executable_validate(elf_executable_T* executable) {
if (executable->header.identity[0] != 0x7F ||
executable->header.identity[1] != 'E' ||

View File

@ -78,6 +78,10 @@ void kernel_init(boot_info_T* boot_info) {
limine_terminal_print(boot_info, " ok\n");
}
void test() {
int test = 1 / 0;
}
void kmain(boot_info_T boot_info) {
limine_terminal_print(&boot_info, "Booting NoxOS...\n");
@ -88,7 +92,9 @@ void kmain(boot_info_T boot_info) {
limine_terminal_print(&boot_info, "Kernel initialized\n");
log(LOG_INFO, "!=====[ Kernel Initialized ]=====!\n");
elf_executable_T* executable = elf_executable_create(boot_info.kernel_file->address);
elf_init_kernel_exec(&boot_info);
test();
CORE_HALT_FOREVER
}

40
kernel/src/mm/stack.c Normal file
View File

@ -0,0 +1,40 @@
/*
* Copyright 2023 Antifallobst <antifallobst@systemausfall.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the Software), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include "mm/stack.h"
#include "utils/logger.h"
void stack_dump_call_info(uint64_t rip, symbol_T* symbol) {
log(LOG_NONE, " 0x%x+%xw -> %s", symbol->address, rip - symbol->address, symbol->name);
}
void stack_trace_call_stack(uint64_t rbp) {
uint64_t rip = ((uint64_t*)rbp)[1];
symbol_T* symbol = symbol_resolve_function_from_rip(g_kernel_executable->symbols, g_kernel_executable->num_symbols, rip);
if (symbol != NULL && !string_compare(symbol->name, "_start")) {
stack_trace_call_stack(((uint64_t*)rbp)[0]);
}
stack_dump_call_info(rip, symbol);
}

View File

@ -25,6 +25,7 @@
#include "utils/logger.h"
#include "utils/core.h"
#include "platform/exceptions.h"
#include "mm/stack.h"
void panic_log_paging_info(cpu_state_T* state) {
log(LOG_NONE, "Paging Info:");
@ -76,6 +77,12 @@ void panic_log_registers(cpu_state_T* state) {
log(LOG_NONE, " RDI: 0x%x RBP: 0x%x RSP: 0x%x\n", state->rdi, state->rbp, state->rsp);
}
void panic_log_call_stack(cpu_state_T* state) {
log(LOG_NONE, "Call Stack:");
stack_trace_call_stack(state->rbp);
stack_dump_call_info(state->rip, symbol_resolve_function_from_rip(g_kernel_executable->symbols, g_kernel_executable->num_symbols, state->rip));
}
void panic(cpu_state_T* state, string_t message) {
log(LOG_ERROR, "!=====[ KERNEL PANIC ]=====!");
log(LOG_NONE, "Error Message: %s", message);
@ -92,6 +99,7 @@ void panic(cpu_state_T* state, string_t message) {
panic_log_paging_info(state);
panic_log_eflags(state);
panic_log_registers(state);
panic_log_call_stack(state);
CORE_HALT_FOREVER
}

39
kernel/src/utils/symbol.c Normal file
View File

@ -0,0 +1,39 @@
/*
* Copyright 2023 Antifallobst <antifallobst@systemausfall.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the Software), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include "utils/symbol.h"
symbol_T* symbol_resolve_function_from_rip(symbol_T* symbols, uint64_t num_symbols, uint64_t rip) {
symbol_T* symbol = NULL;
for (uint64_t i = 0; i < num_symbols; i++) {
if (symbols[i].address <= rip &&
(symbol == NULL || symbols[i].address > symbol->address) &&
symbols[i].type == SYMBOL_FUNCTION)
{
symbol = &symbols[i];
}
}
return symbol;
}