univdreams 0.4.0

Byte-identical binary <-> source round-tripping: binary formats, multi-arch decode/encode, a .ud decompiler/compiler, and an x86 emulator with Win16/Win32/Linux hosts.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
//! Per-architecture Linux syscall adapters.
//!
//! The [`LinuxKernel`](super::LinuxKernel) is arch-agnostic: it works in
//! terms of a canonical [`Sysno`] and six `u64` arguments. A [`LinuxAbi`]
//! adapter bridges a concrete `Cpu` to it — it knows which registers
//! carry the syscall number / arguments / return value for its ABI, and
//! maps the architecture's syscall *numbers* (which differ wildly between
//! i386, x86-64 and aarch64) onto the canonical set.
//!
//! Only [`I386Abi`] is wired to a working CPU today (the interpreter is
//! 32-bit). The `x86-64` / `aarch64` number tables and register layouts
//! are provided so those adapters are a small addition once their CPU
//! back-ends exist — the kernel engine itself needs no changes.

use super::guest::GuestCpu;

/// Canonical syscall identity used inside the kernel engine. Per-arch
/// number tables map onto this; the engine switches on it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Sysno {
    Read,
    Write,
    Readv,
    Writev,
    Open,
    Openat,
    Close,
    Lseek,
    Brk,
    Mmap,
    Munmap,
    Mremap,
    Mprotect,
    Exit,
    ExitGroup,
    Fstat,
    Stat,
    Lstat,
    Newfstatat,
    Statx,
    Fcntl,
    Dup,
    Dup2,
    Dup3,
    Chdir,
    Fchdir,
    Faccessat,
    Umask,
    Fchmod,
    Fchmodat,
    Utimensat,
    Nanosleep,
    Poll,
    Ppoll,
    Select,
    Pselect6,
    Rename,
    Renameat,
    Renameat2,
    Chown,
    Pread64,
    Pwrite64,
    CopyFileRange,
    Fallocate,
    Uname,
    Getpid,
    Getppid,
    Gettid,
    Getuid,
    Geteuid,
    Getgid,
    Getegid,
    Access,
    Getcwd,
    Ioctl,
    ArchPrctl,
    SetTidAddress,
    SetRobustList,
    RtSigaction,
    RtSigprocmask,
    RtSigreturn,
    Time,
    ClockGettime,
    Gettimeofday,
    Futex,
    Getrandom,
    Readlink,
    Readlinkat,
    Getdents64,
    Mkdir,
    Mkdirat,
    Unlink,
    Unlinkat,
    Rmdir,
    Symlink,
    Symlinkat,
    Link,
    Linkat,
    Truncate,
    Ftruncate,
    Clone,
    Fork,
    Vfork,
    Execve,
    Wait4,
    Pipe,
    Pipe2,
    Flock,
    Statfs,
    Fstatfs,
    Socket,
    Connect,
    Bind,
    Listen,
    Accept,
    Sendto,
    Recvfrom,
    Sendmsg,
    Recvmsg,
    Shutdown,
    Getsockname,
    Getpeername,
    Setsockopt,
    Getsockopt,
    SchedYield,
    /// Recognised but intentionally a no-op-success for static-binary
    /// startup (TLS / threading setup we don't model).
    Ignored,
}

/// Byte offsets of the fields the kernel fills in a `stat` buffer, plus the
/// total struct size. The `stat` struct layout is architecture-specific
/// (i386 `stat64`, x86-64 `stat`, aarch64 `stat` all differ), so each ABI
/// reports its own. `st_mode`/`st_blksize`/`st_nlink` are written as `u32`,
/// `st_size`/`st_blocks` as `u64` (the union of all three layouts).
#[derive(Debug, Clone, Copy)]
pub struct StatLayout {
    pub size: usize,
    pub mode_off: usize,
    pub nlink_off: usize,
    pub size_off: usize,
    pub blksize_off: usize,
    pub blocks_off: usize,
    /// `st_dev` (8 bytes) and `st_ino` (8 bytes) offsets. The dynamic linker
    /// dedups libraries by `(dev, ino)`, so these must be unique per file or it
    /// aliases distinct `.so`s and drops their symbols.
    pub dev_off: usize,
    pub ino_off: usize,
}

