syscalls.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #ifndef __set_errno
  11. # define __set_errno(val) (*__errno_location ()) = (val)
  12. #endif
  13. /*
  14. Some of the sneaky macros in the code were taken from
  15. glibc-2.2.5/sysdeps/unix/sysv/linux/i386/sysdep.h
  16. */
  17. #ifndef __ASSEMBLER__
  18. /* We need some help from the assembler to generate optimal code. We
  19. define some macros here which later will be used. */
  20. asm (".L__X'%ebx = 1\n\t"
  21. ".L__X'%ecx = 2\n\t"
  22. ".L__X'%edx = 2\n\t"
  23. ".L__X'%eax = 3\n\t"
  24. ".L__X'%esi = 3\n\t"
  25. ".L__X'%edi = 3\n\t"
  26. ".L__X'%ebp = 3\n\t"
  27. ".L__X'%esp = 3\n\t"
  28. ".ifndef _BITS_SYSCALLS_ASM\n\t"
  29. ".set _BITS_SYSCALLS_ASM,1\n\t"
  30. ".macro bpushl name reg\n\t"
  31. ".if 1 - \\name\n\t"
  32. ".if 2 - \\name\n\t"
  33. "pushl %ebx\n\t"
  34. ".else\n\t"
  35. "xchgl \\reg, %ebx\n\t"
  36. ".endif\n\t"
  37. ".endif\n\t"
  38. ".endm\n\t"
  39. ".macro bpopl name reg\n\t"
  40. ".if 1 - \\name\n\t"
  41. ".if 2 - \\name\n\t"
  42. "popl %ebx\n\t"
  43. ".else\n\t"
  44. "xchgl \\reg, %ebx\n\t"
  45. ".endif\n\t"
  46. ".endif\n\t"
  47. ".endm\n\t"
  48. ".macro bmovl name reg\n\t"
  49. ".if 1 - \\name\n\t"
  50. ".if 2 - \\name\n\t"
  51. "movl \\reg, %ebx\n\t"
  52. ".endif\n\t"
  53. ".endif\n\t"
  54. ".endm\n\t"
  55. ".endif\n\t");
  56. #undef _syscall0
  57. #define _syscall0(type,name) \
  58. type name(void) \
  59. { \
  60. return (type) (INLINE_SYSCALL(name, 0)); \
  61. }
  62. #undef _syscall1
  63. #define _syscall1(type,name,type1,arg1) \
  64. type name(type1 arg1) \
  65. { \
  66. return (type) (INLINE_SYSCALL(name, 1, arg1)); \
  67. }
  68. #undef _syscall2
  69. #define _syscall2(type,name,type1,arg1,type2,arg2) \
  70. type name(type1 arg1,type2 arg2) \
  71. { \
  72. return (type) (INLINE_SYSCALL(name, 2, arg1, arg2)); \
  73. }
  74. #undef _syscall3
  75. #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
  76. type name(type1 arg1,type2 arg2,type3 arg3) \
  77. { \
  78. return (type) (INLINE_SYSCALL(name, 3, arg1, arg2, arg3)); \
  79. }
  80. #undef _syscall4
  81. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
  82. type name (type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
  83. { \
  84. return (type) (INLINE_SYSCALL(name, 4, arg1, arg2, arg3, arg4)); \
  85. }
  86. #undef _syscall5
  87. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  88. type5,arg5) \
  89. type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
  90. { \
  91. return (type) (INLINE_SYSCALL(name, 5, arg1, arg2, arg3, arg4, arg5)); \
  92. }
  93. #define INLINE_SYSCALL(name, nr, args...) \
  94. ({ \
  95. unsigned int resultvar; \
  96. asm volatile ( \
  97. LOADARGS_##nr \
  98. "movl %1, %%eax\n\t" \
  99. "int $0x80\n\t" \
  100. RESTOREARGS_##nr \
  101. : "=a" (resultvar) \
  102. : "i" (__NR_##name) ASMFMT_##nr(args) : "memory", "cc"); \
  103. if (resultvar >= 0xfffff001) \
  104. { \
  105. __set_errno (-resultvar); \
  106. resultvar = 0xffffffff; \
  107. } \
  108. (int) resultvar; })
  109. #define LOADARGS_0
  110. #define LOADARGS_1 \
  111. "bpushl .L__X'%k2, %k2\n\t" \
  112. "bmovl .L__X'%k2, %k2\n\t"
  113. #define LOADARGS_2 LOADARGS_1
  114. #define LOADARGS_3 LOADARGS_1
  115. #define LOADARGS_4 LOADARGS_1
  116. #define LOADARGS_5 LOADARGS_1
  117. #define RESTOREARGS_0
  118. #define RESTOREARGS_1 \
  119. "bpopl .L__X'%k2, %k2\n\t"
  120. #define RESTOREARGS_2 RESTOREARGS_1
  121. #define RESTOREARGS_3 RESTOREARGS_1
  122. #define RESTOREARGS_4 RESTOREARGS_1
  123. #define RESTOREARGS_5 RESTOREARGS_1
  124. #define ASMFMT_0()
  125. #define ASMFMT_1(arg1) \
  126. , "acdSD" (arg1)
  127. #define ASMFMT_2(arg1, arg2) \
  128. , "adCD" (arg1), "c" (arg2)
  129. #define ASMFMT_3(arg1, arg2, arg3) \
  130. , "aCD" (arg1), "c" (arg2), "d" (arg3)
  131. #define ASMFMT_4(arg1, arg2, arg3, arg4) \
  132. , "aD" (arg1), "c" (arg2), "d" (arg3), "S" (arg4)
  133. #define ASMFMT_5(arg1, arg2, arg3, arg4, arg5) \
  134. , "a" (arg1), "c" (arg2), "d" (arg3), "S" (arg4), "D" (arg5)
  135. #endif /* __ASSEMBLER__ */
  136. #endif /* _BITS_SYSCALLS_H */