From ab458968bbeac4f17e41faa3bdd7e612b814430d Mon Sep 17 00:00:00 2001 From: vandechat96 Date: Thu, 25 May 2023 16:38:31 +0200 Subject: [PATCH] good nmi stack --- src/arch/x86_64/interrupt/exception.rs | 4 +- src/arch/x86_64/interrupt/handler.rs | 94 +++++++++++++++++--------- 2 files changed, 63 insertions(+), 35 deletions(-) diff --git a/src/arch/x86_64/interrupt/exception.rs b/src/arch/x86_64/interrupt/exception.rs index 8819ea7..a4f1b11 100644 --- a/src/arch/x86_64/interrupt/exception.rs +++ b/src/arch/x86_64/interrupt/exception.rs @@ -55,13 +55,13 @@ nmi_handler!(non_maskable, |stack| { // Page Fault let addr = 0xDEADC0DE as *mut u8; let n = core::ptr::read(addr); - println!("Value is {}", n); + //println!("Value is {}", n); // core::arch::asm!( // " // mov rdx, [0xDEADC0DE] // "); - loop{} + //loop{} let time = realtime(); println!("Exiting NMI inner: {}", time); diff --git a/src/arch/x86_64/interrupt/handler.rs b/src/arch/x86_64/interrupt/handler.rs index d1ee151..290f7ee 100644 --- a/src/arch/x86_64/interrupt/handler.rs +++ b/src/arch/x86_64/interrupt/handler.rs @@ -453,34 +453,15 @@ macro_rules! interrupt_stack { ($name:ident, @paranoid, |$stack:ident| $code:block) => { interrupt_stack!($name, nop!, save_and_set_gsbase_paranoid!, restore_gsbase_paranoid!, nop!, is_paranoid: true, |$stack| $code); } } -macro_rules! print_stack { +macro_rules! print_interrupt_stack { () => { " - mov rdi, [rsp + 8*5] - call {printV} - mov rdi, [rsp + 8*4] - call {printV} - mov rdi, [rsp + 8*3] - call {printV} - mov rdi, [rsp + 8*2] - call {printV} - mov rdi, [rsp + 8] - call {printV} - mov rdi, [rsp] - call {printV} - mov rdi, [rsp - 8] - call {printV} - mov rdi, [rsp - 8*2] - call {printV} - mov rdi, [rsp - 8*3] - call {printV} - mov rdi, [rsp - 8*4] - call {printV} - mov rdi, [rsp - 8*5] - call {printV} - " + mov rdi, rsp + call {printStack} + " } } + #[macro_export] macro_rules! nmi_handler { ($name:ident, |$stack:ident| $code:block) => { @@ -497,6 +478,47 @@ macro_rules! nmi_handler { fn hnmi(){ println!("-> Handler NMI"); } + unsafe extern "C" fn reportWord(label: &str, rsp: u64, addr: u64) { + let val : u64; + core::arch::asm!("mov {rval}, [{maddr}]", rval = out(reg) val, maddr = in(reg) addr); + print!("> {:} : 0x{:016x}", label, val); + if(addr-4 == rsp) { print!(" <-TOP>"); } else {} + if(addr == rsp) { print!(" <-RSP>"); } else {} + println!(""); + } + unsafe extern "C" fn printStack() { + unsafe { + let mut rsp: u64; + let cs: u64; + core::arch::asm!("mov {}, rdi", out(reg) rsp); + core::arch::asm!("mov {}, cs", out(reg) cs); + + let btm: u64 = crate::gdt::KPCR.tss.0.ist[0]; + let mut val: u64; + println!("Interrupt Stack [BTM:0x{:016x},RSP:0x{:016x}] ({:} bytes) CS:{}", btm, rsp, btm - rsp, cs); + + reportWord("O.SS ", rsp, btm - 1*8); + reportWord("O.RSP ", rsp, btm - 2*8); + reportWord("O.RFLAGS", rsp, btm - 3*8); + reportWord("O.CS ", rsp, btm - 4*8); + reportWord("O.RIP ", rsp, btm - 5*8); + + reportWord("Temp.RDX", rsp, btm - 6*8); + reportWord("NMI.XVar", rsp, btm - 7*8); + + reportWord("I.SS ", rsp, btm - 8*8); + reportWord("I.RSP ", rsp, btm - 9*8); + reportWord("I.RFLAGS", rsp, btm - 10*8); + reportWord("I.CS ", rsp, btm - 11*8); + reportWord("I.RIP ", rsp, btm - 12*8); + + reportWord("S.SS ", rsp, btm - 13*8); + reportWord("S.RSP ", rsp, btm - 14*8); + reportWord("S.RFLAGS", rsp, btm - 15*8); + reportWord("S.CS ", rsp, btm - 16*8); + reportWord("S.RIP ", rsp, btm - 17*8); + } + } unsafe extern "C" fn printV() { unsafe{ core::arch::asm!(" @@ -523,10 +545,12 @@ macro_rules! nmi_handler { } core::arch::asm!(concat!( " - call {hnmi} + //call {hnmi} + sub rsp, $(50*8) ", - print_stack!(), + print_interrupt_stack!(), " + add rsp, $(50*8) push rdx cmp QWORD PTR [rsp + 8*2], {GDT_KERNEL_CODE} @@ -534,9 +558,9 @@ macro_rules! nmi_handler { ", // not from user space " - call {vnmi} + //call {vnmi} mov rdi, [rsp - 8] - call {printV} + //call {printV} cmp QWORD PTR [rsp - 8], $1 je nested_nmi @@ -550,7 +574,8 @@ macro_rules! nmi_handler { // nested nmi " nested_nmi: - call {nnmi} + //call {nnmi} + /// ! repalce with address of repeat nmi and end repeat nmi mov rdx, 0x080beb1c cmp rdx, [rsp + 8] @@ -583,9 +608,9 @@ macro_rules! nmi_handler { first_nmi: mov rdx, [rsp] push $1 - call {fnmi} + //call {fnmi} ", - print_stack!(), + //print_interrupt_stack!(), // first copy (saved) " sub rsp, $(5*8) @@ -632,10 +657,12 @@ macro_rules! nmi_handler { mov QWORD PTR [rsp + 5*8], $0 - call {onmi} + //call {onmi} + sub rsp, $(50*8) ", - print_stack!(), + print_interrupt_stack!(), " + add rsp, $(50*8) iretq ", @@ -649,6 +676,7 @@ macro_rules! nmi_handler { fnmi = sym fnmi, nnmi = sym nnmi, onmi = sym onmi, + printStack = sym printStack, printV = sym printV, GDT_KERNEL_CODE = const(crate::gdt::GDT_KERNEL_CODE),