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