use arbitrary_int::traits::Integer;
use arbitrary_int::{u3, u4};
use bitbybit::bitfield;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct IptOption {
trace_mode: TraceMode,
timing_mode: TimingMode,
mtc_freq: u8,
cyc_thresh: u8,
buffer_size_exp: u8,
match_settings: MatchSettings,
inherit_to_children: bool,
ring_filter: RingFilter,
}
impl Default for IptOption {
fn default() -> Self {
IptOption {
trace_mode: TraceMode::default(),
timing_mode: TimingMode::default(),
mtc_freq: 0,
cyc_thresh: 0,
buffer_size_exp: 8,
match_settings: MatchSettings::default(),
inherit_to_children: false,
ring_filter: RingFilter::default(),
}
}
}
impl IptOption {
#[must_use]
pub const fn with_trace_mode(mut self, trace_mode: TraceMode) -> Self {
self.trace_mode = trace_mode;
self
}
#[must_use]
pub const fn trace_mode(&self) -> TraceMode {
self.trace_mode
}
#[must_use]
pub const fn with_timing_mode(mut self, timing_mode: TimingMode) -> Self {
self.timing_mode = timing_mode;
self
}
#[must_use]
pub const fn timing_mode(&self) -> TimingMode {
self.timing_mode
}
#[must_use]
pub const fn with_mtc_freq(mut self, mtc_freq: u8) -> Option<Self> {
if mtc_freq > MAX_U4 {
return None;
}
self.mtc_freq = mtc_freq;
Some(self)
}
#[must_use]
pub const fn mtc_freq(&self) -> u8 {
self.mtc_freq
}
#[must_use]
pub const fn with_cyc_thresh(mut self, cyc_thresh: u8) -> Option<Self> {
if cyc_thresh > MAX_U4 {
return None;
}
self.cyc_thresh = cyc_thresh;
Some(self)
}
#[must_use]
pub const fn cyc_thresh(&self) -> u8 {
self.cyc_thresh
}
#[must_use]
pub const fn with_buffer_size_exp(mut self, buffer_size_exp: u8) -> Option<Self> {
if buffer_size_exp > MAX_U4 {
return None;
}
self.buffer_size_exp = buffer_size_exp;
Some(self)
}
#[must_use]
pub const fn buffer_size_exp(&self) -> u8 {
self.buffer_size_exp
}
#[must_use]
pub const fn with_match_settings(mut self, match_settings: MatchSettings) -> Self {
self.match_settings = match_settings;
self
}
#[must_use]
pub const fn match_settings(&self) -> MatchSettings {
self.match_settings
}
#[must_use]
pub const fn with_inherit_to_children(mut self, inherit_to_children: bool) -> Self {
self.inherit_to_children = inherit_to_children;
self
}
#[must_use]
pub const fn inherit_to_children(&self) -> bool {
self.inherit_to_children
}
#[must_use]
pub const fn with_ring_filter(mut self, ring_filter: RingFilter) -> Self {
self.ring_filter = ring_filter;
self
}
#[must_use]
pub const fn ring_filter(&self) -> RingFilter {
self.ring_filter
}
}
const MAX_U4: u8 = u4::MAX.value();
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
#[repr(u8)]
pub enum TraceMode {
#[default]
Buffer = 1,
File = 2,
}
impl TraceMode {
const fn from_repr(repr: u8) -> Option<Self> {
match repr {
1 => Some(Self::Buffer),
2 => Some(Self::File),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
#[repr(u8)]
pub enum TimingMode {
#[default]
Off = 0,
Mtc = 1,
MtcCyc = 2,
}
impl TimingMode {
const fn from_repr(repr: u8) -> Option<Self> {
match repr {
0 => Some(Self::Off),
1 => Some(Self::Mtc),
2 => Some(Self::MtcCyc),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
#[repr(u8)]
pub enum MatchSettings {
#[default]
None = 0,
ImageName = 1,
PackageIdentity = 3,
}
impl MatchSettings {
const fn from_repr(repr: u8) -> Option<Self> {
match repr {
0 => Some(Self::None),
1 => Some(Self::ImageName),
3 => Some(Self::PackageIdentity),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
#[repr(u8)]
pub enum RingFilter {
#[default]
User = 0,
Kernel = 1,
KernelUser = 2,
UserSystemWide = 3,
KernelSystemWide = 4,
KernelUserSystemWide = 5,
}
impl RingFilter {
const fn from_repr(repr: u8) -> Option<Self> {
match repr {
0 => Some(Self::User),
1 => Some(Self::Kernel),
2 => Some(Self::KernelUser),
3 => Some(Self::UserSystemWide),
4 => Some(Self::KernelSystemWide),
5 => Some(Self::KernelUserSystemWide),
_ => None,
}
}
}
#[bitfield(u64, default = 0x8_0001)]
#[derive(Debug, PartialEq, Eq)]
pub struct IptOptionRaw {
#[bits(0..=3, rw)]
pub trace_mode: u4,
#[bits(4..=7, rw)]
pub timing_mode: u4,
#[bits(8..=11, rw)]
pub mtc_freq: u4,
#[bits(12..=15, rw)]
pub cyc_thresh: u4,
#[bits(16..=19, rw)]
pub buffer_size_exp: u4,
#[bits(20..=22, rw)]
pub match_settings: u3,
#[bit(23, rw)]
pub inherit_to_children: bool,
#[bits(24..=27, rw)]
pub ring_filter: u4,
#[bits(28..=63)]
_reserved: u36,
}
impl From<IptOption> for IptOptionRaw {
fn from(value: IptOption) -> Self {
IptOptionRaw::builder()
.with_trace_mode(u4::new(value.trace_mode as u8))
.with_timing_mode(u4::new(value.timing_mode as u8))
.with_mtc_freq(u4::new(value.mtc_freq))
.with_cyc_thresh(u4::new(value.cyc_thresh))
.with_buffer_size_exp(u4::new(value.buffer_size_exp))
.with_match_settings(u3::new(value.match_settings as u8))
.with_inherit_to_children(value.inherit_to_children)
.with_ring_filter(u4::new(value.ring_filter as u8))
.build()
}
}
impl TryFrom<IptOptionRaw> for IptOption {
type Error = ();
fn try_from(value: IptOptionRaw) -> Result<Self, Self::Error> {
Ok(IptOption {
trace_mode: TraceMode::from_repr(value.trace_mode().value()).ok_or(())?,
timing_mode: TimingMode::from_repr(value.timing_mode().value()).ok_or(())?,
mtc_freq: value.mtc_freq().value(),
cyc_thresh: value.cyc_thresh().value(),
buffer_size_exp: value.buffer_size_exp().value(),
match_settings: MatchSettings::from_repr(value.match_settings().value()).ok_or(())?,
inherit_to_children: value.inherit_to_children(),
ring_filter: RingFilter::from_repr(value.ring_filter().value()).ok_or(())?,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default() {
let default: IptOptionRaw = IptOption::default().into();
let raw_default = IptOptionRaw::default();
assert_eq!(default, raw_default);
}
#[test]
fn size_matches_msr_width() {
assert_eq!(size_of::<IptOptionRaw>(), 8);
}
#[test]
fn field_round_trip() {
let opt = IptOption::default()
.with_trace_mode(TraceMode::File)
.with_timing_mode(TimingMode::MtcCyc)
.with_mtc_freq(3)
.unwrap()
.with_cyc_thresh(15)
.unwrap()
.with_buffer_size_exp(12)
.unwrap()
.with_match_settings(MatchSettings::PackageIdentity)
.with_inherit_to_children(true)
.with_ring_filter(RingFilter::KernelUser);
assert_eq!(opt.trace_mode(), TraceMode::File);
assert_eq!(opt.timing_mode(), TimingMode::MtcCyc);
assert_eq!(opt.mtc_freq(), 3);
assert_eq!(opt.cyc_thresh(), 15);
assert_eq!(opt.buffer_size_exp(), 12);
assert_eq!(opt.match_settings(), MatchSettings::PackageIdentity);
assert!(opt.inherit_to_children());
assert_eq!(opt.ring_filter(), RingFilter::KernelUser);
let raw: IptOptionRaw = opt.into();
assert_eq!(raw.trace_mode(), u4::new(TraceMode::File as u8));
assert_eq!(raw.timing_mode(), u4::new(TimingMode::MtcCyc as u8));
assert_eq!(raw.mtc_freq(), u4::new(3));
assert_eq!(raw.cyc_thresh(), u4::new(15));
assert_eq!(raw.buffer_size_exp(), u4::new(12));
assert_eq!(
raw.match_settings(),
u3::new(MatchSettings::PackageIdentity as u8)
);
assert!(raw.inherit_to_children());
assert_eq!(raw.ring_filter(), u4::new(RingFilter::KernelUser as u8));
}
#[test]
fn raw_round_trip() {
let opt = IptOption::default()
.with_trace_mode(TraceMode::File)
.with_timing_mode(TimingMode::MtcCyc)
.with_mtc_freq(3)
.unwrap()
.with_ring_filter(RingFilter::KernelUserSystemWide);
assert_eq!(IptOption::try_from(IptOptionRaw::from(opt)), Ok(opt));
}
#[test]
fn raw_with_unknown_discriminants_is_rejected() {
let zeroed = IptOptionRaw::new_with_raw_value(0);
assert_eq!(IptOption::try_from(zeroed), Err(()));
let bad_ring_filter = IptOptionRaw::from(IptOption::default()).with_ring_filter(u4::new(6));
assert_eq!(IptOption::try_from(bad_ring_filter), Err(()));
let inert_match_settings =
IptOptionRaw::from(IptOption::default()).with_match_settings(u3::new(2));
assert_eq!(IptOption::try_from(inert_match_settings), Err(()));
}
#[test]
fn u8_setters_reject_out_of_range() {
assert!(IptOption::default().with_mtc_freq(16).is_none());
assert!(IptOption::default().with_cyc_thresh(16).is_none());
assert!(IptOption::default().with_buffer_size_exp(16).is_none());
assert!(IptOption::default().with_mtc_freq(15).is_some());
assert!(IptOption::default().with_cyc_thresh(15).is_some());
assert!(IptOption::default().with_buffer_size_exp(15).is_some());
}
}