Skip to content

Commit 7229509

Browse files
committed
Change set_pmp() and clear_pmp() functions to a macro_rule.
1 parent 0caebab commit 7229509

File tree

2 files changed

+45
-95
lines changed

2 files changed

+45
-95
lines changed

src/register/macros.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,40 @@ macro_rules! read_composite_csr {
270270
}
271271
};
272272
}
273+
274+
macro_rules! set_pmp {
275+
{$range:ty, $permission:ty} => {
276+
/// Set the pmp configuration corresponding to the index
277+
#[inline]
278+
pub unsafe fn set_pmp(index: usize, range: $range, permission: $permission, locked: bool) {
279+
#[cfg(riscv32)]
280+
assert!(index < 4);
281+
282+
#[cfg(riscv64)]
283+
assert!(index < 8);
284+
285+
let mut value = _read();
286+
let byte = (locked as usize) << 7 | (range as usize) << 3 | (permission as usize);
287+
value.set_bits(8 * index..=8 * index + 7, byte);
288+
_write(value);
289+
}
290+
};
291+
}
292+
293+
macro_rules! clear_pmp {
294+
() => {
295+
/// Clear the pmp configuration corresponding to the index
296+
#[inline]
297+
pub unsafe fn clear_pmp(index: usize) {
298+
#[cfg(riscv32)]
299+
assert!(index < 4);
300+
301+
#[cfg(riscv64)]
302+
assert!(index < 8);
303+
304+
let mut value = _read();
305+
value.set_bits(8 * index..=8 * index + 7, 0);
306+
_write(value);
307+
}
308+
};
309+
}

src/register/pmpcfgx.rs

Lines changed: 8 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -86,33 +86,8 @@ pub mod pmpcfg0 {
8686
read_csr_as!(Pmpcsr, 0x3A0, __read_pmpcfg0);
8787
write_csr_as_usize!(0x3A0, __write_pmpcfg0);
8888

89-
/// sets a configuration for the defined index in pmpcfg0
90-
pub unsafe fn set_pmp(index: usize, range: Range, permission: Permission, locked: bool) {
91-
#[cfg(riscv32)]
92-
assert!(index < 4);
93-
94-
#[cfg(riscv64)]
95-
assert!(index < 8);
96-
97-
let mut value = _read();
98-
let byte = (locked as usize) << 7 | (range as usize) << 3 | (permission as usize);
99-
value.set_bits(8 * index..=8 * index + 7, byte);
100-
_write(value)
101-
}
102-
103-
/// clears the configuration for the defined index in pmpcfg0
104-
/// cannot clear when pmp is locked
105-
pub unsafe fn clear_pmp(index: usize) {
106-
#[cfg(riscv32)]
107-
assert!(index < 4);
108-
109-
#[cfg(riscv64)]
110-
assert!(index < 8);
111-
112-
let mut value = _read();
113-
value.set_bits(8 * index..=8 * index + 7, 0);
114-
_write(value);
115-
}
89+
set_pmp!(Range,Permission);
90+
clear_pmp!();
11691
}
11792

11893
/// Physical memory protection configuration
@@ -125,25 +100,8 @@ pub mod pmpcfg1 {
125100
read_csr_as!(Pmpcsr, 0x3A1, __read_pmpcfg1);
126101
write_csr_as_usize_rv32!(0x3A1, __write_pmpcfg1);
127102

128-
/// sets a configuration for the defined index in pmpcfg1
129-
pub unsafe fn set_pmp(index: usize, range: Range, permission: Permission, locked: bool) {
130-
assert!(index < 4);
131-
132-
let mut value = _read();
133-
let byte = (locked as usize) << 7 | (range as usize) << 3 | (permission as usize);
134-
value.set_bits(8 * index..=8 * index + 7, byte);
135-
_write(value)
136-
}
137-
138-
/// clears the configuration for the defined index in pmpcfg1
139-
/// cannot clear when pmp is locked
140-
pub unsafe fn clear_pmp(index: usize) {
141-
assert!(index < 4);
142-
143-
let mut value = _read();
144-
value.set_bits(8 * index..=8 * index + 7, 0);
145-
_write(value);
146-
}
103+
set_pmp!(Range,Permission);
104+
clear_pmp!();
147105
}
148106

149107
/// Physical memory protection configuration
@@ -155,33 +113,8 @@ pub mod pmpcfg2 {
155113
read_csr_as!(Pmpcsr, 0x3A2, __read_pmpcfg2);
156114
write_csr_as_usize!(0x3A2, __write_pmpcfg2);
157115

158-
/// sets a configuration for the defined index in pmpcfg2
159-
pub unsafe fn set_pmp(index: usize, range: Range, permission: Permission, locked: bool) {
160-
#[cfg(riscv32)]
161-
assert!(index < 4);
162-
163-
#[cfg(riscv64)]
164-
assert!(index < 8);
165-
166-
let mut value = _read();
167-
let byte = (locked as usize) << 7 | (range as usize) << 3 | (permission as usize);
168-
value.set_bits(8 * index..=8 * index + 7, byte);
169-
_write(value)
170-
}
171-
172-
/// clears the configuration for the defined index in pmpcfg2
173-
/// cannot clear when pmp is locked
174-
pub unsafe fn clear_pmp(index: usize) {
175-
#[cfg(riscv32)]
176-
assert!(index < 4);
177-
178-
#[cfg(riscv64)]
179-
assert!(index < 8);
180-
181-
let mut value = _read();
182-
value.set_bits(8 * index..=8 * index + 7, 0);
183-
_write(value);
184-
}
116+
set_pmp!(Range,Permission);
117+
clear_pmp!();
185118
}
186119

187120
/// Physical memory protection configuration
@@ -194,26 +127,6 @@ pub mod pmpcfg3 {
194127
read_csr_as!(Pmpcsr, 0x3A3, __read_pmpcfg3);
195128
write_csr_as_usize_rv32!(0x3A3, __write_pmpcfg3);
196129

197-
/// sets a configuration for the defined index in pmpcfg3
198-
pub unsafe fn set_pmp(index: usize, range: Range, permission: Permission, locked: bool) {
199-
assert!(index < 4);
200-
201-
let mut value = _read();
202-
let byte = (locked as usize) << 7 | (range as usize) << 3 | (permission as usize);
203-
value.set_bits(8 * index..=8 * index + 7, byte);
204-
_write(value)
205-
}
206-
/// clears the configuration for the defined index in pmpcfg3
207-
/// cannot clear when pmp is locked
208-
pub unsafe fn clear_pmp(index: usize) {
209-
#[cfg(riscv32)]
210-
assert!(index < 4);
211-
212-
#[cfg(riscv64)]
213-
assert!(index < 8);
214-
215-
let mut value = _read();
216-
value.set_bits(8 * index..=8 * index + 7, 0);
217-
_write(value);
218-
}
130+
set_pmp!(Range,Permission);
131+
clear_pmp!();
219132
}

0 commit comments

Comments
 (0)