sysdep.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. #endif /* __ASSEMBLER__ */
  128. #ifndef offsetof
  129. # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  130. #endif
  131. /* For Linux we can use the system call table in the header file
  132. /usr/include/asm/unistd.h
  133. of the kernel. But these symbols do not follow the SYS_* syntax
  134. so we have to redefine the `SYS_ify' macro here. */
  135. #undef SYS_ify
  136. #define SYS_ify(syscall_name) __NR_##syscall_name
  137. #if defined USE_DL_SYSINFO \
  138. && (!defined NOT_IN_libc || defined IS_IN_libpthread)
  139. # define I386_USE_SYSENTER 1
  140. #else
  141. # undef I386_USE_SYSENTER
  142. #endif
  143. #ifdef __ASSEMBLER__
  144. /* Linux uses a negative return value to indicate syscall errors,
  145. unlike most Unices, which use the condition codes' carry flag.
  146. Since version 2.1 the return value of a system call might be
  147. negative even if the call succeeded. E.g., the `lseek' system call
  148. might return a large offset. Therefore we must not anymore test
  149. for < 0, but test for a real error by making sure the value in %eax
  150. is a real error number. Linus said he will make sure the no syscall
  151. returns a value in -1 .. -4095 as a valid result so we can savely
  152. test with -4095. */
  153. /* We don't want the label for the error handle to be global when we define
  154. it here. */
  155. #ifdef __PIC__
  156. # define SYSCALL_ERROR_LABEL 0f
  157. #else
  158. # define SYSCALL_ERROR_LABEL syscall_error
  159. #endif
  160. #undef PSEUDO
  161. #define PSEUDO(name, syscall_name, args) \
  162. .text; \
  163. ENTRY (name) \
  164. DO_CALL (syscall_name, args); \
  165. cmpl $-4095, %eax; \
  166. jae SYSCALL_ERROR_LABEL; \
  167. L(pseudo_end):
  168. #undef PSEUDO_END
  169. #define PSEUDO_END(name) \
  170. SYSCALL_ERROR_HANDLER \
  171. END (name)
  172. #undef PSEUDO_NOERRNO
  173. #define PSEUDO_NOERRNO(name, syscall_name, args) \
  174. .text; \
  175. ENTRY (name) \
  176. DO_CALL (syscall_name, args)
  177. #undef PSEUDO_END_NOERRNO
  178. #define PSEUDO_END_NOERRNO(name) \
  179. END (name)
  180. #define ret_NOERRNO ret
  181. /* The function has to return the error code. */
  182. #undef PSEUDO_ERRVAL
  183. #define PSEUDO_ERRVAL(name, syscall_name, args) \
  184. .text; \
  185. ENTRY (name) \
  186. DO_CALL (syscall_name, args); \
  187. negl %eax
  188. #undef PSEUDO_END_ERRVAL
  189. #define PSEUDO_END_ERRVAL(name) \
  190. END (name)
  191. #define ret_ERRVAL ret
  192. #ifndef __PIC__
  193. # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
  194. #else
  195. # ifdef RTLD_PRIVATE_ERRNO
  196. # define SYSCALL_ERROR_HANDLER \
  197. 0:SETUP_PIC_REG(cx); \
  198. addl $_GLOBAL_OFFSET_TABLE_, %ecx; \
  199. xorl %edx, %edx; \
  200. subl %eax, %edx; \
  201. movl %edx, rtld_errno@GOTOFF(%ecx); \
  202. orl $-1, %eax; \
  203. jmp L(pseudo_end);
  204. # elif defined _LIBC_REENTRANT
  205. # if defined USE___THREAD
  206. # ifndef NOT_IN_libc
  207. # define SYSCALL_ERROR_ERRNO __libc_errno
  208. # else
  209. # define SYSCALL_ERROR_ERRNO errno
  210. # endif
  211. # define SYSCALL_ERROR_HANDLER \
  212. 0:SETUP_PIC_REG (cx); \
  213. addl $_GLOBAL_OFFSET_TABLE_, %ecx; \
  214. movl SYSCALL_ERROR_ERRNO@GOTNTPOFF(%ecx), %ecx; \
  215. xorl %edx, %edx; \
  216. subl %eax, %edx; \
  217. SYSCALL_ERROR_HANDLER_TLS_STORE (%edx, %ecx); \
  218. orl $-1, %eax; \
  219. jmp L(pseudo_end);
  220. # ifndef NO_TLS_DIRECT_SEG_REFS
  221. # define SYSCALL_ERROR_HANDLER_TLS_STORE(src, destoff) \
  222. movl src, %gs:(destoff)
  223. # else
  224. # define SYSCALL_ERROR_HANDLER_TLS_STORE(src, destoff) \
  225. addl %gs:0, destoff; \
  226. movl src, (destoff)
  227. # endif
  228. # else
  229. # define SYSCALL_ERROR_HANDLER \
  230. 0:pushl %ebx; \
  231. cfi_adjust_cfa_offset (4); \
  232. cfi_rel_offset (ebx, 0); \
  233. SETUP_PIC_REG (bx); \
  234. addl $_GLOBAL_OFFSET_TABLE_, %ebx; \
  235. xorl %edx, %edx; \
  236. subl %eax, %edx; \
  237. pushl %edx; \
  238. cfi_adjust_cfa_offset (4); \
  239. call __errno_location@PLT; \
  240. popl %ecx; \
  241. cfi_adjust_cfa_offset (-4); \
  242. popl %ebx; \
  243. cfi_adjust_cfa_offset (-4); \
  244. cfi_restore (ebx); \
  245. movl %ecx, (%eax); \
  246. orl $-1, %eax; \
  247. jmp L(pseudo_end);
  248. /* A quick note: it is assumed that the call to `__errno_location' does
  249. not modify the stack! */
  250. # endif
  251. # else
  252. /* Store (- %eax) into errno through the GOT. */
  253. # define SYSCALL_ERROR_HANDLER \
  254. 0:SETUP_PIC_REG(cx); \
  255. addl $_GLOBAL_OFFSET_TABLE_, %ecx; \
  256. xorl %edx, %edx; \
  257. subl %eax, %edx; \
  258. movl errno@GOT(%ecx), %ecx; \
  259. movl %edx, (%ecx); \
  260. orl $-1, %eax; \
  261. jmp L(pseudo_end);
  262. # endif /* _LIBC_REENTRANT */
  263. #endif /* __PIC__ */
  264. /* The original calling convention for system calls on Linux/i386 is
  265. to use int $0x80. */
  266. #ifdef I386_USE_SYSENTER
  267. # ifdef SHARED
  268. # define ENTER_KERNEL call *%gs:SYSINFO_OFFSET
  269. # else
  270. # define ENTER_KERNEL call *_dl_sysinfo
  271. # endif
  272. #else
  273. # define ENTER_KERNEL int $0x80
  274. #endif
  275. /* Linux takes system call arguments in registers:
  276. syscall number %eax call-clobbered
  277. arg 1 %ebx call-saved
  278. arg 2 %ecx call-clobbered
  279. arg 3 %edx call-clobbered
  280. arg 4 %esi call-saved
  281. arg 5 %edi call-saved
  282. arg 6 %ebp call-saved
  283. The stack layout upon entering the function is:
  284. 24(%esp) Arg# 6
  285. 20(%esp) Arg# 5
  286. 16(%esp) Arg# 4
  287. 12(%esp) Arg# 3
  288. 8(%esp) Arg# 2
  289. 4(%esp) Arg# 1
  290. (%esp) Return address
  291. (Of course a function with say 3 arguments does not have entries for
  292. arguments 4, 5, and 6.)
  293. The following code tries hard to be optimal. A general assumption
  294. (which is true according to the data books I have) is that
  295. 2 * xchg is more expensive than pushl + movl + popl
  296. Beside this a neat trick is used. The calling conventions for Linux
  297. tell that among the registers used for parameters %ecx and %edx need
  298. not be saved. Beside this we may clobber this registers even when
  299. they are not used for parameter passing.
  300. As a result one can see below that we save the content of the %ebx
  301. register in the %edx register when we have less than 3 arguments
  302. (2 * movl is less expensive than pushl + popl).
  303. Second unlike for the other registers we don't save the content of
  304. %ecx and %edx when we have more than 1 and 2 registers resp.
  305. The code below might look a bit long but we have to take care for
  306. the pipelined processors (i586). Here the `pushl' and `popl'
  307. instructions are marked as NP (not pairable) but the exception is
  308. two consecutive of these instruction. This gives no penalty on
  309. other processors though. */
  310. #undef DO_CALL
  311. #define DO_CALL(syscall_name, args) \
  312. PUSHARGS_##args \
  313. DOARGS_##args \
  314. movl $SYS_ify (syscall_name), %eax; \
  315. ENTER_KERNEL \
  316. POPARGS_##args
  317. #define PUSHARGS_0 /* No arguments to push. */
  318. #define DOARGS_0 /* No arguments to frob. */
  319. #define POPARGS_0 /* No arguments to pop. */
  320. #define _PUSHARGS_0 /* No arguments to push. */
  321. #define _DOARGS_0(n) /* No arguments to frob. */
  322. #define _POPARGS_0 /* No arguments to pop. */
  323. #define PUSHARGS_1 movl %ebx, %edx; L(SAVEBX1): PUSHARGS_0
  324. #define DOARGS_1 _DOARGS_1 (4)
  325. #define POPARGS_1 POPARGS_0; movl %edx, %ebx; L(RESTBX1):
  326. #define _PUSHARGS_1 pushl %ebx; cfi_adjust_cfa_offset (4); \
  327. cfi_rel_offset (ebx, 0); L(PUSHBX1): _PUSHARGS_0
  328. #define _DOARGS_1(n) movl n(%esp), %ebx; _DOARGS_0(n-4)
  329. #define _POPARGS_1 _POPARGS_0; popl %ebx; cfi_adjust_cfa_offset (-4); \
  330. cfi_restore (ebx); L(POPBX1):
  331. #define PUSHARGS_2 PUSHARGS_1
  332. #define DOARGS_2 _DOARGS_2 (8)
  333. #define POPARGS_2 POPARGS_1
  334. #define _PUSHARGS_2 _PUSHARGS_1
  335. #define _DOARGS_2(n) movl n(%esp), %ecx; _DOARGS_1 (n-4)
  336. #define _POPARGS_2 _POPARGS_1
  337. #define PUSHARGS_3 _PUSHARGS_2
  338. #define DOARGS_3 _DOARGS_3 (16)
  339. #define POPARGS_3 _POPARGS_3
  340. #define _PUSHARGS_3 _PUSHARGS_2
  341. #define _DOARGS_3(n) movl n(%esp), %edx; _DOARGS_2 (n-4)
  342. #define _POPARGS_3 _POPARGS_2
  343. #define PUSHARGS_4 _PUSHARGS_4
  344. #define DOARGS_4 _DOARGS_4 (24)
  345. #define POPARGS_4 _POPARGS_4
  346. #define _PUSHARGS_4 pushl %esi; cfi_adjust_cfa_offset (4); \
  347. cfi_rel_offset (esi, 0); L(PUSHSI1): _PUSHARGS_3
  348. #define _DOARGS_4(n) movl n(%esp), %esi; _DOARGS_3 (n-4)
  349. #define _POPARGS_4 _POPARGS_3; popl %esi; cfi_adjust_cfa_offset (-4); \
  350. cfi_restore (esi); L(POPSI1):
  351. #define PUSHARGS_5 _PUSHARGS_5
  352. #define DOARGS_5 _DOARGS_5 (32)
  353. #define POPARGS_5 _POPARGS_5
  354. #define _PUSHARGS_5 pushl %edi; cfi_adjust_cfa_offset (4); \
  355. cfi_rel_offset (edi, 0); L(PUSHDI1): _PUSHARGS_4
  356. #define _DOARGS_5(n) movl n(%esp), %edi; _DOARGS_4 (n-4)
  357. #define _POPARGS_5 _POPARGS_4; popl %edi; cfi_adjust_cfa_offset (-4); \
  358. cfi_restore (edi); L(POPDI1):
  359. #define PUSHARGS_6 _PUSHARGS_6
  360. #define DOARGS_6 _DOARGS_6 (40)
  361. #define POPARGS_6 _POPARGS_6
  362. #define _PUSHARGS_6 pushl %ebp; cfi_adjust_cfa_offset (4); \
  363. cfi_rel_offset (ebp, 0); L(PUSHBP1): _PUSHARGS_5
  364. #define _DOARGS_6(n) movl n(%esp), %ebp; _DOARGS_5 (n-4)
  365. #define _POPARGS_6 _POPARGS_5; popl %ebp; cfi_adjust_cfa_offset (-4); \
  366. cfi_restore (ebp); L(POPBP1):
  367. #endif /* __ASSEMBLER__ */
  368. /* Pointer mangling support. */
  369. #if defined NOT_IN_libc && defined IS_IN_rtld
  370. /* We cannot use the thread descriptor because in ld.so we use setjmp
  371. earlier than the descriptor is initialized. Using a global variable
  372. is too complicated here since we have no PC-relative addressing mode. */
  373. #else
  374. # ifdef __ASSEMBLER__
  375. # define PTR_MANGLE(reg) xorl %gs:POINTER_GUARD, reg; \
  376. roll $9, reg
  377. # define PTR_DEMANGLE(reg) rorl $9, reg; \
  378. xorl %gs:POINTER_GUARD, reg
  379. # else
  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 */