sysdep.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* Copyright (C) 1992,1993,1995-2000,2002-2006,2007
  2. Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper, <drepper@gnu.org>, August 1995.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #ifndef _LINUX_I386_SYSDEP_H
  18. #define _LINUX_I386_SYSDEP_H 1
  19. #include <sys/syscall.h>
  20. #include <sysdeps/i386/sysdep.h>
  21. #ifndef offsetof
  22. # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  23. #endif
  24. /* For Linux we can use the system call table in the header file
  25. /usr/include/asm/unistd.h
  26. of the kernel. But these symbols do not follow the SYS_* syntax
  27. so we have to redefine the `SYS_ify' macro here. */
  28. #undef SYS_ify
  29. #define SYS_ify(syscall_name) __NR_##syscall_name
  30. #if defined USE_DL_SYSINFO \
  31. && (!defined NOT_IN_libc || defined IS_IN_libpthread)
  32. # define I386_USE_SYSENTER 1
  33. #else
  34. # undef I386_USE_SYSENTER
  35. #endif
  36. #ifdef __ASSEMBLER__
  37. /* Linux uses a negative return value to indicate syscall errors,
  38. unlike most Unices, which use the condition codes' carry flag.
  39. Since version 2.1 the return value of a system call might be
  40. negative even if the call succeeded. E.g., the `lseek' system call
  41. might return a large offset. Therefore we must not anymore test
  42. for < 0, but test for a real error by making sure the value in %eax
  43. is a real error number. Linus said he will make sure the no syscall
  44. returns a value in -1 .. -4095 as a valid result so we can savely
  45. test with -4095. */
  46. /* We don't want the label for the error handle to be global when we define
  47. it here. */
  48. #ifdef __PIC__
  49. # define SYSCALL_ERROR_LABEL 0f
  50. #else
  51. # define SYSCALL_ERROR_LABEL syscall_error
  52. #endif
  53. #undef PSEUDO
  54. #define PSEUDO(name, syscall_name, args) \
  55. .text; \
  56. ENTRY (name) \
  57. DO_CALL (syscall_name, args); \
  58. cmpl $-4095, %eax; \
  59. jae SYSCALL_ERROR_LABEL; \
  60. L(pseudo_end):
  61. #undef PSEUDO_END
  62. #define PSEUDO_END(name) \
  63. SYSCALL_ERROR_HANDLER \
  64. END (name)
  65. #undef PSEUDO_NOERRNO
  66. #define PSEUDO_NOERRNO(name, syscall_name, args) \
  67. .text; \
  68. ENTRY (name) \
  69. DO_CALL (syscall_name, args)
  70. #undef PSEUDO_END_NOERRNO
  71. #define PSEUDO_END_NOERRNO(name) \
  72. END (name)
  73. #define ret_NOERRNO ret
  74. /* The function has to return the error code. */
  75. #undef PSEUDO_ERRVAL
  76. #define PSEUDO_ERRVAL(name, syscall_name, args) \
  77. .text; \
  78. ENTRY (name) \
  79. DO_CALL (syscall_name, args); \
  80. negl %eax
  81. #undef PSEUDO_END_ERRVAL
  82. #define PSEUDO_END_ERRVAL(name) \
  83. END (name)
  84. #define ret_ERRVAL ret
  85. #ifndef __PIC__
  86. # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
  87. #else
  88. # ifdef RTLD_PRIVATE_ERRNO
  89. # define SYSCALL_ERROR_HANDLER \
  90. 0:SETUP_PIC_REG(cx); \
  91. addl $_GLOBAL_OFFSET_TABLE_, %ecx; \
  92. xorl %edx, %edx; \
  93. subl %eax, %edx; \
  94. movl %edx, rtld_errno@GOTOFF(%ecx); \
  95. orl $-1, %eax; \
  96. jmp L(pseudo_end);
  97. # elif defined _LIBC_REENTRANT
  98. # if USE___THREAD
  99. # ifndef NOT_IN_libc
  100. # define SYSCALL_ERROR_ERRNO __libc_errno
  101. # else
  102. # define SYSCALL_ERROR_ERRNO errno
  103. # endif
  104. # define SYSCALL_ERROR_HANDLER \
  105. 0:SETUP_PIC_REG (cx); \
  106. addl $_GLOBAL_OFFSET_TABLE_, %ecx; \
  107. movl SYSCALL_ERROR_ERRNO@GOTNTPOFF(%ecx), %ecx; \
  108. xorl %edx, %edx; \
  109. subl %eax, %edx; \
  110. SYSCALL_ERROR_HANDLER_TLS_STORE (%edx, %ecx); \
  111. orl $-1, %eax; \
  112. jmp L(pseudo_end);
  113. # ifndef NO_TLS_DIRECT_SEG_REFS
  114. # define SYSCALL_ERROR_HANDLER_TLS_STORE(src, destoff) \
  115. movl src, %gs:(destoff)
  116. # else
  117. # define SYSCALL_ERROR_HANDLER_TLS_STORE(src, destoff) \
  118. addl %gs:0, destoff; \
  119. movl src, (destoff)
  120. # endif
  121. # else
  122. # define SYSCALL_ERROR_HANDLER \
  123. 0:pushl %ebx; \
  124. cfi_adjust_cfa_offset (4); \
  125. cfi_rel_offset (ebx, 0); \
  126. SETUP_PIC_REG (bx); \
  127. addl $_GLOBAL_OFFSET_TABLE_, %ebx; \
  128. xorl %edx, %edx; \
  129. subl %eax, %edx; \
  130. pushl %edx; \
  131. cfi_adjust_cfa_offset (4); \
  132. call __errno_location@PLT; \
  133. popl %ecx; \
  134. cfi_adjust_cfa_offset (-4); \
  135. popl %ebx; \
  136. cfi_adjust_cfa_offset (-4); \
  137. cfi_restore (ebx); \
  138. movl %ecx, (%eax); \
  139. orl $-1, %eax; \
  140. jmp L(pseudo_end);
  141. /* A quick note: it is assumed that the call to `__errno_location' does
  142. not modify the stack! */
  143. # endif
  144. # else
  145. /* Store (- %eax) into errno through the GOT. */
  146. # define SYSCALL_ERROR_HANDLER \
  147. 0:SETUP_PIC_REG(cx); \
  148. addl $_GLOBAL_OFFSET_TABLE_, %ecx; \
  149. xorl %edx, %edx; \
  150. subl %eax, %edx; \
  151. movl errno@GOT(%ecx), %ecx; \
  152. movl %edx, (%ecx); \
  153. orl $-1, %eax; \
  154. jmp L(pseudo_end);
  155. # endif /* _LIBC_REENTRANT */
  156. #endif /* __PIC__ */
  157. /* The original calling convention for system calls on Linux/i386 is
  158. to use int $0x80. */
  159. #ifdef I386_USE_SYSENTER
  160. # ifdef SHARED
  161. # define ENTER_KERNEL call *%gs:SYSINFO_OFFSET
  162. # else
  163. # define ENTER_KERNEL call *_dl_sysinfo
  164. # endif
  165. #else
  166. # define ENTER_KERNEL int $0x80
  167. #endif
  168. /* Linux takes system call arguments in registers:
  169. syscall number %eax call-clobbered
  170. arg 1 %ebx call-saved
  171. arg 2 %ecx call-clobbered
  172. arg 3 %edx call-clobbered
  173. arg 4 %esi call-saved
  174. arg 5 %edi call-saved
  175. arg 6 %ebp call-saved
  176. The stack layout upon entering the function is:
  177. 24(%esp) Arg# 6
  178. 20(%esp) Arg# 5
  179. 16(%esp) Arg# 4
  180. 12(%esp) Arg# 3
  181. 8(%esp) Arg# 2
  182. 4(%esp) Arg# 1
  183. (%esp) Return address
  184. (Of course a function with say 3 arguments does not have entries for
  185. arguments 4, 5, and 6.)
  186. The following code tries hard to be optimal. A general assumption
  187. (which is true according to the data books I have) is that
  188. 2 * xchg is more expensive than pushl + movl + popl
  189. Beside this a neat trick is used. The calling conventions for Linux
  190. tell that among the registers used for parameters %ecx and %edx need
  191. not be saved. Beside this we may clobber this registers even when
  192. they are not used for parameter passing.
  193. As a result one can see below that we save the content of the %ebx
  194. register in the %edx register when we have less than 3 arguments
  195. (2 * movl is less expensive than pushl + popl).
  196. Second unlike for the other registers we don't save the content of
  197. %ecx and %edx when we have more than 1 and 2 registers resp.
  198. The code below might look a bit long but we have to take care for
  199. the pipelined processors (i586). Here the `pushl' and `popl'
  200. instructions are marked as NP (not pairable) but the exception is
  201. two consecutive of these instruction. This gives no penalty on
  202. other processors though. */
  203. #undef DO_CALL
  204. #define DO_CALL(syscall_name, args) \
  205. PUSHARGS_##args \
  206. DOARGS_##args \
  207. movl $SYS_ify (syscall_name), %eax; \
  208. ENTER_KERNEL \
  209. POPARGS_##args
  210. #define PUSHARGS_0 /* No arguments to push. */
  211. #define DOARGS_0 /* No arguments to frob. */
  212. #define POPARGS_0 /* No arguments to pop. */
  213. #define _PUSHARGS_0 /* No arguments to push. */
  214. #define _DOARGS_0(n) /* No arguments to frob. */
  215. #define _POPARGS_0 /* No arguments to pop. */
  216. #define PUSHARGS_1 movl %ebx, %edx; L(SAVEBX1): PUSHARGS_0
  217. #define DOARGS_1 _DOARGS_1 (4)
  218. #define POPARGS_1 POPARGS_0; movl %edx, %ebx; L(RESTBX1):
  219. #define _PUSHARGS_1 pushl %ebx; cfi_adjust_cfa_offset (4); \
  220. cfi_rel_offset (ebx, 0); L(PUSHBX1): _PUSHARGS_0
  221. #define _DOARGS_1(n) movl n(%esp), %ebx; _DOARGS_0(n-4)
  222. #define _POPARGS_1 _POPARGS_0; popl %ebx; cfi_adjust_cfa_offset (-4); \
  223. cfi_restore (ebx); L(POPBX1):
  224. #define PUSHARGS_2 PUSHARGS_1
  225. #define DOARGS_2 _DOARGS_2 (8)
  226. #define POPARGS_2 POPARGS_1
  227. #define _PUSHARGS_2 _PUSHARGS_1
  228. #define _DOARGS_2(n) movl n(%esp), %ecx; _DOARGS_1 (n-4)
  229. #define _POPARGS_2 _POPARGS_1
  230. #define PUSHARGS_3 _PUSHARGS_2
  231. #define DOARGS_3 _DOARGS_3 (16)
  232. #define POPARGS_3 _POPARGS_3
  233. #define _PUSHARGS_3 _PUSHARGS_2
  234. #define _DOARGS_3(n) movl n(%esp), %edx; _DOARGS_2 (n-4)
  235. #define _POPARGS_3 _POPARGS_2
  236. #define PUSHARGS_4 _PUSHARGS_4
  237. #define DOARGS_4 _DOARGS_4 (24)
  238. #define POPARGS_4 _POPARGS_4
  239. #define _PUSHARGS_4 pushl %esi; cfi_adjust_cfa_offset (4); \
  240. cfi_rel_offset (esi, 0); L(PUSHSI1): _PUSHARGS_3
  241. #define _DOARGS_4(n) movl n(%esp), %esi; _DOARGS_3 (n-4)
  242. #define _POPARGS_4 _POPARGS_3; popl %esi; cfi_adjust_cfa_offset (-4); \
  243. cfi_restore (esi); L(POPSI1):
  244. #define PUSHARGS_5 _PUSHARGS_5
  245. #define DOARGS_5 _DOARGS_5 (32)
  246. #define POPARGS_5 _POPARGS_5
  247. #define _PUSHARGS_5 pushl %edi; cfi_adjust_cfa_offset (4); \
  248. cfi_rel_offset (edi, 0); L(PUSHDI1): _PUSHARGS_4
  249. #define _DOARGS_5(n) movl n(%esp), %edi; _DOARGS_4 (n-4)
  250. #define _POPARGS_5 _POPARGS_4; popl %edi; cfi_adjust_cfa_offset (-4); \
  251. cfi_restore (edi); L(POPDI1):
  252. #define PUSHARGS_6 _PUSHARGS_6
  253. #define DOARGS_6 _DOARGS_6 (40)
  254. #define POPARGS_6 _POPARGS_6
  255. #define _PUSHARGS_6 pushl %ebp; cfi_adjust_cfa_offset (4); \
  256. cfi_rel_offset (ebp, 0); L(PUSHBP1): _PUSHARGS_5
  257. #define _DOARGS_6(n) movl n(%esp), %ebp; _DOARGS_5 (n-4)
  258. #define _POPARGS_6 _POPARGS_5; popl %ebp; cfi_adjust_cfa_offset (-4); \
  259. cfi_restore (ebp); L(POPBP1):
  260. #endif /* __ASSEMBLER__ */
  261. /* Pointer mangling support. */
  262. #if defined NOT_IN_libc && defined IS_IN_rtld
  263. /* We cannot use the thread descriptor because in ld.so we use setjmp
  264. earlier than the descriptor is initialized. Using a global variable
  265. is too complicated here since we have no PC-relative addressing mode. */
  266. #else
  267. # ifdef __ASSEMBLER__
  268. # define PTR_MANGLE(reg) xorl %gs:POINTER_GUARD, reg; \
  269. roll $9, reg
  270. # define PTR_DEMANGLE(reg) rorl $9, reg; \
  271. xorl %gs:POINTER_GUARD, reg
  272. # else
  273. # define PTR_MANGLE(var) asm ("xorl %%gs:%c2, %0\n" \
  274. "roll $9, %0" \
  275. : "=r" (var) \
  276. : "0" (var), \
  277. "i" (offsetof (tcbhead_t, \
  278. pointer_guard)))
  279. # define PTR_DEMANGLE(var) asm ("rorl $9, %0\n" \
  280. "xorl %%gs:%c2, %0" \
  281. : "=r" (var) \
  282. : "0" (var), \
  283. "i" (offsetof (tcbhead_t, \
  284. pointer_guard)))
  285. # endif
  286. #endif
  287. #endif /* linux/i386/sysdep.h */