Skip to content

riscv: add mseccfg/h CSR registers #338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions riscv/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- New `riscv::register::xip::clear_pending` atomic function for `mip` and `sip` registers.
This function is marked as `unsafe`, as its availability depends both on the target chip
and the target interrupt source.
- Add `mseccfg` CSR
- Add `mseccfgh` CSR

### Changed

Expand Down
3 changes: 3 additions & 0 deletions riscv/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ pub use self::mhpmeventx::*;

// Machine configuration
pub mod mconfigptr;
pub mod mseccfg;
#[cfg(any(test, target_arch = "riscv32"))]
pub mod mseccfgh;

#[cfg(test)]
mod tests;
Expand Down
12 changes: 4 additions & 8 deletions riscv/src/register/mip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,11 @@ mod tests {

#[test]
fn test_mip() {
let mut m = Mip::from_bits(0);
let mip = Mip::from_bits(0);

test_csr_field!(m, ssoft);
test_csr_field!(m, stimer);
test_csr_field!(m, sext);

assert!(!m.msoft());
assert!(!m.mtimer());
assert!(!m.mext());
assert!(!mip.msoft());
assert!(!mip.mtimer());
assert!(!mip.mext());

assert!(Mip::from_bits(1 << 3).msoft());
assert!(Mip::from_bits(1 << 7).mtimer());
Expand Down
118 changes: 118 additions & 0 deletions riscv/src/register/mseccfg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//! mseccfg register

#[cfg(not(target_arch = "riscv32"))]
const MASK: usize = 0x3_0000_0707;
#[cfg(target_arch = "riscv32")]
const MASK: usize = 0x707;

read_write_csr! {
/// mseccfg register
Mseccfg: 0x747,
mask: MASK,
}

read_write_csr_field! {
Mseccfg,
/// Machine-Mode Lockdown
///
/// # Note
///
/// Defined in in the [Smepmp](https://raw.githubusercontent.com/riscv/riscv-tee/main/Smepmp/Smepmp.pdf) extension.
mml: 0,
}

read_write_csr_field! {
Mseccfg,
/// Machine-Mode Whitelist Policy
///
/// # Note
///
/// Defined in in the [Smepmp](https://raw.githubusercontent.com/riscv/riscv-tee/main/Smepmp/Smepmp.pdf) extension.
mmwp: 1,
}

read_write_csr_field! {
Mseccfg,
/// Rule Locking Bypass
///
/// # Note
///
/// Defined in in the [Smepmp](https://raw.githubusercontent.com/riscv/riscv-tee/main/Smepmp/Smepmp.pdf) extension.
rlb: 2,
}

read_write_csr_field! {
Mseccfg,
/// User-mode seed
///
/// # Note
///
/// Defined in in the [Zkr](https://github.com/riscv/riscv-crypto/releases/download/v1.0.1-scalar/riscv-crypto-spec-scalar-v1.0.1.pdf) extension.
useed: 8,
}

read_write_csr_field! {
Mseccfg,
/// Supervisor-mode seed
///
/// # Note
///
/// Defined in in the [Zkr](https://github.com/riscv/riscv-crypto/releases/download/v1.0.1-scalar/riscv-crypto-spec-scalar-v1.0.1.pdf) extension.
sseed: 9,
}

read_write_csr_field! {
Mseccfg,
/// Machine-mode Landing Pad Enable
///
/// # Note
///
/// Defined in in the [Zicfilp](https://github.com/riscv/riscv-cfi/releases/download/v1.0/riscv-cfi.pdf) extension.
mlpe: 10,
}

csr_field_enum! {
/// Pointer Masking Machine-mode
PMM {
default: Disabled,
Disabled = 0,
EnabledXlen57 = 2,
EnabledXlen48 = 3,
}
}

#[cfg(not(target_arch = "riscv32"))]
read_write_csr_field! {
Mseccfg,
/// Pointer Masking Machine-mode
///
/// # Note
///
/// Defined in in the [Smmpm](https://github.com/riscv/riscv-j-extension/releases/download/pointer-masking-ratified/pointer-masking-ratified.pdf) extension.
pmm,
PMM: [32:33],
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_mseccfg() {
let mut mseccfg = Mseccfg::from_bits(0);

test_csr_field!(mseccfg, mml);
test_csr_field!(mseccfg, mmwp);
test_csr_field!(mseccfg, rlb);
test_csr_field!(mseccfg, useed);
test_csr_field!(mseccfg, sseed);
test_csr_field!(mseccfg, mlpe);

#[cfg(not(target_arch = "riscv32"))]
{
test_csr_field!(mseccfg, pmm: PMM::Disabled);
test_csr_field!(mseccfg, pmm: PMM::EnabledXlen57);
test_csr_field!(mseccfg, pmm: PMM::EnabledXlen48);
}
}
}
34 changes: 34 additions & 0 deletions riscv/src/register/mseccfgh.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//! mseccfgh register

use super::mseccfg::PMM;

read_write_csr! {
/// mseccfgh register
Mseccfgh: 0x757,
mask: 0x3,
}

read_write_csr_field! {
Mseccfgh,
/// Pointer Masking Machine-mode
///
/// # Note
///
/// Defined in in the [Smmpm](https://github.com/riscv/riscv-j-extension/releases/download/pointer-masking-ratified/pointer-masking-ratified.pdf) extension.
pmm,
PMM: [0:1],
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_mseccfgh() {
let mut mseccfgh = Mseccfgh::from_bits(0);

test_csr_field!(mseccfgh, pmm: PMM::Disabled);
test_csr_field!(mseccfgh, pmm: PMM::EnabledXlen57);
test_csr_field!(mseccfgh, pmm: PMM::EnabledXlen48);
}
}
3 changes: 1 addition & 2 deletions riscv/src/register/sip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ mod tests {

#[test]
fn test_sip() {
let mut sip = Sip::from_bits(0);
let sip = Sip::from_bits(0);

test_csr_field!(sip, ssoft);
assert!(!sip.stimer());
assert!(!sip.sext());

Expand Down
2 changes: 1 addition & 1 deletion typos.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[default]
extend-ignore-re = ["[Ss][Ii][Ee]", "[Ss][Xx][Ll]"]
extend-ignore-re = ["[Ss][Ii][Ee]", "[Ss][Xx][Ll]", "[.]?useed[.,:]?"]
Loading