sysdep.h 10 KB

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