1SYSCALL(2)                 Linux Programmer's Manual                SYSCALL(2)
2
3
4

NAME

6       syscall - indirect system call
7

SYNOPSIS

9       #include <unistd.h>
10       #include <sys/syscall.h>   /* For SYS_xxx definitions */
11
12       long syscall(long number, ...);
13
14   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
15       syscall():
16           Since glibc 2.19:
17               _DEFAULT_SOURCE
18           Before glibc 2.19:
19               _BSD_SOURCE || _SVID_SOURCE
20

DESCRIPTION

22       syscall()  is  a  small  library  function that invokes the system call
23       whose assembly language interface has the  specified  number  with  the
24       specified  arguments.  Employing syscall() is useful, for example, when
25       invoking a system call that has no wrapper function in the C library.
26
27       syscall() saves CPU registers before making the system  call,  restores
28       the  registers  upon  return from the system call, and stores any error
29       returned by the system call in errno(3).
30
31       Symbolic constants for system call numbers can be found in  the  header
32       file <sys/syscall.h>.
33

RETURN VALUE

35       The  return value is defined by the system call being invoked.  In gen‐
36       eral, a 0 return value indicates success.  A -1 return value  indicates
37       an error, and an error number is stored in errno.
38

NOTES

40       syscall() first appeared in 4BSD.
41
42   Architecture-specific requirements
43       Each architecture ABI has its own requirements on how system call argu‐
44       ments are passed to the kernel.  For system calls  that  have  a  glibc
45       wrapper (e.g., most system calls), glibc handles the details of copying
46       arguments to the right registers in a manner suitable for the architec‐
47       ture.   However, when using syscall() to make a system call, the caller
48       might need to handle architecture-dependent details;  this  requirement
49       is most commonly encountered on certain 32-bit architectures.
50
51       For  example,  on  the  ARM  architecture Embedded ABI (EABI), a 64-bit
52       value (e.g., long long) must be  aligned  to  an  even  register  pair.
53       Thus,  using  syscall()  instead  of the wrapper provided by glibc, the
54       readahead(2) system call would be invoked as follows on the ARM  archi‐
55       tecture with the EABI in little endian mode:
56
57           syscall(SYS_readahead, fd, 0,
58                   (unsigned int) (offset & 0xFFFFFFFF),
59                   (unsigned int) (offset >> 32),
60                   count);
61
62       Since  the  offset  argument is 64 bits, and the first argument (fd) is
63       passed in r0, the caller must manually split and align the 64-bit value
64       so  that it is passed in the r2/r3 register pair.  That means inserting
65       a dummy value into r1 (the second argument of 0).  Care  also  must  be
66       taken  so that the split follows endian conventions (according to the C