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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
pub use super::model_specific::{Efer, EferFlags};
use bitflags::bitflags;
#[derive(Debug)]
pub struct Cr0;
bitflags! {
pub struct Cr0Flags: u64 {
const PROTECTED_MODE_ENABLE = 1;
const MONITOR_COPROCESSOR = 1 << 1;
const EMULATE_COPROCESSOR = 1 << 2;
const TASK_SWITCHED = 1 << 3;
const NUMERIC_ERROR = 1 << 5;
const WRITE_PROTECT = 1 << 16;
const ALIGNMENT_MASK = 1 << 18;
const NOT_WRITE_THROUGH = 1 << 29;
const CACHE_DISABLE = 1 << 30;
const PAGING = 1 << 31;
}
}
#[derive(Debug)]
pub struct Cr2;
#[derive(Debug)]
pub struct Cr3;
bitflags! {
pub struct Cr3Flags: u64 {
const PAGE_LEVEL_WRITETHROUGH = 1 << 3;
const PAGE_LEVEL_CACHE_DISABLE = 1 << 4;
}
}
#[derive(Debug)]
pub struct Cr4;
bitflags! {
pub struct Cr4Flags: u64 {
const VIRTUAL_8086_MODE_EXTENSIONS = 1;
const PROTECTED_MODE_VIRTUAL_INTERRUPTS = 1 << 1;
const TIMESTAMP_DISABLE = 1 << 2;
const DEBUGGING_EXTENSIONS = 1 << 3;
const PAGE_SIZE_EXTENSION = 1 << 4;
const PHYSICAL_ADDRESS_EXTENSION = 1 << 5;
const MACHINE_CHECK_EXCEPTION = 1 << 6;
const PAGE_GLOBAL = 1 << 7;
const PERFORMANCE_MONITOR_COUNTER = 1 << 8;
const OSFXSR = 1 << 9;
const OSXMMEXCPT_ENABLE = 1 << 10;
const USER_MODE_INSTRUCTION_PREVENTION = 1 << 11;
const L5_PAGING = 1 << 12;
const VIRTUAL_MACHINE_EXTENSIONS = 1 << 13;
const SAFER_MODE_EXTENSIONS = 1 << 14;
const FSGSBASE = 1 << 16;
const PCID = 1 << 17;
const OSXSAVE = 1 << 18;
const SUPERVISOR_MODE_EXECUTION_PROTECTION = 1 << 20;
const SUPERVISOR_MODE_ACCESS_PREVENTION = 1 << 21;
const PROTECTION_KEY = 1 << 22;
}
}
#[cfg(feature = "instructions")]
mod x86_64 {
use super::*;
use crate::structures::paging::PhysFrame;
use crate::{PhysAddr, VirtAddr};
impl Cr0 {
#[inline]
pub fn read() -> Cr0Flags {
Cr0Flags::from_bits_truncate(Self::read_raw())
}
#[inline]
pub fn read_raw() -> u64 {
let value: u64;
#[cfg(feature = "inline_asm")]
unsafe {
asm!("mov {}, cr0", out(reg) value, options(nomem));
}
#[cfg(not(feature = "inline_asm"))]
unsafe {
value = crate::asm::x86_64_asm_read_cr0();
}
value
}
#[inline]
pub unsafe fn write(flags: Cr0Flags) {
let old_value = Self::read_raw();
let reserved = old_value & !(Cr0Flags::all().bits());
let new_value = reserved | flags.bits();
Self::write_raw(new_value);
}
#[inline]
pub unsafe fn write_raw(value: u64) {
#[cfg(feature = "inline_asm")]
asm!("mov cr0, {}", in(reg) value, options(nostack));
#[cfg(not(feature = "inline_asm"))]
crate::asm::x86_64_asm_write_cr0(value);
}
#[inline]
pub unsafe fn update<F>(f: F)
where
F: FnOnce(&mut Cr0Flags),
{
let mut flags = Self::read();
f(&mut flags);
Self::write(flags);
}
}
impl Cr2 {
#[inline]
pub fn read() -> VirtAddr {
let value: u64;
#[cfg(feature = "inline_asm")]
unsafe {
asm!("mov {}, cr2", out(reg) value, options(nomem));
}
#[cfg(not(feature = "inline_asm"))]
unsafe {
value = crate::asm::x86_64_asm_read_cr2();
}
VirtAddr::new(value)
}
}
impl Cr3 {
#[inline]
pub fn read() -> (PhysFrame, Cr3Flags) {
let value: u64;
#[cfg(feature = "inline_asm")]
unsafe {
asm!("mov {}, cr3", out(reg) value, options(nomem));
}
#[cfg(not(feature = "inline_asm"))]
unsafe {
value = crate::asm::x86_64_asm_read_cr3();
}
let flags = Cr3Flags::from_bits_truncate(value);
let addr = PhysAddr::new(value & 0x_000f_ffff_ffff_f000);
let frame = PhysFrame::containing_address(addr);
(frame, flags)
}
#[inline]
pub unsafe fn write(frame: PhysFrame, flags: Cr3Flags) {
let addr = frame.start_address();
let value = addr.as_u64() | flags.bits();
#[cfg(feature = "inline_asm")]
asm!("mov cr3, {}", in(reg) value, options(nostack));
#[cfg(not(feature = "inline_asm"))]
crate::asm::x86_64_asm_write_cr3(value)
}
}
impl Cr4 {
#[inline]
pub fn read() -> Cr4Flags {
Cr4Flags::from_bits_truncate(Self::read_raw())
}
#[inline]
pub fn read_raw() -> u64 {
let value: u64;
#[cfg(feature = "inline_asm")]
unsafe {
asm!("mov {}, cr4", out(reg) value, options(nostack));
}
#[cfg(not(feature = "inline_asm"))]
unsafe {
value = crate::asm::x86_64_asm_read_cr4();
}
value
}
#[inline]
pub unsafe fn write(flags: Cr4Flags) {
let old_value = Self::read_raw();
let reserved = old_value & !(Cr4Flags::all().bits());
let new_value = reserved | flags.bits();
Self::write_raw(new_value);
}
#[inline]
pub unsafe fn write_raw(value: u64) {
#[cfg(feature = "inline_asm")]
asm!("mov cr4, {}", in(reg) value, options(nostack));
#[cfg(not(feature = "inline_asm"))]
crate::asm::x86_64_asm_write_cr4(value);
}
#[inline]
pub unsafe fn update<F>(f: F)
where
F: FnOnce(&mut Cr4Flags),
{
let mut flags = Self::read();
f(&mut flags);
Self::write(flags);
}
}
}