syscalls.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. /* This includes the `__NR_<name>' syscall numbers taken from the Linux kernel
  7. * header files. It also defines the traditional `SYS_<name>' macros for older
  8. * programs. */
  9. #include <bits/sysnum.h>
  10. /*
  11. Some of the sneaky macros in the code were taken from
  12. glibc-2.3.2/sysdeps/unix/sysv/linux/arm/sysdep.h
  13. */
  14. #define SYS_ify(syscall_name) (__NR_##syscall_name)
  15. #ifdef __ASSEMBLER__
  16. /* Call a given syscall, with arguments loaded. For EABI, we must
  17. save and restore r7 for the syscall number. Unlike the DO_CALL
  18. macro in glibc, this macro does not load syscall arguments. */
  19. #undef DO_CALL
  20. #if defined(__ARM_EABI__)
  21. #define DO_CALL(syscall_name) \
  22. mov ip, r7; \
  23. ldr r7, =SYS_ify (syscall_name); \
  24. swi 0x0; \
  25. mov r7, ip;
  26. #else
  27. #define DO_CALL(syscall_name) \
  28. swi SYS_ify (syscall_name);
  29. #endif
  30. #else
  31. #include <errno.h>
  32. #undef _syscall0
  33. #define _syscall0(type,name) \
  34. type name(void) \
  35. { \
  36. return (type) (INLINE_SYSCALL(name, 0)); \
  37. }
  38. #undef _syscall1
  39. #define _syscall1(type,name,type1,arg1) \
  40. type name(type1 arg1) \
  41. { \
  42. return (type) (INLINE_SYSCALL(name, 1, arg1)); \
  43. }
  44. #undef _syscall2
  45. #define _syscall2(type,name,type1,arg1,type2,arg2) \
  46. type name(type1 arg1,type2 arg2) \
  47. { \
  48. return (type) (INLINE_SYSCALL(name, 2, arg1, arg2)); \
  49. }
  50. #undef _syscall3
  51. #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
  52. type name(type1 arg1,type2 arg2,type3 arg3) \
  53. { \
  54. return (type) (INLINE_SYSCALL(name, 3, arg1, arg2, arg3)); \
  55. }
  56. #undef _syscall4
  57. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
  58. type name (type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
  59. { \
  60. return (type) (INLINE_SYSCALL(name, 4, arg1, arg2, arg3, arg4)); \
  61. }
  62. #undef _syscall5
  63. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  64. type5,arg5) \
  65. type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
  66. { \
  67. return (type) (INLINE_SYSCALL(name, 5, arg1, arg2, arg3, arg4, arg5)); \
  68. }
  69. #undef _syscall6
  70. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  71. type5,arg5,type6,arg6) \
  72. type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, type6 arg6) \
  73. { \
  74. return (type) (INLINE_SYSCALL(name, 6, arg1, arg2, arg3, arg4, arg5, arg6)); \
  75. }
  76. #undef _syscall7
  77. #define _syscall7(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  78. type5,arg5,type6,arg6,type7,arg7) \
  79. type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, type6 arg6,type7 arg7) \
  80. { \
  81. return (type) (INLINE_SYSCALL(name, 7, arg1, arg2, arg3, arg4, arg5, arg6, arg7)); \
  82. }
  83. #undef INLINE_SYSCALL
  84. #define INLINE_SYSCALL(name, nr, args...) \
  85. ({ unsigned int __sys_result = INTERNAL_SYSCALL (name, , nr, args); \
  86. if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (__sys_result, ), 0)) \
  87. { \
  88. __set_errno (INTERNAL_SYSCALL_ERRNO (__sys_result, )); \
  89. __sys_result = (unsigned int) -1; \
  90. } \
  91. (int) __sys_result; })
  92. #undef INTERNAL_SYSCALL_DECL
  93. #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
  94. #undef INTERNAL_SYSCALL
  95. #if defined(__ARM_EABI__)
  96. #if !defined(__thumb__)
  97. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  98. ({unsigned int _sys_result; \
  99. { \
  100. register int _a1 __asm__ ("r0"), _nr __asm__ ("r7"); \
  101. LOAD_ARGS_##nr (args) \
  102. _nr = SYS_ify(name); \
  103. __asm__ volatile ("swi 0x0 @ syscall " #name \
  104. : "=r" (_a1) \
  105. : "r" (_nr) ASM_ARGS_##nr \
  106. : "memory"); \
  107. _sys_result = _a1; \
  108. } \
  109. (int) _sys_result; })
  110. #else /* !defined(__thumb__) */
  111. /* So hide the use of r7 from the compiler, this would be a lot
  112. * easier but for the fact that the syscalls can exceed 255.
  113. * For the moment the LOAD_ARG_7 is sacrificed.
  114. */
  115. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  116. ({ unsigned int _sys_result; \
  117. { \
  118. register int _a1 asm ("a1"); \
  119. LOAD_ARGS_##nr (args) \
  120. register int _v3 asm ("v3") = (int) (SYS_ify(name)); \
  121. asm volatile ("push {r7}\n" \
  122. "\tmov r7, v3\n" \
  123. "\tswi 0 @ syscall " #name "\n" \
  124. "\tpop {r7}" \
  125. : "=r" (_a1) \
  126. : "r" (_v3) ASM_ARGS_##nr \
  127. : "memory"); \
  128. _sys_result = _a1; \
  129. } \
  130. (int) _sys_result; })
  131. #endif /*!defined(__thumb__)*/
  132. #else /* !defined(__ARM_EABI__) */
  133. #if !defined(__thumb__)
  134. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  135. ({ unsigned int _sys_result; \
  136. { \
  137. register int _a1 __asm__ ("a1"); \
  138. LOAD_ARGS_##nr (args) \
  139. __asm__ volatile ("swi %1 @ syscall " #name \
  140. : "=r" (_a1) \
  141. : "i" (SYS_ify(name)) ASM_ARGS_##nr \
  142. : "memory"); \
  143. _sys_result = _a1; \
  144. } \
  145. (int) _sys_result; })
  146. #else
  147. #if 0
  148. /* This doesn't work because GCC uses r7 as a frame pointer in
  149. * some cases and doesn't notice that the _r7 value changes
  150. * it, resulting in mysterious crashes after the SWI.
  151. */
  152. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  153. ({ unsigned int _sys_result; \
  154. { \
  155. register int _a1 __asm__ ("a1"); \
  156. LOAD_ARGS_##nr (args) \
  157. register int _r7 __asm__ ("r7") = (int) (SYS_ify(name)); \
  158. __asm__ volatile ("swi 0 @ syscall " #name \
  159. : "=r" (_a1) \
  160. : "r" (_r7) ASM_ARGS_##nr \
  161. : "memory"); \
  162. _sys_result = _a1; \
  163. } \
  164. (int) _sys_result; })
  165. #else
  166. /* So hide the use of r7 from the compiler, this would be a lot
  167. * easier but for the fact that the syscalls can exceed 255.
  168. * For the moment the LOAD_ARG_7 is sacrificed.
  169. */
  170. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  171. ({ unsigned int _sys_result; \
  172. { \
  173. register int _a1 __asm__ ("a1"); \
  174. LOAD_ARGS_##nr (args) \
  175. register int _v3 __asm__ ("v3") = (int) (SYS_ify(name)); \
  176. __asm__ volatile ("push {r7}\n" \
  177. "\tmov r7, v3\n" \
  178. "\tswi 0 @ syscall " #name "\n" \
  179. "\tpop {r7}" \
  180. : "=r" (_a1) \
  181. : "r" (_v3) ASM_ARGS_##nr \
  182. : "memory"); \
  183. _sys_result = _a1; \
  184. } \
  185. (int) _sys_result; })
  186. #endif
  187. #endif
  188. #endif /* !defined(__ARM_EABI__) */
  189. #undef INTERNAL_SYSCALL_ERROR_P
  190. #define INTERNAL_SYSCALL_ERROR_P(val, err) \
  191. ((unsigned int) (val) >= 0xfffff001u)
  192. #undef INTERNAL_SYSCALL_ERRNO
  193. #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
  194. #define LOAD_ARGS_0()
  195. #define ASM_ARGS_0
  196. #define LOAD_ARGS_1(a1) \
  197. _a1 = (int) (a1); \
  198. LOAD_ARGS_0 ()
  199. #define ASM_ARGS_1 ASM_ARGS_0, "r" (_a1)
  200. #define LOAD_ARGS_2(a1, a2) \
  201. register int _a2 __asm__ ("a2") = (int) (a2); \
  202. LOAD_ARGS_1 (a1)
  203. #define ASM_ARGS_2 ASM_ARGS_1, "r" (_a2)
  204. #define LOAD_ARGS_3(a1, a2, a3) \
  205. register int _a3 __asm__ ("a3") = (int) (a3); \
  206. LOAD_ARGS_2 (a1, a2)
  207. #define ASM_ARGS_3 ASM_ARGS_2, "r" (_a3)
  208. #define LOAD_ARGS_4(a1, a2, a3, a4) \
  209. register int _a4 __asm__ ("a4") = (int) (a4); \
  210. LOAD_ARGS_3 (a1, a2, a3)
  211. #define ASM_ARGS_4 ASM_ARGS_3, "r" (_a4)
  212. #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
  213. register int _v1 __asm__ ("v1") = (int) (a5); \
  214. LOAD_ARGS_4 (a1, a2, a3, a4)
  215. #define ASM_ARGS_5 ASM_ARGS_4, "r" (_v1)
  216. #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
  217. register int _v2 __asm__ ("v2") = (int) (a6); \
  218. LOAD_ARGS_5 (a1, a2, a3, a4, a5)
  219. #define ASM_ARGS_6 ASM_ARGS_5, "r" (_v2)
  220. #define LOAD_ARGS_7(a1, a2, a3, a4, a5, a6, a7) \
  221. register int _v3 __asm__ ("v3") = (int) (a7); \
  222. LOAD_ARGS_6 (a1, a2, a3, a4, a5, a6)
  223. #define ASM_ARGS_7 ASM_ARGS_6, "r" (_v3)
  224. #endif /* __ASSEMBLER__ */
  225. #endif /* _BITS_SYSCALLS_H */