syscalls.h 6.9 KB

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