1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
pub use super::mstatus::FS;
use bit_field::BitField;
use core::mem::size_of;
#[derive(Clone, Copy, Debug)]
pub struct Sstatus {
bits: usize,
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum SPP {
Supervisor = 1,
User = 0,
}
impl Sstatus {
#[inline]
pub fn bits(&self) -> usize {
self.bits
}
#[inline]
pub fn uie(&self) -> bool {
self.bits.get_bit(0)
}
#[inline]
pub fn sie(&self) -> bool {
self.bits.get_bit(1)
}
#[inline]
pub fn upie(&self) -> bool {
self.bits.get_bit(4)
}
#[inline]
pub fn spie(&self) -> bool {
self.bits.get_bit(5)
}
#[inline]
pub fn spp(&self) -> SPP {
match self.bits.get_bit(8) {
true => SPP::Supervisor,
false => SPP::User,
}
}
#[inline]
pub fn fs(&self) -> FS {
match self.bits.get_bits(13..15) {
0 => FS::Off,
1 => FS::Initial,
2 => FS::Clean,
3 => FS::Dirty,
_ => unreachable!(),
}
}
#[inline]
pub fn xs(&self) -> FS {
match self.bits.get_bits(15..17) {
0 => FS::Off,
1 => FS::Initial,
2 => FS::Clean,
3 => FS::Dirty,
_ => unreachable!(),
}
}
#[inline]
pub fn sum(&self) -> bool {
self.bits.get_bit(18)
}
#[inline]
pub fn mxr(&self) -> bool {
self.bits.get_bit(19)
}
#[inline]
pub fn sd(&self) -> bool {
self.bits.get_bit(size_of::<usize>() * 8 - 1)
}
#[inline]
pub fn set_spie(&mut self, val: bool) {
self.bits.set_bit(5, val);
}
#[inline]
pub fn set_sie(&mut self, val: bool) {
self.bits.set_bit(1, val);
}
#[inline]
pub fn set_spp(&mut self, val: SPP) {
self.bits.set_bit(8, val == SPP::Supervisor);
}
}
read_csr_as!(Sstatus, 0x100, __read_sstatus);
write_csr!(0x100, __write_sstatus);
set!(0x100, __set_sstatus);
clear!(0x100, __clear_sstatus);
set_clear_csr!(
, set_uie, clear_uie, 1 << 0);
set_clear_csr!(
, set_sie, clear_sie, 1 << 1);
set_csr!(
, set_upie, 1 << 4);
set_csr!(
, set_spie, 1 << 5);
set_clear_csr!(
, set_mxr, clear_mxr, 1 << 19);
set_clear_csr!(
, set_sum, clear_sum, 1 << 18);
#[inline]
#[cfg(riscv)]
pub unsafe fn set_spp(spp: SPP) {
match spp {
SPP::Supervisor => _set(1 << 8),
SPP::User => _clear(1 << 8),
}
}
#[inline]
#[cfg(riscv)]
pub unsafe fn set_fs(fs: FS) {
let mut value = _read();
value.set_bits(13..15, fs as usize);
_write(value);
}