readv2: Note preadv2(..., RWF_NOWAIT) bug in BUGS section
[man-pages.git] / man2 / syscall.2
blobabdf1c7027072a08a04d258211ca613a9bbc01a3
1 .\" Copyright (c) 1980, 1991, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
7 .\" are met:
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\"    notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\"    notice, this list of conditions and the following disclaimer in the
12 .\"    documentation and/or other materials provided with the distribution.
13 .\" 3. All advertising materials mentioning features or use of this software
14 .\"    must display the following acknowledgement:
15 .\"     This product includes software developed by the University of
16 .\"     California, Berkeley and its contributors.
17 .\" 4. Neither the name of the University nor the names of its contributors
18 .\"    may be used to endorse or promote products derived from this software
19 .\"    without specific prior written permission.
20 .\"
21 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 .\" SUCH DAMAGE.
32 .\" %%%LICENSE_END
33 .\"
34 .\"     @(#)syscall.2   8.1 (Berkeley) 6/16/93
35 .\"
36 .\"
37 .\" 2002-03-20  Christoph Hellwig <hch@infradead.org>
38 .\"     - adopted for Linux
39 .\" 2015-01-17, Kees Cook <keescook@chromium.org>
40 .\"     Added mips and arm64.
41 .\"
42 .TH SYSCALL 2 2021-03-22 "Linux" "Linux Programmer's Manual"
43 .SH NAME
44 syscall \- indirect system call
45 .SH SYNOPSIS
46 .nf
47 .BR "#include <sys/syscall.h>" "      /* Definition of " SYS_* " constants */"
48 .B #include <unistd.h>
49 .PP
50 .BI "long syscall(long " number ", ...);"
51 .fi
52 .PP
53 .RS -4
54 Feature Test Macro Requirements for glibc (see
55 .BR feature_test_macros (7)):
56 .RE
57 .PP
58 .BR syscall ():
59 .nf
60     Since glibc 2.19:
61         _DEFAULT_SOURCE
62     Before glibc 2.19:
63         _BSD_SOURCE || _SVID_SOURCE
64 .fi
65 .SH DESCRIPTION
66 .BR syscall ()
67 is a small library function that invokes
68 the system call whose assembly language
69 interface has the specified
70 .I number
71 with the specified arguments.
72 Employing
73 .BR syscall ()
74 is useful, for example,
75 when invoking a system call that has no wrapper function in the C library.
76 .PP
77 .BR syscall ()
78 saves CPU registers before making the system call,
79 restores the registers upon return from the system call,
80 and stores any error returned by the system call in
81 .BR errno (3).
82 .PP
83 Symbolic constants for system call numbers can be found in the header file
84 .IR <sys/syscall.h> .
85 .SH RETURN VALUE
86 The return value is defined by the system call being invoked.
87 In general, a 0 return value indicates success.
88 A \-1 return value indicates an error,
89 and an error number is stored in
90 .IR errno .
91 .SH NOTES
92 .BR syscall ()
93 first appeared in
94 4BSD.
95 .SS Architecture-specific requirements
96 Each architecture ABI has its own requirements on how
97 system call arguments are passed to the kernel.
98 For system calls that have a glibc wrapper (e.g., most system calls),
99 glibc handles the details of copying arguments to the right registers
100 in a manner suitable for the architecture.
101 However, when using
102 .BR syscall ()
103 to make a system call,
104 the caller might need to handle architecture-dependent details;
105 this requirement is most commonly encountered on certain 32-bit architectures.
107 For example, on the ARM architecture Embedded ABI (EABI), a
108 64-bit value (e.g.,
109 .IR "long long" )
110 must be aligned to an even register pair.
111 Thus, using
112 .BR syscall ()
113 instead of the wrapper provided by glibc,
115 .BR readahead (2)
116 system call would be invoked as follows on the ARM architecture with the EABI
117 in little endian mode:
119 .in +4n
121 syscall(SYS_readahead, fd, 0,
122         (unsigned int) (offset & 0xFFFFFFFF),
123         (unsigned int) (offset >> 32),
124         count);
128 Since the offset argument is 64 bits, and the first argument
129 .RI ( fd )
130 is passed in
131 .IR r0 ,
132 the caller must manually split and align the 64-bit value
133 so that it is passed in the
134 .IR r2 / r3
135 register pair.
136 That means inserting a dummy value into
137 .I r1
138 (the second argument of 0).
139 Care also must be taken so that the split follows endian conventions
140 (according to the C ABI for the platform).
142 Similar issues can occur on MIPS with the O32 ABI,
143 on PowerPC and parisc with the 32-bit ABI, and on Xtensa.
144 .\" Mike Frysinger: this issue ends up forcing MIPS
145 .\" O32 to take 7 arguments to syscall()
147 .\" See arch/parisc/kernel/sys_parisc.c.
148 Note that while the parisc C ABI also uses aligned register pairs,
149 it uses a shim layer to hide the issue from user space.
151 The affected system calls are
152 .BR fadvise64_64 (2),
153 .BR ftruncate64 (2),
154 .BR posix_fadvise (2),
155 .BR pread64 (2),
156 .BR pwrite64 (2),
157 .BR readahead (2),
158 .BR sync_file_range (2),
160 .BR truncate64 (2).
162 .\" You need to look up the syscalls directly in the kernel source to see if
163 .\" they should be in this list.  For example, look at fs/read_write.c and
164 .\" the function signatures that do:
165 .\" ..., unsigned long, pos_l, unsigned long, pos_h, ...
166 .\" If they use off_t, then they most likely do not belong in this list.
167 This does not affect syscalls that manually split and assemble 64-bit values
168 such as
169 .BR _llseek (2),
170 .BR preadv (2),
171 .BR preadv2 (2),
172 .BR pwritev (2),
174 .BR pwritev2 (2).
175 Welcome to the wonderful world of historical baggage.
176 .SS Architecture calling conventions
177 Every architecture has its own way of invoking and passing arguments to the
178 kernel.
179 The details for various architectures are listed in the two tables below.
181 The first table lists the instruction used to transition to kernel mode
182 (which might not be the fastest or best way to transition to the kernel,
183 so you might have to refer to
184 .BR vdso (7)),
185 the register used to indicate the system call number,
186 the register(s) used to return the system call result,
187 and the register used to signal an error.
188 .if t \{\
189 .ft CW
192 l2      l2      l2      l2      l1      l2      l.
193 Arch/ABI        Instruction     System  Ret     Ret     Error   Notes
194                 call #  val     val2
196 alpha   callsys v0      v0      a4      a3      1, 6
197 arc     trap0   r8      r0      -       -
198 arm/OABI        swi NR  -       r0      -       -       2
199 arm/EABI        swi 0x0 r7      r0      r1      -
200 arm64   svc #0  w8      x0      x1      -
201 blackfin        excpt 0x0       P0      R0      -       -
202 i386    int $0x80       eax     eax     edx     -
203 ia64    break 0x100000  r15     r8      r9      r10     1, 6
204 m68k    trap #0 d0      d0      -       -
205 microblaze      brki r14,8      r12     r3      -       -
206 mips    syscall v0      v0      v1      a3      1, 6
207 nios2   trap    r2      r2      -       r7
208 parisc  ble 0x100(%sr2, %r0)    r20     r28     -       -
209 powerpc sc      r0      r3      -       r0      1
210 powerpc64       sc      r0      r3      -       cr0.SO  1
211 riscv   ecall   a7      a0      a1      -
212 s390    svc 0   r1      r2      r3      -       3
213 s390x   svc 0   r1      r2      r3      -       3
214 superh  trapa #31       r3      r0      r1      -       4, 6
215 sparc/32        t 0x10  g1      o0      o1      psr/csr 1, 6
216 sparc/64        t 0x6d  g1      o0      o1      psr/csr 1, 6
217 tile    swint1  R10     R00     -       R01     1
218 x86-64  syscall rax     rax     rdx     -       5
219 x32     syscall rax     rax     rdx     -       5
220 xtensa  syscall a2      a2      -       -
223 Notes:
224 .IP [1] 4
225 On a few architectures,
226 a register is used as a boolean
227 (0 indicating no error, and \-1 indicating an error) to signal that the
228 system call failed.
229 The actual error value is still contained in the return register.
230 On sparc, the carry bit
231 .RI ( csr )
232 in the processor status register
233 .RI ( psr )
234 is used instead of a full register.
235 On powerpc64, the summary overflow bit
236 .RI ( SO )
237 in field 0 of the condition register
238 .RI ( cr0 )
239 is used.
240 .IP [2]
241 .I NR
242 is the system call number.
243 .IP [3]
244 For s390 and s390x,
245 .I NR
246 (the system call number) may be passed directly with
247 .I "svc\ NR"
248 if it is less than 256.
249 .IP [4]
250 On SuperH additional trap numbers are supported for historic reasons, but
251 .BR trapa #31
252 is the recommended "unified" ABI.
253 .IP [5]
254 The x32 ABI shares syscall table with x86-64 ABI, but there are some
255 nuances:
257 .IP \(bu 3
258 In order to indicate that a system call is called under the x32 ABI,
259 an additional bit,
260 .BR __X32_SYSCALL_BIT ,
261 is bitwise-ORed with the system call number.
262 The ABI used by a process affects some process behaviors,
263 including signal handling or system call restarting.
264 .IP \(bu
265 Since x32 has different sizes for
266 .I long
267 and pointer types, layouts of some (but not all;
268 .I struct timeval
270 .I struct rlimit
271 are 64-bit, for example) structures are different.
272 In order to handle this,
273 additional system calls are added to the system call table,
274 starting from number 512
275 (without the
276 .BR __X32_SYSCALL_BIT ).
277 For example,
278 .B __NR_readv
279 is defined as 19 for the x86-64 ABI and as
280 .IR __X32_SYSCALL_BIT " | " \fB515\fP
281 for the x32 ABI.
282 Most of these additional system calls are actually identical
283 to the system calls used for providing i386 compat.
284 There are some notable exceptions, however, such as
285 .BR preadv2 (2),
286 which uses
287 .I struct iovec
288 entities with 4-byte pointers and sizes ("compat_iovec" in kernel terms),
289 but passes an 8-byte
290 .I pos
291 argument in a single register and not two, as is done in every other ABI.
293 .IP [6]
294 Some architectures
295 (namely, Alpha, IA-64, MIPS, SuperH, sparc/32, and sparc/64)
296 use an additional register ("Retval2" in the above table)
297 to pass back a second return value from the
298 .BR pipe (2)
299 system call;
300 Alpha uses this technique in the architecture-specific
301 .BR getxpid (2),
302 .BR getxuid (2),
304 .BR getxgid (2)
305 system calls as well.
306 Other architectures do not use the second return value register
307 in the system call interface, even if it is defined in the System V ABI.
308 .if t \{\
310 .ft P
313 The second table shows the registers used to pass the system call arguments.
314 .if t \{\
315 .ft CW
318 l       l2      l2      l2      l2      l2      l2      l2      l.
319 Arch/ABI        arg1    arg2    arg3    arg4    arg5    arg6    arg7    Notes
321 alpha   a0      a1      a2      a3      a4      a5      -
322 arc     r0      r1      r2      r3      r4      r5      -
323 arm/OABI        r0      r1      r2      r3      r4      r5      r6
324 arm/EABI        r0      r1      r2      r3      r4      r5      r6
325 arm64   x0      x1      x2      x3      x4      x5      -
326 blackfin        R0      R1      R2      R3      R4      R5      -
327 i386    ebx     ecx     edx     esi     edi     ebp     -
328 ia64    out0    out1    out2    out3    out4    out5    -
329 m68k    d1      d2      d3      d4      d5      a0      -
330 microblaze      r5      r6      r7      r8      r9      r10     -
331 mips/o32        a0      a1      a2      a3      -       -       -       1
332 mips/n32,64     a0      a1      a2      a3      a4      a5      -
333 nios2   r4      r5      r6      r7      r8      r9      -
334 parisc  r26     r25     r24     r23     r22     r21     -
335 powerpc r3      r4      r5      r6      r7      r8      r9
336 powerpc64       r3      r4      r5      r6      r7      r8      -
337 riscv   a0      a1      a2      a3      a4      a5      -
338 s390    r2      r3      r4      r5      r6      r7      -
339 s390x   r2      r3      r4      r5      r6      r7      -
340 superh  r4      r5      r6      r7      r0      r1      r2
341 sparc/32        o0      o1      o2      o3      o4      o5      -
342 sparc/64        o0      o1      o2      o3      o4      o5      -
343 tile    R00     R01     R02     R03     R04     R05     -
344 x86-64  rdi     rsi     rdx     r10     r8      r9      -
345 x32     rdi     rsi     rdx     r10     r8      r9      -
346 xtensa  a6      a3      a4      a5      a8      a9      -
349 Notes:
350 .IP [1] 4
351 The mips/o32 system call convention passes
352 arguments 5 through 8 on the user stack.
353 .if t \{\
355 .ft P
358 Note that these tables don't cover the entire calling convention\(emsome
359 architectures may indiscriminately clobber other registers not listed here.
360 .SH EXAMPLES
362 #define _GNU_SOURCE
363 #include <unistd.h>
364 #include <sys/syscall.h>
365 #include <sys/types.h>
366 #include <signal.h>
369 main(int argc, char *argv[])
371     pid_t tid;
373     tid = syscall(SYS_gettid);
374     syscall(SYS_tgkill, getpid(), tid, SIGHUP);
377 .SH SEE ALSO
378 .BR _syscall (2),
379 .BR intro (2),
380 .BR syscalls (2),
381 .BR errno (3),
382 .BR vdso (7)