1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//! mcounteren register
use crate::result::{Error, Result};
read_write_csr! {
/// `mcounteren` register
Mcounteren: 0x306,
mask: 0xffff_ffff,
}
read_write_csr_field! {
Mcounteren,
/// Supervisor "cycle\[h\]" Enable
cy: 0,
}
read_write_csr_field! {
Mcounteren,
/// Supervisor "time\[h\]" Enable
tm: 1,
}
read_write_csr_field! {
Mcounteren,
/// Supervisor "instret\[h\]" Enable
ir: 2,
}
read_write_csr_field! {
Mcounteren,
/// Supervisor "hpm\[x\]" Enable (bits 3-31)
hpm: 3..=31,
}
set!(0x306);
clear!(0x306);
set_clear_csr!(
/// Supervisor cycle Enable
, set_cy, clear_cy, 1 << 0);
set_clear_csr!(
/// Supervisor time Enable
, set_tm, clear_tm, 1 << 1);
set_clear_csr!(
/// Supervisor instret Enable
, set_ir, clear_ir, 1 << 2);
/// Enables the "hpm\[X\]" counter.
///
/// Updates the `mcounteren` register.
///
/// **WARNING**: panics on:
///
/// - non-`riscv` targets
/// - `index` out-of-bounds
#[inline]
pub unsafe fn set_hpm(index: usize) {
try_set_hpm(index).unwrap();
}
/// Attempts to enable the "hpm\[X\]" counter.
///
/// Updates the `mcounteren` register.
#[inline]
pub unsafe fn try_set_hpm(index: usize) -> Result<()> {
if (3..32).contains(&index) {
_try_set(1 << index)
} else {
Err(Error::IndexOutOfBounds {
index,
min: 3,
max: 31,
})
}
}
/// Disables the "hpm\[X\]" counter.
///
/// Updates the `mcounteren` register.
///
/// **WARNING**: panics on:
///
/// - non-`riscv` targets
/// - `index` out-of-bounds
#[inline]
pub unsafe fn clear_hpm(index: usize) {
try_clear_hpm(index).unwrap();
}
/// Attempts to disable the "hpm\[X\]" counter.
///
/// Updates the `mcounteren` register.
#[inline]
pub unsafe fn try_clear_hpm(index: usize) -> Result<()> {
if (3..32).contains(&index) {
_try_clear(1 << index)
} else {
Err(Error::IndexOutOfBounds {
index,
min: 3,
max: 31,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_mcounteren() {
let mut m = Mcounteren { bits: 0 };
test_csr_field!(m, cy);
test_csr_field!(m, tm);
test_csr_field!(m, ir);
(3..32).for_each(|i| test_csr_field!(m, hpm, i));
(0..3).chain(32..64).for_each(|index| {
test_csr_field!(
m,
hpm,
index,
Error::IndexOutOfBounds {
index,
min: 3,
max: 31
}
)
});
}
}