mach_sys/
structs.rs

1//! This module corresponds to `mach/i386/_structs.h` and `mach/arm/_structs.h`.
2
3use core::mem::size_of;
4
5use crate::message::mach_msg_type_number_t;
6
7// re-export if compile for aarch64/arm64 CPUs
8#[cfg(target_arch = "aarch64")]
9pub use arm::*;
10
11// re-export if compile for x86 or x86-64 CPUs
12#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
13pub use x86::*;
14
15/* 
16 * but these `mod` both always exists whatever which target arch.
17 *
18 * in some cases, this maybe helps for cross-platforms.
19 */
20
21pub mod arm {
22    use super::{size_of, mach_msg_type_number_t};
23
24    #[repr(C)]
25    #[derive(Copy, Clone, Debug, Default, Hash, PartialOrd, PartialEq, Eq, Ord)]
26    pub struct arm_state_hdr_t {
27        pub flavor: u32,
28        pub count:  u32,
29    }
30    pub type   arm_state_hdr = arm_state_hdr_t;
31
32    // because cannot impl Debug for union types.
33    #[allow(missing_debug_implementations)]
34    #[repr(C)]
35    #[derive(Copy, Clone)]
36    pub struct arm_unified_thread_state_t {
37        pub ash: arm_state_hdr_t,
38        pub uts: arm_unified_thread_state__bindgen_ty_1,
39    }
40    pub type   arm_unified_thread_state = arm_unified_thread_state_t;
41
42    impl arm_unified_thread_state_t {
43        pub const fn from_32(flavor: u32, ts_32: arm_thread_state32_t) -> Self {
44            let count = arm_thread_state32_t::count() as u32;
45
46            let ash = arm_state_hdr_t { flavor, count };
47            let uts = arm_thread_state32or64_t { ts_32 };
48
49            Self { ash, uts }
50        }
51        pub const fn from_64(flavor: u32, ts_64: arm_thread_state64_t) -> Self {
52            let count = arm_thread_state64_t::count() as u32;
53
54            let ash = arm_state_hdr_t { flavor, count };
55            let uts = arm_thread_state32or64_t { ts_64 };
56
57            Self { ash, uts }
58        }
59    }
60
61    // because cannot impl Debug for union types.
62    #[allow(missing_debug_implementations)]
63    #[repr(C)]
64    #[derive(Copy, Clone)]
65    pub union arm_thread_state32or64_t {
66        pub ts_32: arm_thread_state32_t,
67        pub ts_64: arm_thread_state64_t,
68    }
69    pub type arm_thread_state32or64                 = arm_thread_state32or64_t;
70    pub type arm_unified_thread_state__bindgen_ty_1 = arm_thread_state32or64_t;
71
72    #[repr(C)]
73    #[derive(Copy, Clone, Debug, Default, Hash, PartialOrd, PartialEq, Eq, Ord)]
74    pub struct arm_thread_state32_t {
75        pub __r:   [u32; 13usize],
76        pub __sp:   u32,
77        pub __lr:   u32,
78        pub __pc:   u32,
79        pub __cpsr: u32,
80    }
81    pub type arm_thread_state32        = arm_thread_state32_t;
82    pub type __darwin_arm_thread_state = arm_thread_state32_t;
83
84    impl arm_thread_state32_t {
85        pub const fn new() -> Self {
86            Self {
87                __r:   [0; 13],
88                __sp:   0,
89                __lr:   0,
90                __pc:   0,
91                __cpsr: 0,
92            }
93        }
94
95        pub const fn count() -> mach_msg_type_number_t {
96            let n = size_of::<Self>() / size_of::<u32>();
97            n as mach_msg_type_number_t
98        }
99    }
100
101    #[repr(C)]
102    #[derive(Copy, Clone, Debug, Default, Hash, PartialOrd, PartialEq, Eq, Ord)]
103    pub struct arm_thread_state64_t {
104        pub __x:   [u64; 29usize],
105        pub __fp:   u64,
106        pub __lr:   u64,
107        pub __sp:   u64,
108        pub __pc:   u64,
109        pub __cpsr: u32,
110        pub __pad:  u32,
111    }
112    pub type arm_thread_state64          = arm_thread_state64_t;
113    pub type __darwin_arm_thread_state64 = arm_thread_state64_t;
114
115    impl arm_thread_state64_t {
116        pub const fn new() -> Self {
117            Self {
118                __x:   [0; 29],
119                __fp:   0,
120                __lr:   0,
121                __sp:   0,
122                __pc:   0,
123                __cpsr: 0,
124                __pad:  0,
125            }
126        }
127
128        pub const fn count() -> mach_msg_type_number_t {
129            let n = size_of::<Self>() / size_of::<u64>();
130            n as mach_msg_type_number_t
131        }
132    }
133}
134
135pub mod x86 {
136    use super::{size_of, mach_msg_type_number_t};
137
138    #[repr(C)]
139    #[derive(Copy, Clone, Debug, Default, Hash, PartialOrd, PartialEq, Eq, Ord)]
140    pub struct x86_thread_state64_t {
141        pub __rax:    u64,
142        pub __rbx:    u64,
143        pub __rcx:    u64,
144        pub __rdx:    u64,
145        pub __rdi:    u64,
146        pub __rsi:    u64,
147        pub __rbp:    u64,
148        pub __rsp:    u64,
149        pub __r8:     u64,
150        pub __r9:     u64,
151        pub __r10:    u64,
152        pub __r11:    u64,
153        pub __r12:    u64,
154        pub __r13:    u64,
155        pub __r14:    u64,
156        pub __r15:    u64,
157        pub __rip:    u64,
158        pub __rflags: u64,
159        pub __cs:     u64,
160        pub __fs:     u64,
161        pub __gs:     u64,
162    }
163
164    impl x86_thread_state64_t {
165        pub const fn new() -> Self {
166            Self {
167                __rax:    0,
168                __rbx:    0,
169                __rcx:    0,
170                __rdx:    0,
171                __rdi:    0,
172                __rsi:    0,
173                __rbp:    0,
174                __rsp:    0,
175                __r8:     0,
176                __r9:     0,
177                __r10:    0,
178                __r11:    0,
179                __r12:    0,
180                __r13:    0,
181                __r14:    0,
182                __r15:    0,
183                __rip:    0,
184                __rflags: 0,
185                __cs:     0,
186                __fs:     0,
187                __gs:     0,
188            }
189        }
190
191        pub const fn count() -> mach_msg_type_number_t {
192            let n = size_of::<Self>() / size_of::<u64>();
193            n as mach_msg_type_number_t
194        }
195    }
196}
197