sysdep.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. ASM_GLOBAL_DIRECTIVE 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. # define ret_ERRVAL ret
  152. # ifndef __PIC__
  153. # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
  154. # elif defined(RTLD_PRIVATE_ERRNO)
  155. # define SYSCALL_ERROR_HANDLER \
  156. 0: \
  157. leaq rtld_errno(%rip), %rcx; \
  158. xorl %edx, %edx; \
  159. subq %rax, %rdx; \
  160. movl %edx, (%rcx); \
  161. orq $-1, %rax; \
  162. jmp L(pseudo_end);
  163. # elif USE___THREAD
  164. # ifndef NOT_IN_libc
  165. # define SYSCALL_ERROR_ERRNO __libc_errno
  166. # else
  167. # define SYSCALL_ERROR_ERRNO errno
  168. # endif
  169. # define SYSCALL_ERROR_HANDLER \
  170. 0: \
  171. movq SYSCALL_ERROR_ERRNO@GOTTPOFF(%rip), %rcx;\
  172. xorl %edx, %edx; \
  173. subq %rax, %rdx; \
  174. movl %edx, %fs:(%rcx); \
  175. orq $-1, %rax; \
  176. jmp L(pseudo_end);
  177. # elif defined _LIBC_REENTRANT
  178. /* Store (- %rax) into errno through the GOT.
  179. Note that errno occupies only 4 bytes. */
  180. # define SYSCALL_ERROR_HANDLER \
  181. 0: \
  182. xorl %edx, %edx; \
  183. subq %rax, %rdx; \
  184. pushq %rdx; \
  185. cfi_adjust_cfa_offset(8); \
  186. call __errno_location@PLT; \
  187. popq %rdx; \
  188. cfi_adjust_cfa_offset(-8); \
  189. movl %edx, (%rax); \
  190. orq $-1, %rax; \
  191. jmp L(pseudo_end);
  192. /* A quick note: it is assumed that the call to `__errno_location' does
  193. not modify the stack! */
  194. # else /* Not _LIBC_REENTRANT. */
  195. # define SYSCALL_ERROR_HANDLER \
  196. 0:movq errno@GOTPCREL(%RIP), %rcx; \
  197. xorl %edx, %edx; \
  198. subq %rax, %rdx; \
  199. movl %edx, (%rcx); \
  200. orq $-1, %rax; \
  201. jmp L(pseudo_end);
  202. # endif /* __PIC__ */
  203. /* The Linux/x86-64 kernel expects the system call parameters in
  204. registers according to the following table:
  205. syscall number rax
  206. arg 1 rdi
  207. arg 2 rsi
  208. arg 3 rdx
  209. arg 4 r10
  210. arg 5 r8
  211. arg 6 r9
  212. The Linux kernel uses and destroys internally these registers:
  213. return address from
  214. syscall rcx
  215. eflags from syscall r11
  216. Normal function call, including calls to the system call stub
  217. functions in the libc, get the first six parameters passed in
  218. registers and the seventh parameter and later on the stack. The
  219. register use is as follows:
  220. system call number in the DO_CALL macro
  221. arg 1 rdi
  222. arg 2 rsi
  223. arg 3 rdx
  224. arg 4 rcx
  225. arg 5 r8
  226. arg 6 r9
  227. We have to take care that the stack is aligned to 16 bytes. When
  228. called the stack is not aligned since the return address has just
  229. been pushed.
  230. Syscalls of more than 6 arguments are not supported. */
  231. # undef DO_CALL
  232. # define DO_CALL(syscall_name, args) \
  233. DOARGS_##args \
  234. movl $SYS_ify (syscall_name), %eax; \
  235. syscall;
  236. # define DOARGS_0 /* nothing */
  237. # define DOARGS_1 /* nothing */
  238. # define DOARGS_2 /* nothing */
  239. # define DOARGS_3 /* nothing */
  240. # define DOARGS_4 movq %rcx, %r10;
  241. # define DOARGS_5 DOARGS_4
  242. # define DOARGS_6 DOARGS_5
  243. #endif /* __ASSEMBLER__ */
  244. /* Pointer mangling support. */
  245. #if defined NOT_IN_libc && defined IS_IN_rtld
  246. /* We cannot use the thread descriptor because in ld.so we use setjmp
  247. earlier than the descriptor is initialized. */
  248. # ifdef __ASSEMBLER__
  249. # define PTR_MANGLE(reg) xorq __pointer_chk_guard_local(%rip), reg; \
  250. rolq $17, reg
  251. # define PTR_DEMANGLE(reg) rorq $17, reg; \
  252. xorq __pointer_chk_guard_local(%rip), reg
  253. # else
  254. # define PTR_MANGLE(reg) __asm__ ("xorq __pointer_chk_guard_local(%%rip), %0\n" \
  255. "rolq $17, %0" \
  256. : "=r" (reg) : "0" (reg))
  257. # define PTR_DEMANGLE(reg) __asm__ ("rorq $17, %0\n" \
  258. "xorq __pointer_chk_guard_local(%%rip), %0" \
  259. : "=r" (reg) : "0" (reg))
  260. # endif
  261. #else
  262. # ifdef __ASSEMBLER__
  263. # define PTR_MANGLE(reg) xorq %fs:POINTER_GUARD, reg; \
  264. rolq $17, reg
  265. # define PTR_DEMANGLE(reg) rorq $17, reg; \
  266. xorq %fs:POINTER_GUARD, reg
  267. # else
  268. # define PTR_MANGLE(var) __asm__ ("xorq %%fs:%c2, %0\n" \
  269. "rolq $17, %0" \
  270. : "=r" (var) \
  271. : "0" (var), \
  272. "i" (offsetof (tcbhead_t, \
  273. pointer_guard)))
  274. # define PTR_DEMANGLE(var) __asm__ ("rorq $17, %0\n" \
  275. "xorq %%fs:%c2, %0" \
  276. : "=r" (var) \
  277. : "0" (var), \
  278. "i" (offsetof (tcbhead_t, \
  279. pointer_guard)))
  280. # endif
  281. #endif
  282. #endif /* linux/x86_64/sysdep.h */