syscalls.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. __asm__ (".L__X'%ebx = 1\n\t"
  19. ".L__X'%ecx = 2\n\t"
  20. ".L__X'%edx = 2\n\t"
  21. ".L__X'%eax = 3\n\t"
  22. ".L__X'%esi = 3\n\t"
  23. ".L__X'%edi = 3\n\t"
  24. ".L__X'%ebp = 3\n\t"
  25. ".L__X'%esp = 3\n\t"
  26. ".ifndef _BITS_SYSCALLS_ASM\n\t"
  27. ".set _BITS_SYSCALLS_ASM,1\n\t"
  28. ".macro bpushl name reg\n\t"
  29. ".if 1 - \\name\n\t"
  30. ".if 2 - \\name\n\t"
  31. "pushl %ebx\n\t"
  32. ".else\n\t"
  33. "xchgl \\reg, %ebx\n\t"
  34. ".endif\n\t"
  35. ".endif\n\t"
  36. ".endm\n\t"
  37. ".macro bpopl name reg\n\t"
  38. ".if 1 - \\name\n\t"
  39. ".if 2 - \\name\n\t"
  40. "popl %ebx\n\t"
  41. ".else\n\t"
  42. "xchgl \\reg, %ebx\n\t"
  43. ".endif\n\t"
  44. ".endif\n\t"
  45. ".endm\n\t"
  46. ".macro bmovl name reg\n\t"
  47. ".if 1 - \\name\n\t"
  48. ".if 2 - \\name\n\t"
  49. "movl \\reg, %ebx\n\t"
  50. ".endif\n\t"
  51. ".endif\n\t"
  52. ".endm\n\t"
  53. ".endif\n\t");
  54. #undef _syscall0
  55. #define _syscall0(type,name) \
  56. type name(void) \
  57. { \
  58. return (type) (INLINE_SYSCALL(name, 0)); \
  59. }
  60. #undef _syscall1
  61. #define _syscall1(type,name,type1,arg1) \
  62. type name(type1 arg1) \
  63. { \
  64. return (type) (INLINE_SYSCALL(name, 1, arg1)); \
  65. }
  66. #undef _syscall2
  67. #define _syscall2(type,name,type1,arg1,type2,arg2) \
  68. type name(type1 arg1,type2 arg2) \
  69. { \
  70. return (type) (INLINE_SYSCALL(name, 2, arg1, arg2)); \
  71. }
  72. #undef _syscall3
  73. #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
  74. type name(type1 arg1,type2 arg2,type3 arg3) \
  75. { \
  76. return (type) (INLINE_SYSCALL(name, 3, arg1, arg2, arg3)); \
  77. }
  78. #undef _syscall4
  79. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
  80. type name (type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
  81. { \
  82. return (type) (INLINE_SYSCALL(name, 4, arg1, arg2, arg3, arg4)); \
  83. }
  84. #undef _syscall5
  85. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  86. type5,arg5) \
  87. type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
  88. { \
  89. return (type) (INLINE_SYSCALL(name, 5, arg1, arg2, arg3, arg4, arg5)); \
  90. }
  91. #undef _syscall6
  92. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  93. type5,arg5,type6,arg6) \
  94. type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, type6 arg6) \
  95. { \
  96. return (type) (INLINE_SYSCALL(name, 6, arg1, arg2, arg3, arg4, arg5, arg6)); \
  97. }
  98. #define INLINE_SYSCALL(name, nr, args...) \
  99. ({ \
  100. unsigned int resultvar; \
  101. __asm__ __volatile__ ( \
  102. LOADARGS_##nr \
  103. "movl %1, %%eax\n\t" \
  104. "int $0x80\n\t" \
  105. RESTOREARGS_##nr \
  106. : "=a" (resultvar) \
  107. : "i" (__NR_##name) ASMFMT_##nr(args) : "memory", "cc"); \
  108. if (resultvar >= 0xfffff001) \
  109. { \
  110. __set_errno (-resultvar); \
  111. resultvar = 0xffffffff; \
  112. } \
  113. (int) resultvar; })
  114. #define LOADARGS_0
  115. #define LOADARGS_1 \
  116. "bpushl .L__X'%k2, %k2\n\t" \
  117. "bmovl .L__X'%k2, %k2\n\t"
  118. #define LOADARGS_2 LOADARGS_1
  119. #define LOADARGS_3 LOADARGS_1
  120. #define LOADARGS_4 LOADARGS_1
  121. #define LOADARGS_5 LOADARGS_1
  122. #define LOADARGS_6 LOADARGS_1 "push %%ebp ; movl %7, %%ebp\n\t"
  123. #define RESTOREARGS_0
  124. #define RESTOREARGS_1 \
  125. "bpopl .L__X'%k2, %k2\n\t"
  126. #define RESTOREARGS_2 RESTOREARGS_1
  127. #define RESTOREARGS_3 RESTOREARGS_1
  128. #define RESTOREARGS_4 RESTOREARGS_1
  129. #define RESTOREARGS_5 RESTOREARGS_1
  130. #define RESTOREARGS_6 "pop %%ebp\n\t" RESTOREARGS_1
  131. #define ASMFMT_0()
  132. #define ASMFMT_1(arg1) \
  133. , "acdSD" (arg1)
  134. #define ASMFMT_2(arg1, arg2) \
  135. , "adCD" (arg1), "c" (arg2)
  136. #define ASMFMT_3(arg1, arg2, arg3) \
  137. , "aCD" (arg1), "c" (arg2), "d" (arg3)
  138. #define ASMFMT_4(arg1, arg2, arg3, arg4) \
  139. , "aD" (arg1), "c" (arg2), "d" (arg3), "S" (arg4)
  140. #define ASMFMT_5(arg1, arg2, arg3, arg4, arg5) \
  141. , "a" (arg1), "c" (arg2), "d" (arg3), "S" (arg4), "D" (arg5)
  142. #define ASMFMT_6(arg1, arg2, arg3, arg4, arg5, arg6) \
  143. , "a" (arg1), "c" (arg2), "d" (arg3), "S" (arg4), "D" (arg5), "m" (arg6)
  144. #endif /* __ASSEMBLER__ */
  145. #endif /* _BITS_SYSCALLS_H */