copy_template.S 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * linux/arch/arm/lib/copy_template.s
  3. *
  4. * Code template for optimized memory copy functions
  5. *
  6. * Author: Nicolas Pitre
  7. * Created: Sep 28, 2005
  8. * Copyright: MontaVista Software, Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. /*
  15. * Theory of operation
  16. * -------------------
  17. *
  18. * This file provides the core code for a forward memory copy used in
  19. * the implementation of memcopy(), copy_to_user() and copy_from_user().
  20. *
  21. * The including file must define the following accessor macros
  22. * according to the need of the given function:
  23. *
  24. * ldr1w ptr reg abort
  25. *
  26. * This loads one word from 'ptr', stores it in 'reg' and increments
  27. * 'ptr' to the next word. The 'abort' argument is used for fixup tables.
  28. *
  29. * ldr4w ptr reg1 reg2 reg3 reg4 abort
  30. * ldr8w ptr, reg1 reg2 reg3 reg4 reg5 reg6 reg7 reg8 abort
  31. *
  32. * This loads four or eight words starting from 'ptr', stores them
  33. * in provided registers and increments 'ptr' past those words.
  34. * The'abort' argument is used for fixup tables.
  35. *
  36. * ldr1b ptr reg cond abort
  37. *
  38. * Similar to ldr1w, but it loads a byte and increments 'ptr' one byte.
  39. * It also must apply the condition code if provided, otherwise the
  40. * "al" condition is assumed by default.
  41. *
  42. * str1w ptr reg abort
  43. * str8w ptr reg1 reg2 reg3 reg4 reg5 reg6 reg7 reg8 abort
  44. * str1b ptr reg cond abort
  45. *
  46. * Same as their ldr* counterparts, but data is stored to 'ptr' location
  47. * rather than being loaded.
  48. *
  49. * enter reg1 reg2
  50. *
  51. * Preserve the provided registers on the stack plus any additional
  52. * data as needed by the implementation including this code. Called
  53. * upon code entry.
  54. *
  55. * exit reg1 reg2
  56. *
  57. * Restore registers with the values previously saved with the
  58. * 'preserv' macro. Called upon code termination.
  59. *
  60. * LDR1W_SHIFT
  61. * STR1W_SHIFT
  62. *
  63. * Correction to be applied to the "ip" register when branching into
  64. * the ldr1w or str1w instructions (some of these macros may expand to
  65. * than one 32bit instruction in Thumb-2)
  66. */
  67. enter r4, lr
  68. subs r2, r2, #4
  69. blt 8f
  70. ands ip, r0, #3
  71. PLD( pld [r1, #0] )
  72. bne 9f
  73. ands ip, r1, #3
  74. bne 10f
  75. 1: subs r2, r2, #(28)
  76. stmfd sp!, {r5 - r8}
  77. blt 5f
  78. CALGN( ands ip, r0, #31 )
  79. CALGN( rsb r3, ip, #32 )
  80. CALGN( sbcnes r4, r3, r2 ) @ C is always set here
  81. CALGN( bcs 2f )
  82. CALGN( adr r4, 6f )
  83. CALGN( subs r2, r2, r3 ) @ C gets set
  84. CALGN( add pc, r4, ip )
  85. PLD( pld [r1, #0] )
  86. 2: PLD( subs r2, r2, #96 )
  87. PLD( pld [r1, #28] )
  88. PLD( blt 4f )
  89. PLD( pld [r1, #60] )
  90. PLD( pld [r1, #92] )
  91. 3: PLD( pld [r1, #124] )
  92. 4: ldr8w r1, r3, r4, r5, r6, r7, r8, ip, lr, abort=20f
  93. subs r2, r2, #32
  94. str8w r0, r3, r4, r5, r6, r7, r8, ip, lr, abort=20f
  95. bge 3b
  96. PLD( cmn r2, #96 )
  97. PLD( bge 4b )
  98. 5: ands ip, r2, #28
  99. rsb ip, ip, #32
  100. #if LDR1W_SHIFT > 0
  101. lsl ip, ip, #LDR1W_SHIFT
  102. #endif
  103. addne pc, pc, ip @ C is always clear here
  104. b 7f
  105. 6:
  106. .rept (1 << LDR1W_SHIFT)
  107. W(nop)
  108. .endr
  109. ldr1w r1, r3, abort=20f
  110. ldr1w r1, r4, abort=20f
  111. ldr1w r1, r5, abort=20f
  112. ldr1w r1, r6, abort=20f
  113. ldr1w r1, r7, abort=20f
  114. ldr1w r1, r8, abort=20f
  115. ldr1w r1, lr, abort=20f
  116. #if LDR1W_SHIFT < STR1W_SHIFT
  117. lsl ip, ip, #STR1W_SHIFT - LDR1W_SHIFT
  118. #elif LDR1W_SHIFT > STR1W_SHIFT
  119. lsr ip, ip, #LDR1W_SHIFT - STR1W_SHIFT
  120. #endif
  121. add pc, pc, ip
  122. nop
  123. .rept (1 << STR1W_SHIFT)
  124. W(nop)
  125. .endr
  126. str1w r0, r3, abort=20f
  127. str1w r0, r4, abort=20f
  128. str1w r0, r5, abort=20f
  129. str1w r0, r6, abort=20f
  130. str1w r0, r7, abort=20f
  131. str1w r0, r8, abort=20f
  132. str1w r0, lr, abort=20f
  133. CALGN( bcs 2b )
  134. 7: ldmfd sp!, {r5 - r8}
  135. 8: movs r2, r2, lsl #31
  136. ldr1b r1, r3, ne, abort=21f
  137. ldr1b r1, r4, cs, abort=21f
  138. ldr1b r1, ip, cs, abort=21f
  139. str1b r0, r3, ne, abort=21f
  140. str1b r0, r4, cs, abort=21f
  141. str1b r0, ip, cs, abort=21f
  142. exit r4, pc
  143. 9: rsb ip, ip, #4
  144. cmp ip, #2
  145. ldr1b r1, r3, gt, abort=21f
  146. ldr1b r1, r4, ge, abort=21f
  147. ldr1b r1, lr, abort=21f
  148. str1b r0, r3, gt, abort=21f
  149. str1b r0, r4, ge, abort=21f
  150. subs r2, r2, ip
  151. str1b r0, lr, abort=21f
  152. blt 8b
  153. ands ip, r1, #3
  154. beq 1b
  155. 10: bic r1, r1, #3
  156. cmp ip, #2
  157. ldr1w r1, lr, abort=21f
  158. beq 17f
  159. bgt 18f
  160. .macro forward_copy_shift pull push
  161. subs r2, r2, #28
  162. blt 14f
  163. CALGN( ands ip, r0, #31 )
  164. CALGN( rsb ip, ip, #32 )
  165. CALGN( sbcnes r4, ip, r2 ) @ C is always set here
  166. CALGN( subcc r2, r2, ip )
  167. CALGN( bcc 15f )
  168. 11: stmfd sp!, {r5 - r9}
  169. PLD( pld [r1, #0] )
  170. PLD( subs r2, r2, #96 )
  171. PLD( pld [r1, #28] )
  172. PLD( blt 13f )
  173. PLD( pld [r1, #60] )
  174. PLD( pld [r1, #92] )
  175. 12: PLD( pld [r1, #124] )
  176. 13: ldr4w r1, r4, r5, r6, r7, abort=19f
  177. mov r3, lr, pull #\pull
  178. subs r2, r2, #32
  179. ldr4w r1, r8, r9, ip, lr, abort=19f
  180. orr r3, r3, r4, push #\push
  181. mov r4, r4, pull #\pull
  182. orr r4, r4, r5, push #\push
  183. mov r5, r5, pull #\pull
  184. orr r5, r5, r6, push #\push
  185. mov r6, r6, pull #\pull
  186. orr r6, r6, r7, push #\push
  187. mov r7, r7, pull #\pull
  188. orr r7, r7, r8, push #\push
  189. mov r8, r8, pull #\pull
  190. orr r8, r8, r9, push #\push
  191. mov r9, r9, pull #\pull
  192. orr r9, r9, ip, push #\push
  193. mov ip, ip, pull #\pull
  194. orr ip, ip, lr, push #\push
  195. str8w r0, r3, r4, r5, r6, r7, r8, r9, ip, , abort=19f
  196. bge 12b
  197. PLD( cmn r2, #96 )
  198. PLD( bge 13b )
  199. ldmfd sp!, {r5 - r9}
  200. 14: ands ip, r2, #28
  201. beq 16f
  202. 15: mov r3, lr, pull #\pull
  203. ldr1w r1, lr, abort=21f
  204. subs ip, ip, #4
  205. orr r3, r3, lr, push #\push
  206. str1w r0, r3, abort=21f
  207. bgt 15b
  208. CALGN( cmp r2, #0 )
  209. CALGN( bge 11b )
  210. 16: sub r1, r1, #(\push / 8)
  211. b 8b
  212. .endm
  213. forward_copy_shift pull=8 push=24
  214. 17: forward_copy_shift pull=16 push=16
  215. 18: forward_copy_shift pull=24 push=8
  216. /*
  217. * Abort preamble and completion macros.
  218. * If a fixup handler is required then those macros must surround it.
  219. * It is assumed that the fixup code will handle the private part of
  220. * the exit macro.
  221. */
  222. .macro copy_abort_preamble
  223. 19: ldmfd sp!, {r5 - r9}
  224. b 21f
  225. 20: ldmfd sp!, {r5 - r8}
  226. 21:
  227. .endm
  228. .macro copy_abort_end
  229. ldmfd sp!, {r4, pc}
  230. .endm