/// The adapter a [`LinuxKernel`](super::LinuxKernel) talks to. Works over
/// a [`GuestCpu`] so any arch back-end plugs in.
pub trait LinuxAbi {
    /// Raw syscall number from the arch's number register.
    fn syscall_nr(&self, cpu: &dyn GuestCpu) -> u64;
    /// The six argument registers, in canonical order.
    fn syscall_args(&self, cpu: &dyn GuestCpu) -> [u64; 6];
    /// Write the return value (or `-errno`) to the arch's result register.
    fn set_return(&self, cpu: &mut dyn GuestCpu, ret: i64);
    /// Map a raw syscall number to the canonical [`Sysno`].
    fn map_syscall(&self, nr: u64) -> Option<Sysno>;
    /// Pointer width — 32 for i386, 64 for x86-64 / aarch64.
    fn ptr_bits(&self) -> u8;
    /// Field offsets + size of this ABI's `stat` buffer.
    fn stat_layout(&self) -> StatLayout;
}

/// Linux/i386 ABI: `int 0x80`, number in `eax`, args in
/// `ebx, ecx, edx, esi, edi, ebp`, return in `eax`. (Canonical x86 reg
/// indices: eax=0, ecx=1, edx=2, ebx=3, ebp=5, esi=6, edi=7.)
#[derive(Debug, Default, Clone, Copy)]
pub struct I386Abi;

impl LinuxAbi for I386Abi {
    fn syscall_nr(&self, cpu: &dyn GuestCpu) -> u64 {
        cpu.reg(0)
    }
    fn syscall_args(&self, cpu: &dyn GuestCpu) -> [u64; 6] {
        [
            cpu.reg(3),
            cpu.reg(1),
            cpu.reg(2),
            cpu.reg(6),
            cpu.reg(7),
            cpu.reg(5),
        ]
    }
    fn set_return(&self, cpu: &mut dyn GuestCpu, ret: i64) {
        cpu.set_reg(0, ret as u64);
    }
    fn map_syscall(&self, nr: u64) -> Option<Sysno> {
        Some(match nr {
            1 => Sysno::Exit,
            3 => Sysno::Read,
            4 => Sysno::Write,
            5 => Sysno::Open,
            6 => Sysno::Close,
            13 => Sysno::Time,
            19 => Sysno::Lseek,
            20 => Sysno::Getpid,
            24 => Sysno::Getuid,
            33 => Sysno::Access,
            45 => Sysno::Brk,
            49 => Sysno::Geteuid,
            47 => Sysno::Getgid,
            50 => Sysno::Getegid,
            54 => Sysno::Ioctl,
            64 => Sysno::Getppid,
            2 => Sysno::Fork,
            7 => Sysno::Wait4, // waitpid
            10 => Sysno::Unlink,
            11 => Sysno::Execve,
            12 => Sysno::Chdir,
            42 => Sysno::Pipe,
            114 => Sysno::Wait4,
            190 => Sysno::Vfork,
            331 => Sysno::Pipe2,
            143 => Sysno::Flock,
            99 => Sysno::Statfs,
            100 => Sysno::Fstatfs,
            268 => Sysno::Statfs,  // statfs64
            269 => Sysno::Fstatfs, // fstatfs64
            39 => Sysno::Mkdir,
            40 => Sysno::Rmdir,
            41 => Sysno::Dup,
            55 => Sysno::Fcntl,
            60 => Sysno::Umask,
            63 => Sysno::Dup2,
            94 => Sysno::Fchmod,
            133 => Sysno::Fchdir,
            142 => Sysno::Select, // _newselect
            162 => Sysno::Nanosleep,
            168 => Sysno::Poll,
            180 => Sysno::Pread64,
            181 => Sysno::Pwrite64,
            377 => Sysno::CopyFileRange,
            324 => Sysno::Fallocate,
            196 => Sysno::Lstat,     // lstat64
            221 => Sysno::Fcntl,     // fcntl64
            267 => Sysno::Nanosleep, // clock_nanosleep
            306 => Sysno::Fchmodat,
            307 => Sysno::Faccessat,
            309 => Sysno::Ppoll,
            308 => Sysno::Pselect6,
            320 => Sysno::Utimensat,
            330 => Sysno::Dup3,
            383 => Sysno::Statx,
            439 => Sysno::Faccessat, // faccessat2
            38 => Sysno::Rename,
            302 => Sysno::Renameat,
            353 => Sysno::Renameat2,
            // chown family (16- and 32-bit variants) — ownership is a no-op.
            182 => Sysno::Chown,
            212 => Sysno::Chown, // chown32
            198 => Sysno::Chown, // lchown32
            207 => Sysno::Chown, // fchown32
            298 => Sysno::Chown, // fchownat
            83 => Sysno::Symlink,
            85 => Sysno::Readlink,
            92 => Sysno::Truncate,
            93 => Sysno::Ftruncate,
            193 => Sysno::Truncate,  // truncate64
            194 => Sysno::Ftruncate, // ftruncate64
            220 => Sysno::Getdents64,
            296 => Sysno::Mkdirat,
            301 => Sysno::Unlinkat,
            304 => Sysno::Symlinkat,
            9 => Sysno::Link,
            303 => Sysno::Linkat,
            91 => Sysno::Munmap,
            163 => Sysno::Mremap,
            122 => Sysno::Uname,
            125 => Sysno::Mprotect,
            145 => Sysno::Readv,
            146 => Sysno::Writev,
            174 => Sysno::RtSigaction,
            175 => Sysno::RtSigprocmask,
            173 => Sysno::RtSigreturn,
            183 => Sysno::Getcwd,
            192 => Sysno::Mmap,  // mmap2 (page-offset variant) — treated as mmap
            195 => Sysno::Stat,  // stat64
            197 => Sysno::Fstat, // fstat64
            224 => Sysno::Gettid,
            243 => Sysno::Ignored, // set_thread_area (TLS) — accept
            258 => Sysno::SetTidAddress,
            295 => Sysno::Openat,
            300 => Sysno::Newfstatat, // fstatat64
            311 => Sysno::SetRobustList,
            252 => Sysno::ExitGroup,
            265 => Sysno::ClockGettime,
            78 => Sysno::Gettimeofday,
            240 => Sysno::Futex,
            355 => Sysno::Getrandom,
            340 => Sysno::Ignored, // prlimit64
            305 => Sysno::Readlinkat,
            _ => return None,
        })
    }
    fn ptr_bits(&self) -> u8 {
        32
    }
    fn stat_layout(&self) -> StatLayout {
        // struct stat64 (asm/stat.h): total 96 bytes.
        StatLayout {
            size: 96,
            mode_off: 16,
            nlink_off: 20,
            size_off: 44,
            blksize_off: 52,
            blocks_off: 56,
            dev_off: 0,
            ino_off: 88,
        }
    }
}

