sysdep.h 7.6 KB

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