sysdep.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* Assembler macros for SH.
  2. Copyright (C) 1999, 2000, 2005 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. #include <common/sysdep.h>
  16. #include <features.h>
  17. #ifdef __ASSEMBLER__
  18. /* Syntactic details of assembler. */
  19. #define LOCAL(X) .L_##X
  20. #define ALIGNARG(log2) log2
  21. /* For ELF we need the `.type' directive to make shared libs work right. */
  22. #define ASM_TYPE_DIRECTIVE(name,typearg) .type name,@##typearg;
  23. #define ASM_SIZE_DIRECTIVE(name) .size name,.-name
  24. #ifdef SHARED
  25. #define PLTJMP(_x) _x##@PLT
  26. #else
  27. #define PLTJMP(_x) _x
  28. #endif
  29. /* Define an entry point visible from C. */
  30. #define ENTRY(name) \
  31. ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME(name); \
  32. ASM_TYPE_DIRECTIVE (C_SYMBOL_NAME(name),function) \
  33. .align ALIGNARG(5); \
  34. C_LABEL(name) \
  35. cfi_startproc; \
  36. CALL_MCOUNT
  37. #undef END
  38. #define END(name) \
  39. cfi_endproc; \
  40. ASM_SIZE_DIRECTIVE(C_SYMBOL_NAME(name))
  41. /* If compiled for profiling, call `mcount' at the start of each function. */
  42. #ifdef PROF
  43. #define CALL_MCOUNT \
  44. mov.l 1f,r1; \
  45. sts.l pr,@-r15; \
  46. cfi_adjust_cfa_offset (4); \
  47. cfi_rel_offset (pr, 0); \
  48. mova 2f,r0; \
  49. jmp @r1; \
  50. lds r0,pr; \
  51. .align 2; \
  52. 1: .long mcount; \
  53. 2: lds.l @r15+,pr; \
  54. cfi_adjust_cfa_offset (-4); \
  55. cfi_restore (pr)
  56. #else
  57. #define CALL_MCOUNT /* Do nothing. */
  58. #endif
  59. #ifdef __UCLIBC_UNDERSCORES__
  60. /* Since C identifiers are not normally prefixed with an underscore
  61. on this system, the asm identifier `syscall_error' intrudes on the
  62. C name space. Make sure we use an innocuous name. */
  63. #define syscall_error __syscall_error
  64. #define mcount _mcount
  65. #endif
  66. /* For Linux we can use the system call table in the header file
  67. /usr/include/asm/unistd.h
  68. of the kernel. But these symbols do not follow the SYS_* syntax
  69. so we have to redefine the `SYS_ify' macro here. */
  70. #undef SYS_ify
  71. #define SYS_ify(syscall_name) (__NR_##syscall_name)
  72. #define ret rts ; nop
  73. /* The sh move insn is s, d. */
  74. #define MOVE(x,y) mov x , y
  75. /* Linux uses a negative return value to indicate syscall errors,
  76. unlike most Unices, which use the condition codes' carry flag.
  77. Since version 2.1 the return value of a system call might be
  78. negative even if the call succeeded. E.g., the `lseek' system call
  79. might return a large offset. Therefore we must not anymore test
  80. for < 0, but test for a real error by making sure the value in R0
  81. is a real error number. Linus said he will make sure the no syscall
  82. returns a value in -1 .. -4095 as a valid result so we can savely
  83. test with -4095. */
  84. #define _IMM1 #-1
  85. #define _IMM12 #-12
  86. #undef PSEUDO
  87. #define PSEUDO(name, syscall_name, args) \
  88. .text; \
  89. ENTRY (name); \
  90. DO_CALL (syscall_name, args); \
  91. mov r0,r1; \
  92. mov _IMM12,r2; \
  93. shad r2,r1; \
  94. not r1,r1; \
  95. tst r1,r1; \
  96. bf .Lpseudo_end; \
  97. SYSCALL_ERROR_HANDLER; \
  98. .Lpseudo_end:
  99. #undef PSEUDO_END
  100. #define PSEUDO_END(name) \
  101. END (name)
  102. #undef PSEUDO_NOERRNO
  103. #define PSEUDO_NOERRNO(name, syscall_name, args) \
  104. .text; \
  105. ENTRY (name); \
  106. DO_CALL (syscall_name, args)
  107. #undef PSEUDO_END_NOERRNO
  108. #define PSEUDO_END_NOERRNO(name) \
  109. END (name)
  110. #define ret_NOERRNO ret
  111. #define PSEUDO_ERRVAL(name, syscall_name, args) \
  112. .text; \
  113. ENTRY (name); \
  114. DO_CALL (syscall_name, args);
  115. #undef PSEUDO_END_ERRVAL
  116. #define PSEUDO_END_ERRVAL(name) \
  117. END (name)
  118. #define ret_ERRVAL ret
  119. #ifndef __PIC__
  120. # define SYSCALL_ERROR_HANDLER \
  121. mov.l 0f,r1; \
  122. jmp @r1; \
  123. mov r0,r4; \
  124. .align 2; \
  125. 0: .long __syscall_error
  126. #include <libc/sysdeps/linux/sh/syscall_error.S>
  127. #else
  128. # ifdef RTLD_PRIVATE_ERRNO
  129. # define SYSCALL_ERROR_HANDLER \
  130. neg r0,r1; \
  131. mov.l 0f,r12; \
  132. mova 0f,r0; \
  133. add r0,r12; \
  134. mov.l 1f,r0; \
  135. mov.l r1,@(r0,r12)
  136. bra .Lpseudo_end; \
  137. mov _IMM1,r0; \
  138. .align 2; \
  139. 0: .long _GLOBAL_OFFSET_TABLE_; \
  140. 1: .long rtld_errno@GOTOFF
  141. # elif defined _LIBC_REENTRANT
  142. # if defined USE___THREAD
  143. # ifndef NOT_IN_libc
  144. # define SYSCALL_ERROR_ERRNO __libc_errno
  145. # else
  146. # define SYSCALL_ERROR_ERRNO errno
  147. # endif
  148. # define SYSCALL_ERROR_HANDLER \
  149. neg r0,r1; \
  150. mov r12,r2; \
  151. mov.l 0f,r12; \
  152. mova 0f,r0; \
  153. add r0,r12; \
  154. mov.l 1f,r0; \
  155. stc gbr, r4; \
  156. mov.l @(r0,r12),r0; \
  157. bra .Lskip; \
  158. add r4,r0; \
  159. .align 2; \
  160. 1: .long SYSCALL_ERROR_ERRNO@GOTTPOFF; \
  161. .Lskip: \
  162. mov r2,r12; \
  163. mov.l r1,@r0; \
  164. bra .Lpseudo_end; \
  165. mov _IMM1,r0; \
  166. .align 2; \
  167. 0: .long _GLOBAL_OFFSET_TABLE_
  168. # else
  169. # define SYSCALL_ERROR_HANDLER \
  170. neg r0,r1; \
  171. mov.l r14,@-r15; \
  172. mov.l r12,@-r15; \
  173. mov.l r1,@-r15; \
  174. mov.l 0f,r12; \
  175. mova 0f,r0; \
  176. add r0,r12; \
  177. sts.l pr,@-r15; \
  178. mov r15,r14; \
  179. mov.l 1f,r1; \
  180. bsrf r1; \
  181. nop; \
  182. 2: mov r14,r15; \
  183. lds.l @r15+,pr; \
  184. mov.l @r15+,r1; \
  185. mov.l r1,@r0; \
  186. mov.l @r15+,r12; \
  187. mov.l @r15+,r14; \
  188. bra .Lpseudo_end; \
  189. mov _IMM1,r0; \
  190. .align 2; \
  191. 0: .long _GLOBAL_OFFSET_TABLE_; \
  192. 1: .long PLTJMP(C_SYMBOL_NAME(__errno_location))-(2b-.)
  193. /* A quick note: it is assumed that the call to `__errno_location' does
  194. not modify the stack! */
  195. # endif
  196. # else
  197. /* Store (-r0) into errno through the GOT. */
  198. # define SYSCALL_ERROR_HANDLER \
  199. neg r0,r1; \
  200. mov r12,r2; \
  201. mov.l 0f,r12; \
  202. mova 0f,r0; \
  203. add r0,r12; \
  204. mov.l 1f,r0; \
  205. mov.l @(r0,r12),r0; \
  206. mov r2,r12; \
  207. mov.l r1,@r0; \
  208. bra .Lpseudo_end; \
  209. mov _IMM1,r0; \
  210. .align 2; \
  211. 0: .long _GLOBAL_OFFSET_TABLE_; \
  212. 1: .long errno@GOT
  213. # endif /* _LIBC_REENTRANT */
  214. #endif /* __PIC__ */
  215. # ifdef __SH4__
  216. # define SYSCALL_INST_PAD \
  217. or r0,r0; or r0,r0; or r0,r0; or r0,r0; or r0,r0
  218. # else
  219. # define SYSCALL_INST_PAD
  220. # endif
  221. #define SYSCALL_INST0 trapa #0x10
  222. #define SYSCALL_INST1 trapa #0x11
  223. #define SYSCALL_INST2 trapa #0x12
  224. #define SYSCALL_INST3 trapa #0x13
  225. #define SYSCALL_INST4 trapa #0x14
  226. #define SYSCALL_INST5 mov.l @(0,r15),r0; trapa #0x15
  227. #define SYSCALL_INST6 mov.l @(0,r15),r0; mov.l @(4,r15),r1; trapa #0x16
  228. #undef DO_CALL
  229. #define DO_CALL(syscall_name, args) \
  230. mov.l 1f,r3; \
  231. SYSCALL_INST##args; \
  232. SYSCALL_INST_PAD; \
  233. bra 2f; \
  234. nop; \
  235. .align 2; \
  236. 1: .long SYS_ify (syscall_name); \
  237. 2:
  238. #endif /* __ASSEMBLER__ */
  239. /* Pointer mangling support. */
  240. #if defined NOT_IN_libc && defined IS_IN_rtld
  241. /* We cannot use the thread descriptor because in ld.so we use setjmp
  242. earlier than the descriptor is initialized. Using a global variable
  243. is too complicated here since we have no PC-relative addressing mode. */
  244. #else
  245. # ifdef __ASSEMBLER__
  246. # define PTR_MANGLE(reg, tmp) \
  247. stc gbr,tmp; mov.l @(POINTER_GUARD,tmp),tmp; xor tmp,reg
  248. # define PTR_MANGLE2(reg, tmp) xor tmp,reg
  249. # define PTR_DEMANGLE(reg, tmp) PTR_MANGLE (reg, tmp)
  250. # define PTR_DEMANGLE2(reg, tmp) PTR_MANGLE2 (reg, tmp)
  251. # else
  252. # define PTR_MANGLE(var) \
  253. (var) = (void *) ((uintptr_t) (var) ^ THREAD_GET_POINTER_GUARD ())
  254. # define PTR_DEMANGLE(var) PTR_MANGLE (var)
  255. # endif
  256. #endif