mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 22:37:49 +00:00
Port cpu exceptions post to second edition
This commit is contained in:
77
src/bin/test-exception-breakpoint.rs
Normal file
77
src/bin/test-exception-breakpoint.rs
Normal file
@@ -0,0 +1,77 @@
|
||||
#![feature(panic_implementation)]
|
||||
#![feature(abi_x86_interrupt)]
|
||||
#![no_std]
|
||||
#![cfg_attr(not(test), no_main)]
|
||||
#![cfg_attr(test, allow(dead_code, unused_macros, unused_imports))]
|
||||
|
||||
#[macro_use]
|
||||
extern crate blog_os;
|
||||
extern crate x86_64;
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
|
||||
use blog_os::exit_qemu;
|
||||
use core::panic::PanicInfo;
|
||||
use core::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
static BREAKPOINT_HANDLER_CALLED: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
#[cfg(not(test))]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn _start() -> ! {
|
||||
init_idt();
|
||||
|
||||
// invoke a breakpoint exception
|
||||
x86_64::instructions::int3();
|
||||
|
||||
match BREAKPOINT_HANDLER_CALLED.load(Ordering::SeqCst) {
|
||||
1 => serial_println!("ok"),
|
||||
0 => {
|
||||
serial_println!("failed");
|
||||
serial_println!("Breakpoint handler was not called.");
|
||||
}
|
||||
other => {
|
||||
serial_println!("failed");
|
||||
serial_println!("Breakpoint handler was called {} times", other);
|
||||
}
|
||||
}
|
||||
|
||||
unsafe {
|
||||
exit_qemu();
|
||||
}
|
||||
|
||||
loop {}
|
||||
}
|
||||
|
||||
/// This function is called on panic.
|
||||
#[cfg(not(test))]
|
||||
#[panic_implementation]
|
||||
#[no_mangle]
|
||||
pub fn panic(info: &PanicInfo) -> ! {
|
||||
serial_println!("failed");
|
||||
serial_println!("{}", info);
|
||||
|
||||
unsafe {
|
||||
exit_qemu();
|
||||
}
|
||||
|
||||
loop {}
|
||||
}
|
||||
|
||||
use x86_64::structures::idt::{ExceptionStackFrame, Idt};
|
||||
|
||||
lazy_static! {
|
||||
static ref IDT: Idt = {
|
||||
let mut idt = Idt::new();
|
||||
idt.breakpoint.set_handler_fn(breakpoint_handler);
|
||||
idt
|
||||
};
|
||||
}
|
||||
|
||||
pub fn init_idt() {
|
||||
IDT.load();
|
||||
}
|
||||
|
||||
extern "x86-interrupt" fn breakpoint_handler(_stack_frame: &mut ExceptionStackFrame) {
|
||||
BREAKPOINT_HANDLER_CALLED.fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
28
src/main.rs
28
src/main.rs
@@ -1,10 +1,14 @@
|
||||
#![feature(panic_implementation)] // required for defining the panic handler
|
||||
#![feature(abi_x86_interrupt)]
|
||||
#![no_std] // don't link the Rust standard library
|
||||
#![cfg_attr(not(test), no_main)] // disable all Rust-level entry points
|
||||
#![cfg_attr(test, allow(dead_code, unused_macros, unused_imports))]
|
||||
|
||||
#[macro_use]
|
||||
extern crate blog_os;
|
||||
extern crate x86_64;
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
|
||||
use core::panic::PanicInfo;
|
||||
|
||||
@@ -15,6 +19,12 @@ use core::panic::PanicInfo;
|
||||
pub extern "C" fn _start() -> ! {
|
||||
println!("Hello World{}", "!");
|
||||
|
||||
init_idt();
|
||||
|
||||
// invoke a breakpoint exception
|
||||
x86_64::instructions::int3();
|
||||
|
||||
println!("It did not crash!");
|
||||
loop {}
|
||||
}
|
||||
|
||||
@@ -26,3 +36,21 @@ pub fn panic(info: &PanicInfo) -> ! {
|
||||
println!("{}", info);
|
||||
loop {}
|
||||
}
|
||||
|
||||
use x86_64::structures::idt::{ExceptionStackFrame, Idt};
|
||||
|
||||
lazy_static! {
|
||||
static ref IDT: Idt = {
|
||||
let mut idt = Idt::new();
|
||||
idt.breakpoint.set_handler_fn(breakpoint_handler);
|
||||
idt
|
||||
};
|
||||
}
|
||||
|
||||
pub fn init_idt() {
|
||||
IDT.load();
|
||||
}
|
||||
|
||||
extern "x86-interrupt" fn breakpoint_handler(stack_frame: &mut ExceptionStackFrame) {
|
||||
println!("EXCEPTION: BREAKPOINT\n{:#?}", stack_frame);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user