1 /* Copyright (C) 1992-2013 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>
26 /* Defines RTLD_PRIVATE_ERRNO and USE_DL_SYSINFO. */
27 #include <dl-sysdep.h>
31 /* For Linux we can use the system call table in the header file
32 /usr/include/asm/unistd.h
33 of the kernel. But these symbols do not follow the SYS_* syntax
34 so we have to redefine the `SYS_ify' macro here. */
36 #define SYS_ify(syscall_name) __NR_##syscall_name
38 #if defined USE_DL_SYSINFO \
39 && (!defined NOT_IN_libc || defined IS_IN_libpthread)
40 # define I386_USE_SYSENTER 1
42 # undef I386_USE_SYSENTER
47 /* Linux uses a negative return value to indicate syscall errors,
48 unlike most Unices, which use the condition codes' carry flag.
50 Since version 2.1 the return value of a system call might be
51 negative even if the call succeeded. E.g., the `lseek' system call
52 might return a large offset. Therefore we must not anymore test
53 for < 0, but test for a real error by making sure the value in %eax
54 is a real error number. Linus said he will make sure the no syscall
55 returns a value in -1 .. -4095 as a valid result so we can savely
58 /* We don't want the label for the error handle to be global when we define
61 # define SYSCALL_ERROR_LABEL 0f
63 # define SYSCALL_ERROR_LABEL syscall_error
67 #define PSEUDO(name, syscall_name, args) \
70 DO_CALL (syscall_name, args); \
72 jae SYSCALL_ERROR_LABEL
75 #define PSEUDO_END(name) \
76 SYSCALL_ERROR_HANDLER \
80 #define PSEUDO_NOERRNO(name, syscall_name, args) \
83 DO_CALL (syscall_name, args)
85 #undef PSEUDO_END_NOERRNO
86 #define PSEUDO_END_NOERRNO(name) \
89 #define ret_NOERRNO ret
91 /* The function has to return the error code. */
93 #define PSEUDO_ERRVAL(name, syscall_name, args) \
96 DO_CALL (syscall_name, args); \
99 #undef PSEUDO_END_ERRVAL
100 #define PSEUDO_END_ERRVAL(name) \
103 #define ret_ERRVAL ret
106 # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
109 # if RTLD_PRIVATE_ERRNO
110 # define SYSCALL_ERROR_HANDLER \
111 0:SETUP_PIC_REG(cx); \
112 addl $_GLOBAL_OFFSET_TABLE_, %ecx; \
114 movl %eax, rtld_errno@GOTOFF(%ecx); \
118 # elif defined _LIBC_REENTRANT
121 # define SYSCALL_ERROR_ERRNO __libc_errno
123 # define SYSCALL_ERROR_ERRNO errno
125 # define SYSCALL_ERROR_HANDLER \
126 0:SETUP_PIC_REG (cx); \
127 addl $_GLOBAL_OFFSET_TABLE_, %ecx; \
128 movl SYSCALL_ERROR_ERRNO@GOTNTPOFF(%ecx), %ecx; \
130 SYSCALL_ERROR_HANDLER_TLS_STORE (%eax, %ecx); \
133 # ifndef NO_TLS_DIRECT_SEG_REFS
134 # define SYSCALL_ERROR_HANDLER_TLS_STORE(src, destoff) \
135 movl src, %gs:(destoff)
137 # define SYSCALL_ERROR_HANDLER_TLS_STORE(src, destoff) \
138 addl %gs:0, destoff; \
142 /* Store (- %eax) into errno through the GOT. */
143 # define SYSCALL_ERROR_HANDLER \
144 0:SETUP_PIC_REG(cx); \
145 addl $_GLOBAL_OFFSET_TABLE_, %ecx; \
147 movl errno@GOT(%ecx), %ecx; \
151 # endif /* _LIBC_REENTRANT */
155 /* The original calling convention for system calls on Linux/i386 is
157 #ifdef I386_USE_SYSENTER
159 # define ENTER_KERNEL call *%gs:SYSINFO_OFFSET
161 # define ENTER_KERNEL call *_dl_sysinfo
164 # define ENTER_KERNEL int $0x80
167 /* Linux takes system call arguments in registers:
169 syscall number %eax call-clobbered
170 arg 1 %ebx call-saved
171 arg 2 %ecx call-clobbered
172 arg 3 %edx call-clobbered
173 arg 4 %esi call-saved
174 arg 5 %edi call-saved
175 arg 6 %ebp call-saved
177 The stack layout upon entering the function is:
185 (%esp) Return address
187 (Of course a function with say 3 arguments does not have entries for
188 arguments 4, 5, and 6.)
190 The following code tries hard to be optimal. A general assumption
191 (which is true according to the data books I have) is that
193 2 * xchg is more expensive than pushl + movl + popl
195 Beside this a neat trick is used. The calling conventions for Linux
196 tell that among the registers used for parameters %ecx and %edx need
197 not be saved. Beside this we may clobber this registers even when
198 they are not used for parameter passing.
200 As a result one can see below that we save the content of the %ebx
201 register in the %edx register when we have less than 3 arguments
202 (2 * movl is less expensive than pushl + popl).
204 Second unlike for the other registers we don't save the content of
205 %ecx and %edx when we have more than 1 and 2 registers resp.
207 The code below might look a bit long but we have to take care for
208 the pipelined processors (i586). Here the `pushl' and `popl'
209 instructions are marked as NP (not pairable) but the exception is
210 two consecutive of these instruction. This gives no penalty on
211 other processors though. */
214 #define DO_CALL(syscall_name, args) \
217 movl $SYS_ify (syscall_name), %eax; \
221 #define PUSHARGS_0 /* No arguments to push. */
222 #define DOARGS_0 /* No arguments to frob. */
223 #define POPARGS_0 /* No arguments to pop. */
224 #define _PUSHARGS_0 /* No arguments to push. */
225 #define _DOARGS_0(n) /* No arguments to frob. */
226 #define _POPARGS_0 /* No arguments to pop. */
228 #define PUSHARGS_1 movl %ebx, %edx; L(SAVEBX1): PUSHARGS_0
229 #define DOARGS_1 _DOARGS_1 (4)
230 #define POPARGS_1 POPARGS_0; movl %edx, %ebx; L(RESTBX1):
231 #define _PUSHARGS_1 pushl %ebx; cfi_adjust_cfa_offset (4); \
232 cfi_rel_offset (ebx, 0); L(PUSHBX1): _PUSHARGS_0
233 #define _DOARGS_1(n) movl n(%esp), %ebx; _DOARGS_0(n-4)
234 #define _POPARGS_1 _POPARGS_0; popl %ebx; cfi_adjust_cfa_offset (-4); \
235 cfi_restore (ebx); L(POPBX1):
237 #define PUSHARGS_2 PUSHARGS_1
238 #define DOARGS_2 _DOARGS_2 (8)
239 #define POPARGS_2 POPARGS_1
240 #define _PUSHARGS_2 _PUSHARGS_1
241 #define _DOARGS_2(n) movl n(%esp), %ecx; _DOARGS_1 (n-4)
242 #define _POPARGS_2 _POPARGS_1
244 #define PUSHARGS_3 _PUSHARGS_2
245 #define DOARGS_3 _DOARGS_3 (16)
246 #define POPARGS_3 _POPARGS_3
247 #define _PUSHARGS_3 _PUSHARGS_2
248 #define _DOARGS_3(n) movl n(%esp), %edx; _DOARGS_2 (n-4)
249 #define _POPARGS_3 _POPARGS_2
251 #define PUSHARGS_4 _PUSHARGS_4
252 #define DOARGS_4 _DOARGS_4 (24)
253 #define POPARGS_4 _POPARGS_4
254 #define _PUSHARGS_4 pushl %esi; cfi_adjust_cfa_offset (4); \
255 cfi_rel_offset (esi, 0); L(PUSHSI1): _PUSHARGS_3
256 #define _DOARGS_4(n) movl n(%esp), %esi; _DOARGS_3 (n-4)
257 #define _POPARGS_4 _POPARGS_3; popl %esi; cfi_adjust_cfa_offset (-4); \
258 cfi_restore (esi); L(POPSI1):
260 #define PUSHARGS_5 _PUSHARGS_5
261 #define DOARGS_5 _DOARGS_5 (32)
262 #define POPARGS_5 _POPARGS_5
263 #define _PUSHARGS_5 pushl %edi; cfi_adjust_cfa_offset (4); \
264 cfi_rel_offset (edi, 0); L(PUSHDI1): _PUSHARGS_4
265 #define _DOARGS_5(n) movl n(%esp), %edi; _DOARGS_4 (n-4)
266 #define _POPARGS_5 _POPARGS_4; popl %edi; cfi_adjust_cfa_offset (-4); \
267 cfi_restore (edi); L(POPDI1):
269 #define PUSHARGS_6 _PUSHARGS_6
270 #define DOARGS_6 _DOARGS_6 (40)
271 #define POPARGS_6 _POPARGS_6
272 #define _PUSHARGS_6 pushl %ebp; cfi_adjust_cfa_offset (4); \
273 cfi_rel_offset (ebp, 0); L(PUSHBP1): _PUSHARGS_5
274 #define _DOARGS_6(n) movl n(%esp), %ebp; _DOARGS_5 (n-4)
275 #define _POPARGS_6 _POPARGS_5; popl %ebp; cfi_adjust_cfa_offset (-4); \
276 cfi_restore (ebp); L(POPBP1):
278 #else /* !__ASSEMBLER__ */
280 /* We need some help from the assembler to generate optimal code. We
281 define some macros here which later will be used. */
282 asm (".L__X'%ebx = 1\n\t"
290 ".macro bpushl name reg\n\t"
295 "xchgl \\reg, %ebx\n\t"
299 ".macro bpopl name reg\n\t"
304 "xchgl \\reg, %ebx\n\t"
309 /* Define a macro which expands inline into the wrapper code for a system
311 #undef INLINE_SYSCALL
312 #define INLINE_SYSCALL(name, nr, args...) \
314 unsigned int resultvar = INTERNAL_SYSCALL (name, , nr, args); \
315 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (resultvar, ), 0)) \
317 __set_errno (INTERNAL_SYSCALL_ERRNO (resultvar, )); \
318 resultvar = 0xffffffff; \
322 /* Define a macro which expands inline into the wrapper code for a system
323 call. This use is for internal calls that do not need to handle errors
324 normally. It will never touch errno. This returns just what the kernel
327 The _NCS variant allows non-constant syscall numbers but it is not
328 possible to use more than four parameters. */
329 #undef INTERNAL_SYSCALL
330 #ifdef I386_USE_SYSENTER
332 # define INTERNAL_SYSCALL(name, err, nr, args...) \
334 register unsigned int resultvar; \
338 "movl %1, %%eax\n\t" \
339 "call *%%gs:%P2\n\t" \
342 : "i" (__NR_##name), "i" (offsetof (tcbhead_t, sysinfo)) \
343 ASMFMT_##nr(args) : "memory", "cc"); \
345 # define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
347 register unsigned int resultvar; \
351 "call *%%gs:%P2\n\t" \
354 : "0" (name), "i" (offsetof (tcbhead_t, sysinfo)) \
355 ASMFMT_##nr(args) : "memory", "cc"); \
358 # define INTERNAL_SYSCALL(name, err, nr, args...) \
360 register unsigned int resultvar; \
364 "movl %1, %%eax\n\t" \
365 "call *_dl_sysinfo\n\t" \
368 : "i" (__NR_##name) ASMFMT_##nr(args) : "memory", "cc"); \
370 # define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
372 register unsigned int resultvar; \
376 "call *_dl_sysinfo\n\t" \
379 : "0" (name) ASMFMT_##nr(args) : "memory", "cc"); \
383 # define INTERNAL_SYSCALL(name, err, nr, args...) \
385 register unsigned int resultvar; \
389 "movl %1, %%eax\n\t" \
393 : "i" (__NR_##name) ASMFMT_##nr(args) : "memory", "cc"); \
395 # define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
397 register unsigned int resultvar; \
404 : "0" (name) ASMFMT_##nr(args) : "memory", "cc"); \
408 #undef INTERNAL_SYSCALL_DECL
409 #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
411 #undef INTERNAL_SYSCALL_ERROR_P
412 #define INTERNAL_SYSCALL_ERROR_P(val, err) \
413 ((unsigned int) (val) >= 0xfffff001u)
415 #undef INTERNAL_SYSCALL_ERRNO
416 #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
420 # if defined I386_USE_SYSENTER && defined SHARED
421 # define LOADARGS_1 \
422 "bpushl .L__X'%k3, %k3\n\t"
423 # define LOADARGS_5 \
424 "movl %%ebx, %4\n\t" \
427 # define LOADARGS_1 \
428 "bpushl .L__X'%k2, %k2\n\t"
429 # define LOADARGS_5 \
430 "movl %%ebx, %3\n\t" \
433 # define LOADARGS_2 LOADARGS_1
434 # define LOADARGS_3 \
435 "xchgl %%ebx, %%edi\n\t"
436 # define LOADARGS_4 LOADARGS_3
445 #define RESTOREARGS_0
447 # if defined I386_USE_SYSENTER && defined SHARED
448 # define RESTOREARGS_1 \
449 "bpopl .L__X'%k3, %k3\n\t"
450 # define RESTOREARGS_5 \
453 # define RESTOREARGS_1 \
454 "bpopl .L__X'%k2, %k2\n\t"
455 # define RESTOREARGS_5 \
458 # define RESTOREARGS_2 RESTOREARGS_1
459 # define RESTOREARGS_3 \
460 "xchgl %%edi, %%ebx\n\t"
461 # define RESTOREARGS_4 RESTOREARGS_3
463 # define RESTOREARGS_1
464 # define RESTOREARGS_2
465 # define RESTOREARGS_3
466 # define RESTOREARGS_4
467 # define RESTOREARGS_5
472 # define ASMFMT_1(arg1) \
474 # define ASMFMT_2(arg1, arg2) \
475 , "d" (arg1), "c" (arg2)
476 # define ASMFMT_3(arg1, arg2, arg3) \
477 , "D" (arg1), "c" (arg2), "d" (arg3)
478 # define ASMFMT_4(arg1, arg2, arg3, arg4) \
479 , "D" (arg1), "c" (arg2), "d" (arg3), "S" (arg4)
480 # define ASMFMT_5(arg1, arg2, arg3, arg4, arg5) \
481 , "0" (arg1), "m" (_xv), "c" (arg2), "d" (arg3), "S" (arg4), "D" (arg5)
483 # define ASMFMT_1(arg1) \
485 # define ASMFMT_2(arg1, arg2) \
486 , "b" (arg1), "c" (arg2)
487 # define ASMFMT_3(arg1, arg2, arg3) \
488 , "b" (arg1), "c" (arg2), "d" (arg3)
489 # define ASMFMT_4(arg1, arg2, arg3, arg4) \
490 , "b" (arg1), "c" (arg2), "d" (arg3), "S" (arg4)
491 # define ASMFMT_5(arg1, arg2, arg3, arg4, arg5) \
492 , "b" (arg1), "c" (arg2), "d" (arg3), "S" (arg4), "D" (arg5)
501 # define EXTRAVAR_5 int _xv;
506 /* Consistency check for position-independent code. */
508 # define check_consistency() \
510 __asm__ __volatile__ \
511 (LOAD_PIC_REG_STR (cx) ";" \
512 "subl %%ebx, %%ecx;" \
520 #endif /* __ASSEMBLER__ */
523 /* Pointer mangling support. */
524 #if defined NOT_IN_libc && defined IS_IN_rtld
525 /* We cannot use the thread descriptor because in ld.so we use setjmp
526 earlier than the descriptor is initialized. Using a global variable
527 is too complicated here since we have no PC-relative addressing mode. */
529 # ifdef __ASSEMBLER__
530 # define PTR_MANGLE(reg) xorl %gs:POINTER_GUARD, reg; \
532 # define PTR_DEMANGLE(reg) rorl $9, reg; \
533 xorl %gs:POINTER_GUARD, reg
535 # define PTR_MANGLE(var) asm ("xorl %%gs:%c2, %0\n" \
539 "i" (offsetof (tcbhead_t, \
541 # define PTR_DEMANGLE(var) asm ("rorl $9, %0\n" \
542 "xorl %%gs:%c2, %0" \
545 "i" (offsetof (tcbhead_t, \
550 #endif /* linux/i386/sysdep.h */