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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//
// Copyright 2023, Colias Group, LLC
// Copyright (c) 2020 Arm Limited
//
// SPDX-License-Identifier: MIT
//

use sel4_config::{sel4_cfg, sel4_cfg_enum, sel4_cfg_if, sel4_cfg_wrap_match};

use crate::{declare_fault_newtype, sys, Word};

declare_fault_newtype!(NullFault, sys::seL4_Fault_NullFault);
declare_fault_newtype!(CapFault, sys::seL4_Fault_CapFault);
declare_fault_newtype!(UnknownSyscall, sys::seL4_Fault_UnknownSyscall);
declare_fault_newtype!(UserException, sys::seL4_Fault_UserException);
declare_fault_newtype!(VmFault, sys::seL4_Fault_VMFault);

#[sel4_cfg(KERNEL_MCS)]
declare_fault_newtype!(Timeout, sys::seL4_Fault_Timeout);

sel4_cfg_if! {
    if #[sel4_cfg(ARM_HYPERVISOR_SUPPORT)] {
        declare_fault_newtype!(VGicMaintenance, sys::seL4_Fault_VGICMaintenance);
        declare_fault_newtype!(VCpuFault, sys::seL4_Fault_VCPUFault);
        declare_fault_newtype!(VPpiEvent, sys::seL4_Fault_VPPIEvent);
    }
}

#[sel4_cfg_enum]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Fault {
    NullFault(NullFault),
    CapFault(CapFault),
    UnknownSyscall(UnknownSyscall),
    UserException(UserException),
    VmFault(VmFault),
    #[sel4_cfg(KERNEL_MCS)]
    Timeout(Timeout),
    #[sel4_cfg(ARM_HYPERVISOR_SUPPORT)]
    VGicMaintenance(VGicMaintenance),
    #[sel4_cfg(ARM_HYPERVISOR_SUPPORT)]
    VCpuFault(VCpuFault),
    #[sel4_cfg(ARM_HYPERVISOR_SUPPORT)]
    VPpiEvent(VPpiEvent),
}

impl Fault {
    pub fn from_sys(raw: sys::seL4_Fault) -> Self {
        sel4_cfg_wrap_match! {
            match raw.splay() {
                sys::seL4_Fault_Splayed::NullFault(inner) => {
                    Self::NullFault(NullFault::from_inner(inner))
                }
                sys::seL4_Fault_Splayed::CapFault(inner) => Self::CapFault(CapFault::from_inner(inner)),
                sys::seL4_Fault_Splayed::UnknownSyscall(inner) => {
                    Self::UnknownSyscall(UnknownSyscall::from_inner(inner))
                }
                sys::seL4_Fault_Splayed::UserException(inner) => {
                    Self::UserException(UserException::from_inner(inner))
                }
                sys::seL4_Fault_Splayed::VMFault(inner) => Self::VmFault(VmFault::from_inner(inner)),
                #[sel4_cfg(KERNEL_MCS)]
                sys::seL4_Fault_Splayed::Timeout(inner) => Self::Timeout(Timeout::from_inner(inner)),
                #[sel4_cfg(ARM_HYPERVISOR_SUPPORT)]
                sys::seL4_Fault_Splayed::VGICMaintenance(inner) => {
                    Self::VGicMaintenance(VGicMaintenance::from_inner(inner))
                }
                #[sel4_cfg(ARM_HYPERVISOR_SUPPORT)]
                sys::seL4_Fault_Splayed::VCPUFault(inner) => {
                    Self::VCpuFault(VCpuFault::from_inner(inner))
                }
                #[sel4_cfg(ARM_HYPERVISOR_SUPPORT)]
                sys::seL4_Fault_Splayed::VPPIEvent(inner) => {
                    Self::VPpiEvent(VPpiEvent::from_inner(inner))
                }
            }
        }
    }
}

impl CapFault {
    // TODO
}

impl UnknownSyscall {
    pub fn fault_ip(&self) -> Word {
        self.inner().get_FaultIP()
    }

    pub fn sp(&self) -> Word {
        self.inner().get_SP()
    }

    pub fn lr(&self) -> Word {
        self.inner().get_LR()
    }

    pub fn syscall(&self) -> Word {
        self.inner().get_Syscall()
    }
}

impl UserException {
    // TODO
}

impl VmFault {
    pub fn ip(&self) -> Word {
        self.inner().get_IP()
    }

    pub fn addr(&self) -> Word {
        self.inner().get_Addr()
    }

    pub fn is_prefetch(&self) -> bool {
        self.inner().get_PrefetchFault() != 0
    }

    pub fn fsr(&self) -> Word {
        self.inner().get_FSR()
    }
}

#[sel4_cfg(ARM_HYPERVISOR_SUPPORT)]
impl VGicMaintenance {
    pub fn idx(&self) -> Option<Word> {
        match self.inner().get_IDX() {
            Word::MAX => None,
            idx => Some(idx),
        }
    }
}

#[sel4_cfg(ARM_HYPERVISOR_SUPPORT)]
impl VCpuFault {
    pub fn hsr(&self) -> Word {
        self.inner().get_HSR()
    }
}

#[sel4_cfg(ARM_HYPERVISOR_SUPPORT)]
impl VPpiEvent {
    pub fn irq(&self) -> Word {
        self.inner().get_irq()
    }
}