syscalls.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
  14. #define INTERNAL_SYSCALL_ERROR_P(val, err) \
  15. ((unsigned int) (val) >= 0xfffff001u)
  16. #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
  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 = INTERNAL_SYSCALL (name, , nr, args); \
  141. if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (_resultvar, ), 0)) \
  142. { \
  143. __set_errno (INTERNAL_SYSCALL_ERRNO (_resultvar, )); \
  144. _resultvar = 0xffffffff; \
  145. } \
  146. (int) _resultvar; })
  147. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  148. ({ \
  149. register unsigned int resultvar; \
  150. __asm__ __volatile__ ( \
  151. LOADARGS_##nr \
  152. "movl %1, %%eax\n\t" \
  153. "int $0x80\n\t" \
  154. RESTOREARGS_##nr \
  155. : "=a" (resultvar) \
  156. : "i" (__NR_##name) ASMFMT_##nr(args) : "memory", "cc"); \
  157. (int) resultvar; })
  158. #define LOADARGS_0
  159. #define LOADARGS_1 \
  160. "bpushl .L__X'%k2, %k2\n\t" \
  161. "bmovl .L__X'%k2, %k2\n\t"
  162. #define LOADARGS_2 LOADARGS_1
  163. #define LOADARGS_3 LOADARGS_1
  164. #define LOADARGS_4 LOADARGS_1
  165. #define LOADARGS_5 LOADARGS_1
  166. #define LOADARGS_6 LOADARGS_1 "push %%ebp ; movl %7, %%ebp\n\t"
  167. #define RESTOREARGS_0
  168. #define RESTOREARGS_1 \
  169. "bpopl .L__X'%k2, %k2\n\t"
  170. #define RESTOREARGS_2 RESTOREARGS_1
  171. #define RESTOREARGS_3 RESTOREARGS_1
  172. #define RESTOREARGS_4 RESTOREARGS_1
  173. #define RESTOREARGS_5 RESTOREARGS_1
  174. #define RESTOREARGS_6 "pop %%ebp\n\t" RESTOREARGS_1
  175. #define ASMFMT_0()
  176. #define ASMFMT_1(arg1) \
  177. , "acdSD" (arg1)
  178. #define ASMFMT_2(arg1, arg2) \
  179. , "adSD" (arg1), "c" (arg2)
  180. #define ASMFMT_3(arg1, arg2, arg3) \
  181. , "aSD" (arg1), "c" (arg2), "d" (arg3)
  182. #define ASMFMT_4(arg1, arg2, arg3, arg4) \
  183. , "aD" (arg1), "c" (arg2), "d" (arg3), "S" (arg4)
  184. #define ASMFMT_5(arg1, arg2, arg3, arg4, arg5) \
  185. , "a" (arg1), "c" (arg2), "d" (arg3), "S" (arg4), "D" (arg5)
  186. #define ASMFMT_6(arg1, arg2, arg3, arg4, arg5, arg6) \
  187. , "a" (arg1), "c" (arg2), "d" (arg3), "S" (arg4), "D" (arg5), "m" (arg6)
  188. #endif /* __ASSEMBLER__ */
  189. #endif /* _BITS_SYSCALLS_H */