/// Linux/x86-64 ABI table + register layout. Inert until the interpreter
/// gains 64-bit execution; provided so adding that arch is a small step.
#[derive(Debug, Default, Clone, Copy)]
pub struct Amd64Abi;

impl Amd64Abi {
    /// Map an x86-64 syscall number to the canonical [`Sysno`].
    #[must_use]
    pub fn map(nr: u64) -> Option<Sysno> {
        Some(match nr {
            0 => Sysno::Read,
            1 => Sysno::Write,
            2 => Sysno::Open,
            3 => Sysno::Close,
            4 => Sysno::Stat,
            5 => Sysno::Fstat,
            6 => Sysno::Lstat,
            7 => Sysno::Poll,
            8 => Sysno::Lseek,
            23 => Sysno::Select,
            17 => Sysno::Pread64,
            18 => Sysno::Pwrite64,
            326 => Sysno::CopyFileRange,
            285 => Sysno::Fallocate,
            32 => Sysno::Dup,
            33 => Sysno::Dup2,
            35 => Sysno::Nanosleep,
            72 => Sysno::Fcntl,
            80 => Sysno::Chdir,
            81 => Sysno::Fchdir,
            91 => Sysno::Fchmod,
            95 => Sysno::Umask,
            230 => Sysno::Nanosleep, // clock_nanosleep → treat as nanosleep
            268 => Sysno::Fchmodat,
            269 => Sysno::Faccessat,
            270 => Sysno::Pselect6,
            82 => Sysno::Rename,
            264 => Sysno::Renameat,
            316 => Sysno::Renameat2,
            92 => Sysno::Chown,
            93 => Sysno::Chown,  // fchown
            94 => Sysno::Chown,  // lchown
            260 => Sysno::Chown, // fchownat
            271 => Sysno::Ppoll,
            280 => Sysno::Utimensat,
            292 => Sysno::Dup3,
            332 => Sysno::Statx,
            439 => Sysno::Faccessat, // faccessat2
            9 => Sysno::Mmap,
            10 => Sysno::Mprotect,
            11 => Sysno::Munmap,
            25 => Sysno::Mremap,
            12 => Sysno::Brk,
            13 => Sysno::RtSigaction,
            14 => Sysno::RtSigprocmask,
            15 => Sysno::RtSigreturn,
            16 => Sysno::Ioctl,
            19 => Sysno::Readv,
            20 => Sysno::Writev,
            21 => Sysno::Access,
            39 => Sysno::Getpid,
            60 => Sysno::Exit,
            63 => Sysno::Uname,
            79 => Sysno::Getcwd,
            102 => Sysno::Getuid,
            104 => Sysno::Getgid,
            107 => Sysno::Geteuid,
            108 => Sysno::Getegid,
            110 => Sysno::Getppid,
            158 => Sysno::ArchPrctl,
            186 => Sysno::Gettid,
            218 => Sysno::SetTidAddress,
            231 => Sysno::ExitGroup,
            257 => Sysno::Openat,
            262 => Sysno::Newfstatat,
            273 => Sysno::SetRobustList,
            228 => Sysno::ClockGettime,
            96 => Sysno::Gettimeofday,
            202 => Sysno::Futex,
            204 => Sysno::Ignored, // sched_getaffinity
            302 => Sysno::Ignored, // prlimit64
            318 => Sysno::Getrandom,
            334 => Sysno::Ignored, // rseq — restartable sequences, ignore
            267 => Sysno::Readlinkat,
            76 => Sysno::Truncate,
            77 => Sysno::Ftruncate,
            83 => Sysno::Mkdir,
            84 => Sysno::Rmdir,
            87 => Sysno::Unlink,
            88 => Sysno::Symlink,
            89 => Sysno::Readlink,
            217 => Sysno::Getdents64,
            258 => Sysno::Mkdirat,
            263 => Sysno::Unlinkat,
            266 => Sysno::Symlinkat,
            86 => Sysno::Link,
            265 => Sysno::Linkat,
            56 => Sysno::Clone,
            57 => Sysno::Fork,
            58 => Sysno::Vfork,
            59 => Sysno::Execve,
            61 => Sysno::Wait4,
            22 => Sysno::Pipe,
            293 => Sysno::Pipe2,
            73 => Sysno::Flock,
            137 => Sysno::Statfs,
            138 => Sysno::Fstatfs,
            41 => Sysno::Socket,
            42 => Sysno::Connect,
            43 => Sysno::Accept,
            44 => Sysno::Sendto,
            45 => Sysno::Recvfrom,
            46 => Sysno::Sendmsg,
            47 => Sysno::Recvmsg,
            48 => Sysno::Shutdown,
            49 => Sysno::Bind,
            50 => Sysno::Listen,
            51 => Sysno::Getsockname,
            52 => Sysno::Getpeername,
            54 => Sysno::Setsockopt,
            55 => Sysno::Getsockopt,
            288 => Sysno::Accept,  // accept4
            165 => Sysno::Ignored, // mount — accept (we don't model mount points)
            166 => Sysno::Ignored, // umount2
            24 => Sysno::SchedYield,
            28 => Sysno::Ignored, // madvise — advisory, safe to no-op
            _ => return None,
        })
    }
}

