syscalls.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 int __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. /* Invoke the syscall and return unprocessed kernel status */
  53. #define INTERNAL_SYSCALL(nm, err, nr, args...) \
  54. INTERNAL_SYSCALL_NCS(SYS_ify (nm), err, nr, args)
  55. /* -1 to -1023 as valid error values will suffice for some time */
  56. #define INTERNAL_SYSCALL_ERROR_P(val, err) \
  57. ((unsigned int) (val) > (unsigned int) -1024)
  58. /*
  59. * Standard sycall wrapper:
  60. * -"const" syscall number @nm, sets errno, return success/error-codes
  61. */
  62. #define INLINE_SYSCALL(nm, nr_args, args...) \
  63. ({ \
  64. register int __res __asm__("r0"); \
  65. __res = INTERNAL_SYSCALL(nm, , nr_args, args); \
  66. if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P ((__res), ), 0)) \
  67. { \
  68. ERRNO_ERRANDS(__res); \
  69. } \
  70. __res; \
  71. })
  72. /* Non const syscall number @nm
  73. * Ideally this could be folded within INLINE_SYSCALL with
  74. * __builtin_constant_p in INTERNAL_SYSCALL but that fails for syscall.c
  75. */
  76. #define INLINE_SYSCALL_NCS(nm, nr_args, args...) \
  77. ({ \
  78. register int __res __asm__("r0"); \
  79. __res = INTERNAL_SYSCALL_NCS(nm, , nr_args, args); \
  80. if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P ((__res), ), 0)) \
  81. { \
  82. ERRNO_ERRANDS(__res); \
  83. } \
  84. __res; \
  85. })
  86. #define INLINE_SYSCALL_NOERR(name, nr, args...) \
  87. ({ unsigned int _inline_sys_result = INTERNAL_SYSCALL (name, , nr, args);\
  88. (int) _inline_sys_result; })
  89. /*-------------------------------------------------------------------------
  90. * Mechanics of Trap - specific to ARC700
  91. *
  92. * Note the memory clobber is not strictly needed for intended semantics of
  93. * the inline asm. However some of the cases, such as old-style 6 arg mmap
  94. * gcc was generating code for inline syscall ahead of buffer packing needed
  95. * for syscall itself.
  96. *-------------------------------------------------------------------------*/
  97. #define ARC_TRAP_INSN "trap0 \n\t"
  98. #define INTERNAL_SYSCALL_NCS(nm, err, nr_args, args...) \
  99. ({ \
  100. /* Per ABI, r0 is 1st arg and return reg */ \
  101. register int __ret __asm__("r0"); \
  102. register int _sys_num __asm__("r8"); \
  103. \
  104. LOAD_ARGS_##nr_args (nm, args) \
  105. \
  106. __asm__ volatile ( \
  107. ARC_TRAP_INSN \
  108. : "+r" (__ret) \
  109. : "r"(_sys_num) ASM_ARGS_##nr_args \
  110. : "memory"); \
  111. \
  112. __ret; \
  113. })
  114. /* Macros for setting up inline __asm__ input regs */
  115. #define ASM_ARGS_0
  116. #define ASM_ARGS_1 ASM_ARGS_0, "r" (__ret)
  117. #define ASM_ARGS_2 ASM_ARGS_1, "r" (_arg2)
  118. #define ASM_ARGS_3 ASM_ARGS_2, "r" (_arg3)
  119. #define ASM_ARGS_4 ASM_ARGS_3, "r" (_arg4)
  120. #define ASM_ARGS_5 ASM_ARGS_4, "r" (_arg5)
  121. #define ASM_ARGS_6 ASM_ARGS_5, "r" (_arg6)
  122. #define ASM_ARGS_7 ASM_ARGS_6, "r" (_arg7)
  123. /* Macros for converting sys-call wrapper args into sys call args */
  124. #define LOAD_ARGS_0(nm, arg) \
  125. _sys_num = (int) (nm); \
  126. #define LOAD_ARGS_1(nm, arg1) \
  127. __ret = (int) (arg1); \
  128. LOAD_ARGS_0 (nm, arg1)
  129. /*
  130. * Note that the use of _tmpX might look superflous, however it is needed
  131. * to ensure that register variables are not clobbered if arg happens to be
  132. * a function call itself. e.g. sched_setaffinity() calling getpid() for arg2
  133. *
  134. * Also this specific order of recursive calling is important to segregate
  135. * the tmp args evaluation (function call case described above) and assigment
  136. * of register variables
  137. */
  138. #define LOAD_ARGS_2(nm, arg1, arg2) \
  139. int _tmp2 = (int) (arg2); \
  140. LOAD_ARGS_1 (nm, arg1) \
  141. register int _arg2 __asm__ ("r1") = _tmp2;
  142. #define LOAD_ARGS_3(nm, arg1, arg2, arg3) \
  143. int _tmp3 = (int) (arg3); \
  144. LOAD_ARGS_2 (nm, arg1, arg2) \
  145. register int _arg3 __asm__ ("r2") = _tmp3;
  146. #define LOAD_ARGS_4(nm, arg1, arg2, arg3, arg4) \
  147. int _tmp4 = (int) (arg4); \
  148. LOAD_ARGS_3 (nm, arg1, arg2, arg3) \
  149. register int _arg4 __asm__ ("r3") = _tmp4;
  150. #define LOAD_ARGS_5(nm, arg1, arg2, arg3, arg4, arg5) \
  151. int _tmp5 = (int) (arg5); \
  152. LOAD_ARGS_4 (nm, arg1, arg2, arg3, arg4) \
  153. register int _arg5 __asm__ ("r4") = _tmp5;
  154. #define LOAD_ARGS_6(nm, arg1, arg2, arg3, arg4, arg5, arg6) \
  155. int _tmp6 = (int) (arg6); \
  156. LOAD_ARGS_5 (nm, arg1, arg2, arg3, arg4, arg5) \
  157. register int _arg6 __asm__ ("r5") = _tmp6;
  158. #define LOAD_ARGS_7(nm, arg1, arg2, arg3, arg4, arg5, arg6, arg7)\
  159. int _tmp7 = (int) (arg7); \
  160. LOAD_ARGS_6 (nm, arg1, arg2, arg3, arg4, arg5, arg6) \
  161. register int _arg7 __asm__ ("r6") = _tmp7;
  162. #else
  163. #define ARC_TRAP_INSN trap0
  164. #endif /* __ASSEMBLER__ */
  165. #endif /* _BITS_SYSCALLS_H */