Skip to content

Commit 22aeec3

Browse files
committed
Make Segment64::read_base safe
This function only throws a #UD, which we generally consider to be safe. Also, add an `Exceptions` section to the `Segment64` docs (this is similar to the `Panic` section in normal Rust docs). Signed-off-by: Joe Richey <[email protected]>
1 parent 6164bd0 commit 22aeec3

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/instructions/segmentation.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,22 @@ pub trait Segment {
3535
/// address can be set via the GDT, or by using the `FSGSBASE` instructions.
3636
pub trait Segment64: Segment {
3737
/// MSR containing the segment base. This MSR can be used to set the base
38-
/// when [`CR4.FSGSBASE`][Cr4Flags::FSGSBASE] is not set.
38+
/// when [`CR4.FSGSBASE`][Cr4Flags::FSGSBASE] is **not** set.
3939
const BASE: Msr;
4040
/// Reads the segment base address
4141
///
42-
/// ## Safety
42+
/// ## Exceptions
4343
///
4444
/// If [`CR4.FSGSBASE`][Cr4Flags::FSGSBASE] is not set, this instruction will throw a `#UD`.
45-
unsafe fn read_base() -> VirtAddr;
45+
fn read_base() -> VirtAddr;
4646
/// Writes the segment base address
4747
///
48-
/// ## Safety
48+
/// ## Exceptions
4949
///
5050
/// If [`CR4.FSGSBASE`][Cr4Flags::FSGSBASE] is not set, this instruction will throw a `#UD`.
5151
///
52+
/// ## Safety
53+
///
5254
/// The caller must ensure that this write operation has no unsafe side
5355
/// effects, as the segment base address might be in use.
5456
unsafe fn write_base(base: VirtAddr);
@@ -91,15 +93,17 @@ macro_rules! segment64_impl {
9193
($type:ty, $name:literal, $base:ty, $asm_rd:ident, $asm_wr:ident) => {
9294
impl Segment64 for $type {
9395
const BASE: Msr = <$base>::MSR;
94-
unsafe fn read_base() -> VirtAddr {
96+
fn read_base() -> VirtAddr {
9597
#[cfg(feature = "inline_asm")]
96-
{
98+
unsafe {
9799
let val: u64;
98100
asm!(concat!("rd", $name, "base {}"), out(reg) val, options(nomem, nostack, preserves_flags));
99101
VirtAddr::new_unsafe(val)
100102
}
101103
#[cfg(not(feature = "inline_asm"))]
102-
VirtAddr::new_unsafe(crate::asm::$asm_rd())
104+
unsafe {
105+
VirtAddr::new_unsafe(crate::asm::$asm_rd())
106+
}
103107
}
104108

105109
unsafe fn write_base(base: VirtAddr) {

0 commit comments

Comments
 (0)