Skip to content

Commit cd9d748

Browse files
authored
feat(instructions): define tables::sgdt (#279)
1 parent 5afb69b commit cd9d748

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/asm/asm.s

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ _x86_64_asm_lidt:
123123
lidt (%rdi)
124124
retq
125125

126+
.global _x86_64_asm_sgdt
127+
.p2align 4
128+
_x86_64_asm_sgdt:
129+
sgdt (%rdi)
130+
retq
131+
126132
.global _x86_64_asm_sidt
127133
.p2align 4
128134
_x86_64_asm_sidt:

src/asm/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ extern "sysv64" {
168168
)]
169169
pub(crate) fn x86_64_asm_lidt(idt: *const crate::instructions::tables::DescriptorTablePointer);
170170

171+
#[cfg_attr(
172+
any(target_env = "gnu", target_env = "musl"),
173+
link_name = "_x86_64_asm_sgdt"
174+
)]
175+
pub(crate) fn x86_64_asm_sgdt(gdt: *mut crate::instructions::tables::DescriptorTablePointer);
176+
171177
#[cfg_attr(
172178
any(target_env = "gnu", target_env = "musl"),
173179
link_name = "_x86_64_asm_sidt"

src/instructions/tables.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,23 @@ pub unsafe fn lidt(idt: &DescriptorTablePointer) {
4545
crate::asm::x86_64_asm_lidt(idt as *const _);
4646
}
4747

48+
/// Get the address of the current GDT.
49+
#[inline]
50+
pub fn sgdt() -> DescriptorTablePointer {
51+
let mut gdt: DescriptorTablePointer = DescriptorTablePointer {
52+
limit: 0,
53+
base: VirtAddr::new(0),
54+
};
55+
unsafe {
56+
#[cfg(feature = "inline_asm")]
57+
asm!("sgdt [{}]", in(reg) &mut gdt, options(nostack, preserves_flags));
58+
59+
#[cfg(not(feature = "inline_asm"))]
60+
crate::asm::x86_64_asm_sgdt(&mut gdt as *mut _);
61+
}
62+
gdt
63+
}
64+
4865
/// Get the address of the current IDT.
4966
#[inline]
5067
pub fn sidt() -> DescriptorTablePointer {

0 commit comments

Comments
 (0)