sysdep.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #undef END
  37. #define END(name) \
  38. cfi_endproc; \
  39. ASM_SIZE_DIRECTIVE(name)
  40. #ifdef NO_UNDERSCORES
  41. /* Since C identifiers are not normally prefixed with an underscore
  42. on this system, the asm identifier `syscall_error' intrudes on the
  43. C name space. Make sure we use an innocuous name. */
  44. #define syscall_error __syscall_error
  45. #define mcount _mcount
  46. #endif
  47. #define PSEUDO(name, syscall_name, args) \
  48. lose: \
  49. jmp JUMPTARGET(syscall_error) \
  50. .globl syscall_error; \
  51. ENTRY (name) \
  52. DO_CALL (syscall_name, args); \
  53. jb lose
  54. #undef PSEUDO_END
  55. #define PSEUDO_END(name) \
  56. END (name)
  57. #undef JUMPTARGET
  58. #ifdef __PIC__
  59. #define JUMPTARGET(name) name##@PLT
  60. #else
  61. #define JUMPTARGET(name) name
  62. #endif
  63. /* Local label name for asm code. */
  64. #ifndef L
  65. #define L(name) .L##name
  66. #endif
  67. #endif /* __ASSEMBLER__ */
  68. /* For Linux we can use the system call table in the header file
  69. /usr/include/asm/unistd.h
  70. of the kernel. But these symbols do not follow the SYS_* syntax
  71. so we have to redefine the `SYS_ify' macro here. */
  72. #undef SYS_ify
  73. #define SYS_ify(syscall_name) __NR_##syscall_name
  74. /* This is a kludge to make syscalls.list find these under the names
  75. pread and pwrite, since some kernel headers define those names
  76. and some define the *64 names for the same system calls. */
  77. #if !defined __NR_pread && defined __NR_pread64
  78. # define __NR_pread __NR_pread64
  79. #endif
  80. #if !defined __NR_pwrite && defined __NR_pwrite64
  81. # define __NR_pwrite __NR_pwrite64
  82. #endif
  83. /* This is to help the old kernel headers where __NR_semtimedop is not
  84. available. */
  85. #ifndef __NR_semtimedop
  86. # define __NR_semtimedop 220
  87. #endif
  88. #ifdef __ASSEMBLER__
  89. /* Linux uses a negative return value to indicate syscall errors,
  90. unlike most Unices, which use the condition codes' carry flag.
  91. Since version 2.1 the return value of a system call might be
  92. negative even if the call succeeded. E.g., the `lseek' system call
  93. might return a large offset. Therefore we must not anymore test
  94. for < 0, but test for a real error by making sure the value in %eax
  95. is a real error number. Linus said he will make sure the no syscall
  96. returns a value in -1 .. -4095 as a valid result so we can savely
  97. test with -4095. */
  98. /* We don't want the label for the error handle to be global when we define
  99. it here. */
  100. # ifdef __PIC__
  101. # define SYSCALL_ERROR_LABEL 0f
  102. # else
  103. # define SYSCALL_ERROR_LABEL syscall_error
  104. # endif
  105. # undef PSEUDO
  106. # define PSEUDO(name, syscall_name, args) \
  107. .text; \
  108. ENTRY (name) \
  109. DO_CALL (syscall_name, args); \
  110. cmpq $-4095, %rax; \
  111. jae SYSCALL_ERROR_LABEL; \
  112. L(pseudo_end):
  113. # undef PSEUDO_END
  114. # define PSEUDO_END(name) \
  115. SYSCALL_ERROR_HANDLER \
  116. END (name)
  117. # undef PSEUDO_NOERRNO
  118. # define PSEUDO_NOERRNO(name, syscall_name, args) \
  119. .text; \
  120. ENTRY (name) \
  121. DO_CALL (syscall_name, args)
  122. # undef PSEUDO_END_NOERRNO
  123. # define PSEUDO_END_NOERRNO(name) \
  124. END (name)
  125. # define ret_NOERRNO ret
  126. # undef PSEUDO_ERRVAL
  127. # define PSEUDO_ERRVAL(name, syscall_name, args) \
  128. .text; \
  129. ENTRY (name) \
  130. DO_CALL (syscall_name, args); \
  131. negq %rax
  132. # undef PSEUDO_END_ERRVAL
  133. # define PSEUDO_END_ERRVAL(name) \
  134. END (name)
  135. # ifndef __PIC__
  136. # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
  137. # elif USE___THREAD
  138. # define SYSCALL_ERROR_ERRNO errno
  139. # define SYSCALL_ERROR_HANDLER \
  140. 0: \
  141. movq SYSCALL_ERROR_ERRNO@GOTTPOFF(%rip), %rcx;\
  142. xorl %edx, %edx; \
  143. subq %rax, %rdx; \
  144. movl %edx, %fs:(%rcx); \
  145. orq $-1, %rax; \
  146. jmp L(pseudo_end);
  147. # elif defined _LIBC_REENTRANT
  148. /* Store (- %rax) into errno through the GOT.
  149. Note that errno occupies only 4 bytes. */
  150. # define SYSCALL_ERROR_HANDLER \
  151. 0: \
  152. xorl %edx, %edx; \
  153. subq %rax, %rdx; \
  154. pushq %rdx; \
  155. cfi_adjust_cfa_offset(8); \
  156. call __errno_location@PLT; \
  157. popq %rdx; \
  158. cfi_adjust_cfa_offset(-8); \
  159. movl %edx, (%rax); \
  160. orq $-1, %rax; \
  161. jmp L(pseudo_end);
  162. /* A quick note: it is assumed that the call to `__errno_location' does
  163. not modify the stack! */
  164. # else /* Not _LIBC_REENTRANT. */
  165. # define SYSCALL_ERROR_HANDLER \
  166. 0:movq errno@GOTPCREL(%RIP), %rcx; \
  167. xorl %edx, %edx; \
  168. subq %rax, %rdx; \
  169. movl %edx, (%rcx); \
  170. orq $-1, %rax; \
  171. jmp L(pseudo_end);
  172. # endif /* __PIC__ */
  173. /* The Linux/x86-64 kernel expects the system call parameters in
  174. registers according to the following table:
  175. syscall number rax
  176. arg 1 rdi
  177. arg 2 rsi
  178. arg 3 rdx
  179. arg 4 r10
  180. arg 5 r8
  181. arg 6 r9
  182. The Linux kernel uses and destroys internally these registers:
  183. return address from
  184. syscall rcx
  185. eflags from syscall r11
  186. Normal function call, including calls to the system call stub
  187. functions in the libc, get the first six parameters passed in
  188. registers and the seventh parameter and later on the stack. The
  189. register use is as follows:
  190. system call number in the DO_CALL macro
  191. arg 1 rdi
  192. arg 2 rsi
  193. arg 3 rdx
  194. arg 4 rcx
  195. arg 5 r8
  196. arg 6 r9
  197. We have to take care that the stack is aligned to 16 bytes. When
  198. called the stack is not aligned since the return address has just
  199. been pushed.
  200. Syscalls of more than 6 arguments are not supported. */
  201. # undef DO_CALL
  202. # define DO_CALL(syscall_name, args) \
  203. DOARGS_##args \
  204. movl $SYS_ify (syscall_name), %eax; \
  205. syscall;
  206. # define DOARGS_0 /* nothing */
  207. # define DOARGS_1 /* nothing */
  208. # define DOARGS_2 /* nothing */
  209. # define DOARGS_3 /* nothing */
  210. # define DOARGS_4 movq %rcx, %r10;
  211. # define DOARGS_5 DOARGS_4
  212. # define DOARGS_6 DOARGS_5
  213. #endif /* __ASSEMBLER__ */
  214. #endif /* linux/x86_64/sysdep.h */