sysdep.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. .globl 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. #ifndef __PIC__
  119. # define SYSCALL_ERROR_HANDLER \
  120. mov.l 0f,r1; \
  121. jmp @r1; \
  122. mov r0,r4; \
  123. .align 2; \
  124. 0: .long __syscall_error
  125. #include <libc/sysdeps/linux/sh/syscall_error.S>
  126. #else
  127. # ifdef RTLD_PRIVATE_ERRNO
  128. # define SYSCALL_ERROR_HANDLER \
  129. neg r0,r1; \
  130. mov.l 0f,r12; \
  131. mova 0f,r0; \
  132. add r0,r12; \
  133. mov.l 1f,r0; \
  134. mov.l r1,@(r0,r12)
  135. bra .Lpseudo_end; \
  136. mov _IMM1,r0; \
  137. .align 2; \
  138. 0: .long _GLOBAL_OFFSET_TABLE_; \
  139. 1: .long rtld_errno@GOTOFF
  140. # elif defined _LIBC_REENTRANT
  141. # if defined USE___THREAD
  142. # ifndef NOT_IN_libc
  143. # define SYSCALL_ERROR_ERRNO __libc_errno
  144. # else
  145. # define SYSCALL_ERROR_ERRNO errno
  146. # endif
  147. # define SYSCALL_ERROR_HANDLER \
  148. neg r0,r1; \
  149. mov r12,r2; \
  150. mov.l 0f,r12; \
  151. mova 0f,r0; \
  152. add r0,r12; \
  153. mov.l 1f,r0; \
  154. stc gbr, r4; \
  155. mov.l @(r0,r12),r0; \
  156. bra .Lskip; \
  157. add r4,r0; \
  158. .align 2; \
  159. 1: .long SYSCALL_ERROR_ERRNO@GOTTPOFF; \
  160. .Lskip: \
  161. mov r2,r12; \
  162. mov.l r1,@r0; \
  163. bra .Lpseudo_end; \
  164. mov _IMM1,r0; \
  165. .align 2; \
  166. 0: .long _GLOBAL_OFFSET_TABLE_
  167. # else
  168. # define SYSCALL_ERROR_HANDLER \
  169. neg r0,r1; \
  170. mov.l r14,@-r15; \
  171. mov.l r12,@-r15; \
  172. mov.l r1,@-r15; \
  173. mov.l 0f,r12; \
  174. mova 0f,r0; \
  175. add r0,r12; \
  176. sts.l pr,@-r15; \
  177. mov r15,r14; \
  178. mov.l 1f,r1; \
  179. bsrf r1; \
  180. nop; \
  181. 2: mov r14,r15; \
  182. lds.l @r15+,pr; \
  183. mov.l @r15+,r1; \
  184. mov.l r1,@r0; \
  185. mov.l @r15+,r12; \
  186. mov.l @r15+,r14; \
  187. bra .Lpseudo_end; \
  188. mov _IMM1,r0; \
  189. .align 2; \
  190. 0: .long _GLOBAL_OFFSET_TABLE_; \
  191. 1: .long PLTJMP(C_SYMBOL_NAME(__errno_location))-(2b-.)
  192. /* A quick note: it is assumed that the call to `__errno_location' does
  193. not modify the stack! */
  194. # endif
  195. # else
  196. /* Store (-r0) into errno through the GOT. */
  197. # define SYSCALL_ERROR_HANDLER \
  198. neg r0,r1; \
  199. mov r12,r2; \
  200. mov.l 0f,r12; \
  201. mova 0f,r0; \
  202. add r0,r12; \
  203. mov.l 1f,r0; \
  204. mov.l @(r0,r12),r0; \
  205. mov r2,r12; \
  206. mov.l r1,@r0; \
  207. bra .Lpseudo_end; \
  208. mov _IMM1,r0; \
  209. .align 2; \
  210. 0: .long _GLOBAL_OFFSET_TABLE_; \
  211. 1: .long errno@GOT
  212. # endif /* _LIBC_REENTRANT */
  213. #endif /* __PIC__ */
  214. # ifdef __SH4__
  215. # define SYSCALL_INST_PAD \
  216. or r0,r0; or r0,r0; or r0,r0; or r0,r0; or r0,r0
  217. # else
  218. # define SYSCALL_INST_PAD
  219. # endif
  220. #define SYSCALL_INST0 trapa #0x10
  221. #define SYSCALL_INST1 trapa #0x11
  222. #define SYSCALL_INST2 trapa #0x12
  223. #define SYSCALL_INST3 trapa #0x13
  224. #define SYSCALL_INST4 trapa #0x14
  225. #define SYSCALL_INST5 mov.l @(0,r15),r0; trapa #0x15
  226. #define SYSCALL_INST6 mov.l @(0,r15),r0; mov.l @(4,r15),r1; trapa #0x16
  227. #undef DO_CALL
  228. #define DO_CALL(syscall_name, args) \
  229. mov.l 1f,r3; \
  230. SYSCALL_INST##args; \
  231. SYSCALL_INST_PAD; \
  232. bra 2f; \
  233. nop; \
  234. .align 2; \
  235. 1: .long SYS_ify (syscall_name); \
  236. 2:
  237. #endif /* __ASSEMBLER__ */
  238. /* Pointer mangling support. */
  239. #if defined NOT_IN_libc && defined IS_IN_rtld
  240. /* We cannot use the thread descriptor because in ld.so we use setjmp
  241. earlier than the descriptor is initialized. Using a global variable
  242. is too complicated here since we have no PC-relative addressing mode. */
  243. #else
  244. # ifdef __ASSEMBLER__
  245. # define PTR_MANGLE(reg, tmp) \
  246. stc gbr,tmp; mov.l @(POINTER_GUARD,tmp),tmp; xor tmp,reg
  247. # define PTR_MANGLE2(reg, tmp) xor tmp,reg
  248. # define PTR_DEMANGLE(reg, tmp) PTR_MANGLE (reg, tmp)
  249. # define PTR_DEMANGLE2(reg, tmp) PTR_MANGLE2 (reg, tmp)
  250. # else
  251. # define PTR_MANGLE(var) \
  252. (var) = (void *) ((uintptr_t) (var) ^ THREAD_GET_POINTER_GUARD ())
  253. # define PTR_DEMANGLE(var) PTR_MANGLE (var)
  254. # endif
  255. #endif