Skip to content
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

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

Merged
merged 11 commits into from
Jul 7, 2021
49 changes: 46 additions & 3 deletions src/structures/idt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
// except according to those terms.

//! Provides types for the Interrupt Descriptor Table and its entries.
//!
//! # For the builds without the `abi_x86_interrupt` feature
//! The following types are opaque and non-constructable instead of function pointers.
//!
//! - [`DivergingHandlerFunc`]
//! - [`DivergingHandlerFuncWithErrCode`]
//! - [`HandlerFunc`]
//! - [`HandlerFuncWithErrCode`]
//! - [`PageFaultHandlerFunc`]
//!
//! These types are defined for the compatibility with the Nightly Rust build.

use crate::{PrivilegeLevel, VirtAddr};
use bit_field::BitField;
Expand Down Expand Up @@ -593,17 +604,41 @@ 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);
#[cfg(not(feature = "abi_x86_interrupt"))]
#[derive(Copy, Clone, Debug)]
pub struct 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);
#[cfg(not(feature = "abi_x86_interrupt"))]
#[derive(Copy, Clone, Debug)]
pub struct 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);
#[cfg(not(feature = "abi_x86_interrupt"))]
#[derive(Copy, Clone, Debug)]
pub struct 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) -> !;
#[cfg(not(feature = "abi_x86_interrupt"))]
#[derive(Copy, Clone, Debug)]
pub struct 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) -> !;
#[cfg(not(feature = "abi_x86_interrupt"))]
#[derive(Copy, Clone, Debug)]
pub struct DivergingHandlerFuncWithErrCode(());

impl<F> Entry<F> {
/// Creates a non-present IDT entry (but sets the must-be-one bits).
Expand All @@ -627,11 +662,18 @@ 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 address of a valid interrupt handler function,
/// and the signature of such a function is correct for the entry type.
#[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 +694,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 +705,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