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    /// `pipe`.
87    Pipe,
88    /// `socket`.
89    Socket,
90    /// `connect`.
91    Connect,
92    /// `bind`.
93    Bind,
94    /// `listen`.
95    Listen,
96    /// `accept`.
97    Accept,
98    /// `setsockopt`.
99    Setsockopt,
100    /// `getsockopt`.
101    Getsockopt,
102    /// `shutdown`.
103    Shutdown,
104    /// `sendto`.
105    Sendto,
106    /// `recvfrom`.
107    Recvfrom,
108    /// `send` (often libc → sendto).
109    Send,
110    /// `recv`.
111    Recv,
112    /// `poll`.
113    Poll,
114    /// `select`.
115    Select,
116    /// `getsockname`.
117    Getsockname,
118    /// `getpeername`.
119    Getpeername,
120}
121
122impl BsdSyscall {
123    /// Trace / CLI label.
124    #[must_use]
125    pub const fn name(self) -> &'static str {
126        match self {
127            Self::Exit => "exit",
128            Self::Read => "read",
129            Self::Write => "write",
130            Self::Open => "open",
131            Self::Close => "close",
132            Self::Getpid => "getpid",
133            Self::Getppid => "getppid",
134            Self::Access => "access",
135            Self::Issetugid => "issetugid",
136            Self::Munmap => "munmap",
137            Self::Mprotect => "mprotect",
138            Self::Mmap => "mmap",
139            Self::Msync => "msync",
140            Self::Dup => "dup",
141            Self::Fcntl => "fcntl",
142            Self::Lseek => "lseek",
143            Self::Stat => "stat",
144            Self::Fstat => "fstat",
145            Self::Lstat => "lstat",
146            Self::Unlink => "unlink",
147            Self::Link => "link",
148            Self::Symlink => "symlink",
149            Self::Readlink => "readlink",
150            Self::Mkdir => "mkdir",
151            Self::Rmdir => "rmdir",
152            Self::Rename => "rename",
153            Self::Ftruncate => "ftruncate",
154            Self::Fsync => "fsync",
155            Self::Openat => "openat",
156            Self::Fstatat => "fstatat",
157            Self::Gettimeofday => "gettimeofday",
158            Self::ClockGettime => "clock_gettime",
159            Self::Sysctl => "sysctl",
160            Self::Sysctlbyname => "sysctlbyname",
161            Self::Sigprocmask => "sigprocmask",
162            Self::Sigaction => "sigaction",
163            Self::BsdthreadCreate => "bsdthread_create",
164            Self::BsdthreadTerminate => "bsdthread_terminate",
165            Self::BsdthreadRegister => "bsdthread_register",
166            Self::ThreadSelfid => "thread_selfid",
167            Self::Pipe => "pipe",
168            Self::Socket => "socket",
169            Self::Connect => "connect",
170            Self::Bind => "bind",
171            Self::Listen => "listen",
172            Self::Accept => "accept",
173            Self::Setsockopt => "setsockopt",
174            Self::Getsockopt => "getsockopt",
175            Self::Shutdown => "shutdown",
176            Self::Sendto => "sendto",
177            Self::Recvfrom => "recvfrom",
178            Self::Send => "send",
179            Self::Recv => "recv",
180            Self::Poll => "poll",
181            Self::Select => "select",
182            Self::Getsockname => "getsockname",
183            Self::Getpeername => "getpeername",
184        }
185    }
186
187    /// Primary Darwin syscall number.
188    #[must_use]
189    pub const fn number(self) -> u32 {
190        match self {
191            Self::Exit => 1,
192            Self::Read => 3,
193            Self::Write => 4,
194            Self::Open => 5,
195            Self::Close => 6,
196            Self::Unlink => 10,
197            Self::Link => 9,
198            Self::Getpid => 20,
199            Self::Getppid => 39,
200            Self::Access => 33,
201            Self::Dup => 41,
202            Self::Symlink => 57,
203            Self::Readlink => 58,
204            Self::Sigaction => 46,
205            Self::Sigprocmask => 48,
206            Self::Msync => 65,
207            Self::Munmap => 73,
208            Self::Mprotect => 74,
209            Self::Fcntl => 92,
210            Self::Fsync => 95,
211            Self::Gettimeofday => 116,
212            Self::Rename => 128,
213            Self::Mkdir => 136,
214            Self::Rmdir => 137,
215            Self::Mmap => 197,
216            Self::Lseek => 199,
217            Self::Ftruncate => 201,
218            Self::Sysctl => 202,
219            Self::ClockGettime => 266,
220            Self::Sysctlbyname => 274,
221            Self::Issetugid => 327,
222            // Prefer 64-bit variants as primary; aliases in lookup.
223            Self::Stat => 338,
224            Self::Fstat => 339,
225            Self::Lstat => 340,
226            Self::BsdthreadCreate => 360,
227            Self::BsdthreadTerminate => 361,
228            Self::BsdthreadRegister => 366,
229            Self::ThreadSelfid => 372,
230            Self::Openat => 463,
231            Self::Fstatat => 470,
232            Self::Pipe => 42,
233            Self::Accept => 30,
234            Self::Recvfrom | Self::Recv => 29,
235            Self::Socket => 97,
236            Self::Connect => 98,
237            Self::Bind => 104,
238            Self::Setsockopt => 105,
239            Self::Listen => 106,
240            Self::Getsockopt => 118,
241            Self::Sendto | Self::Send => 133,
242            Self::Shutdown => 134,
243            Self::Select => 93,
244            Self::Poll => 230,
245            Self::Getpeername => 31,
246            Self::Getsockname => 32,
247        }
248    }
249}
250
251/// Looks up a syscall by Darwin number (includes NOCANCEL / 64-bit aliases).
252#[must_use]
253pub const fn lookup(number: u32) -> Option<BsdSyscall> {
254    match number {
255        1 => Some(BsdSyscall::Exit),
256        3 | 396 => Some(BsdSyscall::Read),
257        4 | 397 => Some(BsdSyscall::Write),
258        5 | 398 => Some(BsdSyscall::Open),
259        6 | 399 => Some(BsdSyscall::Close),
260        9 => Some(BsdSyscall::Link),
261        10 | 401 => Some(BsdSyscall::Unlink),
262        20 => Some(BsdSyscall::Getpid),
263        33 => Some(BsdSyscall::Access),
264        39 => Some(BsdSyscall::Getppid),
265        29 | 403 => Some(BsdSyscall::Recvfrom), // recvfrom / nocancel
266        30 | 404 => Some(BsdSyscall::Accept),   // accept / nocancel
267        31 => Some(BsdSyscall::Getpeername),
268        32 => Some(BsdSyscall::Getsockname),
269        41 => Some(BsdSyscall::Dup),
270        42 | 405 => Some(BsdSyscall::Pipe), // pipe / nocancel-ish
271        46 => Some(BsdSyscall::Sigaction),
272        48 => Some(BsdSyscall::Sigprocmask),
273        57 => Some(BsdSyscall::Symlink),
274        58 => Some(BsdSyscall::Readlink),
275        65 => Some(BsdSyscall::Msync),
276        73 => Some(BsdSyscall::Munmap),
277        74 => Some(BsdSyscall::Mprotect),
278        92 => Some(BsdSyscall::Fcntl),
279        93 | 394 => Some(BsdSyscall::Select), // select / select_nocancel
280        95 | 406 => Some(BsdSyscall::Fsync),
281        97 => Some(BsdSyscall::Socket),
282        98 => Some(BsdSyscall::Connect),
283        104 => Some(BsdSyscall::Bind),
284        105 => Some(BsdSyscall::Setsockopt),
285        106 => Some(BsdSyscall::Listen),
286        116 => Some(BsdSyscall::Gettimeofday),
287        118 => Some(BsdSyscall::Getsockopt),
288        128 => Some(BsdSyscall::Rename),
289        133 | 407 => Some(BsdSyscall::Sendto), // sendto / nocancel
290        134 => Some(BsdSyscall::Shutdown),
291        136 => Some(BsdSyscall::Mkdir),
292        137 => Some(BsdSyscall::Rmdir),
293        188 | 338 => Some(BsdSyscall::Stat),  // stat / stat64
294        189 | 339 => Some(BsdSyscall::Fstat), // fstat / fstat64
295        190 | 340 => Some(BsdSyscall::Lstat), // lstat / lstat64
296        197 => Some(BsdSyscall::Mmap),
297        199 => Some(BsdSyscall::Lseek),
298        201 => Some(BsdSyscall::Ftruncate),
299        202 => Some(BsdSyscall::Sysctl),
300        230 => Some(BsdSyscall::Poll),
301        266 => Some(BsdSyscall::ClockGettime),
302        274 => Some(BsdSyscall::Sysctlbyname),
303        327 => Some(BsdSyscall::Issetugid),
304        360 => Some(BsdSyscall::BsdthreadCreate),
305        361 => Some(BsdSyscall::BsdthreadTerminate),
306        366 => Some(BsdSyscall::BsdthreadRegister),
307        372 => Some(BsdSyscall::ThreadSelfid),
308        463 => Some(BsdSyscall::Openat),
309        470 | 471 => Some(BsdSyscall::Fstatat), // fstatat / nocancel
310        _ => None,
311    }
312}
313
314/// Name for a raw number, if known.
315#[must_use]
316pub const fn name_of(number: u32) -> Option<&'static str> {
317    match lookup(number) {
318        Some(s) => Some(s.name()),
319        None => None,
320    }
321}
322
323/// Snapshot of the table used by unit tests / docs.
324#[must_use]
325pub fn known_syscalls() -> &'static [(u32, &'static str)] {
326    &[
327        (1, "exit"),
328        (3, "read"),
329        (4, "write"),
330        (5, "open"),
331        (6, "close"),
332        (20, "getpid"),
333        (33, "access"),
334        (39, "getppid"),
335        (41, "dup"),
336        (46, "sigaction"),
337        (48, "sigprocmask"),
338        (57, "symlink"),
339        (58, "readlink"),
340        (65, "msync"),
341        (73, "munmap"),
342        (74, "mprotect"),
343        (92, "fcntl"),
344        (116, "gettimeofday"),
345        (197, "mmap"),
346        (199, "lseek"),
347        (202, "sysctl"),
348        (266, "clock_gettime"),
349        (274, "sysctlbyname"),
350        (327, "issetugid"),
351        (9, "link"),
352        (10, "unlink"),
353        (95, "fsync"),
354        (128, "rename"),
355        (136, "mkdir"),
356        (137, "rmdir"),
357        (201, "ftruncate"),
358        (338, "stat"),
359        (339, "fstat"),
360        (340, "lstat"),
361        (463, "openat"),
362        (470, "fstatat"),
363        (360, "bsdthread_create"),
364        (361, "bsdthread_terminate"),
365        (366, "bsdthread_register"),
366        (372, "thread_selfid"),
367        (463, "openat"),
368        (42, "pipe"),
369        (97, "socket"),
370        (98, "connect"),
371        (104, "bind"),
372        (105, "setsockopt"),
373        (106, "listen"),
374        (118, "getsockopt"),
375        (133, "sendto"),
376        (134, "shutdown"),
377        (29, "recvfrom"),
378        (30, "accept"),
379        (93, "select"),
380        (230, "poll"),
381        (31, "getpeername"),
382        (32, "getsockname"),
383    ]
384}
385
386#[cfg(test)]
387mod tests {
388    use super::*;
389
390    #[test]
391    fn snapshot_table_consistent() {
392        for &(num, name) in known_syscalls() {
393            assert_eq!(name_of(num), Some(name));
394            assert_eq!(lookup(num).map(BsdSyscall::name), Some(name));
395        }
396    }
397
398    #[test]
399    fn nocancel_and_stat_aliases() {
400        assert_eq!(lookup(396), Some(BsdSyscall::Read));
401        assert_eq!(lookup(397), Some(BsdSyscall::Write));
402        assert_eq!(lookup(398), Some(BsdSyscall::Open));
403        assert_eq!(lookup(399), Some(BsdSyscall::Close));
404        assert_eq!(lookup(188), Some(BsdSyscall::Stat));
405        assert_eq!(lookup(189), Some(BsdSyscall::Fstat));
406    }
407}