Skip to content

Commit 805333e

Browse files
committed
debug: Add DebugAddressRegisterNumber
1 parent 6df8e8d commit 805333e

File tree

1 file changed

+89
-94
lines changed

1 file changed

+89
-94
lines changed

src/registers/debug.rs

Lines changed: 89 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,44 @@ debug_address_register_impl!(Dr1, "DR1");
4949
debug_address_register_impl!(Dr2, "DR2");
5050
debug_address_register_impl!(Dr3, "DR3");
5151

52+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
53+
#[repr(transparent)]
54+
pub struct DebugAddressRegisterNumber(u8);
55+
56+
impl DebugAddressRegisterNumber {
57+
pub const unsafe fn new_unchecked(n: u8) -> Self {
58+
Self(n)
59+
}
60+
61+
pub const fn new(n: u8) -> Option<Self> {
62+
match n {
63+
0..=3 => Some(Self(n as u8)),
64+
_ => None,
65+
}
66+
}
67+
68+
pub const fn get(self) -> u8 {
69+
self.0
70+
}
71+
}
72+
73+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
74+
pub struct TryFromIntError(());
75+
76+
impl From<core::num::TryFromIntError> for TryFromIntError {
77+
fn from(_: core::num::TryFromIntError) -> Self {
78+
Self(())
79+
}
80+
}
81+
82+
impl TryFrom<u8> for DebugAddressRegisterNumber {
83+
type Error = TryFromIntError;
84+
85+
fn try_from(n: u8) -> Result<Self, Self::Error> {
86+
Self::new(n).ok_or(TryFromIntError(()))
87+
}
88+
}
89+
5290
/// Debug Status Register (DR6).
5391
///
5492
/// Reports debug conditions from the last debug exception.
@@ -96,6 +134,18 @@ bitflags! {
96134
}
97135
}
98136

137+
impl Dr6Flags {
138+
pub const fn trap(n: DebugAddressRegisterNumber) -> Self {
139+
match n.0 {
140+
0 => Self::TRAP0,
141+
1 => Self::TRAP1,
142+
2 => Self::TRAP2,
143+
3 => Self::TRAP3,
144+
_ => unreachable!(),
145+
}
146+
}
147+
}
148+
99149
impl Dr6 {
100150
/// Read the current set of DR6 flags.
101151
#[inline]
@@ -143,6 +193,28 @@ bitflags! {
143193
}
144194
}
145195

