Skip to main content

kh_runtime/syscall/
table.rs

1//! Darwin BSD syscall numbers used by the micro translator (arm64).
2
3/// Known BSD syscalls we dispatch (subset of XNU `syscalls.master`).
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5pub enum BsdSyscall {
6    /// `exit`.
7    Exit,
8    /// `read`.
9    Read,
10    /// `write`.
11    Write,
12    /// `open`.
13    Open,
14    /// `close`.
15    Close,
16    /// `getpid`.
17    Getpid,
18    /// `access`.
19    Access,
20    /// `issetugid`.
21    Issetugid,
22    /// `munmap`.
23    Munmap,
24    /// `mprotect`.
25    Mprotect,
26    /// `mmap`.
27    Mmap,
28    /// `msync`.
29    Msync,
30    /// `dup`.
31    Dup,
32    /// `fcntl`.
33    Fcntl,
34    /// `lseek`.
35    Lseek,
36    /// `stat` / `stat64`.
37    Stat,
38    /// `fstat` / `fstat64`.
39    Fstat,
40    /// `lstat` / `lstat64`.
41    Lstat,
42    /// `unlink`.
43    Unlink,
44    /// `mkdir`.
45    Mkdir,
46    /// `rmdir`.
47    Rmdir,
48    /// `rename`.
49    Rename,
50    /// `ftruncate`.
51    Ftruncate,
52    /// `fsync`.
53    Fsync,
54    /// `openat`.
55    Openat,
56    /// `fstatat` / `fstatat64`.
57    Fstatat,
58    /// `gettimeofday`.
59    Gettimeofday,
60    /// `clock_gettime`.
61    ClockGettime,
62    /// `sysctl`.
63    Sysctl,
64    /// `sysctlbyname`.
65    Sysctlbyname,
66    /// `sigprocmask`.
67    Sigprocmask,
68    /// `sigaction`.
69    Sigaction,
70    /// `bsdthread_create`.
71    BsdthreadCreate,
72    /// `bsdthread_terminate`.
73    BsdthreadTerminate,
74    /// `bsdthread_register`.
75    BsdthreadRegister,
76    /// `thread_selfid`.
77    ThreadSelfid,
78}
79
80impl BsdSyscall {
81    /// Trace / CLI label.
82    #[must_use]
83    pub const fn name(self) -> &'static str {
84        match self {
85            Self::Exit => "exit",
86            Self::Read => "read",
87            Self::Write => "write",
88            Self::Open => "open",
89            Self::Close => "close",
90            Self::Getpid => "getpid",
91            Self::Access => "access",
92            Self::Issetugid => "issetugid",
93            Self::Munmap => "munmap",
94            Self::Mprotect => "mprotect",
95            Self::Mmap => "mmap",
96            Self::Msync => "msync",
97            Self::Dup => "dup",
98            Self::Fcntl => "fcntl",
99            Self::Lseek => "lseek",
100            Self::Stat => "stat",
101            Self::Fstat => "fstat",
102            Self::Lstat => "lstat",
103            Self::Unlink => "unlink",
104            Self::Mkdir => "mkdir",
105            Self::Rmdir => "rmdir",
106            Self::Rename => "rename",
107            Self::Ftruncate => "ftruncate",
108            Self::Fsync => "fsync",
109            Self::Openat => "openat",
110            Self::Fstatat => "fstatat",
111            Self::Gettimeofday => "gettimeofday",
112            Self::ClockGettime => "clock_gettime",
113            Self::Sysctl => "sysctl",
114            Self::Sysctlbyname => "sysctlbyname",
115            Self::Sigprocmask => "sigprocmask",
116            Self::Sigaction => "sigaction",
117            Self::BsdthreadCreate => "bsdthread_create",
118            Self::BsdthreadTerminate => "bsdthread_terminate",
119            Self::BsdthreadRegister => "bsdthread_register",
120            Self::ThreadSelfid => "thread_selfid",
121        }
122    }
123
124    /// Primary Darwin syscall number.
125    #[must_use]
126    pub const fn number(self) -> u32 {
127        match self {
128            Self::Exit => 1,
129            Self::Read => 3,
130            Self::Write => 4,
131            Self::Open => 5,
132            Self::Close => 6,
133            Self::Unlink => 10,
134            Self::Getpid => 20,
135            Self::Access => 33,
136            Self::Dup => 41,
137            Self::Sigaction => 46,
138            Self::Sigprocmask => 48,
139            Self::Msync => 65,
140            Self::Munmap => 73,
141            Self::Mprotect => 74,
142            Self::Fcntl => 92,
143            Self::Fsync => 95,
144            Self::Gettimeofday => 116,
145            Self::Rename => 128,
146            Self::Mkdir => 136,
147            Self::Rmdir => 137,
148            Self::Mmap => 197,
149            Self::Lseek => 199,
150            Self::Ftruncate => 201,
151            Self::Sysctl => 202,
152            Self::ClockGettime => 266,
153            Self::Sysctlbyname => 274,
154            Self::Issetugid => 327,
155            // Prefer 64-bit variants as primary; aliases in lookup.
156            Self::Stat => 338,
157            Self::Fstat => 339,
158            Self::Lstat => 340,
159            Self::BsdthreadCreate => 360,
160            Self::BsdthreadTerminate => 361,
161            Self::BsdthreadRegister => 366,
162            Self::ThreadSelfid => 372,
163            Self::Openat => 463,
164            Self::Fstatat => 470,
165        }
166    }
167}
168
169/// Looks up a syscall by Darwin number (includes NOCANCEL / 64-bit aliases).
170#[must_use]
171pub const fn lookup(number: u32) -> Option<BsdSyscall> {
172    match number {
173        1 => Some(BsdSyscall::Exit),
174        3 | 396 => Some(BsdSyscall::Read),
175        4 | 397 => Some(BsdSyscall::Write),
176        5 | 398 => Some(BsdSyscall::Open),
177        6 | 399 => Some(BsdSyscall::Close),
178        10 | 401 => Some(BsdSyscall::Unlink),
179        20 => Some(BsdSyscall::Getpid),
180        33 => Some(BsdSyscall::Access),
181        41 => Some(BsdSyscall::Dup),
182        46 => Some(BsdSyscall::Sigaction),
183        48 => Some(BsdSyscall::Sigprocmask),
184        65 => Some(BsdSyscall::Msync),
185        73 => Some(BsdSyscall::Munmap),
186        74 => Some(BsdSyscall::Mprotect),
187        92 => Some(BsdSyscall::Fcntl),
188        95 | 406 => Some(BsdSyscall::Fsync),
189        116 => Some(BsdSyscall::Gettimeofday),
190        128 => Some(BsdSyscall::Rename),
191        136 => Some(BsdSyscall::Mkdir),
192        137 => Some(BsdSyscall::Rmdir),
193        188 | 338 => Some(BsdSyscall::Stat),  // stat / stat64
194        189 | 339 => Some(BsdSyscall::Fstat), // fstat / fstat64
195        190 | 340 => Some(BsdSyscall::Lstat), // lstat / lstat64
196        197 => Some(BsdSyscall::Mmap),
197        199 => Some(BsdSyscall::Lseek),
198        201 => Some(BsdSyscall::Ftruncate),
199        202 => Some(BsdSyscall::Sysctl),
200        266 => Some(BsdSyscall::ClockGettime),
201        274 => Some(BsdSyscall::Sysctlbyname),
202        327 => Some(BsdSyscall::Issetugid),
203        360 => Some(BsdSyscall::BsdthreadCreate),
204        361 => Some(BsdSyscall::BsdthreadTerminate),
205        366 => Some(BsdSyscall::BsdthreadRegister),
206        372 => Some(BsdSyscall::ThreadSelfid),
207        463 => Some(BsdSyscall::Openat),
208        470 | 471 => Some(BsdSyscall::Fstatat), // fstatat / nocancel
209        _ => None,
210    }
211}
212
213/// Name for a raw number, if known.
214#[must_use]
215pub const fn name_of(number: u32) -> Option<&'static str> {
216    match lookup(number) {
217        Some(s) => Some(s.name()),
218        None => None,
219    }
220}
221
222/// Snapshot of the table used by unit tests / docs.
223#[must_use]
224pub fn known_syscalls() -> &'static [(u32, &'static str)] {
225    &[
226        (1, "exit"),
227        (3, "read"),
228        (4, "write"),
229        (5, "open"),
230        (6, "close"),
231        (20, "getpid"),
232        (33, "access"),
233        (41, "dup"),
234        (46, "sigaction"),
235        (48, "sigprocmask"),
236        (65, "msync"),
237        (73, "munmap"),
238        (74, "mprotect"),
239        (92, "fcntl"),
240        (116, "gettimeofday"),
241        (197, "mmap"),
242        (199, "lseek"),
243        (202, "sysctl"),
244        (266, "clock_gettime"),
245        (274, "sysctlbyname"),
246        (327, "issetugid"),
247        (10, "unlink"),
248        (95, "fsync"),
249        (128, "rename"),
250        (136, "mkdir"),
251        (137, "rmdir"),
252        (201, "ftruncate"),
253        (338, "stat"),
254        (339, "fstat"),
255        (340, "lstat"),
256        (463, "openat"),
257        (470, "fstatat"),
258        (360, "bsdthread_create"),
259        (361, "bsdthread_terminate"),
260        (366, "bsdthread_register"),
261        (372, "thread_selfid"),
262        (463, "openat"),
263    ]
264}
265
266#[cfg(test)]
267mod tests {
268    use super::*;
269
270    #[test]
271    fn snapshot_table_consistent() {
272        for &(num, name) in known_syscalls() {
273            assert_eq!(name_of(num), Some(name));
274            assert_eq!(lookup(num).map(BsdSyscall::name), Some(name));
275        }
276    }
277
278    #[test]
279    fn nocancel_and_stat_aliases() {
280        assert_eq!(lookup(396), Some(BsdSyscall::Read));
281        assert_eq!(lookup(397), Some(BsdSyscall::Write));
282        assert_eq!(lookup(398), Some(BsdSyscall::Open));
283        assert_eq!(lookup(399), Some(BsdSyscall::Close));
284        assert_eq!(lookup(188), Some(BsdSyscall::Stat));
285        assert_eq!(lookup(189), Some(BsdSyscall::Fstat));
286    }
287}