]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/syscall.2
382c40b22ac99616dd4147810038886f3eb2b75d
[thirdparty/man-pages.git] / man2 / syscall.2
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 2020-02-09 "Linux" "Linux Programmer's Manual"
43 .SH NAME
44 syscall \- indirect system call
45 .SH SYNOPSIS
46 .nf
47 .B #include <unistd.h>
48 .BR "#include <sys/syscall.h> " "/* For SYS_xxx definitions */"
49 .PP
50 .BI "long syscall(long " number ", ...);"
51 .fi
52 .PP
53 .in -4n
54 Feature Test Macro Requirements for glibc (see
55 .BR feature_test_macros (7)):
56 .in
57 .BR syscall ():
58 .PD 0
59 .ad l
60 .RS 4
61 .TP 4
62 Since glibc 2.19:
63 _DEFAULT_SOURCE
64 .TP
65 Before glibc 2.19:
66 _BSD_SOURCE || _SVID_SOURCE
67 .RE
68 .ad
69 .PD
70 .SH DESCRIPTION
71 .BR syscall ()
72 is a small library function that invokes
73 the system call whose assembly language
74 interface has the specified
75 .I number
76 with the specified arguments.
77 Employing
78 .BR syscall ()
79 is useful, for example,
80 when invoking a system call that has no wrapper function in the C library.
81 .PP
82 .BR syscall ()
83 saves CPU registers before making the system call,
84 restores the registers upon return from the system call,
85 and stores any error returned by the system call in
86 .BR errno (3).
87 .PP
88 Symbolic constants for system call numbers can be found in the header file
89 .IR <sys/syscall.h> .
90 .SH RETURN VALUE
91 The return value is defined by the system call being invoked.
92 In general, a 0 return value indicates success.
93 A \-1 return value indicates an error,
94 and an error number is stored in
95 .IR errno .
96 .SH NOTES
97 .BR syscall ()
98 first appeared in
99 4BSD.
100 .SS Architecture-specific requirements
101 Each architecture ABI has its own requirements on how
102 system call arguments are passed to the kernel.
103 For system calls that have a glibc wrapper (e.g., most system calls),
104 glibc handles the details of copying arguments to the right registers
105 in a manner suitable for the architecture.
106 However, when using
107 .BR syscall ()
108 to make a system call,
109 the caller might need to handle architecture-dependent details;
110 this requirement is most commonly encountered on certain 32-bit architectures.
111 .PP
112 For example, on the ARM architecture Embedded ABI (EABI), a
113 64-bit value (e.g.,
114 .IR "long long" )
115 must be aligned to an even register pair.
116 Thus, using
117 .BR syscall ()
118 instead of the wrapper provided by glibc,
119 the
120 .BR readahead (2)
121 system call would be invoked as follows on the ARM architecture with the EABI
122 in little endian mode:
123 .PP
124 .in +4n
125 .EX
126 syscall(SYS_readahead, fd, 0,
127 (unsigned int) (offset & 0xFFFFFFFF),
128 (unsigned int) (offset >> 32),
129 count);
130 .EE
131 .in
132 .PP
133 Since the offset argument is 64 bits, and the first argument
134 .RI ( fd )
135 is passed in
136 .IR r0 ,
137 the caller must manually split and align the 64-bit value
138 so that it is passed in the
139 .IR r2 / r3
140 register pair.
141 That means inserting a dummy value into
142 .I r1
143 (the second argument of 0).
144 Care also must be taken so that the split follows endian conventions
145 (according to the C ABI for the platform).
146 .PP
147 Similar issues can occur on MIPS with the O32 ABI,
148 on PowerPC and parisc with the 32-bit ABI, and on Xtensa.
149 .\" Mike Frysinger: this issue ends up forcing MIPS
150 .\" O32 to take 7 arguments to syscall()
151 .PP
152 .\" See arch/parisc/kernel/sys_parisc.c.
153 Note that while the parisc C ABI also uses aligned register pairs,
154 it uses a shim layer to hide the issue from user space.
155 .PP
156 The affected system calls are
157 .BR fadvise64_64 (2),
158 .BR ftruncate64 (2),
159 .BR posix_fadvise (2),
160 .BR pread64 (2),
161 .BR pwrite64 (2),
162 .BR readahead (2),
163 .BR sync_file_range (2),
164 and
165 .BR truncate64 (2).
166 .PP
167 .\" You need to look up the syscalls directly in the kernel source to see if
168 .\" they should be in this list. For example, look at fs/read_write.c and
169 .\" the function signatures that do:
170 .\" ..., unsigned long, pos_l, unsigned long, pos_h, ...
171 .\" If they use off_t, then they most likely do not belong in this list.
172 This does not affect syscalls that manually split and assemble 64-bit values
173 such as
174 .BR _llseek (2),
175 .BR preadv (2),
176 .BR preadv2 (2),
177 .BR pwritev (2),
178 and
179 .BR pwritev2 (2).
180 Welcome to the wonderful world of historical baggage.
181 .SS Architecture calling conventions
182 Every architecture has its own way of invoking and passing arguments to the
183 kernel.
184 The details for various architectures are listed in the two tables below.
185 .PP
186 The first table lists the instruction used to transition to kernel mode
187 (which might not be the fastest or best way to transition to the kernel,
188 so you might have to refer to
189 .BR vdso (7)),
190 the register used to indicate the system call number,
191 the register(s) used to return the system call result,
192 and the register used to signal an error.
193 .if t \{\
194 .ft CW
195 \}
196 .TS
197 l2 l2 l2 l2 l1 l2 l.
198 Arch/ABI Instruction System Ret Ret Error Notes
199 call # val val2
200 _
201 alpha callsys v0 v0 a4 a3 1, 6
202 arc trap0 r8 r0 - -
203 arm/OABI swi NR - r0 - - 2
204 arm/EABI swi 0x0 r7 r0 r1 -
205 arm64 svc #0 w8 x0 x1 -
206 blackfin excpt 0x0 P0 R0 - -
207 i386 int $0x80 eax eax edx -
208 ia64 break 0x100000 r15 r8 r9 r10 1, 6
209 m68k trap #0 d0 d0 - -
210 microblaze brki r14,8 r12 r3 - -
211 mips syscall v0 v0 v1 a3 1, 6
212 nios2 trap r2 r2 - r7
213 parisc ble 0x100(%sr2, %r0) r20 r28 - -
214 powerpc sc r0 r3 - r0 1
215 powerpc64 sc r0 r3 - cr0.SO 1
216 riscv ecall a7 a0 a1 -
217 s390 svc 0 r1 r2 r3 - 3
218 s390x svc 0 r1 r2 r3 - 3
219 superh trap #0x17 r3 r0 r1 - 4, 6
220 sparc/32 t 0x10 g1 o0 o1 psr/csr 1, 6
221 sparc/64 t 0x6d g1 o0 o1 psr/csr 1, 6
222 tile swint1 R10 R00 - R01 1
223 x86-64 syscall rax rax rdx - 5
224 x32 syscall rax rax rdx - 5
225 xtensa syscall a2 a2 - -
226 .TE
227 .PP
228 Notes:
229 .IP [1] 4
230 On a few architectures,
231 a register is used as a boolean
232 (0 indicating no error, and \-1 indicating an error) to signal that the
233 system call failed.
234 The actual error value is still contained in the return register.
235 On sparc, the carry bit
236 .RI ( csr )
237 in the processor status register
238 .RI ( psr )
239 is used instead of a full register.
240 On powerpc64, the summary overflow bit
241 .RI ( SO )
242 in field 0 of the condition register
243 .RI ( cr0 )
244 is used.
245 .IP [2]
246 .I NR
247 is the system call number.
248 .IP [3]
249 For s390 and s390x,
250 .I NR
251 (the system call number) may be passed directly with
252 .I "svc\ NR"
253 if it is less than 256.
254 .IP [4]
255 On SuperH, the trap number controls the maximum number of arguments passed.
256 A
257 .IR "trap\ #0x10"
258 can be used with only 0-argument system calls, a
259 .IR "trap\ #0x11"
260 can be used with 0- or 1-argument system calls,
261 and so on up to
262 .IR "trap #0x17"
263 for 7-argument system calls.
264 .IP [5]
265 The x32 ABI shares syscall table with x86-64 ABI, but there are some
266 nuances:
267 .RS
268 .IP \(bu 3
269 In order to indicate that a system call is called under the x32 ABI,
270 an additional bit,
271 .BR __X32_SYSCALL_BIT ,
272 is bitwise-ORed with the system call number.
273 The ABI used by a process affects some process behaviors,
274 including signal handling or system call restarting.
275 .IP \(bu
276 Since x32 has different sizes for
277 .I long
278 and pointer types, layouts of some (but not all;
279 .I struct timeval
280 or
281 .I struct rlimit
282 are 64-bit, for example) structures are different.
283 In order to handle this,
284 additional system calls are added to the system call table,
285 starting from number 512
286 (without the
287 .BR __X32_SYSCALL_BIT ).
288 For example,
289 .B __NR_readv
290 is defined as 19 for the x86-64 ABI and as
291 .IR __X32_SYSCALL_BIT " | " \fB515\fP
292 for the x32 ABI.
293 Most of these additional system calls are actually identical
294 to the system calls used for providing i386 compat.
295 There are some notable exceptions, however, such as
296 .BR preadv2 (2),
297 which uses
298 .I struct iovec
299 entities with 4-byte pointers and sizes ("compat_iovec" in kernel terms),
300 but passes an 8-byte
301 .I pos
302 argument in a single register and not two, as is done in every other ABI.
303 .RE
304 .IP [6]
305 Some architectures
306 (namely, Alpha, IA-64, MIPS, SuperH, sparc/32, and sparc/64)
307 use an additional register ("Retval2" in the above table)
308 to pass back a second return value from the
309 .BR pipe (2)
310 system call;
311 Alpha uses this technique in the architecture-specific
312 .BR getxpid (2),
313 .BR getxuid (2),
314 and
315 .BR getxgid (2)
316 system calls as well.
317 Other architectures do not use the second return value register
318 in the system call interface, even if it is defined in the System V ABI.
319 .if t \{\
320 .in
321 .ft P
322 \}
323 .PP
324 The second table shows the registers used to pass the system call arguments.
325 .if t \{\
326 .ft CW
327 \}
328 .TS
329 l l2 l2 l2 l2 l2 l2 l2 l.
330 Arch/ABI arg1 arg2 arg3 arg4 arg5 arg6 arg7 Notes
331 _
332 alpha a0 a1 a2 a3 a4 a5 -
333 arc r0 r1 r2 r3 r4 r5 -
334 arm/OABI r0 r1 r2 r3 r4 r5 r6
335 arm/EABI r0 r1 r2 r3 r4 r5 r6
336 arm64 x0 x1 x2 x3 x4 x5 -
337 blackfin R0 R1 R2 R3 R4 R5 -
338 i386 ebx ecx edx esi edi ebp -
339 ia64 out0 out1 out2 out3 out4 out5 -
340 m68k d1 d2 d3 d4 d5 a0 -
341 microblaze r5 r6 r7 r8 r9 r10 -
342 mips/o32 a0 a1 a2 a3 - - - 1
343 mips/n32,64 a0 a1 a2 a3 a4 a5 -
344 nios2 r4 r5 r6 r7 r8 r9 -
345 parisc r26 r25 r24 r23 r22 r21 -
346 powerpc r3 r4 r5 r6 r7 r8 r9
347 powerpc64 r3 r4 r5 r6 r7 r8 -
348 riscv a0 a1 a2 a3 a4 a5 -
349 s390 r2 r3 r4 r5 r6 r7 -
350 s390x r2 r3 r4 r5 r6 r7 -
351 superh r4 r5 r6 r7 r0 r1 r2
352 sparc/32 o0 o1 o2 o3 o4 o5 -
353 sparc/64 o0 o1 o2 o3 o4 o5 -
354 tile R00 R01 R02 R03 R04 R05 -
355 x86-64 rdi rsi rdx r10 r8 r9 -
356 x32 rdi rsi rdx r10 r8 r9 -
357 xtensa a6 a3 a4 a5 a8 a9 -
358 .TE
359 .PP
360 Notes:
361 .IP [1] 4
362 The mips/o32 system call convention passes
363 arguments 5 through 8 on the user stack.
364 .if t \{\
365 .in
366 .ft P
367 \}
368 .PP
369 Note that these tables don't cover the entire calling convention\(emsome
370 architectures may indiscriminately clobber other registers not listed here.
371 .SH EXAMPLES
372 .EX
373 #define _GNU_SOURCE
374 #include <unistd.h>
375 #include <sys/syscall.h>
376 #include <sys/types.h>
377 #include <signal.h>
378
379 int
380 main(int argc, char *argv[])
381 {
382 pid_t tid;
383
384 tid = syscall(SYS_gettid);
385 syscall(SYS_tgkill, getpid(), tid, SIGHUP);
386 }
387 .EE
388 .SH SEE ALSO
389 .BR _syscall (2),
390 .BR intro (2),
391 .BR syscalls (2),
392 .BR errno (3),
393 .BR vdso (7)