sysdep.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* Copyright (C) 2001-2005, 2007 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  15. #ifndef _LINUX_X86_64_SYSDEP_H
  16. #define _LINUX_X86_64_SYSDEP_H 1
  17. /* There is some commonality. */
  18. #include <sys/syscall.h>
  19. #include <sysdeps/i386/sysdep.h>
  20. #ifdef IS_IN_rtld
  21. # include <dl-sysdep.h> /* Defines RTLD_PRIVATE_ERRNO. */
  22. #endif
  23. /* For Linux we can use the system call table in the header file
  24. /usr/include/asm/unistd.h
  25. of the kernel. But these symbols do not follow the SYS_* syntax
  26. so we have to redefine the `SYS_ify' macro here. */
  27. #undef SYS_ify
  28. #define SYS_ify(syscall_name) __NR_##syscall_name
  29. /* This is a kludge to make syscalls.list find these under the names
  30. pread and pwrite, since some kernel headers define those names
  31. and some define the *64 names for the same system calls. */
  32. #if !defined __NR_pread && defined __NR_pread64
  33. # define __NR_pread __NR_pread64
  34. #endif
  35. #if !defined __NR_pwrite && defined __NR_pwrite64
  36. # define __NR_pwrite __NR_pwrite64
  37. #endif
  38. /* This is to help the old kernel headers where __NR_semtimedop is not
  39. available. */
  40. #ifndef __NR_semtimedop
  41. # define __NR_semtimedop 220
  42. #endif
  43. #ifdef __ASSEMBLER__
  44. /* Linux uses a negative return value to indicate syscall errors,
  45. unlike most Unices, which use the condition codes' carry flag.
  46. Since version 2.1 the return value of a system call might be
  47. negative even if the call succeeded. E.g., the `lseek' system call
  48. might return a large offset. Therefore we must not anymore test
  49. for < 0, but test for a real error by making sure the value in %eax
  50. is a real error number. Linus said he will make sure the no syscall
  51. returns a value in -1 .. -4095 as a valid result so we can savely
  52. test with -4095. */
  53. /* We don't want the label for the error handle to be global when we define
  54. it here. */
  55. # ifdef __PIC__
  56. # define SYSCALL_ERROR_LABEL 0f
  57. # else
  58. # define SYSCALL_ERROR_LABEL syscall_error
  59. # endif
  60. # undef PSEUDO
  61. # define PSEUDO(name, syscall_name, args) \
  62. .text; \
  63. ENTRY (name) \
  64. DO_CALL (syscall_name, args); \
  65. cmpq $-4095, %rax; \
  66. jae SYSCALL_ERROR_LABEL; \
  67. L(pseudo_end):
  68. # undef PSEUDO_END
  69. # define PSEUDO_END(name) \
  70. SYSCALL_ERROR_HANDLER \
  71. END (name)
  72. # undef PSEUDO_NOERRNO
  73. # define PSEUDO_NOERRNO(name, syscall_name, args) \
  74. .text; \
  75. ENTRY (name) \
  76. DO_CALL (syscall_name, args)
  77. # undef PSEUDO_END_NOERRNO
  78. # define PSEUDO_END_NOERRNO(name) \
  79. END (name)
  80. # define ret_NOERRNO ret
  81. # undef PSEUDO_ERRVAL
  82. # define PSEUDO_ERRVAL(name, syscall_name, args) \
  83. .text; \
  84. ENTRY (name) \
  85. DO_CALL (syscall_name, args); \
  86. negq %rax
  87. # undef PSEUDO_END_ERRVAL
  88. # define PSEUDO_END_ERRVAL(name) \
  89. END (name)
  90. # define ret_ERRVAL ret
  91. # ifndef __PIC__
  92. # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
  93. # elif defined(RTLD_PRIVATE_ERRNO)
  94. # define SYSCALL_ERROR_HANDLER \
  95. 0: \
  96. leaq rtld_errno(%rip), %rcx; \
  97. xorl %edx, %edx; \
  98. subq %rax, %rdx; \
  99. movl %edx, (%rcx); \
  100. orq $-1, %rax; \
  101. jmp L(pseudo_end);
  102. # elif USE___THREAD
  103. # ifndef NOT_IN_libc
  104. # define SYSCALL_ERROR_ERRNO __libc_errno
  105. # else
  106. # define SYSCALL_ERROR_ERRNO errno
  107. # endif
  108. # define SYSCALL_ERROR_HANDLER \
  109. 0: \
  110. movq SYSCALL_ERROR_ERRNO@GOTTPOFF(%rip), %rcx;\
  111. xorl %edx, %edx; \
  112. subq %rax, %rdx; \
  113. movl %edx, %fs:(%rcx); \
  114. orq $-1, %rax; \
  115. jmp L(pseudo_end);
  116. # elif defined _LIBC_REENTRANT
  117. /* Store (- %rax) into errno through the GOT.
  118. Note that errno occupies only 4 bytes. */
  119. # define SYSCALL_ERROR_HANDLER \
  120. 0: \
  121. xorl %edx, %edx; \
  122. subq %rax, %rdx; \
  123. pushq %rdx; \
  124. cfi_adjust_cfa_offset(8); \
  125. call __errno_location@PLT; \
  126. popq %rdx; \
  127. cfi_adjust_cfa_offset(-8); \
  128. movl %edx, (%rax); \
  129. orq $-1, %rax; \
  130. jmp L(pseudo_end);
  131. /* A quick note: it is assumed that the call to `__errno_location' does
  132. not modify the stack! */
  133. # else /* Not _LIBC_REENTRANT. */
  134. # define SYSCALL_ERROR_HANDLER \
  135. 0:movq errno@GOTPCREL(%RIP), %rcx; \
  136. xorl %edx, %edx; \
  137. subq %rax, %rdx; \
  138. movl %edx, (%rcx); \
  139. orq $-1, %rax; \
  140. jmp L(pseudo_end);
  141. # endif /* __PIC__ */
  142. /* The Linux/x86-64 kernel expects the system call parameters in
  143. registers according to the following table:
  144. syscall number rax
  145. arg 1 rdi
  146. arg 2 rsi
  147. arg 3 rdx
  148. arg 4 r10
  149. arg 5 r8
  150. arg 6 r9
  151. The Linux kernel uses and destroys internally these registers:
  152. return address from
  153. syscall rcx
  154. eflags from syscall r11
  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. system call number in the DO_CALL macro
  160. arg 1 rdi
  161. arg 2 rsi
  162. arg 3 rdx
  163. arg 4 rcx
  164. arg 5 r8
  165. arg 6 r9
  166. We have to take care that the stack is aligned to 16 bytes. When
  167. called the stack is not aligned since the return address has just
  168. been pushed.
  169. Syscalls of more than 6 arguments are not supported. */
  170. # undef DO_CALL
  171. # define DO_CALL(syscall_name, args) \
  172. DOARGS_##args \
  173. movl $SYS_ify (syscall_name), %eax; \
  174. syscall;
  175. # define DOARGS_0 /* nothing */
  176. # define DOARGS_1 /* nothing */
  177. # define DOARGS_2 /* nothing */
  178. # define DOARGS_3 /* nothing */
  179. # define DOARGS_4 movq %rcx, %r10;
  180. # define DOARGS_5 DOARGS_4
  181. # define DOARGS_6 DOARGS_5
  182. #endif /* __ASSEMBLER__ */
  183. /* Pointer mangling support. */
  184. #if defined NOT_IN_libc && defined IS_IN_rtld
  185. /* We cannot use the thread descriptor because in ld.so we use setjmp
  186. earlier than the descriptor is initialized. */
  187. # ifdef __ASSEMBLER__
  188. # define PTR_MANGLE(reg) xorq __pointer_chk_guard_local(%rip), reg; \
  189. rolq $17, reg
  190. # define PTR_DEMANGLE(reg) rorq $17, reg; \
  191. xorq __pointer_chk_guard_local(%rip), reg
  192. # else
  193. # define PTR_MANGLE(reg) asm ("xorq __pointer_chk_guard_local(%%rip), %0\n" \
  194. "rolq $17, %0" \
  195. : "=r" (reg) : "0" (reg))
  196. # define PTR_DEMANGLE(reg) asm ("rorq $17, %0\n" \
  197. "xorq __pointer_chk_guard_local(%%rip), %0" \
  198. : "=r" (reg) : "0" (reg))
  199. # endif
  200. #else
  201. # ifdef __ASSEMBLER__
  202. # define PTR_MANGLE(reg) xorq %fs:POINTER_GUARD, reg; \
  203. rolq $17, reg
  204. # define PTR_DEMANGLE(reg) rorq $17, reg; \
  205. xorq %fs:POINTER_GUARD, reg
  206. # else
  207. # define PTR_MANGLE(var) asm ("xorq %%fs:%c2, %0\n" \
  208. "rolq $17, %0" \
  209. : "=r" (var) \
  210. : "0" (var), \
  211. "i" (offsetof (tcbhead_t, \
  212. pointer_guard)))
  213. # define PTR_DEMANGLE(var) asm ("rorq $17, %0\n" \
  214. "xorq %%fs:%c2, %0" \
  215. : "=r" (var) \
  216. : "0" (var), \
  217. "i" (offsetof (tcbhead_t, \
  218. pointer_guard)))
  219. # endif
  220. #endif
  221. #endif /* linux/x86_64/sysdep.h */