syscalls.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. (__extension__ \
  47. ({ \
  48. register long __A4 __asm__("A4"); \
  49. register long __b0 __asm__("B0") = sys_num; \
  50. LOAD_ARGS_##nr(args) \
  51. __asm__ __volatile__(__SYSCALL_STRING \
  52. : "=a" (__A4) \
  53. : "b" (__b0) ASM_ARGS_##nr \
  54. : __SYSCALL_CLOBBERS ); \
  55. (int)__A4; \
  56. }) \
  57. )
  58. #undef INTERNAL_SYSCALL_ERROR_P
  59. #define INTERNAL_SYSCALL_ERROR_P(val, err) \
  60. ((unsigned int) (val) >= 0xfffff001u)
  61. #undef INTERNAL_SYSCALL_ERRNO
  62. #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
  63. #if 0
  64. # define CALL_ERRNO_LOCATION "call __errno_location;"
  65. #define __CLONE_SYSCALL_STRING \
  66. "ta 0x10;" \
  67. "bcs 2f;" \
  68. " sub %%o1, 1, %%o1;" \
  69. "and %%A4, %%o1, %%A4;" \
  70. "1:" \
  71. ".subsection 2;" \
  72. "2:" \
  73. "save %%sp, -192, %%sp;" \
  74. CALL_ERRNO_LOCATION \
  75. " nop;" \
  76. "st %%i0, [%%A4];" \
  77. "ba 1b;" \
  78. " restore %%g0, -1, %%A4;" \
  79. ".previous;"
  80. #define INLINE_CLONE_SYSCALL(arg1,arg2,arg3,arg4,arg5) \
  81. ({ \
  82. register long __A4 __asm__ ("A4") = (long)(arg1); \
  83. register long __B4 __asm__ ("B4") = (long)(arg2); \
  84. register long __A6 __asm__ ("A6") = (long)(arg3); \
  85. register long __B6 __asm__ ("B6") = (long)(arg4); \
  86. register long __A8 __asm__ ("A8") = (long)(arg5); \
  87. register long __g1 __asm__ ("g1") = __NR_clone; \
  88. __asm__ __volatile__ (__CLONE_SYSCALL_STRING : \
  89. "=r" (__g1), "=r" (__A4), "=r" (__B4) : \
  90. "0" (__g1), "1" (__A4), "2" (__B4), \
  91. "r" (__A6), "r" (__B6), "r" (__A8) : \
  92. __SYSCALL_CLOBBERS); \
  93. __A4; \
  94. })
  95. #endif
  96. #define LOAD_ARGS_0()
  97. #define ASM_ARGS_0
  98. #define LOAD_ARGS_1(A4) \
  99. __A4 = (int)A4; \
  100. LOAD_ARGS_0()
  101. #define ASM_ARGS_1 ASM_ARGS_0, "a" (__A4)
  102. #define LOAD_ARGS_2(A4, B4) \
  103. register int __B4 __asm__ ("B4") = (int) (B4); \
  104. LOAD_ARGS_1 (A4)
  105. #define ASM_ARGS_2 ASM_ARGS_1, "b" (__B4)
  106. #define LOAD_ARGS_3(A4, B4, A6) \
  107. register int __A6 __asm__ ("A6") = (int) (A6); \
  108. LOAD_ARGS_2 (A4, B4)
  109. #define ASM_ARGS_3 ASM_ARGS_2, "a" (__A6)
  110. #define LOAD_ARGS_4(A4, B4, A6, B6) \
  111. register int __B6 __asm__ ("B6") = (int) (B6); \
  112. LOAD_ARGS_3 (A4, B4, A6)
  113. #define ASM_ARGS_4 ASM_ARGS_3, "b" (__B6)
  114. #define LOAD_ARGS_5(A4, B4, A6, B6, A8) \
  115. register int __A8 __asm__ ("A8") = (int) (A8); \
  116. LOAD_ARGS_4 (A4, B4, A6, B6)
  117. #define ASM_ARGS_5 ASM_ARGS_4, "a" (__A8)
  118. #define LOAD_ARGS_6(A4, B4, A6, B6, A8, B8) \
  119. register int __B8 __asm__ ("B8") = (int) (B8); \
  120. LOAD_ARGS_5 (A4, B4, A6, B6, A8)
  121. #define ASM_ARGS_6 ASM_ARGS_5, "b" (__B8)
  122. #ifndef _syscall0
  123. #define C_DECL_ARGS_0() void
  124. #define C_DECL_ARGS_1(t, v) t v
  125. #define C_DECL_ARGS_2(t, v, args...) t v, C_DECL_ARGS_1(args)
  126. #define C_DECL_ARGS_3(t, v, args...) t v, C_DECL_ARGS_2(args)
  127. #define C_DECL_ARGS_4(t, v, args...) t v, C_DECL_ARGS_3(args)
  128. #define C_DECL_ARGS_5(t, v, args...) t v, C_DECL_ARGS_4(args)
  129. #define C_DECL_ARGS_6(t, v, args...) t v, C_DECL_ARGS_5(args)
  130. #define C_ARGS_0()
  131. #define C_ARGS_1(t, v) v
  132. #define C_ARGS_2(t, v, args...) v, C_ARGS_1(args)
  133. #define C_ARGS_3(t, v, args...) v, C_ARGS_2(args)
  134. #define C_ARGS_4(t, v, args...) v, C_ARGS_3(args)
  135. #define C_ARGS_5(t, v, args...) v, C_ARGS_4(args)
  136. #define C_ARGS_6(t, v, args...) v, C_ARGS_5(args)
  137. #define SYSCALL_FUNC(nargs, type, name, args...) \
  138. type name(C_DECL_ARGS_##nargs(args)) { \
  139. return (type)INLINE_SYSCALL(name, nargs, C_ARGS_##nargs(args)); \
  140. }
  141. #define SYSCALL_NOERR_FUNC(nargs, type, name, args...) \
  142. type name(C_DECL_ARGS_##nargs(args)) { \
  143. return (type)INLINE_SYSCALL_NOERR(name, nargs, C_ARGS_##nargs(args)); \
  144. }
  145. #define _syscall0(args...) SYSCALL_FUNC(0, args)
  146. #define _syscall_noerr0(args...) SYSCALL_NOERR_FUNC(0, args)
  147. #define _syscall1(args...) SYSCALL_FUNC(1, args)
  148. #define _syscall_noerr1(args...) SYSCALL_NOERR_FUNC(1, args)
  149. #define _syscall2(args...) SYSCALL_FUNC(2, args)
  150. #define _syscall3(args...) SYSCALL_FUNC(3, args)
  151. #define _syscall4(args...) SYSCALL_FUNC(4, args)
  152. #define _syscall5(args...) SYSCALL_FUNC(5, args)
  153. #define _syscall6(args...) SYSCALL_FUNC(6, args)
  154. #endif /* _syscall0 */
  155. #endif /* __ASSEMBLER__ */
  156. #endif /* _BITS_SYSCALLS_H */