syscall(2)

๐Ÿฆ‹ syscall โ€” indirect system call ๐Ÿฆ‹

okay bestie so you've opened the syscall(2) page, which lives in the System Calls Manual, and honestly? this one's a whole journey. grab a snack. ๐Ÿ“


โœจ NAME โœจ

syscall โ€” indirect system call

๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, -lc) โ€” she's built in, no extra linking drama. ๐Ÿซถ

๐ŸŽ€ SYNOPSIS ๐ŸŽ€

#include <sys/syscall.h>      /* Definition of SYS_* constants */
#include <unistd.h>

long syscall(long number, ...);

๐Ÿ’… Feature Test Macro Requirements for glibc ๐Ÿ’…

(psst, that's feature_test_macros(7) if you wanna go deeper)

syscall():

  • Since glibc 2.19:
    • _DEFAULT_SOURCE
  • Before glibc 2.19:
    • _BSD_SOURCE || _SVID_SOURCE
yeah, if you're on ancient glibc you're gonna need those old macros. she's giving legacy energy. ๐Ÿ’€

๐ŸŒธ DESCRIPTION ๐ŸŒธ

So syscall() is this lil library function that goes and invokes the system call whose assembly-language interface has the number you give it, passing along the arguments you give it. it's the direct line to the kernel, no middleman.

when is this useful? when you want to call a system call that has no wrapper function in the C library โ€” like, glibc just said "not my problem" and you have to dial the kernel yourself. ๐Ÿ“ž

what syscall() does under the hood, step by step:

  • it saves the CPU registers before making the system call
  • it restores those registers once the system call returns
  • and it stores any error that the system call handed back into errno(3) ๐Ÿ”ฅ

and where do you get those iconic system call numbers? symbolic constants live in the header file <sys/syscall.h>. they're all like SYS_ something. you got this. โœจ


๐Ÿ‘‘ RETURN VALUE ๐Ÿ‘‘

okay so the return value is literally defined by the system call being invoked. it's not syscall()'s call to make, she's just the messenger.

but in general, the vibes are:

  • a 0 return value = success ๐ŸŽ‰
  • a -1 return value = an error happened, and the error number got stashed in errno

periodt.


๐Ÿ˜ญ ERRORS ๐Ÿ˜ญ

  • ENOSYS โ€” the system call number you asked for is not implemented. she doesn't exist. pack it up. ๐Ÿ™ˆ
  • Other errors โ€” these are specific to whatever system call you actually invoked, so go read that page.

๐Ÿงš HISTORY ๐Ÿงš

syscall() first appeared in 4BSD. she's an elder, respect her. ๐Ÿ‘‘


โญ NOTES โญ

๐ŸŒˆ Architecture-specific requirements ๐ŸŒˆ

not me about to explain that every architecture ABI has its own opinions on how system call arguments get passed to the kernel. ๐Ÿ˜ฎโ€๐Ÿ’จ

here's the tea: for system calls that have a glibc wrapper (like, most of them), glibc handles all the fiddly details of copying arguments into the right registers in whatever way suits the architecture. easy. but when you use syscall() to make a system call yourself, you the caller might have to handle architecture-dependent details. you're on your own, bestie. and this requirement hits most commonly on certain 32-bit architectures.

for example โ€” on the ARM architecture Embedded ABI (EABI), a 64-bit value (like a long long) must be aligned to an even register pair. so if you use syscall() instead of the wrapper glibc would've given you, invoking the readahead(2) system call would look like this on ARM with EABI in little endian mode:

syscall(SYS_readahead, fd, 0,
        (unsigned int) (offset & 0xFFFFFFFF),
        (unsigned int) (offset >> 32),
        count);

so why the 0? because offset is 64 bits, and the first argument (fd) gets passed in r0, so you have to manually split and align that 64-bit value so it lands in the r2/r3 register pair. that means inserting a dummy value into r1 (hence the second argument of 0). and you have to be careful that the split follows endian conventions (according to the C ABI for the platform). no cap.

similar issues can pop up on:

  • MIPS with the O32 ABI
  • PowerPC with the 32-bit ABI
  • parisc with the 32-bit ABI
  • Xtensa
heads up: even though the parisc C ABI also uses aligned register pairs, it uses a shim layer to hide the whole issue from user space. so, lucky them. ๐Ÿ’…

the affected system calls are: fadvise64_64(2), ftruncate64(2), posix_fadvise(2), pread64(2), pwrite64(2), readahead(2), sync_file_range(2), and truncate64(2).

and this does not affect syscalls that manually split and assemble 64-bit values such as _llseek(2), preadv(2), preadv2(2), pwritev(2), and pwritev2(2). they already did the work. welcome to the wonderful world of historical baggage. ๐Ÿงณ

๐Ÿฆ‹ Architecture calling conventions ๐Ÿฆ‹

every architecture has its own way of invoking the kernel and passing arguments to it. the details for the various architectures are listed in the two tables below.

the first table lists: the instruction used to transition to kernel mode (which might not be the fastest or best way to transition to the kernel, so you might have to refer to vdso(7)), the register used to indicate the system call number, the register(s) used to return the system call result, and the register used to signal an error.

Arch/ABI    Instruction           System  Ret  Ret  Error    Notes
                                  call #  val  val2
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
alpha       callsys               v0      v0   a4   a3       1, 6
arc         trap0                 r8      r0   -    -
arm/OABI    swi NR                -       r0   -    -        2
arm/EABI    swi 0x0               r7      r0   r1   -
arm64       svc #0                w8      x0   x1   -
blackfin    excpt 0x0             P0      R0   -    -
i386        int $0x80             eax     eax  edx  -
ia64        break 0x100000        r15     r8   r9   r10      1, 6
loongarch   syscall 0             a7      a0   -    -
m68k        trap #0               d0      d0   -    -
microblaze  brki r14,8            r12     r3   -    -
mips        syscall               v0      v0   v1   a3       1, 6
nios2       trap                  r2      r2   -    r7
parisc      ble 0x100(%sr2, %r0)  r20     r28  -    -
powerpc     sc                    r0      r3   -    r0       1
powerpc64   sc                    r0      r3   -    cr0.SO   1
riscv       ecall                 a7      a0   a1   -
s390        svc 0                 r1      r2   r3   -        3
s390x       svc 0                 r1      r2   r3   -        3
superh      trapa #31             r3      r0   r1   -        4, 6
sparc/32    t 0x10                g1      o0   o1   psr/csr  1, 6
sparc/64    t 0x6d                g1      o0   o1   psr/csr  1, 6
tile        swint1                R10     R00  -    R01      1
x86-64      syscall               eax     rax  rdx  -        5
x32         syscall               eax     rax  rdx  -        5
xtensa      syscall               a2      a2   -    -

Notes:

  • [1] On a few architectures, a register is used as a boolean (0 indicating no error, and -1 indicating an error) to signal that the system call failed. the actual error value is still contained in the return register. on sparc, the carry bit (csr) in the processor status register (psr) is used instead of a full register. on powerpc64, the summary overflow bit (SO) in field 0 of the condition register (cr0) is used.
  • [2] NR is the system call number.
  • [3] For s390 and s390x, NR (the system call number) may be passed directly with svc NR if it is less than 256.
  • [4] On SuperH additional trap numbers are supported for historic reasons, but trapa#31 is the recommended "unified" ABI.
  • [5] The x32 ABI shares syscall table with x86-64 ABI, but there are some nuances:
    • In order to indicate that a system call is called under the x32 ABI, an additional bit, __X32_SYSCALL_BIT, is bitwise ORed with the system call number. the ABI used by a process affects some process behaviors, including signal handling or system call restarting.
    • Since x32 has different sizes for long and pointer types, layouts of some (but not all; struct timeval or struct rlimit are 64-bit, for example) structures are different. in order to handle this, additional system calls are added to the system call table, starting from number 512 (without the __X32_SYSCALL_BIT). for example, __NR_readv is defined as 19 for the x86-64 ABI and as __X32_SYSCALL_BIT | 515 for the x32 ABI. most of these additional system calls are actually identical to the system calls used for providing i386 compat. there are some notable exceptions, however, such as preadv2(2), which uses struct iovec entities with 4-byte pointers and sizes ("compat_iovec" in kernel terms), but passes an 8-byte pos argument in a single register and not two, as is done in every other ABI.
  • [6] Some architectures (namely, Alpha, IA-64, MIPS, SuperH, sparc/32, and sparc/64) use an additional register ("Retval2" in the above table) to pass back a second return value from the pipe(2) system call; Alpha uses this technique in the architecture-specific getxpid(2), getxuid(2), and getxgid(2) system calls as well. other architectures do not use the second return value register in the system call interface, even if it is defined in the System V ABI.

the second table shows the registers used to pass the system call arguments. ๐Ÿซถ

Arch/ABI      arg1  arg2  arg3  arg4  arg5  arg6  arg7  Notes
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
alpha         a0    a1    a2    a3    a4    a5    -
arc           r0    r1    r2    r3    r4    r5    -
arm/OABI      r0    r1    r2    r3    r4    r5    r6
arm/EABI      r0    r1    r2    r3    r4    r5    r6
arm64         x0    x1    x2    x3    x4    x5    -
blackfin      R0    R1    R2    R3    R4    R5    -
i386          ebx   ecx   edx   esi   edi   ebp   -
ia64          out0  out1  out2  out3  out4  out5  -
loongarch     a0    a1    a2    a3    a4    a5    a6
m68k          d1    d2    d3    d4    d5    a0    -
microblaze    r5    r6    r7    r8    r9    r10   -
mips/o32      a0    a1    a2    a3    -     -     -     1
mips/n32,64   a0    a1    a2    a3    a4    a5    -
nios2         r4    r5    r6    r7    r8    r9    -
parisc        r26   r25   r24   r23   r22   r21   -
powerpc       r3    r4    r5    r6    r7    r8    r9
powerpc64     r3    r4    r5    r6    r7    r8    -
riscv         a0    a1    a2    a3    a4    a5    -
s390          r2    r3    r4    r5    r6    r7    -
s390x         r2    r3    r4    r5    r6    r7    -
superh        r4    r5    r6    r7    r0    r1    r2
sparc/32      o0    o1    o2    o3    o4    o5    -
sparc/64      o0    o1    o2    o3    o4    o5    -
tile          R00   R01   R02   R03   R04   R05   -
x86-64        rdi   rsi   rdx   r10   r8    r9    -
x32           rdi   rsi   rdx   r10   r8    r9    -
xtensa        a6    a3    a4    a5    a8    a9    -

Notes:

  • [1] The mips/o32 system call convention passes arguments 5 through 8 on the user stack.
note that these tables don't cover the entire calling convention โ€” some architectures may indiscriminately clobber other registers not listed here. so like. stay alert. ๐Ÿ’€

๐Ÿ’– EXAMPLES ๐Ÿ’–

here's the iconic little demo โ€” grab your thread id, then tgkill yourself with a SIGHUP:

#define _GNU_SOURCE
#include <signal.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>

int
main(void)
{
    pid_t tid;

    tid = syscall(SYS_gettid);
    syscall(SYS_tgkill, getpid(), tid, SIGHUP);
}

โœจ SEE ALSO โœจ

okay bestie, you made it to the end, and now it's reading-recs o'clock ๐Ÿ“šโœจ if you wanna keep spiraling down the rabbit hole (and honestly? you should), these are the pages that live in the same neighborhood:

  • _syscall(2) โ€” the old-school macro situation, for when you wanna holler at the kernel without going through the whole syscall() pipeline ๐Ÿซถ
  • intro(2) โ€” the lil intro to the whole system calls section, aka the orientation tour before you start running around in there ๐ŸŽ€
  • syscalls(2) โ€” the big list of all the syscalls, because sometimes you just want the full menu, no cap ๐Ÿ’…
  • errno(3) โ€” every error number explained, for when your return value came back screaming and you need to know why ๐Ÿ˜ญ
  • vdso(7) โ€” the virtual dynamic shared object lore, the whole "some of this stuff doesn't even leave userspace" plot twist ๐Ÿฆ‹

and that's a wrap on this one, fr fr ๐Ÿ”ฅ the page you've been reading comes from the Linux man-pages 6.20 collection, dated 2026-02-02 โ€” so yeah, it's fresh, it's maintained, and it's giving very much "someone actually read the kernel source so you don't have to" energy ๐Ÿ‘‘ go forth, call some syscalls, and may your return values never be -1 ๐Ÿ’–โœจ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: BSD-4-Clause-UC
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms