x86/
dtables.rs

1//! Functions and data-structures for working with descriptor tables.
2use crate::segmentation::SegmentSelector;
3use core::arch::asm;
4use core::fmt;
5use core::mem::size_of;
6
7/// A struct describing a pointer to a descriptor table (GDT / IDT).
8/// This is in a format suitable for giving to 'lgdt' or 'lidt'.
9#[repr(C, packed)]
10pub struct DescriptorTablePointer<Entry> {
11    /// Size of the DT.
12    pub limit: u16,
13    /// Pointer to the memory region containing the DT.
14    pub base: *const Entry,
15}
16
17impl<T> Default for DescriptorTablePointer<T> {
18    fn default() -> DescriptorTablePointer<T> {
19        DescriptorTablePointer {
20            limit: 0,
21            base: core::ptr::null(),
22        }
23    }
24}
25
26impl<T> DescriptorTablePointer<T> {
27    pub fn new(tbl: &T) -> Self {
28        // GDT, LDT, and IDT all expect the limit to be set to "one less".
29        // See Intel 3a, Section 3.5.1 "Segment Descriptor Tables" and
30        // Section 6.10 "Interrupt Descriptor Table (IDT)".
31        let len = size_of::<T>() - 1;
32        assert!(len < 0x10000);
33        DescriptorTablePointer {
34            base: tbl as *const T,
35            limit: len as u16,
36        }
37    }
38
39    pub fn new_from_slice(slice: &[T]) -> Self {
40        // GDT, LDT, and IDT all expect the limit to be set to "one less".
41        // See Intel 3a, Section 3.5.1 "Segment Descriptor Tables" and
42        // Section 6.10 "Interrupt Descriptor Table (IDT)".
43        let len = slice.len() * size_of::<T>() - 1;
44        assert!(len < 0x10000);
45        DescriptorTablePointer {
46            base: slice.as_ptr(),
47            limit: len as u16,
48        }
49    }
50}
51
52impl<T> fmt::Debug for DescriptorTablePointer<T> {
53    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
54        write!(f, "DescriptorTablePointer ({} {:?})", { self.limit }, {
55            self.base
56        })
57    }
58}
59
60/// Load the GDTR register with the specified base and limit.
61///
62/// # Safety
63/// Needs CPL 0.
64pub unsafe fn lgdt<T>(gdt: &DescriptorTablePointer<T>) {
65    asm!("lgdt ({0})", in(reg) gdt, options(att_syntax));
66}
67
68/// Retrieve base and limit from the GDTR register.
69///
70/// # Safety
71/// Needs CPL 0.
72pub unsafe fn sgdt<T>(idt: &mut DescriptorTablePointer<T>) {
73    asm!("sgdt ({0})", in(reg) idt as *mut DescriptorTablePointer<T>, options(att_syntax));
74}
75
76/// Loads the segment selector into the selector field of the local
77/// descriptor table register (LDTR).
78///
79/// After the segment selector is loaded in the LDTR,
80/// the processor uses the segment selector to locate
81/// the segment descriptor for the LDT in the global
82/// descriptor table (GDT).
83///
84/// # Safety
85/// Needs CPL 0.
86pub unsafe fn load_ldtr(selector: SegmentSelector) {
87    asm!("lldt {0:x}", in(reg) selector.bits(), options(att_syntax));
88}
89
90/// Returns the segment selector from the local descriptor table register (LDTR).
91///
92/// The returned segment selector points to the segment descriptor
93/// (located in the GDT) for the current LDT.
94///
95/// # Safety
96/// Needs CPL 0.
97pub unsafe fn ldtr() -> SegmentSelector {
98    let selector: u16;
99    asm!("sldt {0:x}", out(reg) selector, options(att_syntax));
100    SegmentSelector::from_raw(selector)
101}
102
103/// Load the IDTR register with the specified base and limit.
104///
105/// # Safety
106/// Needs CPL 0.
107pub unsafe fn lidt<T>(idt: &DescriptorTablePointer<T>) {
108    asm!("lidt ({0})", in(reg) idt, options(att_syntax));
109}
110
111/// Retrieve base and limit from the IDTR register.
112///
113/// # Safety
114/// Needs CPL 0.
115pub unsafe fn sidt<T>(idt: &mut DescriptorTablePointer<T>) {
116    asm!("sidt ({0})", in(reg) idt as *mut DescriptorTablePointer<T>, options(att_syntax));
117}
118
119#[cfg(all(test, feature = "utest"))]
120mod test {
121    use super::*;
122
123    #[test]
124    fn check_sgdt() {
125        let mut gdtr: super::DescriptorTablePointer<u64> = Default::default();
126        gdtr.limit = 0xdead;
127        gdtr.base = 0xbadc0de as *mut u64;
128        unsafe {
129            sgdt(&mut gdtr);
130        }
131        let base = gdtr.base;
132        let limit = gdtr.limit;
133        assert_ne!(base, core::ptr::null_mut());
134        assert_ne!(limit, 0xdead);
135        assert_ne!(base as u64, 0xbadc0de);
136    }
137
138    #[test]
139    fn check_sidt() {
140        let mut gdtr: super::DescriptorTablePointer<u64> = Default::default();
141        gdtr.limit = 0xdead;
142        gdtr.base = 0xbadc0de as *mut u64;
143        unsafe {
144            sidt(&mut gdtr);
145        }
146        let base = gdtr.base;
147        let limit = gdtr.limit;
148        assert_ne!(base, core::ptr::null_mut());
149        assert_ne!(limit, 0xdead);
150        assert_ne!(base as u64, 0xbadc0de);
151    }
152}