]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/i386/sysdep.h
Update.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / i386 / sysdep.h
1 /* Copyright (C) 1992, 1993, 1995-2000, 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper, <drepper@gnu.org>, August 1995.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20 #ifndef _LINUX_I386_SYSDEP_H
21 #define _LINUX_I386_SYSDEP_H 1
22
23 /* There is some commonality. */
24 #include <sysdeps/unix/i386/sysdep.h>
25 #include <bp-sym.h>
26 #include <bp-asm.h>
27
28 /* For Linux we can use the system call table in the header file
29 /usr/include/asm/unistd.h
30 of the kernel. But these symbols do not follow the SYS_* syntax
31 so we have to redefine the `SYS_ify' macro here. */
32 #undef SYS_ify
33 #define SYS_ify(syscall_name) __NR_##syscall_name
34
35 /* ELF-like local names start with `.L'. */
36 #undef L
37 #define L(name) .L##name
38
39 #ifdef __ASSEMBLER__
40
41 /* Linux uses a negative return value to indicate syscall errors,
42 unlike most Unices, which use the condition codes' carry flag.
43
44 Since version 2.1 the return value of a system call might be
45 negative even if the call succeeded. E.g., the `lseek' system call
46 might return a large offset. Therefore we must not anymore test
47 for < 0, but test for a real error by making sure the value in %eax
48 is a real error number. Linus said he will make sure the no syscall
49 returns a value in -1 .. -4095 as a valid result so we can savely
50 test with -4095. */
51
52 /* We don't want the label for the error handle to be global when we define
53 it here. */
54 #ifdef PIC
55 # define SYSCALL_ERROR_LABEL 0f
56 #else
57 # define SYSCALL_ERROR_LABEL syscall_error
58 #endif
59
60 #undef PSEUDO
61 #define PSEUDO(name, syscall_name, args) \
62 .text; \
63 ENTRY (name) \
64 DO_CALL (args, syscall_name); \
65 cmpl $-4095, %eax; \
66 jae SYSCALL_ERROR_LABEL; \
67 L(pseudo_end):
68
69 #undef PSEUDO_END
70 #define PSEUDO_END(name) \
71 SYSCALL_ERROR_HANDLER \
72 END (name)
73
74 #ifndef PIC
75 #define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
76 #else
77 /* Store (- %eax) into errno through the GOT. */
78 #ifdef _LIBC_REENTRANT
79 #define SYSCALL_ERROR_HANDLER \
80 0:pushl %ebx; \
81 call 1f; \
82 .subsection 1; \
83 1:movl (%esp), %ebx; \
84 ret; \
85 .previous; \
86 addl $_GLOBAL_OFFSET_TABLE_, %ebx; \
87 xorl %edx, %edx; \
88 subl %eax, %edx; \
89 pushl %edx; \
90 PUSH_ERRNO_LOCATION_RETURN; \
91 call BP_SYM (__errno_location)@PLT; \
92 POP_ERRNO_LOCATION_RETURN; \
93 popl %ecx; \
94 popl %ebx; \
95 movl %ecx, (%eax); \
96 orl $-1, %eax; \
97 jmp L(pseudo_end);
98 /* A quick note: it is assumed that the call to `__errno_location' does
99 not modify the stack! */
100 #else
101 #define SYSCALL_ERROR_HANDLER \
102 0:call 1f; \
103 .subsection 1; \
104 1:movl (%esp), %ecx; \
105 ret; \
106 .previous; \
107 addl $_GLOBAL_OFFSET_TABLE_, %ecx; \
108 xorl %edx, %edx; \
109 subl %eax, %edx; \
110 movl errno@GOT(%ecx), %ecx; \
111 movl %edx, (%ecx); \
112 orl $-1, %eax; \
113 jmp L(pseudo_end);
114 #endif /* _LIBC_REENTRANT */
115 #endif /* PIC */
116
117 /* Linux takes system call arguments in registers:
118
119 syscall number %eax call-clobbered
120 arg 1 %ebx call-saved
121 arg 2 %ecx call-clobbered
122 arg 3 %edx call-clobbered
123 arg 4 %esi call-saved
124 arg 5 %edi call-saved
125
126 The stack layout upon entering the function is:
127
128 20(%esp) Arg# 5
129 16(%esp) Arg# 4
130 12(%esp) Arg# 3
131 8(%esp) Arg# 2
132 4(%esp) Arg# 1
133 (%esp) Return address
134
135 (Of course a function with say 3 arguments does not have entries for
136 arguments 4 and 5.)
137
138 The following code tries hard to be optimal. A general assumption
139 (which is true according to the data books I have) is that
140
141 2 * xchg is more expensive than pushl + movl + popl
142
143 Beside this a neat trick is used. The calling conventions for Linux
144 tell that among the registers used for parameters %ecx and %edx need
145 not be saved. Beside this we may clobber this registers even when
146 they are not used for parameter passing.
147
148 As a result one can see below that we save the content of the %ebx
149 register in the %edx register when we have less than 3 arguments
150 (2 * movl is less expensive than pushl + popl).
151
152 Second unlike for the other registers we don't save the content of
153 %ecx and %edx when we have more than 1 and 2 registers resp.
154
155 The code below might look a bit long but we have to take care for
156 the pipelined processors (i586). Here the `pushl' and `popl'
157 instructions are marked as NP (not pairable) but the exception is
158 two consecutive of these instruction. This gives no penalty on
159 other processors though. */
160
161 #undef DO_CALL
162 #define DO_CALL(args, syscall_name) \
163 PUSHARGS_##args \
164 DOARGS_##args \
165 movl $SYS_ify (syscall_name), %eax; \
166 int $0x80 \
167 POPARGS_##args
168
169 #define PUSHARGS_0 /* No arguments to push. */
170 #define DOARGS_0 /* No arguments to frob. */
171 #define POPARGS_0 /* No arguments to pop. */
172 #define _PUSHARGS_0 /* No arguments to push. */
173 #define _DOARGS_0(n) /* No arguments to frob. */
174 #define _POPARGS_0 /* No arguments to pop. */
175
176 #define PUSHARGS_1 movl %ebx, %edx; PUSHARGS_0
177 #define DOARGS_1 _DOARGS_1 (4)
178 #define POPARGS_1 POPARGS_0; movl %edx, %ebx
179 #define _PUSHARGS_1 pushl %ebx; _PUSHARGS_0
180 #define _DOARGS_1(n) movl n(%esp), %ebx; _DOARGS_0(n-4)
181 #define _POPARGS_1 _POPARGS_0; popl %ebx
182
183 #define PUSHARGS_2 PUSHARGS_1
184 #define DOARGS_2 _DOARGS_2 (8)
185 #define POPARGS_2 POPARGS_1
186 #define _PUSHARGS_2 _PUSHARGS_1
187 #define _DOARGS_2(n) movl n(%esp), %ecx; _DOARGS_1 (n-4)
188 #define _POPARGS_2 _POPARGS_1
189
190 #define PUSHARGS_3 _PUSHARGS_2
191 #define DOARGS_3 _DOARGS_3 (16)
192 #define POPARGS_3 _POPARGS_3
193 #define _PUSHARGS_3 _PUSHARGS_2
194 #define _DOARGS_3(n) movl n(%esp), %edx; _DOARGS_2 (n-4)
195 #define _POPARGS_3 _POPARGS_2
196
197 #define PUSHARGS_4 _PUSHARGS_4
198 #define DOARGS_4 _DOARGS_4 (24)
199 #define POPARGS_4 _POPARGS_4
200 #define _PUSHARGS_4 pushl %esi; _PUSHARGS_3
201 #define _DOARGS_4(n) movl n(%esp), %esi; _DOARGS_3 (n-4)
202 #define _POPARGS_4 _POPARGS_3; popl %esi
203
204 #define PUSHARGS_5 _PUSHARGS_5
205 #define DOARGS_5 _DOARGS_5 (32)
206 #define POPARGS_5 _POPARGS_5
207 #define _PUSHARGS_5 pushl %edi; _PUSHARGS_4
208 #define _DOARGS_5(n) movl n(%esp), %edi; _DOARGS_4 (n-4)
209 #define _POPARGS_5 _POPARGS_4; popl %edi
210
211 #else /* !__ASSEMBLER__ */
212
213 /* We need some help from the assembler to generate optimal code. We
214 define some macros here which later will be used. */
215 asm (".L__X'%ebx = 1\n\t"
216 ".L__X'%ecx = 2\n\t"
217 ".L__X'%edx = 2\n\t"
218 ".L__X'%eax = 3\n\t"
219 ".L__X'%esi = 3\n\t"
220 ".L__X'%edi = 3\n\t"
221 ".L__X'%ebp = 3\n\t"
222 ".L__X'%esp = 3\n\t"
223 ".macro bpushl name reg\n\t"
224 ".if 1 - \\name\n\t"
225 ".if 2 - \\name\n\t"
226 "pushl %ebx\n\t"
227 ".else\n\t"
228 "xchgl \\reg, %ebx\n\t"
229 ".endif\n\t"
230 ".endif\n\t"
231 ".endm\n\t"
232 ".macro bpopl name reg\n\t"
233 ".if 1 - \\name\n\t"
234 ".if 2 - \\name\n\t"
235 "popl %ebx\n\t"
236 ".else\n\t"
237 "xchgl \\reg, %ebx\n\t"
238 ".endif\n\t"
239 ".endif\n\t"
240 ".endm\n\t"
241 ".macro bmovl name reg\n\t"
242 ".if 1 - \\name\n\t"
243 ".if 2 - \\name\n\t"
244 "movl \\reg, %ebx\n\t"
245 ".endif\n\t"
246 ".endif\n\t"
247 ".endm\n\t");
248
249 /* Define a macro which expands inline into the wrapper code for a system
250 call. */
251 #undef INLINE_SYSCALL
252 #define INLINE_SYSCALL(name, nr, args...) \
253 ({ \
254 unsigned int resultvar; \
255 asm volatile ( \
256 LOADARGS_##nr \
257 "movl %1, %%eax\n\t" \
258 "int $0x80\n\t" \
259 RESTOREARGS_##nr \
260 : "=a" (resultvar) \
261 : "i" (__NR_##name) ASMFMT_##nr(args) : "memory", "cc"); \
262 if (resultvar >= 0xfffff001) \
263 { \
264 __set_errno (-resultvar); \
265 resultvar = 0xffffffff; \
266 } \
267 (int) resultvar; })
268
269 #define LOADARGS_0
270 #define LOADARGS_1 \
271 "bpushl .L__X'%k2, %k2\n\t" \
272 "bmovl .L__X'%k2, %k2\n\t"
273 #define LOADARGS_2 LOADARGS_1
274 #define LOADARGS_3 LOADARGS_1
275 #define LOADARGS_4 LOADARGS_1
276 #define LOADARGS_5 LOADARGS_1
277
278 #define RESTOREARGS_0
279 #define RESTOREARGS_1 \
280 "bpopl .L__X'%k2, %k2\n\t"
281 #define RESTOREARGS_2 RESTOREARGS_1
282 #define RESTOREARGS_3 RESTOREARGS_1
283 #define RESTOREARGS_4 RESTOREARGS_1
284 #define RESTOREARGS_5 RESTOREARGS_1
285
286 #define ASMFMT_0()
287 #define ASMFMT_1(arg1) \
288 , "acdSD" (arg1)
289 #define ASMFMT_2(arg1, arg2) \
290 , "adCD" (arg1), "c" (arg2)
291 #define ASMFMT_3(arg1, arg2, arg3) \
292 , "aCD" (arg1), "c" (arg2), "d" (arg3)
293 #define ASMFMT_4(arg1, arg2, arg3, arg4) \
294 , "aD" (arg1), "c" (arg2), "d" (arg3), "S" (arg4)
295 #define ASMFMT_5(arg1, arg2, arg3, arg4, arg5) \
296 , "a" (arg1), "c" (arg2), "d" (arg3), "S" (arg4), "D" (arg5)
297
298 #endif /* __ASSEMBLER__ */
299
300 #endif /* linux/i386/sysdep.h */