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
201
202
203
204
205
206
207
use core::fmt;
bitflags! {
flags SegmentSelector: u16 {
const RPL_0 = 0b00,
const RPL_1 = 0b01,
const RPL_2 = 0b10,
const RPL_3 = 0b11,
const TI_GDT = 0 << 3,
const TI_LDT = 1 << 3,
}
}
impl SegmentSelector {
pub fn new(index: u16) -> SegmentSelector {
SegmentSelector { bits: index << 3 }
}
pub fn from_raw(bits: u16) -> SegmentSelector {
SegmentSelector { bits: bits }
}
}
impl fmt::Debug for SegmentSelector {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let r0 = match self.contains(RPL_0) {
false => "",
true => "Ring 0 segment selector.",
};
let r1 = match self.contains(RPL_1) {
false => "",
true => "Ring 1 segment selector.",
};
let r2 = match self.contains(RPL_2) {
false => "",
true => "Ring 2 segment selector.",
};
let r3 = match self.contains(RPL_3) {
false => "",
true => "Ring 3 segment selector.",
};
let tbl = match self.contains(TI_LDT) {
false => "GDT Table",
true => "LDT Table",
};
write!(f,
"Index {} in {}, {}{}{}{}",
self.bits >> 3,
tbl,
r0,
r1,
r2,
r3)
}
}
bitflags! {
flags SegmentDescriptor: u64 {
const DESC_S = 1 << (32+12),
const DESC_DPL0 = 0b00 << (32+13),
const DESC_DPL1 = 0b01 << (32+13),
const DESC_DPL2 = 0b10 << (32+13),
const DESC_DPL3 = 0b11 << (32+13),
const DESC_P = 1 << (32+15),
const DESC_AVL = 1 << (32+20),
const DESC_L = 1 << (32+21),
const DESC_DB = 1 << (32+22),
const DESC_G = 1 << (32+23),
const TYPE_SYS_LDT = 0b0010 << (32+8),
const TYPE_SYS_TSS_AVAILABLE = 0b1001 << (32+8),
const TYPE_SYS_TSS_BUSY = 0b1011 << (32+8),
const TYPE_SYS_CALL_GATE = 0b1100 << (32+8),
const TYPE_SYS_INTERRUPT_GATE = 0b1110 << (32+8),
const TYPE_SYS_TRAP_GATE = 0b1111 << (32+8),
const TYPE_D_RO = 0b0000 << (32+8),
const TYPE_D_ROA = 0b0001 << (32+8),
const TYPE_D_RW = 0b0010 << (32+8),
const TYPE_D_RWA = 0b0011 << (32+8),
const TYPE_D_ROEXD = 0b0100 << (32+8),
const TYPE_D_ROEXDA = 0b0101 << (32+8),
const TYPE_D_RWEXD = 0b0110 << (32+8),
const TYPE_D_RWEXDA = 0b0111 << (32+8),
const TYPE_C_EO = 0b1000 << (32+8),
const TYPE_C_EOA = 0b1001 << (32+8),
const TYPE_C_ER = 0b1010 << (32+8),
const TYPE_C_ERA = 0b1011 << (32+8),
const TYPE_C_EOC = 0b1100 << (32+8),
const TYPE_C_EOCA = 0b1101 << (32+8),
const TYPE_C_ERC = 0b1110 << (32+8),
const TYPE_C_ERCA = 0b1111 << (32+8),
}
}
impl SegmentDescriptor {
pub fn new(base: u32, limit: u32) -> SegmentDescriptor {
let base_low: u64 = base as u64 & 0xffffff;
let base_high: u64 = (base as u64 >> 24) & 0xff;
let limit_low: u64 = limit as u64 & 0xffff;
let limit_high: u64 = (limit as u64 & (0b1111 << 16)) >> 16;
SegmentDescriptor {
bits: limit_low | base_low << 16 | limit_high << (32 + 16) | base_high << (32 + 24),
}
}
}
impl fmt::Debug for SegmentDescriptor {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "SD: 0x{:x}", self.bits)
}
}
pub unsafe fn load_ss(sel: SegmentSelector) {
asm!("movw $0, %ss " :: "r" (sel) : "memory");
}
pub unsafe fn load_ds(sel: SegmentSelector) {
asm!("movw $0, %ds " :: "r" (sel) : "memory");
}
pub unsafe fn load_es(sel: SegmentSelector) {
asm!("movw $0, %es " :: "r" (sel) : "memory");
}
pub unsafe fn load_fs(sel: SegmentSelector) {
asm!("movw $0, %fs " :: "r" (sel) : "memory");
}
pub unsafe fn load_gs(sel: SegmentSelector) {
asm!("movw $0, %gs " :: "r" (sel) : "memory");
}
pub unsafe fn load_cs(sel: SegmentSelector) {
asm!("pushq $0
lea 1f(%rip), %rax
pushq %rax
lretq
1:" :: "r" (sel.bits() as u64) : "rax" "memory");
}