hw_exception/
cdecls.rs

1// Copyright (c) 2023 Daniel Fox Franke
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3
4use std::ffi::{c_int, c_short, c_void};
5
6#[non_exhaustive]
7#[repr(C)]
8#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
9/// Subtype code for SIGILL.
10pub enum SigillCode {
11    /// Illegal opcode.
12    ILLOPC,
13    /// Illegal operand.
14    ILLOPN,
15    /// Illegal addressing mode.
16    ILLADR,
17    /// Illegal trap.
18    ILLTRP,
19    /// Privileged opcode.
20    PRVOPC,
21    /// Privileged register.
22    PRVREG,
23    /// Coprocessor error.
24    CORPOC,
25    /// Internal stack error.
26    BADSTK,
27}
28
29#[non_exhaustive]
30#[repr(C)]
31#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
32/// Subtype code for SIGFPE.
33pub enum SigfpeCode {
34    /// Integer divide by zero.
35    INTDIV,
36    /// Integer overflow.
37    INTOVF,
38    /// Floating-point divide by zero.
39    FLTDIV,
40    /// Floating-point overflow.
41    FLTOVF,
42    /// Floating-point underflow.
43    FLTUND,
44    /// Floating-point inexact result.
45    FLTRES,
46    /// Floating-point invalid operation.
47    FLTINV,
48    /// Subscript out of range.
49    FLTSUB,
50}
51
52#[non_exhaustive]
53#[repr(C)]
54#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
55/// Subtype code for SIGSEGV.
56pub enum SigsegvCode {
57    /// Address not mapped to object.
58    MAPERR,
59    /// Invalid permissions for mapped object.
60    ACCERR,
61    /// Failed address bound checks.
62    BNDERR,
63    /// Access was denied by memory protection keys.
64    PKUERR,
65}
66
67#[non_exhaustive]
68#[repr(C)]
69#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
70#[allow(non_camel_case_types)]
71/// Subtype code for SIGBUS.
72pub enum SigbusCode {
73    /// Invalid address alignment.
74    ADRALN,
75    /// Nonexistent physical address.
76    ADDERR,
77    /// Object-specific hardware error.
78    OBJERR,
79    /// Hardware memory error consumed on a machine check; action required.
80    MCEERR_AR,
81    /// Hardware memory error detected in process but not consumed; action optional.
82    MCEERR_AO,
83}
84
85#[non_exhaustive]
86#[repr(C)]
87#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
88/// Subtype code for SIGTRAP.
89pub enum SigtrapCode {
90    /// Process breakpoint.
91    BRKPT,
92    /// Process trace trap.
93    TRACE,
94    /// Process taken branch trap (IA-64 only).
95    BRANCH,
96    /// Hardware breakpoint/watchpoint (IA-64 only).
97    HWBKPT,
98}
99
100extern "C" {
101    pub(crate) fn hwexception_catch(
102        callback: unsafe extern "C" fn(*mut c_void),
103        context: *mut c_void,
104        exception: *mut c_void,
105        jmp_buf_ptr: *mut *mut c_void,
106        err_buf_ptr: *mut *mut c_void,
107    ) -> c_int;
108
109    pub(crate) fn hwexception_throw(
110        exception: *const c_void,
111        exception_len: usize,
112        jmp_buf_ptr: *mut *mut c_void,
113        err_buf_ptr: *mut *mut c_void,
114    );
115
116    pub(crate) fn hwexception_translate_sigill_code(
117        out: *mut SigillCode,
118        code: c_int,
119    ) -> c_int;
120    pub(crate) fn hwexception_translate_sigfpe_code(
121        out: *mut SigfpeCode,
122        code: c_int,
123    ) -> c_int;
124    pub(crate) fn hwexception_translate_sigsegv_code(
125        out: *mut SigsegvCode,
126        code: c_int,
127    ) -> c_int;
128    pub(crate) fn hwexception_translate_sigbus_code(
129        out: *mut SigbusCode,
130        code: c_int,
131    ) -> c_int;
132    pub(crate) fn hwexception_translate_sigtrap_code(
133        out: *mut SigtrapCode,
134        code: c_int,
135    ) -> c_int;
136
137    pub(crate) fn hwexception_get_ip(context: *const c_void) -> *mut c_void;
138    pub(crate) fn hwexception_get_sp(context: *const c_void) -> *mut c_void;
139    pub(crate) fn hwexception_get_symbol_address(context: *const c_void) -> *mut c_void;
140
141    pub(crate) fn hwexception_get_addr_lsb(info: *const libc::siginfo_t) -> c_short;
142    pub(crate) fn hwexception_get_lower(info: *const libc::siginfo_t) -> *mut c_void;
143    pub(crate) fn hwexception_get_upper(info: *const libc::siginfo_t) -> *mut c_void;
144    pub(crate) fn hwexception_get_pkey(info: *const libc::siginfo_t) -> c_int;
145}