Skip to content

Commit

Permalink
riscv: add fallible functions to mcounteren
Browse files Browse the repository at this point in the history
Adds fallible access functions for `Mcounteren` HPM fields.
  • Loading branch information
rmsyn committed Jun 28, 2024
1 parent 573a8a5 commit 4d4ef06
Showing 1 changed file with 85 additions and 1 deletion.
86 changes: 85 additions & 1 deletion riscv/src/register/mcounteren.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! mcounteren register

use crate::bits::{bf_extract, bf_insert};
use crate::result::{Error, Result};

/// mcounteren register
#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -58,6 +59,22 @@ impl Mcounteren {
bf_extract(self.bits, index, 1) != 0
}

/// Fallible Supervisor "hpm\[x\]" Enable (bits 3-31).
///
/// Attempts to read the "hpm\[x\]" value, and returns an error if the `index` is invalid.
#[inline]
pub fn try_hpm(&self, index: usize) -> Result<bool> {
if (3..32).contains(&index) {
Ok(bf_extract(self.bits, index, 1) != 0)
} else {
Err(Error::OutOfBounds {
index,
min: 3,
max: 31,
})
}
}

/// Sets whether to enable the "hpm\[X\]" counter.
///
/// Only updates the in-memory value, does not modify the `mcounteren` register.
Expand All @@ -66,6 +83,25 @@ impl Mcounteren {
assert!((3..32).contains(&index));
self.bits = bf_insert(self.bits, index, 1, hpm as usize);
}

/// Sets whether to enable the "hpm\[X\]" counter.
///
/// Only updates the in-memory value, does not modify the `mcounteren` register.
///
/// Attempts to update the "hpm\[x\]" value, and returns an error if the `index` is invalid.
#[inline]
pub fn try_set_hpm(&mut self, index: usize, hpm: bool) -> Result<()> {
if (3..32).contains(&index) {
self.bits = bf_insert(self.bits, index, 1, hpm as usize);
Ok(())
} else {
Err(Error::OutOfBounds {
index,
min: 3,
max: 31,
})
}
}
}

read_csr_as!(Mcounteren, 0x306);
Expand All @@ -91,12 +127,38 @@ pub unsafe fn set_hpm(index: usize) {
_set(1 << index);
}

#[inline]
pub unsafe fn try_set_hpm(index: usize) -> Result<()> {
if (3..32).contains(&index) {
_try_set(1 << index)
} else {
Err(Error::OutOfBounds {
index,
min: 3,
max: 31,
})
}
}

#[inline]
pub unsafe fn clear_hpm(index: usize) {
assert!((3..32).contains(&index));
_clear(1 << index);
}

#[inline]
pub unsafe fn try_clear_hpm(index: usize) -> Result<()> {
if (3..32).contains(&index) {
_try_clear(1 << index)
} else {
Err(Error::OutOfBounds {
index,
min: 3,
max: 31,
})
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -131,12 +193,34 @@ mod tests {

(3..32).for_each(|i| {
assert!(!m.hpm(i));
assert_eq!(m.try_hpm(i), Ok(false));

m.set_hpm(i, true);
assert!(m.hpm(i));

m.set_hpm(i, false);
assert_eq!(m.try_set_hpm(i, false), Ok(()));
assert_eq!(m.try_hpm(i), Ok(false));

assert!(!m.hpm(i));
});

(0..3).chain(32..64).for_each(|index| {
assert_eq!(
m.try_hpm(index),
Err(Error::OutOfBounds {
index,
min: 3,
max: 31
})
);
assert_eq!(
m.try_set_hpm(index, false),
Err(Error::OutOfBounds {
index,
min: 3,
max: 31
})
);
})
}
}

0 comments on commit 4d4ef06

Please sign in to comment.