sysdep.h 15 KB

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