66#[ macro_export]
77macro_rules! read_csr {
88 ( $csr_number: literal) => {
9- /// Reads the CSR
9+ /// Reads the CSR.
10+ ///
11+ /// **WARNING**: panics on non-`riscv` targets.
1012 #[ inline]
1113 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( )
2315 }
2416
2517 /// Attempts to read the CSR.
@@ -48,20 +40,12 @@ macro_rules! read_csr {
4840#[ macro_export]
4941macro_rules! read_csr_rv32 {
5042 ( $csr_number: literal) => {
51- /// Reads the CSR
43+ /// Reads the CSR.
44+ ///
45+ /// **WARNING**: panics on non-`riscv` targets.
5246 #[ inline]
5347 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( )
6549 }
6650
6751 /// Attempts to read the CSR.
@@ -90,7 +74,9 @@ macro_rules! read_csr_as {
9074 ( $register: ident, $csr_number: literal) => {
9175 $crate:: read_csr!( $csr_number) ;
9276
93- /// Reads the CSR
77+ /// Reads the CSR.
78+ ///
79+ /// **WARNING**: panics on non-`riscv` targets.
9480 #[ inline]
9581 pub fn read( ) -> $register {
9682 $register {
@@ -116,7 +102,9 @@ macro_rules! read_csr_as_rv32 {
116102 ( $register: ident, $csr_number: literal) => {
117103 $crate:: read_csr_rv32!( $csr_number) ;
118104
119- /// Reads the CSR
105+ /// Reads the CSR.
106+ ///
107+ /// **WARNING**: panics on non-`riscv` targets.
120108 #[ inline]
121109 pub fn read( ) -> $register {
122110 $register {
@@ -140,7 +128,9 @@ macro_rules! read_csr_as_usize {
140128 ( $csr_number: literal) => {
141129 $crate:: read_csr!( $csr_number) ;
142130
143- /// Reads the CSR
131+ /// Reads the CSR.
132+ ///
133+ /// **WARNING**: panics on non-`riscv` targets.
144134 #[ inline]
145135 pub fn read( ) -> usize {
146136 unsafe { _read( ) }
@@ -160,7 +150,9 @@ macro_rules! read_csr_as_usize_rv32 {
160150 ( $csr_number: literal) => {
161151 $crate:: read_csr_rv32!( $csr_number) ;
162152
163- /// Reads the CSR
153+ /// Reads the CSR.
154+ ///
155+ /// **WARNING**: panics on non-`riscv` targets.
164156 #[ inline]
165157 pub fn read( ) -> usize {
166158 unsafe { _read( ) }
@@ -182,17 +174,13 @@ macro_rules! read_csr_as_usize_rv32 {
182174#[ macro_export]
183175macro_rules! write_csr {
184176 ( $csr_number: literal) => {
185- /// Writes the CSR
177+ /// Writes the CSR.
178+ ///
179+ /// **WARNING**: panics on non-`riscv` targets.
186180 #[ inline]
187181 #[ allow( unused_variables) ]
188182 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( ) ;
196184 }
197185
198186 /// Attempts to write the CSR.
@@ -221,17 +209,13 @@ macro_rules! write_csr {
221209#[ macro_export]
222210macro_rules! write_csr_rv32 {
223211 ( $csr_number: literal) => {
224- /// Writes the CSR
212+ /// Writes the CSR.
213+ ///
214+ /// **WARNING**: panics on non-`riscv` targets.
225215 #[ inline]
226216 #[ allow( unused_variables) ]
227217 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( ) ;
235219 }
236220
237221 /// Attempts to write the CSR.
@@ -258,7 +242,9 @@ macro_rules! write_csr_as {
258242 ( $csr_type: ty, $csr_number: literal) => {
259243 $crate:: write_csr!( $csr_number) ;
260244
261- /// Writes the CSR
245+ /// Writes the CSR.
246+ ///
247+ /// **WARNING**: panics on non-`riscv` targets.
262248 #[ inline]
263249 pub fn write( value: $csr_type) {
264250 unsafe { _write( value. bits) }
@@ -278,7 +264,9 @@ macro_rules! write_csr_as_rv32 {
278264 ( $csr_type: ty, $csr_number: literal) => {
279265 $crate:: write_csr_rv32!( $csr_number) ;
280266
281- /// Writes the CSR
267+ /// Writes the CSR.
268+ ///
269+ /// **WARNING**: panics on non-`riscv` targets.
282270 #[ inline]
283271 pub fn write( value: $csr_type) {
284272 unsafe { _write( value. bits) }
@@ -298,7 +286,9 @@ macro_rules! write_csr_as_usize {
298286 ( $csr_number: literal) => {
299287 $crate:: write_csr!( $csr_number) ;
300288
301- /// Writes the CSR
289+ /// Writes the CSR.
290+ ///
291+ /// **WARNING**: panics on non-`riscv` targets.
302292 #[ inline]
303293 pub fn write( bits: usize ) {
304294 unsafe { _write( bits) }
@@ -318,7 +308,9 @@ macro_rules! write_csr_as_usize_rv32 {
318308 ( $csr_number: literal) => {
319309 $crate:: write_csr_rv32!( $csr_number) ;
320310
321- /// Writes the CSR
311+ /// Writes the CSR.
312+ ///
313+ /// **WARNING**: panics on non-`riscv` targets.
322314 #[ inline]
323315 pub fn write( bits: usize ) {
324316 unsafe { _write( bits) }
@@ -338,17 +330,13 @@ macro_rules! write_csr_as_usize_rv32 {
338330#[ macro_export]
339331macro_rules! set {
340332 ( $csr_number: literal) => {
341- /// Set the CSR
333+ /// Set the CSR.
334+ ///
335+ /// **WARNING**: panics on non-`riscv` targets.
342336 #[ inline]
343337 #[ allow( unused_variables) ]
344338 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( ) ;
352340 }
353341
354342 /// Attempts to set the CSR.
@@ -375,17 +363,13 @@ macro_rules! set {
375363#[ macro_export]
376364macro_rules! set_rv32 {
377365 ( $csr_number: literal) => {
378- /// Set the CSR
366+ /// Set the CSR.
367+ ///
368+ /// **WARNING**: panics on non-`riscv` targets.
379369 #[ inline]
380370 #[ allow( unused_variables) ]
381371 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( ) ;
389373 }
390374
391375 /// Attempts to set the CSR.
@@ -412,17 +396,13 @@ macro_rules! set_rv32 {
412396#[ macro_export]
413397macro_rules! clear {
414398 ( $csr_number: literal) => {
415- /// Clear the CSR
399+ /// Clear the CSR.
400+ ///
401+ /// **WARNING**: panics on non-`riscv` targets.
416402 #[ inline]
417403 #[ allow( unused_variables) ]
418404 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( ) ;
426406 }
427407
428408 /// Attempts to clear the CSR.
@@ -449,17 +429,13 @@ macro_rules! clear {
449429#[ macro_export]
450430macro_rules! clear_rv32 {
451431 ( $csr_number: literal) => {
452- /// Clear the CSR
432+ /// Clear the CSR.
433+ ///
434+ /// **WARNING**: panics on non-`riscv` targets.
453435 #[ inline]
454436 #[ allow( unused_variables) ]
455437 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( ) ;
463439 }
464440
465441 /// Attempts to clear the CSR.
@@ -542,20 +518,12 @@ macro_rules! read_composite_csr {
542518
543519macro_rules! set_pmp {
544520 ( ) => {
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.
546524 #[ inline]
547525 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( )
559527 }
560528
561529 /// Attempts to set the pmp configuration corresponding to the index.
@@ -568,12 +536,13 @@ macro_rules! set_pmp {
568536 permission: Permission ,
569537 locked: bool ,
570538 ) -> $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 ) ,
577546 } ?;
578547
579548 if index < max {
@@ -595,31 +564,26 @@ macro_rules! set_pmp {
595564
596565macro_rules! clear_pmp {
597566 ( ) => {
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.
599570 #[ inline]
600571 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( ) ;
610573 }
611574
612575 /// Attempts to clear the pmp configuration corresponding to the index.
613576 ///
614577 /// Returns an error if the index is invalid.
615578 #[ inline]
616579 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 ) ,
623587 } ?;
624588
625589 if index < max {
0 commit comments