Debian Codegraph @

upstream/debian-unstable/man2/syscall.2

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