/// Linux/x86-64 ABI: `syscall` (0F 05), number in `rax`, args in
/// `rdi, rsi, rdx, r10, r8, r9`, return in `rax`. (Canonical x86-64 reg
/// indices: rax=0, rcx=1, rdx=2, rbx=3, rsp=4, rbp=5, rsi=6, rdi=7,
/// r8=8, r9=9, r10=10.)
impl LinuxAbi for Amd64Abi {
    fn syscall_nr(&self, cpu: &dyn GuestCpu) -> u64 {
        cpu.reg(0)
    }
    fn syscall_args(&self, cpu: &dyn GuestCpu) -> [u64; 6] {
        [
            cpu.reg(7),
            cpu.reg(6),
            cpu.reg(2),
            cpu.reg(10),
            cpu.reg(8),
            cpu.reg(9),
        ]
    }
    fn set_return(&self, cpu: &mut dyn GuestCpu, ret: i64) {
        cpu.set_reg(0, ret as u64);
    }
    fn map_syscall(&self, nr: u64) -> Option<Sysno> {
        Self::map(nr)
    }
    fn ptr_bits(&self) -> u8 {
        64
    }
    fn stat_layout(&self) -> StatLayout {
        // struct stat (x86-64 asm/stat.h): total 144 bytes.
        StatLayout {
            size: 144,
            mode_off: 24,
            nlink_off: 16,
            size_off: 48,
            blksize_off: 56,
            blocks_off: 64,
            dev_off: 0,
            ino_off: 8,
        }
    }
}

