Fix stack trace, WIP usermode

This commit is contained in:
Jeremy Soller 2016-09-02 12:27:45 -06:00
parent eab03299be
commit f45958f449
5 changed files with 59 additions and 10 deletions

View file

@ -1,7 +1,8 @@
use super::halt; use super::{halt, stack_trace};
interrupt!(divide_by_zero, { interrupt!(divide_by_zero, {
print!("Divide by zero fault\n"); print!("Divide by zero fault\n");
stack_trace();
loop { halt(); } loop { halt(); }
}); });
@ -23,75 +24,90 @@ interrupt!(overflow, {
interrupt!(bound_range, { interrupt!(bound_range, {
print!("Bound range exceeded fault\n"); print!("Bound range exceeded fault\n");
stack_trace();
loop { halt(); } loop { halt(); }
}); });
interrupt!(invalid_opcode, { interrupt!(invalid_opcode, {
print!("Invalid opcode fault\n"); print!("Invalid opcode fault\n");
stack_trace();
loop { halt(); } loop { halt(); }
}); });
interrupt!(device_not_available, { interrupt!(device_not_available, {
print!("Device not available fault\n"); print!("Device not available fault\n");
stack_trace();
loop { halt(); } loop { halt(); }
}); });
interrupt_error!(double_fault, { interrupt_error!(double_fault, {
print!("Double fault\n"); print!("Double fault\n");
stack_trace();
loop { halt(); } loop { halt(); }
}); });
interrupt_error!(invalid_tss, { interrupt_error!(invalid_tss, {
print!("Invalid TSS fault\n"); print!("Invalid TSS fault\n");
stack_trace();
loop { halt(); } loop { halt(); }
}); });
interrupt_error!(segment_not_present, { interrupt_error!(segment_not_present, {
print!("Segment not present fault\n"); print!("Segment not present fault\n");
stack_trace();
loop { halt(); } loop { halt(); }
}); });
interrupt_error!(stack_segment, { interrupt_error!(stack_segment, {
print!("Stack segment fault\n"); print!("Stack segment fault\n");
stack_trace();
loop { halt(); } loop { halt(); }
}); });
interrupt_error!(protection, { interrupt_error!(protection, {
print!("Protection fault\n"); print!("Protection fault\n");
stack_trace();
loop { halt(); } loop { halt(); }
}); });
interrupt_error!(page, { interrupt_error!(page, {
print!("Page fault\n"); print!("Page fault\n");
stack_trace();
loop { halt(); } loop { halt(); }
}); });
interrupt!(fpu, { interrupt!(fpu, {
print!("FPU floating point fault\n"); print!("FPU floating point fault\n");
stack_trace();
loop { halt(); } loop { halt(); }
}); });
interrupt_error!(alignment_check, { interrupt_error!(alignment_check, {
print!("Alignment check fault\n"); print!("Alignment check fault\n");
stack_trace();
loop { halt(); } loop { halt(); }
}); });
interrupt!(machine_check, { interrupt!(machine_check, {
print!("Machine check fault\n"); print!("Machine check fault\n");
stack_trace();
loop { halt(); } loop { halt(); }
}); });
interrupt!(simd, { interrupt!(simd, {
print!("SIMD floating point fault\n"); print!("SIMD floating point fault\n");
stack_trace();
loop { halt(); } loop { halt(); }
}); });
interrupt!(virtualization, { interrupt!(virtualization, {
print!("Virtualization fault\n"); print!("Virtualization fault\n");
stack_trace();
loop { halt(); } loop { halt(); }
}); });
interrupt_error!(security, { interrupt_error!(security, {
print!("Security exception\n"); print!("Security exception\n");
stack_trace();
loop { halt(); } loop { halt(); }
}); });

View file

@ -1,5 +1,9 @@
//! Interrupt instructions //! Interrupt instructions
use core::mem;
use paging::{ActivePageTable, VirtualAddress};
pub mod exception; pub mod exception;
pub mod irq; pub mod irq;
pub mod syscall; pub mod syscall;
@ -46,12 +50,15 @@ pub unsafe fn stack_trace() {
println!("TRACE: {:>016X}", rbp); println!("TRACE: {:>016X}", rbp);
//Maximum 64 frames //Maximum 64 frames
let active_table = ActivePageTable::new();
for _frame in 0..64 { for _frame in 0..64 {
let rip = *(rbp as *const usize).offset(1); if active_table.translate(VirtualAddress::new(rbp)).is_some() && active_table.translate(VirtualAddress::new(rbp + mem::size_of::<usize>())).is_some() {
println!(" {:>016X}: {:>016X}", rbp, rip); let rip = *(rbp as *const usize).offset(1);
if rip == 0 { println!(" {:>016X}: {:>016X}", rbp, rip);
rbp = *(rbp as *const usize);
} else {
println!(" {:>016X}: GUARD PAGE", rbp);
break; break;
} }
rbp = *(rbp as *const usize);
} }
} }

View file

@ -48,7 +48,8 @@ macro_rules! interrupt {
} }
// Push scratch registers // Push scratch registers
asm!("push rax asm!("xchg bx, bx
push rax
push rcx push rcx
push rdx push rdx
push rdi push rdi
@ -63,7 +64,8 @@ macro_rules! interrupt {
inner(); inner();
// Pop scratch registers and return // Pop scratch registers and return
asm!("pop r11 asm!("xchg bx,bx
pop r11
pop r10 pop r10
pop r9 pop r9
pop r8 pop r8
@ -89,7 +91,8 @@ macro_rules! interrupt_error {
} }
// Push scratch registers // Push scratch registers
asm!("push rax asm!("xchg bx, bx
push rax
push rcx push rcx
push rdx push rdx
push rdi push rdi
@ -104,7 +107,8 @@ macro_rules! interrupt_error {
inner(); inner();
// Pop scratch registers, error code, and return // Pop scratch registers, error code, and return
asm!("pop r11 asm!("xchg bx, bx
pop r11
pop r10 pop r10
pop r9 pop r9
pop r8 pop r8

View file

@ -180,7 +180,7 @@ impl DerefMut for ActivePageTable {
} }
impl ActivePageTable { impl ActivePageTable {
unsafe fn new() -> ActivePageTable { pub unsafe fn new() -> ActivePageTable {
ActivePageTable { ActivePageTable {
mapper: Mapper::new(), mapper: Mapper::new(),
} }

View file

@ -137,6 +137,28 @@ pub unsafe extern fn kstart() -> ! {
kmain(); 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 /// Entry to rust for an AP
pub unsafe extern fn kstart_ap(stack_start: usize, stack_end: usize) -> ! { pub unsafe extern fn kstart_ap(stack_start: usize, stack_end: usize) -> ! {
{ {