memset.S 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1998, 1999, 2000 by Ralf Baechle
  7. * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
  8. * Copyright (C) 2007 by Maciej W. Rozycki
  9. * Copyright (C) 2011, 2012 MIPS Technologies, Inc.
  10. */
  11. #include <asm/asm.h>
  12. #include <asm/asm-offsets.h>
  13. #include <asm/regdef.h>
  14. #if LONGSIZE == 4
  15. #define LONG_S_L swl
  16. #define LONG_S_R swr
  17. #else
  18. #define LONG_S_L sdl
  19. #define LONG_S_R sdr
  20. #endif
  21. #ifdef CONFIG_CPU_MICROMIPS
  22. #define STORSIZE (LONGSIZE * 2)
  23. #define STORMASK (STORSIZE - 1)
  24. #define FILL64RG t8
  25. #define FILLPTRG t7
  26. #undef LONG_S
  27. #define LONG_S LONG_SP
  28. #else
  29. #define STORSIZE LONGSIZE
  30. #define STORMASK LONGMASK
  31. #define FILL64RG a1
  32. #define FILLPTRG t0
  33. #endif
  34. #define LEGACY_MODE 1
  35. #define EVA_MODE 2
  36. /*
  37. * No need to protect it with EVA #ifdefery. The generated block of code
  38. * will never be assembled if EVA is not enabled.
  39. */
  40. #define __EVAFY(insn, reg, addr) __BUILD_EVA_INSN(insn##e, reg, addr)
  41. #define ___BUILD_EVA_INSN(insn, reg, addr) __EVAFY(insn, reg, addr)
  42. #define EX(insn,reg,addr,handler) \
  43. .if \mode == LEGACY_MODE; \
  44. 9: insn reg, addr; \
  45. .else; \
  46. 9: ___BUILD_EVA_INSN(insn, reg, addr); \
  47. .endif; \
  48. .section __ex_table,"a"; \
  49. PTR 9b, handler; \
  50. .previous
  51. .macro f_fill64 dst, offset, val, fixup, mode
  52. EX(LONG_S, \val, (\offset + 0 * STORSIZE)(\dst), \fixup)
  53. EX(LONG_S, \val, (\offset + 1 * STORSIZE)(\dst), \fixup)
  54. EX(LONG_S, \val, (\offset + 2 * STORSIZE)(\dst), \fixup)
  55. EX(LONG_S, \val, (\offset + 3 * STORSIZE)(\dst), \fixup)
  56. #if ((defined(CONFIG_CPU_MICROMIPS) && (LONGSIZE == 4)) || !defined(CONFIG_CPU_MICROMIPS))
  57. EX(LONG_S, \val, (\offset + 4 * STORSIZE)(\dst), \fixup)
  58. EX(LONG_S, \val, (\offset + 5 * STORSIZE)(\dst), \fixup)
  59. EX(LONG_S, \val, (\offset + 6 * STORSIZE)(\dst), \fixup)
  60. EX(LONG_S, \val, (\offset + 7 * STORSIZE)(\dst), \fixup)
  61. #endif
  62. #if (!defined(CONFIG_CPU_MICROMIPS) && (LONGSIZE == 4))
  63. EX(LONG_S, \val, (\offset + 8 * STORSIZE)(\dst), \fixup)
  64. EX(LONG_S, \val, (\offset + 9 * STORSIZE)(\dst), \fixup)
  65. EX(LONG_S, \val, (\offset + 10 * STORSIZE)(\dst), \fixup)
  66. EX(LONG_S, \val, (\offset + 11 * STORSIZE)(\dst), \fixup)
  67. EX(LONG_S, \val, (\offset + 12 * STORSIZE)(\dst), \fixup)
  68. EX(LONG_S, \val, (\offset + 13 * STORSIZE)(\dst), \fixup)
  69. EX(LONG_S, \val, (\offset + 14 * STORSIZE)(\dst), \fixup)
  70. EX(LONG_S, \val, (\offset + 15 * STORSIZE)(\dst), \fixup)
  71. #endif
  72. .endm
  73. .set noreorder
  74. .align 5
  75. /*
  76. * Macro to generate the __bzero{,_user} symbol
  77. * Arguments:
  78. * mode: LEGACY_MODE or EVA_MODE
  79. */
  80. .macro __BUILD_BZERO mode
  81. /* Initialize __memset if this is the first time we call this macro */
  82. .ifnotdef __memset
  83. .set __memset, 1
  84. .hidden __memset /* Make sure it does not leak */
  85. .endif
  86. sltiu t0, a2, STORSIZE /* very small region? */
  87. bnez t0, .Lsmall_memset\@
  88. andi t0, a0, STORMASK /* aligned? */
  89. #ifdef CONFIG_CPU_MICROMIPS
  90. move t8, a1 /* used by 'swp' instruction */
  91. move t9, a1
  92. #endif
  93. #ifndef CONFIG_CPU_DADDI_WORKAROUNDS
  94. beqz t0, 1f
  95. PTR_SUBU t0, STORSIZE /* alignment in bytes */
  96. #else
  97. .set noat
  98. li AT, STORSIZE
  99. beqz t0, 1f
  100. PTR_SUBU t0, AT /* alignment in bytes */
  101. .set at
  102. #endif
  103. #ifndef CONFIG_CPU_MIPSR6
  104. R10KCBARRIER(0(ra))
  105. #ifdef __MIPSEB__
  106. EX(LONG_S_L, a1, (a0), .Lfirst_fixup\@) /* make word/dword aligned */
  107. #else
  108. EX(LONG_S_R, a1, (a0), .Lfirst_fixup\@) /* make word/dword aligned */
  109. #endif
  110. PTR_SUBU a0, t0 /* long align ptr */
  111. PTR_ADDU a2, t0 /* correct size */
  112. #else /* CONFIG_CPU_MIPSR6 */
  113. #define STORE_BYTE(N) \
  114. EX(sb, a1, N(a0), .Lbyte_fixup\@); \
  115. beqz t0, 0f; \
  116. PTR_ADDU t0, 1;
  117. PTR_ADDU a2, t0 /* correct size */
  118. PTR_ADDU t0, 1
  119. STORE_BYTE(0)
  120. STORE_BYTE(1)
  121. #if LONGSIZE == 4
  122. EX(sb, a1, 2(a0), .Lbyte_fixup\@)
  123. #else
  124. STORE_BYTE(2)
  125. STORE_BYTE(3)
  126. STORE_BYTE(4)
  127. STORE_BYTE(5)
  128. EX(sb, a1, 6(a0), .Lbyte_fixup\@)
  129. #endif
  130. 0:
  131. ori a0, STORMASK
  132. xori a0, STORMASK
  133. PTR_ADDIU a0, STORSIZE
  134. #endif /* CONFIG_CPU_MIPSR6 */
  135. 1: ori t1, a2, 0x3f /* # of full blocks */
  136. xori t1, 0x3f
  137. beqz t1, .Lmemset_partial\@ /* no block to fill */
  138. andi t0, a2, 0x40-STORSIZE
  139. PTR_ADDU t1, a0 /* end address */
  140. .set reorder
  141. 1: PTR_ADDIU a0, 64
  142. R10KCBARRIER(0(ra))
  143. f_fill64 a0, -64, FILL64RG, .Lfwd_fixup\@, \mode
  144. bne t1, a0, 1b
  145. .set noreorder
  146. .Lmemset_partial\@:
  147. R10KCBARRIER(0(ra))
  148. PTR_LA t1, 2f /* where to start */
  149. #ifdef CONFIG_CPU_MICROMIPS
  150. LONG_SRL t7, t0, 1
  151. #endif
  152. #if LONGSIZE == 4
  153. PTR_SUBU t1, FILLPTRG
  154. #else
  155. .set noat
  156. LONG_SRL AT, FILLPTRG, 1
  157. PTR_SUBU t1, AT
  158. .set at
  159. #endif
  160. jr t1
  161. PTR_ADDU a0, t0 /* dest ptr */
  162. .set push
  163. .set noreorder
  164. .set nomacro
  165. /* ... but first do longs ... */
  166. f_fill64 a0, -64, FILL64RG, .Lpartial_fixup\@, \mode
  167. 2: .set pop
  168. andi a2, STORMASK /* At most one long to go */
  169. beqz a2, 1f
  170. #ifndef CONFIG_CPU_MIPSR6
  171. PTR_ADDU a0, a2 /* What's left */
  172. R10KCBARRIER(0(ra))
  173. #ifdef __MIPSEB__
  174. EX(LONG_S_R, a1, -1(a0), .Llast_fixup\@)
  175. #else
  176. EX(LONG_S_L, a1, -1(a0), .Llast_fixup\@)
  177. #endif
  178. #else
  179. PTR_SUBU t0, $0, a2
  180. PTR_ADDIU t0, 1
  181. STORE_BYTE(0)
  182. STORE_BYTE(1)
  183. #if LONGSIZE == 4
  184. EX(sb, a1, 2(a0), .Lbyte_fixup\@)
  185. #else
  186. STORE_BYTE(2)
  187. STORE_BYTE(3)
  188. STORE_BYTE(4)
  189. STORE_BYTE(5)
  190. EX(sb, a1, 6(a0), .Lbyte_fixup\@)
  191. #endif
  192. 0:
  193. #endif
  194. 1: jr ra
  195. move a2, zero
  196. .Lsmall_memset\@:
  197. beqz a2, 2f
  198. PTR_ADDU t1, a0, a2
  199. 1: PTR_ADDIU a0, 1 /* fill bytewise */
  200. R10KCBARRIER(0(ra))
  201. bne t1, a0, 1b
  202. sb a1, -1(a0)
  203. 2: jr ra /* done */
  204. move a2, zero
  205. .if __memset == 1
  206. END(memset)
  207. .set __memset, 0
  208. .hidden __memset
  209. .endif
  210. .Lbyte_fixup\@:
  211. PTR_SUBU a2, $0, t0
  212. jr ra
  213. PTR_ADDIU a2, 1
  214. .Lfirst_fixup\@:
  215. jr ra
  216. nop
  217. .Lfwd_fixup\@:
  218. PTR_L t0, TI_TASK($28)
  219. andi a2, 0x3f
  220. LONG_L t0, THREAD_BUADDR(t0)
  221. LONG_ADDU a2, t1
  222. jr ra
  223. LONG_SUBU a2, t0
  224. .Lpartial_fixup\@:
  225. PTR_L t0, TI_TASK($28)
  226. andi a2, STORMASK
  227. LONG_L t0, THREAD_BUADDR(t0)
  228. LONG_ADDU a2, t1
  229. jr ra
  230. LONG_SUBU a2, t0
  231. .Llast_fixup\@:
  232. jr ra
  233. andi v1, a2, STORMASK
  234. .endm
  235. /*
  236. * memset(void *s, int c, size_t n)
  237. *
  238. * a0: start of area to clear
  239. * a1: char to fill with
  240. * a2: size of area to clear
  241. */
  242. LEAF(memset)
  243. beqz a1, 1f
  244. move v0, a0 /* result */
  245. andi a1, 0xff /* spread fillword */
  246. LONG_SLL t1, a1, 8
  247. or a1, t1
  248. LONG_SLL t1, a1, 16
  249. #if LONGSIZE == 8
  250. or a1, t1
  251. LONG_SLL t1, a1, 32
  252. #endif
  253. or a1, t1
  254. 1:
  255. #ifndef CONFIG_EVA
  256. FEXPORT(__bzero)
  257. #endif
  258. __BUILD_BZERO LEGACY_MODE
  259. #ifdef CONFIG_EVA
  260. LEAF(__bzero)
  261. __BUILD_BZERO EVA_MODE
  262. END(__bzero)
  263. #endif