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. #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. #define INLINE_SYSCALL(name, nr, args...) \
  92. ({ \
  93. unsigned int resultvar; \
  94. asm volatile ( \
  95. LOADARGS_##nr \
  96. "movl %1, %%eax\n\t" \
  97. "int $0x80\n\t" \
  98. RESTOREARGS_##nr \
  99. : "=a" (resultvar) \
  100. : "i" (__NR_##name) ASMFMT_##nr(args) : "memory", "cc"); \
  101. if (resultvar >= 0xfffff001) \
  102. { \
  103. __set_errno (-resultvar); \
  104. resultvar = 0xffffffff; \
  105. } \
  106. (int) resultvar; })
  107. #define LOADARGS_0
  108. #define LOADARGS_1 \
  109. "bpushl .L__X'%k2, %k2\n\t" \
  110. "bmovl .L__X'%k2, %k2\n\t"
  111. #define LOADARGS_2 LOADARGS_1
  112. #define LOADARGS_3 LOADARGS_1
  113. #define LOADARGS_4 LOADARGS_1
  114. #define LOADARGS_5 LOADARGS_1
  115. #define RESTOREARGS_0
  116. #define RESTOREARGS_1 \
  117. "bpopl .L__X'%k2, %k2\n\t"
  118. #define RESTOREARGS_2 RESTOREARGS_1
  119. #define RESTOREARGS_3 RESTOREARGS_1
  120. #define RESTOREARGS_4 RESTOREARGS_1
  121. #define RESTOREARGS_5 RESTOREARGS_1
  122. #define ASMFMT_0()
  123. #define ASMFMT_1(arg1) \
  124. , "acdSD" (arg1)
  125. #define ASMFMT_2(arg1, arg2) \
  126. , "adCD" (arg1), "c" (arg2)
  127. #define ASMFMT_3(arg1, arg2, arg3) \
  128. , "aCD" (arg1), "c" (arg2), "d" (arg3)
  129. #define ASMFMT_4(arg1, arg2, arg3, arg4) \
  130. , "aD" (arg1), "c" (arg2), "d" (arg3), "S" (arg4)
  131. #define ASMFMT_5(arg1, arg2, arg3, arg4, arg5) \
  132. , "a" (arg1), "c" (arg2), "d" (arg3), "S" (arg4), "D" (arg5)
  133. #endif /* __ASSEMBLER__ */
  134. #endif /* _BITS_SYSCALLS_H */