syscalls.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (C) 2013 Synopsys, Inc. (www.synopsys.com)
  3. *
  4. * Licensed under LGPL v2.1 or later, see the file COPYING.LIB in this tarball.
  5. *
  6. */
  7. #ifndef _BITS_SYSCALLS_H
  8. #define _BITS_SYSCALLS_H
  9. #ifndef _SYSCALL_H
  10. #error "Never use <bits/syscalls.h> directly; include <sys/syscall.h> instead."
  11. #endif
  12. #ifndef __ASSEMBLER__
  13. #include <errno.h>
  14. /*
  15. * Fine tuned code for errno handling in syscall wrappers.
  16. *
  17. * 1. __syscall_error(raw_syscall_ret_val) is used to set the errno (vs.
  18. * the typical __set_errno). This helps elide the generated code for
  19. * GOT fetch for __errno_location pointer etc, in each wrapper.
  20. *
  21. * 2. The call to above is also disguised in inline asm. This elides
  22. * unconditional save/restore of a few callee regs which gcc almost
  23. * always generates if the call is exposed
  24. *
  25. * 3. The function can't be hidden because wrappers from librt et all also
  26. * call it. However hidden is not really needed to bypass PLT for
  27. * intra-libc calls as the branch insn w/o @plt is sufficient.
  28. */
  29. #ifdef IS_IN_rtld
  30. /* ldso doesn't have real errno */
  31. #define ERRNO_ERRANDS(_sys_result)
  32. #else /* !IS_IN_rtld */
  33. extern long __syscall_error (int);
  34. #ifndef IS_IN_libc
  35. /* Inter-libc callers use PLT */
  36. #define CALL_ERRNO_SETTER "bl __syscall_error@plt \n\t"
  37. #else
  38. /* intra-libc callers, despite PIC can bypass PLT */
  39. #define CALL_ERRNO_SETTER "bl __syscall_error \n\t"
  40. #endif
  41. #define ERRNO_ERRANDS(_sys_result) \
  42. __asm__ volatile ( \
  43. "st.a blink, [sp, -4] \n\t" \
  44. CALL_ERRNO_SETTER \
  45. "ld.ab blink, [sp, 4] \n\t" \
  46. :"+r" (_sys_result) \
  47. : \
  48. :"r1","r2","r3","r4","r5","r6", \
  49. "r7","r8","r9","r10","r11","r12" \
  50. );
  51. #endif /* IS_IN_rtld */
  52. /* -1 to -1023 as valid error values will suffice for some time */
  53. #define INTERNAL_SYSCALL_ERROR_P(val, err) \
  54. ((unsigned int) (val) > (unsigned int) -1024)
  55. /*
  56. * Standard sycall wrapper
  57. * -Gets syscall name (conv to __NR_xxx)
  58. * -sets errno, return success/error-codes
  59. */
  60. #define INLINE_SYSCALL(name, nr_args, args...) \
  61. ({ \
  62. register int __res __asm__("r0"); \
  63. __res = INTERNAL_SYSCALL_NCS(__NR_##name, , nr_args, args); \
  64. if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P ((__res), ), 0)) \
  65. { \
  66. ERRNO_ERRANDS(__res); \
  67. } \
  68. __res; \
  69. })
  70. /* variant of INLINE_SYSCALL, gets syscall number
  71. */
  72. #define INLINE_SYSCALL_NCS(num, nr_args, args...) \
  73. ({ \
  74. register int __res __asm__("r0"); \
  75. __res = INTERNAL_SYSCALL_NCS(num, , nr_args, args); \
  76. if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P ((__res), ), 0)) \
  77. { \
  78. ERRNO_ERRANDS(__res); \
  79. } \
  80. __res; \
  81. })
  82. /*-------------------------------------------------------------------------
  83. * Mechanics of Trap - specific to ARC700
  84. *
  85. * Note the memory clobber is not strictly needed for intended semantics of
  86. * the inline asm. However some of the cases, such as old-style 6 arg mmap
  87. * gcc was generating code for inline syscall ahead of buffer packing needed
  88. * for syscall itself.
  89. *-------------------------------------------------------------------------*/
  90. #ifdef __A7__
  91. #define ARC_TRAP_INSN "trap0 \n\t"
  92. #else
  93. #define ARC_TRAP_INSN "trap_s 0 \n\t"
  94. #endif
  95. #define INTERNAL_SYSCALL_NCS(nm, err, nr_args, args...) \
  96. ({ \
  97. /* Per ABI, r0 is 1st arg and return reg */ \
  98. register int __ret __asm__("r0"); \
  99. register int _sys_num __asm__("r8"); \
  100. \
  101. LOAD_ARGS_##nr_args (nm, args) \
  102. \
  103. __asm__ volatile ( \
  104. ARC_TRAP_INSN \
  105. : "+r" (__ret) \
  106. : "r"(_sys_num) ASM_ARGS_##nr_args \
  107. : "memory"); \
  108. \
  109. __ret; \
  110. })
  111. /* Macros for setting up inline __asm__ input regs */
  112. #define ASM_ARGS_0
  113. #define ASM_ARGS_1 ASM_ARGS_0, "r" (__ret)
  114. #define ASM_ARGS_2 ASM_ARGS_1, "r" (_arg2)
  115. #define ASM_ARGS_3 ASM_ARGS_2, "r" (_arg3)
  116. #define ASM_ARGS_4 ASM_ARGS_3, "r" (_arg4)
  117. #define ASM_ARGS_5 ASM_ARGS_4, "r" (_arg5)
  118. #define ASM_ARGS_6 ASM_ARGS_5, "r" (_arg6)
  119. #define ASM_ARGS_7 ASM_ARGS_6, "r" (_arg7)
  120. /* Macros for converting sys-call wrapper args into sys call args */
  121. #define LOAD_ARGS_0(nm, arg) \
  122. _sys_num = (int) (nm); \
  123. #define LOAD_ARGS_1(nm, arg1) \
  124. __ret = (int) (arg1); \
  125. LOAD_ARGS_0 (nm, arg1)
  126. /*
  127. * Note that the use of _tmpX might look superflous, however it is needed
  128. * to ensure that register variables are not clobbered if arg happens to be
  129. * a function call itself. e.g. sched_setaffinity() calling getpid() for arg2
  130. *
  131. * Also this specific order of recursive calling is important to segregate
  132. * the tmp args evaluation (function call case described above) and assigment
  133. * of register variables
  134. */
  135. #define LOAD_ARGS_2(nm, arg1, arg2) \
  136. int _tmp2 = (int) (arg2); \
  137. LOAD_ARGS_1 (nm, arg1) \
  138. register int _arg2 __asm__ ("r1") = _tmp2;
  139. #define LOAD_ARGS_3(nm, arg1, arg2, arg3) \
  140. int _tmp3 = (int) (arg3); \
  141. LOAD_ARGS_2 (nm, arg1, arg2) \
  142. register int _arg3 __asm__ ("r2") = _tmp3;
  143. #define LOAD_ARGS_4(nm, arg1, arg2, arg3, arg4) \
  144. int _tmp4 = (int) (arg4); \
  145. LOAD_ARGS_3 (nm, arg1, arg2, arg3) \
  146. register int _arg4 __asm__ ("r3") = _tmp4;
  147. #define LOAD_ARGS_5(nm, arg1, arg2, arg3, arg4, arg5) \
  148. int _tmp5 = (int) (arg5); \
  149. LOAD_ARGS_4 (nm, arg1, arg2, arg3, arg4) \
  150. register int _arg5 __asm__ ("r4") = _tmp5;
  151. #define LOAD_ARGS_6(nm, arg1, arg2, arg3, arg4, arg5, arg6) \
  152. int _tmp6 = (int) (arg6); \
  153. LOAD_ARGS_5 (nm, arg1, arg2, arg3, arg4, arg5) \
  154. register int _arg6 __asm__ ("r5") = _tmp6;
  155. #define LOAD_ARGS_7(nm, arg1, arg2, arg3, arg4, arg5, arg6, arg7)\
  156. int _tmp7 = (int) (arg7); \
  157. LOAD_ARGS_6 (nm, arg1, arg2, arg3, arg4, arg5, arg6) \
  158. register int _arg7 __asm__ ("r6") = _tmp7;
  159. #else
  160. #ifdef __A7__
  161. #define ARC_TRAP_INSN trap0
  162. #else
  163. #define ARC_TRAP_INSN trap_s 0
  164. #endif
  165. #endif /* __ASSEMBLER__ */
  166. #endif /* _BITS_SYSCALLS_H */