sysdep.h 13 KB

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