1 /* Copyright (C) 1992,1993,1995,1996,1997,1998,1999,2000,2002,2003,2004,2005
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper, <drepper@gnu.org>, August 1995.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 #ifndef _LINUX_I386_SYSDEP_H
22 #define _LINUX_I386_SYSDEP_H 1
24 /* There is some commonality. */
25 #include <sysdeps/unix/i386/sysdep.h>
28 /* Defines RTLD_PRIVATE_ERRNO and USE_DL_SYSINFO. */
29 #include <dl-sysdep.h>
33 /* For Linux we can use the system call table in the header file
34 /usr/include/asm/unistd.h
35 of the kernel. But these symbols do not follow the SYS_* syntax
36 so we have to redefine the `SYS_ify' macro here. */
38 #define SYS_ify(syscall_name) __NR_##syscall_name
40 #if defined USE_DL_SYSINFO \
41 && (!defined NOT_IN_libc || defined IS_IN_libpthread)
42 # define I386_USE_SYSENTER 1
44 # undef I386_USE_SYSENTER
49 /* Linux uses a negative return value to indicate syscall errors,
50 unlike most Unices, which use the condition codes' carry flag.
52 Since version 2.1 the return value of a system call might be
53 negative even if the call succeeded. E.g., the `lseek' system call
54 might return a large offset. Therefore we must not anymore test
55 for < 0, but test for a real error by making sure the value in %eax
56 is a real error number. Linus said he will make sure the no syscall
57 returns a value in -1 .. -4095 as a valid result so we can savely
60 /* We don't want the label for the error handle to be global when we define
63 # define SYSCALL_ERROR_LABEL 0f
65 # define SYSCALL_ERROR_LABEL syscall_error
69 #define PSEUDO(name, syscall_name, args) \
72 DO_CALL (syscall_name, args); \
74 jae SYSCALL_ERROR_LABEL; \
78 #define PSEUDO_END(name) \
79 SYSCALL_ERROR_HANDLER \
83 #define PSEUDO_NOERRNO(name, syscall_name, args) \
86 DO_CALL (syscall_name, args)
88 #undef PSEUDO_END_NOERRNO
89 #define PSEUDO_END_NOERRNO(name) \
92 #define ret_NOERRNO ret
94 /* The function has to return the error code. */
96 #define PSEUDO_ERRVAL(name, syscall_name, args) \
99 DO_CALL (syscall_name, args); \
102 #undef PSEUDO_END_ERRVAL
103 #define PSEUDO_END_ERRVAL(name) \
106 #define ret_ERRVAL ret
109 # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
113 # define SETUP_PIC_REG(reg) \
116 1:movl (%esp), %e##reg; \
120 # define SETUP_PIC_REG(reg) \
121 .section .gnu.linkonce.t.__i686.get_pc_thunk.reg,"ax",@progbits; \
122 .globl __i686.get_pc_thunk.reg; \
123 .hidden __i686.get_pc_thunk.reg; \
124 .type __i686.get_pc_thunk.reg,@function; \
125 __i686.get_pc_thunk.reg: \
126 movl (%esp), %e##reg; \
128 .size __i686.get_pc_thunk.reg, . - __i686.get_pc_thunk.reg; \
130 call __i686.get_pc_thunk.reg
133 # if RTLD_PRIVATE_ERRNO
134 # define SYSCALL_ERROR_HANDLER \
135 0:SETUP_PIC_REG(cx); \
136 addl $_GLOBAL_OFFSET_TABLE_, %ecx; \
139 movl %edx, rtld_errno@GOTOFF(%ecx); \
143 # elif defined _LIBC_REENTRANT
147 # define SYSCALL_ERROR_ERRNO __libc_errno
149 # define SYSCALL_ERROR_ERRNO errno
151 # define SYSCALL_ERROR_HANDLER \
152 0:SETUP_PIC_REG (cx); \
153 addl $_GLOBAL_OFFSET_TABLE_, %ecx; \
154 movl SYSCALL_ERROR_ERRNO@GOTNTPOFF(%ecx), %ecx; \
157 SYSCALL_ERROR_HANDLER_TLS_STORE (%edx, %ecx); \
160 # ifndef NO_TLS_DIRECT_SEG_REFS
161 # define SYSCALL_ERROR_HANDLER_TLS_STORE(src, destoff) \
162 movl src, %gs:0(destoff)
164 # define SYSCALL_ERROR_HANDLER_TLS_STORE(src, destoff) \
165 addl %gs:0, destoff; \
169 # define SYSCALL_ERROR_HANDLER \
171 SETUP_PIC_REG (bx); \
172 addl $_GLOBAL_OFFSET_TABLE_, %ebx; \
176 PUSH_ERRNO_LOCATION_RETURN; \
177 call BP_SYM (__errno_location)@PLT; \
178 POP_ERRNO_LOCATION_RETURN; \
184 /* A quick note: it is assumed that the call to `__errno_location' does
185 not modify the stack! */
188 /* Store (- %eax) into errno through the GOT. */
189 # define SYSCALL_ERROR_HANDLER \
190 0:SETUP_PIC_REG(cx); \
191 addl $_GLOBAL_OFFSET_TABLE_, %ecx; \
194 movl errno@GOT(%ecx), %ecx; \
198 # endif /* _LIBC_REENTRANT */
202 /* The original calling convention for system calls on Linux/i386 is
204 #ifdef I386_USE_SYSENTER
206 # define ENTER_KERNEL call *%gs:SYSINFO_OFFSET
208 # define ENTER_KERNEL call *_dl_sysinfo
211 # define ENTER_KERNEL int $0x80
214 /* Linux takes system call arguments in registers:
216 syscall number %eax call-clobbered
217 arg 1 %ebx call-saved
218 arg 2 %ecx call-clobbered
219 arg 3 %edx call-clobbered
220 arg 4 %esi call-saved
221 arg 5 %edi call-saved
223 The stack layout upon entering the function is:
230 (%esp) Return address
232 (Of course a function with say 3 arguments does not have entries for
235 The following code tries hard to be optimal. A general assumption
236 (which is true according to the data books I have) is that
238 2 * xchg is more expensive than pushl + movl + popl
240 Beside this a neat trick is used. The calling conventions for Linux
241 tell that among the registers used for parameters %ecx and %edx need
242 not be saved. Beside this we may clobber this registers even when
243 they are not used for parameter passing.
245 As a result one can see below that we save the content of the %ebx
246 register in the %edx register when we have less than 3 arguments
247 (2 * movl is less expensive than pushl + popl).
249 Second unlike for the other registers we don't save the content of
250 %ecx and %edx when we have more than 1 and 2 registers resp.
252 The code below might look a bit long but we have to take care for
253 the pipelined processors (i586). Here the `pushl' and `popl'
254 instructions are marked as NP (not pairable) but the exception is
255 two consecutive of these instruction. This gives no penalty on
256 other processors though. */
259 #define DO_CALL(syscall_name, args) \
262 movl $SYS_ify (syscall_name), %eax; \
266 #define PUSHARGS_0 /* No arguments to push. */
267 #define DOARGS_0 /* No arguments to frob. */
268 #define POPARGS_0 /* No arguments to pop. */
269 #define _PUSHARGS_0 /* No arguments to push. */
270 #define _DOARGS_0(n) /* No arguments to frob. */
271 #define _POPARGS_0 /* No arguments to pop. */
273 #define PUSHARGS_1 movl %ebx, %edx; L(SAVEBX1): PUSHARGS_0
274 #define DOARGS_1 _DOARGS_1 (4)
275 #define POPARGS_1 POPARGS_0; movl %edx, %ebx; L(RESTBX1):
276 #define _PUSHARGS_1 pushl %ebx; L(PUSHBX1): _PUSHARGS_0
277 #define _DOARGS_1(n) movl n(%esp), %ebx; _DOARGS_0(n-4)
278 #define _POPARGS_1 _POPARGS_0; popl %ebx; L(POPBX1):
280 #define PUSHARGS_2 PUSHARGS_1
281 #define DOARGS_2 _DOARGS_2 (8)
282 #define POPARGS_2 POPARGS_1
283 #define _PUSHARGS_2 _PUSHARGS_1
284 #define _DOARGS_2(n) movl n(%esp), %ecx; _DOARGS_1 (n-4)
285 #define _POPARGS_2 _POPARGS_1
287 #define PUSHARGS_3 _PUSHARGS_2
288 #define DOARGS_3 _DOARGS_3 (16)
289 #define POPARGS_3 _POPARGS_3
290 #define _PUSHARGS_3 _PUSHARGS_2
291 #define _DOARGS_3(n) movl n(%esp), %edx; _DOARGS_2 (n-4)
292 #define _POPARGS_3 _POPARGS_2
294 #define PUSHARGS_4 _PUSHARGS_4
295 #define DOARGS_4 _DOARGS_4 (24)
296 #define POPARGS_4 _POPARGS_4
297 #define _PUSHARGS_4 pushl %esi; L(PUSHSI1): _PUSHARGS_3
298 #define _DOARGS_4(n) movl n(%esp), %esi; _DOARGS_3 (n-4)
299 #define _POPARGS_4 _POPARGS_3; popl %esi; L(POPSI1):
301 #define PUSHARGS_5 _PUSHARGS_5
302 #define DOARGS_5 _DOARGS_5 (32)
303 #define POPARGS_5 _POPARGS_5
304 #define _PUSHARGS_5 pushl %edi; L(PUSHDI1): _PUSHARGS_4
305 #define _DOARGS_5(n) movl n(%esp), %edi; _DOARGS_4 (n-4)
306 #define _POPARGS_5 _POPARGS_4; popl %edi; L(POPDI1):
308 #define PUSHARGS_6 _PUSHARGS_6
309 #define DOARGS_6 _DOARGS_6 (36)
310 #define POPARGS_6 _POPARGS_6
311 #define _PUSHARGS_6 pushl %ebp; L(PUSHBP1): _PUSHARGS_5
312 #define _DOARGS_6(n) movl n(%esp), %ebp; _DOARGS_5 (n-4)
313 #define _POPARGS_6 _POPARGS_5; popl %ebp; L(POPBP1):
315 #else /* !__ASSEMBLER__ */
317 /* We need some help from the assembler to generate optimal code. We
318 define some macros here which later will be used. */
319 asm (".L__X'%ebx = 1\n\t"
327 ".macro bpushl name reg\n\t"
332 "xchgl \\reg, %ebx\n\t"
336 ".macro bpopl name reg\n\t"
341 "xchgl \\reg, %ebx\n\t"
346 /* Define a macro which expands inline into the wrapper code for a system
348 #undef INLINE_SYSCALL
349 #define INLINE_SYSCALL(name, nr, args...) \
351 unsigned int resultvar = INTERNAL_SYSCALL (name, , nr, args); \
352 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (resultvar, ), 0)) \
354 __set_errno (INTERNAL_SYSCALL_ERRNO (resultvar, )); \
355 resultvar = 0xffffffff; \
359 /* Define a macro which expands inline into the wrapper code for a system
360 call. This use is for internal calls that do not need to handle errors
361 normally. It will never touch errno. This returns just what the kernel
364 The _NCS variant allows non-constant syscall numbers but it is not
365 possible to use more than four parameters. */
366 #undef INTERNAL_SYSCALL
367 #ifdef I386_USE_SYSENTER
369 # define INTERNAL_SYSCALL(name, err, nr, args...) \
371 register unsigned int resultvar; \
375 "movl %1, %%eax\n\t" \
376 "call *%%gs:%P2\n\t" \
379 : "i" (__NR_##name), "i" (offsetof (tcbhead_t, sysinfo)) \
380 ASMFMT_##nr(args) : "memory", "cc"); \
382 # define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
384 register unsigned int resultvar; \
388 "call *%%gs:%P2\n\t" \
391 : "0" (name), "i" (offsetof (tcbhead_t, sysinfo)) \
392 ASMFMT_##nr(args) : "memory", "cc"); \
395 # define INTERNAL_SYSCALL(name, err, nr, args...) \
397 register unsigned int resultvar; \
401 "movl %1, %%eax\n\t" \
402 "call *_dl_sysinfo\n\t" \
405 : "i" (__NR_##name) ASMFMT_##nr(args) : "memory", "cc"); \
407 # define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
409 register unsigned int resultvar; \
413 "call *_dl_sysinfo\n\t" \
416 : "0" (name) ASMFMT_##nr(args) : "memory", "cc"); \
420 # define INTERNAL_SYSCALL(name, err, nr, args...) \
422 register unsigned int resultvar; \
426 "movl %1, %%eax\n\t" \
430 : "i" (__NR_##name) ASMFMT_##nr(args) : "memory", "cc"); \
432 # define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
434 register unsigned int resultvar; \
441 : "0" (name) ASMFMT_##nr(args) : "memory", "cc"); \
445 #undef INTERNAL_SYSCALL_DECL
446 #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
448 #undef INTERNAL_SYSCALL_ERROR_P
449 #define INTERNAL_SYSCALL_ERROR_P(val, err) \
450 ((unsigned int) (val) >= 0xfffff001u)
452 #undef INTERNAL_SYSCALL_ERRNO
453 #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
457 # if defined I386_USE_SYSENTER
458 # define LOADARGS_1 \
459 "bpushl .L__X'%k3, %k3\n\t"
460 # define LOADARGS_5 \
461 "movl %%ebx, %4\n\t" \
464 # define LOADARGS_1 \
465 "bpushl .L__X'%k2, %k2\n\t"
466 # define LOADARGS_5 \
467 "movl %%ebx, %3\n\t" \
470 # define LOADARGS_2 LOADARGS_1
471 # define LOADARGS_3 \
472 "xchgl %%ebx, %%edi\n\t"
473 # define LOADARGS_4 LOADARGS_3
482 #define RESTOREARGS_0
484 # if defined I386_USE_SYSENTER && defined SHARED
485 # define RESTOREARGS_1 \
486 "bpopl .L__X'%k3, %k3\n\t"
487 # define RESTOREARGS_5 \
490 # define RESTOREARGS_1 \
491 "bpopl .L__X'%k2, %k2\n\t"
492 # define RESTOREARGS_5 \
495 # define RESTOREARGS_2 RESTOREARGS_1
496 # define RESTOREARGS_3 \
497 "xchgl %%edi, %%ebx\n\t"
498 # define RESTOREARGS_4 RESTOREARGS_3
500 # define RESTOREARGS_1
501 # define RESTOREARGS_2
502 # define RESTOREARGS_3
503 # define RESTOREARGS_4
504 # define RESTOREARGS_5
509 # define ASMFMT_1(arg1) \
511 # define ASMFMT_2(arg1, arg2) \
512 , "d" (arg1), "c" (arg2)
513 # define ASMFMT_3(arg1, arg2, arg3) \
514 , "D" (arg1), "c" (arg2), "d" (arg3)
515 # define ASMFMT_4(arg1, arg2, arg3, arg4) \
516 , "D" (arg1), "c" (arg2), "d" (arg3), "S" (arg4)
517 # define ASMFMT_5(arg1, arg2, arg3, arg4, arg5) \
518 , "0" (arg1), "m" (_xv), "c" (arg2), "d" (arg3), "S" (arg4), "D" (arg5)
520 # define ASMFMT_1(arg1) \
522 # define ASMFMT_2(arg1, arg2) \
523 , "b" (arg1), "c" (arg2)
524 # define ASMFMT_3(arg1, arg2, arg3) \
525 , "b" (arg1), "c" (arg2), "d" (arg3)
526 # define ASMFMT_4(arg1, arg2, arg3, arg4) \
527 , "b" (arg1), "c" (arg2), "d" (arg3), "S" (arg4)
528 # define ASMFMT_5(arg1, arg2, arg3, arg4, arg5) \
529 , "b" (arg1), "c" (arg2), "d" (arg3), "S" (arg4), "D" (arg5)
538 # define EXTRAVAR_5 int _xv;
543 #endif /* __ASSEMBLER__ */
545 #endif /* linux/i386/sysdep.h */