sysdep.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 <common/sysdep.h>
  21. #ifdef __ASSEMBLER__
  22. /* Syntactic details of assembler. */
  23. /* ELF uses byte-counts for .align, most others use log2 of count of bytes. */
  24. #define ALIGNARG(log2) 1<<log2
  25. /* For ELF we need the `.type' directive to make shared libs work right. */
  26. #define ASM_TYPE_DIRECTIVE(name,typearg) .type name,typearg;
  27. #define ASM_SIZE_DIRECTIVE(name) .size name,.-name;
  28. /* In ELF C symbols are asm symbols. */
  29. #undef NO_UNDERSCORES
  30. #define NO_UNDERSCORES
  31. /* Define an entry point visible from C.
  32. There is currently a bug in gdb which prevents us from specifying
  33. incomplete stabs information. Fake some entries here which specify
  34. the current source file. */
  35. #define ENTRY(name) \
  36. STABS_CURRENT_FILE1("") \
  37. STABS_CURRENT_FILE(name) \
  38. ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME(name); \
  39. ASM_TYPE_DIRECTIVE (C_SYMBOL_NAME(name),@function) \
  40. .align ALIGNARG(4); \
  41. STABS_FUN(name) \
  42. C_LABEL(name) \
  43. cfi_startproc; \
  44. CALL_MCOUNT
  45. #undef END
  46. #define END(name) \
  47. cfi_endproc; \
  48. ASM_SIZE_DIRECTIVE(name) \
  49. STABS_FUN_END(name)
  50. #ifdef HAVE_CPP_ASM_DEBUGINFO
  51. /* Disable that goop, because we just pass -g through to the assembler
  52. and it generates proper line number information directly. */
  53. # define STABS_CURRENT_FILE1(name)
  54. # define STABS_CURRENT_FILE(name)
  55. # define STABS_FUN(name)
  56. # define STABS_FUN_END(name)
  57. #else
  58. /* Remove the following two lines once the gdb bug is fixed. */
  59. #define STABS_CURRENT_FILE(name) \
  60. STABS_CURRENT_FILE1 (#name)
  61. #define STABS_CURRENT_FILE1(name) \
  62. 1: .stabs name,100,0,0,1b;
  63. /* Emit stabs definition lines. We use F(0,1) and define t(0,1) as `int',
  64. the same way gcc does it. */
  65. #define STABS_FUN(name) STABS_FUN2(name, name##:F(0,1))
  66. #define STABS_FUN2(name, namestr) \
  67. .stabs "int:t(0,1)=r(0,1);-2147483648;2147483647;",128,0,0,0; \
  68. .stabs #namestr,36,0,0,name;
  69. #define STABS_FUN_END(name) \
  70. 1: .stabs "",36,0,0,1b-name;
  71. #endif
  72. /* If compiled for profiling, call `mcount' at the start of each function. */
  73. #ifdef PROF
  74. /* The mcount code relies on a normal frame pointer being on the stack
  75. to locate our caller, so push one just for its benefit. */
  76. #define CALL_MCOUNT \
  77. pushl %ebp; cfi_adjust_cfa_offset (4); movl %esp, %ebp; \
  78. cfi_def_cfa_register (ebp); call JUMPTARGET(mcount); \
  79. popl %ebp; cfi_def_cfa (esp, 4);
  80. #else
  81. #define CALL_MCOUNT /* Do nothing. */
  82. #endif
  83. #ifdef NO_UNDERSCORES
  84. /* Since C identifiers are not normally prefixed with an underscore
  85. on this system, the asm identifier `syscall_error' intrudes on the
  86. C name space. Make sure we use an innocuous name. */
  87. #define syscall_error __syscall_error
  88. #define mcount _mcount
  89. #endif
  90. #undef JUMPTARGET
  91. #ifdef __PIC__
  92. #define JUMPTARGET(name) name##@PLT
  93. #define SYSCALL_PIC_SETUP \
  94. pushl %ebx; \
  95. cfi_adjust_cfa_offset (4); \
  96. call 0f; \
  97. 0: popl %ebx; \
  98. cfi_adjust_cfa_offset (-4); \
  99. addl $_GLOBAL_OFFSET_TABLE+[.-0b], %ebx;
  100. # define SETUP_PIC_REG(reg) \
  101. .ifndef __x86.get_pc_thunk.reg; \
  102. .section .gnu.linkonce.t.__x86.get_pc_thunk.reg,"ax",@progbits; \
  103. .globl __x86.get_pc_thunk.reg; \
  104. .hidden __x86.get_pc_thunk.reg; \
  105. .type __x86.get_pc_thunk.reg,@function; \
  106. __x86.get_pc_thunk.reg: \
  107. movl (%esp), %e##reg; \
  108. ret; \
  109. .size __x86.get_pc_thunk.reg, . - __x86.get_pc_thunk.reg; \
  110. .previous; \
  111. .endif; \
  112. call __x86.get_pc_thunk.reg
  113. # define LOAD_PIC_REG(reg) \
  114. SETUP_PIC_REG(reg); addl $_GLOBAL_OFFSET_TABLE_, %e##reg
  115. #else
  116. #define JUMPTARGET(name) name
  117. #define SYSCALL_PIC_SETUP /* Nothing. */
  118. #endif
  119. /* Local label name for asm code. */
  120. #ifndef L
  121. #ifdef HAVE_ELF
  122. #define L(name) .L##name
  123. #else
  124. #define L(name) name
  125. #endif
  126. #endif
  127. /* Avoid conflics with thunk section */
  128. #undef __i686
  129. #endif /* __ASSEMBLER__ */
  130. /* For Linux we can use the system call table in the header file
  131. /usr/include/asm/unistd.h
  132. of the kernel. But these symbols do not follow the SYS_* syntax
  133. so we have to redefine the `SYS_ify' macro here. */
  134. #undef SYS_ify
  135. #define SYS_ify(syscall_name) __NR_##syscall_name
  136. #if defined USE_DL_SYSINFO \
  137. && (!defined NOT_IN_libc || defined IS_IN_libpthread)
  138. # define I386_USE_SYSENTER 1
  139. #else
  140. # undef I386_USE_SYSENTER
  141. #endif
  142. #ifdef __ASSEMBLER__
  143. /* Linux uses a negative return value to indicate syscall errors,
  144. unlike most Unices, which use the condition codes' carry flag.
  145. Since version 2.1 the return value of a system call might be
  146. negative even if the call succeeded. E.g., the `lseek' system call
  147. might return a large offset. Therefore we must not anymore test
  148. for < 0, but test for a real error by making sure the value in %eax
  149. is a real error number. Linus said he will make sure the no syscall
  150. returns a value in -1 .. -4095 as a valid result so we can savely
  151. test with -4095. */
  152. /* We don't want the label for the error handle to be global when we define
  153. it here. */
  154. #ifdef __PIC__
  155. # define SYSCALL_ERROR_LABEL 0f
  156. #else
  157. # define SYSCALL_ERROR_LABEL syscall_error
  158. #endif
  159. #undef PSEUDO
  160. #define PSEUDO(name, syscall_name, args) \
  161. .text; \
  162. ENTRY (name) \
  163. DO_CALL (syscall_name, args); \
  164. cmpl $-4095, %eax; \
  165. jae SYSCALL_ERROR_LABEL; \
  166. L(pseudo_end):
  167. #undef PSEUDO_END
  168. #define PSEUDO_END(name) \
  169. SYSCALL_ERROR_HANDLER \
  170. END (name)
  171. #undef PSEUDO_NOERRNO
  172. #define PSEUDO_NOERRNO(name, syscall_name, args) \
  173. .text; \
  174. ENTRY (name) \
  175. DO_CALL (syscall_name, args)
  176. #undef PSEUDO_END_NOERRNO
  177. #define PSEUDO_END_NOERRNO(name) \
  178. END (name)
  179. #define ret_NOERRNO ret
  180. /* The function has to return the error code. */
  181. #undef PSEUDO_ERRVAL
  182. #define PSEUDO_ERRVAL(name, syscall_name, args) \
  183. .text; \
  184. ENTRY (name) \
  185. DO_CALL (syscall_name, args); \
  186. negl %eax
  187. #undef PSEUDO_END_ERRVAL
  188. #define PSEUDO_END_ERRVAL(name) \
  189. END (name)
  190. #define ret_ERRVAL ret
  191. #ifndef __PIC__
  192. # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
  193. #else
  194. # ifdef RTLD_PRIVATE_ERRNO
  195. # define SYSCALL_ERROR_HANDLER \
  196. 0:SETUP_PIC_REG(cx); \
  197. addl $_GLOBAL_OFFSET_TABLE_, %ecx; \
  198. xorl %edx, %edx; \
  199. subl %eax, %edx; \
  200. movl %edx, rtld_errno@GOTOFF(%ecx); \
  201. orl $-1, %eax; \
  202. jmp L(pseudo_end);
  203. # elif defined _LIBC_REENTRANT
  204. # if defined USE___THREAD
  205. # ifndef NOT_IN_libc
  206. # define SYSCALL_ERROR_ERRNO __libc_errno
  207. # else
  208. # define SYSCALL_ERROR_ERRNO errno
  209. # endif
  210. # define SYSCALL_ERROR_HANDLER \
  211. 0:SETUP_PIC_REG (cx); \
  212. addl $_GLOBAL_OFFSET_TABLE_, %ecx; \
  213. movl SYSCALL_ERROR_ERRNO@GOTNTPOFF(%ecx), %ecx; \
  214. xorl %edx, %edx; \
  215. subl %eax, %edx; \
  216. SYSCALL_ERROR_HANDLER_TLS_STORE (%edx, %ecx); \
  217. orl $-1, %eax; \
  218. jmp L(pseudo_end);
  219. # ifndef NO_TLS_DIRECT_SEG_REFS
  220. # define SYSCALL_ERROR_HANDLER_TLS_STORE(src, destoff) \
  221. movl src, %gs:(destoff)
  222. # else
  223. # define SYSCALL_ERROR_HANDLER_TLS_STORE(src, destoff) \
  224. addl %gs:0, destoff; \
  225. movl src, (destoff)
  226. # endif
  227. # else
  228. # define SYSCALL_ERROR_HANDLER \
  229. 0:pushl %ebx; \
  230. cfi_adjust_cfa_offset (4); \
  231. cfi_rel_offset (ebx, 0); \
  232. SETUP_PIC_REG (bx); \
  233. addl $_GLOBAL_OFFSET_TABLE_, %ebx; \
  234. xorl %edx, %edx; \
  235. subl %eax, %edx; \
  236. pushl %edx; \
  237. cfi_adjust_cfa_offset (4); \
  238. call __errno_location@PLT; \
  239. popl %ecx; \
  240. cfi_adjust_cfa_offset (-4); \
  241. popl %ebx; \
  242. cfi_adjust_cfa_offset (-4); \
  243. cfi_restore (ebx); \
  244. movl %ecx, (%eax); \
  245. orl $-1, %eax; \
  246. jmp L(pseudo_end);
  247. /* A quick note: it is assumed that the call to `__errno_location' does
  248. not modify the stack! */
  249. # endif
  250. # else
  251. /* Store (- %eax) into errno through the GOT. */
  252. # define SYSCALL_ERROR_HANDLER \
  253. 0:SETUP_PIC_REG(cx); \
  254. addl $_GLOBAL_OFFSET_TABLE_, %ecx; \
  255. xorl %edx, %edx; \
  256. subl %eax, %edx; \
  257. movl errno@GOT(%ecx), %ecx; \
  258. movl %edx, (%ecx); \
  259. orl $-1, %eax; \
  260. jmp L(pseudo_end);
  261. # endif /* _LIBC_REENTRANT */
  262. #endif /* __PIC__ */
  263. /* The original calling convention for system calls on Linux/i386 is
  264. to use int $0x80. */
  265. #ifdef I386_USE_SYSENTER
  266. # ifdef SHARED
  267. # define ENTER_KERNEL call *%gs:SYSINFO_OFFSET
  268. # else
  269. # define ENTER_KERNEL call *_dl_sysinfo
  270. # endif
  271. #else
  272. # define ENTER_KERNEL int $0x80
  273. #endif
  274. /* Linux takes system call arguments in registers:
  275. syscall number %eax call-clobbered
  276. arg 1 %ebx call-saved
  277. arg 2 %ecx call-clobbered
  278. arg 3 %edx call-clobbered
  279. arg 4 %esi call-saved
  280. arg 5 %edi call-saved
  281. arg 6 %ebp call-saved
  282. The stack layout upon entering the function is:
  283. 24(%esp) Arg# 6
  284. 20(%esp) Arg# 5
  285. 16(%esp) Arg# 4
  286. 12(%esp) Arg# 3
  287. 8(%esp) Arg# 2
  288. 4(%esp) Arg# 1
  289. (%esp) Return address
  290. (Of course a function with say 3 arguments does not have entries for
  291. arguments 4, 5, and 6.)
  292. The following code tries hard to be optimal. A general assumption
  293. (which is true according to the data books I have) is that
  294. 2 * xchg is more expensive than pushl + movl + popl
  295. Beside this a neat trick is used. The calling conventions for Linux
  296. tell that among the registers used for parameters %ecx and %edx need
  297. not be saved. Beside this we may clobber this registers even when
  298. they are not used for parameter passing.
  299. As a result one can see below that we save the content of the %ebx
  300. register in the %edx register when we have less than 3 arguments
  301. (2 * movl is less expensive than pushl + popl).
  302. Second unlike for the other registers we don't save the content of
  303. %ecx and %edx when we have more than 1 and 2 registers resp.
  304. The code below might look a bit long but we have to take care for
  305. the pipelined processors (i586). Here the `pushl' and `popl'
  306. instructions are marked as NP (not pairable) but the exception is
  307. two consecutive of these instruction. This gives no penalty on
  308. other processors though. */
  309. #undef DO_CALL
  310. #define DO_CALL(syscall_name, args) \
  311. PUSHARGS_##args \
  312. DOARGS_##args \
  313. movl $SYS_ify (syscall_name), %eax; \
  314. ENTER_KERNEL \
  315. POPARGS_##args
  316. #define PUSHARGS_0 /* No arguments to push. */
  317. #define DOARGS_0 /* No arguments to frob. */
  318. #define POPARGS_0 /* No arguments to pop. */
  319. #define _PUSHARGS_0 /* No arguments to push. */
  320. #define _DOARGS_0(n) /* No arguments to frob. */
  321. #define _POPARGS_0 /* No arguments to pop. */
  322. #define PUSHARGS_1 movl %ebx, %edx; L(SAVEBX1): PUSHARGS_0
  323. #define DOARGS_1 _DOARGS_1 (4)
  324. #define POPARGS_1 POPARGS_0; movl %edx, %ebx; L(RESTBX1):
  325. #define _PUSHARGS_1 pushl %ebx; cfi_adjust_cfa_offset (4); \
  326. cfi_rel_offset (ebx, 0); L(PUSHBX1): _PUSHARGS_0
  327. #define _DOARGS_1(n) movl n(%esp), %ebx; _DOARGS_0(n-4)
  328. #define _POPARGS_1 _POPARGS_0; popl %ebx; cfi_adjust_cfa_offset (-4); \
  329. cfi_restore (ebx); L(POPBX1):
  330. #define PUSHARGS_2 PUSHARGS_1
  331. #define DOARGS_2 _DOARGS_2 (8)
  332. #define POPARGS_2 POPARGS_1
  333. #define _PUSHARGS_2 _PUSHARGS_1
  334. #define _DOARGS_2(n) movl n(%esp), %ecx; _DOARGS_1 (n-4)
  335. #define _POPARGS_2 _POPARGS_1
  336. #define PUSHARGS_3 _PUSHARGS_2
  337. #define DOARGS_3 _DOARGS_3 (16)
  338. #define POPARGS_3 _POPARGS_3
  339. #define _PUSHARGS_3 _PUSHARGS_2
  340. #define _DOARGS_3(n) movl n(%esp), %edx; _DOARGS_2 (n-4)
  341. #define _POPARGS_3 _POPARGS_2
  342. #define PUSHARGS_4 _PUSHARGS_4
  343. #define DOARGS_4 _DOARGS_4 (24)
  344. #define POPARGS_4 _POPARGS_4
  345. #define _PUSHARGS_4 pushl %esi; cfi_adjust_cfa_offset (4); \
  346. cfi_rel_offset (esi, 0); L(PUSHSI1): _PUSHARGS_3
  347. #define _DOARGS_4(n) movl n(%esp), %esi; _DOARGS_3 (n-4)
  348. #define _POPARGS_4 _POPARGS_3; popl %esi; cfi_adjust_cfa_offset (-4); \
  349. cfi_restore (esi); L(POPSI1):
  350. #define PUSHARGS_5 _PUSHARGS_5
  351. #define DOARGS_5 _DOARGS_5 (32)
  352. #define POPARGS_5 _POPARGS_5
  353. #define _PUSHARGS_5 pushl %edi; cfi_adjust_cfa_offset (4); \
  354. cfi_rel_offset (edi, 0); L(PUSHDI1): _PUSHARGS_4
  355. #define _DOARGS_5(n) movl n(%esp), %edi; _DOARGS_4 (n-4)
  356. #define _POPARGS_5 _POPARGS_4; popl %edi; cfi_adjust_cfa_offset (-4); \
  357. cfi_restore (edi); L(POPDI1):
  358. #define PUSHARGS_6 _PUSHARGS_6
  359. #define DOARGS_6 _DOARGS_6 (40)
  360. #define POPARGS_6 _POPARGS_6
  361. #define _PUSHARGS_6 pushl %ebp; cfi_adjust_cfa_offset (4); \
  362. cfi_rel_offset (ebp, 0); L(PUSHBP1): _PUSHARGS_5
  363. #define _DOARGS_6(n) movl n(%esp), %ebp; _DOARGS_5 (n-4)
  364. #define _POPARGS_6 _POPARGS_5; popl %ebp; cfi_adjust_cfa_offset (-4); \
  365. cfi_restore (ebp); L(POPBP1):
  366. #endif /* __ASSEMBLER__ */
  367. /* Pointer mangling support. */
  368. #if defined NOT_IN_libc && defined IS_IN_rtld
  369. /* We cannot use the thread descriptor because in ld.so we use setjmp
  370. earlier than the descriptor is initialized. Using a global variable
  371. is too complicated here since we have no PC-relative addressing mode. */
  372. #else
  373. # ifdef __ASSEMBLER__
  374. # define PTR_MANGLE(reg) xorl %gs:POINTER_GUARD, reg; \
  375. roll $9, reg
  376. # define PTR_DEMANGLE(reg) rorl $9, reg; \
  377. xorl %gs:POINTER_GUARD, reg
  378. # else
  379. # include <stddef.h>
  380. # define PTR_MANGLE(var) __asm__ ("xorl %%gs:%c2, %0\n" \
  381. "roll $9, %0" \
  382. : "=r" (var) \
  383. : "0" (var), \
  384. "i" (offsetof (tcbhead_t, \
  385. pointer_guard)))
  386. # define PTR_DEMANGLE(var) __asm__ ("rorl $9, %0\n" \
  387. "xorl %%gs:%c2, %0" \
  388. : "=r" (var) \
  389. : "0" (var), \
  390. "i" (offsetof (tcbhead_t, \
  391. pointer_guard)))
  392. # endif
  393. #endif
  394. #endif /* linux/i386/sysdep.h */