summaryrefslogtreecommitdiff
path: root/src/exceptions.rs
blob: 8ea448032fbb240a9db8a4ff36287e5b0cf97467 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) 2025 taitep
// SPDX-License-Identifier: BSD-2-Clause
//
// 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,
}

impl ExceptionType {
    pub fn with_no_value(self) -> Exception {
        Exception {
            type_: self,
            value: 0,
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub struct Exception {
    pub type_: ExceptionType,
    pub value: u64,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MemoryExceptionType {
    AddressMisaligned,
    AccessFault,
    PageFault,
}

impl MemoryExceptionType {
    pub(crate) fn with_addr(self, addr: u64) -> MemoryException {
        MemoryException { type_: self, addr }
    }
}

#[derive(Debug, Clone, Copy)]
pub struct MemoryException {
    pub type_: MemoryExceptionType,
    pub addr: u64,
}

impl MemoryException {
    pub(crate) fn into_exception_store(self) -> Exception {
        Exception {
            type_: match self.type_ {
                MemoryExceptionType::AddressMisaligned => ExceptionType::StoreAmoAddressMisaligned,
                MemoryExceptionType::AccessFault => ExceptionType::StoreAmoAccessFault,
                MemoryExceptionType::PageFault => ExceptionType::StoreAmoPageFault,
            },
            value: self.addr,
        }
    }

    pub(crate) fn into_exception_instr(self) -> Exception {
        Exception {
            type_: match self.type_ {
                MemoryExceptionType::AddressMisaligned => ExceptionType::InstructionAccessFault,
                MemoryExceptionType::AccessFault => ExceptionType::InstructionAccessFault,
                MemoryExceptionType::PageFault => ExceptionType::InstructionPageFault,
            },
            value: self.addr,
        }
    }

    pub(crate) fn into_exception_load(self) -> Exception {
        Exception {
            type_: match self.type_ {
                MemoryExceptionType::AddressMisaligned => ExceptionType::LoadAddressMisaligned,
                MemoryExceptionType::AccessFault => ExceptionType::LoadAccessFault,
                MemoryExceptionType::PageFault => ExceptionType::LoadPageFault,
            },
            value: self.addr,
        }
    }
}

impl From<MemoryException> for MemoryExceptionType {
    fn from(val: MemoryException) -> Self {
        val.type_
    }
}

impl From<Exception> for ExceptionType {
    fn from(val: Exception) -> Self {
        val.type_
    }
}