summaryrefslogtreecommitdiff
path: root/src/exceptions.rs
blob: 7f00e5c8181c6ef9c1401a14ec56c45b21d24db3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright (c) 2025 taitep
// SPDX-License-Identifier: MIT
//
// This file is part of TRVE (https://gitea.taitep.se/taitep/trve)
// See LICENSE file in the project root for full license text.

use int_enum::IntEnum;

#[repr(u8)]
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, IntEnum)]
pub enum ExceptionType {
    InstructionAddressMisaligned = 0,
    InstructionAccessFault = 1,
    IllegalInstruction = 2,
    Breakpoint = 3,
    LoadAddressMisaligned = 4,
    LoadAccessFault = 5,
    StoreAmoAddressMisaligned = 6,
    StoreAmoAccessFault = 7,
    EnvironmentCallFromUMode = 8,
    EnvironmentCallFromSMode = 9,
    EnvironmentCallFromMMode = 11,
    InstructionPageFault = 12,
    LoadPageFault = 13,
    StoreAmoPageFault = 15,
    DoubleTrap = 16,
    SoftwareCheck = 18,
    HardwareError = 19,
}

pub enum MemoryExceptionType {
    AddressMisaligned,
    AccessFault,
    PageFault,
}

impl MemoryExceptionType {
    pub(crate) fn to_exception_store(&self) -> ExceptionType {
        match self {
            Self::AddressMisaligned => ExceptionType::StoreAmoAddressMisaligned,
            Self::AccessFault => ExceptionType::StoreAmoAccessFault,
            Self::PageFault => ExceptionType::StoreAmoPageFault,
        }
    }

    pub(crate) fn to_exception_instr(&self) -> ExceptionType {
        match self {
            Self::AddressMisaligned => ExceptionType::InstructionAddressMisaligned,
            Self::AccessFault => ExceptionType::InstructionAccessFault,
            Self::PageFault => ExceptionType::InstructionPageFault,
        }
    }

    pub(crate) fn to_exception_load(&self) -> ExceptionType {
        match self {
            Self::AddressMisaligned => ExceptionType::LoadAddressMisaligned,
            Self::AccessFault => ExceptionType::LoadAccessFault,
            Self::PageFault => ExceptionType::LoadPageFault,
        }
    }
}