2 * Copyright 2008 Vitaly Mayatskikh <vmayatsk@redhat.com>
3 * Copyright 2002 Andi Kleen, SuSE Labs.
4 * Subject to the GNU Public License v2.
6 * Functions to copy from and to user space.
9 #include <linux/linkage.h>
10 #include <asm/current.h>
11 #include <asm/asm-offsets.h>
12 #include <asm/thread_info.h>
13 #include <asm/cpufeature.h>
14 #include <asm/alternative-asm.h>
18 /* Standard copy_to_user with segment limit checking */
24 cmpq TI_addr_limit(%rax),%rcx
26 ALTERNATIVE_2 "jmp copy_user_generic_unrolled", \
27 "jmp copy_user_generic_string", \
28 X86_FEATURE_REP_GOOD, \
29 "jmp copy_user_enhanced_fast_string", \
31 ENDPROC(_copy_to_user)
33 /* Standard copy_from_user with segment limit checking */
34 ENTRY(_copy_from_user)
39 cmpq TI_addr_limit(%rax),%rcx
41 ALTERNATIVE_2 "jmp copy_user_generic_unrolled", \
42 "jmp copy_user_generic_string", \
43 X86_FEATURE_REP_GOOD, \
44 "jmp copy_user_enhanced_fast_string", \
46 ENDPROC(_copy_from_user)
59 ENDPROC(bad_from_user)
63 * copy_user_generic_unrolled - memory copy with exception handling.
64 * This version is for CPUs like P4 that don't have efficient micro
73 * eax uncopied bytes or 0 if successful.
75 ENTRY(copy_user_generic_unrolled)
78 jb 20f /* less then 8 bytes, go to byte copy loop */
86 3: movq 2*8(%rsi),%r10
87 4: movq 3*8(%rsi),%r11
90 7: movq %r10,2*8(%rdi)
91 8: movq %r11,3*8(%rdi)
93 10: movq 5*8(%rsi),%r9
94 11: movq 6*8(%rsi),%r10
95 12: movq 7*8(%rsi),%r11
96 13: movq %r8,4*8(%rdi)
97 14: movq %r9,5*8(%rdi)
98 15: movq %r10,6*8(%rdi)
99 16: movq %r11,7*8(%rdi)
131 40: leal (%rdx,%rcx,8),%edx
134 60: jmp copy_user_handle_tail /* ecx is zerorest also */
146 _ASM_EXTABLE(10b,30b)
147 _ASM_EXTABLE(11b,30b)
148 _ASM_EXTABLE(12b,30b)
149 _ASM_EXTABLE(13b,30b)
150 _ASM_EXTABLE(14b,30b)
151 _ASM_EXTABLE(15b,30b)
152 _ASM_EXTABLE(16b,30b)
153 _ASM_EXTABLE(18b,40b)
154 _ASM_EXTABLE(19b,40b)
155 _ASM_EXTABLE(21b,50b)
156 _ASM_EXTABLE(22b,50b)
157 ENDPROC(copy_user_generic_unrolled)
159 /* Some CPUs run faster using the string copy instructions.
160 * This is also a lot simpler. Use them when possible.
162 * Only 4GB of copy is supported. This shouldn't be a problem
163 * because the kernel normally only writes from/to page sized chunks
164 * even if user space passed a longer buffer.
165 * And more would be dangerous because both Intel and AMD have
166 * errata with rep movsq > 4GB. If someone feels the need to fix
167 * this please consider this.
175 * eax uncopied bytes or 0 if successful.
177 ENTRY(copy_user_generic_string)
180 jb 2f /* less than 8 bytes, go to byte copy loop */
195 11: leal (%rdx,%rcx,8),%ecx
196 12: movl %ecx,%edx /* ecx is zerorest also */
197 jmp copy_user_handle_tail
202 ENDPROC(copy_user_generic_string)
205 * Some CPUs are adding enhanced REP MOVSB/STOSB instructions.
206 * It's recommended to use enhanced REP MOVSB/STOSB if it's enabled.
214 * eax uncopied bytes or 0 if successful.
216 ENTRY(copy_user_enhanced_fast_string)
226 12: movl %ecx,%edx /* ecx is zerorest also */
227 jmp copy_user_handle_tail
231 ENDPROC(copy_user_enhanced_fast_string)
234 * copy_user_nocache - Uncached memory copy with exception handling
235 * This will force destination/source out of cache for more performance.
237 ENTRY(__copy_user_nocache)
240 jb 20f /* less then 8 bytes, go to byte copy loop */
247 2: movq 1*8(%rsi),%r9
248 3: movq 2*8(%rsi),%r10
249 4: movq 3*8(%rsi),%r11
251 6: movnti %r9,1*8(%rdi)
252 7: movnti %r10,2*8(%rdi)
253 8: movnti %r11,3*8(%rdi)
254 9: movq 4*8(%rsi),%r8
255 10: movq 5*8(%rsi),%r9
256 11: movq 6*8(%rsi),%r10
257 12: movq 7*8(%rsi),%r11
258 13: movnti %r8,4*8(%rdi)
259 14: movnti %r9,5*8(%rdi)
260 15: movnti %r10,6*8(%rdi)
261 16: movnti %r11,7*8(%rdi)
271 19: movnti %r8,(%rdi)
294 40: lea (%rdx,%rcx,8),%rdx
298 jmp copy_user_handle_tail
310 _ASM_EXTABLE(10b,30b)
311 _ASM_EXTABLE(11b,30b)
312 _ASM_EXTABLE(12b,30b)
313 _ASM_EXTABLE(13b,30b)
314 _ASM_EXTABLE(14b,30b)
315 _ASM_EXTABLE(15b,30b)
316 _ASM_EXTABLE(16b,30b)
317 _ASM_EXTABLE(18b,40b)
318 _ASM_EXTABLE(19b,40b)
319 _ASM_EXTABLE(21b,50b)
320 _ASM_EXTABLE(22b,50b)
321 ENDPROC(__copy_user_nocache)