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