syscalls.h 5.9 KB

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