1 /* Copyright (C) 2001-2005, 2007 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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/>. */
18 #ifndef _LINUX_X86_64_SYSDEP_H
19 #define _LINUX_X86_64_SYSDEP_H 1
21 /* There is some commonality. */
22 #include <sys/syscall.h>
23 #include <common/sysdep.h>
27 /* Syntactic details of assembler. */
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;
35 /* In ELF C symbols are asm symbols. */
37 #define NO_UNDERSCORES
39 /* Define an entry point visible from C. */
41 ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME(name); \
42 ASM_TYPE_DIRECTIVE (C_SYMBOL_NAME(name),@function) \
51 ASM_SIZE_DIRECTIVE(name)
53 /* If compiled for profiling, call `mcount' at the start of each function. */
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. */
59 cfi_adjust_cfa_offset(8); \
61 cfi_def_cfa_register(%rbp); \
62 call JUMPTARGET(mcount); \
66 #define CALL_MCOUNT /* Do nothing. */
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
77 #define PSEUDO(name, syscall_name, args) \
79 jmp JUMPTARGET(syscall_error) \
80 .globl syscall_error; \
82 DO_CALL (syscall_name, args); \
86 #define PSEUDO_END(name) \
91 #define JUMPTARGET(name) name##@PLT
93 #define JUMPTARGET(name) name
96 /* Local label name for asm code. */
98 #define L(name) .L##name
101 #endif /* __ASSEMBLER__ */
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. */
108 #define SYS_ify(syscall_name) __NR_##syscall_name
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
116 #if !defined __NR_pwrite && defined __NR_pwrite64
117 # define __NR_pwrite __NR_pwrite64
120 /* This is to help the old kernel headers where __NR_semtimedop is not
122 #ifndef __NR_semtimedop
123 # define __NR_semtimedop 220
129 /* Linux uses a negative return value to indicate syscall errors,
130 unlike most Unices, which use the condition codes' carry flag.
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
140 /* We don't want the label for the error handle to be global when we define
143 # define SYSCALL_ERROR_LABEL 0f
145 # define SYSCALL_ERROR_LABEL syscall_error
149 # define PSEUDO(name, syscall_name, args) \
152 DO_CALL (syscall_name, args); \
154 jae SYSCALL_ERROR_LABEL; \
158 # define PSEUDO_END(name) \
159 SYSCALL_ERROR_HANDLER \
162 # undef PSEUDO_NOERRNO
163 # define PSEUDO_NOERRNO(name, syscall_name, args) \
166 DO_CALL (syscall_name, args)
168 # undef PSEUDO_END_NOERRNO
169 # define PSEUDO_END_NOERRNO(name) \
172 # define ret_NOERRNO ret
174 # undef PSEUDO_ERRVAL
175 # define PSEUDO_ERRVAL(name, syscall_name, args) \
178 DO_CALL (syscall_name, args); \
181 # undef PSEUDO_END_ERRVAL
182 # define PSEUDO_END_ERRVAL(name) \
185 # define ret_ERRVAL ret
188 # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
189 # elif defined(RTLD_PRIVATE_ERRNO)
190 # define SYSCALL_ERROR_HANDLER \
192 leaq rtld_errno(%rip), %rcx; \
200 # define SYSCALL_ERROR_ERRNO __libc_errno
202 # define SYSCALL_ERROR_ERRNO errno
204 # define SYSCALL_ERROR_HANDLER \
206 movq SYSCALL_ERROR_ERRNO@GOTTPOFF(%rip), %rcx;\
209 movl %edx, %fs:(%rcx); \
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 \
220 cfi_adjust_cfa_offset(8); \
221 call __errno_location@PLT; \
223 cfi_adjust_cfa_offset(-8); \
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; \
238 # endif /* __PIC__ */
240 /* The Linux/x86-64 kernel expects the system call parameters in
241 registers according to the following table:
251 The Linux kernel uses and destroys internally these registers:
254 eflags from syscall r11
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:
261 system call number in the DO_CALL macro
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
274 Syscalls of more than 6 arguments are not supported. */
277 # define DO_CALL(syscall_name, args) \
279 movl $SYS_ify (syscall_name), %eax; \
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
290 #endif /* __ASSEMBLER__ */
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; \
300 # define PTR_DEMANGLE(reg) rorq $17, reg; \
301 xorq __pointer_chk_guard_local(%rip), reg
303 # define PTR_MANGLE(reg) __asm__ ("xorq __pointer_chk_guard_local(%%rip), %0\n" \
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))
311 # ifdef __ASSEMBLER__
312 # define PTR_MANGLE(reg) xorq %fs:POINTER_GUARD, reg; \
314 # define PTR_DEMANGLE(reg) rorq $17, reg; \
315 xorq %fs:POINTER_GUARD, reg
317 # define PTR_MANGLE(var) __asm__ ("xorq %%fs:%c2, %0\n" \
321 "i" (offsetof (tcbhead_t, \
323 # define PTR_DEMANGLE(var) __asm__ ("rorq $17, %0\n" \
324 "xorq %%fs:%c2, %0" \
327 "i" (offsetof (tcbhead_t, \
332 #endif /* linux/x86_64/sysdep.h */