|
| 1 | +//! `mconfigptr` register. |
| 2 | +
|
| 3 | +use crate::result::{Error, Result}; |
| 4 | + |
| 5 | +const MASK: usize = usize::MAX; |
| 6 | + |
| 7 | +read_only_csr! { |
| 8 | + /// `mconfigptr` register. |
| 9 | + Mconfigptr: 0xf15, |
| 10 | + mask: MASK, |
| 11 | + sentinel: 0, |
| 12 | +} |
| 13 | + |
| 14 | +impl Mconfigptr { |
| 15 | + /// Represents the bitshift for a properly aligned configuration pointer. |
| 16 | + pub const ALIGN_SHIFT: usize = (usize::BITS / 8).ilog2() as usize; |
| 17 | + /// Represents the bitmask for a properly aligned configuration pointer. |
| 18 | + pub const ALIGN_MASK: usize = (1usize << Self::ALIGN_SHIFT) - 1; |
| 19 | + |
| 20 | + /// Gets the pointer to the machine configuration structure. |
| 21 | + /// |
| 22 | + /// # Panics |
| 23 | + /// |
| 24 | + /// Panics if: |
| 25 | + /// |
| 26 | + /// - the value is `0`, indicating no configuration structure |
| 27 | + /// - the pointer is not aligned to an MXLEN byte value |
| 28 | + pub fn as_ptr(&self) -> *const u8 { |
| 29 | + self.try_as_ptr().unwrap() |
| 30 | + } |
| 31 | + |
| 32 | + /// Attempts to get the pointer to the machine configuration structure. |
| 33 | + /// |
| 34 | + /// # Note |
| 35 | + /// |
| 36 | + /// Returns an error if: |
| 37 | + /// |
| 38 | + /// - the value is `0`, indicating no configuration structure |
| 39 | + /// - the pointer is not aligned to an MXLEN byte value |
| 40 | + pub const fn try_as_ptr(&self) -> Result<*const u8> { |
| 41 | + match self.bits() { |
| 42 | + 0 => Err(Error::InvalidFieldVariant { |
| 43 | + field: "mconfigptr", |
| 44 | + value: 0, |
| 45 | + }), |
| 46 | + p if p & Self::ALIGN_MASK != 0 => Err(Error::InvalidFieldValue { |
| 47 | + field: "mconfigptr", |
| 48 | + value: p, |
| 49 | + bitmask: !Self::ALIGN_MASK, |
| 50 | + }), |
| 51 | + p => Ok(p as *const _), |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +#[cfg(test)] |
| 57 | +mod tests { |
| 58 | + use super::*; |
| 59 | + |
| 60 | + #[test] |
| 61 | + fn test_mconfigptr() { |
| 62 | + #[cfg(target_arch = "riscv32")] |
| 63 | + const EXP_SHIFT: usize = 2; |
| 64 | + #[cfg(not(target_arch = "riscv32"))] |
| 65 | + const EXP_SHIFT: usize = 3; |
| 66 | + |
| 67 | + const EXP_MASK: usize = (1usize << EXP_SHIFT) - 1; |
| 68 | + |
| 69 | + assert_eq!(Mconfigptr::ALIGN_SHIFT, EXP_SHIFT); |
| 70 | + assert_eq!(Mconfigptr::ALIGN_MASK, EXP_MASK); |
| 71 | + |
| 72 | + (1..usize::BITS) |
| 73 | + .map(|b| ((1u128 << b) - 1) as usize) |
| 74 | + .for_each(|ptr| { |
| 75 | + let mconfigptr = Mconfigptr::from_bits(ptr); |
| 76 | + assert_eq!(mconfigptr.bits(), ptr); |
| 77 | + |
| 78 | + match mconfigptr.try_as_ptr() { |
| 79 | + Ok(cfg_ptr) => { |
| 80 | + assert_eq!(cfg_ptr, ptr as *const _); |
| 81 | + assert_eq!(mconfigptr.as_ptr(), ptr as *const _); |
| 82 | + } |
| 83 | + Err(err) if ptr == 0 => assert_eq!( |
| 84 | + err, |
| 85 | + Error::InvalidFieldVariant { |
| 86 | + field: "mconfigptr", |
| 87 | + value: 0 |
| 88 | + } |
| 89 | + ), |
| 90 | + Err(err) => assert_eq!( |
| 91 | + err, |
| 92 | + Error::InvalidFieldValue { |
| 93 | + field: "mconfigptr", |
| 94 | + value: ptr, |
| 95 | + bitmask: !Mconfigptr::ALIGN_MASK, |
| 96 | + } |
| 97 | + ), |
| 98 | + } |
| 99 | + |
| 100 | + let aligned_ptr = ptr << Mconfigptr::ALIGN_SHIFT; |
| 101 | + let aligned_mconfigptr = Mconfigptr::from_bits(aligned_ptr); |
| 102 | + |
| 103 | + assert_eq!(aligned_mconfigptr.try_as_ptr(), Ok(aligned_ptr as *const _)); |
| 104 | + assert_eq!(aligned_mconfigptr.as_ptr(), aligned_ptr as *const _); |
| 105 | + }); |
| 106 | + } |
| 107 | +} |
0 commit comments