From d061368f18945e1d086455e3c0d0f6e4826ca18a Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 24 Sep 2019 16:24:38 -0400 Subject: [PATCH] post-05 --- src/interrupts.rs | 33 +++++++++++++++++++++++++++++++++ src/lib.rs | 7 +++++++ src/main.rs | 4 ++++ 3 files changed, 44 insertions(+) create mode 100644 src/interrupts.rs diff --git a/src/interrupts.rs b/src/interrupts.rs new file mode 100644 index 0000000..7fa4d5e --- /dev/null +++ b/src/interrupts.rs @@ -0,0 +1,33 @@ +use lazy_static::lazy_static; +use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame}; + +use crate::println; + +#[cfg(test)] +use crate::{serial_print, serial_println}; + +lazy_static!{ + static ref IDT: InterruptDescriptorTable = { + let mut idt = InterruptDescriptorTable::new(); + idt.breakpoint.set_handler_fn(breakpoint_handler); + idt + }; +} + +pub fn init_idt() { + IDT.load(); +} + +extern "x86-interrupt" fn breakpoint_handler( + stack_frame: &mut InterruptStackFrame +) { + println!("EXCEPTION: BREAKPOINT\n{:#?}", stack_frame); +} + +#[test_case] +fn test_breakpoint_exception() { + serial_print!("test_breakpoint_exception..."); + // invoke a breakpoint exception + x86_64::instructions::interrupts::int3(); + serial_println!("[ok]"); +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index fe8dde5..395f595 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,11 +1,13 @@ #![no_std] #![cfg_attr(test, no_main)] #![feature(custom_test_frameworks)] +#![feature(abi_x86_interrupt)] #![test_runner(crate::test_runner)] #![reexport_test_harness_main = "test_main"] use core::panic::PanicInfo; +pub mod interrupts; pub mod serial; pub mod macros; pub mod vga_buffer; @@ -29,6 +31,7 @@ pub fn test_panic_handler(info: &PanicInfo) -> ! { #[cfg(test)] #[no_mangle] pub extern "C" fn _start() -> ! { + init(); test_main(); loop {} } @@ -53,4 +56,8 @@ pub fn exit_qemu(exit_code: QemuExitCode) { let mut port = Port::new(0xf4); port.write(exit_code as u32); } +} + +pub fn init() { + interrupts::init_idt(); } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 7df5839..1e32c3f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,6 +26,10 @@ fn panic(info: &PanicInfo) -> ! { pub extern "C" fn _start() -> ! { println!("Hello World{}", "!"); + blog_os::init(); + + x86_64::instructions::interrupts::int3(); + #[cfg(test)] test_main();