1 /* Copyright (C) 1992-2014 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, see
17 <http://www.gnu.org/licenses/>. */
19 #ifndef _LINUX_I386_SYSDEP_H
20 #define _LINUX_I386_SYSDEP_H 1
22 /* There is some commonality. */
23 #include <sysdeps/unix/i386/sysdep.h>
24 /* Defines RTLD_PRIVATE_ERRNO and USE_DL_SYSINFO. */
25 #include <dl-sysdep.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. */
34 #define SYS_ify(syscall_name) __NR_##syscall_name
36 #if defined USE_DL_SYSINFO \
37 && (!defined NOT_IN_libc || defined IS_IN_libpthread)
38 # define I386_USE_SYSENTER 1
40 # undef I386_USE_SYSENTER
45 /* Linux uses a negative return value to indicate syscall errors,
46 unlike most Unices, which use the condition codes' carry flag.
48 Since version 2.1 the return value of a system call might be
49 negative even if the call succeeded. E.g., the `lseek' system call
50 might return a large offset. Therefore we must not anymore test
51 for < 0, but test for a real error by making sure the value in %eax
52 is a real error number. Linus said he will make sure the no syscall
53 returns a value in -1 .. -4095 as a valid result so we can savely
56 /* We don't want the label for the error handle to be global when we define
59 # define SYSCALL_ERROR_LABEL 0f
61 # define SYSCALL_ERROR_LABEL syscall_error
65 #define PSEUDO(name, syscall_name, args) \
68 DO_CALL (syscall_name, args); \
70 jae SYSCALL_ERROR_LABEL
73 #define PSEUDO_END(name) \
74 SYSCALL_ERROR_HANDLER \
78 #define PSEUDO_NOERRNO(name, syscall_name, args) \
81 DO_CALL (syscall_name, args)
83 #undef PSEUDO_END_NOERRNO
84 #define PSEUDO_END_NOERRNO(name) \
87 #define ret_NOERRNO ret
89 /* The function has to return the error code. */
91 #define PSEUDO_ERRVAL(name, syscall_name, args) \
94 DO_CALL (syscall_name, args); \
97 #undef PSEUDO_END_ERRVAL
98 #define PSEUDO_END_ERRVAL(name) \
101 #define ret_ERRVAL ret
104 # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
107 # if RTLD_PRIVATE_ERRNO
108 # define SYSCALL_ERROR_HANDLER \
109 0:SETUP_PIC_REG(cx); \
110 addl $_GLOBAL_OFFSET_TABLE_, %ecx; \
112 movl %eax, rtld_errno@GOTOFF(%ecx); \
116 # elif defined _LIBC_REENTRANT
119 # define SYSCALL_ERROR_ERRNO __libc_errno
121 # define SYSCALL_ERROR_ERRNO errno
123 # define SYSCALL_ERROR_HANDLER \
124 0:SETUP_PIC_REG (cx); \
125 addl $_GLOBAL_OFFSET_TABLE_, %ecx; \
126 movl SYSCALL_ERROR_ERRNO@GOTNTPOFF(%ecx), %ecx; \
128 SYSCALL_ERROR_HANDLER_TLS_STORE (%eax, %ecx); \
131 # ifndef NO_TLS_DIRECT_SEG_REFS
132 # define SYSCALL_ERROR_HANDLER_TLS_STORE(src, destoff) \
133 movl src, %gs:(destoff)
135 # define SYSCALL_ERROR_HANDLER_TLS_STORE(src, destoff) \
136 addl %gs:0, destoff; \
140 /* Store (- %eax) into errno through the GOT. */
141 # define SYSCALL_ERROR_HANDLER \
142 0:SETUP_PIC_REG(cx); \
143 addl $_GLOBAL_OFFSET_TABLE_, %ecx; \
145 movl errno@GOT(%ecx), %ecx; \
149 # endif /* _LIBC_REENTRANT */
153 /* The original calling convention for system calls on Linux/i386 is
155 #ifdef I386_USE_SYSENTER
157 # define ENTER_KERNEL call *%gs:SYSINFO_OFFSET
159 # define ENTER_KERNEL call *_dl_sysinfo
162 # define ENTER_KERNEL int $0x80
165 /* Linux takes system call arguments in registers:
167 syscall number %eax call-clobbered
168 arg 1 %ebx call-saved
169 arg 2 %ecx call-clobbered
170 arg 3 %edx call-clobbered
171 arg 4 %esi call-saved
172 arg 5 %edi call-saved
173 arg 6 %ebp call-saved
175 The stack layout upon entering the function is:
183 (%esp) Return address
185 (Of course a function with say 3 arguments does not have entries for
186 arguments 4, 5, and 6.)
188 The following code tries hard to be optimal. A general assumption
189 (which is true according to the data books I have) is that
191 2 * xchg is more expensive than pushl + movl + popl
193 Beside this a neat trick is used. The calling conventions for Linux
194 tell that among the registers used for parameters %ecx and %edx need
195 not be saved. Beside this we may clobber this registers even when
196 they are not used for parameter passing.
198 As a result one can see below that we save the content of the %ebx
199 register in the %edx register when we have less than 3 arguments
200 (2 * movl is less expensive than pushl + popl).
202 Second unlike for the other registers we don't save the content of
203 %ecx and %edx when we have more than 1 and 2 registers resp.
205 The code below might look a bit long but we have to take care for
206 the pipelined processors (i586). Here the `pushl' and `popl'
207 instructions are marked as NP (not pairable) but the exception is
208 two consecutive of these instruction. This gives no penalty on
209 other processors though. */
212 #define DO_CALL(syscall_name, args) \
215 movl $SYS_ify (syscall_name), %eax; \
219 #define PUSHARGS_0 /* No arguments to push. */
220 #define DOARGS_0 /* No arguments to frob. */
221 #define POPARGS_0 /* No arguments to pop. */
222 #define _PUSHARGS_0 /* No arguments to push. */
223 #define _DOARGS_0(n) /* No arguments to frob. */
224 #define _POPARGS_0 /* No arguments to pop. */
226 #define PUSHARGS_1 movl %ebx, %edx; L(SAVEBX1): PUSHARGS_0
227 #define DOARGS_1 _DOARGS_1 (4)
228 #define POPARGS_1 POPARGS_0; movl %edx, %ebx; L(RESTBX1):
229 #define _PUSHARGS_1 pushl %ebx; cfi_adjust_cfa_offset (4); \
230 cfi_rel_offset (ebx, 0); L(PUSHBX1): _PUSHARGS_0
231 #define _DOARGS_1(n) movl n(%esp), %ebx; _DOARGS_0(n-4)
232 #define _POPARGS_1 _POPARGS_0; popl %ebx; cfi_adjust_cfa_offset (-4); \
233 cfi_restore (ebx); L(POPBX1):
235 #define PUSHARGS_2 PUSHARGS_1
236 #define DOARGS_2 _DOARGS_2 (8)
237 #define POPARGS_2 POPARGS_1
238 #define _PUSHARGS_2 _PUSHARGS_1
239 #define _DOARGS_2(n) movl n(%esp), %ecx; _DOARGS_1 (n-4)
240 #define _POPARGS_2 _POPARGS_1
242 #define PUSHARGS_3 _PUSHARGS_2
243 #define DOARGS_3 _DOARGS_3 (16)
244 #define POPARGS_3 _POPARGS_3
245 #define _PUSHARGS_3 _PUSHARGS_2
246 #define _DOARGS_3(n) movl n(%esp), %edx; _DOARGS_2 (n-4)
247 #define _POPARGS_3 _POPARGS_2
249 #define PUSHARGS_4 _PUSHARGS_4
250 #define DOARGS_4 _DOARGS_4 (24)
251 #define POPARGS_4 _POPARGS_4
252 #define _PUSHARGS_4 pushl %esi; cfi_adjust_cfa_offset (4); \
253 cfi_rel_offset (esi, 0); L(PUSHSI1): _PUSHARGS_3
254 #define _DOARGS_4(n) movl n(%esp), %esi; _DOARGS_3 (n-4)
255 #define _POPARGS_4 _POPARGS_3; popl %esi; cfi_adjust_cfa_offset (-4); \
256 cfi_restore (esi); L(POPSI1):
258 #define PUSHARGS_5 _PUSHARGS_5
259 #define DOARGS_5 _DOARGS_5 (32)
260 #define POPARGS_5 _POPARGS_5
261 #define _PUSHARGS_5 pushl %edi; cfi_adjust_cfa_offset (4); \
262 cfi_rel_offset (edi, 0); L(PUSHDI1): _PUSHARGS_4
263 #define _DOARGS_5(n) movl n(%esp), %edi; _DOARGS_4 (n-4)
264 #define _POPARGS_5 _POPARGS_4; popl %edi; cfi_adjust_cfa_offset (-4); \
265 cfi_restore (edi); L(POPDI1):
267 #define PUSHARGS_6 _PUSHARGS_6
268 #define DOARGS_6 _DOARGS_6 (40)
269 #define POPARGS_6 _POPARGS_6
270 #define _PUSHARGS_6 pushl %ebp; cfi_adjust_cfa_offset (4); \
271 cfi_rel_offset (ebp, 0); L(PUSHBP1): _PUSHARGS_5
272 #define _DOARGS_6(n) movl n(%esp), %ebp; _DOARGS_5 (n-4)
273 #define _POPARGS_6 _POPARGS_5; popl %ebp; cfi_adjust_cfa_offset (-4); \
274 cfi_restore (ebp); L(POPBP1):
276 #else /* !__ASSEMBLER__ */
278 /* We need some help from the assembler to generate optimal code. We
279 define some macros here which later will be used. */
280 asm (".L__X'%ebx = 1\n\t"
288 ".macro bpushl name reg\n\t"
293 "xchgl \\reg, %ebx\n\t"
297 ".macro bpopl name reg\n\t"
302 "xchgl \\reg, %ebx\n\t"
307 /* Define a macro which expands inline into the wrapper code for a system
309 #undef INLINE_SYSCALL
310 #define INLINE_SYSCALL(name, nr, args...) \
312 unsigned int resultvar = INTERNAL_SYSCALL (name, , nr, args); \
313 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (resultvar, ), 0)) \
315 __set_errno (INTERNAL_SYSCALL_ERRNO (resultvar, )); \
316 resultvar = 0xffffffff; \
320 /* Define a macro which expands inline into the wrapper code for a system
321 call. This use is for internal calls that do not need to handle errors
322 normally. It will never touch errno. This returns just what the kernel
325 The _NCS variant allows non-constant syscall numbers but it is not
326 possible to use more than four parameters. */
327 #undef INTERNAL_SYSCALL
328 #ifdef I386_USE_SYSENTER
330 # define INTERNAL_SYSCALL(name, err, nr, args...) \
332 register unsigned int resultvar; \
336 "movl %1, %%eax\n\t" \
337 "call *%%gs:%P2\n\t" \
340 : "i" (__NR_##name), "i" (offsetof (tcbhead_t, sysinfo)) \
341 ASMFMT_##nr(args) : "memory", "cc"); \
343 # define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
345 register unsigned int resultvar; \
349 "call *%%gs:%P2\n\t" \
352 : "0" (name), "i" (offsetof (tcbhead_t, sysinfo)) \
353 ASMFMT_##nr(args) : "memory", "cc"); \
356 # define INTERNAL_SYSCALL(name, err, nr, args...) \
358 register unsigned int resultvar; \
362 "movl %1, %%eax\n\t" \
363 "call *_dl_sysinfo\n\t" \
366 : "i" (__NR_##name) ASMFMT_##nr(args) : "memory", "cc"); \
368 # define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
370 register unsigned int resultvar; \
374 "call *_dl_sysinfo\n\t" \
377 : "0" (name) ASMFMT_##nr(args) : "memory", "cc"); \
381 # define INTERNAL_SYSCALL(name, err, nr, args...) \
383 register unsigned int resultvar; \
387 "movl %1, %%eax\n\t" \
391 : "i" (__NR_##name) ASMFMT_##nr(args) : "memory", "cc"); \
393 # define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
395 register unsigned int resultvar; \
402 : "0" (name) ASMFMT_##nr(args) : "memory", "cc"); \
406 #undef INTERNAL_SYSCALL_DECL
407 #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
409 #undef INTERNAL_SYSCALL_ERROR_P
410 #define INTERNAL_SYSCALL_ERROR_P(val, err) \
411 ((unsigned int) (val) >= 0xfffff001u)
413 #undef INTERNAL_SYSCALL_ERRNO
414 #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
418 # if defined I386_USE_SYSENTER && defined SHARED
419 # define LOADARGS_1 \
420 "bpushl .L__X'%k3, %k3\n\t"
421 # define LOADARGS_5 \
422 "movl %%ebx, %4\n\t" \
425 # define LOADARGS_1 \
426 "bpushl .L__X'%k2, %k2\n\t"
427 # define LOADARGS_5 \
428 "movl %%ebx, %3\n\t" \
431 # define LOADARGS_2 LOADARGS_1
432 # define LOADARGS_3 \
433 "xchgl %%ebx, %%edi\n\t"
434 # define LOADARGS_4 LOADARGS_3
443 #define RESTOREARGS_0
445 # if defined I386_USE_SYSENTER && defined SHARED
446 # define RESTOREARGS_1 \
447 "bpopl .L__X'%k3, %k3\n\t"
448 # define RESTOREARGS_5 \
451 # define RESTOREARGS_1 \
452 "bpopl .L__X'%k2, %k2\n\t"
453 # define RESTOREARGS_5 \
456 # define RESTOREARGS_2 RESTOREARGS_1
457 # define RESTOREARGS_3 \
458 "xchgl %%edi, %%ebx\n\t"
459 # define RESTOREARGS_4 RESTOREARGS_3
461 # define RESTOREARGS_1
462 # define RESTOREARGS_2
463 # define RESTOREARGS_3
464 # define RESTOREARGS_4
465 # define RESTOREARGS_5
470 # define ASMFMT_1(arg1) \
472 # define ASMFMT_2(arg1, arg2) \
473 , "d" (arg1), "c" (arg2)
474 # define ASMFMT_3(arg1, arg2, arg3) \
475 , "D" (arg1), "c" (arg2), "d" (arg3)
476 # define ASMFMT_4(arg1, arg2, arg3, arg4) \
477 , "D" (arg1), "c" (arg2), "d" (arg3), "S" (arg4)
478 # define ASMFMT_5(arg1, arg2, arg3, arg4, arg5) \
479 , "0" (arg1), "m" (_xv), "c" (arg2), "d" (arg3), "S" (arg4), "D" (arg5)
481 # define ASMFMT_1(arg1) \
483 # define ASMFMT_2(arg1, arg2) \
484 , "b" (arg1), "c" (arg2)
485 # define ASMFMT_3(arg1, arg2, arg3) \
486 , "b" (arg1), "c" (arg2), "d" (arg3)
487 # define ASMFMT_4(arg1, arg2, arg3, arg4) \
488 , "b" (arg1), "c" (arg2), "d" (arg3), "S" (arg4)
489 # define ASMFMT_5(arg1, arg2, arg3, arg4, arg5) \
490 , "b" (arg1), "c" (arg2), "d" (arg3), "S" (arg4), "D" (arg5)
499 # define EXTRAVAR_5 int _xv;
504 /* Consistency check for position-independent code. */
506 # define check_consistency() \
508 __asm__ __volatile__ \
509 (LOAD_PIC_REG_STR (cx) ";" \
510 "subl %%ebx, %%ecx;" \
518 #endif /* __ASSEMBLER__ */
521 /* Pointer mangling support. */
522 #if defined NOT_IN_libc && defined IS_IN_rtld
523 /* We cannot use the thread descriptor because in ld.so we use setjmp
524 earlier than the descriptor is initialized. Using a global variable
525 is too complicated here since we have no PC-relative addressing mode. */
527 # ifdef __ASSEMBLER__
528 # define PTR_MANGLE(reg) xorl %gs:POINTER_GUARD, reg; \
530 # define PTR_DEMANGLE(reg) rorl $9, reg; \
531 xorl %gs:POINTER_GUARD, reg
533 # define PTR_MANGLE(var) asm ("xorl %%gs:%c2, %0\n" \
537 "i" (offsetof (tcbhead_t, \
539 # define PTR_DEMANGLE(var) asm ("rorl $9, %0\n" \
540 "xorl %%gs:%c2, %0" \
543 "i" (offsetof (tcbhead_t, \
548 #endif /* linux/i386/sysdep.h */