syscalls.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. /* The Linux kernel uses different trap numbers on sh-2. */
  7. #ifdef __CONFIG_SH2__
  8. # define __SH_SYSCALL_TRAP_BASE 0x20
  9. #else
  10. # define __SH_SYSCALL_TRAP_BASE 0x10
  11. #endif
  12. #ifndef __ASSEMBLER__
  13. #include <errno.h>
  14. #define SYS_ify(syscall_name) (__NR_##syscall_name)
  15. /* user-visible error numbers are in the range -1 - -125: see <asm-sh/errno.h> */
  16. #define __syscall_return(type, res) \
  17. do { \
  18. if ((unsigned long)(res) >= (unsigned long)(-125)) { \
  19. /* Avoid using "res" which is declared to be in register r0; \
  20. errno might expand to a function call and clobber it. */ \
  21. int __err = -(res); \
  22. __set_errno(__err); \
  23. res = -1; \
  24. } \
  25. return (type) (res); \
  26. } while (0)
  27. /* XXX - _foo needs to be __foo, while __NR_bar could be _NR_bar. */
  28. #define _syscall0(type,name) \
  29. type name(void) \
  30. { \
  31. register long __sc0 __asm__ ("r3") = __NR_##name; \
  32. __asm__ __volatile__ ("trapa %1" \
  33. : "=z" (__sc0) \
  34. : "i" (__SH_SYSCALL_TRAP_BASE), "0" (__sc0) \
  35. : "memory" ); \
  36. __syscall_return(type,__sc0); \
  37. }
  38. #define _syscall1(type,name,type1,arg1) \
  39. type name(type1 arg1) \
  40. { \
  41. register long __sc0 __asm__ ("r3") = __NR_##name; \
  42. register long __sc4 __asm__ ("r4") = (long) arg1; \
  43. __asm__ __volatile__ ("trapa %1" \
  44. : "=z" (__sc0) \
  45. : "i" (__SH_SYSCALL_TRAP_BASE + 1), "0" (__sc0), "r" (__sc4) \
  46. : "memory"); \
  47. __syscall_return(type,__sc0); \
  48. }
  49. #define _syscall2(type,name,type1,arg1,type2,arg2) \
  50. type name(type1 arg1,type2 arg2) \
  51. { \
  52. register long __sc0 __asm__ ("r3") = __NR_##name; \
  53. register long __sc4 __asm__ ("r4") = (long) arg1; \
  54. register long __sc5 __asm__ ("r5") = (long) arg2; \
  55. __asm__ __volatile__ ("trapa %1" \
  56. : "=z" (__sc0) \
  57. : "i" (__SH_SYSCALL_TRAP_BASE + 2), "0" (__sc0), "r" (__sc4), \
  58. "r" (__sc5) \
  59. : "memory"); \
  60. __syscall_return(type,__sc0); \
  61. }
  62. #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
  63. type name(type1 arg1,type2 arg2,type3 arg3) \
  64. { \
  65. register long __sc0 __asm__ ("r3") = __NR_##name; \
  66. register long __sc4 __asm__ ("r4") = (long) arg1; \
  67. register long __sc5 __asm__ ("r5") = (long) arg2; \
  68. register long __sc6 __asm__ ("r6") = (long) arg3; \
  69. __asm__ __volatile__ ("trapa %1" \
  70. : "=z" (__sc0) \
  71. : "i" (__SH_SYSCALL_TRAP_BASE + 3), "0" (__sc0), "r" (__sc4), \
  72. "r" (__sc5), "r" (__sc6) \
  73. : "memory"); \
  74. __syscall_return(type,__sc0); \
  75. }
  76. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
  77. type name (type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
  78. { \
  79. register long __sc0 __asm__ ("r3") = __NR_##name; \
  80. register long __sc4 __asm__ ("r4") = (long) arg1; \
  81. register long __sc5 __asm__ ("r5") = (long) arg2; \
  82. register long __sc6 __asm__ ("r6") = (long) arg3; \
  83. register long __sc7 __asm__ ("r7") = (long) arg4; \
  84. __asm__ __volatile__ ("trapa %1" \
  85. : "=z" (__sc0) \
  86. : "i" (__SH_SYSCALL_TRAP_BASE + 4), "0" (__sc0), "r" (__sc4), \
  87. "r" (__sc5), "r" (__sc6), \
  88. "r" (__sc7) \
  89. : "memory" ); \
  90. __syscall_return(type,__sc0); \
  91. }
  92. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,type5,arg5) \
  93. type name (type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5) \
  94. { \
  95. register long __sc3 __asm__ ("r3") = __NR_##name; \
  96. register long __sc4 __asm__ ("r4") = (long) arg1; \
  97. register long __sc5 __asm__ ("r5") = (long) arg2; \
  98. register long __sc6 __asm__ ("r6") = (long) arg3; \
  99. register long __sc7 __asm__ ("r7") = (long) arg4; \
  100. register long __sc0 __asm__ ("r0") = (long) arg5; \
  101. __asm__ __volatile__ ("trapa %1" \
  102. : "=z" (__sc0) \
  103. : "i" (__SH_SYSCALL_TRAP_BASE + 5), "0" (__sc0), "r" (__sc4), \
  104. "r" (__sc5), "r" (__sc6), "r" (__sc7), "r" (__sc3) \
  105. : "memory" ); \
  106. __syscall_return(type,__sc0); \
  107. }
  108. #ifndef __SH_SYSCALL6_TRAPA
  109. #define __SH_SYSCALL6_TRAPA __SH_SYSCALL_TRAP_BASE + 6
  110. #endif
  111. /* Add in _syscall6 which is not in the kernel header */
  112. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,type5,arg5,type6,arg6) \
  113. type name (type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5, type6 arg6) \
  114. { \
  115. register long __sc3 __asm__ ("r3") = __NR_##name; \
  116. register long __sc4 __asm__ ("r4") = (long) arg1; \
  117. register long __sc5 __asm__ ("r5") = (long) arg2; \
  118. register long __sc6 __asm__ ("r6") = (long) arg3; \
  119. register long __sc7 __asm__ ("r7") = (long) arg4; \
  120. register long __sc0 __asm__ ("r0") = (long) arg5; \
  121. register long __sc1 __asm__ ("r1") = (long) arg6; \
  122. __asm__ __volatile__ ("trapa %1" \
  123. : "=z" (__sc0) \
  124. : "i" (__SH_SYSCALL6_TRAPA), "0" (__sc0), "r" (__sc4), \
  125. "r" (__sc5), "r" (__sc6), "r" (__sc7), "r" (__sc3), "r" (__sc1) \
  126. : "memory" ); \
  127. __syscall_return(type,__sc0); \
  128. }
  129. #define SYSCALL_INST_STR(x) "trapa #"__stringify(__SH_SYSCALL_TRAP_BASE + x)"\n\t"
  130. #define SYSCALL_INST_STR0 SYSCALL_INST_STR(0)
  131. #define SYSCALL_INST_STR1 SYSCALL_INST_STR(1)
  132. #define SYSCALL_INST_STR2 SYSCALL_INST_STR(2)
  133. #define SYSCALL_INST_STR3 SYSCALL_INST_STR(3)
  134. #define SYSCALL_INST_STR4 SYSCALL_INST_STR(4)
  135. #define SYSCALL_INST_STR5 SYSCALL_INST_STR(5)
  136. #define SYSCALL_INST_STR6 SYSCALL_INST_STR(6)
  137. # ifdef NEED_SYSCALL_INST_PAD
  138. # define SYSCALL_INST_PAD "\
  139. or r0,r0; or r0,r0; or r0,r0; or r0,r0; or r0,r0"
  140. # else
  141. # define SYSCALL_INST_PAD
  142. # endif
  143. #define ASMFMT_0
  144. #define ASMFMT_1 \
  145. , "r" (r4)
  146. #define ASMFMT_2 \
  147. , "r" (r4), "r" (r5)
  148. #define ASMFMT_3 \
  149. , "r" (r4), "r" (r5), "r" (r6)
  150. #define ASMFMT_4 \
  151. , "r" (r4), "r" (r5), "r" (r6), "r" (r7)
  152. #define ASMFMT_5 \
  153. , "r" (r4), "r" (r5), "r" (r6), "r" (r7), "0" (r0)
  154. #define ASMFMT_6 \
  155. , "r" (r4), "r" (r5), "r" (r6), "r" (r7), "0" (r0), "r" (r1)
  156. #define ASMFMT_7 \
  157. , "r" (r4), "r" (r5), "r" (r6), "r" (r7), "0" (r0), "r" (r1), "r" (r2)
  158. #define SUBSTITUTE_ARGS_0()
  159. #define SUBSTITUTE_ARGS_1(arg1) \
  160. long int _arg1 = (long int) (arg1); \
  161. register long int r4 __asm__ ("%r4") = (long int) (_arg1)
  162. #define SUBSTITUTE_ARGS_2(arg1, arg2) \
  163. long int _arg1 = (long int) (arg1); \
  164. long int _arg2 = (long int) (arg2); \
  165. register long int r4 __asm__ ("%r4") = (long int) (_arg1); \
  166. register long int r5 __asm__ ("%r5") = (long int) (_arg2)
  167. #define SUBSTITUTE_ARGS_3(arg1, arg2, arg3) \
  168. long int _arg1 = (long int) (arg1); \
  169. long int _arg2 = (long int) (arg2); \
  170. long int _arg3 = (long int) (arg3); \
  171. register long int r4 __asm__ ("%r4") = (long int) (_arg1); \
  172. register long int r5 __asm__ ("%r5") = (long int) (_arg2); \
  173. register long int r6 __asm__ ("%r6") = (long int) (_arg3)
  174. #define SUBSTITUTE_ARGS_4(arg1, arg2, arg3, arg4) \
  175. long int _arg1 = (long int) (arg1); \
  176. long int _arg2 = (long int) (arg2); \
  177. long int _arg3 = (long int) (arg3); \
  178. long int _arg4 = (long int) (arg4); \
  179. register long int r4 __asm__ ("%r4") = (long int) (_arg1); \
  180. register long int r5 __asm__ ("%r5") = (long int) (_arg2); \
  181. register long int r6 __asm__ ("%r6") = (long int) (_arg3); \
  182. register long int r7 __asm__ ("%r7") = (long int) (_arg4)
  183. #define SUBSTITUTE_ARGS_5(arg1, arg2, arg3, arg4, arg5) \
  184. long int _arg1 = (long int) (arg1); \
  185. long int _arg2 = (long int) (arg2); \
  186. long int _arg3 = (long int) (arg3); \
  187. long int _arg4 = (long int) (arg4); \
  188. long int _arg5 = (long int) (arg5); \
  189. register long int r4 __asm__ ("%r4") = (long int) (_arg1); \
  190. register long int r5 __asm__ ("%r5") = (long int) (_arg2); \
  191. register long int r6 __asm__ ("%r6") = (long int) (_arg3); \
  192. register long int r7 __asm__ ("%r7") = (long int) (_arg4); \
  193. register long int r0 __asm__ ("%r0") = (long int) (_arg5)
  194. #define SUBSTITUTE_ARGS_6(arg1, arg2, arg3, arg4, arg5, arg6) \
  195. long int _arg1 = (long int) (arg1); \
  196. long int _arg2 = (long int) (arg2); \
  197. long int _arg3 = (long int) (arg3); \
  198. long int _arg4 = (long int) (arg4); \
  199. long int _arg5 = (long int) (arg5); \
  200. long int _arg6 = (long int) (arg6); \
  201. register long int r4 __asm__ ("%r4") = (long int)(_arg1); \
  202. register long int r5 __asm__ ("%r5") = (long int) (_arg2); \
  203. register long int r6 __asm__ ("%r6") = (long int) (_arg3); \
  204. register long int r7 __asm__ ("%r7") = (long int) (_arg4); \
  205. register long int r0 __asm__ ("%r0") = (long int) (_arg5); \
  206. register long int r1 __asm__ ("%r1") = (long int) (_arg6)
  207. #define SUBSTITUTE_ARGS_7(arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
  208. long int _arg1 = (long int) (arg1); \
  209. long int _arg2 = (long int) (arg2); \
  210. long int _arg3 = (long int) (arg3); \
  211. long int _arg4 = (long int) (arg4); \
  212. long int _arg5 = (long int) (arg5); \
  213. long int _arg6 = (long int) (arg6); \
  214. long int _arg7 = (long int) (arg7); \
  215. register long int r4 __asm__ ("%r4") = (long int) (_arg1); \
  216. register long int r5 __asm__ ("%r5") = (long int) (_arg2); \
  217. register long int r6 __asm__ ("%r6") = (long int) (_arg3); \
  218. register long int r7 __asm__ ("%r7") = (long int) (_arg4); \
  219. register long int r0 __asm__ ("%r0") = (long int) (_arg5); \
  220. register long int r1 __asm__ ("%r1") = (long int) (_arg6); \
  221. register long int r2 __asm__ ("%r2") = (long int) (_arg7)
  222. #undef INLINE_SYSCALL
  223. #define INLINE_SYSCALL(name, nr, args...) \
  224. ({ \
  225. unsigned int __resultvar = INTERNAL_SYSCALL (name, , nr, args); \
  226. if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (__resultvar, ), 0)) \
  227. { \
  228. __set_errno (INTERNAL_SYSCALL_ERRNO (__resultvar, )); \
  229. __resultvar = 0xffffffff; \
  230. } \
  231. (int) __resultvar; })
  232. #undef INTERNAL_SYSCALL
  233. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  234. ({ \
  235. unsigned long int resultvar; \
  236. register long int r3 __asm__ ("%r3") = SYS_ify (name); \
  237. SUBSTITUTE_ARGS_##nr(args); \
  238. \
  239. __asm__ volatile (SYSCALL_INST_STR##nr SYSCALL_INST_PAD \
  240. : "=z" (resultvar) \
  241. : "r" (r3) ASMFMT_##nr \
  242. : "memory"); \
  243. \
  244. (int) resultvar; })
  245. /* The _NCS variant allows non-constant syscall numbers. */
  246. #define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
  247. ({ \
  248. unsigned long int resultvar; \
  249. register long int r3 __asm__ ("%r3") = (name); \
  250. SUBSTITUTE_ARGS_##nr(args); \
  251. \
  252. __asm__ volatile (SYSCALL_INST_STR##nr SYSCALL_INST_PAD \
  253. : "=z" (resultvar) \
  254. : "r" (r3) ASMFMT_##nr \
  255. : "memory"); \
  256. \
  257. (int) resultvar; })
  258. #undef INTERNAL_SYSCALL_DECL
  259. #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
  260. #undef INTERNAL_SYSCALL_ERROR_P
  261. #define INTERNAL_SYSCALL_ERROR_P(val, err) \
  262. ((unsigned int) (val) >= 0xfffff001u)
  263. #undef INTERNAL_SYSCALL_ERRNO
  264. #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
  265. #endif /* __ASSEMBLER__ */
  266. #endif /* _BITS_SYSCALLS_H */