Skip to content

Commit 97f1347

Browse files
committed
Add supervisor privilege bits defined in 1.10
1 parent 7630683 commit 97f1347

File tree

2 files changed

+77
-11
lines changed

2 files changed

+77
-11
lines changed

src/register/mstatus.rs

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
//! mstatus register
2-
// TODO: Virtualization, Memory Privilege and Extension Context Fields
2+
// FIXME: in 1.12 spec there will be `SBE` and `MBE` bits.
3+
// They allows to execute supervisor in given big endian,
4+
// they would be in a new register `mstatush` in RV32; we should implement `mstatush`
5+
// at that time.
6+
// FIXME: `SXL` and `UXL` bits require a structure interpreting XLEN,
7+
// which would be the best way we implement this using Rust?
38

49
use bit_field::BitField;
510
use core::mem::size_of;
@@ -136,6 +141,59 @@ impl Mstatus {
136141
}
137142
}
138143

144+
/// Permit Supervisor User Memory access
145+
#[inline]
146+
pub fn sum(&self) -> bool {
147+
self.bits.get_bit(18)
148+
}
149+
150+
/// Make eXecutable Readable
151+
#[inline]
152+
pub fn mxr(&self) -> bool {
153+
self.bits.get_bit(19)
154+
}
155+
156+
/// Trap Virtual Memory
157+
///
158+
/// If this bit is set, reads or writes to `satp` CSR or execute `sfence.vma`
159+
/// instruction when in S-mode will raise an illegal instruction exception.
160+
///
161+
/// TVM is hard-wired to 0 when S-mode is not supported.
162+
#[inline]
163+
pub fn tvm(&self) -> bool {
164+
self.bits.get_bit(20)
165+
}
166+
167+
/// Timeout Wait
168+
///
169+
/// Indicates that if WFI instruction should be intercepted.
170+
///
171+
/// If this bit is set, when WFI is executed in S-mode, and it does not complete
172+
/// within an implementation specific, bounded time limit, the WFI instruction will cause
173+
/// an illegal instruction trap; or could always cause trap then the time limit is zero.
174+
///
175+
/// TW is hard-wired to 0 when S-mode is not supported.
176+
#[inline]
177+
pub fn tw(&self) -> bool {
178+
self.bits.get_bit(21)
179+
}
180+
181+
/// Trap SRET
182+
///
183+
/// Indicates that if SRET instruction should be trapped to raise illegal
184+
/// instruction exception.
185+
///
186+
/// If S-mode is not supported, TSR bit is hard-wired to 0.
187+
#[inline]
188+
pub fn tsr(&self) -> bool {
189+
self.bits.get_bit(22)
190+
}
191+
192+
/*
193+
FIXME: There are MBE and SBE bits in 1.12; once Privileged Specification version 1.12
194+
is ratified, there should be read functions of these bits as well.
195+
*/
196+
139197
/// Whether either the FS field or XS field
140198
/// signals the presence of some dirty state
141199
#[inline]
@@ -152,26 +210,36 @@ clear!(0x300, __clear_mstatus);
152210
set_clear_csr!(
153211
/// User Interrupt Enable
154212
, set_uie, clear_uie, 1 << 0);
155-
156213
set_clear_csr!(
157214
/// Supervisor Interrupt Enable
158215
, set_sie, clear_sie, 1 << 1);
159-
160216
set_clear_csr!(
161217
/// Machine Interrupt Enable
162218
, set_mie, clear_mie, 1 << 3);
163-
164219
set_csr!(
165220
/// User Previous Interrupt Enable
166221
, set_upie, 1 << 4);
167-
168222
set_csr!(
169223
/// Supervisor Previous Interrupt Enable
170224
, set_spie, 1 << 5);
171-
172225
set_csr!(
173226
/// Machine Previous Interrupt Enable
174227
, set_mpie, 1 << 7);
228+
set_clear_csr!(
229+
/// Permit Supervisor User Memory access
230+
, set_sum, clear_sum, 1 << 18);
231+
set_clear_csr!(
232+
/// Make eXecutable Readable
233+
, set_mxr, clear_mxr, 1 << 19);
234+
set_clear_csr!(
235+
/// Trap Virtual Memory
236+
, set_tvm, clear_tvm, 1 << 20);
237+
set_clear_csr!(
238+
/// Timeout Wait
239+
, set_tw, clear_tw, 1 << 21);
240+
set_clear_csr!(
241+
/// Trap SRET
242+
, set_tsr, clear_tsr, 1 << 22);
175243

176244
/// Supervisor Previous Privilege Mode
177245
#[inline]

src/register/sstatus.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,15 @@ set_csr!(
113113
set_csr!(
114114
/// Supervisor Previous Interrupt Enable
115115
, set_spie, 1 << 5);
116-
set_clear_csr!(
117-
/// Make eXecutable Readable
118-
, set_mxr, clear_mxr, 1 << 19);
119116
set_clear_csr!(
120117
/// Permit Supervisor User Memory access
121118
, set_sum, clear_sum, 1 << 18);
119+
set_clear_csr!(
120+
/// Make eXecutable Readable
121+
, set_mxr, clear_mxr, 1 << 19);
122122

123123
/// Supervisor Previous Privilege Mode
124124
#[inline]
125-
#[cfg(riscv)]
126125
pub unsafe fn set_spp(spp: SPP) {
127126
match spp {
128127
SPP::Supervisor => _set(1 << 8),
@@ -132,7 +131,6 @@ pub unsafe fn set_spp(spp: SPP) {
132131

133132
/// The status of the floating-point unit
134133
#[inline]
135-
#[cfg(riscv)]
136134
pub unsafe fn set_fs(fs: FS) {
137135
let mut value = _read();
138136
value.set_bits(13..15, fs as usize);

0 commit comments

Comments
 (0)