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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use core::arch::asm;
use super::interrupt::{
AbortDoubleFault, AbortMachineCheck, Handler, HandlerPageFault,
HandlerWithSegmentSelectorErrorCode, InterruptGateDescriptor,
};
pub struct LocalDescriptorTable;
impl LocalDescriptorTable {
#[inline(always)]
pub fn kill() {
unsafe {
asm!("lldt ax", in("eax") 0_u16, options(nostack));
}
}
}
impl DescriptorTable for LocalDescriptorTable {
#[inline(always)]
fn load<T>(table: SystemTableRegister<T>) {
unsafe {
asm!("lldt [{0}]", in(reg) &table, options(nostack));
}
}
}
#[doc(hidden)]
pub trait DescriptorTable {
fn load<T>(table: SystemTableRegister<T>);
}
pub struct GlobalDescriptorTable;
impl DescriptorTable for GlobalDescriptorTable {
#[inline(always)]
fn load<T>(table: SystemTableRegister<T>) {
unsafe {
asm!("lgdt [{0}]", in(reg) &table, options(nostack));
}
}
}
#[repr(C)]
pub struct InterruptDescriptorTable {
pub divide_error: InterruptGateDescriptor<Handler>,
pub debug: InterruptGateDescriptor<Handler>,
pub nmi: InterruptGateDescriptor<Handler>,
pub breakpoint: InterruptGateDescriptor<Handler>,
pub overflow_exception: InterruptGateDescriptor<Handler>,
pub bound_range_exceeded: InterruptGateDescriptor<Handler>,
pub invalid_opcode: InterruptGateDescriptor<Handler>,
pub device_not_available: InterruptGateDescriptor<Handler>,
pub double_fault: InterruptGateDescriptor<AbortDoubleFault>,
pub coprocessor_segment_overrun: InterruptGateDescriptor<Handler>,
pub invalid_tss: InterruptGateDescriptor<HandlerWithSegmentSelectorErrorCode>,
pub segment_not_present: InterruptGateDescriptor<HandlerWithSegmentSelectorErrorCode>,
pub stack_fault: InterruptGateDescriptor<HandlerWithSegmentSelectorErrorCode>,
pub general_protection: InterruptGateDescriptor<HandlerWithSegmentSelectorErrorCode>,
pub page_fault: InterruptGateDescriptor<HandlerPageFault>,
_reserved0: InterruptGateDescriptor<core::convert::Infallible>,
pub x87_fpu_floating_point_error: InterruptGateDescriptor<Handler>,
pub alignment_check_exception: InterruptGateDescriptor<Handler>,
pub machine_check_exception: InterruptGateDescriptor<AbortMachineCheck>,
pub simd_floating_point_exception: InterruptGateDescriptor<Handler>,
pub virtualization_exception: InterruptGateDescriptor<Handler>,
_reserved1: InterruptGateDescriptor<core::convert::Infallible>,
_reserved2: InterruptGateDescriptor<core::convert::Infallible>,
_reserved3: InterruptGateDescriptor<core::convert::Infallible>,
_reserved4: InterruptGateDescriptor<core::convert::Infallible>,
_reserved5: InterruptGateDescriptor<core::convert::Infallible>,
_reserved6: InterruptGateDescriptor<core::convert::Infallible>,
_reserved7: InterruptGateDescriptor<core::convert::Infallible>,
_reserved8: InterruptGateDescriptor<core::convert::Infallible>,
_reserved9: InterruptGateDescriptor<core::convert::Infallible>,
_reserved10: InterruptGateDescriptor<core::convert::Infallible>,
_reserved11: InterruptGateDescriptor<core::convert::Infallible>,
pub user_defined: [InterruptGateDescriptor<Handler>; 224],
}
impl DescriptorTable for InterruptDescriptorTable {
#[inline(always)]
fn load<T>(table: SystemTableRegister<T>) {
unsafe {
asm!("lidt [{0}]", in(reg) &table, options(nostack));
}
}
}
impl InterruptDescriptorTable {
pub const fn empty() -> Self {
Self {
divide_error: InterruptGateDescriptor::empty(),
debug: InterruptGateDescriptor::empty(),
nmi: InterruptGateDescriptor::empty(),
breakpoint: InterruptGateDescriptor::empty(),
overflow_exception: InterruptGateDescriptor::empty(),
bound_range_exceeded: InterruptGateDescriptor::empty(),
invalid_opcode: InterruptGateDescriptor::empty(),
device_not_available: InterruptGateDescriptor::empty(),
double_fault: InterruptGateDescriptor::empty(),
coprocessor_segment_overrun: InterruptGateDescriptor::empty(),
invalid_tss: InterruptGateDescriptor::empty(),
segment_not_present: InterruptGateDescriptor::empty(),
stack_fault: InterruptGateDescriptor::empty(),
general_protection: InterruptGateDescriptor::empty(),
page_fault: InterruptGateDescriptor::empty(),
_reserved0: InterruptGateDescriptor::empty(),
x87_fpu_floating_point_error: InterruptGateDescriptor::empty(),
alignment_check_exception: InterruptGateDescriptor::empty(),
machine_check_exception: InterruptGateDescriptor::empty(),
simd_floating_point_exception: InterruptGateDescriptor::empty(),
virtualization_exception: InterruptGateDescriptor::empty(),
_reserved1: InterruptGateDescriptor::empty(),
_reserved2: InterruptGateDescriptor::empty(),
_reserved3: InterruptGateDescriptor::empty(),
_reserved4: InterruptGateDescriptor::empty(),
_reserved5: InterruptGateDescriptor::empty(),
_reserved6: InterruptGateDescriptor::empty(),
_reserved7: InterruptGateDescriptor::empty(),
_reserved8: InterruptGateDescriptor::empty(),
_reserved9: InterruptGateDescriptor::empty(),
_reserved10: InterruptGateDescriptor::empty(),
_reserved11: InterruptGateDescriptor::empty(),
user_defined: [InterruptGateDescriptor::empty(); 224],
}
}
pub fn load(&'static self) {
SystemTableRegister::new(self).load::<Self>();
}
}
#[repr(C, packed)]
pub struct SystemTableRegister<T> {
pub size: u16,
pub address: u64,
_ty: core::marker::PhantomData<T>,
}
impl<T> SystemTableRegister<T> {
#[inline]
pub fn new(t: &'static T) -> Self {
SystemTableRegister {
size: (core::mem::size_of::<T>() as u16) - 1,
address: t as *const T as usize as u64,
_ty: core::marker::PhantomData,
}
}
#[inline]
pub fn load<V>(self)
where
V: DescriptorTable,
{
V::load(self)
}
}