diff --git a/arch/x86_64/src/interrupt/exception.rs b/arch/x86_64/src/interrupt/exception.rs index 18fc47c..40f54d5 100644 --- a/arch/x86_64/src/interrupt/exception.rs +++ b/arch/x86_64/src/interrupt/exception.rs @@ -1,7 +1,8 @@ -use super::halt; +use super::{halt, stack_trace}; interrupt!(divide_by_zero, { print!("Divide by zero fault\n"); + stack_trace(); loop { halt(); } }); @@ -23,75 +24,90 @@ interrupt!(overflow, { interrupt!(bound_range, { print!("Bound range exceeded fault\n"); + stack_trace(); loop { halt(); } }); interrupt!(invalid_opcode, { print!("Invalid opcode fault\n"); + stack_trace(); loop { halt(); } }); interrupt!(device_not_available, { print!("Device not available fault\n"); + stack_trace(); loop { halt(); } }); interrupt_error!(double_fault, { print!("Double fault\n"); + stack_trace(); loop { halt(); } }); interrupt_error!(invalid_tss, { print!("Invalid TSS fault\n"); + stack_trace(); loop { halt(); } }); interrupt_error!(segment_not_present, { print!("Segment not present fault\n"); + stack_trace(); loop { halt(); } }); interrupt_error!(stack_segment, { print!("Stack segment fault\n"); + stack_trace(); loop { halt(); } }); interrupt_error!(protection, { print!("Protection fault\n"); + stack_trace(); loop { halt(); } }); interrupt_error!(page, { print!("Page fault\n"); + stack_trace(); loop { halt(); } }); interrupt!(fpu, { print!("FPU floating point fault\n"); + stack_trace(); loop { halt(); } }); interrupt_error!(alignment_check, { print!("Alignment check fault\n"); + stack_trace(); loop { halt(); } }); interrupt!(machine_check, { print!("Machine check fault\n"); + stack_trace(); loop { halt(); } }); interrupt!(simd, { print!("SIMD floating point fault\n"); + stack_trace(); loop { halt(); } }); interrupt!(virtualization, { print!("Virtualization fault\n"); + stack_trace(); loop { halt(); } }); interrupt_error!(security, { print!("Security exception\n"); + stack_trace(); loop { halt(); } }); diff --git a/arch/x86_64/src/interrupt/mod.rs b/arch/x86_64/src/interrupt/mod.rs index bde8178..e758436 100644 --- a/arch/x86_64/src/interrupt/mod.rs +++ b/arch/x86_64/src/interrupt/mod.rs @@ -1,5 +1,9 @@ //! Interrupt instructions +use core::mem; + +use paging::{ActivePageTable, VirtualAddress}; + pub mod exception; pub mod irq; pub mod syscall; @@ -46,12 +50,15 @@ pub unsafe fn stack_trace() { println!("TRACE: {:>016X}", rbp); //Maximum 64 frames + let active_table = ActivePageTable::new(); for _frame in 0..64 { - let rip = *(rbp as *const usize).offset(1); - println!(" {:>016X}: {:>016X}", rbp, rip); - if rip == 0 { + if active_table.translate(VirtualAddress::new(rbp)).is_some() && active_table.translate(VirtualAddress::new(rbp + mem::size_of::())).is_some() { + let rip = *(rbp as *const usize).offset(1); + println!(" {:>016X}: {:>016X}", rbp, rip); + rbp = *(rbp as *const usize); + } else { + println!(" {:>016X}: GUARD PAGE", rbp); break; } - rbp = *(rbp as *const usize); } } diff --git a/arch/x86_64/src/lib.rs b/arch/x86_64/src/lib.rs index d54cb5d..33748be 100644 --- a/arch/x86_64/src/lib.rs +++ b/arch/x86_64/src/lib.rs @@ -48,7 +48,8 @@ macro_rules! interrupt { } // Push scratch registers - asm!("push rax + asm!("xchg bx, bx + push rax push rcx push rdx push rdi @@ -63,7 +64,8 @@ macro_rules! interrupt { inner(); // Pop scratch registers and return - asm!("pop r11 + asm!("xchg bx,bx + pop r11 pop r10 pop r9 pop r8 @@ -89,7 +91,8 @@ macro_rules! interrupt_error { } // Push scratch registers - asm!("push rax + asm!("xchg bx, bx + push rax push rcx push rdx push rdi @@ -104,7 +107,8 @@ macro_rules! interrupt_error { inner(); // Pop scratch registers, error code, and return - asm!("pop r11 + asm!("xchg bx, bx + pop r11 pop r10 pop r9 pop r8 diff --git a/arch/x86_64/src/paging/mod.rs b/arch/x86_64/src/paging/mod.rs index 61db9a0..f55e015 100644 --- a/arch/x86_64/src/paging/mod.rs +++ b/arch/x86_64/src/paging/mod.rs @@ -180,7 +180,7 @@ impl DerefMut for ActivePageTable { } impl ActivePageTable { - unsafe fn new() -> ActivePageTable { + pub unsafe fn new() -> ActivePageTable { ActivePageTable { mapper: Mapper::new(), } diff --git a/arch/x86_64/src/start.rs b/arch/x86_64/src/start.rs index 2ecdc9e..1b1047d 100644 --- a/arch/x86_64/src/start.rs +++ b/arch/x86_64/src/start.rs @@ -137,6 +137,28 @@ pub unsafe extern fn kstart() -> ! { kmain(); } +unsafe fn usermode(ip: usize, sp: usize) { + // Test usermode + asm!("xchg bx, bx + mov rax, 0x2B + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + + push rax + push rbx + pushfq + mov rax, 0x23 + push rax + push rcx + iretq" + : + : "{rbx}"(sp), "{rcx}"(ip) + : "rax", "rbx", "rcx", "sp" + : "intel", "volatile"); +} + /// Entry to rust for an AP pub unsafe extern fn kstart_ap(stack_start: usize, stack_end: usize) -> ! { {