Skip to content

Commit 3a58c35

Browse files
committed
register: add mconfigptr register
Adds the definition for the `mconfigptr` Machine configuration pointer CSR.
1 parent 74c2091 commit 3a58c35

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

riscv/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1313
- Write utilities for `mcycle`, `minstret`
1414
- Add `senvcfg` CSR
1515
- Add `scontext` CSR
16-
- Add `mtinst` CSR
16+
- Add `mconfigptr` CSR
1717

1818
### Changed
1919

riscv/src/register.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ pub mod minstreth;
110110
mod mhpmeventx;
111111
pub use self::mhpmeventx::*;
112112

113+
// Machine configuration
114+
pub mod mconfigptr;
115+
113116
#[cfg(test)]
114117
mod tests;
115118

riscv/src/register/mconfigptr.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
}

0 commit comments

Comments
 (0)