syscalls.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #ifndef _BITS_SYSCALLS_H
  2. #define _BITS_SYSCALLS_H
  3. #ifndef _SYSCALL_H
  4. # error "Never use <bits/syscalls.h> directly; include <sys/syscall.h> instead."
  5. #endif
  6. #include <errno.h>
  7. /* This includes the `__NR_<name>' syscall numbers taken from the Linux kernel
  8. * header files. It also defines the traditional `SYS_<name>' macros for older
  9. * programs. */
  10. #include <bits/sysnum.h>
  11. /*
  12. Some of the sneaky macros in the code were taken from
  13. glibc-2.2.5/sysdeps/unix/sysv/linux/i386/sysdep.h
  14. */
  15. #ifndef __ASSEMBLER__
  16. /* We need some help from the assembler to generate optimal code. We
  17. define some macros here which later will be used. */
  18. #if defined __SUPPORT_LD_DEBUG__ && defined __DOMULTI__
  19. #error LD debugging and DOMULTI are incompatible
  20. #endif
  21. #ifdef __DOMULTI__
  22. __asm__ (".L__X'%ebx = 1\n\t"
  23. ".L__X'%ecx = 2\n\t"
  24. ".L__X'%edx = 2\n\t"
  25. ".L__X'%eax = 3\n\t"
  26. ".L__X'%esi = 3\n\t"
  27. ".L__X'%edi = 3\n\t"
  28. ".L__X'%ebp = 3\n\t"
  29. ".L__X'%esp = 3\n\t"
  30. ".ifndef _BITS_SYSCALLS_ASM\n\t"
  31. ".set _BITS_SYSCALLS_ASM,1\n\t"
  32. ".macro bpushl name reg\n\t"
  33. ".if 1 - \\name\n\t"
  34. ".if 2 - \\name\n\t"
  35. "pushl %ebx\n\t"
  36. ".else\n\t"
  37. "xchgl \\reg, %ebx\n\t"
  38. ".endif\n\t"
  39. ".endif\n\t"
  40. ".endm\n\t"
  41. ".macro bpopl name reg\n\t"
  42. ".if 1 - \\name\n\t"
  43. ".if 2 - \\name\n\t"
  44. "popl %ebx\n\t"
  45. ".else\n\t"
  46. "xchgl \\reg, %ebx\n\t"
  47. ".endif\n\t"
  48. ".endif\n\t"
  49. ".endm\n\t"
  50. ".macro bmovl name reg\n\t"
  51. ".if 1 - \\name\n\t"
  52. ".if 2 - \\name\n\t"
  53. "movl \\reg, %ebx\n\t"
  54. ".endif\n\t"
  55. ".endif\n\t"
  56. ".endm\n\t"
  57. ".endif\n\t");
  58. #else
  59. __asm__ (".L__X'%ebx = 1\n\t"
  60. ".L__X'%ecx = 2\n\t"
  61. ".L__X'%edx = 2\n\t"
  62. ".L__X'%eax = 3\n\t"
  63. ".L__X'%esi = 3\n\t"
  64. ".L__X'%edi = 3\n\t"
  65. ".L__X'%ebp = 3\n\t"
  66. ".L__X'%esp = 3\n\t"
  67. ".macro bpushl name reg\n\t"
  68. ".if 1 - \\name\n\t"
  69. ".if 2 - \\name\n\t"
  70. "pushl %ebx\n\t"
  71. ".else\n\t"
  72. "xchgl \\reg, %ebx\n\t"
  73. ".endif\n\t"
  74. ".endif\n\t"
  75. ".endm\n\t"
  76. ".macro bpopl name reg\n\t"
  77. ".if 1 - \\name\n\t"
  78. ".if 2 - \\name\n\t"
  79. "popl %ebx\n\t"
  80. ".else\n\t"
  81. "xchgl \\reg, %ebx\n\t"
  82. ".endif\n\t"
  83. ".endif\n\t"
  84. ".endm\n\t"
  85. ".macro bmovl name reg\n\t"
  86. ".if 1 - \\name\n\t"
  87. ".if 2 - \\name\n\t"
  88. "movl \\reg, %ebx\n\t"
  89. ".endif\n\t"
  90. ".endif\n\t"
  91. ".endm\n\t");
  92. #endif
  93. #undef _syscall0
  94. #define _syscall0(type,name) \
  95. type name(void) \
  96. { \
  97. return (type) (INLINE_SYSCALL(name, 0)); \
  98. }
  99. #undef _syscall1
  100. #define _syscall1(type,name,type1,arg1) \
  101. type name(type1 arg1) \
  102. { \
  103. return (type) (INLINE_SYSCALL(name, 1, arg1)); \
  104. }
  105. #undef _syscall2
  106. #define _syscall2(type,name,type1,arg1,type2,arg2) \
  107. type name(type1 arg1,type2 arg2) \
  108. { \
  109. return (type) (INLINE_SYSCALL(name, 2, arg1, arg2)); \
  110. }
  111. #undef _syscall3
  112. #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
  113. type name(type1 arg1,type2 arg2,type3 arg3) \
  114. { \
  115. return (type) (INLINE_SYSCALL(name, 3, arg1, arg2, arg3)); \
  116. }
  117. #undef _syscall4
  118. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
  119. type name (type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
  120. { \
  121. return (type) (INLINE_SYSCALL(name, 4, arg1, arg2, arg3, arg4)); \
  122. }
  123. #undef _syscall5
  124. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  125. type5,arg5) \
  126. type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
  127. { \
  128. return (type) (INLINE_SYSCALL(name, 5, arg1, arg2, arg3, arg4, arg5)); \
  129. }
  130. #undef _syscall6
  131. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  132. type5,arg5,type6,arg6) \
  133. type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, type6 arg6) \
  134. { \
  135. return (type) (INLINE_SYSCALL(name, 6, arg1, arg2, arg3, arg4, arg5, arg6)); \
  136. }
  137. #define INLINE_SYSCALL(name, nr, args...) \
  138. ({ \
  139. unsigned int resultvar; \
  140. __asm__ __volatile__ ( \
  141. LOADARGS_##nr \
  142. "movl %1, %%eax\n\t" \
  143. "int $0x80\n\t" \
  144. RESTOREARGS_##nr \
  145. : "=a" (resultvar) \
  146. : "i" (__NR_##name) ASMFMT_##nr(args) : "memory", "cc"); \
  147. if (resultvar >= 0xfffff001) \
  148. { \
  149. __set_errno (-resultvar); \
  150. resultvar = 0xffffffff; \
  151. } \
  152. (int) resultvar; })
  153. #define LOADARGS_0
  154. #define LOADARGS_1 \
  155. "bpushl .L__X'%k2, %k2\n\t" \
  156. "bmovl .L__X'%k2, %k2\n\t"
  157. #define LOADARGS_2 LOADARGS_1
  158. #define LOADARGS_3 LOADARGS_1
  159. #define LOADARGS_4 LOADARGS_1
  160. #define LOADARGS_5 LOADARGS_1
  161. #define LOADARGS_6 LOADARGS_1 "push %%ebp ; movl %7, %%ebp\n\t"
  162. #define RESTOREARGS_0
  163. #define RESTOREARGS_1 \
  164. "bpopl .L__X'%k2, %k2\n\t"
  165. #define RESTOREARGS_2 RESTOREARGS_1
  166. #define RESTOREARGS_3 RESTOREARGS_1
  167. #define RESTOREARGS_4 RESTOREARGS_1
  168. #define RESTOREARGS_5 RESTOREARGS_1
  169. #define RESTOREARGS_6 "pop %%ebp\n\t" RESTOREARGS_1
  170. #define ASMFMT_0()
  171. #define ASMFMT_1(arg1) \
  172. , "acdSD" (arg1)
  173. #define ASMFMT_2(arg1, arg2) \
  174. , "adCD" (arg1), "c" (arg2)
  175. #define ASMFMT_3(arg1, arg2, arg3) \
  176. , "aCD" (arg1), "c" (arg2), "d" (arg3)
  177. #define ASMFMT_4(arg1, arg2, arg3, arg4) \
  178. , "aD" (arg1), "c" (arg2), "d" (arg3), "S" (arg4)
  179. #define ASMFMT_5(arg1, arg2, arg3, arg4, arg5) \
  180. , "a" (arg1), "c" (arg2), "d" (arg3), "S" (arg4), "D" (arg5)
  181. #define ASMFMT_6(arg1, arg2, arg3, arg4, arg5, arg6) \
  182. , "a" (arg1), "c" (arg2), "d" (arg3), "S" (arg4), "D" (arg5), "m" (arg6)
  183. #endif /* __ASSEMBLER__ */
  184. #endif /* _BITS_SYSCALLS_H */