Skip to content

feat(idt): add unsafe const set_handler_{fn|addr}_unchecked methods #225

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#![cfg_attr(feature = "const_fn", feature(const_mut_refs))]
#![cfg_attr(feature = "const_fn", feature(const_fn_fn_ptr_basics))]
#![cfg_attr(feature = "const_fn", feature(const_in_array_repeat_expressions))]
#![cfg_attr(feature = "const_fn", feature(const_raw_ptr_to_usize_cast))]
#![cfg_attr(feature = "inline_asm", feature(asm))]
#![cfg_attr(feature = "abi_x86_interrupt", feature(abi_x86_interrupt))]
#![warn(missing_docs)]
Expand Down
67 changes: 55 additions & 12 deletions src/structures/idt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

//! Provides types for the Interrupt Descriptor Table and its entries.

use crate::{structures::DescriptorTablePointer, PrivilegeLevel, VirtAddr};
use crate::{
structures::{gdt::SegmentSelector, DescriptorTablePointer},
PrivilegeLevel, VirtAddr,
};
use bit_field::BitField;
use bitflags::bitflags;
use core::fmt;
Expand Down Expand Up @@ -611,15 +614,27 @@ impl<F> Entry<F> {
#[inline]
fn set_handler_addr(&mut self, addr: u64) -> &mut EntryOptions {
use crate::instructions::segmentation;
// SAFETY: CS segment is loaded directly from the register, so it's valid.
unsafe { self.set_handler_addr_with_segment(addr, segmentation::cs()) }
}

const_fn! {
#[inline]
unsafe fn set_handler_addr_with_segment(
&mut self,
addr: u64,
cs_segment: SegmentSelector,
) -> &mut EntryOptions {
self.pointer_low = addr as u16;
self.pointer_middle = (addr >> 16) as u16;
self.pointer_high = (addr >> 32) as u32;

self.pointer_low = addr as u16;
self.pointer_middle = (addr >> 16) as u16;
self.pointer_high = (addr >> 32) as u32;
self.gdt_selector = cs_segment.0;

self.gdt_selector = segmentation::cs().0;
self.options.set_present(true);

self.options.set_present(true);
&mut self.options
&mut self.options
}
}
}

Expand All @@ -638,6 +653,27 @@ macro_rules! impl_set_handler_fn {
pub fn set_handler_fn(&mut self, handler: $h) -> &mut EntryOptions {
self.set_handler_addr(handler as u64)
}

const_fn! {
/// Set the handler function for the IDT entry and sets the present bit.
///
/// # Safety
///
/// For the code selector field, this function uses the code segment selector that
/// is passed in through the arguments, the user must take care to ensure that it is
/// valid.
///
/// The function returns a mutable reference to the entry's options that allows
/// further customization.
#[inline]
pub unsafe fn set_handler_fn_with_segment(
&mut self,
handler: $h,
cs_segment: SegmentSelector,
) -> &mut EntryOptions {
self.set_handler_addr_with_segment(handler as u64, cs_segment)
}
}
}
};
}
Expand All @@ -660,11 +696,18 @@ impl EntryOptions {
EntryOptions(0b1110_0000_0000)
}

/// Set or reset the preset bit.
#[inline]
pub fn set_present(&mut self, present: bool) -> &mut Self {
self.0.set_bit(15, present);
self
const_fn! {
/// Set or reset the preset bit.
#[inline]
pub fn set_present(&mut self, present: bool) -> &mut Self {
if present {
self.0 |= 1 << 15;
} else {
self.0 &= !(1 << 15);
}

self
}
}

/// Let the CPU disable hardware interrupts when the handler is invoked. By default,
Expand Down