syscalls.h 5.5 KB

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