/// Linux/aarch64 ABI table (generic Linux syscall numbers). Inert until
/// an aarch64 executor exists.
#[derive(Debug, Default, Clone, Copy)]
pub struct Aarch64Abi;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::emulator::emulator::Cpu;
    use crate::emulator::emulator::regs::Reg32;

    #[test]
    fn i386_reads_nr_args_and_writes_return() {
        let mut cpu = Cpu::new();
        cpu.regs.set32(Reg32::Eax, 4); // write
        cpu.regs.set32(Reg32::Ebx, 1); // fd
        cpu.regs.set32(Reg32::Ecx, 0x1234); // buf
        cpu.regs.set32(Reg32::Edx, 3); // len
        let abi = I386Abi;
        assert_eq!(abi.syscall_nr(&cpu), 4);
        assert_eq!(abi.syscall_args(&cpu)[..4], [1, 0x1234, 3, 0][..4]);
        assert_eq!(abi.map_syscall(4), Some(Sysno::Write));
        assert_eq!(abi.map_syscall(1), Some(Sysno::Exit));
        assert_eq!(abi.map_syscall(45), Some(Sysno::Brk));
        assert_eq!(abi.map_syscall(9999), None);
        abi.set_return(&mut cpu, -38);
        assert_eq!(cpu.regs.get32(Reg32::Eax), (-38i32) as u32);
    }

    #[test]
    fn stat_layouts_match_known_struct_sizes() {
        // The three ABIs report architecture-correct stat struct geometry.
        let i386 = I386Abi.stat_layout();
        assert_eq!((i386.size, i386.mode_off, i386.size_off), (96, 16, 44));
        let amd64 = Amd64Abi.stat_layout();
        assert_eq!((amd64.size, amd64.mode_off, amd64.size_off), (144, 24, 48));
        let a64 = Aarch64Abi.stat_layout();
        assert_eq!((a64.size, a64.mode_off, a64.size_off), (128, 16, 48));
        // Every field must fit inside its struct (u64 fields need 8 bytes).
        for l in [i386, amd64, a64] {
            assert!(l.size_off + 8 <= l.size && l.blocks_off + 8 <= l.size);
            assert!(l.mode_off + 4 <= l.size && l.nlink_off + 4 <= l.size);
        }
    }

    #[test]
    fn new_dir_syscall_numbers_map() {
        assert_eq!(I386Abi.map_syscall(220), Some(Sysno::Getdents64));
        assert_eq!(I386Abi.map_syscall(85), Some(Sysno::Readlink));
        assert_eq!(Amd64Abi::map(217), Some(Sysno::Getdents64));
        assert_eq!(Amd64Abi::map(83), Some(Sysno::Mkdir));
        assert_eq!(Aarch64Abi::map(61), Some(Sysno::Getdents64));
        assert_eq!(Aarch64Abi::map(35), Some(Sysno::Unlinkat));
    }

    #[test]
    fn arch_tables_agree_on_common_calls() {
        // The canonical mapping must be consistent across arches.
        assert_eq!(Amd64Abi::map(1), Some(Sysno::Write));
        assert_eq!(Amd64Abi::map(60), Some(Sysno::Exit));
        assert_eq!(Aarch64Abi::map(64), Some(Sysno::Write));
        assert_eq!(Aarch64Abi::map(93), Some(Sysno::Exit));
    }
}

