syscalls.h 7.9 KB

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