1syscall(2)                    System Calls Manual                   syscall(2)
2
3
4

NAME

6       syscall - indirect system call
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <sys/syscall.h>      /* Definition of SYS_* constants */
13       #include <unistd.h>
14
15       long syscall(long number, ...);
16
17   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19       syscall():
20           Since glibc 2.19:
21               _DEFAULT_SOURCE
22           Before glibc 2.19:
23               _BSD_SOURCE || _SVID_SOURCE
24

DESCRIPTION

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

RETURN VALUE

39       The  return value is defined by the system call being invoked.  In gen‐
40       eral, a 0 return value indicates success.  A -1 return value  indicates
41       an error, and an error number is stored in errno.
42

ERRORS

44       ENOSYS The requested system call number is not implemented.
45
46       Other errors are specific to the invoked system call.
47

NOTES

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

EXAMPLES

226       #define _GNU_SOURCE
227       #include <signal.h>
228       #include <sys/syscall.h>
229       #include <unistd.h>
230
231       int
232       main(void)
233       {
234           pid_t tid;
235
236           tid = syscall(SYS_gettid);
237           syscall(SYS_tgkill, getpid(), tid, SIGHUP);
238       }
239

SEE ALSO

241       _syscall(2), intro(2), syscalls(2), errno(3), vdso(7)
242
243
244
245Linux man-pages 6.05              2023-05-03                        syscall(2)
Impressum