2005-01-23 Roland McGrath <roland@redhat.com>
[glibc.git] / sysdeps / unix / sysv / linux / i386 / sysdep.h
blob02a35dc129b116359910cba6b65a4a5ff88f3027
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
19 02111-1307 USA. */
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>
26 #include <bp-sym.h>
27 #include <bp-asm.h>
28 /* Defines RTLD_PRIVATE_ERRNO and USE_DL_SYSINFO. */
29 #include <dl-sysdep.h>
30 #include <tls.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. */
37 #undef SYS_ify
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
43 #else
44 # undef I386_USE_SYSENTER
45 #endif
47 #ifdef __ASSEMBLER__
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
58 test with -4095. */
60 /* We don't want the label for the error handle to be global when we define
61 it here. */
62 #ifdef PIC
63 # define SYSCALL_ERROR_LABEL 0f
64 #else
65 # define SYSCALL_ERROR_LABEL syscall_error
66 #endif
68 #undef PSEUDO
69 #define PSEUDO(name, syscall_name, args) \
70 .text; \
71 ENTRY (name) \
72 DO_CALL (syscall_name, args); \
73 cmpl $-4095, %eax; \
74 jae SYSCALL_ERROR_LABEL; \
75 L(pseudo_end):
77 #undef PSEUDO_END
78 #define PSEUDO_END(name) \
79 SYSCALL_ERROR_HANDLER \
80 END (name)
82 #undef PSEUDO_NOERRNO
83 #define PSEUDO_NOERRNO(name, syscall_name, args) \
84 .text; \
85 ENTRY (name) \
86 DO_CALL (syscall_name, args)
88 #undef PSEUDO_END_NOERRNO
89 #define PSEUDO_END_NOERRNO(name) \
90 END (name)
92 #define ret_NOERRNO ret
94 /* The function has to return the error code. */
95 #undef PSEUDO_ERRVAL
96 #define PSEUDO_ERRVAL(name, syscall_name, args) \
97 .text; \
98 ENTRY (name) \
99 DO_CALL (syscall_name, args); \
100 negl %eax
102 #undef PSEUDO_END_ERRVAL
103 #define PSEUDO_END_ERRVAL(name) \
104 END (name)
106 #define ret_ERRVAL ret
108 #ifndef PIC
109 # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
110 #else
112 # ifndef HAVE_HIDDEN
113 # define SETUP_PIC_REG(reg) \
114 call 1f; \
115 .subsection 1; \
116 1:movl (%esp), %e##reg; \
117 ret; \
118 .previous
119 # else
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; \
127 ret; \
128 .size __i686.get_pc_thunk.reg, . - __i686.get_pc_thunk.reg; \
129 .previous; \
130 call __i686.get_pc_thunk.reg
131 # endif
133 # if RTLD_PRIVATE_ERRNO
134 # define SYSCALL_ERROR_HANDLER \
135 0:SETUP_PIC_REG(cx); \
136 addl $_GLOBAL_OFFSET_TABLE_, %ecx; \
137 xorl %edx, %edx; \
138 subl %eax, %edx; \
139 movl %edx, rtld_errno@GOTOFF(%ecx); \
140 orl $-1, %eax; \
141 jmp L(pseudo_end);
143 # elif defined _LIBC_REENTRANT
145 # if USE___THREAD
146 # ifndef NOT_IN_libc
147 # define SYSCALL_ERROR_ERRNO __libc_errno
148 # else
149 # define SYSCALL_ERROR_ERRNO errno
150 # endif
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; \
155 xorl %edx, %edx; \
156 subl %eax, %edx; \
157 SYSCALL_ERROR_HANDLER_TLS_STORE (%edx, %ecx); \
158 orl $-1, %eax; \
159 jmp L(pseudo_end);
160 # ifndef NO_TLS_DIRECT_SEG_REFS
161 # define SYSCALL_ERROR_HANDLER_TLS_STORE(src, destoff) \
162 movl src, %gs:0(destoff)
163 # else
164 # define SYSCALL_ERROR_HANDLER_TLS_STORE(src, destoff) \
165 addl %gs:0, destoff; \
166 movl src, (destoff)
167 # endif
168 # else
169 # define SYSCALL_ERROR_HANDLER \
170 0:pushl %ebx; \
171 SETUP_PIC_REG (bx); \
172 addl $_GLOBAL_OFFSET_TABLE_, %ebx; \
173 xorl %edx, %edx; \
174 subl %eax, %edx; \
175 pushl %edx; \
176 PUSH_ERRNO_LOCATION_RETURN; \
177 call BP_SYM (__errno_location)@PLT; \
178 POP_ERRNO_LOCATION_RETURN; \
179 popl %ecx; \
180 popl %ebx; \
181 movl %ecx, (%eax); \
182 orl $-1, %eax; \
183 jmp L(pseudo_end);
184 /* A quick note: it is assumed that the call to `__errno_location' does
185 not modify the stack! */
186 # endif
187 # else
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; \
192 xorl %edx, %edx; \
193 subl %eax, %edx; \
194 movl errno@GOT(%ecx), %ecx; \
195 movl %edx, (%ecx); \
196 orl $-1, %eax; \
197 jmp L(pseudo_end);
198 # endif /* _LIBC_REENTRANT */
199 #endif /* PIC */
202 /* The original calling convention for system calls on Linux/i386 is
203 to use int $0x80. */
204 #ifdef I386_USE_SYSENTER
205 # ifdef SHARED
206 # define ENTER_KERNEL call *%gs:SYSINFO_OFFSET
207 # else
208 # define ENTER_KERNEL call *_dl_sysinfo
209 # endif
210 #else
211 # define ENTER_KERNEL int $0x80
212 #endif
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:
225 20(%esp) Arg# 5
226 16(%esp) Arg# 4
227 12(%esp) Arg# 3
228 8(%esp) Arg# 2
229 4(%esp) Arg# 1
230 (%esp) Return address
232 (Of course a function with say 3 arguments does not have entries for
233 arguments 4 and 5.)
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. */
258 #undef DO_CALL
259 #define DO_CALL(syscall_name, args) \
260 PUSHARGS_##args \
261 DOARGS_##args \
262 movl $SYS_ify (syscall_name), %eax; \
263 ENTER_KERNEL \
264 POPARGS_##args
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"
320 ".L__X'%ecx = 2\n\t"
321 ".L__X'%edx = 2\n\t"
322 ".L__X'%eax = 3\n\t"
323 ".L__X'%esi = 3\n\t"
324 ".L__X'%edi = 3\n\t"
325 ".L__X'%ebp = 3\n\t"
326 ".L__X'%esp = 3\n\t"
327 ".macro bpushl name reg\n\t"
328 ".if 1 - \\name\n\t"
329 ".if 2 - \\name\n\t"
330 "error\n\t"
331 ".else\n\t"
332 "xchgl \\reg, %ebx\n\t"
333 ".endif\n\t"
334 ".endif\n\t"
335 ".endm\n\t"
336 ".macro bpopl name reg\n\t"
337 ".if 1 - \\name\n\t"
338 ".if 2 - \\name\n\t"
339 "error\n\t"
340 ".else\n\t"
341 "xchgl \\reg, %ebx\n\t"
342 ".endif\n\t"
343 ".endif\n\t"
344 ".endm\n\t");
346 /* Define a macro which expands inline into the wrapper code for a system
347 call. */
348 #undef INLINE_SYSCALL
349 #define INLINE_SYSCALL(name, nr, args...) \
350 ({ \
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; \
357 (int) resultvar; })
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
362 gave back.
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
368 # ifdef SHARED
369 # define INTERNAL_SYSCALL(name, err, nr, args...) \
370 ({ \
371 register unsigned int resultvar; \
372 EXTRAVAR_##nr \
373 asm volatile ( \
374 LOADARGS_##nr \
375 "movl %1, %%eax\n\t" \
376 "call *%%gs:%P2\n\t" \
377 RESTOREARGS_##nr \
378 : "=a" (resultvar) \
379 : "i" (__NR_##name), "i" (offsetof (tcbhead_t, sysinfo)) \
380 ASMFMT_##nr(args) : "memory", "cc"); \
381 (int) resultvar; })
382 # define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
383 ({ \
384 register unsigned int resultvar; \
385 EXTRAVAR_##nr \
386 asm volatile ( \
387 LOADARGS_##nr \
388 "call *%%gs:%P2\n\t" \
389 RESTOREARGS_##nr \
390 : "=a" (resultvar) \
391 : "0" (name), "i" (offsetof (tcbhead_t, sysinfo)) \
392 ASMFMT_##nr(args) : "memory", "cc"); \
393 (int) resultvar; })
394 # else
395 # define INTERNAL_SYSCALL(name, err, nr, args...) \
396 ({ \
397 register unsigned int resultvar; \
398 EXTRAVAR_##nr \
399 asm volatile ( \
400 LOADARGS_##nr \
401 "movl %1, %%eax\n\t" \
402 "call *_dl_sysinfo\n\t" \
403 RESTOREARGS_##nr \
404 : "=a" (resultvar) \
405 : "i" (__NR_##name) ASMFMT_##nr(args) : "memory", "cc"); \
406 (int) resultvar; })
407 # define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
408 ({ \
409 register unsigned int resultvar; \
410 EXTRAVAR_##nr \
411 asm volatile ( \
412 LOADARGS_##nr \
413 "call *_dl_sysinfo\n\t" \
414 RESTOREARGS_##nr \
415 : "=a" (resultvar) \
416 : "0" (name) ASMFMT_##nr(args) : "memory", "cc"); \
417 (int) resultvar; })
418 # endif
419 #else
420 # define INTERNAL_SYSCALL(name, err, nr, args...) \
421 ({ \
422 register unsigned int resultvar; \
423 EXTRAVAR_##nr \
424 asm volatile ( \
425 LOADARGS_##nr \
426 "movl %1, %%eax\n\t" \
427 "int $0x80\n\t" \
428 RESTOREARGS_##nr \
429 : "=a" (resultvar) \
430 : "i" (__NR_##name) ASMFMT_##nr(args) : "memory", "cc"); \
431 (int) resultvar; })
432 # define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
433 ({ \
434 register unsigned int resultvar; \
435 EXTRAVAR_##nr \
436 asm volatile ( \
437 LOADARGS_##nr \
438 "int $0x80\n\t" \
439 RESTOREARGS_##nr \
440 : "=a" (resultvar) \
441 : "0" (name) ASMFMT_##nr(args) : "memory", "cc"); \
442 (int) resultvar; })
443 #endif
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))
455 #define LOADARGS_0
456 #ifdef __PIC__
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" \
462 "movl %3, %%ebx\n\t"
463 # else
464 # define LOADARGS_1 \
465 "bpushl .L__X'%k2, %k2\n\t"
466 # define LOADARGS_5 \
467 "movl %%ebx, %3\n\t" \
468 "movl %2, %%ebx\n\t"
469 # endif
470 # define LOADARGS_2 LOADARGS_1
471 # define LOADARGS_3 \
472 "xchgl %%ebx, %%edi\n\t"
473 # define LOADARGS_4 LOADARGS_3
474 #else
475 # define LOADARGS_1
476 # define LOADARGS_2
477 # define LOADARGS_3
478 # define LOADARGS_4
479 # define LOADARGS_5
480 #endif
482 #define RESTOREARGS_0
483 #ifdef __PIC__
484 # if defined I386_USE_SYSENTER && defined SHARED
485 # define RESTOREARGS_1 \
486 "bpopl .L__X'%k3, %k3\n\t"
487 # define RESTOREARGS_5 \
488 "movl %4, %%ebx"
489 # else
490 # define RESTOREARGS_1 \
491 "bpopl .L__X'%k2, %k2\n\t"
492 # define RESTOREARGS_5 \
493 "movl %3, %%ebx"
494 # endif
495 # define RESTOREARGS_2 RESTOREARGS_1
496 # define RESTOREARGS_3 \
497 "xchgl %%edi, %%ebx\n\t"
498 # define RESTOREARGS_4 RESTOREARGS_3
499 #else
500 # define RESTOREARGS_1
501 # define RESTOREARGS_2
502 # define RESTOREARGS_3
503 # define RESTOREARGS_4
504 # define RESTOREARGS_5
505 #endif
507 #define ASMFMT_0()
508 #ifdef __PIC__
509 # define ASMFMT_1(arg1) \
510 , "cd" (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)
519 #else
520 # define ASMFMT_1(arg1) \
521 , "b" (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)
530 #endif
532 #define EXTRAVAR_0
533 #define EXTRAVAR_1
534 #define EXTRAVAR_2
535 #define EXTRAVAR_3
536 #define EXTRAVAR_4
537 #ifdef __PIC__
538 # define EXTRAVAR_5 int _xv;
539 #else
540 # define EXTRAVAR_5
541 #endif
543 #endif /* __ASSEMBLER__ */
545 #endif /* linux/i386/sysdep.h */