sysdep.h 15 KB

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