syscalls.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  122. ({ unsigned int __sys_result; \
  123. { \
  124. register int _a1 __asm__ ("a1"); \
  125. LOAD_ARGS_##nr (args) \
  126. register int _v3 __asm__ ("v3") = (int) (SYS_ify(name)); \
  127. __asm__ __volatile__ ("push {r7}\n" \
  128. "\tmov r7, v3\n" \
  129. "\tswi 0 @ syscall " #name "\n" \
  130. "\tpop {r7}" \
  131. : "=r" (_a1) \
  132. : "r" (_v3) ASM_ARGS_##nr \
  133. : "memory"); \
  134. __sys_result = _a1; \
  135. } \
  136. (int) __sys_result; })
  137. #endif /*!defined(__thumb__)*/
  138. #undef INTERNAL_SYSCALL_ERROR_P
  139. #define INTERNAL_SYSCALL_ERROR_P(val, err) \
  140. ((unsigned int) (val) >= 0xfffff001u)
  141. #undef INTERNAL_SYSCALL_ERRNO
  142. #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
  143. #define LOAD_ARGS_0()
  144. #define ASM_ARGS_0
  145. #define LOAD_ARGS_1(a1) \
  146. _a1 = (int) (a1); \
  147. LOAD_ARGS_0 ()
  148. #define ASM_ARGS_1 ASM_ARGS_0, "r" (_a1)
  149. #define LOAD_ARGS_2(a1, a2) \
  150. register int _a2 __asm__ ("a2") = (int) (a2); \
  151. LOAD_ARGS_1 (a1)
  152. #define ASM_ARGS_2 ASM_ARGS_1, "r" (_a2)
  153. #define LOAD_ARGS_3(a1, a2, a3) \
  154. register int _a3 __asm__ ("a3") = (int) (a3); \
  155. LOAD_ARGS_2 (a1, a2)
  156. #define ASM_ARGS_3 ASM_ARGS_2, "r" (_a3)
  157. #define LOAD_ARGS_4(a1, a2, a3, a4) \
  158. register int _a4 __asm__ ("a4") = (int) (a4); \
  159. LOAD_ARGS_3 (a1, a2, a3)
  160. #define ASM_ARGS_4 ASM_ARGS_3, "r" (_a4)
  161. #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
  162. register int _v1 __asm__ ("v1") = (int) (a5); \
  163. LOAD_ARGS_4 (a1, a2, a3, a4)
  164. #define ASM_ARGS_5 ASM_ARGS_4, "r" (_v1)
  165. #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
  166. register int _v2 __asm__ ("v2") = (int) (a6); \
  167. LOAD_ARGS_5 (a1, a2, a3, a4, a5)
  168. #define ASM_ARGS_6 ASM_ARGS_5, "r" (_v2)
  169. #define LOAD_ARGS_7(a1, a2, a3, a4, a5, a6, a7) \
  170. register int _v3 __asm__ ("v3") = (int) (a7); \
  171. LOAD_ARGS_6 (a1, a2, a3, a4, a5, a6)
  172. #define ASM_ARGS_7 ASM_ARGS_6, "r" (_v3)
  173. #endif /* __ASSEMBLER__ */
  174. #endif /* _BITS_SYSCALLS_H */