use core::fmt;
use core::mem::size_of;
#[repr(C, packed)]
pub struct DescriptorTablePointer<Entry> {
pub limit: u16,
pub base: *const Entry,
}
impl<T> DescriptorTablePointer<T> {
pub fn new(tbl: &T) -> Self {
let len = size_of::<T>() - 1;
assert!(len < 0x10000);
DescriptorTablePointer {
base: tbl as *const T,
limit: len as u16,
}
}
pub fn new_from_slice(slice: &[T]) -> Self {
let len = slice.len() * size_of::<T>() - 1;
assert!(len < 0x10000);
DescriptorTablePointer {
base: slice.as_ptr(),
limit: len as u16,
}
}
}
impl<T> fmt::Debug for DescriptorTablePointer<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
unsafe { write!(f, "DescriptorTablePointer ({} {:?})", self.limit, self.base) }
}
}
pub unsafe fn lgdt<T>(gdt: &DescriptorTablePointer<T>) {
asm!("lgdt ($0)" :: "r" (gdt) : "memory");
}
pub unsafe fn lldt<T>(ldt: &DescriptorTablePointer<T>) {
asm!("lldt ($0)" :: "r" (ldt) : "memory");
}
pub unsafe fn lidt<T>(idt: &DescriptorTablePointer<T>) {
asm!("lidt ($0)" :: "r" (idt) : "memory");
}