1/* Copyright (C) 2001-2018 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/* Create a variable 'name' based on type 'X' to avoid explicit types.
225 This is mainly used set use 64-bits arguments in x32. */
226#define TYPEFY(X, name) __typeof__ ((X) - (X)) name
227/* Explicit cast the argument to avoid integer from pointer warning on
228 x32. */
229#define ARGIFY(X) ((__typeof__ ((X) - (X))) (X))
230
231#undef INTERNAL_SYSCALL
232#define INTERNAL_SYSCALL(name, err, nr, args...) \
233 internal_syscall##nr (SYS_ify (name), err, args)
234
235#undef INTERNAL_SYSCALL_NCS
236#define INTERNAL_SYSCALL_NCS(number, err, nr, args...) \
237 internal_syscall##nr (number, err, args)
238
239#undef internal_syscall0
240#define internal_syscall0(number, err, dummy...) \
241({ \
242 unsigned long int resultvar; \
243 asm volatile ( \
244 "syscall\n\t" \
245 : "=a" (resultvar) \
246 : "0" (number) \
247 : "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
248 (long int) resultvar; \
249})
250
251#undef internal_syscall1
252#define internal_syscall1(number, err, arg1) \
253({ \
254 unsigned long int resultvar; \
255 TYPEFY (arg1, __arg1) = ARGIFY (arg1); \
256 register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \
257 asm volatile ( \
258 "syscall\n\t" \
259 : "=a" (resultvar) \
260 : "0" (number), "r" (_a1) \
261 : "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
262 (long int) resultvar; \
263})
264
265#undef internal_syscall2
266#define internal_syscall2(number, err, arg1, arg2) \
267({ \
268 unsigned long int resultvar; \
269 TYPEFY (arg2, __arg2) = ARGIFY (arg2); \
270 TYPEFY (arg1, __arg1) = ARGIFY (arg1); \
271 register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \
272 register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \
273 asm volatile ( \
274 "syscall\n\t" \
275 : "=a" (resultvar) \
276 : "0" (number), "r" (_a1), "r" (_a2) \
277 : "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
278 (long int) resultvar; \
279})
280
281#undef internal_syscall3
282#define internal_syscall3(number, err, arg1, arg2, arg3) \
283({ \
284 unsigned long int resultvar; \
285 TYPEFY (arg3, __arg3) = ARGIFY (arg3); \
286 TYPEFY (arg2, __arg2) = ARGIFY (arg2); \
287 TYPEFY (arg1, __arg1) = ARGIFY (arg1); \
288 register TYPEFY (arg3, _a3) asm ("rdx") = __arg3; \
289 register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \
290 register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \
291 asm volatile ( \
292 "syscall\n\t" \
293 : "=a" (resultvar) \
294 : "0" (number), "r" (_a1), "r" (_a2), "r" (_a3) \
295 : "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
296 (long int) resultvar; \
297})
298
299#undef internal_syscall4
300#define internal_syscall4(number, err, arg1, arg2, arg3, arg4) \
301({ \
302 unsigned long int resultvar; \
303 TYPEFY (arg4, __arg4) = ARGIFY (arg4); \
304 TYPEFY (arg3, __arg3) = ARGIFY (arg3); \
305 TYPEFY (arg2, __arg2) = ARGIFY (arg2); \
306 TYPEFY (arg1, __arg1) = ARGIFY (arg1); \
307 register TYPEFY (arg4, _a4) asm ("r10") = __arg4; \
308 register TYPEFY (arg3, _a3) asm ("rdx") = __arg3; \
309 register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \
310 register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \
311 asm volatile ( \
312 "syscall\n\t" \
313 : "=a" (resultvar) \
314 : "0" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4) \
315 : "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
316 (long int) resultvar; \
317})
318
319#undef internal_syscall5
320#define internal_syscall5(number, err, arg1, arg2, arg3, arg4, arg5) \
321({ \
322 unsigned long int resultvar; \
323 TYPEFY (arg5, __arg5) = ARGIFY (arg5); \
324 TYPEFY (arg4, __arg4) = ARGIFY (arg4); \
325 TYPEFY (arg3, __arg3) = ARGIFY (arg3); \
326 TYPEFY (arg2, __arg2) = ARGIFY (arg2); \
327 TYPEFY (arg1, __arg1) = ARGIFY (arg1); \
328 register TYPEFY (arg5, _a5) asm ("r8") = __arg5; \
329 register TYPEFY (arg4, _a4) asm ("r10") = __arg4; \
330 register TYPEFY (arg3, _a3) asm ("rdx") = __arg3; \
331 register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \
332 register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \
333 asm volatile ( \
334 "syscall\n\t" \
335 : "=a" (resultvar) \
336 : "0" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4), \
337 "r" (_a5) \
338 : "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
339 (long int) resultvar; \
340})
341
342#undef internal_syscall6
343#define internal_syscall6(number, err, arg1, arg2, arg3, arg4, arg5, arg6) \
344({ \
345 unsigned long int resultvar; \
346 TYPEFY (arg6, __arg6) = ARGIFY (arg6); \
347 TYPEFY (arg5, __arg5) = ARGIFY (arg5); \
348 TYPEFY (arg4, __arg4) = ARGIFY (arg4); \
349 TYPEFY (arg3, __arg3) = ARGIFY (arg3); \
350 TYPEFY (arg2, __arg2) = ARGIFY (arg2); \
351 TYPEFY (arg1, __arg1) = ARGIFY (arg1); \
352 register TYPEFY (arg6, _a6) asm ("r9") = __arg6; \
353 register TYPEFY (arg5, _a5) asm ("r8") = __arg5; \
354 register TYPEFY (arg4, _a4) asm ("r10") = __arg4; \
355 register TYPEFY (arg3, _a3) asm ("rdx") = __arg3; \
356 register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \
357 register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \
358 asm volatile ( \
359 "syscall\n\t" \
360 : "=a" (resultvar) \
361 : "0" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4), \
362 "r" (_a5), "r" (_a6) \
363 : "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
364 (long int) resultvar; \
365})
366
367# undef INTERNAL_SYSCALL_ERROR_P
368# define INTERNAL_SYSCALL_ERROR_P(val, err) \
369 ((unsigned long int) (long int) (val) >= -4095L)
370
371# undef INTERNAL_SYSCALL_ERRNO
372# define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
373
374/* List of system calls which are supported as vsyscalls. */
375# define HAVE_CLOCK_GETTIME_VSYSCALL 1
376# define HAVE_GETTIMEOFDAY_VSYSCALL 1
377# define HAVE_GETCPU_VSYSCALL 1
378
379# define SINGLE_THREAD_BY_GLOBAL 1
380
381#endif /* __ASSEMBLER__ */
382
383
384/* Pointer mangling support. */
385#if IS_IN (rtld)
386/* We cannot use the thread descriptor because in ld.so we use setjmp
387 earlier than the descriptor is initialized. */
388# ifdef __ASSEMBLER__
389# define PTR_MANGLE(reg) xor __pointer_chk_guard_local(%rip), reg; \
390 rol $2*LP_SIZE+1, reg
391# define PTR_DEMANGLE(reg) ror $2*LP_SIZE+1, reg; \
392 xor __pointer_chk_guard_local(%rip), reg
393# else
394# define PTR_MANGLE(reg) asm ("xor __pointer_chk_guard_local(%%rip), %0\n" \
395 "rol $2*" LP_SIZE "+1, %0" \
396 : "=r" (reg) : "0" (reg))
397# define PTR_DEMANGLE(reg) asm ("ror $2*" LP_SIZE "+1, %0\n" \
398 "xor __pointer_chk_guard_local(%%rip), %0" \
399 : "=r" (reg) : "0" (reg))
400# endif
401#else
402# ifdef __ASSEMBLER__
403# define PTR_MANGLE(reg) xor %fs:POINTER_GUARD, reg; \
404 rol $2*LP_SIZE+1, reg
405# define PTR_DEMANGLE(reg) ror $2*LP_SIZE+1, reg; \
406 xor %fs:POINTER_GUARD, reg
407# else
408# define PTR_MANGLE(var) asm ("xor %%fs:%c2, %0\n" \
409 "rol $2*" LP_SIZE "+1, %0" \
410 : "=r" (var) \
411 : "0" (var), \
412 "i" (offsetof (tcbhead_t, \
413 pointer_guard)))
414# define PTR_DEMANGLE(var) asm ("ror $2*" LP_SIZE "+1, %0\n" \
415 "xor %%fs:%c2, %0" \
416 : "=r" (var) \
417 : "0" (var), \
418 "i" (offsetof (tcbhead_t, \
419 pointer_guard)))
420# endif
421#endif
422
423/* How to pass the off{64}_t argument on p{readv,writev}{64}. */
424#undef LO_HI_LONG
425#define LO_HI_LONG(val) (val), 0
426
427#endif /* linux/x86_64/sysdep.h */
428