From: tadryanom_bot Date: Tue, 3 Feb 2026 15:17:25 +0000 (+0000) Subject: Feat: Debugging improvements (Detailed Panic + Heap Protection) X-Git-Url: https://projects.tadryanom.me/?a=commitdiff_plain;h=b0c9f78485916f5683dcf9ed4f6a2d0fc6cbfe2a;p=AdrOS.git Feat: Debugging improvements (Detailed Panic + Heap Protection) --- diff --git a/include/utils.h b/include/utils.h index c48a904..638a62d 100644 --- a/include/utils.h +++ b/include/utils.h @@ -16,5 +16,7 @@ void reverse(char* str, int length); void itoa(int num, char* str, int base); // ASCII to Integer int atoi(const char* str); +// Hex dumper +void itoa_hex(uint32_t num, char* str); #endif diff --git a/src/arch/x86/idt.c b/src/arch/x86/idt.c index 7217ae2..8bef4bc 100644 --- a/src/arch/x86/idt.c +++ b/src/arch/x86/idt.c @@ -142,6 +142,19 @@ void register_interrupt_handler(uint8_t n, isr_handler_t handler) { interrupt_handlers[n] = handler; } +#include "utils.h" + +// ... imports ... + +void print_reg(const char* name, uint32_t val) { + char buf[16]; + uart_print(name); + uart_print(": "); + itoa_hex(val, buf); + uart_print(buf); + uart_print(" "); +} + // The Main Handler called by assembly void isr_handler(struct registers* regs) { // Check if we have a custom handler @@ -151,18 +164,35 @@ void isr_handler(struct registers* regs) { } else { // If Exception (0-31), Panic if (regs->int_no < 32) { - uart_print("\n[PANIC] Unhandled Exception: "); - // TODO: Print int_no hex - uart_print(" (Halting)\n"); + __asm__ volatile("cli"); // Stop everything + + uart_print("\n\n!!! KERNEL PANIC !!!\n"); + uart_print("Exception Number: "); + char num_buf[16]; + itoa(regs->int_no, num_buf, 10); + uart_print(num_buf); + uart_print("\n"); + uart_print("registers:\n"); + print_reg("EAX", regs->eax); print_reg("EBX", regs->ebx); print_reg("ECX", regs->ecx); print_reg("EDX", regs->edx); + uart_print("\n"); + print_reg("ESI", regs->esi); print_reg("EDI", regs->edi); print_reg("EBP", regs->ebp); print_reg("ESP", regs->esp); + uart_print("\n"); + print_reg("EIP", regs->eip); print_reg("CS ", regs->cs); print_reg("EFLAGS", regs->eflags); + uart_print("\n"); + // Print Page Fault specifics if (regs->int_no == 14) { - uart_print("Page Fault! Address: "); uint32_t cr2; __asm__ volatile("mov %%cr2, %0" : "=r"(cr2)); - // Print CR2 + uart_print("\nPAGE FAULT at address: "); + char cr2_buf[16]; + itoa_hex(cr2, cr2_buf); + uart_print(cr2_buf); + uart_print("\n"); } + uart_print("\nSystem Halted.\n"); for(;;) __asm__("hlt"); } } diff --git a/src/kernel/utils.c b/src/kernel/utils.c index 3f8c63f..cbf3370 100644 --- a/src/kernel/utils.c +++ b/src/kernel/utils.c @@ -97,3 +97,15 @@ int atoi(const char* str) { return sign * res; } + +void itoa_hex(uint32_t num, char* str) { + const char hex_chars[] = "0123456789ABCDEF"; + str[0] = '0'; + str[1] = 'x'; + + for (int i = 0; i < 8; i++) { + str[9 - i] = hex_chars[num & 0xF]; + num >>= 4; + } + str[10] = '\0'; +} diff --git a/src/mm/heap.c b/src/mm/heap.c index ed7bf9b..0ea0ee3 100644 --- a/src/mm/heap.c +++ b/src/mm/heap.c @@ -8,16 +8,45 @@ #define KHEAP_INITIAL_SIZE (10 * 1024 * 1024) // 10MB #define PAGE_SIZE 4096 +#define HEAP_MAGIC 0xCAFEBABE + // Advanced Header: Doubly Linked List typedef struct heap_header { + uint32_t magic; // Magic number for corruption detection size_t size; // Size of data uint8_t is_free; // 1 = Free, 0 = Used struct heap_header* next; // Next block - struct heap_header* prev; // Previous block (New!) + struct heap_header* prev; // Previous block } heap_header_t; static heap_header_t* head = NULL; -static heap_header_t* tail = NULL; // Keep track of tail for easier expansion later +static heap_header_t* tail = NULL; + +// Helper to check corruption +void check_integrity(heap_header_t* header) { + if (header->magic != HEAP_MAGIC) { + uart_print("\n[HEAP] CRITICAL: Heap Corruption Detected!\n"); + uart_print("Block at: "); + // TODO: print address + uart_print(" has invalid magic number.\n"); + for(;;) __asm__("hlt"); + } +} + +void kheap_init(void) { + uart_print("[HEAP] Initializing Advanced Heap (Doubly Linked)...\n"); + + // ... (rest of mapping code) ... + // Note: I will need to replace the mapping code block manually or be careful + // Since I'm using 'edit', let's stick to what changes. + // I need to find the exact block to replace. + + // Actually, I can replace the whole struct definition and init logic. + // But 'edit' tool replaces EXACT text matches. + // The previous text block I provided in 'newText' won't match because I added functions. + + // Let's abort this 'edit' approach for the whole file and do precise replacements. + void kheap_init(void) { uart_print("[HEAP] Initializing Advanced Heap (Doubly Linked)...\n"); @@ -44,6 +73,7 @@ void kheap_init(void) { // 2. Initial Block head = (heap_header_t*)KHEAP_START; + head->magic = HEAP_MAGIC; // Set Magic head->size = KHEAP_INITIAL_SIZE - sizeof(heap_header_t); head->is_free = 1; head->next = NULL; @@ -63,12 +93,19 @@ void* kmalloc(size_t size) { heap_header_t* current = head; while (current) { + // Sanity Check + if (current->magic != HEAP_MAGIC) { + uart_print("[HEAP] Corruption Detected in kmalloc scan!\n"); + for(;;) __asm__("hlt"); + } + if (current->is_free && current->size >= aligned_size) { // Found candidate. Split? if (current->size > aligned_size + sizeof(heap_header_t) + 16) { // Create new header in the remaining space heap_header_t* new_block = (heap_header_t*)((uint8_t*)current + sizeof(heap_header_t) + aligned_size); + new_block->magic = HEAP_MAGIC; // Set Magic new_block->size = current->size - aligned_size - sizeof(heap_header_t); new_block->is_free = 1; new_block->next = current->next; @@ -98,6 +135,12 @@ void kfree(void* ptr) { if (!ptr) return; heap_header_t* header = (heap_header_t*)((uint8_t*)ptr - sizeof(heap_header_t)); + + if (header->magic != HEAP_MAGIC) { + uart_print("[HEAP] Corruption Detected in kfree!\n"); + for(;;) __asm__("hlt"); + } + header->is_free = 1; // 1. Coalesce Right (Forward)