|
1 | 1 | /// Physical memory protection configuration
|
| 2 | +use bit_field::BitField; |
| 3 | + |
| 4 | +/// Permission enum contains all possible permission modes for pmp registers |
| 5 | +#[derive(Clone, Copy, Debug)] |
| 6 | +pub enum Permission { |
| 7 | + NONE = 0b000, |
| 8 | + R = 0b001, |
| 9 | + W = 0b010, |
| 10 | + RW = 0b011, |
| 11 | + X = 0b100, |
| 12 | + RX = 0b101, |
| 13 | + WX = 0b110, |
| 14 | + RWX = 0b111, |
| 15 | +} |
| 16 | + |
| 17 | +/// Range enum contains all possible addressing modes for pmp registers |
| 18 | +#[derive(Clone, Copy, Debug)] |
| 19 | +pub enum Range { |
| 20 | + OFF = 0b00, |
| 21 | + TOR = 0b01, |
| 22 | + NA4 = 0b10, |
| 23 | + NAPOT = 0b11, |
| 24 | +} |
| 25 | + |
| 26 | +/// Pmp struct holds a high-level representation of a single pmp configuration |
| 27 | +#[derive(Clone, Copy, Debug)] |
| 28 | +pub struct Pmp { |
| 29 | + /// raw bits |
| 30 | + pub byte: u8, |
| 31 | + /// Current PMP Permission |
| 32 | + pub permission: Permission, |
| 33 | + /// Current PMP Range |
| 34 | + pub range: Range, |
| 35 | + /// Is PMP locked? |
| 36 | + pub locked: bool, |
| 37 | +} |
| 38 | + |
| 39 | +pub struct Pmpcsr { |
| 40 | + /// Holds the raw contents of a PMP CSR Register |
| 41 | + pub bits: usize, |
| 42 | +} |
| 43 | + |
| 44 | +impl Pmpcsr { |
| 45 | + /// Take the register contents and translate into a Pmp configuration struct |
| 46 | + #[inline] |
| 47 | + pub fn into_config(&self, index: usize) -> Pmp { |
| 48 | + #[cfg(riscv32)] |
| 49 | + assert!(index < 4); |
| 50 | + |
| 51 | + #[cfg(riscv64)] |
| 52 | + assert!(index < 8); |
| 53 | + |
| 54 | + let byte = self.bits.get_bits(8 * index..=8 * index + 7) as u8; |
| 55 | + Pmp { |
| 56 | + byte, |
| 57 | + permission: match byte.get_bits(0..=2) { |
| 58 | + 0 => Permission::NONE, |
| 59 | + 1 => Permission::R, |
| 60 | + 2 => Permission::W, |
| 61 | + 3 => Permission::RW, |
| 62 | + 4 => Permission::X, |
| 63 | + 5 => Permission::RX, |
| 64 | + 6 => Permission::WX, |
| 65 | + 7 => Permission::RWX, |
| 66 | + _ => unreachable!(), |
| 67 | + }, |
| 68 | + range: match byte.get_bits(3..=4) { |
| 69 | + 0 => Range::OFF, |
| 70 | + 1 => Range::TOR, |
| 71 | + 2 => Range::NA4, |
| 72 | + 3 => Range::NAPOT, |
| 73 | + _ => unreachable!(), |
| 74 | + }, |
| 75 | + locked: byte.get_bit(7) as bool, |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/// Physical memory protection configuration |
| 81 | +/// pmpcfg0 struct contains pmp0cfg - pmp3cfg for RV32, and pmp0cfg - pmp7cfg for RV64 |
2 | 82 | pub mod pmpcfg0 {
|
3 |
| - read_csr_as_usize!(0x3A0, __read_pmpcfg0); |
| 83 | + use super::{Permission, Pmpcsr, Range}; |
| 84 | + use bit_field::BitField; |
| 85 | + |
| 86 | + read_csr_as!(Pmpcsr, 0x3A0, __read_pmpcfg0); |
4 | 87 | write_csr_as_usize!(0x3A0, __write_pmpcfg0);
|
| 88 | + |
| 89 | + set_pmp!(); |
| 90 | + clear_pmp!(); |
5 | 91 | }
|
6 | 92 |
|
7 |
| -/// Physical memory protection configuration, RV32 only |
| 93 | +/// Physical memory protection configuration |
| 94 | +/// pmpcfg1 struct contains pmp4cfg - pmp7cfg for RV32 only |
| 95 | +#[cfg(riscv32)] |
8 | 96 | pub mod pmpcfg1 {
|
9 |
| - read_csr_as_usize_rv32!(0x3A1, __read_pmpcfg1); |
| 97 | + use super::{Permission, Pmpcsr, Range}; |
| 98 | + use bit_field::BitField; |
| 99 | + |
| 100 | + read_csr_as!(Pmpcsr, 0x3A1, __read_pmpcfg1); |
10 | 101 | write_csr_as_usize_rv32!(0x3A1, __write_pmpcfg1);
|
| 102 | + |
| 103 | + set_pmp!(); |
| 104 | + clear_pmp!(); |
11 | 105 | }
|
12 | 106 |
|
13 | 107 | /// Physical memory protection configuration
|
| 108 | +/// pmpcfg2 struct contains pmp8cfg - pmp11cfg for RV32, or pmp8cfg - pmp15cfg for RV64 |
14 | 109 | pub mod pmpcfg2 {
|
15 |
| - read_csr_as_usize!(0x3A2, __read_pmpcfg2); |
| 110 | + use super::{Permission, Pmpcsr, Range}; |
| 111 | + use bit_field::BitField; |
| 112 | + |
| 113 | + read_csr_as!(Pmpcsr, 0x3A2, __read_pmpcfg2); |
16 | 114 | write_csr_as_usize!(0x3A2, __write_pmpcfg2);
|
| 115 | + |
| 116 | + set_pmp!(); |
| 117 | + clear_pmp!(); |
17 | 118 | }
|
18 | 119 |
|
19 |
| -/// Physical memory protection configuration, RV32 only |
| 120 | +/// Physical memory protection configuration |
| 121 | +/// pmpcfg3 struct contains pmp12cfg - pmp15cfg for RV32 only |
| 122 | +#[cfg(riscv32)] |
20 | 123 | pub mod pmpcfg3 {
|
21 |
| - read_csr_as_usize_rv32!(0x3A3, __read_pmpcfg3); |
| 124 | + use super::{Permission, Pmpcsr, Range}; |
| 125 | + use bit_field::BitField; |
| 126 | + |
| 127 | + read_csr_as!(Pmpcsr, 0x3A3, __read_pmpcfg3); |
22 | 128 | write_csr_as_usize_rv32!(0x3A3, __write_pmpcfg3);
|
| 129 | + |
| 130 | + set_pmp!(); |
| 131 | + clear_pmp!(); |
23 | 132 | }
|
0 commit comments