sysdep.h 10 KB

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