syscalls.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. #ifdef __ASSEMBLER__
  15. #include <errno.h>
  16. #define SYS_ify(syscall_name) (__NR_##syscall_name)
  17. /* Call a given syscall, with arguments loaded. For EABI, we must
  18. save and restore r7 for the syscall number. Unlike the DO_CALL
  19. macro in glibc, this macro does not load syscall arguments. */
  20. #undef DO_CALL
  21. #if defined(__ARM_EABI__)
  22. #define DO_CALL(syscall_name) \
  23. mov ip, r7; \
  24. ldr r7, =SYS_ify (syscall_name); \
  25. swi 0x0; \
  26. mov r7, ip;
  27. #else
  28. #define DO_CALL(syscall_name) \
  29. swi SYS_ify (syscall_name);
  30. #endif
  31. #else
  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. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  97. ({unsigned int _sys_result; \
  98. { \
  99. register int _a1 asm ("r0"), _nr asm ("r7"); \
  100. LOAD_ARGS_##nr (args) \
  101. _nr = SYS_ify(name); \
  102. asm volatile ("swi 0x0 @ syscall " #name \
  103. : "=r" (_a1) \
  104. : "r" (_nr) ASM_ARGS_##nr \
  105. : "memory"); \
  106. _sys_result = _a1; \
  107. } \
  108. (int) _sys_result; })
  109. #else /* !defined(__ARM_EABI__) */
  110. #if !defined(__thumb__)
  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. asm volatile ("swi %1 @ syscall " #name \
  117. : "=r" (_a1) \
  118. : "i" (SYS_ify(name)) ASM_ARGS_##nr \
  119. : "memory"); \
  120. _sys_result = _a1; \
  121. } \
  122. (int) _sys_result; })
  123. #else
  124. #if 0
  125. /* This doesn't work because GCC uses r7 as a frame pointer in
  126. * some cases and doesn't notice that the _r7 value changes
  127. * it, resulting in mysterious crashes after the SWI.
  128. */
  129. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  130. ({ unsigned int _sys_result; \
  131. { \
  132. register int _a1 asm ("a1"); \
  133. LOAD_ARGS_##nr (args) \
  134. register int _r7 asm ("r7") = (int) (SYS_ify(name)); \
  135. asm volatile ("swi 0 @ syscall " #name \
  136. : "=r" (_a1) \
  137. : "r" (_r7) ASM_ARGS_##nr \
  138. : "memory"); \
  139. _sys_result = _a1; \
  140. } \
  141. (int) _sys_result; })
  142. #else
  143. /* So hide the use of r7 from the compiler, this would be a lot
  144. * easier but for the fact that the syscalls can exceed 255.
  145. * For the moment the LOAD_ARG_7 is sacrificed.
  146. */
  147. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  148. ({ unsigned int _sys_result; \
  149. { \
  150. register int _a1 asm ("a1"); \
  151. LOAD_ARGS_##nr (args) \
  152. register int _v3 asm ("v3") = (int) (SYS_ify(name)); \
  153. asm volatile ("push {r7}\n" \
  154. "\tmov r7, v3\n" \
  155. "\tswi 0 @ syscall " #name "\n" \
  156. "\tpop {r7}" \
  157. : "=r" (_a1) \
  158. : "r" (_v3) ASM_ARGS_##nr \
  159. : "memory"); \
  160. _sys_result = _a1; \
  161. } \
  162. (int) _sys_result; })
  163. #endif
  164. #endif
  165. #endif /* !defined(__ARM_EABI__) */
  166. #undef INTERNAL_SYSCALL_ERROR_P
  167. #define INTERNAL_SYSCALL_ERROR_P(val, err) \
  168. ((unsigned int) (val) >= 0xfffff001u)
  169. #undef INTERNAL_SYSCALL_ERRNO
  170. #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
  171. #define LOAD_ARGS_0()
  172. #define ASM_ARGS_0
  173. #define LOAD_ARGS_1(a1) \
  174. _a1 = (int) (a1); \
  175. LOAD_ARGS_0 ()
  176. #define ASM_ARGS_1 ASM_ARGS_0, "r" (_a1)
  177. #define LOAD_ARGS_2(a1, a2) \
  178. register int _a2 asm ("a2") = (int) (a2); \
  179. LOAD_ARGS_1 (a1)
  180. #define ASM_ARGS_2 ASM_ARGS_1, "r" (_a2)
  181. #define LOAD_ARGS_3(a1, a2, a3) \
  182. register int _a3 asm ("a3") = (int) (a3); \
  183. LOAD_ARGS_2 (a1, a2)
  184. #define ASM_ARGS_3 ASM_ARGS_2, "r" (_a3)
  185. #define LOAD_ARGS_4(a1, a2, a3, a4) \
  186. register int _a4 asm ("a4") = (int) (a4); \
  187. LOAD_ARGS_3 (a1, a2, a3)
  188. #define ASM_ARGS_4 ASM_ARGS_3, "r" (_a4)
  189. #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
  190. register int _v1 asm ("v1") = (int) (a5); \
  191. LOAD_ARGS_4 (a1, a2, a3, a4)
  192. #define ASM_ARGS_5 ASM_ARGS_4, "r" (_v1)
  193. #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
  194. register int _v2 asm ("v2") = (int) (a6); \
  195. LOAD_ARGS_5 (a1, a2, a3, a4, a5)
  196. #define ASM_ARGS_6 ASM_ARGS_5, "r" (_v2)
  197. #define LOAD_ARGS_7(a1, a2, a3, a4, a5, a6, a7) \
  198. register int _v3 asm ("v3") = (int) (a7); \
  199. LOAD_ARGS_6 (a1, a2, a3, a4, a5, a6)
  200. #define ASM_ARGS_7 ASM_ARGS_6, "r" (_v3)
  201. #endif /* __ASSEMBLER__ */
  202. #endif /* _BITS_SYSCALLS_H */