1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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");
}