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