sysdep.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* Assembler macros for Nios II.
  2. Copyright (C) 2000-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _LINUX_NIOS2_SYSDEP_H
  16. #define _LINUX_NIOS2_SYSDEP_H 1
  17. #include <asm/unistd.h>
  18. #include <sysdeps/unix/sysdep.h>
  19. #include <sysdeps/nios2/sysdep.h>
  20. #include <sysdeps/unix/sysv/linux/generic/sysdep.h>
  21. /* For RTLD_PRIVATE_ERRNO. */
  22. #include <dl-sysdep.h>
  23. #include <tls.h>
  24. /* For Linux we can use the system call table in the header file
  25. /usr/include/asm/unistd.h
  26. of the kernel. But these symbols do not follow the SYS_* syntax
  27. so we have to redefine the `SYS_ify' macro here. */
  28. #undef SYS_ify
  29. #define SYS_ify(syscall_name) __NR_##syscall_name
  30. #ifdef __ASSEMBLER__
  31. #define SYSCALL_ERROR_LABEL __local_syscall_error
  32. #undef PSEUDO
  33. #define PSEUDO(name, syscall_name, args) \
  34. ENTRY (name) \
  35. DO_CALL (syscall_name, args) \
  36. bne r7, zero, SYSCALL_ERROR_LABEL; \
  37. #undef PSEUDO_END
  38. #define PSEUDO_END(name) \
  39. SYSCALL_ERROR_HANDLER \
  40. END (name)
  41. #undef PSEUDO_NOERRNO
  42. #define PSEUDO_NOERRNO(name, syscall_name, args) \
  43. ENTRY (name) \
  44. DO_CALL (syscall_name, args)
  45. #undef PSEUDO_END_NOERRNO
  46. #define PSEUDO_END_NOERRNO(name) \
  47. END (name)
  48. #undef ret_NOERRNO
  49. #define ret_NOERRNO ret
  50. #undef DO_CALL
  51. #define DO_CALL(syscall_name, args) \
  52. DOARGS_##args \
  53. movi r2, SYS_ify(syscall_name); \
  54. trap;
  55. #if defined(__PIC__) || defined(PIC)
  56. # if RTLD_PRIVATE_ERRNO
  57. # define SYSCALL_ERROR_HANDLER \
  58. SYSCALL_ERROR_LABEL: \
  59. nextpc r3; \
  60. 1: \
  61. movhi r8, %hiadj(rtld_errno - 1b); \
  62. addi r8, r8, %lo(rtld_errno - 1b); \
  63. add r3, r3, r8; \
  64. stw r2, 0(r3); \
  65. movi r2, -1; \
  66. ret;
  67. # else
  68. # if IS_IN (libc)
  69. # define SYSCALL_ERROR_ERRNO __libc_errno
  70. # else
  71. # define SYSCALL_ERROR_ERRNO errno
  72. # endif
  73. # define SYSCALL_ERROR_HANDLER \
  74. SYSCALL_ERROR_LABEL: \
  75. nextpc r3; \
  76. 1: \
  77. movhi r8, %hiadj(_gp_got - 1b); \
  78. addi r8, r8, %lo(_gp_got - 1b); \
  79. add r3, r3, r8; \
  80. ldw r3, %tls_ie(SYSCALL_ERROR_ERRNO)(r3); \
  81. add r3, r23, r3; \
  82. stw r2, 0(r3); \
  83. movi r2, -1; \
  84. ret;
  85. # endif
  86. #else
  87. /* We can use a single error handler in the static library. */
  88. #define SYSCALL_ERROR_HANDLER \
  89. SYSCALL_ERROR_LABEL: \
  90. jmpi __syscall_error;
  91. #endif
  92. #define DOARGS_0 /* nothing */
  93. #define DOARGS_1 /* nothing */
  94. #define DOARGS_2 /* nothing */
  95. #define DOARGS_3 /* nothing */
  96. #define DOARGS_4 /* nothing */
  97. #define DOARGS_5 ldw r8, 0(sp);
  98. #define DOARGS_6 ldw r9, 4(sp); ldw r8, 0(sp);
  99. /* The function has to return the error code. */
  100. #undef PSEUDO_ERRVAL
  101. #define PSEUDO_ERRVAL(name, syscall_name, args) \
  102. ENTRY (name) \
  103. DO_CALL (syscall_name, args)
  104. #undef PSEUDO_END_ERRVAL
  105. #define PSEUDO_END_ERRVAL(name) \
  106. END (name)
  107. #define ret_ERRVAL ret
  108. #else /* __ASSEMBLER__ */
  109. /* In order to get __set_errno() definition in INLINE_SYSCALL. */
  110. #include <errno.h>
  111. /* Previously Nios2 used the generic version without the libc_hidden_def
  112. which lead in a non existent __send symbol in libc.so. */
  113. # undef HAVE_INTERNAL_SEND_SYMBOL
  114. /* Define a macro which expands into the inline wrapper code for a system
  115. call. */
  116. #undef INLINE_SYSCALL
  117. #define INLINE_SYSCALL(name, nr, args...) \
  118. ({ INTERNAL_SYSCALL_DECL(err); \
  119. unsigned int result_var = INTERNAL_SYSCALL (name, err, nr, args); \
  120. if ( INTERNAL_SYSCALL_ERROR_P (result_var, err) ) \
  121. { \
  122. __set_errno (INTERNAL_SYSCALL_ERRNO (result_var, err)); \
  123. result_var = -1L; \
  124. } \
  125. (int) result_var; })
  126. #undef INTERNAL_SYSCALL_DECL
  127. #define INTERNAL_SYSCALL_DECL(err) unsigned int err __attribute__((unused))
  128. #undef INTERNAL_SYSCALL_ERROR_P
  129. #define INTERNAL_SYSCALL_ERROR_P(val, err) ((void) (val), (unsigned int) (err))
  130. #undef INTERNAL_SYSCALL_ERRNO
  131. #define INTERNAL_SYSCALL_ERRNO(val, err) ((void) (err), val)
  132. #undef INTERNAL_SYSCALL_RAW
  133. #define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
  134. ({ unsigned int _sys_result; \
  135. { \
  136. /* Load argument values in temporary variables
  137. to perform side effects like function calls
  138. before the call-used registers are set. */ \
  139. LOAD_ARGS_##nr (args) \
  140. LOAD_REGS_##nr \
  141. register int _r2 asm ("r2") = (int)(name); \
  142. register int _err asm ("r7"); \
  143. asm volatile ("trap" \
  144. : "+r" (_r2), "=r" (_err) \
  145. : ASM_ARGS_##nr \
  146. : __SYSCALL_CLOBBERS); \
  147. _sys_result = _r2; \
  148. err = _err; \
  149. } \
  150. (int) _sys_result; })
  151. #undef INTERNAL_SYSCALL
  152. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  153. INTERNAL_SYSCALL_RAW(SYS_ify(name), err, nr, args)
  154. #undef INTERNAL_SYSCALL_NCS
  155. #define INTERNAL_SYSCALL_NCS(number, err, nr, args...) \
  156. INTERNAL_SYSCALL_RAW(number, err, nr, args)
  157. #define LOAD_ARGS_0()
  158. #define LOAD_REGS_0
  159. #define ASM_ARGS_0
  160. #define LOAD_ARGS_1(a1) \
  161. LOAD_ARGS_0 () \
  162. int __arg1 = (int) (a1);
  163. #define LOAD_REGS_1 \
  164. register int _r4 asm ("r4") = __arg1; \
  165. LOAD_REGS_0
  166. #define ASM_ARGS_1 "r" (_r4)
  167. #define LOAD_ARGS_2(a1, a2) \
  168. LOAD_ARGS_1 (a1) \
  169. int __arg2 = (int) (a2);
  170. #define LOAD_REGS_2 \
  171. register int _r5 asm ("r5") = __arg2; \
  172. LOAD_REGS_1
  173. #define ASM_ARGS_2 ASM_ARGS_1, "r" (_r5)
  174. #define LOAD_ARGS_3(a1, a2, a3) \
  175. LOAD_ARGS_2 (a1, a2) \
  176. int __arg3 = (int) (a3);
  177. #define LOAD_REGS_3 \
  178. register int _r6 asm ("r6") = __arg3; \
  179. LOAD_REGS_2
  180. #define ASM_ARGS_3 ASM_ARGS_2, "r" (_r6)
  181. #define LOAD_ARGS_4(a1, a2, a3, a4) \
  182. LOAD_ARGS_3 (a1, a2, a3) \
  183. int __arg4 = (int) (a4);
  184. #define LOAD_REGS_4 \
  185. register int _r7 asm ("r7") = __arg4; \
  186. LOAD_REGS_3
  187. #define ASM_ARGS_4 ASM_ARGS_3, "r" (_r7)
  188. #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
  189. LOAD_ARGS_4 (a1, a2, a3, a4) \
  190. int __arg5 = (int) (a5);
  191. #define LOAD_REGS_5 \
  192. register int _r8 asm ("r8") = __arg5; \
  193. LOAD_REGS_4
  194. #define ASM_ARGS_5 ASM_ARGS_4, "r" (_r8)
  195. #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
  196. LOAD_ARGS_5 (a1, a2, a3, a4, a5) \
  197. int __arg6 = (int) (a6);
  198. #define LOAD_REGS_6 \
  199. register int _r9 asm ("r9") = __arg6; \
  200. LOAD_REGS_5
  201. #define ASM_ARGS_6 ASM_ARGS_5, "r" (_r9)
  202. #define __SYSCALL_CLOBBERS "memory"
  203. #endif /* __ASSEMBLER__ */
  204. /* Pointer mangling support. */
  205. #if IS_IN (rtld)
  206. /* We cannot use the thread descriptor because in ld.so we use setjmp
  207. earlier than the descriptor is initialized. */
  208. #else
  209. # ifdef __ASSEMBLER__
  210. # define PTR_MANGLE_GUARD(guard) ldw guard, POINTER_GUARD(r23)
  211. # define PTR_MANGLE(dst, src, guard) xor dst, src, guard
  212. # define PTR_DEMANGLE(dst, src, guard) PTR_MANGLE (dst, src, guard)
  213. # else
  214. # define PTR_MANGLE(var) \
  215. (var) = (__typeof (var)) ((uintptr_t) (var) ^ THREAD_GET_POINTER_GUARD ())
  216. # define PTR_DEMANGLE(var) PTR_MANGLE (var)
  217. # endif
  218. #endif
  219. #endif /* linux/nios2/sysdep.h */