196+
impl Dr7Flags {
197+
pub const fn local_breakpoint_enable(n: DebugAddressRegisterNumber) -> Self {
198+
match n.0 {
199+
0 => Self::LOCAL_BREAKPOINT_0_ENABLE,
200+
1 => Self::LOCAL_BREAKPOINT_1_ENABLE,
201+
2 => Self::LOCAL_BREAKPOINT_2_ENABLE,
202+
3 => Self::LOCAL_BREAKPOINT_3_ENABLE,
203+
_ => unreachable!(),
204+
}
205+
}
206+
207+
pub const fn global_breakpoint_enable(n: DebugAddressRegisterNumber) -> Self {
208+
match n.0 {
209+
0 => Self::GLOBAL_BREAKPOINT_0_ENABLE,
210+
1 => Self::GLOBAL_BREAKPOINT_1_ENABLE,
211+
2 => Self::GLOBAL_BREAKPOINT_2_ENABLE,
212+
3 => Self::GLOBAL_BREAKPOINT_3_ENABLE,
213+
_ => unreachable!(),
214+
}
215+
}
216+
}
217+
146218
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
147219
#[repr(u8)]
148220
pub enum HwBreakpointCondition {
@@ -153,8 +225,8 @@ pub enum HwBreakpointCondition {
153225
}
154226

155227
impl HwBreakpointCondition {
156-
const fn bit_position(n: u8) -> u8 {
157-
16 + 4 * n
228+
const fn bit_position(n: DebugAddressRegisterNumber) -> u8 {
229+
16 + 4 * n.0
158230
}
159231
}
160232

@@ -168,31 +240,26 @@ pub enum HwBreakpointSize {
168240
}
169241

170242
impl HwBreakpointSize {
171-
const fn bit_position(n: u8) -> u8 {
172-
18 + 4 * n
243+
pub const fn new(size: usize) -> Option<Self> {
244+
match size {
245+
1 => Some(Self::Length1B),
246+
2 => Some(Self::Length2B),
247+
8 => Some(Self::Length8B),
248+
4 => Some(Self::Length4B),
249+
_ => None,
250+
}
173251
}
174-
}
175-
176-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
177-
pub struct TryFromIntError(());
178252

179-
impl From<core::num::TryFromIntError> for TryFromIntError {
180-
fn from(_: core::num::TryFromIntError) -> Self {
181-
Self(())
253+
const fn bit_position(n: DebugAddressRegisterNumber) -> u8 {
254+
18 + 4 * n.0
182255
}
183256
}
184257

185258
impl TryFrom<usize> for HwBreakpointSize {
186259
type Error = TryFromIntError;
187260

188261
fn try_from(size: usize) -> Result<Self, Self::Error> {
189-
match size {
190-
1 => Ok(Self::Length1B),
191-
2 => Ok(Self::Length2B),
192-
8 => Ok(Self::Length8B),
193-
4 => Ok(Self::Length4B),
194-
_ => Err(TryFromIntError(())),
195-
}
262+
Self::new(size).ok_or(TryFromIntError(()))
196263
}
197264
}
198265

@@ -207,46 +274,6 @@ impl From<Dr7Flags> for Dr7Value {
207274
}
208275
}
209276

210-
macro_rules! set_condition_impl {
211-
($f:ident, $n:expr) => {
212-
#[inline]
213-
pub const fn $f(&mut self, condition: HwBreakpointCondition) {
214-
unsafe {
215-
self.set_condition($n, condition);
216-
}
217-
}
218-
};
219-
}
220-
221-
macro_rules! get_condition_impl {
222-
($f:ident, $n:expr) => {
223-
#[inline]
224-
pub fn $f(&self) -> HwBreakpointCondition {
225-
unsafe { self.get_condition($n) }
226-
}
227-
};
228-
}
229-
230-
macro_rules! set_size_impl {
231-
($f:ident, $n:expr) => {
232-
#[inline]
233-
pub const fn $f(&mut self, size: HwBreakpointSize) {
234-
unsafe {
235-
self.set_size($n, size);
236-
}
237-
}
238-
};
239-
}
240-
241-
macro_rules! get_size_impl {
242-
($f:ident, $n:expr) => {
243-
#[inline]
244-
pub fn $f(&self) -> HwBreakpointSize {
245-
unsafe { self.get_size($n) }
246-
}
247-
};
248-
}
249-
250277
impl Dr7Value {
251278
const fn mask() -> u64 {
252279
let field_mask = (1 << 32) - (1 << 16);
@@ -291,67 +318,35 @@ impl Dr7Value {
291318
unsafe { mem::transmute(&mut self.bits) }
292319
}
293320

294-
/// # Safety
295-
///
296-
/// `n` must be in `0..=3`.
297-
const unsafe fn set_condition(&mut self, n: u8, condition: HwBreakpointCondition) {
321+
pub const fn set_condition(&mut self, n: DebugAddressRegisterNumber, condition: HwBreakpointCondition) {
298322
let pos = HwBreakpointCondition::bit_position(n);
299323
// Clear value
300324
self.bits &= !(0b11 << pos);
301325
// Insert value
302326
self.bits |= (condition as u64) << pos;
303327
}
304328

305-
set_condition_impl!(set_condition_0, 0);
306-
set_condition_impl!(set_condition_1, 1);
307-
set_condition_impl!(set_condition_2, 2);
308-
set_condition_impl!(set_condition_3, 3);
309-
310-
/// # Safety
311-
///
312-
/// `n` must be in `0..=3`.
313-
unsafe fn get_condition(&self, n: u8) -> HwBreakpointCondition {
329+
pub fn get_condition(&self, n: DebugAddressRegisterNumber) -> HwBreakpointCondition {
314330
let pos = HwBreakpointCondition::bit_position(n);
315331
let condition = (self.bits >> pos) & 0b11;
316332
// SAFETY: All two bit patterns are valid variants
317333
unsafe { mem::transmute(condition as u8) }
318334
}
319335

320-
get_condition_impl!(get_condition_0, 0);
321-
get_condition_impl!(get_condition_1, 1);
322-
get_condition_impl!(get_condition_2, 2);
323-
get_condition_impl!(get_condition_3, 3);
324-
325-
/// # Safety
326-
///
327-
/// `n` must be in `0..=3`.
328-
const unsafe fn set_size(&mut self, n: u8, size: HwBreakpointSize) {
336+
pub const fn set_size(&mut self, n: DebugAddressRegisterNumber, size: HwBreakpointSize) {
329337
let pos = HwBreakpointSize::bit_position(n);
330338
// Clear value
331339
self.bits &= !(0b11 << pos);
332340
// Insert value
333341
self.bits |= (size as u64) << pos;
334342
}
335343

336-
set_size_impl!(set_size_0, 0);
337-
set_size_impl!(set_size_1, 1);
338-
set_size_impl!(set_size_2, 2);
339-
set_size_impl!(set_size_3, 3);
340-
341-
/// # Safety
342-
///
343-
/// `n` must be in `0..=3`.
344-
unsafe fn get_size(&self, n: u8) -> HwBreakpointSize {
344+
pub fn get_size(&self, n: DebugAddressRegisterNumber) -> HwBreakpointSize {
345345
let pos = HwBreakpointSize::bit_position(n);
346346
let size = (self.bits >> pos) & 0b11;
347347
// SAFETY: All two bit patterns are valid variants
348348
unsafe { mem::transmute(size as u8) }
349349
}
350-
351-
get_size_impl!(get_size_0, 0);
352-
get_size_impl!(get_size_1, 1);
353-
get_size_impl!(get_size_2, 2);
354-
get_size_impl!(get_size_3, 3);
355350
}
356351

357352
/// Debug Control Register (DR7).

0 commit comments

Comments
 (0)