Skip to content

Commit 0eff064

Browse files
committed
Debug: Disallow extra bits in Dr7Flags
Replace Dr7Value::flags_mut with explicit *_flags methods.
1 parent 6b1dd8c commit 0eff064

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

src/registers/debug.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use core::arch::asm;
66
use core::convert::TryFrom;
7-
use core::mem;
87
use core::ops::Range;
98

109
use crate::VirtAddr;
@@ -149,7 +148,6 @@ impl Dr6Flags {
149148

150149
bitflags! {
151150
/// Debug control flags of the [`Dr7`] register.
152-
// SAFETY: We choose to allow extra bits in `Dr7Flags` to be used for fields in `Dr7Value`.
153151
#[repr(transparent)]
154152
pub struct Dr7Flags: u64 {
155153
/// Breakpoint 0 is enabled for the current task.
@@ -370,18 +368,35 @@ impl Dr7Value {
370368
/// Returns the [`Dr7Flags`] in this value.
371369
#[inline]
372370
pub const fn flags(self) -> Dr7Flags {
373-
// SAFETY: The extra bits used in fields are valid in our representation of `Dr7Flags`.
374-
unsafe { Dr7Flags::from_bits_unchecked(self.bits) }
371+
Dr7Flags::from_bits_truncate(self.bits)
375372
}
376373

377-
/// Returns a mutable reference to the [`Dr7Flags`] in this value.
378-
///
379-
/// This can be used to modify the flags of this value in-place.
374+
/// Inserts the specified [`Dr7Flags`] in-place.
375+
#[inline]
376+
pub fn insert_flags(&mut self, flags: Dr7Flags) {
377+
self.bits |= flags.bits();
378+
}
379+
380+
/// Removes the specified [`Dr7Flags`] in-place.
381+
#[inline]
382+
pub fn remove_flags(&mut self, flags: Dr7Flags) {
383+
self.bits &= !flags.bits();
384+
}
385+
386+
/// Toggles the specified [`Dr7Flags`] in-place.
387+
#[inline]
388+
pub fn toggle_flags(&mut self, flags: Dr7Flags) {
389+
self.bits ^= flags.bits();
390+
}
391+
392+
/// Inserts or removes the specified [`Dr7Flags`] depending on the passed value.
380393
#[inline]
381-
pub fn flags_mut(&mut self) -> &mut Dr7Flags {
382-
// SAFETY: The extra bits used in fields are valid in our representation of `Dr7Flags`.
383-
// SAFETY: `Dr7Flags` has the same representation as `Dr7Value`.
384-
unsafe { mem::transmute(self) }
394+
pub fn set_flags(&mut self, flags: Dr7Flags, value: bool) {
395+
if value {
396+
self.insert_flags(flags);
397+
} else {
398+
self.remove_flags(flags);
399+
}
385400
}
386401

387402
/// Returns the condition field of a debug address register.

0 commit comments

Comments
 (0)