2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / x86_64 / sysdep.h
blob2b9ea85d8e9c91c4d8d414a98cc40b82d261153c
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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #ifndef _LINUX_X86_64_SYSDEP_H
20 #define _LINUX_X86_64_SYSDEP_H 1
22 /* There is some commonality. */
23 #include <sysdeps/unix/x86_64/sysdep.h>
24 #include <bp-sym.h>
25 #include <bp-asm.h>
26 #include <tls.h>
28 #ifdef IS_IN_rtld
29 # include <dl-sysdep.h> /* Defines RTLD_PRIVATE_ERRNO. */
30 #endif
32 /* For Linux we can use the system call table in the header file
33 /usr/include/asm/unistd.h
34 of the kernel. But these symbols do not follow the SYS_* syntax
35 so we have to redefine the `SYS_ify' macro here. */
36 #undef SYS_ify
37 #define SYS_ify(syscall_name) __NR_##syscall_name
39 /* This is a kludge to make syscalls.list find these under the names
40 pread and pwrite, since some kernel headers define those names
41 and some define the *64 names for the same system calls. */
42 #if !defined __NR_pread && defined __NR_pread64
43 # define __NR_pread __NR_pread64
44 #endif
45 #if !defined __NR_pwrite && defined __NR_pwrite64
46 # define __NR_pwrite __NR_pwrite64
47 #endif
49 /* This is to help the old kernel headers where __NR_semtimedop is not
50 available. */
51 #ifndef __NR_semtimedop
52 # define __NR_semtimedop 220
53 #endif
56 #ifdef __ASSEMBLER__
58 /* Linux uses a negative return value to indicate syscall errors,
59 unlike most Unices, which use the condition codes' carry flag.
61 Since version 2.1 the return value of a system call might be
62 negative even if the call succeeded. E.g., the `lseek' system call
63 might return a large offset. Therefore we must not anymore test
64 for < 0, but test for a real error by making sure the value in %eax
65 is a real error number. Linus said he will make sure the no syscall
66 returns a value in -1 .. -4095 as a valid result so we can savely
67 test with -4095. */
69 /* We don't want the label for the error handle to be global when we define
70 it here. */
71 # ifdef PIC
72 # define SYSCALL_ERROR_LABEL 0f
73 # else
74 # define SYSCALL_ERROR_LABEL syscall_error
75 # endif
77 # undef PSEUDO
78 # define PSEUDO(name, syscall_name, args) \
79 .text; \
80 ENTRY (name) \
81 DO_CALL (syscall_name, args); \
82 cmpq $-4095, %rax; \
83 jae SYSCALL_ERROR_LABEL; \
84 L(pseudo_end):
86 # undef PSEUDO_END
87 # define PSEUDO_END(name) \
88 SYSCALL_ERROR_HANDLER \
89 END (name)
91 # undef PSEUDO_NOERRNO
92 # define PSEUDO_NOERRNO(name, syscall_name, args) \
93 .text; \
94 ENTRY (name) \
95 DO_CALL (syscall_name, args)
97 # undef PSEUDO_END_NOERRNO
98 # define PSEUDO_END_NOERRNO(name) \
99 END (name)
101 # define ret_NOERRNO ret
103 # undef PSEUDO_ERRVAL
104 # define PSEUDO_ERRVAL(name, syscall_name, args) \
105 .text; \
106 ENTRY (name) \
107 DO_CALL (syscall_name, args); \
108 negq %rax
110 # undef PSEUDO_END_ERRVAL
111 # define PSEUDO_END_ERRVAL(name) \
112 END (name)
114 # define ret_ERRVAL ret
116 # ifndef PIC
117 # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
118 # elif RTLD_PRIVATE_ERRNO
119 # define SYSCALL_ERROR_HANDLER \
120 0: \
121 leaq rtld_errno(%rip), %rcx; \
122 xorl %edx, %edx; \
123 subq %rax, %rdx; \
124 movl %edx, (%rcx); \
125 orq $-1, %rax; \
126 jmp L(pseudo_end);
127 # elif USE___THREAD
128 # ifndef NOT_IN_libc
129 # define SYSCALL_ERROR_ERRNO __libc_errno
130 # else
131 # define SYSCALL_ERROR_ERRNO errno
132 # endif
133 # define SYSCALL_ERROR_HANDLER \
134 0: \
135 movq SYSCALL_ERROR_ERRNO@GOTTPOFF(%rip), %rcx;\
136 xorl %edx, %edx; \
137 subq %rax, %rdx; \
138 movl %edx, %fs:(%rcx); \
139 orq $-1, %rax; \
140 jmp L(pseudo_end);
141 # elif defined _LIBC_REENTRANT
142 /* Store (- %rax) into errno through the GOT.
143 Note that errno occupies only 4 bytes. */
144 # define SYSCALL_ERROR_HANDLER \
145 0: \
146 xorl %edx, %edx; \
147 subq %rax, %rdx; \
148 pushq %rdx; \
149 cfi_adjust_cfa_offset(8); \
150 PUSH_ERRNO_LOCATION_RETURN; \
151 call BP_SYM (__errno_location)@PLT; \
152 POP_ERRNO_LOCATION_RETURN; \
153 popq %rdx; \
154 cfi_adjust_cfa_offset(-8); \
155 movl %edx, (%rax); \
156 orq $-1, %rax; \
157 jmp L(pseudo_end);
159 /* A quick note: it is assumed that the call to `__errno_location' does
160 not modify the stack! */
161 # else /* Not _LIBC_REENTRANT. */
162 # define SYSCALL_ERROR_HANDLER \
163 0:movq errno@GOTPCREL(%RIP), %rcx; \
164 xorl %edx, %edx; \
165 subq %rax, %rdx; \
166 movl %edx, (%rcx); \
167 orq $-1, %rax; \
168 jmp L(pseudo_end);
169 # endif /* PIC */
171 /* The Linux/x86-64 kernel expects the system call parameters in
172 registers according to the following table:
174 syscall number rax
175 arg 1 rdi
176 arg 2 rsi
177 arg 3 rdx
178 arg 4 r10
179 arg 5 r8
180 arg 6 r9
182 The Linux kernel uses and destroys internally these registers:
183 return address from
184 syscall rcx
185 eflags from syscall r11
187 Normal function call, including calls to the system call stub
188 functions in the libc, get the first six parameters passed in
189 registers and the seventh parameter and later on the stack. The
190 register use is as follows:
192 system call number in the DO_CALL macro
193 arg 1 rdi
194 arg 2 rsi
195 arg 3 rdx
196 arg 4 rcx
197 arg 5 r8
198 arg 6 r9
200 We have to take care that the stack is aligned to 16 bytes. When
201 called the stack is not aligned since the return address has just
202 been pushed.
205 Syscalls of more than 6 arguments are not supported. */
207 # undef DO_CALL
208 # define DO_CALL(syscall_name, args) \
209 DOARGS_##args \
210 movl $SYS_ify (syscall_name), %eax; \
211 syscall;
213 # define DOARGS_0 /* nothing */
214 # define DOARGS_1 /* nothing */
215 # define DOARGS_2 /* nothing */
216 # define DOARGS_3 /* nothing */
217 # define DOARGS_4 movq %rcx, %r10;
218 # define DOARGS_5 DOARGS_4
219 # define DOARGS_6 DOARGS_5
221 #else /* !__ASSEMBLER__ */
222 /* Define a macro which expands inline into the wrapper code for a system
223 call. */
224 # undef INLINE_SYSCALL
225 # define INLINE_SYSCALL(name, nr, args...) \
226 ({ \
227 unsigned long int resultvar = INTERNAL_SYSCALL (name, , nr, args); \
228 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (resultvar, ), 0)) \
230 __set_errno (INTERNAL_SYSCALL_ERRNO (resultvar, )); \
231 resultvar = (unsigned long int) -1; \
233 (long int) resultvar; })
235 # undef INTERNAL_SYSCALL_DECL
236 # define INTERNAL_SYSCALL_DECL(err) do { } while (0)
238 # define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
239 ({ \
240 unsigned long int resultvar; \
241 LOAD_ARGS_##nr (args) \
242 LOAD_REGS_##nr \
243 asm volatile ( \
244 "syscall\n\t" \
245 : "=a" (resultvar) \
246 : "0" (name) ASM_ARGS_##nr : "memory", "cc", "r11", "cx"); \
247 (long int) resultvar; })
248 # undef INTERNAL_SYSCALL
249 # define INTERNAL_SYSCALL(name, err, nr, args...) \
250 INTERNAL_SYSCALL_NCS (__NR_##name, err, nr, ##args)
252 # undef INTERNAL_SYSCALL_ERROR_P
253 # define INTERNAL_SYSCALL_ERROR_P(val, err) \
254 ((unsigned long int) (long int) (val) >= -4095L)
256 # undef INTERNAL_SYSCALL_ERRNO
257 # define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
259 # ifdef SHARED
260 # define INLINE_VSYSCALL(name, nr, args...) \
261 ({ \
262 __label__ out; \
263 __label__ iserr; \
264 INTERNAL_SYSCALL_DECL (sc_err); \
265 long int sc_ret; \
267 __typeof (__vdso_##name) vdsop = __vdso_##name; \
268 PTR_DEMANGLE (vdsop); \
269 if (vdsop != NULL) \
271 sc_ret = vdsop (args); \
272 if (!INTERNAL_SYSCALL_ERROR_P (sc_ret, sc_err)) \
273 goto out; \
274 if (INTERNAL_SYSCALL_ERRNO (sc_ret, sc_err) != ENOSYS) \
275 goto iserr; \
278 sc_ret = INTERNAL_SYSCALL (name, sc_err, nr, ##args); \
279 if (INTERNAL_SYSCALL_ERROR_P (sc_ret, sc_err)) \
281 iserr: \
282 __set_errno (INTERNAL_SYSCALL_ERRNO (sc_ret, sc_err)); \
283 sc_ret = -1L; \
285 out: \
286 sc_ret; \
288 # define INTERNAL_VSYSCALL(name, err, nr, args...) \
289 ({ \
290 __label__ out; \
291 long int v_ret; \
293 __typeof (__vdso_##name) vdsop = __vdso_##name; \
294 PTR_DEMANGLE (vdsop); \
295 if (vdsop != NULL) \
297 v_ret = vdsop (args); \
298 if (!INTERNAL_SYSCALL_ERROR_P (v_ret, err) \
299 || INTERNAL_SYSCALL_ERRNO (v_ret, err) != ENOSYS) \
300 goto out; \
302 v_ret = INTERNAL_SYSCALL (name, err, nr, ##args); \
303 out: \
304 v_ret; \
307 /* List of system calls which are supported as vsyscalls. */
308 # define HAVE_CLOCK_GETTIME_VSYSCALL 1
310 # else
311 # define INLINE_VSYSCALL(name, nr, args...) \
312 INLINE_SYSCALL (name, nr, ##args)
313 # define INTERNAL_VSYSCALL(name, err, nr, args...) \
314 INTERNAL_SYSCALL (name, err, nr, ##args)
315 # endif
317 # define LOAD_ARGS_0()
318 # define LOAD_REGS_0
319 # define ASM_ARGS_0
321 # define LOAD_ARGS_1(a1) \
322 long int __arg1 = (long int) (a1); \
323 LOAD_ARGS_0 ()
324 # define LOAD_REGS_1 \
325 register long int _a1 asm ("rdi") = __arg1; \
326 LOAD_REGS_0
327 # define ASM_ARGS_1 ASM_ARGS_0, "r" (_a1)
329 # define LOAD_ARGS_2(a1, a2) \
330 long int __arg2 = (long int) (a2); \
331 LOAD_ARGS_1 (a1)
332 # define LOAD_REGS_2 \
333 register long int _a2 asm ("rsi") = __arg2; \
334 LOAD_REGS_1
335 # define ASM_ARGS_2 ASM_ARGS_1, "r" (_a2)
337 # define LOAD_ARGS_3(a1, a2, a3) \
338 long int __arg3 = (long int) (a3); \
339 LOAD_ARGS_2 (a1, a2)
340 # define LOAD_REGS_3 \
341 register long int _a3 asm ("rdx") = __arg3; \
342 LOAD_REGS_2
343 # define ASM_ARGS_3 ASM_ARGS_2, "r" (_a3)
345 # define LOAD_ARGS_4(a1, a2, a3, a4) \
346 long int __arg4 = (long int) (a4); \
347 LOAD_ARGS_3 (a1, a2, a3)
348 # define LOAD_REGS_4 \
349 register long int _a4 asm ("r10") = __arg4; \
350 LOAD_REGS_3
351 # define ASM_ARGS_4 ASM_ARGS_3, "r" (_a4)
353 # define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
354 long int __arg5 = (long int) (a5); \
355 LOAD_ARGS_4 (a1, a2, a3, a4)
356 # define LOAD_REGS_5 \
357 register long int _a5 asm ("r8") = __arg5; \
358 LOAD_REGS_4
359 # define ASM_ARGS_5 ASM_ARGS_4, "r" (_a5)
361 # define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
362 long int __arg6 = (long int) (a6); \
363 LOAD_ARGS_5 (a1, a2, a3, a4, a5)
364 # define LOAD_REGS_6 \
365 register long int _a6 asm ("r9") = __arg6; \
366 LOAD_REGS_5
367 # define ASM_ARGS_6 ASM_ARGS_5, "r" (_a6)
369 #endif /* __ASSEMBLER__ */
372 /* Pointer mangling support. */
373 #if defined NOT_IN_libc && defined IS_IN_rtld
374 /* We cannot use the thread descriptor because in ld.so we use setjmp
375 earlier than the descriptor is initialized. */
376 # ifdef __ASSEMBLER__
377 # define PTR_MANGLE(reg) xorq __pointer_chk_guard_local(%rip), reg; \
378 rolq $17, reg
379 # define PTR_DEMANGLE(reg) rorq $17, reg; \
380 xorq __pointer_chk_guard_local(%rip), reg
381 # else
382 # define PTR_MANGLE(reg) asm ("xorq __pointer_chk_guard_local(%%rip), %0\n" \
383 "rolq $17, %0" \
384 : "=r" (reg) : "0" (reg))
385 # define PTR_DEMANGLE(reg) asm ("rorq $17, %0\n" \
386 "xorq __pointer_chk_guard_local(%%rip), %0" \
387 : "=r" (reg) : "0" (reg))
388 # endif
389 #else
390 # ifdef __ASSEMBLER__
391 # define PTR_MANGLE(reg) xorq %fs:POINTER_GUARD, reg; \
392 rolq $17, reg
393 # define PTR_DEMANGLE(reg) rorq $17, reg; \
394 xorq %fs:POINTER_GUARD, reg
395 # else
396 # define PTR_MANGLE(var) asm ("xorq %%fs:%c2, %0\n" \
397 "rolq $17, %0" \
398 : "=r" (var) \
399 : "0" (var), \
400 "i" (offsetof (tcbhead_t, \
401 pointer_guard)))
402 # define PTR_DEMANGLE(var) asm ("rorq $17, %0\n" \
403 "xorq %%fs:%c2, %0" \
404 : "=r" (var) \
405 : "0" (var), \
406 "i" (offsetof (tcbhead_t, \
407 pointer_guard)))
408 # endif
409 #endif
411 #endif /* linux/x86_64/sysdep.h */