syscalls.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. * This includes the `__NR_<name>' syscall numbers taken from the
  8. * Linux kernel header files. It also defines the traditional
  9. * `SYS_<name>' macros for older programs.
  10. */
  11. #include <bits/sysnum.h>
  12. #ifndef __ASSEMBLER__
  13. #include <errno.h>
  14. #define SYS_ify(syscall_name) (__NR_##syscall_name)
  15. #undef _syscall0
  16. #define _syscall0(type,name) \
  17. type name(void) \
  18. { \
  19. return (type)(INLINE_SYSCALL(name, 0)); \
  20. }
  21. #undef _syscall1
  22. #define _syscall1(type,name,type1,arg1) \
  23. type name(type1 arg1) \
  24. { \
  25. return (type)(INLINE_SYSCALL(name, 1, arg1)); \
  26. }
  27. #undef _syscall2
  28. #define _syscall2(type,name,type1,arg1,type2,arg2) \
  29. type name(type1 arg1, type2 arg2) \
  30. { \
  31. return (type)(INLINE_SYSCALL(name, 2, arg1, arg2)); \
  32. }
  33. #undef _syscall3
  34. #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
  35. type name(type1 arg1, type2 arg2, type3 arg3) \
  36. { \
  37. return (type)(INLINE_SYSCALL(name, 3, arg1, \
  38. arg2, arg3)); \
  39. }
  40. #undef _syscall4
  41. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3, \
  42. type4,arg4) \
  43. type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
  44. { \
  45. return (type)(INLINE_SYSCALL(name, 4, arg1, arg2, \
  46. arg3, arg4)); \
  47. }
  48. #undef _syscall5
  49. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3, \
  50. type4,arg4,type5,arg5) \
  51. type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  52. type5 arg5) \
  53. { \
  54. return (type)(INLINE_SYSCALL(name, 5, arg1, arg2, \
  55. arg3, arg4, arg5)); \
  56. }
  57. #undef _syscall6
  58. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3, \
  59. type4,arg4,type5,arg5,type6,arg6) \
  60. type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
  61. type5 arg5, type6 arg6) \
  62. { \
  63. return (type)(INLINE_SYSCALL(name, 6, arg1, arg2, arg3, \
  64. arg4, arg5, arg6)); \
  65. }
  66. #undef unlikely
  67. #define unlikely(x) __builtin_expect((x), 0)
  68. #undef INLINE_SYSCALL
  69. #define INLINE_SYSCALL(name, nr, args...) \
  70. ({ \
  71. unsigned _sys_result = INTERNAL_SYSCALL(name, , nr, args); \
  72. if (unlikely(INTERNAL_SYSCALL_ERROR_P(_sys_result, ))) { \
  73. __set_errno(INTERNAL_SYSCALL_ERRNO(_sys_result, )); \
  74. _sys_result = (unsigned int) -1; \
  75. } \
  76. (int) _sys_result; \
  77. })
  78. #undef INTERNAL_SYSCALL_DECL
  79. #define INTERNAL_SYSCALL_DECL(err) do { } while(0)
  80. #undef INTERNAL_SYSCALL
  81. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  82. ({ \
  83. register int _a1 __asm__("r12"); \
  84. register int _scno __asm__("r8") = SYS_ify(name); \
  85. LOAD_ARGS_##nr (args); \
  86. __asm__ __volatile__("scall /* syscall " #name " */" \
  87. : "=r" (_a1) \
  88. : "r"(_scno) ASM_ARGS_##nr \
  89. : "cc", "memory"); \
  90. _a1; \
  91. })
  92. #undef INTERNAL_SYSCALL_ERROR_P
  93. #define INTERNAL_SYSCALL_ERROR_P(val, err) \
  94. ((unsigned int)(val) >= 0xfffff001U)
  95. #undef INTERNAL_SYSCALL_ERRNO
  96. #define INTERNAL_SYSCALL_ERRNO(val, errr) (-(val))
  97. #define LOAD_ARGS_0() do { } while(0)
  98. #define ASM_ARGS_0
  99. #define LOAD_ARGS_1(a1) \
  100. _a1 = (int) (a1); \
  101. LOAD_ARGS_0()
  102. #define ASM_ARGS_1 ASM_ARGS_0, "r"(_a1)
  103. #define LOAD_ARGS_2(a1, a2) \
  104. register int _a2 __asm__("r11") = (int)(a2); \
  105. LOAD_ARGS_1(a1)
  106. #define ASM_ARGS_2 ASM_ARGS_1, "r"(_a2)
  107. #define LOAD_ARGS_3(a1, a2, a3) \
  108. register int _a3 __asm__("r10") = (int)(a3); \
  109. LOAD_ARGS_2(a1, a2)
  110. #define ASM_ARGS_3 ASM_ARGS_2, "r"(_a3)
  111. #define LOAD_ARGS_4(a1, a2, a3, a4) \
  112. register int _a4 __asm__("r9") = (int)(a4); \
  113. LOAD_ARGS_3(a1, a2, a3)
  114. #define ASM_ARGS_4 ASM_ARGS_3, "r"(_a4)
  115. #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
  116. register int _a5 __asm__("r5") = (int)(a5); \
  117. LOAD_ARGS_4(a1, a2, a3, a4)
  118. #define ASM_ARGS_5 ASM_ARGS_4, "r"(_a5)
  119. #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
  120. register int _a6 __asm__("r3") = (int)(a6); \
  121. LOAD_ARGS_5(a1, a2, a3, a4, a5)
  122. #define ASM_ARGS_6 ASM_ARGS_5, "r"(_a6)
  123. #endif /* __ASSEMBLER__ */
  124. #endif /* _BITS_SYSCALLS_H */