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