@@ -4,74 +4,69 @@ use bit_field::BitField;
4
4
/// Permission enum contains all possible permission modes for pmp registers
5
5
#[ derive( Clone , Copy , Debug ) ]
6
6
pub enum Permission {
7
- NONE = 0 ,
8
- R = 1 ,
9
- W = 2 ,
10
- RW = 3 ,
11
- X = 4 ,
12
- RX = 5 ,
13
- WX = 6 ,
14
- RWX = 7 ,
7
+ NONE = 0b_000 ,
8
+ R = 0b_001 ,
9
+ W = 0b_010 ,
10
+ RW = 0b_011 ,
11
+ X = 0b_100 ,
12
+ RX = 0b_101 ,
13
+ WX = 0b_110 ,
14
+ RWX = 0b_111 ,
15
15
}
16
16
17
17
/// Range enum contains all possible addressing modes for pmp registers
18
18
#[ derive( Clone , Copy , Debug ) ]
19
19
pub enum Range {
20
- OFF = 0 ,
21
- TOR = 1 ,
22
- NA4 = 2 ,
23
- NAPOT = 3 ,
20
+ OFF = 0b_00 ,
21
+ TOR = 0b_01 ,
22
+ NA4 = 0b_10 ,
23
+ NAPOT = 0b_11 ,
24
24
}
25
25
26
26
#[ derive( Clone , Copy , Debug ) ]
27
27
pub struct Pmpcfg {
28
28
pub bits : usize ,
29
29
}
30
30
31
+ /// PmpByte holds the a single pmp configuration
32
+ #[ derive( Clone , Copy , Debug ) ]
33
+ pub struct PmpEntry {
34
+ pub byte : usize ,
35
+ pub index : usize ,
36
+ pub permission : Option < Permission > ,
37
+ pub range : Option < Range > ,
38
+ pub locked : bool
39
+ }
40
+
31
41
impl Pmpcfg {
32
42
#[ inline]
33
- pub fn get_byte ( & self , index : usize ) -> PmpByte {
43
+ pub fn get_config ( & self , index : usize ) -> PmpEntry {
34
44
#[ cfg( riscv32) ]
35
45
assert ! ( index < 4 ) ;
36
46
37
47
#[ cfg( riscv64) ]
38
48
assert ! ( index < 8 ) ;
39
49
40
- PmpByte {
41
- byte : self . bits . get_bits ( 8 * index..8 * ( index + 1 ) ) as u8 ,
50
+ PmpEntry {
51
+ byte : self . get_byte ( index) ,
52
+ index,
53
+ permission : self . get_permission ( index) ,
54
+ range : self . get_range ( index) ,
55
+ locked : self . is_locked ( index)
42
56
}
43
57
}
44
- }
45
58
46
- /// PmpByte holds the a single pmp configuration
47
- #[ derive( Clone , Copy , Debug ) ]
48
- pub struct PmpByte {
49
- pub byte : u8 ,
50
- //permission: Option<Permission>,
51
- //range: Option<Range>,
52
- //locked: bool
53
- }
54
- /// PmpByte methods to get a pmp configuration attributes
55
- impl PmpByte {
56
- #[ inline]
57
- pub fn is_locked ( & self ) -> bool {
58
- self . byte . get_bit ( 7 )
59
- }
59
+ /// PmpByte methods to get a pmp configuration attributes
60
+ pub fn get_byte ( & self , index : usize ) -> usize { self . bits . get_bits ( 8 * index..=( 8 * index) + 7 ) as usize }
60
61
61
62
#[ inline]
62
- pub fn get_range ( & self ) -> Option < Range > {
63
- match self . byte . get_bits ( 3 ..=4 ) {
64
- 0 => Some ( Range :: OFF ) ,
65
- 1 => Some ( Range :: TOR ) ,
66
- 2 => Some ( Range :: NA4 ) ,
67
- 3 => Some ( Range :: NAPOT ) ,
68
- _ => unreachable ! ( ) ,
69
- }
63
+ pub fn is_locked ( & self , index : usize ) -> bool {
64
+ self . bits . get_bit ( 7 + ( 8 * index) )
70
65
}
71
66
72
67
#[ inline]
73
- pub fn get_permission ( & self ) -> Option < Permission > {
74
- match self . byte . get_bits ( 0 ..=2 ) {
68
+ pub fn get_permission ( & self , index : usize ) -> Option < Permission > {
69
+ match self . bits . get_bits ( 8 * index ..=8 * index + 2 ) {
75
70
0 => Some ( Permission :: NONE ) ,
76
71
1 => Some ( Permission :: R ) ,
77
72
2 => Some ( Permission :: W ) ,
@@ -83,13 +78,24 @@ impl PmpByte {
83
78
_ => unreachable ! ( ) ,
84
79
}
85
80
}
86
- }
87
81
82
+ #[ inline]
83
+ pub fn get_range ( & self , index : usize ) -> Option < Range > {
84
+ match self . bits . get_bits ( 8 * index + 3 ..=8 * index + 4 ) {
85
+ 0 => Some ( Range :: OFF ) ,
86
+ 1 => Some ( Range :: TOR ) ,
87
+ 2 => Some ( Range :: NA4 ) ,
88
+ 3 => Some ( Range :: NAPOT ) ,
89
+ _ => unreachable ! ( ) ,
90
+ }
91
+ }
92
+ }
88
93
/// Physical memory protection configuration
89
94
/// Pmpcfg0 struct contains pmp0cfg - pmp3cfg for RV32, or pmp0cfg - pmp7cfg for RV64
90
95
/// get_byte() method retrieves a single pmp<x>cfg held in a PmpByte struct
91
96
pub mod pmpcfg0 {
92
97
use super :: { Permission , Pmpcfg , Range } ;
98
+ use bit_field:: BitField ;
93
99
94
100
read_csr_as ! ( Pmpcfg , 0x3A0 , __read_pmpcfg0) ;
95
101
write_csr ! ( 0x3A0 , __write_pmpcfg0) ;
@@ -104,7 +110,9 @@ pub mod pmpcfg0 {
104
110
#[ cfg( riscv64) ]
105
111
assert ! ( index < 8 ) ;
106
112
107
- _set ( ( permission as usize ) << ( index * 8 ) ) ;
113
+ let mut value = _read ( ) ;
114
+ value. set_bits ( 8 * index..=8 * index + 2 , permission as usize ) ;
115
+ _write ( value) ;
108
116
}
109
117
110
118
#[ inline]
@@ -115,7 +123,9 @@ pub mod pmpcfg0 {
115
123
#[ cfg( riscv64) ]
116
124
assert ! ( index < 8 ) ;
117
125
118
- _set ( ( range as usize ) << ( 3 + ( index * 8 ) ) ) ;
126
+ let mut value = _read ( ) ;
127
+ value. set_bits ( 8 * index + 3 ..=8 * index + 4 , range as usize ) ;
128
+ _write ( value) ;
119
129
}
120
130
121
131
#[ inline]
@@ -126,7 +136,7 @@ pub mod pmpcfg0 {
126
136
#[ cfg( riscv64) ]
127
137
assert ! ( index < 8 ) ;
128
138
129
- _set ( 1 << ( 7 + ( index * 8 ) ) ) ;
139
+ _set ( 1 << ( 7 + index * 8 ) ) ;
130
140
}
131
141
}
132
142
@@ -135,6 +145,7 @@ pub mod pmpcfg0 {
135
145
/// get_byte() method retrieves a single pmp<x>cfg held in a PmpByte struct
136
146
pub mod pmpcfg1 {
137
147
use super :: { Permission , Pmpcfg , Range } ;
148
+ use bit_field:: BitField ;
138
149
139
150
read_csr_as ! ( Pmpcfg , 0x3A1 , __read_pmpcfg1) ;
140
151
write_csr ! ( 0x3A1 , __write_pmpcfg1) ;
@@ -149,7 +160,9 @@ pub mod pmpcfg1 {
149
160
#[ cfg( riscv64) ]
150
161
assert ! ( index < 8 ) ;
151
162
152
- _set ( ( permission as usize ) << ( index * 8 ) ) ;
163
+ let mut value = _read ( ) ;
164
+ value. set_bits ( 8 * index..=8 * index + 2 , permission as usize ) ;
165
+ _write ( value) ;
153
166
}
154
167
155
168
#[ inline]
@@ -160,7 +173,9 @@ pub mod pmpcfg1 {
160
173
#[ cfg( riscv64) ]
161
174
assert ! ( index < 8 ) ;
162
175
163
- _set ( ( range as usize ) << ( 3 + ( index * 8 ) ) ) ;
176
+ let mut value = _read ( ) ;
177
+ value. set_bits ( 8 * index + 3 ..=8 * index + 4 , range as usize ) ;
178
+ _write ( value) ;
164
179
}
165
180
166
181
#[ inline]
@@ -171,7 +186,7 @@ pub mod pmpcfg1 {
171
186
#[ cfg( riscv64) ]
172
187
assert ! ( index < 8 ) ;
173
188
174
- _set ( 1 << ( 7 + ( index * 8 ) ) ) ;
189
+ _set ( 1 << ( 7 + index * 8 ) ) ;
175
190
}
176
191
}
177
192
@@ -180,6 +195,7 @@ pub mod pmpcfg1 {
180
195
/// get_byte() method retrieves a single pmp<x>cfg held in a PmpByte struct
181
196
pub mod pmpcfg2 {
182
197
use super :: { Permission , Pmpcfg , Range } ;
198
+ use bit_field:: BitField ;
183
199
184
200
read_csr_as ! ( Pmpcfg , 0x3A2 , __read_pmpcfg2) ;
185
201
write_csr ! ( 0x3A2 , __write_pmpcfg2) ;
@@ -194,7 +210,9 @@ pub mod pmpcfg2 {
194
210
#[ cfg( riscv64) ]
195
211
assert ! ( index < 8 ) ;
196
212
197
- _set ( ( permission as usize ) << ( index * 8 ) ) ;
213
+ let mut value = _read ( ) ;
214
+ value. set_bits ( 8 * index..=8 * index + 2 , permission as usize ) ;
215
+ _write ( value) ;
198
216
}
199
217
200
218
#[ inline]
@@ -205,7 +223,9 @@ pub mod pmpcfg2 {
205
223
#[ cfg( riscv64) ]
206
224
assert ! ( index < 8 ) ;
207
225
208
- _set ( ( range as usize ) << ( 3 + ( index * 8 ) ) ) ;
226
+ let mut value = _read ( ) ;
227
+ value. set_bits ( 8 * index + 3 ..=8 * index + 4 , range as usize ) ;
228
+ _write ( value) ;
209
229
}
210
230
211
231
#[ inline]
@@ -216,7 +236,7 @@ pub mod pmpcfg2 {
216
236
#[ cfg( riscv64) ]
217
237
assert ! ( index < 8 ) ;
218
238
219
- _set ( 1 << ( 7 + ( index * 8 ) ) ) ;
239
+ _set ( 1 << ( 7 + index * 8 ) ) ;
220
240
}
221
241
}
222
242
@@ -225,6 +245,7 @@ pub mod pmpcfg2 {
225
245
/// get_byte() method retrieves a single pmp<x>cfg held in a PmpByte struct
226
246
pub mod pmpcfg3 {
227
247
use super :: { Permission , Pmpcfg , Range } ;
248
+ use bit_field:: BitField ;
228
249
229
250
read_csr_as ! ( Pmpcfg , 0x3A3 , __read_pmpcfg3) ;
230
251
write_csr ! ( 0x3A3 , __write_pmpcfg3) ;
@@ -239,7 +260,9 @@ pub mod pmpcfg3 {
239
260
#[ cfg( riscv64) ]
240
261
assert ! ( index < 8 ) ;
241
262
242
- _set ( ( permission as usize ) << ( index * 8 ) ) ;
263
+ let mut value = _read ( ) ;
264
+ value. set_bits ( 8 * index..=8 * index + 2 , permission as usize ) ;
265
+ _write ( value) ;
243
266
}
244
267
245
268
#[ inline]
@@ -250,7 +273,9 @@ pub mod pmpcfg3 {
250
273
#[ cfg( riscv64) ]
251
274
assert ! ( index < 8 ) ;
252
275
253
- _set ( ( range as usize ) << ( 3 + ( index * 8 ) ) ) ;
276
+ let mut value = _read ( ) ;
277
+ value. set_bits ( 8 * index + 3 ..=8 * index + 4 , range as usize ) ;
278
+ _write ( value) ;
254
279
}
255
280
256
281
#[ inline]
@@ -261,6 +286,6 @@ pub mod pmpcfg3 {
261
286
#[ cfg( riscv64) ]
262
287
assert ! ( index < 8 ) ;
263
288
264
- _set ( 1 << ( 7 + ( index * 8 ) ) ) ;
289
+ _set ( 1 << ( 7 + index * 8 ) ) ;
265
290
}
266
291
}
0 commit comments