//! Minimal `AArch64` Linux userspace ABI vocabulary. /// A syscall implemented by the first static-program runtime. #[derive(Clone, Copy, Debug, Eq, PartialEq)] #[repr(u64)] pub enum Syscall { Getcwd = 17, Dup3 = 24, Fcntl = 25, Ioctl = 29, Flock = 32, Mkdirat = 34, Unlinkat = 35, Linkat = 37, Renameat = 38, Ftruncate = 46, Faccessat = 48, Chdir = 49, Fchmod = 52, Openat = 56, Close = 57, Pipe2 = 59, Getdents64 = 61, Lseek = 62, Read = 63, Write = 64, Writev = 66, Pread64 = 67, Sendfile = 71, Readlinkat = 78, Newfstatat = 79, Fstat = 80, Utimensat = 88, Ppoll = 73, Exit = 93, ExitGroup = 94, SetTidAddress = 96, Futex = 98, ClockGettime = 113, SchedGetaffinity = 123, SchedYield = 124, Tkill = 130, Sigaltstack = 132, RtSigaction = 134, RtSigprocmask = 135, Getpid = 172, Getppid = 173, Gettid = 178, Socketpair = 199, Recvfrom = 207, Munmap = 215, Clone = 220, Execve = 221, Mmap = 222, Mprotect = 226, Getrandom = 278, CopyFileRange = 285, Wait4 = 260, } impl Syscall { /// Decode an `AArch64` Linux syscall number from `x8`. #[must_use] pub const fn from_number(number: u64) -> Option { match number { 17 => Some(Self::Getcwd), 24 => Some(Self::Dup3), 25 => Some(Self::Fcntl), 29 => Some(Self::Ioctl), 32 => Some(Self::Flock), 34 => Some(Self::Mkdirat), 35 => Some(Self::Unlinkat), 37 => Some(Self::Linkat), 38 => Some(Self::Renameat), 46 => Some(Self::Ftruncate), 48 => Some(Self::Faccessat), 49 => Some(Self::Chdir), 52 => Some(Self::Fchmod), 56 => Some(Self::Openat), 57 => Some(Self::Close), 59 => Some(Self::Pipe2), 61 => Some(Self::Getdents64), 62 => Some(Self::Lseek), 63 => Some(Self::Read), 73 => Some(Self::Ppoll), 64 => Some(Self::Write), 66 => Some(Self::Writev), 67 => Some(Self::Pread64), 71 => Some(Self::Sendfile), 78 => Some(Self::Readlinkat), 79 => Some(Self::Newfstatat), 80 => Some(Self::Fstat), 88 => Some(Self::Utimensat), 93 => Some(Self::Exit), 94 => Some(Self::ExitGroup), 96 => Some(Self::SetTidAddress), 98 => Some(Self::Futex), 113 => Some(Self::ClockGettime), 123 => Some(Self::SchedGetaffinity), 124 => Some(Self::SchedYield), 130 => Some(Self::Tkill), 132 => Some(Self::Sigaltstack), 134 => Some(Self::RtSigaction), 135 => Some(Self::RtSigprocmask), 172 => Some(Self::Getpid), 173 => Some(Self::Getppid), 178 => Some(Self::Gettid), 199 => Some(Self::Socketpair), 207 => Some(Self::Recvfrom), 215 => Some(Self::Munmap), 220 => Some(Self::Clone), 221 => Some(Self::Execve), 222 => Some(Self::Mmap), 226 => Some(Self::Mprotect), 278 => Some(Self::Getrandom), 285 => Some(Self::CopyFileRange), 260 => Some(Self::Wait4), _ => None, } } /// Return the ABI syscall number. #[must_use] pub const fn number(self) -> u64 { self as u64 } } /// Linux errno values returned by the initial syscall layer. #[derive(Clone, Copy, Debug, Eq, PartialEq)] #[repr(i32)] pub enum Errno { NoEntry = 2, ArgumentListTooLong = 7, ExecutableFormat = 8, BadFileDescriptor = 9, NoChild = 10, TryAgain = 11, OutOfMemory = 12, PermissionDenied = 13, Fault = 14, AlreadyExists = 17, NotDirectory = 20, IsDirectory = 21, InvalidArgument = 22, TooManyOpenFiles = 24, NotTty = 25, NoSpace = 28, IllegalSeek = 29, BrokenPipe = 32, Range = 34, NoSystemCall = 38, DirectoryNotEmpty = 39, } impl Errno { /// Encode `-errno` in the unsigned `x0` syscall-return register. #[must_use] pub const fn return_value(self) -> u64 { (-(self as i64)).cast_unsigned() } } #[cfg(test)] mod tests { use super::{Errno, Syscall}; #[test] fn decodes_the_minimal_aarch64_table() { assert_eq!(Syscall::from_number(17), Some(Syscall::Getcwd)); assert_eq!(Syscall::from_number(24), Some(Syscall::Dup3)); assert_eq!(Syscall::from_number(25), Some(Syscall::Fcntl)); assert_eq!(Syscall::from_number(29), Some(Syscall::Ioctl)); assert_eq!(Syscall::from_number(32), Some(Syscall::Flock)); assert_eq!(Syscall::from_number(34), Some(Syscall::Mkdirat)); assert_eq!(Syscall::from_number(35), Some(Syscall::Unlinkat)); assert_eq!(Syscall::from_number(37), Some(Syscall::Linkat)); assert_eq!(Syscall::from_number(38), Some(Syscall::Renameat)); assert_eq!(Syscall::from_number(46), Some(Syscall::Ftruncate)); assert_eq!(Syscall::from_number(48), Some(Syscall::Faccessat)); assert_eq!(Syscall::from_number(49), Some(Syscall::Chdir)); assert_eq!(Syscall::from_number(52), Some(Syscall::Fchmod)); assert_eq!(Syscall::from_number(56), Some(Syscall::Openat)); assert_eq!(Syscall::from_number(57), Some(Syscall::Close)); assert_eq!(Syscall::from_number(59), Some(Syscall::Pipe2)); assert_eq!(Syscall::from_number(61), Some(Syscall::Getdents64)); assert_eq!(Syscall::from_number(62), Some(Syscall::Lseek)); assert_eq!(Syscall::from_number(63), Some(Syscall::Read)); assert_eq!(Syscall::from_number(64), Some(Syscall::Write)); assert_eq!(Syscall::from_number(66), Some(Syscall::Writev)); assert_eq!(Syscall::from_number(78), Some(Syscall::Readlinkat)); assert_eq!(Syscall::from_number(79), Some(Syscall::Newfstatat)); assert_eq!(Syscall::from_number(67), Some(Syscall::Pread64)); assert_eq!(Syscall::from_number(71), Some(Syscall::Sendfile)); assert_eq!(Syscall::from_number(80), Some(Syscall::Fstat)); assert_eq!(Syscall::from_number(88), Some(Syscall::Utimensat)); assert_eq!(Syscall::from_number(73), Some(Syscall::Ppoll)); assert_eq!(Syscall::from_number(93), Some(Syscall::Exit)); assert_eq!(Syscall::from_number(94), Some(Syscall::ExitGroup)); assert_eq!(Syscall::from_number(96), Some(Syscall::SetTidAddress)); assert_eq!(Syscall::from_number(98), Some(Syscall::Futex)); assert_eq!(Syscall::from_number(113), Some(Syscall::ClockGettime)); assert_eq!(Syscall::from_number(123), Some(Syscall::SchedGetaffinity)); assert_eq!(Syscall::from_number(124), Some(Syscall::SchedYield)); assert_eq!(Syscall::from_number(130), Some(Syscall::Tkill)); assert_eq!(Syscall::from_number(132), Some(Syscall::Sigaltstack)); assert_eq!(Syscall::from_number(134), Some(Syscall::RtSigaction)); assert_eq!(Syscall::from_number(135), Some(Syscall::RtSigprocmask)); assert_eq!(Syscall::from_number(172), Some(Syscall::Getpid)); assert_eq!(Syscall::from_number(173), Some(Syscall::Getppid)); assert_eq!(Syscall::from_number(178), Some(Syscall::Gettid)); assert_eq!(Syscall::from_number(199), Some(Syscall::Socketpair)); assert_eq!(Syscall::from_number(207), Some(Syscall::Recvfrom)); assert_eq!(Syscall::from_number(215), Some(Syscall::Munmap)); assert_eq!(Syscall::from_number(220), Some(Syscall::Clone)); assert_eq!(Syscall::from_number(221), Some(Syscall::Execve)); assert_eq!(Syscall::from_number(222), Some(Syscall::Mmap)); assert_eq!(Syscall::from_number(226), Some(Syscall::Mprotect)); assert_eq!(Syscall::from_number(278), Some(Syscall::Getrandom)); assert_eq!(Syscall::from_number(285), Some(Syscall::CopyFileRange)); assert_eq!(Syscall::from_number(260), Some(Syscall::Wait4)); assert_eq!(Syscall::from_number(55), None); } #[test] fn encodes_negative_errno_returns() { assert_eq!(Errno::BadFileDescriptor.return_value(), u64::MAX - 8); assert_eq!(Errno::NoSystemCall.return_value().cast_signed(), -38); } }