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
use crate::structures::gdt::SegmentSelector;
pub unsafe fn set_cs(sel: SegmentSelector) {
#[cfg(feature = "inline_asm")]
#[inline(always)]
unsafe fn inner(sel: SegmentSelector) {
asm!("pushq $0; \
leaq 1f(%rip), %rax; \
pushq %rax; \
lretq; \
1:" :: "ri" (u64::from(sel.0)) : "rax" "memory");
}
#[cfg(not(feature = "inline_asm"))]
#[inline(always)]
unsafe fn inner(sel: SegmentSelector) {
crate::asm::x86_64_asm_set_cs(u64::from(sel.0))
}
inner(sel)
}
#[inline]
pub unsafe fn load_ss(sel: SegmentSelector) {
#[cfg(feature = "inline_asm")]
asm!("movw $0, %ss " :: "r" (sel.0) : "memory");
#[cfg(not(feature = "inline_asm"))]
crate::asm::x86_64_asm_load_ss(sel.0);
}
#[inline]
pub unsafe fn load_ds(sel: SegmentSelector) {
#[cfg(feature = "inline_asm")]
asm!("movw $0, %ds " :: "r" (sel.0) : "memory");
#[cfg(not(feature = "inline_asm"))]
crate::asm::x86_64_asm_load_ds(sel.0);
}
#[inline]
pub unsafe fn load_es(sel: SegmentSelector) {
#[cfg(feature = "inline_asm")]
asm!("movw $0, %es " :: "r" (sel.0) : "memory");
#[cfg(not(feature = "inline_asm"))]
crate::asm::x86_64_asm_load_es(sel.0);
}
#[inline]
pub unsafe fn load_fs(sel: SegmentSelector) {
#[cfg(feature = "inline_asm")]
asm!("movw $0, %fs " :: "r" (sel.0) : "memory");
#[cfg(not(feature = "inline_asm"))]
crate::asm::x86_64_asm_load_fs(sel.0);
}
#[inline]
pub unsafe fn load_gs(sel: SegmentSelector) {
#[cfg(feature = "inline_asm")]
asm!("movw $0, %gs " :: "r" (sel.0) : "memory");
#[cfg(not(feature = "inline_asm"))]
crate::asm::x86_64_asm_load_gs(sel.0);
}
#[inline]
pub unsafe fn swap_gs() {
#[cfg(feature = "inline_asm")]
asm!("swapgs" ::: "memory" : "volatile");
#[cfg(not(feature = "inline_asm"))]
crate::asm::x86_64_asm_swapgs();
}
pub fn cs() -> SegmentSelector {
#[cfg(feature = "inline_asm")]
{
let segment: u16;
unsafe { asm!("mov %cs, $0" : "=r" (segment) ) };
SegmentSelector(segment)
}
#[cfg(not(feature = "inline_asm"))]
{
let segment: u16 = unsafe { crate::asm::x86_64_asm_get_cs() };
SegmentSelector(segment)
}
}