Update copyright notices with scripts/update-copyrights.
[glibc.git] / sysdeps / unix / sysv / linux / i386 / sysdep.h
blob071da714082b21bf307b2ee51940bcf06769dce3
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>
24 #include <bp-sym.h>
25 #include <bp-asm.h>
26 /* Defines RTLD_PRIVATE_ERRNO and USE_DL_SYSINFO. */
27 #include <dl-sysdep.h>
28 #include <tls.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. */
35 #undef SYS_ify
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
41 #else
42 # undef I386_USE_SYSENTER
43 #endif
45 #ifdef __ASSEMBLER__
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
56 test with -4095. */
58 /* We don't want the label for the error handle to be global when we define
59 it here. */
60 #ifdef PIC
61 # define SYSCALL_ERROR_LABEL 0f
62 #else
63 # define SYSCALL_ERROR_LABEL syscall_error
64 #endif
66 #undef PSEUDO
67 #define PSEUDO(name, syscall_name, args) \
68 .text; \
69 ENTRY (name) \
70 DO_CALL (syscall_name, args); \
71 cmpl $-4095, %eax; \
72 jae SYSCALL_ERROR_LABEL
74 #undef PSEUDO_END
75 #define PSEUDO_END(name) \
76 SYSCALL_ERROR_HANDLER \
77 END (name)
79 #undef PSEUDO_NOERRNO
80 #define PSEUDO_NOERRNO(name, syscall_name, args) \
81 .text; \
82 ENTRY (name) \
83 DO_CALL (syscall_name, args)
85 #undef PSEUDO_END_NOERRNO
86 #define PSEUDO_END_NOERRNO(name) \
87 END (name)
89 #define ret_NOERRNO ret
91 /* The function has to return the error code. */
92 #undef PSEUDO_ERRVAL
93 #define PSEUDO_ERRVAL(name, syscall_name, args) \
94 .text; \
95 ENTRY (name) \
96 DO_CALL (syscall_name, args); \
97 negl %eax
99 #undef PSEUDO_END_ERRVAL
100 #define PSEUDO_END_ERRVAL(name) \
101 END (name)
103 #define ret_ERRVAL ret
105 #ifndef PIC
106 # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
107 #else
109 # if RTLD_PRIVATE_ERRNO
110 # define SYSCALL_ERROR_HANDLER \
111 0:SETUP_PIC_REG(cx); \
112 addl $_GLOBAL_OFFSET_TABLE_, %ecx; \
113 negl %eax; \
114 movl %eax, rtld_errno@GOTOFF(%ecx); \
115 orl $-1, %eax; \
116 ret;
118 # elif defined _LIBC_REENTRANT
120 # ifndef NOT_IN_libc
121 # define SYSCALL_ERROR_ERRNO __libc_errno
122 # else
123 # define SYSCALL_ERROR_ERRNO errno
124 # endif
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; \
129 negl %eax; \
130 SYSCALL_ERROR_HANDLER_TLS_STORE (%eax, %ecx); \
131 orl $-1, %eax; \
132 ret;
133 # ifndef NO_TLS_DIRECT_SEG_REFS
134 # define SYSCALL_ERROR_HANDLER_TLS_STORE(src, destoff) \
135 movl src, %gs:(destoff)
136 # else
137 # define SYSCALL_ERROR_HANDLER_TLS_STORE(src, destoff) \
138 addl %gs:0, destoff; \
139 movl src, (destoff)
140 # endif
141 # else
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; \
146 negl %eax; \
147 movl errno@GOT(%ecx), %ecx; \
148 movl %eax, (%ecx); \
149 orl $-1, %eax; \
150 ret;
151 # endif /* _LIBC_REENTRANT */
152 #endif /* PIC */
155 /* The original calling convention for system calls on Linux/i386 is
156 to use int $0x80. */
157 #ifdef I386_USE_SYSENTER
158 # ifdef SHARED
159 # define ENTER_KERNEL call *%gs:SYSINFO_OFFSET
160 # else
161 # define ENTER_KERNEL call *_dl_sysinfo
162 # endif
163 #else
164 # define ENTER_KERNEL int $0x80
165 #endif
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:
179 24(%esp) Arg# 6
180 20(%esp) Arg# 5
181 16(%esp) Arg# 4
182 12(%esp) Arg# 3
183 8(%esp) Arg# 2
184 4(%esp) Arg# 1
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. */
213 #undef DO_CALL
214 #define DO_CALL(syscall_name, args) \
215 PUSHARGS_##args \
216 DOARGS_##args \
217 movl $SYS_ify (syscall_name), %eax; \
218 ENTER_KERNEL \
219 POPARGS_##args
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"
283 ".L__X'%ecx = 2\n\t"
284 ".L__X'%edx = 2\n\t"
285 ".L__X'%eax = 3\n\t"
286 ".L__X'%esi = 3\n\t"
287 ".L__X'%edi = 3\n\t"
288 ".L__X'%ebp = 3\n\t"
289 ".L__X'%esp = 3\n\t"
290 ".macro bpushl name reg\n\t"
291 ".if 1 - \\name\n\t"
292 ".if 2 - \\name\n\t"
293 "error\n\t"
294 ".else\n\t"
295 "xchgl \\reg, %ebx\n\t"
296 ".endif\n\t"
297 ".endif\n\t"
298 ".endm\n\t"
299 ".macro bpopl name reg\n\t"
300 ".if 1 - \\name\n\t"
301 ".if 2 - \\name\n\t"
302 "error\n\t"
303 ".else\n\t"
304 "xchgl \\reg, %ebx\n\t"
305 ".endif\n\t"
306 ".endif\n\t"
307 ".endm\n\t");
309 /* Define a macro which expands inline into the wrapper code for a system
310 call. */
311 #undef INLINE_SYSCALL
312 #define INLINE_SYSCALL(name, nr, args...) \
313 ({ \
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; \
320 (int) resultvar; })
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
325 gave back.
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
331 # ifdef SHARED
332 # define INTERNAL_SYSCALL(name, err, nr, args...) \
333 ({ \
334 register unsigned int resultvar; \
335 EXTRAVAR_##nr \
336 asm volatile ( \
337 LOADARGS_##nr \
338 "movl %1, %%eax\n\t" \
339 "call *%%gs:%P2\n\t" \
340 RESTOREARGS_##nr \
341 : "=a" (resultvar) \
342 : "i" (__NR_##name), "i" (offsetof (tcbhead_t, sysinfo)) \
343 ASMFMT_##nr(args) : "memory", "cc"); \
344 (int) resultvar; })
345 # define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
346 ({ \
347 register unsigned int resultvar; \
348 EXTRAVAR_##nr \
349 asm volatile ( \
350 LOADARGS_##nr \
351 "call *%%gs:%P2\n\t" \
352 RESTOREARGS_##nr \
353 : "=a" (resultvar) \
354 : "0" (name), "i" (offsetof (tcbhead_t, sysinfo)) \
355 ASMFMT_##nr(args) : "memory", "cc"); \
356 (int) resultvar; })
357 # else
358 # define INTERNAL_SYSCALL(name, err, nr, args...) \
359 ({ \
360 register unsigned int resultvar; \
361 EXTRAVAR_##nr \
362 asm volatile ( \
363 LOADARGS_##nr \
364 "movl %1, %%eax\n\t" \
365 "call *_dl_sysinfo\n\t" \
366 RESTOREARGS_##nr \
367 : "=a" (resultvar) \
368 : "i" (__NR_##name) ASMFMT_##nr(args) : "memory", "cc"); \
369 (int) resultvar; })
370 # define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
371 ({ \
372 register unsigned int resultvar; \
373 EXTRAVAR_##nr \
374 asm volatile ( \
375 LOADARGS_##nr \
376 "call *_dl_sysinfo\n\t" \
377 RESTOREARGS_##nr \
378 : "=a" (resultvar) \
379 : "0" (name) ASMFMT_##nr(args) : "memory", "cc"); \
380 (int) resultvar; })
381 # endif
382 #else
383 # define INTERNAL_SYSCALL(name, err, nr, args...) \
384 ({ \
385 register unsigned int resultvar; \
386 EXTRAVAR_##nr \
387 asm volatile ( \
388 LOADARGS_##nr \
389 "movl %1, %%eax\n\t" \
390 "int $0x80\n\t" \
391 RESTOREARGS_##nr \
392 : "=a" (resultvar) \
393 : "i" (__NR_##name) ASMFMT_##nr(args) : "memory", "cc"); \
394 (int) resultvar; })
395 # define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
396 ({ \
397 register unsigned int resultvar; \
398 EXTRAVAR_##nr \
399 asm volatile ( \
400 LOADARGS_##nr \
401 "int $0x80\n\t" \
402 RESTOREARGS_##nr \
403 : "=a" (resultvar) \
404 : "0" (name) ASMFMT_##nr(args) : "memory", "cc"); \
405 (int) resultvar; })
406 #endif
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))
418 #define LOADARGS_0
419 #ifdef __PIC__
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" \
425 "movl %3, %%ebx\n\t"
426 # else
427 # define LOADARGS_1 \
428 "bpushl .L__X'%k2, %k2\n\t"
429 # define LOADARGS_5 \
430 "movl %%ebx, %3\n\t" \
431 "movl %2, %%ebx\n\t"
432 # endif
433 # define LOADARGS_2 LOADARGS_1
434 # define LOADARGS_3 \
435 "xchgl %%ebx, %%edi\n\t"
436 # define LOADARGS_4 LOADARGS_3
437 #else
438 # define LOADARGS_1
439 # define LOADARGS_2
440 # define LOADARGS_3
441 # define LOADARGS_4
442 # define LOADARGS_5
443 #endif
445 #define RESTOREARGS_0
446 #ifdef __PIC__
447 # if defined I386_USE_SYSENTER && defined SHARED
448 # define RESTOREARGS_1 \
449 "bpopl .L__X'%k3, %k3\n\t"
450 # define RESTOREARGS_5 \
451 "movl %4, %%ebx"
452 # else
453 # define RESTOREARGS_1 \
454 "bpopl .L__X'%k2, %k2\n\t"
455 # define RESTOREARGS_5 \
456 "movl %3, %%ebx"
457 # endif
458 # define RESTOREARGS_2 RESTOREARGS_1
459 # define RESTOREARGS_3 \
460 "xchgl %%edi, %%ebx\n\t"
461 # define RESTOREARGS_4 RESTOREARGS_3
462 #else
463 # define RESTOREARGS_1
464 # define RESTOREARGS_2
465 # define RESTOREARGS_3
466 # define RESTOREARGS_4
467 # define RESTOREARGS_5
468 #endif
470 #define ASMFMT_0()
471 #ifdef __PIC__
472 # define ASMFMT_1(arg1) \
473 , "cd" (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)
482 #else
483 # define ASMFMT_1(arg1) \
484 , "b" (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)
493 #endif
495 #define EXTRAVAR_0
496 #define EXTRAVAR_1
497 #define EXTRAVAR_2
498 #define EXTRAVAR_3
499 #define EXTRAVAR_4
500 #ifdef __PIC__
501 # define EXTRAVAR_5 int _xv;
502 #else
503 # define EXTRAVAR_5
504 #endif
506 /* Consistency check for position-independent code. */
507 #ifdef __PIC__
508 # define check_consistency() \
509 ({ int __res; \
510 __asm__ __volatile__ \
511 (LOAD_PIC_REG_STR (cx) ";" \
512 "subl %%ebx, %%ecx;" \
513 "je 1f;" \
514 "ud2;" \
515 "1:\n" \
516 : "=c" (__res)); \
517 __res; })
518 #endif
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. */
528 #else
529 # ifdef __ASSEMBLER__
530 # define PTR_MANGLE(reg) xorl %gs:POINTER_GUARD, reg; \
531 roll $9, reg
532 # define PTR_DEMANGLE(reg) rorl $9, reg; \
533 xorl %gs:POINTER_GUARD, reg
534 # else
535 # define PTR_MANGLE(var) asm ("xorl %%gs:%c2, %0\n" \
536 "roll $9, %0" \
537 : "=r" (var) \
538 : "0" (var), \
539 "i" (offsetof (tcbhead_t, \
540 pointer_guard)))
541 # define PTR_DEMANGLE(var) asm ("rorl $9, %0\n" \
542 "xorl %%gs:%c2, %0" \
543 : "=r" (var) \
544 : "0" (var), \
545 "i" (offsetof (tcbhead_t, \
546 pointer_guard)))
547 # endif
548 #endif
550 #endif /* linux/i386/sysdep.h */