memcpy_64.S 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* Copyright 2002 Andi Kleen */
  2. #include <linux/linkage.h>
  3. #include <asm/cpufeature.h>
  4. #include <asm/dwarf2.h>
  5. #include <asm/alternative-asm.h>
  6. /*
  7. * memcpy - Copy a memory block.
  8. *
  9. * Input:
  10. * rdi destination
  11. * rsi source
  12. * rdx count
  13. *
  14. * Output:
  15. * rax original destination
  16. */
  17. /*
  18. * memcpy_c() - fast string ops (REP MOVSQ) based variant.
  19. *
  20. * This gets patched over the unrolled variant (below) via the
  21. * alternative instructions framework:
  22. */
  23. .section .altinstr_replacement, "ax", @progbits
  24. .Lmemcpy_c:
  25. movq %rdi, %rax
  26. movl %edx, %ecx
  27. shrl $3, %ecx
  28. andl $7, %edx
  29. rep movsq
  30. movl %edx, %ecx
  31. rep movsb
  32. ret
  33. .Lmemcpy_e:
  34. .previous
  35. /*
  36. * memcpy_c_e() - enhanced fast string memcpy. This is faster and simpler than
  37. * memcpy_c. Use memcpy_c_e when possible.
  38. *
  39. * This gets patched over the unrolled variant (below) via the
  40. * alternative instructions framework:
  41. */
  42. .section .altinstr_replacement, "ax", @progbits
  43. .Lmemcpy_c_e:
  44. movq %rdi, %rax
  45. movl %edx, %ecx
  46. rep movsb
  47. ret
  48. .Lmemcpy_e_e:
  49. .previous
  50. ENTRY(__memcpy)
  51. ENTRY(memcpy)
  52. CFI_STARTPROC
  53. movq %rdi, %rax
  54. /*
  55. * Use 32bit CMP here to avoid long NOP padding.
  56. */
  57. cmp $0x20, %edx
  58. jb .Lhandle_tail
  59. /*
  60. * We check whether memory false dependence could occur,
  61. * then jump to corresponding copy mode.
  62. */
  63. cmp %dil, %sil
  64. jl .Lcopy_backward
  65. subl $0x20, %edx
  66. .Lcopy_forward_loop:
  67. subq $0x20, %rdx
  68. /*
  69. * Move in blocks of 4x8 bytes:
  70. */
  71. movq 0*8(%rsi), %r8
  72. movq 1*8(%rsi), %r9
  73. movq 2*8(%rsi), %r10
  74. movq 3*8(%rsi), %r11
  75. leaq 4*8(%rsi), %rsi
  76. movq %r8, 0*8(%rdi)
  77. movq %r9, 1*8(%rdi)
  78. movq %r10, 2*8(%rdi)
  79. movq %r11, 3*8(%rdi)
  80. leaq 4*8(%rdi), %rdi
  81. jae .Lcopy_forward_loop
  82. addq $0x20, %rdx
  83. jmp .Lhandle_tail
  84. .Lcopy_backward:
  85. /*
  86. * Calculate copy position to tail.
  87. */
  88. addq %rdx, %rsi
  89. addq %rdx, %rdi
  90. subq $0x20, %rdx
  91. /*
  92. * At most 3 ALU operations in one cycle,
  93. * so append NOPS in the same 16bytes trunk.
  94. */
  95. .p2align 4
  96. .Lcopy_backward_loop:
  97. subq $0x20, %rdx
  98. movq -1*8(%rsi), %r8
  99. movq -2*8(%rsi), %r9
  100. movq -3*8(%rsi), %r10
  101. movq -4*8(%rsi), %r11
  102. leaq -4*8(%rsi), %rsi
  103. movq %r8, -1*8(%rdi)
  104. movq %r9, -2*8(%rdi)
  105. movq %r10, -3*8(%rdi)
  106. movq %r11, -4*8(%rdi)
  107. leaq -4*8(%rdi), %rdi
  108. jae .Lcopy_backward_loop
  109. /*
  110. * Calculate copy position to head.
  111. */
  112. addq $0x20, %rdx
  113. subq %rdx, %rsi
  114. subq %rdx, %rdi
  115. .Lhandle_tail:
  116. cmpq $16, %rdx
  117. jb .Lless_16bytes
  118. /*
  119. * Move data from 16 bytes to 31 bytes.
  120. */
  121. movq 0*8(%rsi), %r8
  122. movq 1*8(%rsi), %r9
  123. movq -2*8(%rsi, %rdx), %r10
  124. movq -1*8(%rsi, %rdx), %r11
  125. movq %r8, 0*8(%rdi)
  126. movq %r9, 1*8(%rdi)
  127. movq %r10, -2*8(%rdi, %rdx)
  128. movq %r11, -1*8(%rdi, %rdx)
  129. retq
  130. .p2align 4
  131. .Lless_16bytes:
  132. cmpq $8, %rdx
  133. jb .Lless_8bytes
  134. /*
  135. * Move data from 8 bytes to 15 bytes.
  136. */
  137. movq 0*8(%rsi), %r8
  138. movq -1*8(%rsi, %rdx), %r9
  139. movq %r8, 0*8(%rdi)
  140. movq %r9, -1*8(%rdi, %rdx)
  141. retq
  142. .p2align 4
  143. .Lless_8bytes:
  144. cmpq $4, %rdx
  145. jb .Lless_3bytes
  146. /*
  147. * Move data from 4 bytes to 7 bytes.
  148. */
  149. movl (%rsi), %ecx
  150. movl -4(%rsi, %rdx), %r8d
  151. movl %ecx, (%rdi)
  152. movl %r8d, -4(%rdi, %rdx)
  153. retq
  154. .p2align 4
  155. .Lless_3bytes:
  156. cmpl $0, %edx
  157. je .Lend
  158. /*
  159. * Move data from 1 bytes to 3 bytes.
  160. */
  161. .Lloop_1:
  162. movb (%rsi), %r8b
  163. movb %r8b, (%rdi)
  164. incq %rdi
  165. incq %rsi
  166. decl %edx
  167. jnz .Lloop_1
  168. .Lend:
  169. retq
  170. CFI_ENDPROC
  171. ENDPROC(memcpy)
  172. ENDPROC(__memcpy)
  173. /*
  174. * Some CPUs are adding enhanced REP MOVSB/STOSB feature
  175. * If the feature is supported, memcpy_c_e() is the first choice.
  176. * If enhanced rep movsb copy is not available, use fast string copy
  177. * memcpy_c() when possible. This is faster and code is simpler than
  178. * original memcpy().
  179. * Otherwise, original memcpy() is used.
  180. * In .altinstructions section, ERMS feature is placed after REG_GOOD
  181. * feature to implement the right patch order.
  182. *
  183. * Replace only beginning, memcpy is used to apply alternatives,
  184. * so it is silly to overwrite itself with nops - reboot is the
  185. * only outcome...
  186. */
  187. .section .altinstructions, "a"
  188. altinstruction_entry memcpy,.Lmemcpy_c,X86_FEATURE_REP_GOOD,\
  189. .Lmemcpy_e-.Lmemcpy_c,.Lmemcpy_e-.Lmemcpy_c
  190. altinstruction_entry memcpy,.Lmemcpy_c_e,X86_FEATURE_ERMS, \
  191. .Lmemcpy_e_e-.Lmemcpy_c_e,.Lmemcpy_e_e-.Lmemcpy_c_e
  192. .previous