syscalls.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #ifndef __ASSEMBLER__
  7. #include <errno.h>
  8. #define SYS_ify(syscall_name) (__NR_##syscall_name)
  9. /* user-visible error numbers are in the range -1 - -4095: see <asm-frv/errno.h> */
  10. #if defined _LIBC && !defined __set_errno
  11. # define __syscall_return(type, res) \
  12. do { \
  13. unsigned long __sr2 = (res); \
  14. if (__builtin_expect ((unsigned long)(__sr2) \
  15. >= (unsigned long)(-4095), 0)) { \
  16. extern int __syscall_error (int); \
  17. return (type) __syscall_error (__sr2); \
  18. } \
  19. return (type) (__sr2); \
  20. } while (0)
  21. #else
  22. # define __syscall_return(type, res) \
  23. do { \
  24. unsigned long __sr2 = (res); \
  25. if (__builtin_expect ((unsigned long)(__sr2) \
  26. >= (unsigned long)(-4095), 0)) { \
  27. __set_errno (-__sr2); \
  28. __sr2 = -1; \
  29. } \
  30. return (type) (__sr2); \
  31. } while (0)
  32. #endif
  33. #define _syscall0(type,name) \
  34. type name(void) { \
  35. long __res; \
  36. __asm__ __volatile__ ( \
  37. "excpt 0;\n\t" \
  38. : "=q0" (__res) \
  39. : "qA" (__NR_##name) \
  40. : "memory","CC"); \
  41. __syscall_return(type,__res); \
  42. }
  43. #define _syscall1(type,name,type1,arg1) \
  44. type name(type1 arg1) { \
  45. long __res; \
  46. __asm__ __volatile__ ( \
  47. "excpt 0;\n\t" \
  48. : "=q0" (__res) \
  49. : "qA" (__NR_##name), \
  50. "q0" ((long)(arg1)) \
  51. : "memory","CC"); \
  52. __syscall_return(type,__res); \
  53. }
  54. #define _syscall2(type,name,type1,arg1,type2,arg2) \
  55. type name(type1 arg1,type2 arg2) { \
  56. long __res; \
  57. __asm__ __volatile__ ( \
  58. "excpt 0;\n\t" \
  59. "%0=r0;\n\t" \
  60. : "=q0" (__res) \
  61. : "qA" (__NR_##name), \
  62. "q0" ((long)(arg1)), \
  63. "q1" ((long)(arg2)) \
  64. : "memory","CC"); \
  65. __syscall_return(type,__res); \
  66. }
  67. #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
  68. type name(type1 arg1,type2 arg2,type3 arg3) { \
  69. long __res; \
  70. __asm__ __volatile__ ( \
  71. "excpt 0;\n\t" \
  72. : "=q0" (__res) \
  73. : "qA" (__NR_##name), \
  74. "q0" ((long)(arg1)), \
  75. "q1" ((long)(arg2)), \
  76. "q2" ((long)(arg3)) \
  77. : "memory","CC"); \
  78. __syscall_return(type,__res); \
  79. }
  80. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4)\
  81. type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
  82. long __res; \
  83. __asm__ __volatile__ ( \
  84. "excpt 0;\n\t" \
  85. : "=q0" (__res) \
  86. : "qA" (__NR_##name), \
  87. "q0" ((long)(arg1)), \
  88. "q1" ((long)(arg2)), \
  89. "q2" ((long)(arg3)), \
  90. "q3" ((long)(arg4)) \
  91. : "memory","CC"); \
  92. __syscall_return(type,__res); \
  93. }
  94. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,type5,arg5) \
  95. type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5) { \
  96. long __res; \
  97. __asm__ __volatile__ ( \
  98. "excpt 0;\n\t" \
  99. : "=q0" (__res) \
  100. : "qA" (__NR_##name), \
  101. "q0" ((long)(arg1)), \
  102. "q1" ((long)(arg2)), \
  103. "q2" ((long)(arg3)), \
  104. "q3" ((long)(arg4)), \
  105. "q4" ((long)(arg5)) \
  106. : "memory","CC"); \
  107. __syscall_return(type,__res); \
  108. }
  109. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,type5,arg5,type6,arg6) \
  110. type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5, type6 arg6) { \
  111. long __res; \
  112. __asm__ __volatile__ ( \
  113. "excpt 0;\n\t" \
  114. : "=q0" (__res) \
  115. : "qA" (__NR_##name), \
  116. "q0" ((long)(arg1)), \
  117. "q1" ((long)(arg2)), \
  118. "q2" ((long)(arg3)), \
  119. "q3" ((long)(arg4)), \
  120. "q4" ((long)(arg5)), \
  121. "q5" ((long)(arg6)) \
  122. : "memory","CC"); \
  123. __syscall_return(type,__res); \
  124. }
  125. /* Define a macro which expands into the inline wrapper code for a system call */
  126. #define INLINE_SYSCALL(name, nr, args...) \
  127. ({ \
  128. INTERNAL_SYSCALL_DECL(err); \
  129. long result_var = INTERNAL_SYSCALL(name, err, nr, args); \
  130. if (INTERNAL_SYSCALL_ERROR_P(result_var, err)) { \
  131. __set_errno(INTERNAL_SYSCALL_ERRNO(result_var, err)); \
  132. result_var = -1L; \
  133. } \
  134. result_var; \
  135. })
  136. #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
  137. #define INTERNAL_SYSCALL_ERROR_P(val, err) ((unsigned long)val >= (unsigned long)(-4095))
  138. #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
  139. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  140. ({ \
  141. long __res; \
  142. __asm__ __volatile__ ( \
  143. "excpt 0;\n\t" \
  144. : "=q0" (__res) \
  145. : "qA" (__NR_##name) ASMFMT_##nr(args) \
  146. : "memory","CC"); \
  147. __res; \
  148. })
  149. #define ASMFMT_0()
  150. #define ASMFMT_1(arg1) \
  151. , "q0" ((long)(arg1))
  152. #define ASMFMT_2(arg1, arg2) \
  153. ASMFMT_1(arg1) \
  154. , "q1" ((long)(arg2))
  155. #define ASMFMT_3(arg1, arg2, arg3) \
  156. ASMFMT_2(arg1, arg2) \
  157. , "q2" ((long)(arg3))
  158. #define ASMFMT_4(arg1, arg2, arg3, arg4) \
  159. ASMFMT_3(arg1, arg2, arg3) \
  160. , "q3" ((long)(arg4))
  161. #define ASMFMT_5(arg1, arg2, arg3, arg4, arg5) \
  162. ASMFMT_4(arg1, arg2, arg3, arg4) \
  163. , "q4" ((long)(arg5))
  164. #define ASMFMT_6(arg1, arg2, arg3, arg4, arg5, arg6) \
  165. ASMFMT_5(arg1, arg2, arg3, arg4, arg5) \
  166. , "q5" ((long)(arg6))
  167. #endif /* __ASSEMBLER__ */
  168. #endif /* _BITS_SYSCALLS_H */