1/* Copyright (C) 2001-2016 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 <sysdeps/unix/sysv/linux/sysdep.h>
23#include <sysdeps/unix/x86_64/sysdep.h>
24#include <tls.h>
25
26#if IS_IN (rtld)
27# include <dl-sysdep.h> /* Defines RTLD_PRIVATE_ERRNO. */
28#endif
29
30/* For Linux we can use the system call table in the header file
31 /usr/include/asm/unistd.h
32 of the kernel. But these symbols do not follow the SYS_* syntax
33 so we have to redefine the `SYS_ify' macro here. */
34#undef SYS_ify
35#define SYS_ify(syscall_name) __NR_##syscall_name
36
37/* This is a kludge to make syscalls.list find these under the names
38 pread and pwrite, since some kernel headers define those names
39 and some define the *64 names for the same system calls. */
40#if !defined __NR_pread && defined __NR_pread64
41# define __NR_pread __NR_pread64
42#endif
43#if !defined __NR_pwrite && defined __NR_pwrite64
44# define __NR_pwrite __NR_pwrite64
45#endif
46
47/* This is to help the old kernel headers where __NR_semtimedop is not
48 available. */
49#ifndef __NR_semtimedop
50# define __NR_semtimedop 220
51#endif
52
53
54#ifdef __ASSEMBLER__
55
56/* Linux uses a negative return value to indicate syscall errors,
57 unlike most Unices, which use the condition codes' carry flag.
58
59 Since version 2.1 the return value of a system call might be
60 negative even if the call succeeded. E.g., the `lseek' system call
61 might return a large offset. Therefore we must not anymore test
62 for < 0, but test for a real error by making sure the value in %eax
63 is a real error number. Linus said he will make sure the no syscall
64 returns a value in -1 .. -4095 as a valid result so we can savely
65 test with -4095. */
66
67/* We don't want the label for the error handle to be global when we define
68 it here. */
69# ifdef PIC
70# define SYSCALL_ERROR_LABEL 0f
71# else
72# define SYSCALL_ERROR_LABEL syscall_error
73# endif
74
75# undef PSEUDO
76# define PSEUDO(name, syscall_name, args) \
77 .text; \
78 ENTRY (name) \
79 DO_CALL (syscall_name, args); \
80 cmpq $-4095, %rax; \
81 jae SYSCALL_ERROR_LABEL
82
83# undef PSEUDO_END
84# define PSEUDO_END(name) \
85 SYSCALL_ERROR_HANDLER \
86 END (name)
87
88# undef PSEUDO_NOERRNO
89# define PSEUDO_NOERRNO(name, syscall_name, args) \
90 .text; \
91 ENTRY (name) \
92 DO_CALL (syscall_name, args)
93
94# undef PSEUDO_END_NOERRNO
95# define PSEUDO_END_NOERRNO(name) \
96 END (name)
97
98# define ret_NOERRNO ret
99
100# undef PSEUDO_ERRVAL
101# define PSEUDO_ERRVAL(name, syscall_name, args) \
102 .text; \
103 ENTRY (name) \
104 DO_CALL (syscall_name, args); \
105 negq %rax
106
107# undef PSEUDO_END_ERRVAL
108# define PSEUDO_END_ERRVAL(name) \
109 END (name)
110
111# define ret_ERRVAL ret
112
113# if defined PIC && defined RTLD_PRIVATE_ERRNO
114# define SYSCALL_SET_ERRNO \
115 lea rtld_errno(%rip), %RCX_LP; \
116 neg %eax; \
117 movl %eax, (%rcx)
118# else
119# if IS_IN (libc)
120# define SYSCALL_ERROR_ERRNO __libc_errno
121# else
122# define SYSCALL_ERROR_ERRNO errno
123# endif
124# define SYSCALL_SET_ERRNO \
125 movq SYSCALL_ERROR_ERRNO@GOTTPOFF(%rip), %rcx;\
126 neg %eax; \
127 movl %eax, %fs:(%rcx);
128# endif
129
130# ifndef PIC
131# define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
132# else
133# define SYSCALL_ERROR_HANDLER \
1340: \
135 SYSCALL_SET_ERRNO; \
136 or $-1, %RAX_LP; \
137 ret;
138# endif /* PIC */
139
140/* The Linux/x86-64 kernel expects the system call parameters in
141 registers according to the following table:
142
143 syscall number rax
144 arg 1 rdi
145 arg 2 rsi
146 arg 3 rdx
147 arg 4 r10
148 arg 5 r8
149 arg 6 r9
150
151 The Linux kernel uses and destroys internally these registers:
152 return address from
153 syscall rcx
154 eflags from syscall r11
155
156 Normal function call, including calls to the system call stub
157 functions in the libc, get the first six parameters passed in
158 registers and the seventh parameter and later on the stack. The
159 register use is as follows:
160
161 system call number in the DO_CALL macro
162 arg 1 rdi
163 arg 2 rsi
164 arg 3 rdx
165 arg 4 rcx
166 arg 5 r8
167 arg 6 r9
168
169 We have to take care that the stack is aligned to 16 bytes. When
170 called the stack is not aligned since the return address has just
171 been pushed.
172
173
174 Syscalls of more than 6 arguments are not supported. */
175
176# undef DO_CALL
177# define DO_CALL(syscall_name, args) \
178 DOARGS_##args \
179 movl $SYS_ify (syscall_name), %eax; \
180 syscall;
181
182# define DOARGS_0 /* nothing */
183# define DOARGS_1 /* nothing */
184# define DOARGS_2 /* nothing */
185# define DOARGS_3 /* nothing */
186# define DOARGS_4 movq %rcx, %r10;
187# define DOARGS_5 DOARGS_4
188# define DOARGS_6 DOARGS_5
189
190#else /* !__ASSEMBLER__ */
191/* Define a macro which expands inline into the wrapper code for a system
192 call. */
193# undef INLINE_SYSCALL
194# define INLINE_SYSCALL(name, nr, args...) \
195 ({ \
196 unsigned long int resultvar = INTERNAL_SYSCALL (name, , nr, args); \
197 if (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (resultvar, ))) \
198 { \
199 __set_errno (INTERNAL_SYSCALL_ERRNO (resultvar, )); \
200 resultvar = (unsigned long int) -1; \
201 } \
202 (long int) resultvar; })
203
204/* Define a macro with explicit types for arguments, which expands inline
205 into the wrapper code for a system call. It should be used when size
206 of any argument > size of long int. */
207# undef INLINE_SYSCALL_TYPES
208# define INLINE_SYSCALL_TYPES(name, nr, args...) \
209 ({ \
210 unsigned long int resultvar = INTERNAL_SYSCALL_TYPES (name, , nr, args); \
211 if (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (resultvar, ))) \
212 { \
213 __set_errno (INTERNAL_SYSCALL_ERRNO (resultvar, )); \
214 resultvar = (unsigned long int) -1; \
215 } \
216 (long int) resultvar; })
217
218# undef INTERNAL_SYSCALL_DECL
219# define INTERNAL_SYSCALL_DECL(err) do { } while (0)
220
221/* Registers clobbered by syscall. */
222# define REGISTERS_CLOBBERED_BY_SYSCALL "cc", "r11", "cx"
223
224# define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
225 ({ \
226 unsigned long int resultvar; \
227 LOAD_ARGS_##nr (args) \
228 LOAD_REGS_##nr \
229 asm volatile ( \
230 "syscall\n\t" \
231 : "=a" (resultvar) \
232 : "0" (name) ASM_ARGS_##nr : "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
233 (long int) resultvar; })
234# undef INTERNAL_SYSCALL
235# define INTERNAL_SYSCALL(name, err, nr, args...) \
236 INTERNAL_SYSCALL_NCS (__NR_##name, err, nr, ##args)
237
238# define INTERNAL_SYSCALL_NCS_TYPES(name, err, nr, args...) \
239 ({ \
240 unsigned long int resultvar; \
241 LOAD_ARGS_TYPES_##nr (args) \
242 LOAD_REGS_TYPES_##nr (args) \
243 asm volatile ( \
244 "syscall\n\t" \
245 : "=a" (resultvar) \
246 : "0" (name) ASM_ARGS_##nr : "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
247 (long int) resultvar; })
248# undef INTERNAL_SYSCALL_TYPES
249# define INTERNAL_SYSCALL_TYPES(name, err, nr, args...) \
250 INTERNAL_SYSCALL_NCS_TYPES (__NR_##name, err, nr, ##args)
251
252# undef INTERNAL_SYSCALL_ERROR_P
253# define INTERNAL_SYSCALL_ERROR_P(val, err) \
254 ((unsigned long int) (long int) (val) >= -4095L)
255
256# undef INTERNAL_SYSCALL_ERRNO
257# define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
258
259/* List of system calls which are supported as vsyscalls. */
260# define HAVE_CLOCK_GETTIME_VSYSCALL 1
261# define HAVE_GETTIMEOFDAY_VSYSCALL 1
262# define HAVE_GETCPU_VSYSCALL 1
263
264# define LOAD_ARGS_0()
265# define LOAD_REGS_0
266# define ASM_ARGS_0
267
268# define LOAD_ARGS_TYPES_1(t1, a1) \
269 t1 __arg1 = (t1) (a1); \
270 LOAD_ARGS_0 ()
271# define LOAD_REGS_TYPES_1(t1, a1) \
272 register t1 _a1 asm ("rdi") = __arg1; \
273 LOAD_REGS_0
274# define ASM_ARGS_1 ASM_ARGS_0, "r" (_a1)
275# define LOAD_ARGS_1(a1) \
276 LOAD_ARGS_TYPES_1 (long int, a1)
277# define LOAD_REGS_1 \
278 LOAD_REGS_TYPES_1 (long int, a1)
279
280# define LOAD_ARGS_TYPES_2(t1, a1, t2, a2) \
281 t2 __arg2 = (t2) (a2); \
282 LOAD_ARGS_TYPES_1 (t1, a1)
283# define LOAD_REGS_TYPES_2(t1, a1, t2, a2) \
284 register t2 _a2 asm ("rsi") = __arg2; \
285 LOAD_REGS_TYPES_1(t1, a1)
286# define ASM_ARGS_2 ASM_ARGS_1, "r" (_a2)
287# define LOAD_ARGS_2(a1, a2) \
288 LOAD_ARGS_TYPES_2 (long int, a1, long int, a2)
289# define LOAD_REGS_2 \
290 LOAD_REGS_TYPES_2 (long int, a1, long int, a2)
291
292# define LOAD_ARGS_TYPES_3(t1, a1, t2, a2, t3, a3) \
293 t3 __arg3 = (t3) (a3); \
294 LOAD_ARGS_TYPES_2 (t1, a1, t2, a2)
295# define LOAD_REGS_TYPES_3(t1, a1, t2, a2, t3, a3) \
296 register t3 _a3 asm ("rdx") = __arg3; \
297 LOAD_REGS_TYPES_2(t1, a1, t2, a2)
298# define ASM_ARGS_3 ASM_ARGS_2, "r" (_a3)
299# define LOAD_ARGS_3(a1, a2, a3) \
300 LOAD_ARGS_TYPES_3 (long int, a1, long int, a2, long int, a3)
301# define LOAD_REGS_3 \
302 LOAD_REGS_TYPES_3 (long int, a1, long int, a2, long int, a3)
303
304# define LOAD_ARGS_TYPES_4(t1, a1, t2, a2, t3, a3, t4, a4) \
305 t4 __arg4 = (t4) (a4); \
306 LOAD_ARGS_TYPES_3 (t1, a1, t2, a2, t3, a3)
307# define LOAD_REGS_TYPES_4(t1, a1, t2, a2, t3, a3, t4, a4) \
308 register t4 _a4 asm ("r10") = __arg4; \
309 LOAD_REGS_TYPES_3(t1, a2, t2, a2, t3, a3)
310# define ASM_ARGS_4 ASM_ARGS_3, "r" (_a4)
311# define LOAD_ARGS_4(a1, a2, a3, a4) \
312 LOAD_ARGS_TYPES_4 (long int, a1, long int, a2, long int, a3, \
313 long int, a4)
314# define LOAD_REGS_4 \
315 LOAD_REGS_TYPES_4 (long int, a1, long int, a2, long int, a3, \
316 long int, a4)
317
318# define LOAD_ARGS_TYPES_5(t1, a1, t2, a2, t3, a3, t4, a4, t5, a5) \
319 t5 __arg5 = (t5) (a5); \
320 LOAD_ARGS_TYPES_4 (t1, a1, t2, a2, t3, a3, t4, a4)
321# define LOAD_REGS_TYPES_5(t1, a1, t2, a2, t3, a3, t4, a4, t5, a5) \
322 register t5 _a5 asm ("r8") = __arg5; \
323 LOAD_REGS_TYPES_4 (t1, a1, t2, a2, t3, a3, t4, a4)
324# define ASM_ARGS_5 ASM_ARGS_4, "r" (_a5)
325# define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
326 LOAD_ARGS_TYPES_5 (long int, a1, long int, a2, long int, a3, \
327 long int, a4, long int, a5)
328# define LOAD_REGS_5 \
329 LOAD_REGS_TYPES_5 (long int, a1, long int, a2, long int, a3, \
330 long int, a4, long int, a5)
331
332# define LOAD_ARGS_TYPES_6(t1, a1, t2, a2, t3, a3, t4, a4, t5, a5, t6, a6) \
333 t6 __arg6 = (t6) (a6); \
334 LOAD_ARGS_TYPES_5 (t1, a1, t2, a2, t3, a3, t4, a4, t5, a5)
335# define LOAD_REGS_TYPES_6(t1, a1, t2, a2, t3, a3, t4, a4, t5, a5, t6, a6) \
336 register t6 _a6 asm ("r9") = __arg6; \
337 LOAD_REGS_TYPES_5 (t1, a1, t2, a2, t3, a3, t4, a4, t5, a5)
338# define ASM_ARGS_6 ASM_ARGS_5, "r" (_a6)
339# define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
340 LOAD_ARGS_TYPES_6 (long int, a1, long int, a2, long int, a3, \
341 long int, a4, long int, a5, long int, a6)
342# define LOAD_REGS_6 \
343 LOAD_REGS_TYPES_6 (long int, a1, long int, a2, long int, a3, \
344 long int, a4, long int, a5, long int, a6)
345
346#endif /* __ASSEMBLER__ */
347
348
349/* Pointer mangling support. */
350#if IS_IN (rtld)
351/* We cannot use the thread descriptor because in ld.so we use setjmp
352 earlier than the descriptor is initialized. */
353# ifdef __ASSEMBLER__
354# define PTR_MANGLE(reg) xor __pointer_chk_guard_local(%rip), reg; \
355 rol $2*LP_SIZE+1, reg
356# define PTR_DEMANGLE(reg) ror $2*LP_SIZE+1, reg; \
357 xor __pointer_chk_guard_local(%rip), reg
358# else
359# define PTR_MANGLE(reg) asm ("xor __pointer_chk_guard_local(%%rip), %0\n" \
360 "rol $2*" LP_SIZE "+1, %0" \
361 : "=r" (reg) : "0" (reg))
362# define PTR_DEMANGLE(reg) asm ("ror $2*" LP_SIZE "+1, %0\n" \
363 "xor __pointer_chk_guard_local(%%rip), %0" \
364 : "=r" (reg) : "0" (reg))
365# endif
366#else
367# ifdef __ASSEMBLER__
368# define PTR_MANGLE(reg) xor %fs:POINTER_GUARD, reg; \
369 rol $2*LP_SIZE+1, reg
370# define PTR_DEMANGLE(reg) ror $2*LP_SIZE+1, reg; \
371 xor %fs:POINTER_GUARD, reg
372# else
373# define PTR_MANGLE(var) asm ("xor %%fs:%c2, %0\n" \
374 "rol $2*" LP_SIZE "+1, %0" \
375 : "=r" (var) \
376 : "0" (var), \
377 "i" (offsetof (tcbhead_t, \
378 pointer_guard)))
379# define PTR_DEMANGLE(var) asm ("ror $2*" LP_SIZE "+1, %0\n" \
380 "xor %%fs:%c2, %0" \
381 : "=r" (var) \
382 : "0" (var), \
383 "i" (offsetof (tcbhead_t, \
384 pointer_guard)))
385# endif
386#endif
387
388#endif /* linux/x86_64/sysdep.h */
389