OSDN Git Service

LDP: Update original to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / 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 2015-01-22 "Linux" "Linux Programmer's Manual"
43 .SH NAME
44 syscall \- indirect system call
45 .SH SYNOPSIS
46 .nf
47 .BR "#define _GNU_SOURCE" "         /* See feature_test_macros(7) */"
48 .B #include <unistd.h>
49 .BR "#include <sys/syscall.h>   "  "/* For SYS_xxx definitions */"
50
51 .BI "long syscall(long " number ", ...);"
52 .fi
53 .SH DESCRIPTION
54 .BR syscall ()
55 is a small library function that invokes
56 the system call whose assembly language
57 interface has the specified
58 .I number
59 with the specified arguments.
60 Employing
61 .BR syscall ()
62 is useful, for example,
63 when invoking a system call that has no wrapper function in the C library.
64
65 .BR syscall ()
66 saves CPU registers before making the system call,
67 restores the registers upon return from the system call,
68 and stores any error code returned by the system call in
69 .BR errno (3)
70 if an error occurs.
71
72 Symbolic constants for system call numbers can be found in the header file
73 .IR <sys/syscall.h> .
74 .SH RETURN VALUE
75 The return value is defined by the system call being invoked.
76 In general, a 0 return value indicates success.
77 A \-1 return value indicates an error,
78 and an error code is stored in
79 .IR errno .
80 .SH NOTES
81 .BR syscall ()
82 first appeared in
83 4BSD.
84 .SS Architecture-specific requirements
85 Each architecture ABI has its own requirements on how
86 system call arguments are passed to the kernel.
87 For system calls that have a glibc wrapper (e.g., most system calls),
88 glibc handles the details of copying arguments to the right registers
89 in a manner suitable for the architecture.
90 However, when using
91 .BR syscall ()
92 to make a system call,
93 the caller might need to handle architecture-dependent details;
94 this requirement is most commonly encountered on certain 32-bit architectures.
95
96 For example, on the ARM architecture Embedded ABI (EABI), a
97 64-bit value (e.g.,
98 .IR "long long" )
99 must be aligned to an even register pair.
100 Thus, using
101 .BR syscall ()
102 instead of the wrapper provided by glibc,
103 the
104 .BR readahead ()
105 system call would be invoked as follows on the ARM architecture with the EABI:
106
107 .in +4n
108 .nf
109 syscall(SYS_readahead, fd, 0,
110         (unsigned int) (offset >> 32),
111         (unsigned int) (offset & 0xFFFFFFFF),
112         count);
113 .fi
114 .in
115 .PP
116 Since the offset argument is 64 bits, and the first argument
117 .RI ( fd )
118 is passed in
119 .IR r0 ,
120 the caller must manually split and align the 64-bit value
121 so that it is passed in the
122 .IR r2 / r3
123 register pair.
124 That means inserting a dummy value into
125 .I r1
126 (the second argument of 0).
127
128 Similar issues can occur on MIPS with the O32 ABI,
129 on PowerPC with the 32-bit ABI, and on Xtensa.
130 .\" Mike Frysinger: this issue ends up forcing MIPS
131 .\" O32 to take 7 arguments to syscall()
132
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 .SS Architecture calling conventions
144 Every architecture has its own way of invoking and passing arguments to the
145 kernel.
146 The details for various architectures are listed in the two tables below.
147
148 The first table lists the instruction used to transition to kernel mode,
149 (which might not be the fastest or best way to transition to the kernel,
150 so you might have to refer to
151 .BR vdso (7)),
152 the register used to indicate the system call number,
153 and the register used to return the system call result.
154 .if t \{\
155 .ft CW
156 \}
157 .TS
158 l l1 l l1 l.
159 arch/ABI        instruction     syscall #       retval  Notes
160 _
161 arm/OABI        swi NR  -       a1      NR is syscall #
162 arm/EABI        swi 0x0 r7      r0
163 arm64   svc #0  x8      x0
164 blackfin        excpt 0x0       P0      R0
165 i386    int $0x80       eax     eax
166 ia64    break 0x100000  r15     r8      See below
167 mips    syscall v0      v0      See below
168 parisc  ble 0x100(%sr2, %r0)    r20     r28
169 s390    svc 0   r1      r2      See below
170 s390x   svc 0   r1      r2      See below
171 sparc/32        t 0x10  g1      o0
172 sparc/64        t 0x6d  g1      o0
173 x86_64  syscall rax     rax
174 .TE
175 .PP
176 For s390 and s390x, NR (the system call number)
177 may be passed directly with "svc NR" if it is less than 256.
178
179 On a few architectures,
180 a register is used to indicate simple boolean failure of the system call:
181 ia64 uses
182 .I r10
183 for this purpose,
184 and mips uses
185 .IR a3 .
186 .if t \{\
187 .in
188 .ft P
189 \}
190 .PP
191 The second table shows the registers used to pass the system call arguments.
192 .if t \{\
193 .ft CW
194 \}
195 .TS
196 l l2 l2 l2 l2 l2 l2 l2 l.
197 arch/ABI        arg1    arg2    arg3    arg4    arg5    arg6    arg7    Notes
198 _
199 arm/OABI        a1      a2      a3      a4      v1      v2      v3
200 arm/EABI        r0      r1      r2      r3      r4      r5      r6
201 arm64   x0      x1      x2      x3      x4      x5      -
202 blackfin        R0      R1      R2      R3      R4      R5      -
203 i386    ebx     ecx     edx     esi     edi     ebp     -
204 ia64    out0    out1    out2    out3    out4    out5    -
205 mips/o32        a0      a1      a2      a3      -       -       -       See below
206 mips/n32,64     a0      a1      a2      a3      a4      a5      -
207 parisc  r26     r25     r24     r23     r22     r21     -
208 s390    r2      r3      r4      r5      r6      r7      -
209 s390x   r2      r3      r4      r5      r6      r7      -
210 sparc/32        o0      o1      o2      o3      o4      o5      -
211 sparc/64        o0      o1      o2      o3      o4      o5      -
212 x86_64  rdi     rsi     rdx     r10     r8      r9      -
213 .TE
214 .PP
215 The mips/o32 system call convention passes
216 arguments 5 through 8 on the user stack.
217 .if t \{\
218 .in
219 .ft P
220 \}
221 .PP
222 Note that these tables don't cover the entire calling convention\(emsome
223 architectures may indiscriminately clobber other registers not listed here.
224 .SH EXAMPLE
225 .nf
226 #define _GNU_SOURCE
227 #include <unistd.h>
228 #include <sys/syscall.h>
229 #include <sys/types.h>
230 #include <signal.h>
231
232 int
233 main(int argc, char *argv[])
234 {
235     pid_t tid;
236
237     tid = syscall(SYS_gettid);
238     tid = syscall(SYS_tgkill, getpid(), tid, SIGHUP);
239 }
240 .fi
241 .SH SEE ALSO
242 .BR _syscall (2),
243 .BR intro (2),
244 .BR syscalls (2),
245 .BR errno (3),
246 .BR vdso (7)
247 .SH COLOPHON
248 This page is part of release 3.79 of the Linux
249 .I man-pages
250 project.
251 A description of the project,
252 information about reporting bugs,
253 and the latest version of this page,
254 can be found at
255 \%http://www.kernel.org/doc/man\-pages/.