Skip to content

Commit 6e0652f

Browse files
mrjbomFreax13
authored andcommitted
TryFrom implementation for ExceptionVector
1 parent 3943178 commit 6e0652f

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/structures/idt.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use crate::registers::rflags::RFlags;
2424
use crate::{PrivilegeLevel, VirtAddr};
2525
use bit_field::BitField;
2626
use bitflags::bitflags;
27+
use core::convert::TryFrom;
2728
use core::fmt;
2829
use core::marker::PhantomData;
2930
use core::ops::Bound::{Excluded, Included, Unbounded};
@@ -1361,6 +1362,52 @@ pub enum ExceptionVector {
13611362
Security = 0x1E,
13621363
}
13631364

1365+
/// Exception vector number is invalid
1366+
#[derive(Debug)]
1367+
pub struct InvalidExceptionVectorNumber(u8);
1368+
1369+
impl fmt::Display for InvalidExceptionVectorNumber {
1370+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1371+
write!(f, "{} is not a valid exception vector", self.0)
1372+
}
1373+
}
1374+
1375+
impl TryFrom<u8> for ExceptionVector {
1376+
type Error = InvalidExceptionVectorNumber;
1377+
1378+
/// Tries to convert the exception vector number to [`ExceptionVector`]
1379+
///
1380+
/// Fails if exception vector number is Coprocessor Segment Overrun, reserved or not exception vector number
1381+
fn try_from(exception_vector_number: u8) -> Result<Self, Self::Error> {
1382+
match exception_vector_number {
1383+
0x00 => Ok(Self::Division),
1384+
0x01 => Ok(Self::Debug),
1385+
0x02 => Ok(Self::NonMaskableInterrupt),
1386+
0x03 => Ok(Self::Breakpoint),
1387+
0x04 => Ok(Self::Overflow),
1388+
0x05 => Ok(Self::BoundRange),
1389+
0x06 => Ok(Self::InvalidOpcode),
1390+
0x07 => Ok(Self::DeviceNotAvailable),
1391+
0x08 => Ok(Self::Double),
1392+
0x0A => Ok(Self::InvalidTss),
1393+
0x0B => Ok(Self::SegmentNotPresent),
1394+
0x0C => Ok(Self::Stack),
1395+
0x0D => Ok(Self::GeneralProtection),
1396+
0x0E => Ok(Self::Page),
1397+
0x10 => Ok(Self::X87FloatingPoint),
1398+
0x11 => Ok(Self::AlignmentCheck),
1399+
0x12 => Ok(Self::MachineCheck),
1400+
0x13 => Ok(Self::SimdFloatingPoint),
1401+
0x14 => Ok(Self::Virtualization),
1402+
0x15 => Ok(Self::ControlProtection),
1403+
0x1C => Ok(Self::HypervisorInjection),
1404+
0x1D => Ok(Self::VmmCommunication),
1405+
0x1E => Ok(Self::Security),
1406+
_ => Err(InvalidExceptionVectorNumber(exception_vector_number)),
1407+
}
1408+
}
1409+
}
1410+
13641411
#[cfg(all(
13651412
feature = "instructions",
13661413
feature = "abi_x86_interrupt",

0 commit comments

Comments
 (0)