1
1
/// Physical memory protection configuration
2
2
use bit_field:: BitField ;
3
3
4
- /*************************************************************************************************/
5
4
/// Permission enum contains all possible permission modes for pmp registers
6
5
#[ derive( Clone , Copy , Debug ) ]
7
6
pub enum Permission {
@@ -23,7 +22,7 @@ pub enum Range {
23
22
NA4 = 2 ,
24
23
NAPOT = 3 ,
25
24
}
26
- /*************************************************************************************************/
25
+
27
26
/// PmpByte holds the a single pmp configuration
28
27
#[ derive( Clone , Copy , Debug ) ]
29
28
pub struct PmpByte {
@@ -33,21 +32,24 @@ pub struct PmpByte {
33
32
//locked: bool
34
33
}
35
34
/// PmpByte methods to get a pmp configuration attributes
36
- impl PmpByte {
35
+ impl PmpByte {
36
+ #[ inline]
37
37
pub fn is_locked ( & self ) -> bool {
38
38
self . byte . get_bit ( 7 )
39
39
}
40
40
41
+ #[ inline]
41
42
pub fn get_range ( & self ) -> Option < Range > {
42
43
match self . byte . get_bits ( 4 ..=5 ) {
43
44
0 => Some ( Range :: OFF ) ,
44
45
1 => Some ( Range :: TOR ) ,
45
46
2 => Some ( Range :: NA4 ) ,
46
47
3 => Some ( Range :: NAPOT ) ,
47
- _ => None ,
48
+ _ => unreachable ! ( ) ,
48
49
}
49
50
}
50
51
52
+ #[ inline]
51
53
pub fn get_permission ( & self ) -> Option < Permission > {
52
54
match self . byte . get_bits ( 0 ..=2 ) {
53
55
0 => Some ( Permission :: NONE ) ,
@@ -58,141 +60,284 @@ impl PmpByte{
58
60
5 => Some ( Permission :: RX ) ,
59
61
6 => Some ( Permission :: WX ) ,
60
62
7 => Some ( Permission :: RWX ) ,
61
- _ => None ,
63
+ _ => unreachable ! ( ) ,
62
64
}
63
65
}
64
66
}
65
- /*************************************************************************************************/
66
67
67
68
/// 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
68
71
pub mod pmpcfg0 {
72
+ use super :: PmpByte ;
69
73
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
72
74
#[ derive( Clone , Copy , Debug ) ]
73
75
pub struct Pmpcfg0 {
74
76
pub bits : u32 ,
75
77
}
76
78
impl Pmpcfg0 {
77
79
#[ inline]
78
- pub fn get_byte ( & self , index : usize ) -> PmpByte {
80
+ pub fn get_byte ( & self , index : usize ) -> PmpByte {
79
81
#[ cfg( riscv32) ]
80
82
assert ! ( index < 4 ) ;
81
83
82
84
#[ cfg( riscv64) ]
83
85
assert ! ( index < 8 ) ;
84
86
85
87
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 ,
87
89
}
88
90
}
89
- }
90
91
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) ;
95
96
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 ) ;
100
101
101
- #[ cfg( riscv64) ]
102
- assert ! ( index < 8 ) ;
102
+ #[ cfg( riscv64) ]
103
+ assert ! ( index < 8 ) ;
103
104
104
- _write ( ( permission as usize ) << ( index * 8 ) ) ;
105
- }
105
+ _write ( ( permission as usize ) << ( index * 8 ) ) ;
106
+ }
106
107
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 ) ;
111
112
112
- #[ cfg( riscv64) ]
113
- assert ! ( index < 8 ) ;
113
+ #[ cfg( riscv64) ]
114
+ assert ! ( index < 8 ) ;
114
115
115
- _write ( ( range as usize ) << ( 3 + ( index * 8 ) ) ) ;
116
- }
116
+ _write ( ( range as usize ) << ( 3 + ( index * 8 ) ) ) ;
117
+ }
117
118
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 ) ;
122
123
123
- #[ cfg( riscv64) ]
124
- assert ! ( index < 8 ) ;
124
+ #[ cfg( riscv64) ]
125
+ assert ! ( index < 8 ) ;
125
126
126
- _set ( 1 << ( 7 + ( index * 8 ) ) ) ;
127
- }
127
+ _set ( 1 << ( 7 + ( index * 8 ) ) ) ;
128
+ }
128
129
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 ) ;
133
134
134
- #[ cfg( riscv64) ]
135
- assert ! ( index < 8 ) ;
135
+ #[ cfg( riscv64) ]
136
+ assert ! ( index < 8 ) ;
136
137
137
- _clear ( 1 << ( 7 + ( index * 8 ) ) ) ;
138
+ _clear ( 1 << ( 7 + ( index * 8 ) ) ) ;
139
+ }
138
140
}
139
141
}
140
142
141
143
/// 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
142
146
pub mod pmpcfg1 {
147
+ use super :: PmpByte ;
143
148
144
149
#[ derive( Clone , Copy , Debug ) ]
145
150
pub struct Pmpcfg1 {
146
151
pub bits : u32 ,
147
152
}
153
+
148
154
impl Pmpcfg1 {
149
155
#[ inline]
150
- pub fn get_byte ( & self , index : usize ) -> PmpByte {
156
+ pub fn get_byte ( & self , index : usize ) -> PmpByte {
151
157
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 ,
153
159
}
154
160
}
155
- }
156
161
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
+ }
159
209
}
160
210
161
211
/// 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
162
214
pub mod pmpcfg2 {
215
+ use super :: PmpByte ;
163
216
164
217
#[ derive( Clone , Copy , Debug ) ]
165
218
pub struct Pmpcfg2 {
166
219
pub bits : u32 ,
167
220
}
168
221
impl Pmpcfg2 {
169
222
#[ inline]
170
- pub fn get_byte ( & self , index : usize ) -> PmpByte {
223
+ pub fn get_byte ( & self , index : usize ) -> PmpByte {
171
224
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 ,
173
226
}
174
227
}
175
- }
176
228
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
+ }
179
276
}
180
277
181
278
/// 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
182
281
pub mod pmpcfg3 {
282
+ use super :: PmpByte ;
283
+
183
284
#[ derive( Clone , Copy , Debug ) ]
184
285
pub struct Pmpcfg3 {
185
286
pub bits : u32 ,
186
287
}
187
288
impl Pmpcfg3 {
188
289
#[ inline]
189
- pub fn get_byte ( & self , index : usize ) -> PmpByte {
290
+ pub fn get_byte ( & self , index : usize ) -> PmpByte {
190
291
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 ,
192
293
}
193
294
}
194
- }
195
295
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
+ }
198
343
}
0 commit comments