syscalls.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #ifndef __ASSEMBLER__
  21. #undef _syscall0
  22. #define _syscall0(type,name) \
  23. type name(void) \
  24. { \
  25. return (type) (INLINE_SYSCALL(name, 0)); \
  26. }
  27. #undef _syscall1
  28. #define _syscall1(type,name,type1,arg1) \
  29. type name(type1 arg1) \
  30. { \
  31. return (type) (INLINE_SYSCALL(name, 1, arg1)); \
  32. }
  33. #undef _syscall2
  34. #define _syscall2(type,name,type1,arg1,type2,arg2) \
  35. type name(type1 arg1,type2 arg2) \
  36. { \
  37. return (type) (INLINE_SYSCALL(name, 2, arg1, arg2)); \
  38. }
  39. #undef _syscall3
  40. #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
  41. type name(type1 arg1,type2 arg2,type3 arg3) \
  42. { \
  43. return (type) (INLINE_SYSCALL(name, 3, arg1, arg2, arg3)); \
  44. }
  45. #undef _syscall4
  46. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
  47. type name (type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
  48. { \
  49. return (type) (INLINE_SYSCALL(name, 4, arg1, arg2, arg3, arg4)); \
  50. }
  51. #undef _syscall5
  52. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  53. type5,arg5) \
  54. type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
  55. { \
  56. return (type) (INLINE_SYSCALL(name, 5, arg1, arg2, arg3, arg4, arg5)); \
  57. }
  58. #undef _syscall6
  59. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  60. type5,arg5,type6,arg6) \
  61. type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, type6 arg6) \
  62. { \
  63. return (type) (INLINE_SYSCALL(name, 6, arg1, arg2, arg3, arg4, arg5, arg6)); \
  64. }
  65. #undef _syscall7
  66. #define _syscall7(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  67. type5,arg5,type6,arg6,type7,arg7) \
  68. type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, type6 arg6,type7 arg7) \
  69. { \
  70. return (type) (INLINE_SYSCALL(name, 7, arg1, arg2, arg3, arg4, arg5, arg6, arg7)); \
  71. }
  72. #undef INLINE_SYSCALL
  73. #define INLINE_SYSCALL(name, nr, args...) \
  74. ({ unsigned int _sys_result = INTERNAL_SYSCALL (name, , nr, args); \
  75. if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (_sys_result, ), 0)) \
  76. { \
  77. __set_errno (INTERNAL_SYSCALL_ERRNO (_sys_result, )); \
  78. _sys_result = (unsigned int) -1; \
  79. } \
  80. (int) _sys_result; })
  81. #undef INTERNAL_SYSCALL_DECL
  82. #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
  83. #undef INTERNAL_SYSCALL
  84. #if !defined(__thumb__)
  85. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  86. ({ unsigned int _sys_result; \
  87. { \
  88. register int _a1 asm ("a1"); \
  89. LOAD_ARGS_##nr (args) \
  90. asm volatile ("swi %1 @ syscall " #name \
  91. : "=r" (_a1) \
  92. : "i" (SYS_ify(name)) ASM_ARGS_##nr \
  93. : "memory"); \
  94. _sys_result = _a1; \
  95. } \
  96. (int) _sys_result; })
  97. #else
  98. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  99. ({ unsigned int _sys_result; \
  100. { \
  101. register int _a1 asm ("a1"); \
  102. LOAD_ARGS_##nr (args) \
  103. register int _r7 asm ("r7") = (int) (SYS_ify(name)); \
  104. asm volatile ("swi 0 @ syscall " #name \
  105. : "=r" (_a1) \
  106. : "r" (_r7) ASM_ARGS_##nr \
  107. : "memory"); \
  108. _sys_result = _a1; \
  109. } \
  110. (int) _sys_result; })
  111. #endif
  112. #undef INTERNAL_SYSCALL_ERROR_P
  113. #define INTERNAL_SYSCALL_ERROR_P(val, err) \
  114. ((unsigned int) (val) >= 0xfffff001u)
  115. #undef INTERNAL_SYSCALL_ERRNO
  116. #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
  117. #define LOAD_ARGS_0()
  118. #define ASM_ARGS_0
  119. #define LOAD_ARGS_1(a1) \
  120. _a1 = (int) (a1); \
  121. LOAD_ARGS_0 ()
  122. #define ASM_ARGS_1 ASM_ARGS_0, "r" (_a1)
  123. #define LOAD_ARGS_2(a1, a2) \
  124. register int _a2 asm ("a2") = (int) (a2); \
  125. LOAD_ARGS_1 (a1)
  126. #define ASM_ARGS_2 ASM_ARGS_1, "r" (_a2)
  127. #define LOAD_ARGS_3(a1, a2, a3) \
  128. register int _a3 asm ("a3") = (int) (a3); \
  129. LOAD_ARGS_2 (a1, a2)
  130. #define ASM_ARGS_3 ASM_ARGS_2, "r" (_a3)
  131. #define LOAD_ARGS_4(a1, a2, a3, a4) \
  132. register int _a4 asm ("a4") = (int) (a4); \
  133. LOAD_ARGS_3 (a1, a2, a3)
  134. #define ASM_ARGS_4 ASM_ARGS_3, "r" (_a4)
  135. #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
  136. register int _v1 asm ("v1") = (int) (a5); \
  137. LOAD_ARGS_4 (a1, a2, a3, a4)
  138. #define ASM_ARGS_5 ASM_ARGS_4, "r" (_v1)
  139. #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
  140. register int _v2 asm ("v2") = (int) (a6); \
  141. LOAD_ARGS_5 (a1, a2, a3, a4, a5)
  142. #define ASM_ARGS_6 ASM_ARGS_5, "r" (_v2)
  143. #define LOAD_ARGS_7(a1, a2, a3, a4, a5, a6, a7) \
  144. register int _v3 asm ("v3") = (int) (a7); \
  145. LOAD_ARGS_6 (a1, a2, a3, a4, a5, a6)
  146. #define ASM_ARGS_7 ASM_ARGS_6, "r" (_v3)
  147. #endif /* __ASSEMBLER__ */
  148. #endif /* _BITS_SYSCALLS_H */