Merge pull request #3 from phil-opp/refactor-interrupts

Clarify that the exceptions tests use their own IDT
This commit is contained in:
acheronfail
2018-10-18 22:59:46 +11:00
committed by GitHub
3 changed files with 28 additions and 14 deletions

View File

@@ -434,15 +434,14 @@ Let's create an integration test that ensures that the above continues to work.
```rust ```rust
// in src/bin/test-exception-breakpoint.rs // in src/bin/test-exception-breakpoint.rs
use blog_os::exit_qemu; […]
use core::sync::atomic::{AtomicUsize, Ordering}; use core::sync::atomic::{AtomicUsize, Ordering};
static BREAKPOINT_HANDLER_CALLED: AtomicUsize = AtomicUsize::new(0); static BREAKPOINT_HANDLER_CALLED: AtomicUsize = AtomicUsize::new(0);
#[cfg(not(test))]
#[no_mangle] #[no_mangle]
pub extern "C" fn _start() -> ! { pub extern "C" fn _start() -> ! {
init_idt(); init_test_idt();
// invoke a breakpoint exception // invoke a breakpoint exception
x86_64::instructions::int3(); x86_64::instructions::int3();
@@ -463,16 +462,31 @@ pub extern "C" fn _start() -> ! {
loop {} loop {}
} }
extern "x86-interrupt" fn breakpoint_handler(_: &mut ExceptionStackFrame) {
lazy_static! {
static ref TEST_IDT: InterruptDescriptorTable = {
let mut idt = InterruptDescriptorTable::new();
idt.breakpoint.set_handler_fn(breakpoint_handler);
idt
};
}
pub fn init_test_idt() {
TEST_IDT.load();
}
extern "x86-interrupt" fn breakpoint_handler(
_stack_frame: &mut ExceptionStackFrame)
{
BREAKPOINT_HANDLER_CALLED.fetch_add(1, Ordering::SeqCst); BREAKPOINT_HANDLER_CALLED.fetch_add(1, Ordering::SeqCst);
} }
// […] […]
``` ```
For space reasons we don't show the full content here. You can find the full file [on Github](https://github.com/phil-opp/blog_os/blob/master/src/bin/test-exception-breakpoint.rs). For space reasons we don't show the full content here. You can find the full file [on Github](https://github.com/phil-opp/blog_os/blob/master/src/bin/test-exception-breakpoint.rs).
It is basically a copy of our `main.rs` with some modifications to `_start` and `breakpoint_handler`. The most interesting part is the `BREAKPOINT_HANDLER_CALLER` static. It is an [`AtomicUsize`], an integer type that can be safely concurrently modifies because all of its operations are atomic. We increment it when the `breakpoint_handler` is called and verify in our `_start` function that the handler was called exactly once. It is similar to our `main.rs`, but uses a custom IDT called `TEST_IDT` and different `_start` and `breakpoint_handler` functions. The most interesting part is the `BREAKPOINT_HANDLER_CALLER` static. It is an [`AtomicUsize`], an integer type that can be safely concurrently modifies because all of its operations are atomic. We increment it when the `breakpoint_handler` is called and verify in our `_start` function that the handler was called exactly once.
[`AtomicUsize`]: https://doc.rust-lang.org/core/sync/atomic/struct.AtomicUsize.html [`AtomicUsize`]: https://doc.rust-lang.org/core/sync/atomic/struct.AtomicUsize.html

View File

@@ -18,7 +18,7 @@ static BREAKPOINT_HANDLER_CALLED: AtomicUsize = AtomicUsize::new(0);
#[cfg(not(test))] #[cfg(not(test))]
#[no_mangle] #[no_mangle]
pub extern "C" fn _start() -> ! { pub extern "C" fn _start() -> ! {
init_idt(); init_test_idt();
// invoke a breakpoint exception // invoke a breakpoint exception
x86_64::instructions::int3(); x86_64::instructions::int3();
@@ -59,15 +59,15 @@ fn panic(info: &PanicInfo) -> ! {
use x86_64::structures::idt::{ExceptionStackFrame, InterruptDescriptorTable}; use x86_64::structures::idt::{ExceptionStackFrame, InterruptDescriptorTable};
lazy_static! { lazy_static! {
static ref IDT: InterruptDescriptorTable = { static ref TEST_IDT: InterruptDescriptorTable = {
let mut idt = InterruptDescriptorTable::new(); let mut idt = InterruptDescriptorTable::new();
idt.breakpoint.set_handler_fn(breakpoint_handler); idt.breakpoint.set_handler_fn(breakpoint_handler);
idt idt
}; };
} }
pub fn init_idt() { pub fn init_test_idt() {
IDT.load(); TEST_IDT.load();
} }
extern "x86-interrupt" fn breakpoint_handler(_stack_frame: &mut ExceptionStackFrame) { extern "x86-interrupt" fn breakpoint_handler(_stack_frame: &mut ExceptionStackFrame) {

View File

@@ -17,7 +17,7 @@ use core::panic::PanicInfo;
#[allow(unconditional_recursion)] #[allow(unconditional_recursion)]
pub extern "C" fn _start() -> ! { pub extern "C" fn _start() -> ! {
blog_os::gdt::init(); blog_os::gdt::init();
init_idt(); init_test_idt();
fn stack_overflow() { fn stack_overflow() {
stack_overflow(); // for each recursion, the return address is pushed stack_overflow(); // for each recursion, the return address is pushed
@@ -53,7 +53,7 @@ fn panic(info: &PanicInfo) -> ! {
use x86_64::structures::idt::{ExceptionStackFrame, InterruptDescriptorTable}; use x86_64::structures::idt::{ExceptionStackFrame, InterruptDescriptorTable};
lazy_static! { lazy_static! {
static ref IDT: InterruptDescriptorTable = { static ref TEST_IDT: InterruptDescriptorTable = {
let mut idt = InterruptDescriptorTable::new(); let mut idt = InterruptDescriptorTable::new();
unsafe { unsafe {
idt.double_fault idt.double_fault
@@ -65,8 +65,8 @@ lazy_static! {
}; };
} }
pub fn init_idt() { pub fn init_test_idt() {
IDT.load(); TEST_IDT.load();
} }
extern "x86-interrupt" fn double_fault_handler( extern "x86-interrupt" fn double_fault_handler(