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/cpufeatures.h>
14 #include <asm/alternative-asm.h>
17 #include <asm/export.h>
19 /* Standard copy_to_user with segment limit checking */
21 mov PER_CPU_VAR(current_task), %rax
25 cmpq TASK_addr_limit(%rax),%rcx
27 ALTERNATIVE_2 "jmp copy_user_generic_unrolled", \
28 "jmp copy_user_generic_string", \
29 X86_FEATURE_REP_GOOD, \
30 "jmp copy_user_enhanced_fast_string", \
32 ENDPROC(_copy_to_user)
33 EXPORT_SYMBOL(_copy_to_user)
35 /* Standard copy_from_user with segment limit checking */
36 ENTRY(_copy_from_user)
37 mov PER_CPU_VAR(current_task), %rax
41 cmpq TASK_addr_limit(%rax),%rcx
43 ALTERNATIVE_2 "jmp copy_user_generic_unrolled", \
44 "jmp copy_user_generic_string", \
45 X86_FEATURE_REP_GOOD, \
46 "jmp copy_user_enhanced_fast_string", \
48 ENDPROC(_copy_from_user)
49 EXPORT_SYMBOL(_copy_from_user)
63 ENDPROC(bad_from_user)
67 * copy_user_generic_unrolled - memory copy with exception handling.
68 * This version is for CPUs like P4 that don't have efficient micro
77 * eax uncopied bytes or 0 if successful.
79 ENTRY(copy_user_generic_unrolled)
82 jb 20f /* less then 8 bytes, go to byte copy loop */
90 3: movq 2*8(%rsi),%r10
91 4: movq 3*8(%rsi),%r11
94 7: movq %r10,2*8(%rdi)
95 8: movq %r11,3*8(%rdi)
97 10: movq 5*8(%rsi),%r9
98 11: movq 6*8(%rsi),%r10
99 12: movq 7*8(%rsi),%r11
100 13: movq %r8,4*8(%rdi)
101 14: movq %r9,5*8(%rdi)
102 15: movq %r10,6*8(%rdi)
103 16: movq %r11,7*8(%rdi)
135 40: leal (%rdx,%rcx,8),%edx
138 60: jmp copy_user_handle_tail /* ecx is zerorest also */
150 _ASM_EXTABLE(10b,30b)
151 _ASM_EXTABLE(11b,30b)
152 _ASM_EXTABLE(12b,30b)
153 _ASM_EXTABLE(13b,30b)
154 _ASM_EXTABLE(14b,30b)
155 _ASM_EXTABLE(15b,30b)
156 _ASM_EXTABLE(16b,30b)
157 _ASM_EXTABLE(18b,40b)
158 _ASM_EXTABLE(19b,40b)
159 _ASM_EXTABLE(21b,50b)
160 _ASM_EXTABLE(22b,50b)
161 ENDPROC(copy_user_generic_unrolled)
162 EXPORT_SYMBOL(copy_user_generic_unrolled)
164 /* Some CPUs run faster using the string copy instructions.
165 * This is also a lot simpler. Use them when possible.
167 * Only 4GB of copy is supported. This shouldn't be a problem
168 * because the kernel normally only writes from/to page sized chunks
169 * even if user space passed a longer buffer.
170 * And more would be dangerous because both Intel and AMD have
171 * errata with rep movsq > 4GB. If someone feels the need to fix
172 * this please consider this.
180 * eax uncopied bytes or 0 if successful.
182 ENTRY(copy_user_generic_string)
185 jb 2f /* less than 8 bytes, go to byte copy loop */
200 11: leal (%rdx,%rcx,8),%ecx
201 12: movl %ecx,%edx /* ecx is zerorest also */
202 jmp copy_user_handle_tail
207 ENDPROC(copy_user_generic_string)
208 EXPORT_SYMBOL(copy_user_generic_string)
211 * Some CPUs are adding enhanced REP MOVSB/STOSB instructions.
212 * It's recommended to use enhanced REP MOVSB/STOSB if it's enabled.
220 * eax uncopied bytes or 0 if successful.
222 ENTRY(copy_user_enhanced_fast_string)
232 12: movl %ecx,%edx /* ecx is zerorest also */
233 jmp copy_user_handle_tail
237 ENDPROC(copy_user_enhanced_fast_string)
238 EXPORT_SYMBOL(copy_user_enhanced_fast_string)
241 * copy_user_nocache - Uncached memory copy with exception handling
242 * This will force destination out of cache for more performance.
244 * Note: Cached memory copy is used when destination or size is not
245 * naturally aligned. That is:
246 * - Require 8-byte alignment when size is 8 bytes or larger.
247 * - Require 4-byte alignment when size is 4 bytes.
249 ENTRY(__copy_user_nocache)
252 /* If size is less than 8 bytes, go to 4-byte copy */
254 jb .L_4b_nocache_copy_entry
256 /* If destination is not 8-byte aligned, "cache" copy to align it */
259 /* Set 4x8-byte copy count and remainder */
263 jz .L_8b_nocache_copy_entry /* jump if count is 0 */
265 /* Perform 4x8-byte nocache loop-copy */
266 .L_4x8b_nocache_copy_loop:
268 2: movq 1*8(%rsi),%r9
269 3: movq 2*8(%rsi),%r10
270 4: movq 3*8(%rsi),%r11
272 6: movnti %r9,1*8(%rdi)
273 7: movnti %r10,2*8(%rdi)
274 8: movnti %r11,3*8(%rdi)
275 9: movq 4*8(%rsi),%r8
276 10: movq 5*8(%rsi),%r9
277 11: movq 6*8(%rsi),%r10
278 12: movq 7*8(%rsi),%r11
279 13: movnti %r8,4*8(%rdi)
280 14: movnti %r9,5*8(%rdi)
281 15: movnti %r10,6*8(%rdi)
282 16: movnti %r11,7*8(%rdi)
286 jnz .L_4x8b_nocache_copy_loop
288 /* Set 8-byte copy count and remainder */
289 .L_8b_nocache_copy_entry:
293 jz .L_4b_nocache_copy_entry /* jump if count is 0 */
295 /* Perform 8-byte nocache loop-copy */
296 .L_8b_nocache_copy_loop:
298 21: movnti %r8,(%rdi)
302 jnz .L_8b_nocache_copy_loop
304 /* If no byte left, we're done */
305 .L_4b_nocache_copy_entry:
309 /* If destination is not 4-byte aligned, go to byte copy: */
312 jnz .L_1b_cache_copy_entry
314 /* Set 4-byte copy count (1 or 0) and remainder */
318 jz .L_1b_cache_copy_entry /* jump if count is 0 */
320 /* Perform 4-byte nocache copy: */
322 31: movnti %r8d,(%rdi)
326 /* If no bytes left, we're done: */
330 /* Perform byte "cache" loop-copy for the remainder */
331 .L_1b_cache_copy_entry:
333 .L_1b_cache_copy_loop:
339 jnz .L_1b_cache_copy_loop
341 /* Finished copying; fence the prior stores */
352 jmp .L_fixup_handle_tail
354 lea (%rdx,%rcx,8),%rdx
355 jmp .L_fixup_handle_tail
357 lea (%rdx,%rcx,4),%rdx
358 jmp .L_fixup_handle_tail
361 .L_fixup_handle_tail:
363 jmp copy_user_handle_tail
366 _ASM_EXTABLE(1b,.L_fixup_4x8b_copy)
367 _ASM_EXTABLE(2b,.L_fixup_4x8b_copy)
368 _ASM_EXTABLE(3b,.L_fixup_4x8b_copy)
369 _ASM_EXTABLE(4b,.L_fixup_4x8b_copy)
370 _ASM_EXTABLE(5b,.L_fixup_4x8b_copy)
371 _ASM_EXTABLE(6b,.L_fixup_4x8b_copy)
372 _ASM_EXTABLE(7b,.L_fixup_4x8b_copy)
373 _ASM_EXTABLE(8b,.L_fixup_4x8b_copy)
374 _ASM_EXTABLE(9b,.L_fixup_4x8b_copy)
375 _ASM_EXTABLE(10b,.L_fixup_4x8b_copy)
376 _ASM_EXTABLE(11b,.L_fixup_4x8b_copy)
377 _ASM_EXTABLE(12b,.L_fixup_4x8b_copy)
378 _ASM_EXTABLE(13b,.L_fixup_4x8b_copy)
379 _ASM_EXTABLE(14b,.L_fixup_4x8b_copy)
380 _ASM_EXTABLE(15b,.L_fixup_4x8b_copy)
381 _ASM_EXTABLE(16b,.L_fixup_4x8b_copy)
382 _ASM_EXTABLE(20b,.L_fixup_8b_copy)
383 _ASM_EXTABLE(21b,.L_fixup_8b_copy)
384 _ASM_EXTABLE(30b,.L_fixup_4b_copy)
385 _ASM_EXTABLE(31b,.L_fixup_4b_copy)
386 _ASM_EXTABLE(40b,.L_fixup_1b_copy)
387 _ASM_EXTABLE(41b,.L_fixup_1b_copy)
388 ENDPROC(__copy_user_nocache)
389 EXPORT_SYMBOL(__copy_user_nocache)