]>
git.ipfire.org Git - thirdparty/glibc.git/blob - 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.
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
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>
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. */
33 #define SYS_ify(syscall_name) __NR_##syscall_name
35 /* ELF-like local names start with `.L'. */
37 #define L(name) .L##name
41 /* Linux uses a negative return value to indicate syscall errors,
42 unlike most Unices, which use the condition codes' carry flag.
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
52 /* We don't want the label for the error handle to be global when we define
55 # define SYSCALL_ERROR_LABEL 0f
57 # define SYSCALL_ERROR_LABEL syscall_error
61 #define PSEUDO(name, syscall_name, args) \
64 DO_CALL (args, syscall_name); \
66 jae SYSCALL_ERROR_LABEL; \
70 #define PSEUDO_END(name) \
71 SYSCALL_ERROR_HANDLER \
75 #define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
77 /* Store (- %eax) into errno through the GOT. */
78 #ifdef _LIBC_REENTRANT
79 #define SYSCALL_ERROR_HANDLER \
83 1:movl (%esp), %ebx; \
86 addl $_GLOBAL_OFFSET_TABLE_, %ebx; \
90 PUSH_ERRNO_LOCATION_RETURN; \
91 call BP_SYM (__errno_location)@PLT; \
92 POP_ERRNO_LOCATION_RETURN; \
98 /* A quick note: it is assumed that the call to `__errno_location' does
99 not modify the stack! */
101 #define SYSCALL_ERROR_HANDLER \
104 1:movl (%esp), %ecx; \
107 addl $_GLOBAL_OFFSET_TABLE_, %ecx; \
110 movl errno@GOT(%ecx), %ecx; \
114 #endif /* _LIBC_REENTRANT */
117 /* Linux takes system call arguments in registers:
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
126 The stack layout upon entering the function is:
133 (%esp) Return address
135 (Of course a function with say 3 arguments does not have entries for
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
141 2 * xchg is more expensive than pushl + movl + popl
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.
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).
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.
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. */
162 #define DO_CALL(args, syscall_name) \
165 movl $SYS_ify (syscall_name), %eax; \
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. */
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
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
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
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
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
211 #else /* !__ASSEMBLER__ */
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"
223 ".macro bpushl name reg\n\t"
228 "xchgl \\reg, %ebx\n\t"
232 ".macro bpopl name reg\n\t"
237 "xchgl \\reg, %ebx\n\t"
241 ".macro bmovl name reg\n\t"
244 "movl \\reg, %ebx\n\t"
249 /* Define a macro which expands inline into the wrapper code for a system
251 #undef INLINE_SYSCALL
252 #define INLINE_SYSCALL(name, nr, args...) \
254 unsigned int resultvar; \
257 "movl %1, %%eax\n\t" \
261 : "i" (__NR_##name) ASMFMT_##nr(args) : "memory", "cc"); \
262 if (resultvar >= 0xfffff001) \
264 __set_errno (-resultvar); \
265 resultvar = 0xffffffff; \
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
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
287 #define ASMFMT_1(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)
298 #endif /* __ASSEMBLER__ */
300 #endif /* linux/i386/sysdep.h */