Skip to content

Commit 206c7c6

Browse files
Merge pull request #294 from rmsyn/riscv/register/mtval2
register: add `mtval2` register
2 parents 392144e + 04b95cb commit 206c7c6

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

riscv/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1515
- Add `scontext` CSR
1616
- Add `mconfigptr` CSR
1717
- Bump MSRV to 1.67.0 for `log` to `ilog` name change
18+
- Add `mtval2` CSR
1819

1920
### Changed
2021

riscv/src/register.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ pub mod mip;
9090
pub mod mscratch;
9191
pub mod mtinst;
9292
pub mod mtval;
93+
pub mod mtval2;
9394

9495
// Machine Protection and Translation
9596
mod pmpcfgx;

riscv/src/register/mtval2.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//! mtval register
2+
3+
const MASK: usize = usize::MAX;
4+
5+
read_only_csr! {
6+
/// mtval2 register
7+
Mtval2: 0x348,
8+
mask: MASK,
9+
}
10+
11+
impl Mtval2 {
12+
/// Represents the bitshift value of the guest-page address stored in `mtval2`.
13+
pub const GUEST_PAGE_SHIFT: usize = 2;
14+
15+
/// Gets the guest-page fault physical address.
16+
///
17+
/// # Note
18+
///
19+
/// The address is written when an invalid implicit memory access during address translation.
20+
pub const fn guest_fault_address(&self) -> usize {
21+
self.bits() << Self::GUEST_PAGE_SHIFT
22+
}
23+
}
24+
25+
#[cfg(test)]
26+
mod tests {
27+
use super::*;
28+
29+
#[test]
30+
fn test_mtval2() {
31+
(1..=usize::BITS)
32+
.map(|r| ((1u128 << r) - 1) as usize)
33+
.for_each(|bits| {
34+
let mtval2 = Mtval2::from_bits(bits);
35+
assert_eq!(mtval2.bits(), bits);
36+
assert_eq!(
37+
mtval2.guest_fault_address(),
38+
bits << Mtval2::GUEST_PAGE_SHIFT
39+
);
40+
});
41+
}
42+
}

0 commit comments

Comments
 (0)