Skip to content
This repository was archived by the owner on Nov 28, 2023. It is now read-only.

Exceptions enum and table #114

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

## Added

- Standard exceptions enum
- __EXCEPTIONS array for dedicated handlers for each exception type.

## [v0.11.0] - 2023-01-18

### Changed
Expand Down
15 changes: 15 additions & 0 deletions link.x
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ PROVIDE(_max_hart_id = 0);
PROVIDE(_hart_stack_size = 2K);
PROVIDE(_heap_size = 0);

PROVIDE(InstrAddressMisaligned = ExceptionHandler);
PROVIDE(InstrAccessFault = ExceptionHandler);
PROVIDE(IlegalInstr = ExceptionHandler);
PROVIDE(Breakpoint = ExceptionHandler);
PROVIDE(LoadAddressMisaligned = ExceptionHandler);
PROVIDE(LoadAccessFault = ExceptionHandler);
PROVIDE(StoreAMOAddressMisaligned = ExceptionHandler);
PROVIDE(StoreAMOAccessFault = ExceptionHandler);
PROVIDE(EnvUserCall = ExceptionHandler);
PROVIDE(EnvSupervisorCall = ExceptionHandler);
PROVIDE(EnvMachineCall = ExceptionHandler);
PROVIDE(InstrPageFault = ExceptionHandler);
PROVIDE(LoadPageFault = ExceptionHandler);
PROVIDE(StoreAMOPageFault = ExceptionHandler);

PROVIDE(UserSoft = DefaultHandler);
PROVIDE(SupervisorSoft = DefaultHandler);
PROVIDE(MachineSoft = DefaultHandler);
Expand Down
71 changes: 70 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,14 @@ pub extern "C" fn start_trap_rust(trap_frame: *const TrapFrame) {
let cause = xcause::read();

if cause.is_exception() {
ExceptionHandler(&*trap_frame)
if cause.code() < __EXCEPTIONS.len() {
match __EXCEPTIONS[cause.code()] {
Some(handler) => handler(&*trap_frame),
None => ExceptionHandler(&*trap_frame),
};
} else {
ExceptionHandler(&*trap_frame);
}
} else {
if cause.code() < __INTERRUPTS.len() {
let h = &__INTERRUPTS[cause.code()];
Expand Down Expand Up @@ -502,6 +509,68 @@ pub fn DefaultInterruptHandler() {
}
}

/// Standard exception categories.
#[doc(hidden)]
#[repr(u32)]
pub enum Exception {
InstrAddressMisaligned = 0,
InstrAccessFault = 1,
IlegalInstr = 2,
Breakpoint = 3,
LoadAddressMisaligned = 4,
LoadAccessFault = 5,
StoreAMOAddressMisaligned = 6,
StoreAMOAccessFault = 7,
EnvUserCall = 8,
EnvSupervisorCall = 9,
// Reserved = 10,
EnvMachineCall = 11,
InstrPageFault = 12,
LoadPageFault = 13,
// Reserved = 14,
StoreAMOPageFault = 15,
}

pub use self::Exception as exception;

extern "C" {
fn InstrAddressMisaligned(trap_frame: &TrapFrame);
fn InstrAccessFault(trap_frame: &TrapFrame);
fn IlegalInstr(trap_frame: &TrapFrame);
fn Breakpoint(trap_frame: &TrapFrame);
fn LoadAddressMisaligned(trap_frame: &TrapFrame);
fn LoadAccessFault(trap_frame: &TrapFrame);
fn StoreAMOAddressMisaligned(trap_frame: &TrapFrame);
fn StoreAMOAccessFault(trap_frame: &TrapFrame);
fn EnvUserCall(trap_frame: &TrapFrame);
fn EnvSupervisorCall(trap_frame: &TrapFrame);
fn EnvMachineCall(trap_frame: &TrapFrame);
fn InstrPageFault(trap_frame: &TrapFrame);
fn LoadPageFault(trap_frame: &TrapFrame);
fn StoreAMOPageFault(trap_frame: &TrapFrame);
}

#[doc(hidden)]
#[no_mangle]
pub static __EXCEPTIONS: [Option<unsafe extern "C" fn(&TrapFrame)>; 16] = [
Some(InstrAddressMisaligned),
Some(InstrAccessFault),
Some(IlegalInstr),
Some(Breakpoint),
Some(LoadAddressMisaligned),
Some(LoadAccessFault),
Some(StoreAMOAddressMisaligned),
Some(StoreAMOAccessFault),
Some(EnvUserCall),
Some(EnvSupervisorCall),
None, // 10 is reserved
Some(EnvMachineCall),
Some(InstrPageFault),
Some(LoadPageFault),
None, // 14 is reserved
Some(StoreAMOPageFault),
];

/* Interrupts */
#[doc(hidden)]
pub enum Interrupt {
Expand Down