sysdep.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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, see
  15. <http://www.gnu.org/licenses/>. */
  16. #ifndef _LINUX_I386_SYSDEP_H
  17. #define _LINUX_I386_SYSDEP_H 1
  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. There is currently a bug in gdb which prevents us from specifying
  32. incomplete stabs information. Fake some entries here which specify
  33. the current source file. */
  34. #define ENTRY(name) \
  35. .globl C_SYMBOL_NAME(name); \
  36. ASM_TYPE_DIRECTIVE (C_SYMBOL_NAME(name),@function) \
  37. .align ALIGNARG(4); \
  38. C_LABEL(name) \
  39. cfi_startproc;
  40. #undef END
  41. #define END(name) \
  42. cfi_endproc; \
  43. ASM_SIZE_DIRECTIVE(name) \
  44. #ifdef NO_UNDERSCORES
  45. /* Since C identifiers are not normally prefixed with an underscore
  46. on this system, the asm identifier `syscall_error' intrudes on the
  47. C name space. Make sure we use an innocuous name. */
  48. #define syscall_error __syscall_error
  49. #define mcount _mcount
  50. #endif
  51. #undef JUMPTARGET
  52. #ifdef __PIC__
  53. #define JUMPTARGET(name) name##@PLT
  54. #define SYSCALL_PIC_SETUP \
  55. pushl %ebx; \
  56. cfi_adjust_cfa_offset (4); \
  57. call 0f; \
  58. 0: popl %ebx; \
  59. cfi_adjust_cfa_offset (-4); \
  60. addl $_GLOBAL_OFFSET_TABLE+[.-0b], %ebx;
  61. # define SETUP_PIC_REG(reg) \
  62. .ifndef __x86.get_pc_thunk.reg; \
  63. .section .gnu.linkonce.t.__x86.get_pc_thunk.reg,"ax",@progbits; \
  64. .globl __x86.get_pc_thunk.reg; \
  65. .hidden __x86.get_pc_thunk.reg; \
  66. .type __x86.get_pc_thunk.reg,@function; \
  67. __x86.get_pc_thunk.reg: \
  68. movl (%esp), %e##reg; \
  69. ret; \
  70. .size __x86.get_pc_thunk.reg, . - __x86.get_pc_thunk.reg; \
  71. .previous; \
  72. .endif; \
  73. call __x86.get_pc_thunk.reg
  74. # define LOAD_PIC_REG(reg) \
  75. SETUP_PIC_REG(reg); addl $_GLOBAL_OFFSET_TABLE_, %e##reg
  76. #else
  77. #define JUMPTARGET(name) name
  78. #define SYSCALL_PIC_SETUP /* Nothing. */
  79. #endif
  80. /* Local label name for asm code. */
  81. #ifndef L
  82. #ifdef HAVE_ELF
  83. #define L(name) .L##name
  84. #else
  85. #define L(name) name
  86. #endif
  87. #endif
  88. /* Avoid conflics with thunk section */
  89. #undef __i686
  90. #endif /* __ASSEMBLER__ */
  91. /* For Linux we can use the system call table in the header file
  92. /usr/include/asm/unistd.h
  93. of the kernel. But these symbols do not follow the SYS_* syntax
  94. so we have to redefine the `SYS_ify' macro here. */
  95. #undef SYS_ify
  96. #define SYS_ify(syscall_name) __NR_##syscall_name
  97. #if defined USE_DL_SYSINFO \
  98. && (!defined NOT_IN_libc || defined IS_IN_libpthread)
  99. # define I386_USE_SYSENTER 1
  100. #else
  101. # undef I386_USE_SYSENTER
  102. #endif
  103. #ifdef __ASSEMBLER__
  104. /* Linux uses a negative return value to indicate syscall errors,
  105. unlike most Unices, which use the condition codes' carry flag.
  106. Since version 2.1 the return value of a system call might be
  107. negative even if the call succeeded. E.g., the `lseek' system call
  108. might return a large offset. Therefore we must not anymore test
  109. for < 0, but test for a real error by making sure the value in %eax
  110. is a real error number. Linus said he will make sure the no syscall
  111. returns a value in -1 .. -4095 as a valid result so we can savely
  112. test with -4095. */
  113. /* We don't want the label for the error handle to be global when we define
  114. it here. */
  115. #ifdef __PIC__
  116. # define SYSCALL_ERROR_LABEL 0f
  117. #else
  118. # define SYSCALL_ERROR_LABEL syscall_error
  119. #endif
  120. #undef PSEUDO
  121. #define PSEUDO(name, syscall_name, args) \
  122. .text; \
  123. ENTRY (name) \
  124. DO_CALL (syscall_name, args); \
  125. cmpl $-4095, %eax; \
  126. jae SYSCALL_ERROR_LABEL; \
  127. L(pseudo_end):
  128. #undef PSEUDO_END
  129. #define PSEUDO_END(name) \
  130. SYSCALL_ERROR_HANDLER \
  131. END (name)
  132. #undef PSEUDO_NOERRNO
  133. #define PSEUDO_NOERRNO(name, syscall_name, args) \
  134. .text; \
  135. ENTRY (name) \
  136. DO_CALL (syscall_name, args)
  137. #undef PSEUDO_END_NOERRNO
  138. #define PSEUDO_END_NOERRNO(name) \
  139. END (name)
  140. #define ret_NOERRNO ret
  141. /* The function has to return the error code. */
  142. #undef PSEUDO_ERRVAL
  143. #define PSEUDO_ERRVAL(name, syscall_name, args) \
  144. .text; \
  145. ENTRY (name) \
  146. DO_CALL (syscall_name, args); \
  147. negl %eax
  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. #else
  154. # if defined _LIBC_REENTRANT
  155. # if defined USE___THREAD
  156. # define SYSCALL_ERROR_ERRNO errno
  157. # define SYSCALL_ERROR_HANDLER \
  158. 0:SETUP_PIC_REG (cx); \
  159. addl $_GLOBAL_OFFSET_TABLE_, %ecx; \
  160. movl SYSCALL_ERROR_ERRNO@GOTNTPOFF(%ecx), %ecx; \
  161. xorl %edx, %edx; \
  162. subl %eax, %edx; \
  163. SYSCALL_ERROR_HANDLER_TLS_STORE (%edx, %ecx); \
  164. orl $-1, %eax; \
  165. jmp L(pseudo_end);
  166. # ifndef NO_TLS_DIRECT_SEG_REFS
  167. # define SYSCALL_ERROR_HANDLER_TLS_STORE(src, destoff) \
  168. movl src, %gs:(destoff)
  169. # else
  170. # define SYSCALL_ERROR_HANDLER_TLS_STORE(src, destoff) \
  171. addl %gs:0, destoff; \
  172. movl src, (destoff)
  173. # endif
  174. # else
  175. # define SYSCALL_ERROR_HANDLER \
  176. 0:pushl %ebx; \
  177. cfi_adjust_cfa_offset (4); \
  178. cfi_rel_offset (ebx, 0); \
  179. SETUP_PIC_REG (bx); \
  180. addl $_GLOBAL_OFFSET_TABLE_, %ebx; \
  181. xorl %edx, %edx; \
  182. subl %eax, %edx; \
  183. pushl %edx; \
  184. cfi_adjust_cfa_offset (4); \
  185. call __errno_location@PLT; \
  186. popl %ecx; \
  187. cfi_adjust_cfa_offset (-4); \
  188. popl %ebx; \
  189. cfi_adjust_cfa_offset (-4); \
  190. cfi_restore (ebx); \
  191. movl %ecx, (%eax); \
  192. orl $-1, %eax; \
  193. jmp L(pseudo_end);
  194. /* A quick note: it is assumed that the call to `__errno_location' does
  195. not modify the stack! */
  196. # endif
  197. # else
  198. /* Store (- %eax) into errno through the GOT. */
  199. # define SYSCALL_ERROR_HANDLER \
  200. 0:SETUP_PIC_REG(cx); \
  201. addl $_GLOBAL_OFFSET_TABLE_, %ecx; \
  202. xorl %edx, %edx; \
  203. subl %eax, %edx; \
  204. movl errno@GOT(%ecx), %ecx; \
  205. movl %edx, (%ecx); \
  206. orl $-1, %eax; \
  207. jmp L(pseudo_end);
  208. # endif /* _LIBC_REENTRANT */
  209. #endif /* __PIC__ */
  210. /* The original calling convention for system calls on Linux/i386 is
  211. to use int $0x80. */
  212. #ifdef I386_USE_SYSENTER
  213. # ifdef SHARED
  214. # define ENTER_KERNEL call *%gs:SYSINFO_OFFSET
  215. # else
  216. # define ENTER_KERNEL call *_dl_sysinfo
  217. # endif
  218. #else
  219. # define ENTER_KERNEL int $0x80
  220. #endif
  221. /* Linux takes system call arguments in registers:
  222. syscall number %eax call-clobbered
  223. arg 1 %ebx call-saved
  224. arg 2 %ecx call-clobbered
  225. arg 3 %edx call-clobbered
  226. arg 4 %esi call-saved
  227. arg 5 %edi call-saved
  228. arg 6 %ebp call-saved
  229. The stack layout upon entering the function is:
  230. 24(%esp) Arg# 6
  231. 20(%esp) Arg# 5
  232. 16(%esp) Arg# 4
  233. 12(%esp) Arg# 3
  234. 8(%esp) Arg# 2
  235. 4(%esp) Arg# 1
  236. (%esp) Return address
  237. (Of course a function with say 3 arguments does not have entries for
  238. arguments 4, 5, and 6.)
  239. The following code tries hard to be optimal. A general assumption
  240. (which is true according to the data books I have) is that
  241. 2 * xchg is more expensive than pushl + movl + popl
  242. Beside this a neat trick is used. The calling conventions for Linux
  243. tell that among the registers used for parameters %ecx and %edx need
  244. not be saved. Beside this we may clobber this registers even when
  245. they are not used for parameter passing.
  246. As a result one can see below that we save the content of the %ebx
  247. register in the %edx register when we have less than 3 arguments
  248. (2 * movl is less expensive than pushl + popl).
  249. Second unlike for the other registers we don't save the content of
  250. %ecx and %edx when we have more than 1 and 2 registers resp.
  251. The code below might look a bit long but we have to take care for
  252. the pipelined processors (i586). Here the `pushl' and `popl'
  253. instructions are marked as NP (not pairable) but the exception is
  254. two consecutive of these instruction. This gives no penalty on
  255. other processors though. */
  256. #undef DO_CALL
  257. #define DO_CALL(syscall_name, args) \
  258. PUSHARGS_##args \
  259. DOARGS_##args \
  260. movl $SYS_ify (syscall_name), %eax; \
  261. ENTER_KERNEL \
  262. POPARGS_##args
  263. #define PUSHARGS_0 /* No arguments to push. */
  264. #define DOARGS_0 /* No arguments to frob. */
  265. #define POPARGS_0 /* No arguments to pop. */
  266. #define _PUSHARGS_0 /* No arguments to push. */
  267. #define _DOARGS_0(n) /* No arguments to frob. */
  268. #define _POPARGS_0 /* No arguments to pop. */
  269. #define PUSHARGS_1 movl %ebx, %edx; L(SAVEBX1): PUSHARGS_0
  270. #define DOARGS_1 _DOARGS_1 (4)
  271. #define POPARGS_1 POPARGS_0; movl %edx, %ebx; L(RESTBX1):
  272. #define _PUSHARGS_1 pushl %ebx; cfi_adjust_cfa_offset (4); \
  273. cfi_rel_offset (ebx, 0); L(PUSHBX1): _PUSHARGS_0
  274. #define _DOARGS_1(n) movl n(%esp), %ebx; _DOARGS_0(n-4)
  275. #define _POPARGS_1 _POPARGS_0; popl %ebx; cfi_adjust_cfa_offset (-4); \
  276. cfi_restore (ebx); L(POPBX1):
  277. #define PUSHARGS_2 PUSHARGS_1
  278. #define DOARGS_2 _DOARGS_2 (8)
  279. #define POPARGS_2 POPARGS_1
  280. #define _PUSHARGS_2 _PUSHARGS_1
  281. #define _DOARGS_2(n) movl n(%esp), %ecx; _DOARGS_1 (n-4)
  282. #define _POPARGS_2 _POPARGS_1
  283. #define PUSHARGS_3 _PUSHARGS_2
  284. #define DOARGS_3 _DOARGS_3 (16)
  285. #define POPARGS_3 _POPARGS_3
  286. #define _PUSHARGS_3 _PUSHARGS_2
  287. #define _DOARGS_3(n) movl n(%esp), %edx; _DOARGS_2 (n-4)
  288. #define _POPARGS_3 _POPARGS_2
  289. #define PUSHARGS_4 _PUSHARGS_4
  290. #define DOARGS_4 _DOARGS_4 (24)
  291. #define POPARGS_4 _POPARGS_4
  292. #define _PUSHARGS_4 pushl %esi; cfi_adjust_cfa_offset (4); \
  293. cfi_rel_offset (esi, 0); L(PUSHSI1): _PUSHARGS_3
  294. #define _DOARGS_4(n) movl n(%esp), %esi; _DOARGS_3 (n-4)
  295. #define _POPARGS_4 _POPARGS_3; popl %esi; cfi_adjust_cfa_offset (-4); \
  296. cfi_restore (esi); L(POPSI1):
  297. #define PUSHARGS_5 _PUSHARGS_5
  298. #define DOARGS_5 _DOARGS_5 (32)
  299. #define POPARGS_5 _POPARGS_5
  300. #define _PUSHARGS_5 pushl %edi; cfi_adjust_cfa_offset (4); \
  301. cfi_rel_offset (edi, 0); L(PUSHDI1): _PUSHARGS_4
  302. #define _DOARGS_5(n) movl n(%esp), %edi; _DOARGS_4 (n-4)
  303. #define _POPARGS_5 _POPARGS_4; popl %edi; cfi_adjust_cfa_offset (-4); \
  304. cfi_restore (edi); L(POPDI1):
  305. #define PUSHARGS_6 _PUSHARGS_6
  306. #define DOARGS_6 _DOARGS_6 (40)
  307. #define POPARGS_6 _POPARGS_6
  308. #define _PUSHARGS_6 pushl %ebp; cfi_adjust_cfa_offset (4); \
  309. cfi_rel_offset (ebp, 0); L(PUSHBP1): _PUSHARGS_5
  310. #define _DOARGS_6(n) movl n(%esp), %ebp; _DOARGS_5 (n-4)
  311. #define _POPARGS_6 _POPARGS_5; popl %ebp; cfi_adjust_cfa_offset (-4); \
  312. cfi_restore (ebp); L(POPBP1):
  313. #endif /* __ASSEMBLER__ */
  314. #endif /* linux/i386/sysdep.h */