hal_core/interrupt/
ctx.rs

1use crate::VAddr;
2use core::fmt;
3
4pub trait Context {
5    // TODO(eliza): Registers trait
6    type Registers: fmt::Debug + fmt::Display;
7
8    fn registers(&self) -> &Self::Registers;
9
10    /// # Safety
11    ///
12    /// Mutating the value of saved interrupt registers can cause
13    /// undefined behavior.
14    unsafe fn registers_mut(&mut self) -> &mut Self::Registers;
15}
16
17pub trait PageFault: Context {
18    fn fault_vaddr(&self) -> VAddr;
19    fn debug_error_code(&self) -> &dyn fmt::Debug;
20
21    fn display_error_code(&self) -> &dyn fmt::Display;
22    // TODO(eliza): more
23}
24
25/// Trait representing a fault caused by the currently executing code.
26pub trait CodeFault: Context {
27    /// Returns `true` if the code fault occurred while executing in user
28    /// mode code.
29    fn is_user_mode(&self) -> bool;
30
31    /// Returns the virtual address of the instruction pointer where the
32    /// fault occurred.
33    fn instruction_ptr(&self) -> VAddr;
34
35    /// Returns a static string describing the kind of code fault.
36    fn fault_kind(&self) -> &'static str;
37
38    /// Returns a dynamically formatted message if additional information about
39    /// the fault is available. Otherwise, this returns `None`.
40    ///
41    /// # Default Implementation
42    ///
43    /// Returns `None`.
44    fn details(&self) -> Option<&dyn fmt::Display> {
45        None
46    }
47}
48
49#[non_exhaustive]
50pub enum CodeFaultKind {
51    /// The code fault was a division by zero.
52    Division,
53    /// The code fault was caused by an invalid instruction.
54    InvalidInstruction,
55    Overflow,
56    Alignment,
57    Other(&'static str),
58}