syscalls.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 _inline_sys_result = INTERNAL_SYSCALL (name, , nr, args); \
  82. if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (_inline_sys_result, ), 0)) \
  83. { \
  84. __set_errno (INTERNAL_SYSCALL_ERRNO (_inline_sys_result, )); \
  85. _inline_sys_result = (unsigned int) -1; \
  86. } \
  87. (int) _inline_sys_result; })
  88. #undef INTERNAL_SYSCALL_DECL
  89. #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
  90. #undef INTERNAL_SYSCALL
  91. #if !defined(__thumb__)
  92. #if defined(__ARM_EABI__)
  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(__ARM_EABI__) */
  107. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  108. ({ unsigned int __sys_result; \
  109. { \
  110. register int _a1 __asm__ ("a1"); \
  111. LOAD_ARGS_##nr (args) \
  112. __asm__ __volatile__ ("swi %1 @ syscall " #name \
  113. : "=r" (_a1) \
  114. : "i" (SYS_ify(name)) ASM_ARGS_##nr \
  115. : "memory"); \
  116. __sys_result = _a1; \
  117. } \
  118. (int) __sys_result; })
  119. #endif
  120. #else /* !defined(__thumb__) */
  121. /* We can't use push/pop inside the asm because that breaks
  122. unwinding (ie. thread cancellation).
  123. */
  124. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  125. ({ unsigned int __sys_result; \
  126. { \
  127. int _sys_buf[2]; \
  128. register int _a1 __asm__ ("a1"); \
  129. register int *_v3 __asm__ ("v3") = _sys_buf; \
  130. *_v3 = (int) (SYS_ify(name)); \
  131. LOAD_ARGS_##nr (args) \
  132. __asm__ __volatile__ ("str r7, [v3, #4]\n" \
  133. "\tldr r7, [v3]\n" \
  134. "\tswi 0 @ syscall " #name "\n" \
  135. "\tldr r7, [v3, #4]" \
  136. : "=r" (_a1) \
  137. : "r" (_v3) ASM_ARGS_##nr \
  138. : "memory"); \
  139. __sys_result = _a1; \
  140. } \
  141. (int) __sys_result; })
  142. #endif /*!defined(__thumb__)*/
  143. #undef INTERNAL_SYSCALL_ERROR_P
  144. #define INTERNAL_SYSCALL_ERROR_P(val, err) \
  145. ((unsigned int) (val) >= 0xfffff001u)
  146. #undef INTERNAL_SYSCALL_ERRNO
  147. #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
  148. #define LOAD_ARGS_0()
  149. #define ASM_ARGS_0
  150. #define LOAD_ARGS_1(a1) \
  151. _a1 = (int) (a1); \
  152. LOAD_ARGS_0 ()
  153. #define ASM_ARGS_1 ASM_ARGS_0, "r" (_a1)
  154. #define LOAD_ARGS_2(a1, a2) \
  155. register int _a2 __asm__ ("a2") = (int) (a2); \
  156. LOAD_ARGS_1 (a1)
  157. #define ASM_ARGS_2 ASM_ARGS_1, "r" (_a2)
  158. #define LOAD_ARGS_3(a1, a2, a3) \
  159. register int _a3 __asm__ ("a3") = (int) (a3); \
  160. LOAD_ARGS_2 (a1, a2)
  161. #define ASM_ARGS_3 ASM_ARGS_2, "r" (_a3)
  162. #define LOAD_ARGS_4(a1, a2, a3, a4) \
  163. register int _a4 __asm__ ("a4") = (int) (a4); \
  164. LOAD_ARGS_3 (a1, a2, a3)
  165. #define ASM_ARGS_4 ASM_ARGS_3, "r" (_a4)
  166. #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
  167. register int _v1 __asm__ ("v1") = (int) (a5); \
  168. LOAD_ARGS_4 (a1, a2, a3, a4)
  169. #define ASM_ARGS_5 ASM_ARGS_4, "r" (_v1)
  170. #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
  171. register int _v2 __asm__ ("v2") = (int) (a6); \
  172. LOAD_ARGS_5 (a1, a2, a3, a4, a5)
  173. #define ASM_ARGS_6 ASM_ARGS_5, "r" (_v2)
  174. #define LOAD_ARGS_7(a1, a2, a3, a4, a5, a6, a7) \
  175. register int _v3 __asm__ ("v3") = (int) (a7); \
  176. LOAD_ARGS_6 (a1, a2, a3, a4, a5, a6)
  177. #define ASM_ARGS_7 ASM_ARGS_6, "r" (_v3)
  178. #endif /* __ASSEMBLER__ */
  179. #endif /* _BITS_SYSCALLS_H */