Skip to content

Commit

Permalink
riscv-pac: add field and field-less error variants
Browse files Browse the repository at this point in the history
Changes current `Invalid*` variants to `InvalidField*` variants to
indicate their use for subfields in an outer struct.

Adds the field-less counterparts for use in a context without subfields.
  • Loading branch information
rmsyn committed Jul 13, 2024
1 parent fea2e0b commit 2b35502
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 76 deletions.
18 changes: 14 additions & 4 deletions riscv-pac/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ pub enum Error {
max: usize,
},
/// Invalid field value.
InvalidValue {
InvalidFieldValue {
field: &'static str,
value: usize,
bitmask: usize,
},
/// Invalid value of a register field that does not match any known variants.
InvalidVariant { field: &'static str, value: usize },
InvalidFieldVariant { field: &'static str, value: usize },
/// Invalid value.
InvalidValue { value: usize, bitmask: usize },
/// Invalid value that does not match any known variants.
InvalidVariant(usize),
/// Unimplemented function or type.
Unimplemented,
}
Expand All @@ -31,17 +35,23 @@ impl fmt::Display for Error {
f,
"out-of-bounds access, index: {index}, min: {min}, max: {max}"
),
Self::InvalidValue {
Self::InvalidFieldValue {
field,
value,
bitmask,
} => write!(
f,
"invalid {field} field value: {value:#x}, valid bitmask: {bitmask:#x}",
),
Self::InvalidVariant { field, value } => {
Self::InvalidFieldVariant { field, value } => {
write!(f, "invalid {field} field variant: {value:#x}")
}
Self::InvalidValue { value, bitmask } => {
write!(f, "invalid value: {value:#x}, valid bitmask: {bitmask:#x}",)
}
Self::InvalidVariant(value) => {
write!(f, "invalid variant: {value:#x}")
}
Self::Unimplemented => write!(f, "unimplemented"),
}
}
Expand Down
15 changes: 3 additions & 12 deletions riscv-peripheral/examples/e310x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ unsafe impl HartIdNumber for HartId {
#[inline]
fn from_number(number: u16) -> Result<Self> {
if number > Self::MAX_HART_ID_NUMBER {
Err(Error::InvalidVariant {
field: "hart_id",
value: number as usize,
})
Err(Error::InvalidVariant(number as usize))
} else {
// SAFETY: valid context number
Ok(unsafe { core::mem::transmute(number) })
Expand Down Expand Up @@ -101,10 +98,7 @@ unsafe impl InterruptNumber for Interrupt {
#[inline]
fn from_number(number: u16) -> Result<Self> {
if number == 0 || number > Self::MAX_INTERRUPT_NUMBER {
Err(Error::InvalidVariant {
field: "interrupt",
value: number as usize,
})
Err(Error::InvalidVariant(number as usize))
} else {
// SAFETY: valid interrupt number
Ok(unsafe { core::mem::transmute(number) })
Expand Down Expand Up @@ -136,10 +130,7 @@ unsafe impl PriorityNumber for Priority {
#[inline]
fn from_number(number: u8) -> Result<Self> {
if number > Self::MAX_PRIORITY_NUMBER {
Err(Error::InvalidVariant {
field: "priority",
value: number as usize,
})
Err(Error::InvalidVariant(number as usize))
} else {
// SAFETY: valid priority number
Ok(unsafe { core::mem::transmute(number) })
Expand Down
13 changes: 2 additions & 11 deletions riscv-peripheral/src/aclint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,7 @@ pub(crate) mod test {
#[inline]
fn from_number(number: u16) -> Result<Self> {
if number > Self::MAX_HART_ID_NUMBER {
Err(Error::InvalidVariant {
field: "hart_id",
value: number as usize,
})
Err(Error::InvalidVariant(number as usize))
} else {
// SAFETY: valid context number
Ok(unsafe { core::mem::transmute(number) })
Expand All @@ -105,13 +102,7 @@ pub(crate) mod test {
assert_eq!(HartId::from_number(1), Ok(HartId::H1));
assert_eq!(HartId::from_number(2), Ok(HartId::H2));

assert_eq!(
HartId::from_number(3),
Err(Error::InvalidVariant {
field: "hart_id",
value: 3
})
);
assert_eq!(HartId::from_number(3), Err(Error::InvalidVariant(3)));
}

#[allow(dead_code)]
Expand Down
2 changes: 1 addition & 1 deletion riscv-peripheral/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
/// fn number(self) -> u16 { self as _ }
/// fn from_number(number: u16) -> Result<Self> {
/// if number > Self::MAX_HART_ID_NUMBER {
/// Err(Error::InvalidVariant { field: "hart_id", value: number as usize })
/// Err(Error::InvalidVariant(number as usize))
/// } else {
/// // SAFETY: valid context number
/// Ok(unsafe { core::mem::transmute(number) })
Expand Down
47 changes: 7 additions & 40 deletions riscv-peripheral/src/plic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,7 @@ pub(crate) mod test {
#[inline]
fn from_number(number: u16) -> Result<Self> {
if number > Self::MAX_INTERRUPT_NUMBER || number == 0 {
Err(Error::InvalidVariant {
field: "interrupt",
value: number as usize,
})
Err(Error::InvalidVariant(number as usize))
} else {
// SAFETY: valid interrupt number
Ok(unsafe { core::mem::transmute(number) })
Expand All @@ -207,10 +204,7 @@ pub(crate) mod test {
#[inline]
fn from_number(number: u8) -> Result<Self> {
if number > Self::MAX_PRIORITY_NUMBER {
Err(Error::InvalidVariant {
field: "priority",
value: number as usize,
})
Err(Error::InvalidVariant(number as usize))
} else {
// SAFETY: valid priority number
Ok(unsafe { core::mem::transmute(number) })
Expand All @@ -229,10 +223,7 @@ pub(crate) mod test {
#[inline]
fn from_number(number: u16) -> Result<Self> {
if number > Self::MAX_HART_ID_NUMBER {
Err(Error::InvalidVariant {
field: "context",
value: number as usize,
})
Err(Error::InvalidVariant(number as usize))
} else {
// SAFETY: valid context number
Ok(unsafe { core::mem::transmute(number) })
Expand All @@ -252,20 +243,8 @@ pub(crate) mod test {
assert_eq!(Interrupt::from_number(3), Ok(Interrupt::I3));
assert_eq!(Interrupt::from_number(4), Ok(Interrupt::I4));

assert_eq!(
Interrupt::from_number(0),
Err(Error::InvalidVariant {
field: "interrupt",
value: 0
})
);
assert_eq!(
Interrupt::from_number(5),
Err(Error::InvalidVariant {
field: "interrupt",
value: 5
})
);
assert_eq!(Interrupt::from_number(0), Err(Error::InvalidVariant(0)),);
assert_eq!(Interrupt::from_number(5), Err(Error::InvalidVariant(5)),);
}

#[test]
Expand All @@ -280,13 +259,7 @@ pub(crate) mod test {
assert_eq!(Priority::from_number(2), Ok(Priority::P2));
assert_eq!(Priority::from_number(3), Ok(Priority::P3));

assert_eq!(
Priority::from_number(4),
Err(Error::InvalidVariant {
field: "priority",
value: 4
})
);
assert_eq!(Priority::from_number(4), Err(Error::InvalidVariant(4)),);
}

#[test]
Expand All @@ -299,13 +272,7 @@ pub(crate) mod test {
assert_eq!(Context::from_number(1), Ok(Context::C1));
assert_eq!(Context::from_number(2), Ok(Context::C2));

assert_eq!(
Context::from_number(3),
Err(Error::InvalidVariant {
field: "context",
value: 3
})
);
assert_eq!(Context::from_number(3), Err(Error::InvalidVariant(3)),);
}

#[allow(dead_code)]
Expand Down
4 changes: 2 additions & 2 deletions riscv/src/register/pmpcfgx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl TryFrom<u8> for Permission {
0b101 => Ok(Self::RX),
0b110 => Ok(Self::WX),
0b111 => Ok(Self::RWX),
_ => Err(Error::InvalidValue {
_ => Err(Error::InvalidFieldValue {
field: "permission",
value: val as usize,
bitmask: 0b111,
Expand All @@ -55,7 +55,7 @@ impl TryFrom<u8> for Range {
0b01 => Ok(Self::TOR),
0b10 => Ok(Self::NA4),
0b11 => Ok(Self::NAPOT),
_ => Err(Error::InvalidValue {
_ => Err(Error::InvalidFieldValue {
field: "range",
value: val as usize,
bitmask: 0b11,
Expand Down
12 changes: 6 additions & 6 deletions riscv/src/register/satp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl TryFrom<u8> for Mode {
match val {
0 => Ok(Mode::Bare),
1 => Ok(Mode::Sv32),
_ => Err(Error::InvalidVariant {
_ => Err(Error::InvalidFieldVariant {
field: "mode",
value: val as usize,
}),
Expand All @@ -129,7 +129,7 @@ impl TryFrom<u8> for Mode {
9 => Ok(Mode::Sv48),
10 => Ok(Mode::Sv57),
11 => Ok(Mode::Sv64),
_ => Err(Error::InvalidVariant {
_ => Err(Error::InvalidFieldVariant {
field: "mode",
value: val as usize,
}),
Expand Down Expand Up @@ -157,13 +157,13 @@ pub unsafe fn set(mode: Mode, asid: usize, ppn: usize) {
#[cfg(target_pointer_width = "32")]
pub unsafe fn try_set(mode: Mode, asid: usize, ppn: usize) -> Result<()> {
if asid != asid & 0x1FF {
Err(Error::InvalidValue {
Err(Error::InvalidFieldValue {
field: "asid",
value: asid,
bitmask: 0x1FF,
})
} else if ppn != ppn & 0x3F_FFFF {
Err(Error::InvalidValue {
Err(Error::InvalidFieldValue {
field: "ppn",
value: ppn,
bitmask: 0x3F_FFFF,
Expand Down Expand Up @@ -191,13 +191,13 @@ pub unsafe fn set(mode: Mode, asid: usize, ppn: usize) {
#[cfg(target_pointer_width = "64")]
pub unsafe fn try_set(mode: Mode, asid: usize, ppn: usize) -> Result<()> {
if asid != asid & 0xFFFF {
Err(Error::InvalidValue {
Err(Error::InvalidFieldValue {
field: "asid",
value: asid,
bitmask: 0xFFFF,
})
} else if ppn != ppn & 0xFFF_FFFF_FFFF {
Err(Error::InvalidValue {
Err(Error::InvalidFieldValue {
field: "ppn",
value: ppn,
bitmask: 0xFFF_FFFF_FFFF,
Expand Down

0 comments on commit 2b35502

Please sign in to comment.