Skip to content

Commit

Permalink
Macros for fs/gs deref.
Browse files Browse the repository at this point in the history
  • Loading branch information
gz committed Jun 4, 2022
1 parent 10092fd commit d8cbc9e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [unreleased]

## [0.49.0] - 2022-06-03

- Removed `bits64::segmentation::fs_deref()`: Users should replace calls to
`fs_deref` with the more general `fbits64::segmentation::s_deref!` macro.
`fs_deref!(0)` is equivalent to `fs_deref()`.
- Removed `bits64::segmentation::gs_deref()`: Users should replace calls to
`gs_deref` with the more general `bits64::segmentation::gs_deref!` macro.
`fs_deref!(0)` is equivalent to `fs_deref()`.

## [0.48.0] - 2022-05-23

- Added `const new` constructor for X2APIC struct
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "x86"
version = "0.48.0"
version = "0.49.0"
authors = [
"Gerd Zellweger <[email protected]>",
"Eric Kidd <[email protected]>",
Expand Down
46 changes: 34 additions & 12 deletions src/bits64/segmentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,28 +191,50 @@ pub unsafe fn rdfsbase() -> u64 {
fs_base
}

/// "Dereferences" the fs register at offset 0.
/// "Dereferences" the fs register at `offset`.
///
/// # Safety
/// fs needs to point to valid address.
/// - Offset needs to be within valid memory relative to what the fs register
/// points to.
#[cfg(target_arch = "x86_64")]
pub unsafe fn fs_deref() -> u64 {
let fs: u64;
asm!("movq %fs:0x0, {0}", out(reg) fs, options(att_syntax));
fs
#[macro_export]
macro_rules! fs_deref {
($offset:expr) => {{
let fs: u64;
core::arch::asm!("movq %fs:{offset}, {result}",
offset = const ($offset),
result = out(reg) fs,
options(att_syntax)
);
fs
}};
}

/// "Dereferences" the gs register at offset 0.
#[cfg(target_arch = "x86_64")]
pub use fs_deref;

/// "Dereferences" the gs register at `offset`.
///
/// # Safety
/// gs needs to point to valid address.
/// - Offset needs to be within valid memory relative to what the gs register
/// points to.
#[cfg(target_arch = "x86_64")]
pub unsafe fn gs_deref() -> u64 {
let gs: u64;
asm!("movq %gs:0x0, {0}", out(reg) gs, options(att_syntax));
gs
#[macro_export]
macro_rules! gs_deref {
($offset:expr) => {{
let gs: u64;
core::arch::asm!("movq %gs:{offset}, {result}",
offset = const ($offset),
result = out(reg) gs,
options(att_syntax)
);
gs
}};
}

#[cfg(target_arch = "x86_64")]
pub use gs_deref;

/// Swap the GS register.
///
/// Exchanges the current GS base register value with the value contained
Expand Down

0 comments on commit d8cbc9e

Please sign in to comment.