syscalls.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. ".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. #undef _syscall0
  54. #define _syscall0(type,name) \
  55. type name(void) \
  56. { \
  57. return (type) (INLINE_SYSCALL(name, 0)); \
  58. }
  59. #undef _syscall1
  60. #define _syscall1(type,name,type1,arg1) \
  61. type name(type1 arg1) \
  62. { \
  63. return (type) (INLINE_SYSCALL(name, 1, arg1)); \
  64. }
  65. #undef _syscall2
  66. #define _syscall2(type,name,type1,arg1,type2,arg2) \
  67. type name(type1 arg1,type2 arg2) \
  68. { \
  69. return (type) (INLINE_SYSCALL(name, 2, arg1, arg2)); \
  70. }
  71. #undef _syscall3
  72. #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
  73. type name(type1 arg1,type2 arg2,type3 arg3) \
  74. { \
  75. return (type) (INLINE_SYSCALL(name, 3, arg1, arg2, arg3)); \
  76. }
  77. #undef _syscall4
  78. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
  79. type name (type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
  80. { \
  81. return (type) (INLINE_SYSCALL(name, 4, arg1, arg2, arg3, arg4)); \
  82. }
  83. #undef _syscall5
  84. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  85. type5,arg5) \
  86. type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
  87. { \
  88. return (type) (INLINE_SYSCALL(name, 5, arg1, arg2, arg3, arg4, arg5)); \
  89. }
  90. #define INLINE_SYSCALL(name, nr, args...) \
  91. ({ \
  92. unsigned int resultvar; \
  93. asm volatile ( \
  94. LOADARGS_##nr \
  95. "movl %1, %%eax\n\t" \
  96. "int $0x80\n\t" \
  97. RESTOREARGS_##nr \
  98. : "=a" (resultvar) \
  99. : "i" (__NR_##name) ASMFMT_##nr(args) : "memory", "cc"); \
  100. if (resultvar >= 0xfffff001) \
  101. { \
  102. __set_errno (-resultvar); \
  103. resultvar = 0xffffffff; \
  104. } \
  105. (int) resultvar; })
  106. #define LOADARGS_0
  107. #define LOADARGS_1 \
  108. "bpushl .L__X'%k2, %k2\n\t" \
  109. "bmovl .L__X'%k2, %k2\n\t"
  110. #define LOADARGS_2 LOADARGS_1
  111. #define LOADARGS_3 LOADARGS_1
  112. #define LOADARGS_4 LOADARGS_1
  113. #define LOADARGS_5 LOADARGS_1
  114. #define RESTOREARGS_0
  115. #define RESTOREARGS_1 \
  116. "bpopl .L__X'%k2, %k2\n\t"
  117. #define RESTOREARGS_2 RESTOREARGS_1
  118. #define RESTOREARGS_3 RESTOREARGS_1
  119. #define RESTOREARGS_4 RESTOREARGS_1
  120. #define RESTOREARGS_5 RESTOREARGS_1
  121. #define ASMFMT_0()
  122. #define ASMFMT_1(arg1) \
  123. , "acdSD" (arg1)
  124. #define ASMFMT_2(arg1, arg2) \
  125. , "adCD" (arg1), "c" (arg2)
  126. #define ASMFMT_3(arg1, arg2, arg3) \
  127. , "aCD" (arg1), "c" (arg2), "d" (arg3)
  128. #define ASMFMT_4(arg1, arg2, arg3, arg4) \
  129. , "aD" (arg1), "c" (arg2), "d" (arg3), "S" (arg4)
  130. #define ASMFMT_5(arg1, arg2, arg3, arg4, arg5) \
  131. , "a" (arg1), "c" (arg2), "d" (arg3), "S" (arg4), "D" (arg5)
  132. #endif /* __ASSEMBLER__ */
  133. #endif /* _BITS_SYSCALLS_H */