Skip to content

Commit 2c5b62c

Browse files
Merge pull request #290 from rmsyn/riscv/register/mconfigptr
register: add `mconfigptr` register
2 parents 09dc978 + 33e17d8 commit 2c5b62c

File tree

9 files changed

+126
-15
lines changed

9 files changed

+126
-15
lines changed

.github/workflows/riscv-rt.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ jobs:
1010
build-riscv:
1111
strategy:
1212
matrix:
13-
# All generated code should be running on stable now, MRSV is 1.61.0
14-
toolchain: [ stable, nightly, 1.61.0 ]
13+
# All generated code should be running on stable now, MRSV is 1.67.0
14+
toolchain: [ stable, nightly, 1.67.0 ]
1515
target:
1616
- riscv32i-unknown-none-elf
1717
- riscv32im-unknown-none-elf
@@ -28,9 +28,9 @@ jobs:
2828
- toolchain: nightly
2929
experimental: true
3030
exclude:
31-
- toolchain: 1.61.0
31+
- toolchain: 1.67.0
3232
target: riscv32im-unknown-none-elf
33-
- toolchain: 1.61.0
33+
- toolchain: 1.67.0
3434
target: riscv32imafc-unknown-none-elf
3535
runs-on: ubuntu-latest
3636
continue-on-error: ${{ matrix.experimental || false }}

.github/workflows/riscv-semihosting.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ jobs:
1111
build-riscv:
1212
strategy:
1313
matrix:
14-
# All generated code should be running on stable now, MRSV is 1.61.0
15-
toolchain: [ stable, nightly, 1.61.0 ]
14+
# All generated code should be running on stable now, MRSV is 1.67.0
15+
toolchain: [ stable, nightly, 1.67.0 ]
1616
target:
1717
- riscv32i-unknown-none-elf
1818
- riscv32imc-unknown-none-elf

.github/workflows/riscv-target-parser.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
strategy:
1212
matrix:
1313
os: [ macos-latest, ubuntu-latest, windows-latest ]
14-
toolchain: [ stable, nightly, 1.61.0 ]
14+
toolchain: [ stable, nightly, 1.67.0 ]
1515
include:
1616
# Nightly is only for reference and allowed to fail
1717
- rust: nightly

.github/workflows/riscv.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ jobs:
1111
build-riscv:
1212
strategy:
1313
matrix:
14-
# All generated code should be running on stable now, MRSV is 1.61.0
15-
toolchain: [ stable, nightly, 1.61.0 ]
14+
# All generated code should be running on stable now, MRSV is 1.67.0
15+
toolchain: [ stable, nightly, 1.67.0 ]
1616
target:
1717
- riscv32i-unknown-none-elf
1818
- riscv32imc-unknown-none-elf

.github/workflows/tests.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ jobs:
2020
run-build:
2121
strategy:
2222
matrix:
23-
# All generated code should be running on stable now, MRSV is 1.61.0
24-
toolchain: [ stable, nightly, 1.61.0 ]
23+
# All generated code should be running on stable now, MRSV is 1.67.0
24+
toolchain: [ stable, nightly, 1.67.0 ]
2525
target:
2626
- riscv32i-unknown-none-elf
2727
- riscv32im-unknown-none-elf
@@ -37,9 +37,9 @@ jobs:
3737
- toolchain: nightly
3838
experimental: true
3939
exclude:
40-
- toolchain: 1.61.0
40+
- toolchain: 1.67.0
4141
target: riscv32im-unknown-none-elf
42-
- toolchain: 1.61.0
42+
- toolchain: 1.67.0
4343
target: riscv32imafc-unknown-none-elf
4444
runs-on: ubuntu-latest
4545
continue-on-error: ${{ matrix.experimental || false }}

riscv/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ 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
17+
- Bump MSRV to 1.67.0 for `log` to `ilog` name change
1718

1819
### Changed
1920

riscv/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "riscv"
33
version = "0.13.0"
44
edition = "2021"
5-
rust-version = "1.61"
5+
rust-version = "1.67"
66
repository = "https://github.com/rust-embedded/riscv"
77
authors = ["The RISC-V Team <risc-v@teams.rust-embedded.org>"]
88
categories = ["embedded", "hardware-support", "no-std"]

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

Comments
 (0)