Skip to content

Commit 3b861dd

Browse files
committed
Add all methods for each pmpcfg<x>. Import Pmpbyte in each mod using super::PmpByte. Cargo fmt.
1 parent 77747ef commit 3b861dd

File tree

2 files changed

+207
-62
lines changed

2 files changed

+207
-62
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ extern crate bit_field;
2121

2222
pub mod asm;
2323
pub mod interrupt;
24-
pub mod register;
24+
pub mod register;

src/register/pmpcfgx.rs

Lines changed: 206 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/// Physical memory protection configuration
22
use bit_field::BitField;
33

4-
/*************************************************************************************************/
54
/// Permission enum contains all possible permission modes for pmp registers
65
#[derive(Clone, Copy, Debug)]
76
pub enum Permission {
@@ -23,7 +22,7 @@ pub enum Range {
2322
NA4 = 2,
2423
NAPOT = 3,
2524
}
26-
/*************************************************************************************************/
25+
2726
/// PmpByte holds the a single pmp configuration
2827
#[derive(Clone, Copy, Debug)]
2928
pub struct PmpByte {
@@ -33,21 +32,24 @@ pub struct PmpByte {
3332
//locked: bool
3433
}
3534
/// PmpByte methods to get a pmp configuration attributes
36-
impl PmpByte{
35+
impl PmpByte {
36+
#[inline]
3737
pub fn is_locked(&self) -> bool {
3838
self.byte.get_bit(7)
3939
}
4040

41+
#[inline]
4142
pub fn get_range(&self) -> Option<Range> {
4243
match self.byte.get_bits(4..=5) {
4344
0 => Some(Range::OFF),
4445
1 => Some(Range::TOR),
4546
2 => Some(Range::NA4),
4647
3 => Some(Range::NAPOT),
47-
_ => None,
48+
_ => unreachable!(),
4849
}
4950
}
5051

52+
#[inline]
5153
pub fn get_permission(&self) -> Option<Permission> {
5254
match self.byte.get_bits(0..=2) {
5355
0 => Some(Permission::NONE),
@@ -58,141 +60,284 @@ impl PmpByte{
5860
5 => Some(Permission::RX),
5961
6 => Some(Permission::WX),
6062
7 => Some(Permission::RWX),
61-
_ => None,
63+
_ => unreachable!(),
6264
}
6365
}
6466
}
65-
/*************************************************************************************************/
6667

6768
/// Physical memory protection configuration
69+
/// Pmpcf0 struct contains pmp0cfg - pmp3cfg for 32-bit arch, or pmp0cfg - pmp7cfg for 64-bit arch
70+
/// get_byte() method retrieves a single pmp<x>cfg held in a PmpByte struct
6871
pub mod pmpcfg0 {
72+
use super::PmpByte;
6973

70-
/// Pmpcf0 struct contains pmp0cfg - pmp3cfg for 32-bit arch, or pmp0cfg - pmp7cfg for 64-bit arch
71-
/// get_byte() method retrieves a single pmp<x>cfg held in a PmpByte struct
7274
#[derive(Clone, Copy, Debug)]
7375
pub struct Pmpcfg0 {
7476
pub bits: u32,
7577
}
7678
impl Pmpcfg0 {
7779
#[inline]
78-
pub fn get_byte(&self,index:usize) -> PmpByte {
80+
pub fn get_byte(&self, index: usize) -> PmpByte {
7981
#[cfg(riscv32)]
8082
assert!(index < 4);
8183

8284
#[cfg(riscv64)]
8385
assert!(index < 8);
8486

8587
PmpByte {
86-
byte: self.bits.get_bits(8 * index..8 * (index + 1)) as u8
88+
byte: self.bits.get_bits(8 * index..8 * (index + 1)) as u8,
8789
}
8890
}
89-
}
9091

91-
read_csr_as!(Pmpcfg0, 0x3A0, __read_pmpcfg0);
92-
write_csr!(0x3A0, __write_pmpcfg0);
93-
set!(0x3A0, __set_pmpcfg0);
94-
clear!(0x3A0, __clear_pmpcfg0);
92+
read_csr_as!(Pmpcfg0, 0x3A0, __read_pmpcfg0);
93+
write_csr!(0x3A0, __write_pmpcfg0);
94+
set!(0x3A0, __set_pmpcfg0);
95+
clear!(0x3A0, __clear_pmpcfg0);
9596

96-
#[inline]
97-
pub unsafe fn set_permissions(permission: Permission, index: usize) {
98-
#[cfg(riscv32)]
99-
assert!(index < 4);
97+
#[inline]
98+
pub unsafe fn set_permissions(permission: Permission, index: usize) {
99+
#[cfg(riscv32)]
100+
assert!(index < 4);
100101

101-
#[cfg(riscv64)]
102-
assert!(index < 8);
102+
#[cfg(riscv64)]
103+
assert!(index < 8);
103104

104-
_write((permission as usize) << (index * 8));
105-
}
105+
_write((permission as usize) << (index * 8));
106+
}
106107

107-
#[inline]
108-
pub unsafe fn set_range(range: Range, index: usize) {
109-
#[cfg(riscv32)]
110-
assert!(index < 4);
108+
#[inline]
109+
pub unsafe fn set_range(range: Range, index: usize) {
110+
#[cfg(riscv32)]
111+
assert!(index < 4);
111112

112-
#[cfg(riscv64)]
113-
assert!(index < 8);
113+
#[cfg(riscv64)]
114+
assert!(index < 8);
114115

115-
_write((range as usize) << (3 + (index * 8)));
116-
}
116+
_write((range as usize) << (3 + (index * 8)));
117+
}
117118

118-
#[inline]
119-
pub unsafe fn set_lock(index: usize) {
120-
#[cfg(riscv32)]
121-
assert!(index < 4);
119+
#[inline]
120+
pub unsafe fn set_lock(index: usize) {
121+
#[cfg(riscv32)]
122+
assert!(index < 4);
122123

123-
#[cfg(riscv64)]
124-
assert!(index < 8);
124+
#[cfg(riscv64)]
125+
assert!(index < 8);
125126

126-
_set(1 << (7 + (index * 8)));
127-
}
127+
_set(1 << (7 + (index * 8)));
128+
}
128129

129-
#[inline]
130-
pub unsafe fn clear_lock(index: usize) {
131-
#[cfg(riscv32)]
132-
assert!(index < 4);
130+
#[inline]
131+
pub unsafe fn clear_lock(index: usize) {
132+
#[cfg(riscv32)]
133+
assert!(index < 4);
133134

134-
#[cfg(riscv64)]
135-
assert!(index < 8);
135+
#[cfg(riscv64)]
136+
assert!(index < 8);
136137

137-
_clear(1 << (7 + (index * 8)));
138+
_clear(1 << (7 + (index * 8)));
139+
}
138140
}
139141
}
140142

141143
/// Physical memory protection configuration, RV32 only
144+
/// Pmpcf1 struct contains pmp4cfg - pmp7cfg for 32-bit arch only
145+
/// get_byte() method retrieves a single pmp<x>cfg held in a PmpByte struct
142146
pub mod pmpcfg1 {
147+
use super::PmpByte;
143148

144149
#[derive(Clone, Copy, Debug)]
145150
pub struct Pmpcfg1 {
146151
pub bits: u32,
147152
}
153+
148154
impl Pmpcfg1 {
149155
#[inline]
150-
pub fn get_byte(&self,index:usize) -> PmpByte {
156+
pub fn get_byte(&self, index: usize) -> PmpByte {
151157
PmpByte {
152-
byte: self.bits.get_bits(8 * index..8 * (index + 1)) as u8
158+
byte: self.bits.get_bits(8 * index..8 * (index + 1)) as u8,
153159
}
154160
}
155-
}
156161

157-
read_csr_as_usize_rv32!(0x3A1, __read_pmpcfg1);
158-
write_csr_as_usize_rv32!(0x3A1, __write_pmpcfg1);
162+
read_csr_as_usize_rv32!(0x3A1, __read_pmpcfg1);
163+
write_csr_as_usize_rv32!(0x3A1, __write_pmpcfg1);
164+
165+
#[inline]
166+
pub unsafe fn set_permissions(permission: Permission, index: usize) {
167+
#[cfg(riscv32)]
168+
assert!(index < 4);
169+
170+
#[cfg(riscv64)]
171+
assert!(index < 8);
172+
173+
_write((permission as usize) << (index * 8));
174+
}
175+
176+
#[inline]
177+
pub unsafe fn set_range(range: Range, index: usize) {
178+
#[cfg(riscv32)]
179+
assert!(index < 4);
180+
181+
#[cfg(riscv64)]
182+
assert!(index < 8);
183+
184+
_write((range as usize) << (3 + (index * 8)));
185+
}
186+
187+
#[inline]
188+
pub unsafe fn set_lock(index: usize) {
189+
#[cfg(riscv32)]
190+
assert!(index < 4);
191+
192+
#[cfg(riscv64)]
193+
assert!(index < 8);
194+
195+
_set(1 << (7 + (index * 8)));
196+
}
197+
198+
#[inline]
199+
pub unsafe fn clear_lock(index: usize) {
200+
#[cfg(riscv32)]
201+
assert!(index < 4);
202+
203+
#[cfg(riscv64)]
204+
assert!(index < 8);
205+
206+
_clear(1 << (7 + (index * 8)));
207+
}
208+
}
159209
}
160210

161211
/// Physical memory protection configuration
212+
/// Pmpcf0 struct contains pmp8cfg - pmp11cfg for 32-bit arch, or pmp8cfg - pmp15cfg for 64-bit arch
213+
/// get_byte() method retrieves a single pmp<x>cfg held in a PmpByte struct
162214
pub mod pmpcfg2 {
215+
use super::PmpByte;
163216

164217
#[derive(Clone, Copy, Debug)]
165218
pub struct Pmpcfg2 {
166219
pub bits: u32,
167220
}
168221
impl Pmpcfg2 {
169222
#[inline]
170-
pub fn get_byte(&self,index:usize) -> PmpByte {
223+
pub fn get_byte(&self, index: usize) -> PmpByte {
171224
PmpByte {
172-
byte: self.bits.get_bits(8 * index..8 * (index + 1)) as u8
225+
byte: self.bits.get_bits(8 * index..8 * (index + 1)) as u8,
173226
}
174227
}
175-
}
176228

177-
read_csr_as_usize!(0x3A2, __read_pmpcfg2);
178-
write_csr_as_usize!(0x3A2, __write_pmpcfg2);
229+
read_csr_as_usize!(0x3A2, __read_pmpcfg2);
230+
write_csr_as_usize!(0x3A2, __write_pmpcfg2);
231+
232+
#[inline]
233+
pub unsafe fn set_permissions(permission: Permission, index: usize) {
234+
#[cfg(riscv32)]
235+
assert!(index < 4);
236+
237+
#[cfg(riscv64)]
238+
assert!(index < 8);
239+
240+
_write((permission as usize) << (index * 8));
241+
}
242+
243+
#[inline]
244+
pub unsafe fn set_range(range: Range, index: usize) {
245+
#[cfg(riscv32)]
246+
assert!(index < 4);
247+
248+
#[cfg(riscv64)]
249+
assert!(index < 8);
250+
251+
_write((range as usize) << (3 + (index * 8)));
252+
}
253+
254+
#[inline]
255+
pub unsafe fn set_lock(index: usize) {
256+
#[cfg(riscv32)]
257+
assert!(index < 4);
258+
259+
#[cfg(riscv64)]
260+
assert!(index < 8);
261+
262+
_set(1 << (7 + (index * 8)));
263+
}
264+
265+
#[inline]
266+
pub unsafe fn clear_lock(index: usize) {
267+
#[cfg(riscv32)]
268+
assert!(index < 4);
269+
270+
#[cfg(riscv64)]
271+
assert!(index < 8);
272+
273+
_clear(1 << (7 + (index * 8)));
274+
}
275+
}
179276
}
180277

181278
/// Physical memory protection configuration, RV32 only
279+
/// Pmpcf0 struct contains pmp12cfg - pmp15cfg for 32-bit arch only
280+
/// get_byte() method retrieves a single pmp<x>cfg held in a PmpByte struct
182281
pub mod pmpcfg3 {
282+
use super::PmpByte;
283+
183284
#[derive(Clone, Copy, Debug)]
184285
pub struct Pmpcfg3 {
185286
pub bits: u32,
186287
}
187288
impl Pmpcfg3 {
188289
#[inline]
189-
pub fn get_byte(&self,index:usize) -> PmpByte {
290+
pub fn get_byte(&self, index: usize) -> PmpByte {
190291
PmpByte {
191-
byte: self.bits.get_bits(8 * index..8 * (index + 1)) as u8
292+
byte: self.bits.get_bits(8 * index..8 * (index + 1)) as u8,
192293
}
193294
}
194-
}
195295

196-
read_csr_as_usize_rv32!(0x3A3, __read_pmpcfg3);
197-
write_csr_as_usize_rv32!(0x3A3, __write_pmpcfg3);
296+
read_csr_as_usize_rv32!(0x3A3, __read_pmpcfg3);
297+
write_csr_as_usize_rv32!(0x3A3, __write_pmpcfg3);
298+
299+
#[inline]
300+
pub unsafe fn set_permissions(permission: Permission, index: usize) {
301+
#[cfg(riscv32)]
302+
assert!(index < 4);
303+
304+
#[cfg(riscv64)]
305+
assert!(index < 8);
306+
307+
_write((permission as usize) << (index * 8));
308+
}
309+
310+
#[inline]
311+
pub unsafe fn set_range(range: Range, index: usize) {
312+
#[cfg(riscv32)]
313+
assert!(index < 4);
314+
315+
#[cfg(riscv64)]
316+
assert!(index < 8);
317+
318+
_write((range as usize) << (3 + (index * 8)));
319+
}
320+
321+
#[inline]
322+
pub unsafe fn set_lock(index: usize) {
323+
#[cfg(riscv32)]
324+
assert!(index < 4);
325+
326+
#[cfg(riscv64)]
327+
assert!(index < 8);
328+
329+
_set(1 << (7 + (index * 8)));
330+
}
331+
332+
#[inline]
333+
pub unsafe fn clear_lock(index: usize) {
334+
#[cfg(riscv32)]
335+
assert!(index < 4);
336+
337+
#[cfg(riscv64)]
338+
assert!(index < 8);
339+
340+
_clear(1 << (7 + (index * 8)));
341+
}
342+
}
198343
}

0 commit comments

Comments
 (0)