syscalls.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. /*
  11. Some of the sneaky macros in the code were taken from
  12. glibc-2.2.5/sysdeps/unix/sysv/linux/x86_64/sysdep.h
  13. */
  14. #ifndef __ASSEMBLER__
  15. #include <errno.h>
  16. #define SYS_ify(syscall_name) (__NR_##syscall_name)
  17. #undef _syscall0
  18. #define _syscall0(type,name) \
  19. type name(void) \
  20. { \
  21. return (type) (INLINE_SYSCALL(name, 0)); \
  22. }
  23. #undef _syscall1
  24. #define _syscall1(type,name,type1,arg1) \
  25. type name(type1 arg1) \
  26. { \
  27. return (type) (INLINE_SYSCALL(name, 1, arg1)); \
  28. }
  29. #undef _syscall2
  30. #define _syscall2(type,name,type1,arg1,type2,arg2) \
  31. type name(type1 arg1,type2 arg2) \
  32. { \
  33. return (type) (INLINE_SYSCALL(name, 2, arg1, arg2)); \
  34. }
  35. #undef _syscall3
  36. #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
  37. type name(type1 arg1,type2 arg2,type3 arg3) \
  38. { \
  39. return (type) (INLINE_SYSCALL(name, 3, arg1, arg2, arg3)); \
  40. }
  41. #undef _syscall4
  42. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
  43. type name (type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
  44. { \
  45. return (type) (INLINE_SYSCALL(name, 4, arg1, arg2, arg3, arg4)); \
  46. }
  47. #undef _syscall5
  48. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  49. type5,arg5) \
  50. type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
  51. { \
  52. return (type) (INLINE_SYSCALL(name, 5, arg1, arg2, arg3, arg4, arg5)); \
  53. }
  54. #undef _syscall6
  55. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  56. type5,arg5,type6,arg6) \
  57. type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5,type6 arg6) \
  58. { \
  59. return (type) (INLINE_SYSCALL(name, 6, arg1, arg2, arg3, arg4, arg5, arg6)); \
  60. }
  61. /* The Linux/x86-64 kernel expects the system call parameters in
  62. registers according to the following table:
  63. syscall number rax
  64. arg 1 rdi
  65. arg 2 rsi
  66. arg 3 rdx
  67. arg 4 r10
  68. arg 5 r8
  69. arg 6 r9
  70. The Linux kernel uses and destroys internally these registers:
  71. return address from
  72. syscall rcx
  73. additionally clobered: r12-r15,rbx,rbp
  74. eflags from syscall r11
  75. Normal function call, including calls to the system call stub
  76. functions in the libc, get the first six parameters passed in
  77. registers and the seventh parameter and later on the stack. The
  78. register use is as follows:
  79. system call number in the DO_CALL macro
  80. arg 1 rdi
  81. arg 2 rsi
  82. arg 3 rdx
  83. arg 4 rcx
  84. arg 5 r8
  85. arg 6 r9
  86. We have to take care that the stack is aligned to 16 bytes. When
  87. called the stack is not aligned since the return address has just
  88. been pushed.
  89. Syscalls of more than 6 arguments are not supported. */
  90. #undef SYS_ify
  91. #define SYS_ify(syscall_name) __NR_##syscall_name
  92. #undef DO_CALL
  93. #define DO_CALL(syscall_name, args) \
  94. DOARGS_##args \
  95. movq $SYS_ify (syscall_name), %rax; \
  96. syscall;
  97. #define DOARGS_0 /* nothing */
  98. #define DOARGS_1 /* nothing */
  99. #define DOARGS_2 /* nothing */
  100. #define DOARGS_3 /* nothing */
  101. #define DOARGS_4 movq %rcx, %r10;
  102. #define DOARGS_5 DOARGS_4
  103. #define DOARGS_6 DOARGS_5
  104. /* Define a macro which expands inline into the wrapper code for a system
  105. call. */
  106. #undef INLINE_SYSCALL
  107. #define INLINE_SYSCALL(name, nr, args...) \
  108. ({ \
  109. unsigned long _resultvar = INTERNAL_SYSCALL (name, , nr, args); \
  110. if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (_resultvar, ), 0)) \
  111. { \
  112. __set_errno (INTERNAL_SYSCALL_ERRNO (_resultvar, )); \
  113. _resultvar = (unsigned long) -1; \
  114. } \
  115. (long) _resultvar; })
  116. #undef INTERNAL_SYSCALL_DECL
  117. #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
  118. #define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
  119. ({ \
  120. unsigned long resultvar; \
  121. LOAD_ARGS_##nr (args) \
  122. LOAD_REGS_##nr \
  123. asm volatile ( \
  124. "syscall\n\t" \
  125. : "=a" (resultvar) \
  126. : "0" (name) ASM_ARGS_##nr : "memory", "cc", "r11", "cx"); \
  127. (long) resultvar; })
  128. #undef INTERNAL_SYSCALL
  129. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  130. INTERNAL_SYSCALL_NCS (__NR_##name, err, nr, ##args)
  131. #undef INTERNAL_SYSCALL_ERROR_P
  132. #define INTERNAL_SYSCALL_ERROR_P(val, err) \
  133. ((unsigned long) (val) >= -4095L)
  134. #undef INTERNAL_SYSCALL_ERRNO
  135. #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
  136. #define LOAD_ARGS_0()
  137. #define LOAD_REGS_0
  138. #define ASM_ARGS_0
  139. #define LOAD_ARGS_1(a1) \
  140. long int __arg1 = (long) (a1); \
  141. LOAD_ARGS_0 ()
  142. #define LOAD_REGS_1 \
  143. register long int _a1 asm ("rdi") = __arg1; \
  144. LOAD_REGS_0
  145. #define ASM_ARGS_1 ASM_ARGS_0, "r" (_a1)
  146. #define LOAD_ARGS_2(a1, a2) \
  147. long int __arg2 = (long) (a2); \
  148. LOAD_ARGS_1 (a1)
  149. #define LOAD_REGS_2 \
  150. register long int _a2 asm ("rsi") = __arg2; \
  151. LOAD_REGS_1
  152. #define ASM_ARGS_2 ASM_ARGS_1, "r" (_a2)
  153. #define LOAD_ARGS_3(a1, a2, a3) \
  154. long int __arg3 = (long) (a3); \
  155. LOAD_ARGS_2 (a1, a2)
  156. #define LOAD_REGS_3 \
  157. register long int _a3 asm ("rdx") = __arg3; \
  158. LOAD_REGS_2
  159. #define ASM_ARGS_3 ASM_ARGS_2, "r" (_a3)
  160. #define LOAD_ARGS_4(a1, a2, a3, a4) \
  161. long int __arg4 = (long) (a4); \
  162. LOAD_ARGS_3 (a1, a2, a3)
  163. #define LOAD_REGS_4 \
  164. register long int _a4 asm ("r10") = __arg4; \
  165. LOAD_REGS_3
  166. #define ASM_ARGS_4 ASM_ARGS_3, "r" (_a4)
  167. #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
  168. long int __arg5 = (long) (a5); \
  169. LOAD_ARGS_4 (a1, a2, a3, a4)
  170. #define LOAD_REGS_5 \
  171. register long int _a5 asm ("r8") = __arg5; \
  172. LOAD_REGS_4
  173. #define ASM_ARGS_5 ASM_ARGS_4, "r" (_a5)
  174. #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
  175. long int __arg6 = (long) (a6); \
  176. LOAD_ARGS_5 (a1, a2, a3, a4, a5)
  177. #define LOAD_REGS_6 \
  178. register long int _a6 asm ("r9") = __arg6; \
  179. LOAD_REGS_5
  180. #define ASM_ARGS_6 ASM_ARGS_5, "r" (_a6)
  181. #endif /* __ASSEMBLER__ */
  182. #endif /* _BITS_SYSCALLS_H */