|
1 | 1 | /// Physical memory protection configuration
|
| 2 | +
|
2 | 3 | pub mod pmpcfg0 {
|
3 |
| - read_csr_as_usize!(0x3A0, __read_pmpcfg0); |
4 |
| - write_csr_as_usize!(0x3A0, __write_pmpcfg0); |
| 4 | + use bit_field::BitField; |
| 5 | + |
| 6 | + #[derive(Clone, Copy, Debug)] |
| 7 | + pub enum Permission { |
| 8 | + NONE = 0, |
| 9 | + R = 1, |
| 10 | + W = 2, |
| 11 | + RW = 3, |
| 12 | + X = 4, |
| 13 | + RX = 5, |
| 14 | + WX = 6, |
| 15 | + RWX = 7, |
| 16 | + } |
| 17 | + |
| 18 | + #[derive(Clone, Copy, Debug)] |
| 19 | + pub enum Range { |
| 20 | + OFF = 0, |
| 21 | + TOR = 1, |
| 22 | + NA4 = 2, |
| 23 | + NAPOT = 3, |
| 24 | + } |
| 25 | + |
| 26 | + #[derive(Clone, Copy, Debug)] |
| 27 | + pub struct Pmpconfig { |
| 28 | + pub permission: Permission, |
| 29 | + pub range_type: Range, |
| 30 | + pub locked: bool, |
| 31 | + } |
| 32 | + |
| 33 | + #[derive(Clone, Copy, Debug)] |
| 34 | + pub struct Pmpcfg0 { |
| 35 | + bits: usize, |
| 36 | + } |
| 37 | + |
| 38 | + impl Pmpcfg0 { |
| 39 | + ///Returns the pmp byte associated with the index |
| 40 | + #[inline] |
| 41 | + pub fn pmp_byte(&self, index: usize) -> usize { |
| 42 | + assert!(index < 8); |
| 43 | + match index { |
| 44 | + 0 => self.bits.get_bits(0..8), |
| 45 | + 1 => self.bits.get_bits(8..16), |
| 46 | + 2 => self.bits.get_bits(16..24), |
| 47 | + 3 => self.bits.get_bits(24..32), |
| 48 | + 4 => self.bits.get_bits(32..40), |
| 49 | + 5 => self.bits.get_bits(40..48), |
| 50 | + 6 => self.bits.get_bits(48..56), |
| 51 | + 7 => self.bits.get_bits(56..64), |
| 52 | + _ => panic!(), |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + #[inline] |
| 57 | + fn range(&self, byte: usize) -> Range { |
| 58 | + match byte.get_bits(3..5) { |
| 59 | + 0 => Range::OFF, |
| 60 | + 1 => Range::TOR, |
| 61 | + 2 => Range::NA4, |
| 62 | + 3 => Range::NAPOT, |
| 63 | + _ => panic!(), |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + #[inline] |
| 68 | + fn permission(&self, byte: usize) -> Permission { |
| 69 | + match byte.get_bits(3..5) { |
| 70 | + 0 => Permission::NONE, |
| 71 | + 1 => Permission::R, |
| 72 | + 2 => Permission::W, |
| 73 | + 3 => Permission::RW, |
| 74 | + 4 => Permission::X, |
| 75 | + 5 => Permission::RX, |
| 76 | + 6 => Permission::WX, |
| 77 | + 7 => Permission::RWX, |
| 78 | + _ => panic!(), |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + ///Returns pmp[x]cfg configuration structure |
| 83 | + #[inline] |
| 84 | + pub fn pmp_cfg(&self, index: usize) -> Pmpconfig { |
| 85 | + assert!(index < 8); |
| 86 | + let byte = self.pmp_byte(index); |
| 87 | + let p = self.permission(byte); |
| 88 | + let r = self.range(byte); |
| 89 | + let l = byte.get_bit(7); |
| 90 | + |
| 91 | + Pmpconfig { |
| 92 | + permission: p, |
| 93 | + range_type: r, |
| 94 | + locked: l, |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + read_csr_as!(Pmpcfg0, 0x3A0, __read_pmpcfg0); |
| 100 | + write_csr!(0x3A0, __write_pmpcfg0); |
| 101 | + set!(0x3A0, __set_pmpcfg0); |
| 102 | + clear!(0x3A0, __clear_pmpcfg0); |
| 103 | + |
| 104 | + #[inline] |
| 105 | + pub unsafe fn set_permissions(permission: Permission, index: usize) { |
| 106 | + assert!(index < 8); |
| 107 | + _set((permission as usize) << (index * 8)); |
| 108 | + } |
| 109 | + |
| 110 | + #[inline] |
| 111 | + pub unsafe fn set_range(range: Range, index: usize) { |
| 112 | + assert!(index < 8); |
| 113 | + _set((range as usize) << (3 + (index * 8))); |
| 114 | + } |
| 115 | + |
| 116 | + #[inline] |
| 117 | + pub unsafe fn set_lock(index: usize) { |
| 118 | + assert!(index < 8); |
| 119 | + _set(1 << (7 + (index * 8))); |
| 120 | + } |
| 121 | + |
| 122 | + #[inline] |
| 123 | + pub unsafe fn clear_lock(index: usize) { |
| 124 | + assert!(index < 8); |
| 125 | + _set(1 << (7 + (index * 8))); |
| 126 | + } |
5 | 127 | }
|
6 | 128 |
|
7 | 129 | /// Physical memory protection configuration, RV32 only
|
|
0 commit comments