/* [<][>][^][v][top][bottom][index][help] */
1 /* Copyright (C) 2001, 2002, 2003, 2004, 2005 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
18
19 #ifndef _LINUX_X86_64_SYSDEP_H
20 #define _LINUX_X86_64_SYSDEP_H 1
21
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>
27
28 #ifdef IS_IN_rtld
29 # include <dl-sysdep.h> /* Defines RTLD_PRIVATE_ERRNO. */
30 #endif
31
32 /* ADDED TO ORIGINAL:
33 * For Linux we can use the system call table in the header file
34 * /usr/include/asm/unistd.h
35 * of the kernel. But these symbols do not follow the SYS_* syntax
36 * so we have to redefine the `SYS_ify' macro here.
37 */
38 #undef SYS_ify
39 #define SYS_ify(syscall_name) __NR_##syscall_name
40
41 /* This is a kludge to make syscalls.list find these under the names
42 pread and pwrite, since some kernel headers define those names
43 and some define the *64 names for the same system calls. */
44 #if !defined __NR_pread && defined __NR_pread64
45 # define __NR_pread __NR_pread64
46 #endif
47 #if !defined __NR_pwrite && defined __NR_pwrite64
48 # define __NR_pwrite __NR_pwrite64
49 #endif
50
51 /* This is to help the old kernel headers where __NR_semtimedop is not
52 available. */
53 #ifndef __NR_semtimedop
54 # define __NR_semtimedop 220
55 #endif
56
57
58 #ifdef __ASSEMBLER__
59
60 /* Linux uses a negative return value to indicate syscall errors,
61 unlike most Unices, which use the condition codes' carry flag.
62
63 Since version 2.1 the return value of a system call might be
64 negative even if the call succeeded. E.g., the `lseek' system call
65 might return a large offset. Therefore we must not anymore test
66 for < 0, but test for a real error by making sure the value in %eax
67 is a real error number. Linus said he will make sure the no syscall
68 returns a value in -1 .. -4095 as a valid result so we can safely
69 test with -4095. */
70
71 /* We don't want the label for the error handle to be global when we define
72 it here. */
73 #ifdef PIC
74 # define SYSCALL_ERROR_LABEL 0f
75 #else
76 # define SYSCALL_ERROR_LABEL syscall_error
77 #endif
78
79 #undef PSEUDO
80 #define PSEUDO(name, syscall_name, args) \
81 .text; \
82 ENTRY (name) \
83 DO_CALL (syscall_name, args); \
84 cmpq $-4095, %rax; \
85 jae SYSCALL_ERROR_LABEL; \
86 L(pseudo_end):
87
88 #undef PSEUDO_END
89 #define PSEUDO_END(name) \
90 SYSCALL_ERROR_HANDLER \
91 END (name)
92
93 #undef PSEUDO_NOERRNO
94 #define PSEUDO_NOERRNO(name, syscall_name, args) \
95 .text; \
96 ENTRY (name) \
97 DO_CALL (syscall_name, args)
98
99 #undef PSEUDO_END_NOERRNO
100 #define PSEUDO_END_NOERRNO(name) \
101 END (name)
102
103 #define ret_NOERRNO ret
104
105 #undef PSEUDO_ERRVAL
106 #define PSEUDO_ERRVAL(name, syscall_name, args) \
107 .text; \
108 ENTRY (name) \
109 DO_CALL (syscall_name, args); \
110 negq %rax
111
112 #undef PSEUDO_END_ERRVAL
113 #define PSEUDO_END_ERRVAL(name) \
114 END (name)
115
116 #define ret_ERRVAL ret
117
118 #ifndef PIC
119 #define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
120 #elif RTLD_PRIVATE_ERRNO
121 # define SYSCALL_ERROR_HANDLER \
122 0: \
123 leaq rtld_errno(%rip), %rcx; \
124 xorl %edx, %edx; \
125 subq %rax, %rdx; \
126 movl %edx, (%rcx); \
127 orq $-1, %rax; \
128 jmp L(pseudo_end);
129 #elif USE___THREAD
130 # ifndef NOT_IN_libc
131 # define SYSCALL_ERROR_ERRNO __libc_errno
132 # else
133 # define SYSCALL_ERROR_ERRNO errno
134 # endif
135 # define SYSCALL_ERROR_HANDLER \
136 0: \
137 movq SYSCALL_ERROR_ERRNO@GOTTPOFF(%rip), %rcx;\
138 xorl %edx, %edx; \
139 subq %rax, %rdx; \
140 movl %edx, %fs:(%rcx); \
141 orq $-1, %rax; \
142 jmp L(pseudo_end);
143 #elif defined _LIBC_REENTRANT
144 /* Store (- %rax) into errno through the GOT.
145 Note that errno occupies only 4 bytes. */
146 # define SYSCALL_ERROR_HANDLER \
147 0: \
148 xorl %edx, %edx; \
149 subq %rax, %rdx; \
150 pushq %rdx; \
151 cfi_adjust_cfa_offset(8); \
152 PUSH_ERRNO_LOCATION_RETURN; \
153 call BP_SYM (__errno_location)@PLT; \
154 POP_ERRNO_LOCATION_RETURN; \
155 popq %rdx; \
156 cfi_adjust_cfa_offset(-8); \
157 movl %edx, (%rax); \
158 orq $-1, %rax; \
159 jmp L(pseudo_end);
160
161 /* A quick note: it is assumed that the call to `__errno_location' does
162 not modify the stack! */
163 #else /* Not _LIBC_REENTRANT. */
164 # define SYSCALL_ERROR_HANDLER \
165 0:movq errno@GOTPCREL(%RIP), %rcx; \
166 xorl %edx, %edx; \
167 subq %rax, %rdx; \
168 movl %edx, (%rcx); \
169 orq $-1, %rax; \
170 jmp L(pseudo_end);
171 #endif /* PIC */
172
173 /* The Linux/x86-64 kernel expects the system call parameters in
174 registers according to the following table:
175
176 syscall number rax
177 arg 1 rdi
178 arg 2 rsi
179 arg 3 rdx
180 arg 4 r10
181 arg 5 r8
182 arg 6 r9
183
184 The Linux kernel uses and destroys internally these registers:
185 return address from
186 syscall rcx
187 additionally clobbered: r12-r15,rbx,rbp
188 eflags from syscall r11
189
190 Normal function call, including calls to the system call stub
191 functions in the libc, get the first six parameters passed in
192 registers and the seventh parameter and later on the stack. The
193 register use is as follows:
194
195 system call number in the DO_CALL macro
196 arg 1 rdi
197 arg 2 rsi
198 arg 3 rdx
199 arg 4 rcx
200 arg 5 r8
201 arg 6 r9
202
203 We have to take care that the stack is aligned to 16 bytes. When
204 called the stack is not aligned since the return address has just
205 been pushed.
206
207
208 Syscalls of more than 6 arguments are not supported. */
209
210 #undef DO_CALL
211 #define DO_CALL(syscall_name, args) \
212 DOARGS_##args \
213 movl $SYS_ify (syscall_name), %eax; \
214 syscall;
215
216 #define DOARGS_0 /* nothing */
217 #define DOARGS_1 /* nothing */
218 #define DOARGS_2 /* nothing */
219 #define DOARGS_3 /* nothing */
220 #define DOARGS_4 movq %rcx, %r10;
221 #define DOARGS_5 DOARGS_4
222 #define DOARGS_6 DOARGS_5
223
224 #else /* !__ASSEMBLER__ */
225 /* Define a macro which expands inline into the wrapper code for a system
226 call. */
227 #undef INLINE_SYSCALL
228 #define INLINE_SYSCALL(name, nr, args...) \
229 ({ \
230 unsigned long resultvar = INTERNAL_SYSCALL (name, , nr, args); \
231 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (resultvar, ), 0)) \
232 { \
233 __set_errno (INTERNAL_SYSCALL_ERRNO (resultvar, )); \
234 resultvar = (unsigned long) -1; \
235 } \
236 (long) resultvar; })
237
238 #undef INTERNAL_SYSCALL_DECL
239 #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
240
241 #define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
242 ({ \
243 unsigned long resultvar; \
244 LOAD_ARGS_##nr (args) \
245 LOAD_REGS_##nr \
246 asm volatile ( \
247 "syscall\n\t" \
248 : "=a" (resultvar) \
249 : "0" (name) ASM_ARGS_##nr : "memory", "cc", "r11", "cx"); \
250 (long) resultvar; })
251 #undef INTERNAL_SYSCALL
252 #define INTERNAL_SYSCALL(name, err, nr, args...) \
253 INTERNAL_SYSCALL_NCS (__NR_##name, err, nr, ##args)
254
255 #undef INTERNAL_SYSCALL_ERROR_P
256 #define INTERNAL_SYSCALL_ERROR_P(val, err) \
257 ((unsigned long) (val) >= -4095L)
258
259 #undef INTERNAL_SYSCALL_ERRNO
260 #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
261
262 #define LOAD_ARGS_0()
263 #define LOAD_REGS_0
264 #define ASM_ARGS_0
265
266 #define LOAD_ARGS_1(a1) \
267 long int __arg1 = (long) (a1); \
268 LOAD_ARGS_0 ()
269 #define LOAD_REGS_1 \
270 register long int _a1 asm ("rdi") = __arg1; \
271 LOAD_REGS_0
272 #define ASM_ARGS_1 ASM_ARGS_0, "r" (_a1)
273
274 #define LOAD_ARGS_2(a1, a2) \
275 long int __arg2 = (long) (a2); \
276 LOAD_ARGS_1 (a1)
277 #define LOAD_REGS_2 \
278 register long int _a2 asm ("rsi") = __arg2; \
279 LOAD_REGS_1
280 #define ASM_ARGS_2 ASM_ARGS_1, "r" (_a2)
281
282 #define LOAD_ARGS_3(a1, a2, a3) \
283 long int __arg3 = (long) (a3); \
284 LOAD_ARGS_2 (a1, a2)
285 #define LOAD_REGS_3 \
286 register long int _a3 asm ("rdx") = __arg3; \
287 LOAD_REGS_2
288 #define ASM_ARGS_3 ASM_ARGS_2, "r" (_a3)
289
290 #define LOAD_ARGS_4(a1, a2, a3, a4) \
291 long int __arg4 = (long) (a4); \
292 LOAD_ARGS_3 (a1, a2, a3)
293 #define LOAD_REGS_4 \
294 register long int _a4 asm ("r10") = __arg4; \
295 LOAD_REGS_3
296 #define ASM_ARGS_4 ASM_ARGS_3, "r" (_a4)
297
298 #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
299 long int __arg5 = (long) (a5); \
300 LOAD_ARGS_4 (a1, a2, a3, a4)
301 #define LOAD_REGS_5 \
302 register long int _a5 asm ("r8") = __arg5; \
303 LOAD_REGS_4
304 #define ASM_ARGS_5 ASM_ARGS_4, "r" (_a5)
305
306 #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
307 long int __arg6 = (long) (a6); \
308 LOAD_ARGS_5 (a1, a2, a3, a4, a5)
309 #define LOAD_REGS_6 \
310 register long int _a6 asm ("r9") = __arg6; \
311 LOAD_REGS_5
312 #define ASM_ARGS_6 ASM_ARGS_5, "r" (_a6)
313
314 #endif /* __ASSEMBLER__ */
315
316
317 /* Pointer mangling support. */
318 #if defined NOT_IN_libc && defined IS_IN_rtld
319 /* We cannot use the thread descriptor because in ld.so we use setjmp
320 earlier than the descriptor is initialized. */
321 # ifdef __ASSEMBLER__
322 # define PTR_MANGLE(reg) xorq __pointer_chk_guard_local(%rip), reg
323 # define PTR_DEMANGLE(reg) PTR_MANGLE (reg)
324 # else
325 # define PTR_MANGLE(reg) asm ("xorq __pointer_chk_guard_local(%%rip), %0"\
326 : "=r" (reg) : "0" (reg))
327 # define PTR_DEMANGLE(reg) PTR_MANGLE (reg)
328 # endif
329 #else
330 # ifdef __ASSEMBLER__
331 # define PTR_MANGLE(reg) xorq %fs:POINTER_GUARD, reg
332 # define PTR_DEMANGLE(reg) PTR_MANGLE (reg)
333 # else
334 # define PTR_MANGLE(var) asm ("xorq %%fs:%c2, %0" \
335 : "=r" (var) \
336 : "0" (var), \
337 "i" (offsetof (tcbhead_t, \
338 pointer_guard)))
339 # define PTR_DEMANGLE(var) PTR_MANGLE (var)
340 # endif
341 #endif
342
343 #endif /* linux/x86_64/sysdep.h */