sysdep.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. # ifndef NOT_IN_libc
  157. # define SYSCALL_ERROR_ERRNO __libc_errno
  158. # else
  159. # define SYSCALL_ERROR_ERRNO errno
  160. # endif
  161. # define SYSCALL_ERROR_HANDLER \
  162. 0:SETUP_PIC_REG (cx); \
  163. addl $_GLOBAL_OFFSET_TABLE_, %ecx; \
  164. movl SYSCALL_ERROR_ERRNO@GOTNTPOFF(%ecx), %ecx; \
  165. xorl %edx, %edx; \
  166. subl %eax, %edx; \
  167. SYSCALL_ERROR_HANDLER_TLS_STORE (%edx, %ecx); \
  168. orl $-1, %eax; \
  169. jmp L(pseudo_end);
  170. # ifndef NO_TLS_DIRECT_SEG_REFS
  171. # define SYSCALL_ERROR_HANDLER_TLS_STORE(src, destoff) \
  172. movl src, %gs:(destoff)
  173. # else
  174. # define SYSCALL_ERROR_HANDLER_TLS_STORE(src, destoff) \
  175. addl %gs:0, destoff; \
  176. movl src, (destoff)
  177. # endif
  178. # else
  179. # define SYSCALL_ERROR_HANDLER \
  180. 0:pushl %ebx; \
  181. cfi_adjust_cfa_offset (4); \
  182. cfi_rel_offset (ebx, 0); \
  183. SETUP_PIC_REG (bx); \
  184. addl $_GLOBAL_OFFSET_TABLE_, %ebx; \
  185. xorl %edx, %edx; \
  186. subl %eax, %edx; \
  187. pushl %edx; \
  188. cfi_adjust_cfa_offset (4); \
  189. call __errno_location@PLT; \
  190. popl %ecx; \
  191. cfi_adjust_cfa_offset (-4); \
  192. popl %ebx; \
  193. cfi_adjust_cfa_offset (-4); \
  194. cfi_restore (ebx); \
  195. movl %ecx, (%eax); \
  196. orl $-1, %eax; \
  197. jmp L(pseudo_end);
  198. /* A quick note: it is assumed that the call to `__errno_location' does
  199. not modify the stack! */
  200. # endif
  201. # else
  202. /* Store (- %eax) into errno through the GOT. */
  203. # define SYSCALL_ERROR_HANDLER \
  204. 0:SETUP_PIC_REG(cx); \
  205. addl $_GLOBAL_OFFSET_TABLE_, %ecx; \
  206. xorl %edx, %edx; \
  207. subl %eax, %edx; \
  208. movl errno@GOT(%ecx), %ecx; \
  209. movl %edx, (%ecx); \
  210. orl $-1, %eax; \
  211. jmp L(pseudo_end);
  212. # endif /* _LIBC_REENTRANT */
  213. #endif /* __PIC__ */
  214. /* The original calling convention for system calls on Linux/i386 is
  215. to use int $0x80. */
  216. #ifdef I386_USE_SYSENTER
  217. # ifdef SHARED
  218. # define ENTER_KERNEL call *%gs:SYSINFO_OFFSET
  219. # else
  220. # define ENTER_KERNEL call *_dl_sysinfo
  221. # endif
  222. #else
  223. # define ENTER_KERNEL int $0x80
  224. #endif
  225. /* Linux takes system call arguments in registers:
  226. syscall number %eax call-clobbered
  227. arg 1 %ebx call-saved
  228. arg 2 %ecx call-clobbered
  229. arg 3 %edx call-clobbered
  230. arg 4 %esi call-saved
  231. arg 5 %edi call-saved
  232. arg 6 %ebp call-saved
  233. The stack layout upon entering the function is:
  234. 24(%esp) Arg# 6
  235. 20(%esp) Arg# 5
  236. 16(%esp) Arg# 4
  237. 12(%esp) Arg# 3
  238. 8(%esp) Arg# 2
  239. 4(%esp) Arg# 1
  240. (%esp) Return address
  241. (Of course a function with say 3 arguments does not have entries for
  242. arguments 4, 5, and 6.)
  243. The following code tries hard to be optimal. A general assumption
  244. (which is true according to the data books I have) is that
  245. 2 * xchg is more expensive than pushl + movl + popl
  246. Beside this a neat trick is used. The calling conventions for Linux
  247. tell that among the registers used for parameters %ecx and %edx need
  248. not be saved. Beside this we may clobber this registers even when
  249. they are not used for parameter passing.
  250. As a result one can see below that we save the content of the %ebx
  251. register in the %edx register when we have less than 3 arguments
  252. (2 * movl is less expensive than pushl + popl).
  253. Second unlike for the other registers we don't save the content of
  254. %ecx and %edx when we have more than 1 and 2 registers resp.
  255. The code below might look a bit long but we have to take care for
  256. the pipelined processors (i586). Here the `pushl' and `popl'
  257. instructions are marked as NP (not pairable) but the exception is
  258. two consecutive of these instruction. This gives no penalty on
  259. other processors though. */
  260. #undef DO_CALL
  261. #define DO_CALL(syscall_name, args) \
  262. PUSHARGS_##args \
  263. DOARGS_##args \
  264. movl $SYS_ify (syscall_name), %eax; \
  265. ENTER_KERNEL \
  266. POPARGS_##args
  267. #define PUSHARGS_0 /* No arguments to push. */
  268. #define DOARGS_0 /* No arguments to frob. */
  269. #define POPARGS_0 /* No arguments to pop. */
  270. #define _PUSHARGS_0 /* No arguments to push. */
  271. #define _DOARGS_0(n) /* No arguments to frob. */
  272. #define _POPARGS_0 /* No arguments to pop. */
  273. #define PUSHARGS_1 movl %ebx, %edx; L(SAVEBX1): PUSHARGS_0
  274. #define DOARGS_1 _DOARGS_1 (4)
  275. #define POPARGS_1 POPARGS_0; movl %edx, %ebx; L(RESTBX1):
  276. #define _PUSHARGS_1 pushl %ebx; cfi_adjust_cfa_offset (4); \
  277. cfi_rel_offset (ebx, 0); L(PUSHBX1): _PUSHARGS_0
  278. #define _DOARGS_1(n) movl n(%esp), %ebx; _DOARGS_0(n-4)
  279. #define _POPARGS_1 _POPARGS_0; popl %ebx; cfi_adjust_cfa_offset (-4); \
  280. cfi_restore (ebx); L(POPBX1):
  281. #define PUSHARGS_2 PUSHARGS_1
  282. #define DOARGS_2 _DOARGS_2 (8)
  283. #define POPARGS_2 POPARGS_1
  284. #define _PUSHARGS_2 _PUSHARGS_1
  285. #define _DOARGS_2(n) movl n(%esp), %ecx; _DOARGS_1 (n-4)
  286. #define _POPARGS_2 _POPARGS_1
  287. #define PUSHARGS_3 _PUSHARGS_2
  288. #define DOARGS_3 _DOARGS_3 (16)
  289. #define POPARGS_3 _POPARGS_3
  290. #define _PUSHARGS_3 _PUSHARGS_2
  291. #define _DOARGS_3(n) movl n(%esp), %edx; _DOARGS_2 (n-4)
  292. #define _POPARGS_3 _POPARGS_2
  293. #define PUSHARGS_4 _PUSHARGS_4
  294. #define DOARGS_4 _DOARGS_4 (24)
  295. #define POPARGS_4 _POPARGS_4
  296. #define _PUSHARGS_4 pushl %esi; cfi_adjust_cfa_offset (4); \
  297. cfi_rel_offset (esi, 0); L(PUSHSI1): _PUSHARGS_3
  298. #define _DOARGS_4(n) movl n(%esp), %esi; _DOARGS_3 (n-4)
  299. #define _POPARGS_4 _POPARGS_3; popl %esi; cfi_adjust_cfa_offset (-4); \
  300. cfi_restore (esi); L(POPSI1):
  301. #define PUSHARGS_5 _PUSHARGS_5
  302. #define DOARGS_5 _DOARGS_5 (32)
  303. #define POPARGS_5 _POPARGS_5
  304. #define _PUSHARGS_5 pushl %edi; cfi_adjust_cfa_offset (4); \
  305. cfi_rel_offset (edi, 0); L(PUSHDI1): _PUSHARGS_4
  306. #define _DOARGS_5(n) movl n(%esp), %edi; _DOARGS_4 (n-4)
  307. #define _POPARGS_5 _POPARGS_4; popl %edi; cfi_adjust_cfa_offset (-4); \
  308. cfi_restore (edi); L(POPDI1):
  309. #define PUSHARGS_6 _PUSHARGS_6
  310. #define DOARGS_6 _DOARGS_6 (40)
  311. #define POPARGS_6 _POPARGS_6
  312. #define _PUSHARGS_6 pushl %ebp; cfi_adjust_cfa_offset (4); \
  313. cfi_rel_offset (ebp, 0); L(PUSHBP1): _PUSHARGS_5
  314. #define _DOARGS_6(n) movl n(%esp), %ebp; _DOARGS_5 (n-4)
  315. #define _POPARGS_6 _POPARGS_5; popl %ebp; cfi_adjust_cfa_offset (-4); \
  316. cfi_restore (ebp); L(POPBP1):
  317. #endif /* __ASSEMBLER__ */
  318. #endif /* linux/i386/sysdep.h */