summaryrefslogtreecommitdiff
path: root/src/exceptions.rs
blob: 5e3f02fee5c7a6c4e946aec8e760992823d6d689 (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
// 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;

use crate::consts::{Addr, DWord};

#[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,
}

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

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

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

impl MemoryException {
    pub(crate) fn to_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 to_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 to_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 Into<MemoryExceptionType> for MemoryException {
    fn into(self) -> MemoryExceptionType {
        self.type_
    }
}

impl Into<ExceptionType> for Exception {
    fn into(self) -> ExceptionType {
        self.type_
    }
}