From 7509a5d77ff2e3cd9616497c5376768a749170b6 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 14 Oct 2025 07:46:09 +0000 Subject: [PATCH] Add enhanced debugging for segment exceptions Co-authored-by: ParrotXray <87219725+ParrotXray@users.noreply.github.com> --- kernel/src/arch/amd64/gdt.rs | 4 ++++ kernel/src/arch/amd64/isr.rs | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/kernel/src/arch/amd64/gdt.rs b/kernel/src/arch/amd64/gdt.rs index a3d5f8d..08cbc3e 100644 --- a/kernel/src/arch/amd64/gdt.rs +++ b/kernel/src/arch/amd64/gdt.rs @@ -131,4 +131,8 @@ pub fn print_info() { log_info!("User Code: {:#x} (ring 3)", selectors.user_code_selector.0); log_info!("User Data: {:#x} (ring 3)", selectors.user_data_selector.0); log_info!("TSS: {:#x}", selectors.tss_selector.0); + + // Verify that user segments have correct RPL bits + log_debug!("User Code RPL: {:?}", selectors.user_code_selector.rpl()); + log_debug!("User Data RPL: {:?}", selectors.user_data_selector.rpl()); } \ No newline at end of file diff --git a/kernel/src/arch/amd64/isr.rs b/kernel/src/arch/amd64/isr.rs index c3d2737..ac3d46c 100644 --- a/kernel/src/arch/amd64/isr.rs +++ b/kernel/src/arch/amd64/isr.rs @@ -97,6 +97,15 @@ pub extern "x86-interrupt" fn invalid_tss_handler( kprintln!(); log_fatal!("EXCEPTION: INVALID TSS (#TS)"); log_fatal!("Error Code: {:#x}", error_code); + + // Decode the error code to provide more information + let selector_index = (error_code >> 3) & 0x1FFF; // Bits 15-3 + let ti = (error_code >> 2) & 0x1; // Bit 2: Table Indicator (0=GDT, 1=LDT) + let rpl = error_code & 0x3; // Bits 1-0: RPL + + log_fatal!("Segment Selector Index: {:#x} ({})", selector_index, selector_index); + log_fatal!("Table Indicator: {} ({})", ti, if ti == 0 { "GDT" } else { "LDT" }); + log_fatal!("Requested Privilege Level: {}", rpl); log_fatal!("{:#?}", stack_frame); loop { cpu::cpu_halt(); @@ -111,6 +120,15 @@ pub extern "x86-interrupt" fn segment_not_present_handler( kprintln!(); log_fatal!("EXCEPTION: SEGMENT NOT PRESENT (#NP)"); log_fatal!("Error Code: {:#x}", error_code); + + // Decode the error code to provide more information + let selector_index = (error_code >> 3) & 0x1FFF; // Bits 15-3 + let ti = (error_code >> 2) & 0x1; // Bit 2: Table Indicator (0=GDT, 1=LDT) + let rpl = error_code & 0x3; // Bits 1-0: RPL + + log_fatal!("Segment Selector Index: {:#x} ({})", selector_index, selector_index); + log_fatal!("Table Indicator: {} ({})", ti, if ti == 0 { "GDT" } else { "LDT" }); + log_fatal!("Requested Privilege Level: {}", rpl); log_fatal!("{:#?}", stack_frame); loop { cpu::cpu_halt();