1SYSCALL(2) Linux Programmer's Manual SYSCALL(2)
2
3
4
6 syscall - indirect system call
7
9 #include <sys/syscall.h> /* Definition of SYS_* constants */
10 #include <unistd.h>
11
12 long syscall(long number, ...);
13
14 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
15
16 syscall():
17 Since glibc 2.19:
18 _DEFAULT_SOURCE
19 Before glibc 2.19:
20 _BSD_SOURCE || _SVID_SOURCE
21
23 syscall() is a small library function that invokes the system call
24 whose assembly language interface has the specified number with the
25 specified arguments. Employing syscall() is useful, for example, when
26 invoking a system call that has no wrapper function in the C library.
27
28 syscall() saves CPU registers before making the system call, restores
29 the registers upon return from the system call, and stores any error
30 returned by the system call in errno(3).
31
32 Symbolic constants for system call numbers can be found in the header
33 file <sys/syscall.h>.
34
36 The return value is defined by the system call being invoked. In gen‐
37 eral, a 0 return value indicates success. A -1 return value indicates
38 an error, and an error number is stored in errno.
39
41 syscall() first appeared in 4BSD.
42
43 Architecture-specific requirements
44 Each architecture ABI has its own requirements on how system call argu‐
45 ments are passed to the kernel. For system calls that have a glibc
46 wrapper (e.g., most system calls), glibc handles the details of copying
47 arguments to the right registers in a manner suitable for the architec‐
48 ture. However, when using syscall() to make a system call, the caller
49 might need to handle architecture-dependent details; this requirement
50 is most commonly encountered on certain 32-bit architectures.
51
52 For example, on the ARM architecture Embedded ABI (EABI), a 64-bit
53 value (e.g., long long) must be aligned to an even register pair.
54 Thus, using syscall() instead of the wrapper provided by glibc, the
55 readahead(2) system call would be invoked as follows on the ARM archi‐
56 tecture with the EABI in little endian mode:
57
58 syscall(SYS_readahead, fd, 0,
59 (unsigned int) (offset & 0xFFFFFFFF),
60 (unsigned int) (offset >> 32),
61 count);
62
63 Since the offset argument is 64 bits, and the first argument (fd) is
64 passed in r0, the caller must manually split and align the 64-bit value
65 so that it is passed in the r2/r3 register pair. That means inserting
66 a dummy value into r1 (the second argument of 0). Care also must be
67 taken so that the split follows endian con