syscalls.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. ({ \
  57. unsigned long resultvar; \
  58. LOAD_ARGS_##nr (args) \
  59. LOAD_REGS_##nr \
  60. __asm__ __volatile__ ( \
  61. "syscall\n\t" \
  62. : "=a" (resultvar) \
  63. : "0" (name) ASM_ARGS_##nr : "memory", "cc", "r11", "cx"); \
  64. (long) resultvar; })
  65. #define LOAD_ARGS_0()
  66. #define LOAD_REGS_0
  67. #define ASM_ARGS_0
  68. #define LOAD_ARGS_1(a1) \
  69. long int __arg1 = (long) (a1); \
  70. LOAD_ARGS_0 ()
  71. #define LOAD_REGS_1 \
  72. register long int __a1 __asm__ ("rdi") = __arg1; \
  73. LOAD_REGS_0
  74. #define ASM_ARGS_1 ASM_ARGS_0, "r" (__a1)
  75. #define LOAD_ARGS_2(a1, a2) \
  76. long int __arg2 = (long) (a2); \
  77. LOAD_ARGS_1 (a1)
  78. #define LOAD_REGS_2 \
  79. register long int __a2 __asm__ ("rsi") = __arg2; \
  80. LOAD_REGS_1
  81. #define ASM_ARGS_2 ASM_ARGS_1, "r" (__a2)
  82. #define LOAD_ARGS_3(a1, a2, a3) \
  83. long int __arg3 = (long) (a3); \
  84. LOAD_ARGS_2 (a1, a2)
  85. #define LOAD_REGS_3 \
  86. register long int __a3 __asm__ ("rdx") = __arg3; \
  87. LOAD_REGS_2
  88. #define ASM_ARGS_3 ASM_ARGS_2, "r" (__a3)
  89. #define LOAD_ARGS_4(a1, a2, a3, a4) \
  90. long int __arg4 = (long) (a4); \
  91. LOAD_ARGS_3 (a1, a2, a3)
  92. #define LOAD_REGS_4 \
  93. register long int __a4 __asm__ ("r10") = __arg4; \
  94. LOAD_REGS_3
  95. #define ASM_ARGS_4 ASM_ARGS_3, "r" (__a4)
  96. #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
  97. long int __arg5 = (long) (a5); \
  98. LOAD_ARGS_4 (a1, a2, a3, a4)
  99. #define LOAD_REGS_5 \
  100. register long int __a5 __asm__ ("r8") = __arg5; \
  101. LOAD_REGS_4
  102. #define ASM_ARGS_5 ASM_ARGS_4, "r" (__a5)
  103. #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
  104. long int __arg6 = (long) (a6); \
  105. LOAD_ARGS_5 (a1, a2, a3, a4, a5)
  106. #define LOAD_REGS_6 \
  107. register long int __a6 __asm__ ("r9") = __arg6; \
  108. LOAD_REGS_5
  109. #define ASM_ARGS_6 ASM_ARGS_5, "r" (__a6)
  110. #endif /* __ASSEMBLER__ */
  111. #endif /* _BITS_SYSCALLS_H */