syscalls-common.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Common syscall type defines
  3. *
  4. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  5. */
  6. #ifndef _SYSCALLS_COMMON_H
  7. #define _SYSCALLS_COMMON_H 1
  8. #ifndef _SYSCALL_H
  9. # error "Never use <bits/syscalls-common.h> directly; include <sys/syscall.h> instead."
  10. #endif
  11. #ifndef SYS_ify
  12. # define SYS_ify(syscall_name) (__NR_##syscall_name)
  13. #endif
  14. #ifndef __ASSEMBLER__
  15. #include <errno.h>
  16. #ifndef INTERNAL_SYSCALL_DECL
  17. # define INTERNAL_SYSCALL_DECL(err) do { } while (0)
  18. #endif
  19. #ifndef INTERNAL_SYSCALL_ERROR_P
  20. # define INTERNAL_SYSCALL_ERROR_P(val, err) ((unsigned long)val >= (unsigned long)(-4095))
  21. #endif
  22. #ifndef INTERNAL_SYSCALL_ERRNO
  23. # define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
  24. #endif
  25. /* Define a macro which expands into the inline wrapper code for a system call */
  26. #ifndef INLINE_SYSCALL
  27. # define INLINE_SYSCALL(name, nr, args...) INLINE_SYSCALL_NCS(__NR_##name, nr, args)
  28. #endif
  29. #ifndef INLINE_SYSCALL_NOERR
  30. # define INLINE_SYSCALL_NOERR(name, nr, args...) INLINE_SYSCALL_NOERR_NCS(__NR_##name, nr, args)
  31. #endif
  32. /* Just like INLINE_SYSCALL(), but take a non-constant syscall (NCS) argument */
  33. #ifndef INLINE_SYSCALL_NCS
  34. # define INLINE_SYSCALL_NCS(num, nr, args...) \
  35. (__extension__ \
  36. ({ \
  37. long __res; \
  38. INTERNAL_SYSCALL_DECL(__err); \
  39. (__extension__ \
  40. ({ \
  41. __res = INTERNAL_SYSCALL_NCS(num, __err, nr, args); \
  42. if (unlikely(INTERNAL_SYSCALL_ERROR_P(__res, __err))) { \
  43. __set_errno(INTERNAL_SYSCALL_ERRNO(__res, __err)); \
  44. __res = -1L; \
  45. } \
  46. __res; \
  47. }) \
  48. ); \
  49. }) \
  50. )
  51. #endif
  52. #ifndef INLINE_SYSCALL_NOERR_NCS
  53. # define INLINE_SYSCALL_NOERR_NCS(num, nr, args...) \
  54. ({ \
  55. long __res; \
  56. INTERNAL_SYSCALL_DECL(__err); \
  57. __res = INTERNAL_SYSCALL_NCS(num, __err, nr, args); \
  58. __res; \
  59. })
  60. #endif
  61. /* No point in forcing people to implement both when they only need one */
  62. #ifndef INTERNAL_SYSCALL
  63. # define INTERNAL_SYSCALL(name, err, nr, args...) INTERNAL_SYSCALL_NCS(__NR_##name, err, nr, args)
  64. #endif
  65. #ifndef INTERNAL_SYSCALL_NCS
  66. # error your port needs to define INTERNAL_SYSCALL_NCS in bits/syscalls.h
  67. #endif
  68. #ifndef _syscall0
  69. #define C_DECL_ARGS_0() void
  70. #define C_DECL_ARGS_1(t, v) t v
  71. #define C_DECL_ARGS_2(t, v, args...) t v, C_DECL_ARGS_1(args)
  72. #define C_DECL_ARGS_3(t, v, args...) t v, C_DECL_ARGS_2(args)
  73. #define C_DECL_ARGS_4(t, v, args...) t v, C_DECL_ARGS_3(args)
  74. #define C_DECL_ARGS_5(t, v, args...) t v, C_DECL_ARGS_4(args)
  75. #define C_DECL_ARGS_6(t, v, args...) t v, C_DECL_ARGS_5(args)
  76. #define C_ARGS_0()
  77. #define C_ARGS_1(t, v) v
  78. #define C_ARGS_2(t, v, args...) v, C_ARGS_1(args)
  79. #define C_ARGS_3(t, v, args...) v, C_ARGS_2(args)
  80. #define C_ARGS_4(t, v, args...) v, C_ARGS_3(args)
  81. #define C_ARGS_5(t, v, args...) v, C_ARGS_4(args)
  82. #define C_ARGS_6(t, v, args...) v, C_ARGS_5(args)
  83. #define SYSCALL_FUNC(nargs, type, name, args...) \
  84. type name(C_DECL_ARGS_##nargs(args)) { \
  85. return (type)INLINE_SYSCALL(name, nargs, C_ARGS_##nargs(args)); \
  86. }
  87. #define SYSCALL_FUNC_TIME64(nargs, type, name, args...) \
  88. type name(C_DECL_ARGS_##nargs(args)) { \
  89. return (type)INLINE_SYSCALL(name##_time64, nargs, C_ARGS_##nargs(args)); \
  90. }
  91. #define SYSCALL_FUNC_64(nargs, type, name, args...) \
  92. type name(C_DECL_ARGS_##nargs(args)) { \
  93. return (type)INLINE_SYSCALL(name##64, nargs, C_ARGS_##nargs(args)); \
  94. }
  95. #define SYSCALL_NOERR_FUNC(nargs, type, name, args...) \
  96. type name(C_DECL_ARGS_##nargs(args)) { \
  97. return (type)INLINE_SYSCALL_NOERR(name, nargs, C_ARGS_##nargs(args)); \
  98. }
  99. #define SYSCALL_NOERR_FUNC_TIME64(nargs, type, name, args...) \
  100. type name(C_DECL_ARGS_##nargs(args)) { \
  101. return (type)INLINE_SYSCALL_NOERR(name##_time64, nargs, C_ARGS_##nargs(args)); \
  102. }
  103. #define SYSCALL_NOERR_FUNC_64(nargs, type, name, args...) \
  104. type name(C_DECL_ARGS_##nargs(args)) { \
  105. return (type)INLINE_SYSCALL_NOERR(name##64, nargs, C_ARGS_##nargs(args)); \
  106. }
  107. #define SYSCALL_FUNC_BODY(nargs, type, name, args...) \
  108. return (type)INLINE_SYSCALL(name, nargs, C_ARGS_##nargs(args));
  109. #define SYSCALL_FUNC_BODY_TIME64(nargs, type, name, args...) \
  110. return (type)INLINE_SYSCALL(name##_time64, nargs, C_ARGS_##nargs(args));
  111. #define SYSCALL_FUNC_BODY_64(nargs, type, name, args...) \
  112. return (type)INLINE_SYSCALL(name##64, nargs, C_ARGS_##nargs(args));
  113. #define _syscall0(args...) SYSCALL_FUNC(0, args)
  114. #define _syscall_noerr0(args...) SYSCALL_NOERR_FUNC(0, args)
  115. #define _syscall1(args...) SYSCALL_FUNC(1, args)
  116. #define _syscall_noerr1(args...) SYSCALL_NOERR_FUNC(1, args)
  117. #define _syscall2(args...) SYSCALL_FUNC(2, args)
  118. #define _syscall2_body(args...) SYSCALL_FUNC_BODY(2, args)
  119. #define _syscall3(args...) SYSCALL_FUNC(3, args)
  120. #define _syscall4(args...) SYSCALL_FUNC(4, args)
  121. #define _syscall5(args...) SYSCALL_FUNC(5, args)
  122. #define _syscall6(args...) SYSCALL_FUNC(6, args)
  123. #define _syscall0_time64(args...) SYSCALL_FUNC_TIME64(0, args)
  124. #define _syscall_noerr0_time64(args...) SYSCALL_NOERR_FUNC_TIME64(0, args)
  125. #define _syscall1_time64(args...) SYSCALL_FUNC_TIME64(1, args)
  126. #define _syscall_noerr1_time64(args...) SYSCALL_NOERR_FUNC_TIME64(1, args)
  127. #define _syscall2_time64(args...) SYSCALL_FUNC_TIME64(2, args)
  128. #define _syscall2_body_time64(args...) SYSCALL_FUNC_BODY_TIME64(2, args)
  129. #define _syscall3_time64(args...) SYSCALL_FUNC_TIME64(3, args)
  130. #define _syscall4_time64(args...) SYSCALL_FUNC_TIME64(4, args)
  131. #define _syscall5_time64(args...) SYSCALL_FUNC_TIME64(5, args)
  132. #define _syscall6_time64(args...) SYSCALL_FUNC_TIME64(6, args)
  133. #define _syscall0_64(args...) SYSCALL_FUNC_64(0, args)
  134. #define _syscall_noerr0_64(args...) SYSCALL_NOERR_FUNC_64(0, args)
  135. #define _syscall1_64(args...) SYSCALL_FUNC_64(1, args)
  136. #define _syscall_noerr1_64(args...) SYSCALL_NOERR_FUNC_64(1, args)
  137. #define _syscall2_64(args...) SYSCALL_FUNC_64(2, args)
  138. #define _syscall2_body_64(args...) SYSCALL_FUNC_BODY_64(2, args)
  139. #define _syscall3_64(args...) SYSCALL_FUNC_64(3, args)
  140. #define _syscall4_64(args...) SYSCALL_FUNC_64(4, args)
  141. #define _syscall5_64(args...) SYSCALL_FUNC_64(5, args)
  142. #define _syscall6_64(args...) SYSCALL_FUNC_64(6, args)
  143. #endif /* _syscall0 */
  144. #endif /* __ASSEMBLER__ */
  145. #endif