Skip to content

Commit ed49449

Browse files
authored
Merge pull request #219 from rust-osdev/unsafe-rflags-write
Make writing the RFLAGS register unsafe
2 parents aa5d781 + d4c01a0 commit ed49449

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- `interrupts::enable_interrupts_and_hlt`
1212
- **Breaking:** Make `DescriptorTablePointer::base` a `VirtAddr` ([#215](https://github.com/rust-osdev/x86_64/pull/215))
1313
- **Breaking:** Change return type of `read_rip` to `VirtAddr` ([#216](https://github.com/rust-osdev/x86_64/pull/216))
14+
- **Breaking:** Make writing the RFLAGS register unsafe ([#219](https://github.com/rust-osdev/x86_64/pull/219))
1415
- **Breaking:** Remove `PortReadWrite` trait, which is no longer needed ([#217](https://github.com/rust-osdev/x86_64/pull/217))
1516
- Relaxe `Sized` requirement for `FrameAllocator` in `Mapper::map_to` ([204](https://github.com/rust-osdev/x86_64/pull/204))
1617

src/registers/rflags.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,14 @@ mod x86_64 {
9292
}
9393

9494
/// Writes the RFLAGS register, preserves reserved bits.
95+
///
96+
/// ## Safety
97+
///
98+
/// Unsafe because undefined becavior can occur if certain flags are modified. For example,
99+
/// the `DF` flag must be unset in all Rust code. Also, modifying `CF`, `PF`, or any other
100+
/// flags also used by Rust/LLVM can result in undefined behavior too.
95101
#[inline]
96-
pub fn write(flags: RFlags) {
102+
pub unsafe fn write(flags: RFlags) {
97103
let old_value = read_raw();
98104
let reserved = old_value & !(RFlags::all().bits());
99105
let new_value = reserved | flags.bits();
@@ -104,16 +110,23 @@ mod x86_64 {
104110
/// Writes the RFLAGS register.
105111
///
106112
/// Does not preserve any bits, including reserved bits.
113+
///
114+
///
115+
/// ## Safety
116+
///
117+
/// Unsafe because undefined becavior can occur if certain flags are modified. For example,
118+
/// the `DF` flag must be unset in all Rust code. Also, modifying `CF`, `PF`, or any other
119+
/// flags also used by Rust/LLVM can result in undefined behavior too.
107120
#[inline]
108-
pub fn write_raw(val: u64) {
121+
pub unsafe fn write_raw(val: u64) {
109122
#[cfg(feature = "inline_asm")]
110-
unsafe {
123+
{
111124
// FIXME - There's probably a better way than saying we preserve the flags even though we actually don't
112125
asm!("push {}; popf", in(reg) val, options(preserves_flags))
113126
};
114127

115128
#[cfg(not(feature = "inline_asm"))]
116-
unsafe {
129+
{
117130
crate::asm::x86_64_asm_write_rflags(val)
118131
}
119132
}

0 commit comments

Comments
 (0)