OSDN Git Service

fdf7de147b5648da7ede6d8dbc3b74b59803bbdf
[uclinux-h8/uClibc.git] / libc / sysdeps / linux / x86_64 / sysdep.h
1 /* Copyright (C) 2001-2005, 2007 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <http://www.gnu.org/licenses/>.  */
17
18 #ifndef _LINUX_X86_64_SYSDEP_H
19 #define _LINUX_X86_64_SYSDEP_H 1
20
21 /* There is some commonality.  */
22 #include <sys/syscall.h>
23 #include <common/sysdep.h>
24
25 #ifdef  __ASSEMBLER__
26
27 /* Syntactic details of assembler.  */
28
29 /* ELF uses byte-counts for .align, most others use log2 of count of bytes.  */
30 #define ALIGNARG(log2) 1<<log2
31 /* For ELF we need the `.type' directive to make shared libs work right.  */
32 #define ASM_TYPE_DIRECTIVE(name,typearg) .type name,typearg;
33 #define ASM_SIZE_DIRECTIVE(name) .size name,.-name;
34
35 /* In ELF C symbols are asm symbols.  */
36 #undef  NO_UNDERSCORES
37 #define NO_UNDERSCORES
38
39 /* Define an entry point visible from C.  */
40 #define ENTRY(name)                                                           \
41   ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME(name);                                   \
42   ASM_TYPE_DIRECTIVE (C_SYMBOL_NAME(name),@function)                          \
43   .align ALIGNARG(4);                                                         \
44   C_LABEL(name)                                                               \
45   cfi_startproc;                                                              \
46   CALL_MCOUNT
47
48 #undef  END
49 #define END(name)                                                             \
50   cfi_endproc;                                                                \
51   ASM_SIZE_DIRECTIVE(name)
52
53 /* If compiled for profiling, call `mcount' at the start of each function.  */
54 #ifdef  PROF
55 /* The mcount code relies on a normal frame pointer being on the stack
56    to locate our caller, so push one just for its benefit.  */
57 #define CALL_MCOUNT                                                          \
58   pushq %rbp;                                                                \
59   cfi_adjust_cfa_offset(8);                                                  \
60   movq %rsp, %rbp;                                                           \
61   cfi_def_cfa_register(%rbp);                                                \
62   call JUMPTARGET(mcount);                                                   \
63   popq %rbp;                                                                 \
64   cfi_def_cfa(rsp,8);
65 #else
66 #define CALL_MCOUNT             /* Do nothing.  */
67 #endif
68
69 #ifdef  NO_UNDERSCORES
70 /* Since C identifiers are not normally prefixed with an underscore
71    on this system, the asm identifier `syscall_error' intrudes on the
72    C name space.  Make sure we use an innocuous name.  */
73 #define syscall_error   __syscall_error
74 #define mcount          _mcount
75 #endif
76
77 #define PSEUDO(name, syscall_name, args)                                      \
78 lose:                                                                         \
79   jmp JUMPTARGET(syscall_error)                                               \
80   .globl syscall_error;                                                       \
81   ENTRY (name)                                                                \
82   DO_CALL (syscall_name, args);                                               \
83   jb lose
84
85 #undef  PSEUDO_END
86 #define PSEUDO_END(name)                                                      \
87   END (name)
88
89 #undef JUMPTARGET
90 #ifdef __PIC__
91 #define JUMPTARGET(name)        name##@PLT
92 #else
93 #define JUMPTARGET(name)        name
94 #endif
95
96 /* Local label name for asm code. */
97 #ifndef L
98 #define L(name) .L##name
99 #endif
100
101 #endif  /* __ASSEMBLER__ */
102
103 /* For Linux we can use the system call table in the header file
104         /usr/include/asm/unistd.h
105    of the kernel.  But these symbols do not follow the SYS_* syntax
106    so we have to redefine the `SYS_ify' macro here.  */
107 #undef SYS_ify
108 #define SYS_ify(syscall_name)   __NR_##syscall_name
109
110 /* This is a kludge to make syscalls.list find these under the names
111    pread and pwrite, since some kernel headers define those names
112    and some define the *64 names for the same system calls.  */
113 #if !defined __NR_pread && defined __NR_pread64
114 # define __NR_pread __NR_pread64
115 #endif
116 #if !defined __NR_pwrite && defined __NR_pwrite64
117 # define __NR_pwrite __NR_pwrite64
118 #endif
119
120 /* This is to help the old kernel headers where __NR_semtimedop is not
121    available.  */
122 #ifndef __NR_semtimedop
123 # define __NR_semtimedop 220
124 #endif
125
126
127 #ifdef __ASSEMBLER__
128
129 /* Linux uses a negative return value to indicate syscall errors,
130    unlike most Unices, which use the condition codes' carry flag.
131
132    Since version 2.1 the return value of a system call might be
133    negative even if the call succeeded.  E.g., the `lseek' system call
134    might return a large offset.  Therefore we must not anymore test
135    for < 0, but test for a real error by making sure the value in %eax
136    is a real error number.  Linus said he will make sure the no syscall
137    returns a value in -1 .. -4095 as a valid result so we can savely
138    test with -4095.  */
139
140 /* We don't want the label for the error handle to be global when we define
141    it here.  */
142 # ifdef __PIC__
143 #  define SYSCALL_ERROR_LABEL 0f
144 # else
145 #  define SYSCALL_ERROR_LABEL syscall_error
146 # endif
147
148 # undef PSEUDO
149 # define PSEUDO(name, syscall_name, args)                                     \
150   .text;                                                                      \
151   ENTRY (name)                                                                \
152     DO_CALL (syscall_name, args);                                             \
153     cmpq $-4095, %rax;                                                        \
154     jae SYSCALL_ERROR_LABEL;                                                  \
155   L(pseudo_end):
156
157 # undef PSEUDO_END
158 # define PSEUDO_END(name)                                                     \
159   SYSCALL_ERROR_HANDLER                                                       \
160   END (name)
161
162 # undef PSEUDO_NOERRNO
163 # define PSEUDO_NOERRNO(name, syscall_name, args) \
164   .text;                                                                      \
165   ENTRY (name)                                                                \
166     DO_CALL (syscall_name, args)
167
168 # undef PSEUDO_END_NOERRNO
169 # define PSEUDO_END_NOERRNO(name) \
170   END (name)
171
172 # define ret_NOERRNO ret
173
174 # undef PSEUDO_ERRVAL
175 # define PSEUDO_ERRVAL(name, syscall_name, args) \
176   .text;                                                                      \
177   ENTRY (name)                                                                \
178     DO_CALL (syscall_name, args);                                             \
179     negq %rax
180
181 # undef PSEUDO_END_ERRVAL
182 # define PSEUDO_END_ERRVAL(name) \
183   END (name)
184
185 # define ret_ERRVAL ret
186
187 # ifndef __PIC__
188 #  define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used.  */
189 # elif defined(RTLD_PRIVATE_ERRNO)
190 #  define SYSCALL_ERROR_HANDLER                 \
191 0:                                              \
192   leaq rtld_errno(%rip), %rcx;                  \
193   xorl %edx, %edx;                              \
194   subq %rax, %rdx;                              \
195   movl %edx, (%rcx);                            \
196   orq $-1, %rax;                                \
197   jmp L(pseudo_end);
198 # elif USE___THREAD
199 #  ifndef NOT_IN_libc
200 #   define SYSCALL_ERROR_ERRNO __libc_errno
201 #  else
202 #   define SYSCALL_ERROR_ERRNO errno
203 #  endif
204 #  define SYSCALL_ERROR_HANDLER                 \
205 0:                                              \
206   movq SYSCALL_ERROR_ERRNO@GOTTPOFF(%rip), %rcx;\
207   xorl %edx, %edx;                              \
208   subq %rax, %rdx;                              \
209   movl %edx, %fs:(%rcx);                        \
210   orq $-1, %rax;                                \
211   jmp L(pseudo_end);
212 # elif defined _LIBC_REENTRANT
213 /* Store (- %rax) into errno through the GOT.
214    Note that errno occupies only 4 bytes.  */
215 #  define SYSCALL_ERROR_HANDLER                 \
216 0:                                              \
217   xorl %edx, %edx;                              \
218   subq %rax, %rdx;                              \
219   pushq %rdx;                                   \
220   cfi_adjust_cfa_offset(8);                     \
221   call __errno_location@PLT;            \
222   popq %rdx;                                    \
223   cfi_adjust_cfa_offset(-8);                    \
224   movl %edx, (%rax);                            \
225   orq $-1, %rax;                                \
226   jmp L(pseudo_end);
227
228 /* A quick note: it is assumed that the call to `__errno_location' does
229    not modify the stack!  */
230 # else /* Not _LIBC_REENTRANT.  */
231 #  define SYSCALL_ERROR_HANDLER                 \
232 0:movq errno@GOTPCREL(%RIP), %rcx;              \
233   xorl %edx, %edx;                              \
234   subq %rax, %rdx;                              \
235   movl %edx, (%rcx);                            \
236   orq $-1, %rax;                                \
237   jmp L(pseudo_end);
238 # endif /* __PIC__ */
239
240 /* The Linux/x86-64 kernel expects the system call parameters in
241    registers according to the following table:
242
243     syscall number      rax
244     arg 1               rdi
245     arg 2               rsi
246     arg 3               rdx
247     arg 4               r10
248     arg 5               r8
249     arg 6               r9
250
251     The Linux kernel uses and destroys internally these registers:
252     return address from
253     syscall             rcx
254     eflags from syscall r11
255
256     Normal function call, including calls to the system call stub
257     functions in the libc, get the first six parameters passed in
258     registers and the seventh parameter and later on the stack.  The
259     register use is as follows:
260
261      system call number in the DO_CALL macro
262      arg 1              rdi
263      arg 2              rsi
264      arg 3              rdx
265      arg 4              rcx
266      arg 5              r8
267      arg 6              r9
268
269     We have to take care that the stack is aligned to 16 bytes.  When
270     called the stack is not aligned since the return address has just
271     been pushed.
272
273
274     Syscalls of more than 6 arguments are not supported.  */
275
276 # undef DO_CALL
277 # define DO_CALL(syscall_name, args)            \
278     DOARGS_##args                               \
279     movl $SYS_ify (syscall_name), %eax;         \
280     syscall;
281
282 # define DOARGS_0 /* nothing */
283 # define DOARGS_1 /* nothing */
284 # define DOARGS_2 /* nothing */
285 # define DOARGS_3 /* nothing */
286 # define DOARGS_4 movq %rcx, %r10;
287 # define DOARGS_5 DOARGS_4
288 # define DOARGS_6 DOARGS_5
289
290 #endif  /* __ASSEMBLER__ */
291
292
293 /* Pointer mangling support.  */
294 #if defined NOT_IN_libc && defined IS_IN_rtld
295 /* We cannot use the thread descriptor because in ld.so we use setjmp
296    earlier than the descriptor is initialized.  */
297 # ifdef __ASSEMBLER__
298 #  define PTR_MANGLE(reg)       xorq __pointer_chk_guard_local(%rip), reg;    \
299                                 rolq $17, reg
300 #  define PTR_DEMANGLE(reg)     rorq $17, reg;                                \
301                                 xorq __pointer_chk_guard_local(%rip), reg
302 # else
303 #  define PTR_MANGLE(reg)       __asm__ ("xorq __pointer_chk_guard_local(%%rip), %0\n" \
304                                      "rolq $17, %0"                           \
305                                      : "=r" (reg) : "0" (reg))
306 #  define PTR_DEMANGLE(reg)     __asm__ ("rorq $17, %0\n"                             \
307                                      "xorq __pointer_chk_guard_local(%%rip), %0" \
308                                      : "=r" (reg) : "0" (reg))
309 # endif
310 #else
311 # ifdef __ASSEMBLER__
312 #  define PTR_MANGLE(reg)       xorq %fs:POINTER_GUARD, reg;                  \
313                                 rolq $17, reg
314 #  define PTR_DEMANGLE(reg)     rorq $17, reg;                                \
315                                 xorq %fs:POINTER_GUARD, reg
316 # else
317 #  define PTR_MANGLE(var)       __asm__ ("xorq %%fs:%c2, %0\n"                \
318                                      "rolq $17, %0"                           \
319                                      : "=r" (var)                             \
320                                      : "0" (var),                             \
321                                        "i" (offsetof (tcbhead_t,              \
322                                                       pointer_guard)))
323 #  define PTR_DEMANGLE(var)     __asm__ ("rorq $17, %0\n"                             \
324                                      "xorq %%fs:%c2, %0"                      \
325                                      : "=r" (var)                             \
326                                      : "0" (var),                             \
327                                        "i" (offsetof (tcbhead_t,              \
328                                                       pointer_guard)))
329 # endif
330 #endif
331
332 #endif /* linux/x86_64/sysdep.h */