syscalls.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. /* The Linux/x86-64 kernel expects the system call parameters in
  13. registers according to the following table:
  14. syscall number rax
  15. arg 1 rdi
  16. arg 2 rsi
  17. arg 3 rdx
  18. arg 4 r10
  19. arg 5 r8
  20. arg 6 r9
  21. The Linux kernel uses and destroys internally these registers:
  22. return address from
  23. syscall rcx
  24. additionally clobered: r12-r15,rbx,rbp
  25. eflags from syscall r11
  26. Normal function call, including calls to the system call stub
  27. functions in the libc, get the first six parameters passed in
  28. registers and the seventh parameter and later on the stack. The
  29. register use is as follows:
  30. system call number in the DO_CALL macro
  31. arg 1 rdi
  32. arg 2 rsi
  33. arg 3 rdx
  34. arg 4 rcx
  35. arg 5 r8
  36. arg 6 r9
  37. We have to take care that the stack is aligned to 16 bytes. When
  38. called the stack is not aligned since the return address has just
  39. been pushed.
  40. Syscalls of more than 6 arguments are not supported. */
  41. #undef DO_CALL
  42. #define DO_CALL(syscall_name, args) \
  43. DOARGS_##args \
  44. movq $SYS_ify (syscall_name), %rax; \
  45. syscall;
  46. #define DOARGS_0 /* nothing */
  47. #define DOARGS_1 /* nothing */
  48. #define DOARGS_2 /* nothing */
  49. #define DOARGS_3 /* nothing */
  50. #define DOARGS_4 movq %rcx, %r10;
  51. #define DOARGS_5 DOARGS_4
  52. #define DOARGS_6 DOARGS_5
  53. /* Define a macro which expands inline into the wrapper code for a system
  54. call. */
  55. #define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
  56. (__extension__ \
  57. ({ \
  58. unsigned long resultvar; \
  59. LOAD_ARGS_##nr (args) \
  60. LOAD_REGS_##nr \
  61. __asm__ __volatile__ ( \
  62. "syscall\n\t" \
  63. : "=a" (resultvar) \
  64. : "0" (name) ASM_ARGS_##nr : "memory", "cc", "r11", "cx"); \
  65. (long) resultvar; \
  66. }) \
  67. )
  68. #define LOAD_ARGS_0()
  69. #define LOAD_REGS_0
  70. #define ASM_ARGS_0
  71. #define LOAD_ARGS_1(a1) \
  72. long int __arg1 = (long) (a1); \
  73. LOAD_ARGS_0 ()
  74. #define LOAD_REGS_1 \
  75. register long int __a1 __asm__ ("rdi") = __arg1; \
  76. LOAD_REGS_0
  77. #define ASM_ARGS_1 ASM_ARGS_0, "r" (__a1)
  78. #define LOAD_ARGS_2(a1, a2) \
  79. long int __arg2 = (long) (a2); \
  80. LOAD_ARGS_1 (a1)
  81. #define LOAD_REGS_2 \
  82. register long int __a2 __asm__ ("rsi") = __arg2; \
  83. LOAD_REGS_1
  84. #define ASM_ARGS_2 ASM_ARGS_1, "r" (__a2)
  85. #define LOAD_ARGS_3(a1, a2, a3) \
  86. long int __arg3 = (long) (a3); \
  87. LOAD_ARGS_2 (a1, a2)
  88. #define LOAD_REGS_3 \
  89. register long int __a3 __asm__ ("rdx") = __arg3; \
  90. LOAD_REGS_2
  91. #define ASM_ARGS_3 ASM_ARGS_2, "r" (__a3)
  92. #define LOAD_ARGS_4(a1, a2, a3, a4) \
  93. long int __arg4 = (long) (a4); \
  94. LOAD_ARGS_3 (a1, a2, a3)
  95. #define LOAD_REGS_4 \
  96. register long int __a4 __asm__ ("r10") = __arg4; \
  97. LOAD_REGS_3
  98. #define ASM_ARGS_4 ASM_ARGS_3, "r" (__a4)
  99. #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
  100. long int __arg5 = (long) (a5); \
  101. LOAD_ARGS_4 (a1, a2, a3, a4)
  102. #define LOAD_REGS_5 \
  103. register long int __a5 __asm__ ("r8") = __arg5; \
  104. LOAD_REGS_4
  105. #define ASM_ARGS_5 ASM_ARGS_4, "r" (__a5)
  106. #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
  107. long int __arg6 = (long) (a6); \
  108. LOAD_ARGS_5 (a1, a2, a3, a4, a5)
  109. #define LOAD_REGS_6 \
  110. register long int __a6 __asm__ ("r9") = __arg6; \
  111. LOAD_REGS_5
  112. #define ASM_ARGS_6 ASM_ARGS_5, "r" (__a6)
  113. #endif /* __ASSEMBLER__ */
  114. #endif /* _BITS_SYSCALLS_H */