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