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>
11 #include <asm/current.h>
12 #include <asm/asm-offsets.h>
13 #include <asm/thread_info.h>
14 #include <asm/cpufeature.h>
15 #include <asm/alternative-asm.h>
20 * By placing feature2 after feature1 in altinstructions section, we logically
22 * If CPU has feature2, jmp to alt2 is used
23 * else if CPU has feature1, jmp to alt1 is used
24 * else jmp to orig is used.
26 .macro ALTERNATIVE_JUMP feature1,feature2,orig,alt1,alt2
30 .section .altinstr_replacement,"ax"
37 .section .altinstructions,"a"
38 altinstruction_entry 0b,2b,\feature1,5,5,0
39 altinstruction_entry 0b,3b,\feature2,5,5,0
43 .macro ALIGN_DESTINATION
44 /* check for bad alignment of destination */
47 jz 102f /* already aligned */
59 103: addl %ecx,%edx /* ecx is zerorest also */
60 jmp copy_user_handle_tail
63 _ASM_EXTABLE(100b,103b)
64 _ASM_EXTABLE(101b,103b)
67 /* Standard copy_to_user with segment limit checking */
74 cmpq TI_addr_limit(%rax),%rcx
76 ALTERNATIVE_JUMP X86_FEATURE_REP_GOOD,X86_FEATURE_ERMS, \
77 copy_user_generic_unrolled,copy_user_generic_string, \
78 copy_user_enhanced_fast_string
80 ENDPROC(_copy_to_user)
82 /* Standard copy_from_user with segment limit checking */
83 ENTRY(_copy_from_user)
89 cmpq TI_addr_limit(%rax),%rcx
91 ALTERNATIVE_JUMP X86_FEATURE_REP_GOOD,X86_FEATURE_ERMS, \
92 copy_user_generic_unrolled,copy_user_generic_string, \
93 copy_user_enhanced_fast_string
95 ENDPROC(_copy_from_user)
110 ENDPROC(bad_from_user)
114 * copy_user_generic_unrolled - memory copy with exception handling.
115 * This version is for CPUs like P4 that don't have efficient micro
124 * eax uncopied bytes or 0 if successful.
126 ENTRY(copy_user_generic_unrolled)
130 jb 20f /* less then 8 bytes, go to byte copy loop */
137 2: movq 1*8(%rsi),%r9
138 3: movq 2*8(%rsi),%r10
139 4: movq 3*8(%rsi),%r11
141 6: movq %r9,1*8(%rdi)
142 7: movq %r10,2*8(%rdi)
143 8: movq %r11,3*8(%rdi)
144 9: movq 4*8(%rsi),%r8
145 10: movq 5*8(%rsi),%r9
146 11: movq 6*8(%rsi),%r10
147 12: movq 7*8(%rsi),%r11
148 13: movq %r8,4*8(%rdi)
149 14: movq %r9,5*8(%rdi)
150 15: movq %r10,6*8(%rdi)
151 16: movq %r11,7*8(%rdi)
183 40: leal (%rdx,%rcx,8),%edx
186 60: jmp copy_user_handle_tail /* ecx is zerorest also */
198 _ASM_EXTABLE(10b,30b)
199 _ASM_EXTABLE(11b,30b)
200 _ASM_EXTABLE(12b,30b)
201 _ASM_EXTABLE(13b,30b)
202 _ASM_EXTABLE(14b,30b)
203 _ASM_EXTABLE(15b,30b)
204 _ASM_EXTABLE(16b,30b)
205 _ASM_EXTABLE(18b,40b)
206 _ASM_EXTABLE(19b,40b)
207 _ASM_EXTABLE(21b,50b)
208 _ASM_EXTABLE(22b,50b)
210 ENDPROC(copy_user_generic_unrolled)
212 /* Some CPUs run faster using the string copy instructions.
213 * This is also a lot simpler. Use them when possible.
215 * Only 4GB of copy is supported. This shouldn't be a problem
216 * because the kernel normally only writes from/to page sized chunks
217 * even if user space passed a longer buffer.
218 * And more would be dangerous because both Intel and AMD have
219 * errata with rep movsq > 4GB. If someone feels the need to fix
220 * this please consider this.
228 * eax uncopied bytes or 0 if successful.
230 ENTRY(copy_user_generic_string)
234 jb 2f /* less than 8 bytes, go to byte copy loop */
249 11: leal (%rdx,%rcx,8),%ecx
250 12: movl %ecx,%edx /* ecx is zerorest also */
251 jmp copy_user_handle_tail
257 ENDPROC(copy_user_generic_string)
260 * Some CPUs are adding enhanced REP MOVSB/STOSB instructions.
261 * It's recommended to use enhanced REP MOVSB/STOSB if it's enabled.
269 * eax uncopied bytes or 0 if successful.
271 ENTRY(copy_user_enhanced_fast_string)
282 12: movl %ecx,%edx /* ecx is zerorest also */
283 jmp copy_user_handle_tail
288 ENDPROC(copy_user_enhanced_fast_string)