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>
22 * By placing feature2 after feature1 in altinstructions section, we logically
24 * If CPU has feature2, jmp to alt2 is used
25 * else if CPU has feature1, jmp to alt1 is used
26 * else jmp to orig is used.
28 .macro ALTERNATIVE_JUMP feature1,feature2,orig,alt1,alt2
30 .byte 0xe9 /* 32bit jump */
31 .long \orig-1f /* by default jump to orig */
33 .section .altinstr_replacement,"ax"
34 2: .byte 0xe9 /* near jump with 32bit immediate */
35 .long \alt1-1b /* offset */ /* or alternatively to alt1 */
36 3: .byte 0xe9 /* near jump with 32bit immediate */
37 .long \alt2-1b /* offset */ /* or alternatively to alt2 */
40 .section .altinstructions,"a"
41 altinstruction_entry 0b,2b,\feature1,5,5
42 altinstruction_entry 0b,3b,\feature2,5,5
46 .macro ALIGN_DESTINATION
48 /* check for bad alignment of destination */
51 jz 102f /* already aligned */
63 103: addl %ecx,%edx /* ecx is zerorest also */
64 jmp copy_user_handle_tail
67 _ASM_EXTABLE(100b,103b)
68 _ASM_EXTABLE(101b,103b)
72 /* Standard copy_to_user with segment limit checking */
79 cmpq TI_addr_limit(%rax),%rcx
81 ALTERNATIVE_JUMP X86_FEATURE_REP_GOOD,X86_FEATURE_ERMS, \
82 copy_user_generic_unrolled,copy_user_generic_string, \
83 copy_user_enhanced_fast_string
85 ENDPROC(_copy_to_user)
87 /* Standard copy_from_user with segment limit checking */
88 ENTRY(_copy_from_user)
94 cmpq TI_addr_limit(%rax),%rcx
96 ALTERNATIVE_JUMP X86_FEATURE_REP_GOOD,X86_FEATURE_ERMS, \
97 copy_user_generic_unrolled,copy_user_generic_string, \
98 copy_user_enhanced_fast_string
100 ENDPROC(_copy_from_user)
115 ENDPROC(bad_from_user)
119 * copy_user_generic_unrolled - memory copy with exception handling.
120 * This version is for CPUs like P4 that don't have efficient micro
129 * eax uncopied bytes or 0 if successful.
131 ENTRY(copy_user_generic_unrolled)
134 jb 20f /* less then 8 bytes, go to byte copy loop */
141 2: movq 1*8(%rsi),%r9
142 3: movq 2*8(%rsi),%r10
143 4: movq 3*8(%rsi),%r11
145 6: movq %r9,1*8(%rdi)
146 7: movq %r10,2*8(%rdi)
147 8: movq %r11,3*8(%rdi)
148 9: movq 4*8(%rsi),%r8
149 10: movq 5*8(%rsi),%r9
150 11: movq 6*8(%rsi),%r10
151 12: movq 7*8(%rsi),%r11
152 13: movq %r8,4*8(%rdi)
153 14: movq %r9,5*8(%rdi)
154 15: movq %r10,6*8(%rdi)
155 16: movq %r11,7*8(%rdi)
186 40: lea (%rdx,%rcx,8),%rdx
189 60: jmp copy_user_handle_tail /* ecx is zerorest also */
201 _ASM_EXTABLE(10b,30b)
202 _ASM_EXTABLE(11b,30b)
203 _ASM_EXTABLE(12b,30b)
204 _ASM_EXTABLE(13b,30b)
205 _ASM_EXTABLE(14b,30b)
206 _ASM_EXTABLE(15b,30b)
207 _ASM_EXTABLE(16b,30b)
208 _ASM_EXTABLE(18b,40b)
209 _ASM_EXTABLE(19b,40b)
210 _ASM_EXTABLE(21b,50b)
211 _ASM_EXTABLE(22b,50b)
213 ENDPROC(copy_user_generic_unrolled)
215 /* Some CPUs run faster using the string copy instructions.
216 * This is also a lot simpler. Use them when possible.
218 * Only 4GB of copy is supported. This shouldn't be a problem
219 * because the kernel normally only writes from/to page sized chunks
220 * even if user space passed a longer buffer.
221 * And more would be dangerous because both Intel and AMD have
222 * errata with rep movsq > 4GB. If someone feels the need to fix
223 * this please consider this.
231 * eax uncopied bytes or 0 if successful.
233 ENTRY(copy_user_generic_string)
238 jb 2f /* less than 8 bytes, go to byte copy loop */
252 11: lea (%rdx,%rcx,8),%rcx
253 12: movl %ecx,%edx /* ecx is zerorest also */
254 jmp copy_user_handle_tail
260 ENDPROC(copy_user_generic_string)
263 * Some CPUs are adding enhanced REP MOVSB/STOSB instructions.
264 * It's recommended to use enhanced REP MOVSB/STOSB if it's enabled.
272 * eax uncopied bytes or 0 if successful.
274 ENTRY(copy_user_enhanced_fast_string)
285 12: movl %ecx,%edx /* ecx is zerorest also */
286 jmp copy_user_handle_tail
291 ENDPROC(copy_user_enhanced_fast_string)