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/dwarf2.h>
12 #define FIX_ALIGNMENT 1
14 #include <asm/current.h>
15 #include <asm/asm-offsets.h>
16 #include <asm/thread_info.h>
17 #include <asm/cpufeature.h>
18 #include <asm/alternative-asm.h>
21 * By placing feature2 after feature1 in altinstructions section, we logically
23 * If CPU has feature2, jmp to alt2 is used
24 * else if CPU has feature1, jmp to alt1 is used
25 * else jmp to orig is used.
27 .macro ALTERNATIVE_JUMP feature1,feature2,orig,alt1,alt2
29 .byte 0xe9 /* 32bit jump */
30 .long \orig-1f /* by default jump to orig */
32 .section .altinstr_replacement,"ax"
33 2: .byte 0xe9 /* near jump with 32bit immediate */
34 .long \alt1-1b /* offset */ /* or alternatively to alt1 */
35 3: .byte 0xe9 /* near jump with 32bit immediate */
36 .long \alt2-1b /* offset */ /* or alternatively to alt2 */
39 .section .altinstructions,"a"
40 altinstruction_entry 0b,2b,\feature1,5,5
41 altinstruction_entry 0b,3b,\feature2,5,5
45 .macro ALIGN_DESTINATION
47 /* check for bad alignment of destination */
50 jz 102f /* already aligned */
62 103: addl %ecx,%edx /* ecx is zerorest also */
63 jmp copy_user_handle_tail
66 .section __ex_table,"a"
74 /* Standard copy_to_user with segment limit checking */
81 cmpq TI_addr_limit(%rax),%rcx
83 ALTERNATIVE_JUMP X86_FEATURE_REP_GOOD,X86_FEATURE_ERMS, \
84 copy_user_generic_unrolled,copy_user_generic_string, \
85 copy_user_enhanced_fast_string
87 ENDPROC(_copy_to_user)
89 /* Standard copy_from_user with segment limit checking */
90 ENTRY(_copy_from_user)
96 cmpq TI_addr_limit(%rax),%rcx
98 ALTERNATIVE_JUMP X86_FEATURE_REP_GOOD,X86_FEATURE_ERMS, \
99 copy_user_generic_unrolled,copy_user_generic_string, \
100 copy_user_enhanced_fast_string
102 ENDPROC(_copy_from_user)
117 ENDPROC(bad_from_user)
121 * copy_user_generic_unrolled - memory copy with exception handling.
122 * This version is for CPUs like P4 that don't have efficient micro
131 * eax uncopied bytes or 0 if successful.
133 ENTRY(copy_user_generic_unrolled)
136 jb 20f /* less then 8 bytes, go to byte copy loop */
143 2: movq 1*8(%rsi),%r9
144 3: movq 2*8(%rsi),%r10
145 4: movq 3*8(%rsi),%r11
147 6: movq %r9,1*8(%rdi)
148 7: movq %r10,2*8(%rdi)
149 8: movq %r11,3*8(%rdi)
150 9: movq 4*8(%rsi),%r8
151 10: movq 5*8(%rsi),%r9
152 11: movq 6*8(%rsi),%r10
153 12: movq 7*8(%rsi),%r11
154 13: movq %r8,4*8(%rdi)
155 14: movq %r9,5*8(%rdi)
156 15: movq %r10,6*8(%rdi)
157 16: movq %r11,7*8(%rdi)
188 40: lea (%rdx,%rcx,8),%rdx
191 60: jmp copy_user_handle_tail /* ecx is zerorest also */
194 .section __ex_table,"a"
218 ENDPROC(copy_user_generic_unrolled)
220 /* Some CPUs run faster using the string copy instructions.
221 * This is also a lot simpler. Use them when possible.
223 * Only 4GB of copy is supported. This shouldn't be a problem
224 * because the kernel normally only writes from/to page sized chunks
225 * even if user space passed a longer buffer.
226 * And more would be dangerous because both Intel and AMD have
227 * errata with rep movsq > 4GB. If someone feels the need to fix
228 * this please consider this.
236 * eax uncopied bytes or 0 if successful.
238 ENTRY(copy_user_generic_string)
243 jb 2f /* less than 8 bytes, go to byte copy loop */
257 11: lea (%rdx,%rcx,8),%rcx
258 12: movl %ecx,%edx /* ecx is zerorest also */
259 jmp copy_user_handle_tail
262 .section __ex_table,"a"
268 ENDPROC(copy_user_generic_string)
271 * Some CPUs are adding enhanced REP MOVSB/STOSB instructions.
272 * It's recommended to use enhanced REP MOVSB/STOSB if it's enabled.
280 * eax uncopied bytes or 0 if successful.
282 ENTRY(copy_user_enhanced_fast_string)
293 12: movl %ecx,%edx /* ecx is zerorest also */
294 jmp copy_user_handle_tail
297 .section __ex_table,"a"
302 ENDPROC(copy_user_enhanced_fast_string)