@@ -49,6 +49,44 @@ debug_address_register_impl!(Dr1, "DR1");
49
49
debug_address_register_impl ! ( Dr2 , "DR2" ) ;
50
50
debug_address_register_impl ! ( Dr3 , "DR3" ) ;
51
51
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
+
52
90
/// Debug Status Register (DR6).
53
91
///
54
92
/// Reports debug conditions from the last debug exception.
@@ -96,6 +134,18 @@ bitflags! {
96
134
}
97
135
}
98
136
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
+
99
149
impl Dr6 {
100
150
/// Read the current set of DR6 flags.
101
151
#[ inline]
@@ -143,6 +193,28 @@ bitflags! {
143
193
}
144
194
}
145
195
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
+
146
218
#[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
147
219
#[ repr( u8 ) ]
148
220
pub enum HwBreakpointCondition {
@@ -153,8 +225,8 @@ pub enum HwBreakpointCondition {
153
225
}
154
226
155
227
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
158
230
}
159
231
}
160
232
@@ -168,31 +240,26 @@ pub enum HwBreakpointSize {
168
240
}
169
241
170
242
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
+ }
173
251
}
174
- }
175
-
176
- #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
177
- pub struct TryFromIntError ( ( ) ) ;
178
252
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
182
255
}
183
256
}
184
257
185
258
impl TryFrom < usize > for HwBreakpointSize {
186
259
type Error = TryFromIntError ;
187
260
188
261
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 ( ( ) ) )
196
263
}
197
264
}
198
265
@@ -207,46 +274,6 @@ impl From<Dr7Flags> for Dr7Value {
207
274
}
208
275
}
209
276
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
-
250
277
impl Dr7Value {
251
278
const fn mask ( ) -> u64 {
252
279
let field_mask = ( 1 << 32 ) - ( 1 << 16 ) ;
@@ -291,67 +318,35 @@ impl Dr7Value {
291
318
unsafe { mem:: transmute ( self . bits ) }
292
319
}
293
320
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 ) {
298
322
let pos = HwBreakpointCondition :: bit_position ( n) ;
299
323
// Clear value
300
324
self . bits &= !( 0b11 << pos) ;
301
325
// Insert value
302
326
self . bits |= ( condition as u64 ) << pos;
303
327
}
304
328
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 {
314
330
let pos = HwBreakpointCondition :: bit_position ( n) ;
315
331
let condition = ( self . bits >> pos) & 0b11 ;
316
332
// SAFETY: All two bit patterns are valid variants
317
333
unsafe { mem:: transmute ( condition as u8 ) }
318
334
}
319
335
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 ) {
329
337
let pos = HwBreakpointSize :: bit_position ( n) ;
330
338
// Clear value
331
339
self . bits &= !( 0b11 << pos) ;
332
340
// Insert value
333
341
self . bits |= ( size as u64 ) << pos;
334
342
}
335
343
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 {
345
345
let pos = HwBreakpointSize :: bit_position ( n) ;
346
346
let size = ( self . bits >> pos) & 0b11 ;
347
347
// SAFETY: All two bit patterns are valid variants
348
348
unsafe { mem:: transmute ( size as u8 ) }
349
349
}
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 ) ;
355
350
}
356
351
357
352
/// Debug Control Register (DR7).
0 commit comments