sysdep.h 7.6 KB

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