oscall 0.1.0

Low-level cross-platform interface to operating system services: files, memory mapping, networking and more, with no libc on Linux
Documentation
//! x86: `int 0x80`, number in `eax`, arguments in `ebx ecx edx esi edi ebp`.
//!
//! `ebx`, `esi` and `ebp` are reserved by the compiler and cannot be named as
//! inline assembly operands. Up to three arguments, the first one is swapped
//! into `ebx` around the call. From four, the number and the arguments that
//! go in the reserved registers are loaded from memory, with the three
//! registers saved on the stack.

use core::arch::asm;

/// # Safety
///
/// The system call and its arguments must be valid: see `syscall!`.
#[inline]
pub(crate) unsafe fn syscall0(nr: usize) -> usize {
    let ret;
    // SAFETY: the caller upholds the system call's own contract.
    unsafe {
        asm!(
            "int 0x80",
            inlateout("eax") nr => ret,
            options(nostack, preserves_flags),
        );
    }
    ret
}

macro_rules! define {
    ($name:ident $(, $arg:ident in $reg:tt)*) => {
        /// # Safety
        ///
        /// The system call and its arguments must be valid: see `syscall!`.
        #[inline]
        pub(crate) unsafe fn $name(nr: usize, a: usize $(, $arg: usize)*) -> usize {
            let ret;
            // SAFETY: the caller upholds the system call's own contract; ebx
            // is swapped back before the block ends.
            unsafe {
                asm!(
                    "xchg ebx, {a}",
                    "int 0x80",
                    "xchg ebx, {a}",
                    a = in(reg) a,
                    inlateout("eax") nr => ret,
                    $(in($reg) $arg,)*
                    options(nostack, preserves_flags),
                );
            }
            ret
        }
    };
}

define!(syscall1);
define!(syscall2, b in "ecx");
define!(syscall3, b in "ecx", c in "edx");

/// # Safety
///
/// The system call and its arguments must be valid: see `syscall!`.
#[inline]
pub(crate) unsafe fn syscall4(nr: usize, a: usize, b: usize, c: usize, d: usize) -> usize {
    // SAFETY: as for `syscall6`; a system call ignores the arguments it does
    // not take.
    unsafe { syscall6(nr, a, b, c, d, 0, 0) }
}

/// # Safety
///
/// The system call and its arguments must be valid: see `syscall!`.
#[inline]
pub(crate) unsafe fn syscall5(
    nr: usize,
    a: usize,
    b: usize,
    c: usize,
    d: usize,
    e: usize,
) -> usize {
    // SAFETY: as for `syscall4`.
    unsafe { syscall6(nr, a, b, c, d, e, 0) }
}

/// # Safety
///
/// The system call and its arguments must be valid: see `syscall!`.
#[inline]
pub(crate) unsafe fn syscall6(
    nr: usize,
    a: usize,
    b: usize,
    c: usize,
    d: usize,
    e: usize,
    f: usize,
) -> usize {
    let block = [nr, a, d, f];
    let ret;
    // SAFETY: the caller upholds the system call's own contract; ebx, esi
    // and ebp are restored from the stack before the block ends, and `block`
    // outlives the reads from it.
    unsafe {
        asm!(
            "push ebp",
            "push ebx",
            "push esi",
            "mov ebx, [eax + 4]",
            "mov esi, [eax + 8]",
            "mov ebp, [eax + 12]",
            "mov eax, [eax]",
            "int 0x80",
            "pop esi",
            "pop ebx",
            "pop ebp",
            inlateout("eax") block.as_ptr() => ret,
            in("ecx") b,
            in("edx") c,
            in("edi") e,
            options(preserves_flags),
        );
    }
    ret
}