From 9cbecc16897cec84407395d45f3ef95c38419ac8 Mon Sep 17 00:00:00 2001 From: xamidev Date: Sat, 10 Jan 2026 11:04:08 +0100 Subject: [PATCH] GP Fault handler --- src/idt/idt.c | 29 ++++++++++++++++++++++++++++- src/io/term/term.c | 9 +++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/idt/idt.c b/src/idt/idt.c index af05f63..7c59c64 100644 --- a/src/idt/idt.c +++ b/src/idt/idt.c @@ -4,6 +4,7 @@ #include "io/serial/serial.h" #include "io/kbd/ps2.h" #include +#include struct interrupt_descriptor idt[256]; struct idtr idt_reg; @@ -85,6 +86,32 @@ static void page_fault_handler(struct cpu_status_t* ctx) panic(ctx); } +static void gp_fault_handler(struct cpu_status_t* ctx) +{ + DEBUG("\x1b[38;5;231mGeneral Protection Fault at rip=0x%p, err=%u (%s)\x1b[0m", + ctx->iret_rip, + ctx->error_code, + (ctx->error_code == 0) ? "NOT_SEGMENT_RELATED" : "SEGMENT_RELATED"); + + // Segment-related + if (ctx->error_code != 0) + { + bool is_external = CHECK_BIT(ctx->error_code, 0); + // is it IDT, GDT, LDT? + uint8_t table = ctx->error_code & 0x6; // 0b110 (isolate table) + uint16_t index = ctx->error_code & 0xFFF8; // 13*1 1111111111111 + 000 = 1111111111111000 + + char* table_names[4] = {"GDT", "IDT", "LDT", "IDT"}; + + DEBUG("\x1b[38;5;231m%s in %s index %u\x1b[0m", + is_external ? "EXTERNAL" : "INTERNAL", + table_names[table], + index); + } + + panic(ctx); +} + struct cpu_status_t* interrupt_dispatch(struct cpu_status_t* context) { switch(context->vector_number) @@ -129,7 +156,7 @@ struct cpu_status_t* interrupt_dispatch(struct cpu_status_t* context) DEBUG("Stack-Segment Fault!"); break; case 13: - DEBUG("General Protection Fault!"); + gp_fault_handler(context); break; case 14: // Better debugging for page faults... diff --git a/src/io/term/term.c b/src/io/term/term.c index 2666725..6ecb5b9 100644 --- a/src/io/term/term.c +++ b/src/io/term/term.c @@ -46,7 +46,7 @@ int term_init() { fb = boot_ctx.fb->address; framebuffer = boot_ctx.fb; - DEBUG("terminal initialized"); + DEBUG("terminal initialized, fb=0x%p (width=%u height=%u pitch=%u bpp=%u)", fb, framebuffer->width, framebuffer->height, framebuffer->pitch, framebuffer->bpp); return 0; } return -ENOMEM; @@ -102,7 +102,12 @@ void term_scroll() const size_t screen_rows = framebuffer->height; // Move framebuffer up by one text row - memmove(fb, fb + row_height * row_bytes, (screen_rows - row_height) * row_bytes); + //memmove(fb, fb + row_height * row_bytes, (screen_rows - row_height) * row_bytes); + + for (size_t i = 0; i < (screen_rows - row_height) * row_bytes; i++) + { + fb[i] = fb[i + row_height * row_bytes]; + } // Clear last text row size_t clear_start = (screen_rows - row_height) * row_bytes;