memcpy_64.S 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* Copyright 2002 Andi Kleen */
  2. #include <linux/linkage.h>
  3. #include <asm/errno.h>
  4. #include <asm/cpufeatures.h>
  5. #include <asm/alternative-asm.h>
  6. /*
  7. * We build a jump to memcpy_orig by default which gets NOPped out on
  8. * the majority of x86 CPUs which set REP_GOOD. In addition, CPUs which
  9. * have the enhanced REP MOVSB/STOSB feature (ERMS), change those NOPs
  10. * to a jmp to memcpy_erms which does the REP; MOVSB mem copy.
  11. */
  12. .weak memcpy
  13. /*
  14. * memcpy - Copy a memory block.
  15. *
  16. * Input:
  17. * rdi destination
  18. * rsi source
  19. * rdx count
  20. *
  21. * Output:
  22. * rax original destination
  23. */
  24. ENTRY(__memcpy)
  25. ENTRY(memcpy)
  26. ALTERNATIVE_2 "jmp memcpy_orig", "", X86_FEATURE_REP_GOOD, \
  27. "jmp memcpy_erms", X86_FEATURE_ERMS
  28. movq %rdi, %rax
  29. movq %rdx, %rcx
  30. shrq $3, %rcx
  31. andl $7, %edx
  32. rep movsq
  33. movl %edx, %ecx
  34. rep movsb
  35. ret
  36. ENDPROC(memcpy)
  37. ENDPROC(__memcpy)
  38. /*
  39. * memcpy_erms() - enhanced fast string memcpy. This is faster and
  40. * simpler than memcpy. Use memcpy_erms when possible.
  41. */
  42. ENTRY(memcpy_erms)
  43. movq %rdi, %rax
  44. movq %rdx, %rcx
  45. rep movsb
  46. ret
  47. ENDPROC(memcpy_erms)
  48. ENTRY(memcpy_orig)
  49. movq %rdi, %rax
  50. cmpq $0x20, %rdx
  51. jb .Lhandle_tail
  52. /*
  53. * We check whether memory false dependence could occur,
  54. * then jump to corresponding copy mode.
  55. */
  56. cmp %dil, %sil
  57. jl .Lcopy_backward
  58. subq $0x20, %rdx
  59. .Lcopy_forward_loop:
  60. subq $0x20, %rdx
  61. /*
  62. * Move in blocks of 4x8 bytes:
  63. */
  64. movq 0*8(%rsi), %r8
  65. movq 1*8(%rsi), %r9
  66. movq 2*8(%rsi), %r10
  67. movq 3*8(%rsi), %r11
  68. leaq 4*8(%rsi), %rsi
  69. movq %r8, 0*8(%rdi)
  70. movq %r9, 1*8(%rdi)
  71. movq %r10, 2*8(%rdi)
  72. movq %r11, 3*8(%rdi)
  73. leaq 4*8(%rdi), %rdi
  74. jae .Lcopy_forward_loop
  75. addl $0x20, %edx
  76. jmp .Lhandle_tail
  77. .Lcopy_backward:
  78. /*
  79. * Calculate copy position to tail.
  80. */
  81. addq %rdx, %rsi
  82. addq %rdx, %rdi
  83. subq $0x20, %rdx
  84. /*
  85. * At most 3 ALU operations in one cycle,
  86. * so append NOPS in the same 16 bytes trunk.
  87. */
  88. .p2align 4
  89. .Lcopy_backward_loop:
  90. subq $0x20, %rdx
  91. movq -1*8(%rsi), %r8
  92. movq -2*8(%rsi), %r9
  93. movq -3*8(%rsi), %r10
  94. movq -4*8(%rsi), %r11
  95. leaq -4*8(%rsi), %rsi
  96. movq %r8, -1*8(%rdi)
  97. movq %r9, -2*8(%rdi)
  98. movq %r10, -3*8(%rdi)
  99. movq %r11, -4*8(%rdi)
  100. leaq -4*8(%rdi), %rdi
  101. jae .Lcopy_backward_loop
  102. /*
  103. * Calculate copy position to head.
  104. */
  105. addl $0x20, %edx
  106. subq %rdx, %rsi
  107. subq %rdx, %rdi
  108. .Lhandle_tail:
  109. cmpl $16, %edx
  110. jb .Lless_16bytes
  111. /*
  112. * Move data from 16 bytes to 31 bytes.
  113. */
  114. movq 0*8(%rsi), %r8
  115. movq 1*8(%rsi), %r9
  116. movq -2*8(%rsi, %rdx), %r10
  117. movq -1*8(%rsi, %rdx), %r11
  118. movq %r8, 0*8(%rdi)
  119. movq %r9, 1*8(%rdi)
  120. movq %r10, -2*8(%rdi, %rdx)
  121. movq %r11, -1*8(%rdi, %rdx)
  122. retq
  123. .p2align 4
  124. .Lless_16bytes:
  125. cmpl $8, %edx
  126. jb .Lless_8bytes
  127. /*
  128. * Move data from 8 bytes to 15 bytes.
  129. */
  130. movq 0*8(%rsi), %r8
  131. movq -1*8(%rsi, %rdx), %r9
  132. movq %r8, 0*8(%rdi)
  133. movq %r9, -1*8(%rdi, %rdx)
  134. retq
  135. .p2align 4
  136. .Lless_8bytes:
  137. cmpl $4, %edx
  138. jb .Lless_3bytes
  139. /*
  140. * Move data from 4 bytes to 7 bytes.
  141. */
  142. movl (%rsi), %ecx
  143. movl -4(%rsi, %rdx), %r8d
  144. movl %ecx, (%rdi)
  145. movl %r8d, -4(%rdi, %rdx)
  146. retq
  147. .p2align 4
  148. .Lless_3bytes:
  149. subl $1, %edx
  150. jb .Lend
  151. /*
  152. * Move data from 1 bytes to 3 bytes.
  153. */
  154. movzbl (%rsi), %ecx
  155. jz .Lstore_1byte
  156. movzbq 1(%rsi), %r8
  157. movzbq (%rsi, %rdx), %r9
  158. movb %r8b, 1(%rdi)
  159. movb %r9b, (%rdi, %rdx)
  160. .Lstore_1byte:
  161. movb %cl, (%rdi)
  162. .Lend:
  163. retq
  164. ENDPROC(memcpy_orig)
  165. #ifndef CONFIG_UML
  166. /*
  167. * memcpy_mcsafe_unrolled - memory copy with machine check exception handling
  168. * Note that we only catch machine checks when reading the source addresses.
  169. * Writes to target are posted and don't generate machine checks.
  170. */
  171. ENTRY(memcpy_mcsafe_unrolled)
  172. cmpl $8, %edx
  173. /* Less than 8 bytes? Go to byte copy loop */
  174. jb .L_no_whole_words
  175. /* Check for bad alignment of source */
  176. testl $7, %esi
  177. /* Already aligned */
  178. jz .L_8byte_aligned
  179. /* Copy one byte at a time until source is 8-byte aligned */
  180. movl %esi, %ecx
  181. andl $7, %ecx
  182. subl $8, %ecx
  183. negl %ecx
  184. subl %ecx, %edx
  185. .L_copy_leading_bytes:
  186. movb (%rsi), %al
  187. movb %al, (%rdi)
  188. incq %rsi
  189. incq %rdi
  190. decl %ecx
  191. jnz .L_copy_leading_bytes
  192. .L_8byte_aligned:
  193. /* Figure out how many whole cache lines (64-bytes) to copy */
  194. movl %edx, %ecx
  195. andl $63, %edx
  196. shrl $6, %ecx
  197. jz .L_no_whole_cache_lines
  198. /* Loop copying whole cache lines */
  199. .L_cache_w0: movq (%rsi), %r8
  200. .L_cache_w1: movq 1*8(%rsi), %r9
  201. .L_cache_w2: movq 2*8(%rsi), %r10
  202. .L_cache_w3: movq 3*8(%rsi), %r11
  203. movq %r8, (%rdi)
  204. movq %r9, 1*8(%rdi)
  205. movq %r10, 2*8(%rdi)
  206. movq %r11, 3*8(%rdi)
  207. .L_cache_w4: movq 4*8(%rsi), %r8
  208. .L_cache_w5: movq 5*8(%rsi), %r9
  209. .L_cache_w6: movq 6*8(%rsi), %r10
  210. .L_cache_w7: movq 7*8(%rsi), %r11
  211. movq %r8, 4*8(%rdi)
  212. movq %r9, 5*8(%rdi)
  213. movq %r10, 6*8(%rdi)
  214. movq %r11, 7*8(%rdi)
  215. leaq 64(%rsi), %rsi
  216. leaq 64(%rdi), %rdi
  217. decl %ecx
  218. jnz .L_cache_w0
  219. /* Are there any trailing 8-byte words? */
  220. .L_no_whole_cache_lines:
  221. movl %edx, %ecx
  222. andl $7, %edx
  223. shrl $3, %ecx
  224. jz .L_no_whole_words
  225. /* Copy trailing words */
  226. .L_copy_trailing_words:
  227. movq (%rsi), %r8
  228. mov %r8, (%rdi)
  229. leaq 8(%rsi), %rsi
  230. leaq 8(%rdi), %rdi
  231. decl %ecx
  232. jnz .L_copy_trailing_words
  233. /* Any trailing bytes? */
  234. .L_no_whole_words:
  235. andl %edx, %edx
  236. jz .L_done_memcpy_trap
  237. /* Copy trailing bytes */
  238. movl %edx, %ecx
  239. .L_copy_trailing_bytes:
  240. movb (%rsi), %al
  241. movb %al, (%rdi)
  242. incq %rsi
  243. incq %rdi
  244. decl %ecx
  245. jnz .L_copy_trailing_bytes
  246. /* Copy successful. Return zero */
  247. .L_done_memcpy_trap:
  248. xorq %rax, %rax
  249. ret
  250. ENDPROC(memcpy_mcsafe_unrolled)
  251. .section .fixup, "ax"
  252. /* Return -EFAULT for any failure */
  253. .L_memcpy_mcsafe_fail:
  254. mov $-EFAULT, %rax
  255. ret
  256. .previous
  257. _ASM_EXTABLE_FAULT(.L_copy_leading_bytes, .L_memcpy_mcsafe_fail)
  258. _ASM_EXTABLE_FAULT(.L_cache_w0, .L_memcpy_mcsafe_fail)
  259. _ASM_EXTABLE_FAULT(.L_cache_w1, .L_memcpy_mcsafe_fail)
  260. _ASM_EXTABLE_FAULT(.L_cache_w3, .L_memcpy_mcsafe_fail)
  261. _ASM_EXTABLE_FAULT(.L_cache_w3, .L_memcpy_mcsafe_fail)
  262. _ASM_EXTABLE_FAULT(.L_cache_w4, .L_memcpy_mcsafe_fail)
  263. _ASM_EXTABLE_FAULT(.L_cache_w5, .L_memcpy_mcsafe_fail)
  264. _ASM_EXTABLE_FAULT(.L_cache_w6, .L_memcpy_mcsafe_fail)
  265. _ASM_EXTABLE_FAULT(.L_cache_w7, .L_memcpy_mcsafe_fail)
  266. _ASM_EXTABLE_FAULT(.L_copy_trailing_words, .L_memcpy_mcsafe_fail)
  267. _ASM_EXTABLE_FAULT(.L_copy_trailing_bytes, .L_memcpy_mcsafe_fail)
  268. #endif