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

NAME

6       syscall - indirect system call
7

SYNOPSIS

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

DESCRIPTION

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

RETURN VALUE

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

NOTES

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 conventions (according to the C
68       ABI for the platform).
69
70       Similar issues can occur on MIPS with  the  O32  ABI,  on  PowerPC  and
71       parisc with the 32-bit ABI, and on Xtensa.
72
73       Note  that  while the parisc C ABI also uses aligned register pairs, it
74       uses a shim layer to hide the issue from user space.
75
76       The  affected  system  calls   are   fadvise64_64(2),   ftruncate64(2),
77       posix_fadvise(2),      pread64(2),      pwrite64(2),      readahead(2),
78       sync_file_range(2), and truncate64(2).
79
80       This does not affect syscalls that manually split and  assemble  64-bit
81       values  such  as  _llseek(2),  preadv(2),  preadv2(2),  pwritev(2), and
82       pwritev2(2).  Welcome to the wonderful world of historical baggage.
83
84   Architecture calling conventions
85       Every architecture has its own way of invoking and passing arguments to
86       the  kernel.   The  details for various architectures are listed in the
87       two tables below.
88
89       The first table lists the instruction used to transition to kernel mode
90       (which  might  not be the fastest or best way to transition to the ker‐
91       nel, so you might have to refer to vdso(7)), the register used to indi‐
92       cate  the system call number, the register(s) used to return the system
93       call result, and the register used to signal an error.
94
95       Arch/ABI    Instruction           System  Ret  Ret  Error    Notes
96                                         call #  val  val2
97       ───────────────────────────────────────────────────────────────────
98       alpha       callsys               v0      v0   a4   a3       1, 6
99       arc         trap0                 r8      r0   -    -
100       arm/OABI    swi NR                -       r0   -    -        2
101       arm/EABI    swi 0x0               r7      r0   r1   -
102       arm64       svc #0                w8      x0   x1   -
103       blackfin    excpt 0x0             P0      R0   -    -
104       i386        int $0x80             eax     eax  edx  -
105       ia64        break 0x100000        r15     r8   r9   r10      1, 6
106       m68k        trap #0               d0      d0   -    -
107       microblaze  brki r14,8            r12     r3   -    -
108       mips        syscall               v0      v0   v1   a3       1, 6
109       nios2       trap                  r2      r2   -    r7
110       parisc      ble 0x100(%sr2, %r0)  r20     r28  -    -
111       powerpc     sc                    r0      r3   -    r0       1
112       powerpc64   sc                    r0      r3   -    cr0.SO   1
113       riscv       ecall                 a7      a0   a1   -
114       s390        svc 0                 r1      r2   r3   -        3
115       s390x       svc 0                 r1      r2   r3   -        3
116       superh      trapa #31             r3      r0   r1   -        4, 6
117       sparc/32    t 0x10                g1      o0   o1   psr/csr  1, 6
118       sparc/64    t 0x6d                g1      o0   o1   psr/csr  1, 6
119       tile        swint1                R10     R00  -    R01      1
120       x86-64      syscall               rax     rax  rdx  -        5
121       x32         syscall               rax     rax  rdx  -        5
122       xtensa      syscall               a2      a2   -    -
123
124       Notes:
125
126       [1] On a few architectures, a register is used as a boolean (0 indicat‐
127           ing no error, and -1 indicating an error) to signal that the system
128           call failed.  The actual error value is still contained in the  re‐
129           turn register.  On sparc, the carry bit (csr) in the processor sta‐
130           tus register (psr) is used instead of a  full  register.   On  pow‐
131           erpc64,  the  summary overflow bit (SO) in field 0 of the condition
132           register (cr0) is used.
133
134       [2] NR is the system call number.
135
136       [3] For s390 and s390x, NR (the system call number) may be  passed  di‐
137           rectly with svc NR if it is less than 256.
138
139       [4] On  SuperH  additional trap numbers are supported for historic rea‐
140           sons, but trapa#31 is the recommended "unified" ABI.
141
142       [5] The x32 ABI shares syscall table with x86-64  ABI,  but  there  are
143           some nuances:
144
145           •  In  order to indicate that a system call is called under the x32
146              ABI, an additional bit, __X32_SYSCALL_BIT, is bitwise-ORed  with
147              the  system call number.  The ABI used by a process affects some
148              process behaviors, including  signal  handling  or  system  call
149              restarting.
150
151           •  Since  x32  has different sizes for long and pointer types, lay‐
152              outs of some (but not all; struct timeval or struct  rlimit  are
153              64-bit, for example) structures are different.  In order to han‐
154              dle this, additional system calls are added to the  system  call
155              table, starting from number 512 (without the __X32_SYSCALL_BIT).
156              For example, __NR_readv is defined as 19 for the x86-64 ABI  and
157              as __X32_SYSCALL_BIT | 515 for the x32 ABI.  Most of these addi‐
158              tional system calls are actually identical to the  system  calls
159              used  for  providing i386 compat.  There are some notable excep‐
160              tions, however, such as preadv2(2), which uses struct iovec  en‐
161              tities  with 4-byte pointers and sizes ("compat_iovec" in kernel
162              terms), but passes an 8-byte pos argument in a  single  register
163              and not two, as is done in every other ABI.
164
165       [6] Some  architectures  (namely, Alpha, IA-64, MIPS, SuperH, sparc/32,
166           and sparc/64) use an additional register ("Retval2"  in  the  above
167           table)  to  pass back a second return value from the pipe(2) system
168           call; Alpha uses this technique in the architecture-specific  getx‐
169           pid(2), getxuid(2), and getxgid(2) system calls as well.  Other ar‐
170           chitectures do not use the second return value register in the sys‐
171           tem call interface, even if it is defined in the System V ABI.
172
173       The second table shows the registers used to pass the system call argu‐
174       ments.
175
176       Arch/ABI      arg1  arg2  arg3  arg4  arg5  arg6  arg7  Notes
177       ──────────────────────────────────────────────────────────────
178       alpha         a0    a1    a2    a3    a4    a5    -
179       arc           r0    r1    r2    r3    r4    r5    -
180       arm/OABI      r0    r1    r2    r3    r4    r5    r6
181       arm/EABI      r0    r1    r2    r3    r4    r5    r6
182       arm64         x0    x1    x2    x3    x4    x5    -
183       blackfin      R0    R1    R2    R3    R4    R5    -
184       i386          ebx   ecx   edx   esi   edi   ebp   -
185       ia64          out0  out1  out2  out3  out4  out5  -
186       m68k          d1    d2    d3    d4    d5    a0    -
187       microblaze    r5    r6    r7    r8    r9    r10   -
188       mips/o32      a0    a1    a2    a3    -     -     -     1
189       mips/n32,64   a0    a1    a2    a3    a4    a5    -
190       nios2         r4    r5    r6    r7    r8    r9    -
191       parisc        r26   r25   r24   r23   r22   r21   -
192       powerpc       r3    r4    r5    r6    r7    r8    r9
193       powerpc64     r3    r4    r5    r6    r7    r8    -
194       riscv         a0    a1    a2    a3    a4    a5    -
195       s390          r2    r3    r4    r5    r6    r7    -
196       s390x         r2    r3    r4    r5    r6    r7    -
197       superh        r4    r5    r6    r7    r0    r1    r2
198
199       sparc/32      o0    o1    o2    o3    o4    o5    -
200       sparc/64      o0    o1    o2    o3    o4    o5    -
201       tile          R00   R01   R02   R03   R04   R05   -
202       x86-64        rdi   rsi   rdx   r10   r8    r9    -
203       x32           rdi   rsi   rdx   r10   r8    r9    -
204       xtensa        a6    a3    a4    a5    a8    a9    -
205
206       Notes:
207
208       [1] The mips/o32 system call convention passes arguments 5 through 8 on
209           the user stack.
210
211       Note  that  these tables don't cover the entire calling convention—some
212       architectures may indiscriminately clobber other registers  not  listed
213       here.
214

EXAMPLES

216       #define _GNU_SOURCE
217       #include <unistd.h>
218       #include <sys/syscall.h>
219       #include <sys/types.h>
220       #include <signal.h>
221
222       int
223       main(int argc, char *argv[])
224       {
225           pid_t tid;
226
227           tid = syscall(SYS_gettid);
228           syscall(SYS_tgkill, getpid(), tid, SIGHUP);
229       }
230

SEE ALSO

232       _syscall(2), intro(2), syscalls(2), errno(3), vdso(7)
233

COLOPHON

235       This  page  is  part of release 5.13 of the Linux man-pages project.  A
236       description of the project, information about reporting bugs,  and  the
237       latest     version     of     this    page,    can    be    found    at
238       https://www.kernel.org/doc/man-pages/.
239
240
241
242Linux                             2021-03-22                        SYSCALL(2)
Impressum