1
1
/// Physical memory protection configuration
2
2
use bit_field:: BitField ;
3
3
4
- /// Permission enum contains all possible pmp register permission configurations
4
+ /*************************************************************************************************/
5
+ /// Permission enum contains all possible permission modes for pmp registers
5
6
#[ derive( Clone , Copy , Debug ) ]
6
7
pub enum Permission {
7
8
NONE = 0 ,
@@ -22,36 +23,34 @@ pub enum Range {
22
23
NA4 = 2 ,
23
24
NAPOT = 3 ,
24
25
}
25
-
26
- /// Pmpconfig struct to hold pmp register's current settings
27
- #[ derive( Clone , Copy , Debug ) ]
28
- pub struct Pmpconfig {
29
- pub permission : Permission ,
30
- pub range_type : Range ,
31
- pub locked : bool ,
32
- }
33
-
26
+ /*************************************************************************************************/
27
+ /// PmpByte holds the a single pmp configuration
34
28
#[ derive( Clone , Copy , Debug ) ]
35
29
pub struct PmpByte {
36
- byte : u8 ,
30
+ pub byte : u8 ,
31
+ //permission: Option<Permission>,
32
+ //range: Option<Range>,
33
+ //locked: bool
37
34
}
35
+ /// PmpByte methods to get a pmp configuration attributes
36
+ impl PmpByte {
37
+ pub fn is_locked ( & self ) -> bool {
38
+ self . byte . get_bit ( 7 )
39
+ }
38
40
39
- impl PmpByte {
40
- #[ inline]
41
- fn range ( & self ) -> Range {
41
+ pub fn get_range ( & self ) -> Option < Range > {
42
42
match self . byte . get_bits ( 4 ..=5 ) {
43
- 0 => Range :: OFF ,
44
- 1 => Range :: TOR ,
45
- 2 => Range :: NA4 ,
46
- 3 => Range :: NAPOT ,
47
- _ => unreachable ! ( ) ,
43
+ 0 => Some ( Range :: OFF ) ,
44
+ 1 => Some ( Range :: TOR ) ,
45
+ 2 => Some ( Range :: NA4 ) ,
46
+ 3 => Some ( Range :: NAPOT ) ,
47
+ _ => None ,
48
48
}
49
49
}
50
50
51
- #[ inline]
52
- fn permission ( & self ) -> Option < Permission > {
51
+ pub fn get_permission ( & self ) -> Option < Permission > {
53
52
match self . byte . get_bits ( 0 ..=2 ) {
54
- 0 => None ,
53
+ 0 => Some ( Permission :: NONE ) ,
55
54
1 => Some ( Permission :: R ) ,
56
55
2 => Some ( Permission :: W ) ,
57
56
3 => Some ( Permission :: RW ) ,
@@ -62,47 +61,29 @@ impl PmpByte {
62
61
_ => None ,
63
62
}
64
63
}
65
-
66
- #[ inline]
67
- fn locked ( & self ) -> bool {
68
- self . byte . get_bit ( 7 )
69
- }
70
64
}
65
+ /*************************************************************************************************/
66
+
67
+ /// Physical memory protection configuration
71
68
pub mod pmpcfg0 {
72
- use super :: { PmpByte , Pmpconfig , Permission , Range , BitField } ;
73
69
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
74
72
#[ derive( Clone , Copy , Debug ) ]
75
73
pub struct Pmpcfg0 {
76
- bits : usize ,
74
+ pub bits : u32 ,
77
75
}
78
-
79
76
impl Pmpcfg0 {
80
- ///Returns the pmp byte associated with the index
81
77
#[ inline]
82
- fn pmp_byte ( & self , index : usize ) -> PmpByte {
78
+ pub fn get_byte ( & self , index : usize ) -> PmpByte {
83
79
#[ cfg( riscv32) ]
84
80
assert ! ( index < 4 ) ;
85
81
86
82
#[ cfg( riscv64) ]
87
83
assert ! ( index < 8 ) ;
88
84
89
85
PmpByte {
90
- byte : self . bits . get_bits ( 8 * index..8 * ( index + 1 ) ) as u8 ,
91
- }
92
- }
93
-
94
- ///Returns pmpxcfg configuration structure
95
- #[ inline]
96
- pub fn pmp_cfg ( & self , index : usize ) -> Pmpconfig {
97
- let byte = self . pmp_byte ( index) ;
98
- let p = byte. permission ( ) . unwrap ( ) ;
99
- let r = byte. range ( ) ;
100
- let l = byte. locked ( ) ;
101
-
102
- Pmpconfig {
103
- permission : p,
104
- range_type : r,
105
- locked : l,
86
+ byte : self . bits . get_bits ( 8 * index..8 * ( index + 1 ) ) as u8
106
87
}
107
88
}
108
89
}
@@ -159,18 +140,59 @@ pub mod pmpcfg0 {
159
140
160
141
/// Physical memory protection configuration, RV32 only
161
142
pub mod pmpcfg1 {
143
+
144
+ #[ derive( Clone , Copy , Debug ) ]
145
+ pub struct Pmpcfg1 {
146
+ pub bits : u32 ,
147
+ }
148
+ impl Pmpcfg1 {
149
+ #[ inline]
150
+ pub fn get_byte ( & self , index : usize ) -> PmpByte {
151
+ PmpByte {
152
+ byte : self . bits . get_bits ( 8 * index..8 * ( index + 1 ) ) as u8
153
+ }
154
+ }
155
+ }
156
+
162
157
read_csr_as_usize_rv32 ! ( 0x3A1 , __read_pmpcfg1) ;
163
158
write_csr_as_usize_rv32 ! ( 0x3A1 , __write_pmpcfg1) ;
164
159
}
165
160
166
161
/// Physical memory protection configuration
167
162
pub mod pmpcfg2 {
163
+
164
+ #[ derive( Clone , Copy , Debug ) ]
165
+ pub struct Pmpcfg2 {
166
+ pub bits : u32 ,
167
+ }
168
+ impl Pmpcfg2 {
169
+ #[ inline]
170
+ pub fn get_byte ( & self , index : usize ) -> PmpByte {
171
+ PmpByte {
172
+ byte : self . bits . get_bits ( 8 * index..8 * ( index + 1 ) ) as u8
173
+ }
174
+ }
175
+ }
176
+
168
177
read_csr_as_usize ! ( 0x3A2 , __read_pmpcfg2) ;
169
178
write_csr_as_usize ! ( 0x3A2 , __write_pmpcfg2) ;
170
179
}
171
180
172
181
/// Physical memory protection configuration, RV32 only
173
182
pub mod pmpcfg3 {
183
+ #[ derive( Clone , Copy , Debug ) ]
184
+ pub struct Pmpcfg3 {
185
+ pub bits : u32 ,
186
+ }
187
+ impl Pmpcfg3 {
188
+ #[ inline]
189
+ pub fn get_byte ( & self , index : usize ) -> PmpByte {
190
+ PmpByte {
191
+ byte : self . bits . get_bits ( 8 * index..8 * ( index + 1 ) ) as u8
192
+ }
193
+ }
194
+ }
195
+
174
196
read_csr_as_usize_rv32 ! ( 0x3A3 , __read_pmpcfg3) ;
175
197
write_csr_as_usize_rv32 ! ( 0x3A3 , __write_pmpcfg3) ;
176
198
}
0 commit comments