blob: efbf2a0ecdea85a41566f01d05d14e14177e7145 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Copyright 2002 Andi Kleen */
Dave Jones038b0a62006-10-04 03:38:54 -04002
Jan Beulich8d379da2006-09-26 10:52:32 +02003#include <linux/linkage.h>
Ingo Molnarf3b6eaf2009-03-12 12:20:17 +01004
Jan Beulich8d379da2006-09-26 10:52:32 +02005#include <asm/cpufeature.h>
Ingo Molnarf3b6eaf2009-03-12 12:20:17 +01006#include <asm/dwarf2.h>
Fenghua Yu101068c2011-05-17 15:29:16 -07007#include <asm/alternative-asm.h>
Jan Beulich8d379da2006-09-26 10:52:32 +02008
Linus Torvalds1da177e2005-04-16 15:20:36 -07009/*
10 * memcpy - Copy a memory block.
11 *
Ingo Molnarf3b6eaf2009-03-12 12:20:17 +010012 * Input:
13 * rdi destination
14 * rsi source
15 * rdx count
16 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * Output:
18 * rax original destination
Ingo Molnarf3b6eaf2009-03-12 12:20:17 +010019 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Ingo Molnarf3b6eaf2009-03-12 12:20:17 +010021/*
22 * memcpy_c() - fast string ops (REP MOVSQ) based variant.
23 *
Jan Beulich7269e882009-12-18 16:16:03 +000024 * This gets patched over the unrolled variant (below) via the
Ingo Molnarf3b6eaf2009-03-12 12:20:17 +010025 * alternative instructions framework:
26 */
Jan Beulich7269e882009-12-18 16:16:03 +000027 .section .altinstr_replacement, "ax", @progbits
28.Lmemcpy_c:
Ingo Molnarf3b6eaf2009-03-12 12:20:17 +010029 movq %rdi, %rax
30
31 movl %edx, %ecx
32 shrl $3, %ecx
33 andl $7, %edx
Jan Beulich8d379da2006-09-26 10:52:32 +020034 rep movsq
Ingo Molnarf3b6eaf2009-03-12 12:20:17 +010035 movl %edx, %ecx
Jan Beulich8d379da2006-09-26 10:52:32 +020036 rep movsb
37 ret
Jan Beulich7269e882009-12-18 16:16:03 +000038.Lmemcpy_e:
39 .previous
Jan Beulich8d379da2006-09-26 10:52:32 +020040
Fenghua Yu101068c2011-05-17 15:29:16 -070041/*
42 * memcpy_c_e() - enhanced fast string memcpy. This is faster and simpler than
43 * memcpy_c. Use memcpy_c_e when possible.
44 *
45 * This gets patched over the unrolled variant (below) via the
46 * alternative instructions framework:
47 */
48 .section .altinstr_replacement, "ax", @progbits
49.Lmemcpy_c_e:
50 movq %rdi, %rax
51
52 movl %edx, %ecx
53 rep movsb
54 ret
55.Lmemcpy_e_e:
56 .previous
57
Jan Beulich8d379da2006-09-26 10:52:32 +020058ENTRY(__memcpy)
59ENTRY(memcpy)
60 CFI_STARTPROC
Ingo Molnarf3b6eaf2009-03-12 12:20:17 +010061 movq %rdi, %rax
Andi Kleen7bcd3f32006-02-03 21:51:02 +010062
Ma Ling59daa702010-06-29 03:24:25 +080063 /*
64 * Use 32bit CMP here to avoid long NOP padding.
65 */
66 cmp $0x20, %edx
67 jb .Lhandle_tail
68
69 /*
Bart Van Assche9de49662011-05-01 14:09:21 +020070 * We check whether memory false dependence could occur,
Ma Ling59daa702010-06-29 03:24:25 +080071 * then jump to corresponding copy mode.
72 */
73 cmp %dil, %sil
74 jl .Lcopy_backward
75 subl $0x20, %edx
76.Lcopy_forward_loop:
77 subq $0x20, %rdx
78
79 /*
80 * Move in blocks of 4x8 bytes:
81 */
82 movq 0*8(%rsi), %r8
83 movq 1*8(%rsi), %r9
84 movq 2*8(%rsi), %r10
85 movq 3*8(%rsi), %r11
86 leaq 4*8(%rsi), %rsi
87
88 movq %r8, 0*8(%rdi)
89 movq %r9, 1*8(%rdi)
90 movq %r10, 2*8(%rdi)
91 movq %r11, 3*8(%rdi)
92 leaq 4*8(%rdi), %rdi
93 jae .Lcopy_forward_loop
94 addq $0x20, %rdx
95 jmp .Lhandle_tail
96
97.Lcopy_backward:
98 /*
99 * Calculate copy position to tail.
100 */
101 addq %rdx, %rsi
102 addq %rdx, %rdi
103 subq $0x20, %rdx
104 /*
105 * At most 3 ALU operations in one cycle,
106 * so append NOPS in the same 16bytes trunk.
107 */
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100108 .p2align 4
Ma Ling59daa702010-06-29 03:24:25 +0800109.Lcopy_backward_loop:
110 subq $0x20, %rdx
111 movq -1*8(%rsi), %r8
112 movq -2*8(%rsi), %r9
113 movq -3*8(%rsi), %r10
114 movq -4*8(%rsi), %r11
115 leaq -4*8(%rsi), %rsi
116 movq %r8, -1*8(%rdi)
117 movq %r9, -2*8(%rdi)
118 movq %r10, -3*8(%rdi)
119 movq %r11, -4*8(%rdi)
120 leaq -4*8(%rdi), %rdi
121 jae .Lcopy_backward_loop
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100122
Ingo Molnarf3b6eaf2009-03-12 12:20:17 +0100123 /*
Ma Ling59daa702010-06-29 03:24:25 +0800124 * Calculate copy position to head.
Ingo Molnarf3b6eaf2009-03-12 12:20:17 +0100125 */
Ma Ling59daa702010-06-29 03:24:25 +0800126 addq $0x20, %rdx
127 subq %rdx, %rsi
128 subq %rdx, %rdi
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100129.Lhandle_tail:
Ma Ling59daa702010-06-29 03:24:25 +0800130 cmpq $16, %rdx
131 jb .Lless_16bytes
Ingo Molnarf3b6eaf2009-03-12 12:20:17 +0100132
Ma Ling59daa702010-06-29 03:24:25 +0800133 /*
134 * Move data from 16 bytes to 31 bytes.
135 */
136 movq 0*8(%rsi), %r8
137 movq 1*8(%rsi), %r9
138 movq -2*8(%rsi, %rdx), %r10
139 movq -1*8(%rsi, %rdx), %r11
140 movq %r8, 0*8(%rdi)
141 movq %r9, 1*8(%rdi)
142 movq %r10, -2*8(%rdi, %rdx)
143 movq %r11, -1*8(%rdi, %rdx)
144 retq
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100145 .p2align 4
Ma Ling59daa702010-06-29 03:24:25 +0800146.Lless_16bytes:
147 cmpq $8, %rdx
148 jb .Lless_8bytes
149 /*
150 * Move data from 8 bytes to 15 bytes.
151 */
152 movq 0*8(%rsi), %r8
153 movq -1*8(%rsi, %rdx), %r9
154 movq %r8, 0*8(%rdi)
155 movq %r9, -1*8(%rdi, %rdx)
156 retq
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100157 .p2align 4
Ma Ling59daa702010-06-29 03:24:25 +0800158.Lless_8bytes:
159 cmpq $4, %rdx
160 jb .Lless_3bytes
161
162 /*
163 * Move data from 4 bytes to 7 bytes.
164 */
165 movl (%rsi), %ecx
166 movl -4(%rsi, %rdx), %r8d
167 movl %ecx, (%rdi)
168 movl %r8d, -4(%rdi, %rdx)
169 retq
170 .p2align 4
171.Lless_3bytes:
172 cmpl $0, %edx
173 je .Lend
174 /*
175 * Move data from 1 bytes to 3 bytes.
176 */
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100177.Lloop_1:
Ingo Molnarf3b6eaf2009-03-12 12:20:17 +0100178 movb (%rsi), %r8b
179 movb %r8b, (%rdi)
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100180 incq %rdi
181 incq %rsi
Ma Ling59daa702010-06-29 03:24:25 +0800182 decl %edx
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100183 jnz .Lloop_1
184
Ingo Molnarf3b6eaf2009-03-12 12:20:17 +0100185.Lend:
Ma Ling59daa702010-06-29 03:24:25 +0800186 retq
Jan Beulich8d379da2006-09-26 10:52:32 +0200187 CFI_ENDPROC
188ENDPROC(memcpy)
189ENDPROC(__memcpy)
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100190
Ingo Molnarf3b6eaf2009-03-12 12:20:17 +0100191 /*
Fenghua Yu101068c2011-05-17 15:29:16 -0700192 * Some CPUs are adding enhanced REP MOVSB/STOSB feature
193 * If the feature is supported, memcpy_c_e() is the first choice.
194 * If enhanced rep movsb copy is not available, use fast string copy
195 * memcpy_c() when possible. This is faster and code is simpler than
196 * original memcpy().
197 * Otherwise, original memcpy() is used.
198 * In .altinstructions section, ERMS feature is placed after REG_GOOD
199 * feature to implement the right patch order.
200 *
Ingo Molnarf3b6eaf2009-03-12 12:20:17 +0100201 * Replace only beginning, memcpy is used to apply alternatives,
202 * so it is silly to overwrite itself with nops - reboot is the
203 * only outcome...
204 */
Fenghua Yu101068c2011-05-17 15:29:16 -0700205 .section .altinstructions, "a"
206 altinstruction_entry memcpy,.Lmemcpy_c,X86_FEATURE_REP_GOOD,\
207 .Lmemcpy_e-.Lmemcpy_c,.Lmemcpy_e-.Lmemcpy_c
208 altinstruction_entry memcpy,.Lmemcpy_c_e,X86_FEATURE_ERMS, \
209 .Lmemcpy_e_e-.Lmemcpy_c_e,.Lmemcpy_e_e-.Lmemcpy_c_e
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100210 .previous