sysdep.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 <common/sysdep.h>
  20. #ifdef __ASSEMBLER__
  21. /* Syntactic details of assembler. */
  22. #ifdef HAVE_ELF
  23. /* ELF uses byte-counts for .align, most others use log2 of count of bytes. */
  24. #define ALIGNARG(log2) 1<<log2
  25. /* For ELF we need the `.type' directive to make shared libs work right. */
  26. #define ASM_TYPE_DIRECTIVE(name,typearg) .type name,typearg;
  27. #define ASM_SIZE_DIRECTIVE(name) .size name,.-name;
  28. /* In ELF C symbols are asm symbols. */
  29. #undef NO_UNDERSCORES
  30. #define NO_UNDERSCORES
  31. #else
  32. #define ALIGNARG(log2) log2
  33. #define ASM_TYPE_DIRECTIVE(name,type) /* Nothing is specified. */
  34. #define ASM_SIZE_DIRECTIVE(name) /* Nothing is specified. */
  35. #endif
  36. /* Define an entry point visible from C. */
  37. #define ENTRY(name) \
  38. ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME(name); \
  39. ASM_TYPE_DIRECTIVE (C_SYMBOL_NAME(name),@function) \
  40. .align ALIGNARG(4); \
  41. C_LABEL(name) \
  42. cfi_startproc; \
  43. CALL_MCOUNT
  44. #undef END
  45. #define END(name) \
  46. cfi_endproc; \
  47. ASM_SIZE_DIRECTIVE(name)
  48. /* If compiled for profiling, call `mcount' at the start of each function. */
  49. #ifdef PROF
  50. /* The mcount code relies on a normal frame pointer being on the stack
  51. to locate our caller, so push one just for its benefit. */
  52. #define CALL_MCOUNT \
  53. pushq %rbp; \
  54. cfi_adjust_cfa_offset(8); \
  55. movq %rsp, %rbp; \
  56. cfi_def_cfa_register(%rbp); \
  57. call JUMPTARGET(mcount); \
  58. popq %rbp; \
  59. cfi_def_cfa(rsp,8);
  60. #else
  61. #define CALL_MCOUNT /* Do nothing. */
  62. #endif
  63. #ifdef NO_UNDERSCORES
  64. /* Since C identifiers are not normally prefixed with an underscore
  65. on this system, the asm identifier `syscall_error' intrudes on the
  66. C name space. Make sure we use an innocuous name. */
  67. #define syscall_error __syscall_error
  68. #define mcount _mcount
  69. #endif
  70. #define PSEUDO(name, syscall_name, args) \
  71. lose: \
  72. jmp JUMPTARGET(syscall_error) \
  73. .globl syscall_error; \
  74. ENTRY (name) \
  75. DO_CALL (syscall_name, args); \
  76. jb lose
  77. #undef PSEUDO_END
  78. #define PSEUDO_END(name) \
  79. END (name)
  80. #undef JUMPTARGET
  81. #ifdef __PIC__
  82. #define JUMPTARGET(name) name##@PLT
  83. #else
  84. #define JUMPTARGET(name) name
  85. #endif
  86. /* Local label name for asm code. */
  87. #ifndef L
  88. # ifdef HAVE_ELF
  89. /* ELF-like local names start with `.L'. */
  90. # define L(name) .L##name
  91. # else
  92. # define L(name) name
  93. # endif
  94. #endif
  95. #endif /* __ASSEMBLER__ */
  96. /* For Linux we can use the system call table in the header file
  97. /usr/include/asm/unistd.h
  98. of the kernel. But these symbols do not follow the SYS_* syntax
  99. so we have to redefine the `SYS_ify' macro here. */
  100. #undef SYS_ify
  101. #define SYS_ify(syscall_name) __NR_##syscall_name
  102. /* This is a kludge to make syscalls.list find these under the names
  103. pread and pwrite, since some kernel headers define those names
  104. and some define the *64 names for the same system calls. */
  105. #if !defined __NR_pread && defined __NR_pread64
  106. # define __NR_pread __NR_pread64
  107. #endif
  108. #if !defined __NR_pwrite && defined __NR_pwrite64
  109. # define __NR_pwrite __NR_pwrite64
  110. #endif
  111. /* This is to help the old kernel headers where __NR_semtimedop is not
  112. available. */
  113. #ifndef __NR_semtimedop
  114. # define __NR_semtimedop 220
  115. #endif
  116. #ifdef __ASSEMBLER__
  117. /* Linux uses a negative return value to indicate syscall errors,
  118. unlike most Unices, which use the condition codes' carry flag.
  119. Since version 2.1 the return value of a system call might be
  120. negative even if the call succeeded. E.g., the `lseek' system call
  121. might return a large offset. Therefore we must not anymore test
  122. for < 0, but test for a real error by making sure the value in %eax
  123. is a real error number. Linus said he will make sure the no syscall
  124. returns a value in -1 .. -4095 as a valid result so we can savely
  125. test with -4095. */
  126. /* We don't want the label for the error handle to be global when we define
  127. it here. */
  128. # ifdef __PIC__
  129. # define SYSCALL_ERROR_LABEL 0f
  130. # else
  131. # define SYSCALL_ERROR_LABEL syscall_error
  132. # endif
  133. # undef PSEUDO
  134. # define PSEUDO(name, syscall_name, args) \
  135. .text; \
  136. ENTRY (name) \
  137. DO_CALL (syscall_name, args); \
  138. cmpq $-4095, %rax; \
  139. jae SYSCALL_ERROR_LABEL; \
  140. L(pseudo_end):
  141. # undef PSEUDO_END
  142. # define PSEUDO_END(name) \
  143. SYSCALL_ERROR_HANDLER \
  144. END (name)
  145. # undef PSEUDO_NOERRNO
  146. # define PSEUDO_NOERRNO(name, syscall_name, args) \
  147. .text; \
  148. ENTRY (name) \
  149. DO_CALL (syscall_name, args)
  150. # undef PSEUDO_END_NOERRNO
  151. # define PSEUDO_END_NOERRNO(name) \
  152. END (name)
  153. # define ret_NOERRNO ret
  154. # undef PSEUDO_ERRVAL
  155. # define PSEUDO_ERRVAL(name, syscall_name, args) \
  156. .text; \
  157. ENTRY (name) \
  158. DO_CALL (syscall_name, args); \
  159. negq %rax
  160. # undef PSEUDO_END_ERRVAL
  161. # define PSEUDO_END_ERRVAL(name) \
  162. END (name)
  163. # define ret_ERRVAL ret
  164. # ifndef __PIC__
  165. # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
  166. # elif defined(RTLD_PRIVATE_ERRNO)
  167. # define SYSCALL_ERROR_HANDLER \
  168. 0: \
  169. leaq rtld_errno(%rip), %rcx; \
  170. xorl %edx, %edx; \
  171. subq %rax, %rdx; \
  172. movl %edx, (%rcx); \
  173. orq $-1, %rax; \
  174. jmp L(pseudo_end);
  175. # elif USE___THREAD
  176. # ifndef NOT_IN_libc
  177. # define SYSCALL_ERROR_ERRNO __libc_errno
  178. # else
  179. # define SYSCALL_ERROR_ERRNO errno
  180. # endif
  181. # define SYSCALL_ERROR_HANDLER \
  182. 0: \
  183. movq SYSCALL_ERROR_ERRNO@GOTTPOFF(%rip), %rcx;\
  184. xorl %edx, %edx; \
  185. subq %rax, %rdx; \
  186. movl %edx, %fs:(%rcx); \
  187. orq $-1, %rax; \
  188. jmp L(pseudo_end);
  189. # elif defined _LIBC_REENTRANT
  190. /* Store (- %rax) into errno through the GOT.
  191. Note that errno occupies only 4 bytes. */
  192. # define SYSCALL_ERROR_HANDLER \
  193. 0: \
  194. xorl %edx, %edx; \
  195. subq %rax, %rdx; \
  196. pushq %rdx; \
  197. cfi_adjust_cfa_offset(8); \
  198. call __errno_location@PLT; \
  199. popq %rdx; \
  200. cfi_adjust_cfa_offset(-8); \
  201. movl %edx, (%rax); \
  202. orq $-1, %rax; \
  203. jmp L(pseudo_end);
  204. /* A quick note: it is assumed that the call to `__errno_location' does
  205. not modify the stack! */
  206. # else /* Not _LIBC_REENTRANT. */
  207. # define SYSCALL_ERROR_HANDLER \
  208. 0:movq errno@GOTPCREL(%RIP), %rcx; \
  209. xorl %edx, %edx; \
  210. subq %rax, %rdx; \
  211. movl %edx, (%rcx); \
  212. orq $-1, %rax; \
  213. jmp L(pseudo_end);
  214. # endif /* __PIC__ */
  215. /* The Linux/x86-64 kernel expects the system call parameters in
  216. registers according to the following table:
  217. syscall number rax
  218. arg 1 rdi
  219. arg 2 rsi
  220. arg 3 rdx
  221. arg 4 r10
  222. arg 5 r8
  223. arg 6 r9
  224. The Linux kernel uses and destroys internally these registers:
  225. return address from
  226. syscall rcx
  227. eflags from syscall r11
  228. Normal function call, including calls to the system call stub
  229. functions in the libc, get the first six parameters passed in
  230. registers and the seventh parameter and later on the stack. The
  231. register use is as follows:
  232. system call number in the DO_CALL macro
  233. arg 1 rdi
  234. arg 2 rsi
  235. arg 3 rdx
  236. arg 4 rcx
  237. arg 5 r8
  238. arg 6 r9
  239. We have to take care that the stack is aligned to 16 bytes. When
  240. called the stack is not aligned since the return address has just
  241. been pushed.
  242. Syscalls of more than 6 arguments are not supported. */
  243. # undef DO_CALL
  244. # define DO_CALL(syscall_name, args) \
  245. DOARGS_##args \
  246. movl $SYS_ify (syscall_name), %eax; \
  247. syscall;
  248. # define DOARGS_0 /* nothing */
  249. # define DOARGS_1 /* nothing */
  250. # define DOARGS_2 /* nothing */
  251. # define DOARGS_3 /* nothing */
  252. # define DOARGS_4 movq %rcx, %r10;
  253. # define DOARGS_5 DOARGS_4
  254. # define DOARGS_6 DOARGS_5
  255. #endif /* __ASSEMBLER__ */
  256. /* Pointer mangling support. */
  257. #if defined NOT_IN_libc && defined IS_IN_rtld
  258. /* We cannot use the thread descriptor because in ld.so we use setjmp
  259. earlier than the descriptor is initialized. */
  260. # ifdef __ASSEMBLER__
  261. # define PTR_MANGLE(reg) xorq __pointer_chk_guard_local(%rip), reg; \
  262. rolq $17, reg
  263. # define PTR_DEMANGLE(reg) rorq $17, reg; \
  264. xorq __pointer_chk_guard_local(%rip), reg
  265. # else
  266. # define PTR_MANGLE(reg) __asm__ ("xorq __pointer_chk_guard_local(%%rip), %0\n" \
  267. "rolq $17, %0" \
  268. : "=r" (reg) : "0" (reg))
  269. # define PTR_DEMANGLE(reg) __asm__ ("rorq $17, %0\n" \
  270. "xorq __pointer_chk_guard_local(%%rip), %0" \
  271. : "=r" (reg) : "0" (reg))
  272. # endif
  273. #else
  274. # ifdef __ASSEMBLER__
  275. # define PTR_MANGLE(reg) xorq %fs:POINTER_GUARD, reg; \
  276. rolq $17, reg
  277. # define PTR_DEMANGLE(reg) rorq $17, reg; \
  278. xorq %fs:POINTER_GUARD, reg
  279. # else
  280. # define PTR_MANGLE(var) __asm__ ("xorq %%fs:%c2, %0\n" \
  281. "rolq $17, %0" \
  282. : "=r" (var) \
  283. : "0" (var), \
  284. "i" (offsetof (tcbhead_t, \
  285. pointer_guard)))
  286. # define PTR_DEMANGLE(var) __asm__ ("rorq $17, %0\n" \
  287. "xorq %%fs:%c2, %0" \
  288. : "=r" (var) \
  289. : "0" (var), \
  290. "i" (offsetof (tcbhead_t, \
  291. pointer_guard)))
  292. # endif
  293. #endif
  294. #endif /* linux/x86_64/sysdep.h */