syscalls.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #ifndef __ASSEMBLER__
  7. #include <errno.h>
  8. #define SYS_ify(syscall_name) (__NR_##syscall_name)
  9. #undef __SYSCALL_STRING
  10. # define __SYSCALL_STRING \
  11. "swe\n\t" \
  12. "nop\n\t"
  13. # define __SYSCALL_RES_CHECK (__res < -255 || __res >= 0)
  14. #define __SYSCALL_CLOBBERS "cc", "memory"
  15. #define __SYSCALL_RETURN(type) \
  16. if (__SYSCALL_RES_CHECK) \
  17. return (type) __res; \
  18. __set_errno (-__res); \
  19. return (type) -1;
  20. #ifndef NOT_IN_libc
  21. #define DEBUG_SYSCALL(name) { \
  22. char d[64];\
  23. write( 2, d, snprintf( d, 64, "syscall %d error %d\n", __NR_##name, _inline_sys_result)); \
  24. }
  25. #else
  26. #define DEBUG_SYSCALL(name) do{} while(0)
  27. #endif
  28. #undef INLINE_SYSCALL
  29. #define INLINE_SYSCALL(name, nr, args...) \
  30. ({ unsigned int _inline_sys_result = INTERNAL_SYSCALL (name, , nr, args); \
  31. if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (_inline_sys_result, ), 0)) \
  32. { \
  33. __set_errno (INTERNAL_SYSCALL_ERRNO (_inline_sys_result, )); \
  34. _inline_sys_result = (unsigned int) -1; \
  35. } \
  36. (int) _inline_sys_result; })
  37. #undef INLINE_SYSCALL_NOERR
  38. #define INLINE_SYSCALL_NOERR(name, nr, args...) \
  39. ({ unsigned int _inline_sys_result = INTERNAL_SYSCALL (name, , nr, args); \
  40. (int) _inline_sys_result; })
  41. #undef INTERNAL_SYSCALL_DECL
  42. #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
  43. #define INTERNAL_SYSCALL( name, err, nr, args...) \
  44. INTERNAL_SYSCALL_NCS( __NR_##name, err, nr, args )
  45. #define INTERNAL_SYSCALL_NCS(sys_num, err, nr, args...) \
  46. ({ \
  47. register long __A4 __asm__("A4"); \
  48. register long __b0 __asm__("B0") = sys_num; \
  49. LOAD_ARGS_##nr(args) \
  50. __asm__ __volatile__(__SYSCALL_STRING \
  51. : "=a" (__A4) \
  52. : "b" (__b0) ASM_ARGS_##nr \
  53. : __SYSCALL_CLOBBERS ); \
  54. (int)__A4; \
  55. })
  56. #undef INTERNAL_SYSCALL_ERROR_P
  57. #define INTERNAL_SYSCALL_ERROR_P(val, err) \
  58. ((unsigned int) (val) >= 0xfffff001u)
  59. #undef INTERNAL_SYSCALL_ERRNO
  60. #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
  61. #if 0
  62. # define CALL_ERRNO_LOCATION "call __errno_location;"
  63. #define __CLONE_SYSCALL_STRING \
  64. "ta 0x10;" \
  65. "bcs 2f;" \
  66. " sub %%o1, 1, %%o1;" \
  67. "and %%A4, %%o1, %%A4;" \
  68. "1:" \
  69. ".subsection 2;" \
  70. "2:" \
  71. "save %%sp, -192, %%sp;" \
  72. CALL_ERRNO_LOCATION \
  73. " nop;" \
  74. "st %%i0, [%%A4];" \
  75. "ba 1b;" \
  76. " restore %%g0, -1, %%A4;" \
  77. ".previous;"
  78. #define INLINE_CLONE_SYSCALL(arg1,arg2,arg3,arg4,arg5) \
  79. ({ \
  80. register long __A4 __asm__ ("A4") = (long)(arg1); \
  81. register long __B4 __asm__ ("B4") = (long)(arg2); \
  82. register long __A6 __asm__ ("A6") = (long)(arg3); \
  83. register long __B6 __asm__ ("B6") = (long)(arg4); \
  84. register long __A8 __asm__ ("A8") = (long)(arg5); \
  85. register long __g1 __asm__ ("g1") = __NR_clone; \
  86. __asm __volatile (__CLONE_SYSCALL_STRING : \
  87. "=r" (__g1), "=r" (__A4), "=r" (__B4) : \
  88. "0" (__g1), "1" (__A4), "2" (__B4), \
  89. "r" (__A6), "r" (__B6), "r" (__A8) : \
  90. __SYSCALL_CLOBBERS); \
  91. __A4; \
  92. })
  93. #endif
  94. #define LOAD_ARGS_0()
  95. #define ASM_ARGS_0
  96. #define LOAD_ARGS_1(A4) \
  97. __A4 = (int)A4; \
  98. LOAD_ARGS_0()
  99. #define ASM_ARGS_1 ASM_ARGS_0, "a" (__A4)
  100. #define LOAD_ARGS_2(A4, B4) \
  101. register int __B4 __asm__ ("B4") = (int) (B4); \
  102. LOAD_ARGS_1 (A4)
  103. #define ASM_ARGS_2 ASM_ARGS_1, "b" (__B4)
  104. #define LOAD_ARGS_3(A4, B4, A6) \
  105. register int __A6 __asm__ ("A6") = (int) (A6); \
  106. LOAD_ARGS_2 (A4, B4)
  107. #define ASM_ARGS_3 ASM_ARGS_2, "a" (__A6)
  108. #define LOAD_ARGS_4(A4, B4, A6, B6) \
  109. register int __B6 __asm__ ("B6") = (int) (B6); \
  110. LOAD_ARGS_3 (A4, B4, A6)
  111. #define ASM_ARGS_4 ASM_ARGS_3, "b" (__B6)
  112. #define LOAD_ARGS_5(A4, B4, A6, B6, A8) \
  113. register int __A8 __asm__ ("A8") = (int) (A8); \
  114. LOAD_ARGS_4 (A4, B4, A6, B6)
  115. #define ASM_ARGS_5 ASM_ARGS_4, "a" (__A8)
  116. #define LOAD_ARGS_6(A4, B4, A6, B6, A8, B8) \
  117. register int __B8 __asm__ ("B8") = (int) (B8); \
  118. LOAD_ARGS_5 (A4, B4, A6, B6, A8)
  119. #define ASM_ARGS_6 ASM_ARGS_5, "b" (__B8)
  120. #ifndef _syscall0
  121. #define C_DECL_ARGS_0() void
  122. #define C_DECL_ARGS_1(t, v) t v
  123. #define C_DECL_ARGS_2(t, v, args...) t v, C_DECL_ARGS_1(args)
  124. #define C_DECL_ARGS_3(t, v, args...) t v, C_DECL_ARGS_2(args)
  125. #define C_DECL_ARGS_4(t, v, args...) t v, C_DECL_ARGS_3(args)
  126. #define C_DECL_ARGS_5(t, v, args...) t v, C_DECL_ARGS_4(args)
  127. #define C_DECL_ARGS_6(t, v, args...) t v, C_DECL_ARGS_5(args)
  128. #define C_ARGS_0()
  129. #define C_ARGS_1(t, v) v
  130. #define C_ARGS_2(t, v, args...) v, C_ARGS_1(args)
  131. #define C_ARGS_3(t, v, args...) v, C_ARGS_2(args)
  132. #define C_ARGS_4(t, v, args...) v, C_ARGS_3(args)
  133. #define C_ARGS_5(t, v, args...) v, C_ARGS_4(args)
  134. #define C_ARGS_6(t, v, args...) v, C_ARGS_5(args)
  135. #define SYSCALL_FUNC(nargs, type, name, args...) \
  136. type name(C_DECL_ARGS_##nargs(args)) { \
  137. return (type)INLINE_SYSCALL(name, nargs, C_ARGS_##nargs(args)); \
  138. }
  139. #define SYSCALL_NOERR_FUNC(nargs, type, name, args...) \
  140. type name(C_DECL_ARGS_##nargs(args)) { \
  141. return (type)INLINE_SYSCALL_NOERR(name, nargs, C_ARGS_##nargs(args)); \
  142. }
  143. #define _syscall0(args...) SYSCALL_FUNC(0, args)
  144. #define _syscall_noerr0(args...) SYSCALL_NOERR_FUNC(0, args)
  145. #define _syscall1(args...) SYSCALL_FUNC(1, args)
  146. #define _syscall_noerr1(args...) SYSCALL_NOERR_FUNC(1, args)
  147. #define _syscall2(args...) SYSCALL_FUNC(2, args)
  148. #define _syscall3(args...) SYSCALL_FUNC(3, args)
  149. #define _syscall4(args...) SYSCALL_FUNC(4, args)
  150. #define _syscall5(args...) SYSCALL_FUNC(5, args)
  151. #define _syscall6(args...) SYSCALL_FUNC(6, args)
  152. #endif /* _syscall0 */
  153. #endif /* __ASSEMBLER__ */
  154. #endif /* _BITS_SYSCALLS_H */