Skip to content

feat(idt): make it available in the stable Rust #271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 7, 2021
47 changes: 44 additions & 3 deletions src/structures/idt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,17 +593,51 @@ impl<T> PartialEq for Entry<T> {
}

/// A handler function for an interrupt or an exception without error code.
#[cfg(feature = "abi_x86_interrupt")]
pub type HandlerFunc = extern "x86-interrupt" fn(InterruptStackFrame);
/// This type is a dummy.
///
/// This type alias is not used for builds which do not use the `abi_x86_interrupt` feature.
#[cfg(not(feature = "abi_x86_interrupt"))]
pub type HandlerFunc = ();

/// A handler function for an exception that pushes an error code.
#[cfg(feature = "abi_x86_interrupt")]
pub type HandlerFuncWithErrCode = extern "x86-interrupt" fn(InterruptStackFrame, error_code: u64);
/// This type is a dummy.
///
/// This type alias is not used for builds which do not use the `abi_x86_interrupt` feature.
#[cfg(not(feature = "abi_x86_interrupt"))]
pub type HandlerFuncWithErrCode = ();

/// A page fault handler function that pushes a page fault error code.
#[cfg(feature = "abi_x86_interrupt")]
pub type PageFaultHandlerFunc =
extern "x86-interrupt" fn(InterruptStackFrame, error_code: PageFaultErrorCode);
/// This type is a dummy.
///
/// This type alias is not used for builds which do not use the `abi_x86_interrupt` feature.
#[cfg(not(feature = "abi_x86_interrupt"))]
pub type PageFaultHandlerFunc = ();

/// A handler function that must not return, e.g. for a machine check exception.
#[cfg(feature = "abi_x86_interrupt")]
pub type DivergingHandlerFunc = extern "x86-interrupt" fn(InterruptStackFrame) -> !;
/// This type is a dummy.
///
/// This type alias is not used for builds which do not use the `abi_x86_interrupt` feature.
#[cfg(not(feature = "abi_x86_interrupt"))]
pub type DivergingHandlerFunc = ();

/// A handler function with an error code that must not return, e.g. for a double fault exception.
#[cfg(feature = "abi_x86_interrupt")]
pub type DivergingHandlerFuncWithErrCode =
extern "x86-interrupt" fn(InterruptStackFrame, error_code: u64) -> !;
/// This type is a dummy.
///
/// This type alias is not used for builds which do not use the `abi_x86_interrupt` feature.
#[cfg(not(feature = "abi_x86_interrupt"))]
pub type DivergingHandlerFuncWithErrCode = ();

impl<F> Entry<F> {
/// Creates a non-present IDT entry (but sets the must-be-one bits).
Expand All @@ -627,11 +661,17 @@ impl<F> Entry<F> {
///
/// The function returns a mutable reference to the entry's options that allows
/// further customization.
///
/// # Safety
///
/// The caller must ensure that `addr` is the correct address to the handler function.
#[cfg(feature = "instructions")]
#[inline]
fn set_handler_addr(&mut self, addr: u64) -> &mut EntryOptions {
pub unsafe fn set_handler_addr(&mut self, addr: VirtAddr) -> &mut EntryOptions {
use crate::instructions::segmentation::{Segment, CS};

let addr = addr.as_u64();

self.pointer_low = addr as u16;
self.pointer_middle = (addr >> 16) as u16;
self.pointer_high = (addr >> 32) as u32;
Expand All @@ -652,7 +692,7 @@ impl<F> Entry<F> {

macro_rules! impl_set_handler_fn {
($h:ty) => {
#[cfg(feature = "instructions")]
#[cfg(all(feature = "instructions", feature = "abi_x86_interrupt"))]
impl Entry<$h> {
/// Set the handler function for the IDT entry and sets the present bit.
///
Expand All @@ -663,7 +703,8 @@ macro_rules! impl_set_handler_fn {
/// further customization.
#[inline]
pub fn set_handler_fn(&mut self, handler: $h) -> &mut EntryOptions {
self.set_handler_addr(handler as u64)
let handler = VirtAddr::new(handler as u64);
unsafe { self.set_handler_addr(handler) }
}
}
};
Expand Down
2 changes: 0 additions & 2 deletions src/structures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use crate::VirtAddr;

pub mod gdt;

// idt needs `feature(abi_x86_interrupt)`, which is not available on stable rust
#[cfg(feature = "abi_x86_interrupt")]
pub mod idt;

pub mod paging;
Expand Down