sysdep.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. #ifdef IS_IN_rtld
  97. # include <dl-sysdep.h> /* Defines RTLD_PRIVATE_ERRNO. */
  98. #endif
  99. /* For Linux we can use the system call table in the header file
  100. /usr/include/asm/unistd.h
  101. of the kernel. But these symbols do not follow the SYS_* syntax
  102. so we have to redefine the `SYS_ify' macro here. */
  103. #undef SYS_ify
  104. #define SYS_ify(syscall_name) __NR_##syscall_name
  105. /* This is a kludge to make syscalls.list find these under the names
  106. pread and pwrite, since some kernel headers define those names
  107. and some define the *64 names for the same system calls. */
  108. #if !defined __NR_pread && defined __NR_pread64
  109. # define __NR_pread __NR_pread64
  110. #endif
  111. #if !defined __NR_pwrite && defined __NR_pwrite64
  112. # define __NR_pwrite __NR_pwrite64
  113. #endif
  114. /* This is to help the old kernel headers where __NR_semtimedop is not
  115. available. */
  116. #ifndef __NR_semtimedop
  117. # define __NR_semtimedop 220
  118. #endif
  119. #ifdef __ASSEMBLER__
  120. /* Linux uses a negative return value to indicate syscall errors,
  121. unlike most Unices, which use the condition codes' carry flag.
  122. Since version 2.1 the return value of a system call might be
  123. negative even if the call succeeded. E.g., the `lseek' system call
  124. might return a large offset. Therefore we must not anymore test
  125. for < 0, but test for a real error by making sure the value in %eax
  126. is a real error number. Linus said he will make sure the no syscall
  127. returns a value in -1 .. -4095 as a valid result so we can savely
  128. test with -4095. */
  129. /* We don't want the label for the error handle to be global when we define
  130. it here. */
  131. # ifdef __PIC__
  132. # define SYSCALL_ERROR_LABEL 0f
  133. # else
  134. # define SYSCALL_ERROR_LABEL syscall_error
  135. # endif
  136. # undef PSEUDO
  137. # define PSEUDO(name, syscall_name, args) \
  138. .text; \
  139. ENTRY (name) \
  140. DO_CALL (syscall_name, args); \
  141. cmpq $-4095, %rax; \
  142. jae SYSCALL_ERROR_LABEL; \
  143. L(pseudo_end):
  144. # undef PSEUDO_END
  145. # define PSEUDO_END(name) \
  146. SYSCALL_ERROR_HANDLER \
  147. END (name)
  148. # undef PSEUDO_NOERRNO
  149. # define PSEUDO_NOERRNO(name, syscall_name, args) \
  150. .text; \
  151. ENTRY (name) \
  152. DO_CALL (syscall_name, args)
  153. # undef PSEUDO_END_NOERRNO
  154. # define PSEUDO_END_NOERRNO(name) \
  155. END (name)
  156. # define ret_NOERRNO ret
  157. # undef PSEUDO_ERRVAL
  158. # define PSEUDO_ERRVAL(name, syscall_name, args) \
  159. .text; \
  160. ENTRY (name) \
  161. DO_CALL (syscall_name, args); \
  162. negq %rax
  163. # undef PSEUDO_END_ERRVAL
  164. # define PSEUDO_END_ERRVAL(name) \
  165. END (name)
  166. # define ret_ERRVAL ret
  167. # ifndef __PIC__
  168. # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
  169. # elif defined(RTLD_PRIVATE_ERRNO)
  170. # define SYSCALL_ERROR_HANDLER \
  171. 0: \
  172. leaq rtld_errno(%rip), %rcx; \
  173. xorl %edx, %edx; \
  174. subq %rax, %rdx; \
  175. movl %edx, (%rcx); \
  176. orq $-1, %rax; \
  177. jmp L(pseudo_end);
  178. # elif USE___THREAD
  179. # ifndef NOT_IN_libc
  180. # define SYSCALL_ERROR_ERRNO __libc_errno
  181. # else
  182. # define SYSCALL_ERROR_ERRNO errno
  183. # endif
  184. # define SYSCALL_ERROR_HANDLER \
  185. 0: \
  186. movq SYSCALL_ERROR_ERRNO@GOTTPOFF(%rip), %rcx;\
  187. xorl %edx, %edx; \
  188. subq %rax, %rdx; \
  189. movl %edx, %fs:(%rcx); \
  190. orq $-1, %rax; \
  191. jmp L(pseudo_end);
  192. # elif defined _LIBC_REENTRANT
  193. /* Store (- %rax) into errno through the GOT.
  194. Note that errno occupies only 4 bytes. */
  195. # define SYSCALL_ERROR_HANDLER \
  196. 0: \
  197. xorl %edx, %edx; \
  198. subq %rax, %rdx; \
  199. pushq %rdx; \
  200. cfi_adjust_cfa_offset(8); \
  201. call __errno_location@PLT; \
  202. popq %rdx; \
  203. cfi_adjust_cfa_offset(-8); \
  204. movl %edx, (%rax); \
  205. orq $-1, %rax; \
  206. jmp L(pseudo_end);
  207. /* A quick note: it is assumed that the call to `__errno_location' does
  208. not modify the stack! */
  209. # else /* Not _LIBC_REENTRANT. */
  210. # define SYSCALL_ERROR_HANDLER \
  211. 0:movq errno@GOTPCREL(%RIP), %rcx; \
  212. xorl %edx, %edx; \
  213. subq %rax, %rdx; \
  214. movl %edx, (%rcx); \
  215. orq $-1, %rax; \
  216. jmp L(pseudo_end);
  217. # endif /* __PIC__ */
  218. /* The Linux/x86-64 kernel expects the system call parameters in
  219. registers according to the following table:
  220. syscall number rax
  221. arg 1 rdi
  222. arg 2 rsi
  223. arg 3 rdx
  224. arg 4 r10
  225. arg 5 r8
  226. arg 6 r9
  227. The Linux kernel uses and destroys internally these registers:
  228. return address from
  229. syscall rcx
  230. eflags from syscall r11
  231. Normal function call, including calls to the system call stub
  232. functions in the libc, get the first six parameters passed in
  233. registers and the seventh parameter and later on the stack. The
  234. register use is as follows:
  235. system call number in the DO_CALL macro
  236. arg 1 rdi
  237. arg 2 rsi
  238. arg 3 rdx
  239. arg 4 rcx
  240. arg 5 r8
  241. arg 6 r9
  242. We have to take care that the stack is aligned to 16 bytes. When
  243. called the stack is not aligned since the return address has just
  244. been pushed.
  245. Syscalls of more than 6 arguments are not supported. */
  246. # undef DO_CALL
  247. # define DO_CALL(syscall_name, args) \
  248. DOARGS_##args \
  249. movl $SYS_ify (syscall_name), %eax; \
  250. syscall;
  251. # define DOARGS_0 /* nothing */
  252. # define DOARGS_1 /* nothing */
  253. # define DOARGS_2 /* nothing */
  254. # define DOARGS_3 /* nothing */
  255. # define DOARGS_4 movq %rcx, %r10;
  256. # define DOARGS_5 DOARGS_4
  257. # define DOARGS_6 DOARGS_5
  258. #endif /* __ASSEMBLER__ */
  259. /* Pointer mangling support. */
  260. #if defined NOT_IN_libc && defined IS_IN_rtld
  261. /* We cannot use the thread descriptor because in ld.so we use setjmp
  262. earlier than the descriptor is initialized. */
  263. # ifdef __ASSEMBLER__
  264. # define PTR_MANGLE(reg) xorq __pointer_chk_guard_local(%rip), reg; \
  265. rolq $17, reg
  266. # define PTR_DEMANGLE(reg) rorq $17, reg; \
  267. xorq __pointer_chk_guard_local(%rip), reg
  268. # else
  269. # define PTR_MANGLE(reg) asm ("xorq __pointer_chk_guard_local(%%rip), %0\n" \
  270. "rolq $17, %0" \
  271. : "=r" (reg) : "0" (reg))
  272. # define PTR_DEMANGLE(reg) asm ("rorq $17, %0\n" \
  273. "xorq __pointer_chk_guard_local(%%rip), %0" \
  274. : "=r" (reg) : "0" (reg))
  275. # endif
  276. #else
  277. # ifdef __ASSEMBLER__
  278. # define PTR_MANGLE(reg) xorq %fs:POINTER_GUARD, reg; \
  279. rolq $17, reg
  280. # define PTR_DEMANGLE(reg) rorq $17, reg; \
  281. xorq %fs:POINTER_GUARD, reg
  282. # else
  283. # define PTR_MANGLE(var) asm ("xorq %%fs:%c2, %0\n" \
  284. "rolq $17, %0" \
  285. : "=r" (var) \
  286. : "0" (var), \
  287. "i" (offsetof (tcbhead_t, \
  288. pointer_guard)))
  289. # define PTR_DEMANGLE(var) asm ("rorq $17, %0\n" \
  290. "xorq %%fs:%c2, %0" \
  291. : "=r" (var) \
  292. : "0" (var), \
  293. "i" (offsetof (tcbhead_t, \
  294. pointer_guard)))
  295. # endif
  296. #endif
  297. #endif /* linux/x86_64/sysdep.h */