6
6
#[ macro_export]
7
7
macro_rules! read_csr {
8
8
( $csr_number: literal) => {
9
- /// Reads the CSR
9
+ /// Reads the CSR.
10
+ ///
11
+ /// **WARNING**: panics on non-`riscv` targets.
10
12
#[ inline]
11
13
unsafe fn _read( ) -> usize {
12
- match ( ) {
13
- #[ cfg( any( target_arch = "riscv32" , target_arch = "riscv64" ) ) ]
14
- ( ) => {
15
- let r: usize ;
16
- core:: arch:: asm!( concat!( "csrrs {0}, " , stringify!( $csr_number) , ", x0" ) , out( reg) r) ;
17
- r
18
- }
19
-
20
- #[ cfg( not( any( target_arch = "riscv32" , target_arch = "riscv64" ) ) ) ]
21
- ( ) => unimplemented!( ) ,
22
- }
14
+ _try_read( ) . unwrap( )
23
15
}
24
16
25
17
/// Attempts to read the CSR.
@@ -48,20 +40,12 @@ macro_rules! read_csr {
48
40
#[ macro_export]
49
41
macro_rules! read_csr_rv32 {
50
42
( $csr_number: literal) => {
51
- /// Reads the CSR
43
+ /// Reads the CSR.
44
+ ///
45
+ /// **WARNING**: panics on non-`riscv` targets.
52
46
#[ inline]
53
47
unsafe fn _read( ) -> usize {
54
- match ( ) {
55
- #[ cfg( target_arch = "riscv32" ) ]
56
- ( ) => {
57
- let r: usize ;
58
- core:: arch:: asm!( concat!( "csrrs {0}, " , stringify!( $csr_number) , ", x0" ) , out( reg) r) ;
59
- r
60
- }
61
-
62
- #[ cfg( not( target_arch = "riscv32" ) ) ]
63
- ( ) => unimplemented!( ) ,
64
- }
48
+ _try_read( ) . unwrap( )
65
49
}
66
50
67
51
/// Attempts to read the CSR.
@@ -90,7 +74,9 @@ macro_rules! read_csr_as {
90
74
( $register: ident, $csr_number: literal) => {
91
75
$crate:: read_csr!( $csr_number) ;
92
76
93
- /// Reads the CSR
77
+ /// Reads the CSR.
78
+ ///
79
+ /// **WARNING**: panics on non-`riscv` targets.
94
80
#[ inline]
95
81
pub fn read( ) -> $register {
96
82
$register {
@@ -116,7 +102,9 @@ macro_rules! read_csr_as_rv32 {
116
102
( $register: ident, $csr_number: literal) => {
117
103
$crate:: read_csr_rv32!( $csr_number) ;
118
104
119
- /// Reads the CSR
105
+ /// Reads the CSR.
106
+ ///
107
+ /// **WARNING**: panics on non-`riscv` targets.
120
108
#[ inline]
121
109
pub fn read( ) -> $register {
122
110
$register {
@@ -140,7 +128,9 @@ macro_rules! read_csr_as_usize {
140
128
( $csr_number: literal) => {
141
129
$crate:: read_csr!( $csr_number) ;
142
130
143
- /// Reads the CSR
131
+ /// Reads the CSR.
132
+ ///
133
+ /// **WARNING**: panics on non-`riscv` targets.
144
134
#[ inline]
145
135
pub fn read( ) -> usize {
146
136
unsafe { _read( ) }
@@ -160,7 +150,9 @@ macro_rules! read_csr_as_usize_rv32 {
160
150
( $csr_number: literal) => {
161
151
$crate:: read_csr_rv32!( $csr_number) ;
162
152
163
- /// Reads the CSR
153
+ /// Reads the CSR.
154
+ ///
155
+ /// **WARNING**: panics on non-`riscv` targets.
164
156
#[ inline]
165
157
pub fn read( ) -> usize {
166
158
unsafe { _read( ) }
@@ -182,17 +174,13 @@ macro_rules! read_csr_as_usize_rv32 {
182
174
#[ macro_export]
183
175
macro_rules! write_csr {
184
176
( $csr_number: literal) => {
185
- /// Writes the CSR
177
+ /// Writes the CSR.
178
+ ///
179
+ /// **WARNING**: panics on non-`riscv` targets.
186
180
#[ inline]
187
181
#[ allow( unused_variables) ]
188
182
unsafe fn _write( bits: usize ) {
189
- match ( ) {
190
- #[ cfg( any( target_arch = "riscv32" , target_arch = "riscv64" ) ) ]
191
- ( ) => core:: arch:: asm!( concat!( "csrrw x0, " , stringify!( $csr_number) , ", {0}" ) , in( reg) bits) ,
192
-
193
- #[ cfg( not( any( target_arch = "riscv32" , target_arch = "riscv64" ) ) ) ]
194
- ( ) => unimplemented!( ) ,
195
- }
183
+ _try_write( bits) . unwrap( ) ;
196
184
}
197
185
198
186
/// Attempts to write the CSR.
@@ -221,17 +209,13 @@ macro_rules! write_csr {
221
209
#[ macro_export]
222
210
macro_rules! write_csr_rv32 {
223
211
( $csr_number: literal) => {
224
- /// Writes the CSR
212
+ /// Writes the CSR.
213
+ ///
214
+ /// **WARNING**: panics on non-`riscv` targets.
225
215
#[ inline]
226
216
#[ allow( unused_variables) ]
227
217
unsafe fn _write( bits: usize ) {
228
- match ( ) {
229
- #[ cfg( target_arch = "riscv32" ) ]
230
- ( ) => core:: arch:: asm!( concat!( "csrrw x0, " , stringify!( $csr_number) , ", {0}" ) , in( reg) bits) ,
231
-
232
- #[ cfg( not( target_arch = "riscv32" ) ) ]
233
- ( ) => unimplemented!( ) ,
234
- }
218
+ _try_write( bits) . unwrap( ) ;
235
219
}
236
220
237
221
/// Attempts to write the CSR.
@@ -258,7 +242,9 @@ macro_rules! write_csr_as {
258
242
( $csr_type: ty, $csr_number: literal) => {
259
243
$crate:: write_csr!( $csr_number) ;
260
244
261
- /// Writes the CSR
245
+ /// Writes the CSR.
246
+ ///
247
+ /// **WARNING**: panics on non-`riscv` targets.
262
248
#[ inline]
263
249
pub fn write( value: $csr_type) {
264
250
unsafe { _write( value. bits) }
@@ -278,7 +264,9 @@ macro_rules! write_csr_as_rv32 {
278
264
( $csr_type: ty, $csr_number: literal) => {
279
265
$crate:: write_csr_rv32!( $csr_number) ;
280
266
281
- /// Writes the CSR
267
+ /// Writes the CSR.
268
+ ///
269
+ /// **WARNING**: panics on non-`riscv` targets.
282
270
#[ inline]
283
271
pub fn write( value: $csr_type) {
284
272
unsafe { _write( value. bits) }
@@ -298,7 +286,9 @@ macro_rules! write_csr_as_usize {
298
286
( $csr_number: literal) => {
299
287
$crate:: write_csr!( $csr_number) ;
300
288
301
- /// Writes the CSR
289
+ /// Writes the CSR.
290
+ ///
291
+ /// **WARNING**: panics on non-`riscv` targets.
302
292
#[ inline]
303
293
pub fn write( bits: usize ) {
304
294
unsafe { _write( bits) }
@@ -318,7 +308,9 @@ macro_rules! write_csr_as_usize_rv32 {
318
308
( $csr_number: literal) => {
319
309
$crate:: write_csr_rv32!( $csr_number) ;
320
310
321
- /// Writes the CSR
311
+ /// Writes the CSR.
312
+ ///
313
+ /// **WARNING**: panics on non-`riscv` targets.
322
314
#[ inline]
323
315
pub fn write( bits: usize ) {
324
316
unsafe { _write( bits) }
@@ -338,17 +330,13 @@ macro_rules! write_csr_as_usize_rv32 {
338
330
#[ macro_export]
339
331
macro_rules! set {
340
332
( $csr_number: literal) => {
341
- /// Set the CSR
333
+ /// Set the CSR.
334
+ ///
335
+ /// **WARNING**: panics on non-`riscv` targets.
342
336
#[ inline]
343
337
#[ allow( unused_variables) ]
344
338
unsafe fn _set( bits: usize ) {
345
- match ( ) {
346
- #[ cfg( any( target_arch = "riscv32" , target_arch = "riscv64" ) ) ]
347
- ( ) => core:: arch:: asm!( concat!( "csrrs x0, " , stringify!( $csr_number) , ", {0}" ) , in( reg) bits) ,
348
-
349
- #[ cfg( not( any( target_arch = "riscv32" , target_arch = "riscv64" ) ) ) ]
350
- ( ) => unimplemented!( ) ,
351
- }
339
+ _try_set( bits) . unwrap( ) ;
352
340
}
353
341
354
342
/// Attempts to set the CSR.
@@ -375,17 +363,13 @@ macro_rules! set {
375
363
#[ macro_export]
376
364
macro_rules! set_rv32 {
377
365
( $csr_number: literal) => {
378
- /// Set the CSR
366
+ /// Set the CSR.
367
+ ///
368
+ /// **WARNING**: panics on non-`riscv` targets.
379
369
#[ inline]
380
370
#[ allow( unused_variables) ]
381
371
unsafe fn _set( bits: usize ) {
382
- match ( ) {
383
- #[ cfg( target_arch = "riscv32" ) ]
384
- ( ) => core:: arch:: asm!( concat!( "csrrs x0, " , stringify!( $csr_number) , ", {0}" ) , in( reg) bits) ,
385
-
386
- #[ cfg( not( target_arch = "riscv32" ) ) ]
387
- ( ) => unimplemented!( ) ,
388
- }
372
+ _try_set( bits) . unwrap( ) ;
389
373
}
390
374
391
375
/// Attempts to set the CSR.
@@ -412,17 +396,13 @@ macro_rules! set_rv32 {
412
396
#[ macro_export]
413
397
macro_rules! clear {
414
398
( $csr_number: literal) => {
415
- /// Clear the CSR
399
+ /// Clear the CSR.
400
+ ///
401
+ /// **WARNING**: panics on non-`riscv` targets.
416
402
#[ inline]
417
403
#[ allow( unused_variables) ]
418
404
unsafe fn _clear( bits: usize ) {
419
- match ( ) {
420
- #[ cfg( any( target_arch = "riscv32" , target_arch = "riscv64" ) ) ]
421
- ( ) => core:: arch:: asm!( concat!( "csrrc x0, " , stringify!( $csr_number) , ", {0}" ) , in( reg) bits) ,
422
-
423
- #[ cfg( not( any( target_arch = "riscv32" , target_arch = "riscv64" ) ) ) ]
424
- ( ) => unimplemented!( ) ,
425
- }
405
+ _try_clear( bits) . unwrap( ) ;
426
406
}
427
407
428
408
/// Attempts to clear the CSR.
@@ -449,17 +429,13 @@ macro_rules! clear {
449
429
#[ macro_export]
450
430
macro_rules! clear_rv32 {
451
431
( $csr_number: literal) => {
452
- /// Clear the CSR
432
+ /// Clear the CSR.
433
+ ///
434
+ /// **WARNING**: panics on non-`riscv` targets.
453
435
#[ inline]
454
436
#[ allow( unused_variables) ]
455
437
unsafe fn _clear( bits: usize ) {
456
- match ( ) {
457
- #[ cfg( target_arch = "riscv32" ) ]
458
- ( ) => core:: arch:: asm!( concat!( "csrrc x0, " , stringify!( $csr_number) , ", {0}" ) , in( reg) bits) ,
459
-
460
- #[ cfg( not( target_arch = "riscv32" ) ) ]
461
- ( ) => unimplemented!( ) ,
462
- }
438
+ _try_clear( bits) . unwrap( ) ;
463
439
}
464
440
465
441
/// Attempts to clear the CSR.
@@ -542,20 +518,12 @@ macro_rules! read_composite_csr {
542
518
543
519
macro_rules! set_pmp {
544
520
( ) => {
545
- /// Set the pmp configuration corresponding to the index
521
+ /// Set the pmp configuration corresponding to the index.
522
+ ///
523
+ /// **WARNING**: panics on non-`riscv` targets, and/or if `index` is out-of-bounds.
546
524
#[ inline]
547
525
pub unsafe fn set_pmp( index: usize , range: Range , permission: Permission , locked: bool ) {
548
- #[ cfg( target_arch = "riscv32" ) ]
549
- assert!( index < 4 ) ;
550
-
551
- #[ cfg( target_arch = "riscv64" ) ]
552
- assert!( index < 8 ) ;
553
-
554
- let mut value = _read( ) ;
555
- value &= !( 0xFF << ( 8 * index) ) ; // clear previous value
556
- let byte = ( locked as usize ) << 7 | ( range as usize ) << 3 | ( permission as usize ) ;
557
- value |= byte << ( 8 * index) ;
558
- _write( value) ;
526
+ try_set_pmp( index, range, permission, locked) . unwrap( )
559
527
}
560
528
561
529
/// Attempts to set the pmp configuration corresponding to the index.
@@ -568,12 +536,13 @@ macro_rules! set_pmp {
568
536
permission: Permission ,
569
537
locked: bool ,
570
538
) -> $crate:: result:: Result <( ) > {
571
- let max = if cfg!( target_arch = "riscv32" ) {
572
- Ok ( 4usize )
573
- } else if cfg!( target_arch = "riscv64" ) {
574
- Ok ( 8usize )
575
- } else {
576
- Err ( $crate:: result:: Error :: Unimplemented )
539
+ let max = match ( ) {
540
+ #[ cfg( target_arch = "riscv32" ) ]
541
+ ( ) => Ok ( 4usize ) ,
542
+ #[ cfg( target_arch = "riscv64" ) ]
543
+ ( ) => Ok ( 8usize ) ,
544
+ #[ cfg( not( any( target_arch = "riscv32" , target_arch = "riscv64" ) ) ) ]
545
+ ( ) => Err ( $crate:: result:: Error :: Unimplemented ) ,
577
546
} ?;
578
547
579
548
if index < max {
@@ -595,31 +564,26 @@ macro_rules! set_pmp {
595
564
596
565
macro_rules! clear_pmp {
597
566
( ) => {
598
- /// Clear the pmp configuration corresponding to the index
567
+ /// Clear the pmp configuration corresponding to the index.
568
+ ///
569
+ /// **WARNING**: panics on non-`riscv` targets, and/or if `index` is out-of-bounds.
599
570
#[ inline]
600
571
pub unsafe fn clear_pmp( index: usize ) {
601
- #[ cfg( target_arch = "riscv32" ) ]
602
- assert!( index < 4 ) ;
603
-
604
- #[ cfg( target_arch = "riscv64" ) ]
605
- assert!( index < 8 ) ;
606
-
607
- let mut value = _read( ) ;
608
- value &= !( 0xFF << ( 8 * index) ) ; // clear previous value
609
- _write( value) ;
572
+ try_clear_pmp( index) . unwrap( ) ;
610
573
}
611
574
612
575
/// Attempts to clear the pmp configuration corresponding to the index.
613
576
///
614
577
/// Returns an error if the index is invalid.
615
578
#[ inline]
616
579
pub unsafe fn try_clear_pmp( index: usize ) -> $crate:: result:: Result <( ) > {
617
- let max = if cfg!( target_arch = "riscv32" ) {
618
- Ok ( 4usize )
619
- } else if cfg!( target_arch = "riscv64" ) {
620
- Ok ( 8usize )
621
- } else {
622
- Err ( $crate:: result:: Error :: Unimplemented )
580
+ let max = match ( ) {
581
+ #[ cfg( target_arch = "riscv32" ) ]
582
+ ( ) => Ok ( 4usize ) ,
583
+ #[ cfg( target_arch = "riscv64" ) ]
584
+ ( ) => Ok ( 8usize ) ,
585
+ #[ cfg( not( any( target_arch = "riscv32" , target_arch = "riscv64" ) ) ) ]
586
+ ( ) => Err ( $crate:: result:: Error :: Unimplemented ) ,
623
587
} ?;
624
588
625
589
if index < max {
0 commit comments