syscalls.h 5.3 KB

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