syscalls.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  85. ({ unsigned int _sys_result; \
  86. { \
  87. register int _a1 asm ("a1"); \
  88. LOAD_ARGS_##nr (args) \
  89. asm volatile ("swi %1 @ syscall " #name \
  90. : "=r" (_a1) \
  91. : "i" (SYS_ify(name)) ASM_ARGS_##nr \
  92. : "memory"); \
  93. _sys_result = _a1; \
  94. } \
  95. (int) _sys_result; })
  96. #undef INTERNAL_SYSCALL_ERROR_P
  97. #define INTERNAL_SYSCALL_ERROR_P(val, err) \
  98. ((unsigned int) (val) >= 0xfffff001u)
  99. #undef INTERNAL_SYSCALL_ERRNO
  100. #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
  101. #define LOAD_ARGS_0()
  102. #define ASM_ARGS_0
  103. #define LOAD_ARGS_1(a1) \
  104. _a1 = (int) (a1); \
  105. LOAD_ARGS_0 ()
  106. #define ASM_ARGS_1 ASM_ARGS_0, "r" (_a1)
  107. #define LOAD_ARGS_2(a1, a2) \
  108. register int _a2 asm ("a2") = (int) (a2); \
  109. LOAD_ARGS_1 (a1)
  110. #define ASM_ARGS_2 ASM_ARGS_1, "r" (_a2)
  111. #define LOAD_ARGS_3(a1, a2, a3) \
  112. register int _a3 asm ("a3") = (int) (a3); \
  113. LOAD_ARGS_2 (a1, a2)
  114. #define ASM_ARGS_3 ASM_ARGS_2, "r" (_a3)
  115. #define LOAD_ARGS_4(a1, a2, a3, a4) \
  116. register int _a4 asm ("a4") = (int) (a4); \
  117. LOAD_ARGS_3 (a1, a2, a3)
  118. #define ASM_ARGS_4 ASM_ARGS_3, "r" (_a4)
  119. #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
  120. register int _v1 asm ("v1") = (int) (a5); \
  121. LOAD_ARGS_4 (a1, a2, a3, a4)
  122. #define ASM_ARGS_5 ASM_ARGS_4, "r" (_v1)
  123. #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
  124. register int _v2 asm ("v2") = (int) (a6); \
  125. LOAD_ARGS_5 (a1, a2, a3, a4, a5)
  126. #define ASM_ARGS_6 ASM_ARGS_5, "r" (_v2)
  127. #define LOAD_ARGS_7(a1, a2, a3, a4, a5, a6, a7) \
  128. register int _v3 asm ("v3") = (int) (a7); \
  129. LOAD_ARGS_6 (a1, a2, a3, a4, a5, a6)
  130. #define ASM_ARGS_7 ASM_ARGS_6, "r" (_v3)
  131. #endif /* __ASSEMBLER__ */
  132. #endif /* _BITS_SYSCALLS_H */