Debian Codegraph @

upstream/debian-trixie/man2/syscall.2

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