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
} 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");
}
}
#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");
// 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;
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;
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)