impl Aarch64Abi {
    /// Map an aarch64 (generic asm-generic) syscall number to [`Sysno`].
    #[must_use]
    pub fn map(nr: u64) -> Option<Sysno> {
        Some(match nr {
            17 => Sysno::Getcwd,
            23 => Sysno::Dup,
            24 => Sysno::Dup3,
            25 => Sysno::Fcntl,
            29 => Sysno::Ioctl,
            48 => Sysno::Faccessat,
            49 => Sysno::Chdir,
            50 => Sysno::Fchdir,
            52 => Sysno::Fchmod,
            53 => Sysno::Fchmodat,
            56 => Sysno::Openat,
            67 => Sysno::Pread64,
            68 => Sysno::Pwrite64,
            47 => Sysno::Fallocate,
            285 => Sysno::CopyFileRange,
            72 => Sysno::Pselect6,
            38 => Sysno::Renameat,
            276 => Sysno::Renameat2,
            54 => Sysno::Chown, // fchownat
            55 => Sysno::Chown, // fchown
            73 => Sysno::Ppoll,
            88 => Sysno::Utimensat,
            101 => Sysno::Nanosleep,
            115 => Sysno::Nanosleep, // clock_nanosleep
            166 => Sysno::Umask,
            221 => Sysno::Execve,
            260 => Sysno::Wait4,
            59 => Sysno::Pipe2,
            32 => Sysno::Flock,
            43 => Sysno::Statfs,
            44 => Sysno::Fstatfs,
            198 => Sysno::Socket,
            200 => Sysno::Bind,
            201 => Sysno::Listen,
            202 => Sysno::Accept,
            203 => Sysno::Connect,
            204 => Sysno::Getsockname,
            205 => Sysno::Getpeername,
            206 => Sysno::Sendto,
            207 => Sysno::Recvfrom,
            208 => Sysno::Setsockopt,
            209 => Sysno::Getsockopt,
            210 => Sysno::Shutdown,
            211 => Sysno::Sendmsg,
            212 => Sysno::Recvmsg,
            242 => Sysno::Accept, // accept4
            291 => Sysno::Statx,
            57 => Sysno::Close,
            63 => Sysno::Read,
            64 => Sysno::Write,
            65 => Sysno::Readv,
            66 => Sysno::Writev,
            79 => Sysno::Newfstatat,
            80 => Sysno::Fstat,
            93 => Sysno::Exit,
            94 => Sysno::ExitGroup,
            96 => Sysno::SetTidAddress,
            99 => Sysno::SetRobustList,
            134 => Sysno::RtSigaction,
            135 => Sysno::RtSigprocmask,
            139 => Sysno::RtSigreturn,
            160 => Sysno::Uname,
            172 => Sysno::Getpid,
            173 => Sysno::Getppid,
            174 => Sysno::Getuid,
            175 => Sysno::Geteuid,
            176 => Sysno::Getgid,
            177 => Sysno::Getegid,
            178 => Sysno::Gettid,
            214 => Sysno::Brk,
            215 => Sysno::Munmap,
            216 => Sysno::Mremap,
            222 => Sysno::Mmap,
            226 => Sysno::Mprotect,
            113 => Sysno::ClockGettime,
            169 => Sysno::Gettimeofday,
            98 => Sysno::Futex,
            278 => Sysno::Getrandom,
            261 => Sysno::Ignored, // prlimit64
            78 => Sysno::Readlinkat,
            45 => Sysno::Truncate,
            46 => Sysno::Ftruncate,
            61 => Sysno::Getdents64,
            34 => Sysno::Mkdirat,
            35 => Sysno::Unlinkat,
            36 => Sysno::Symlinkat,
            37 => Sysno::Linkat,
            _ => return None,
        })
    }
}

/// Linux/aarch64 ABI: `svc #0`, number in `x8`, args in `x0..x5`,
/// return in `x0`. (Canonical aarch64 reg indices are simply `x0..x30`.)
impl LinuxAbi for Aarch64Abi {
    fn syscall_nr(&self, cpu: &dyn GuestCpu) -> u64 {
        cpu.reg(8)
    }
    fn syscall_args(&self, cpu: &dyn GuestCpu) -> [u64; 6] {
        [
            cpu.reg(0),
            cpu.reg(1),
            cpu.reg(2),
            cpu.reg(3),
            cpu.reg(4),
            cpu.reg(5),
        ]
    }
    fn set_return(&self, cpu: &mut dyn GuestCpu, ret: i64) {
        cpu.set_reg(0, ret as u64);
    }
    fn map_syscall(&self, nr: u64) -> Option<Sysno> {
        Self::map(nr)
    }
    fn ptr_bits(&self) -> u8 {
        64
    }
    fn stat_layout(&self) -> StatLayout {
        // struct stat (arm64 asm-generic): total 128 bytes.
        StatLayout {
            size: 128,
            mode_off: 16,
            nlink_off: 20,
            size_off: 48,
            blksize_off: 56,
            blocks_off: 64,
            dev_off: 0,
            ino_off: 8,
        }
    }
}