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>
20 * copy_user_generic_unrolled - memory copy with exception handling.
21 * This version is for CPUs like P4 that don't have efficient micro
30 * eax uncopied bytes or 0 if successful.
32 ENTRY(copy_user_generic_unrolled)
35 jb 20f /* less then 8 bytes, go to byte copy loop */
43 3: movq 2*8(%rsi),%r10
44 4: movq 3*8(%rsi),%r11
47 7: movq %r10,2*8(%rdi)
48 8: movq %r11,3*8(%rdi)
50 10: movq 5*8(%rsi),%r9
51 11: movq 6*8(%rsi),%r10
52 12: movq 7*8(%rsi),%r11
53 13: movq %r8,4*8(%rdi)
54 14: movq %r9,5*8(%rdi)
55 15: movq %r10,6*8(%rdi)
56 16: movq %r11,7*8(%rdi)
88 40: leal (%rdx,%rcx,8),%edx
91 60: jmp copy_user_handle_tail /* ecx is zerorest also */
103 _ASM_EXTABLE(10b,30b)
104 _ASM_EXTABLE(11b,30b)
105 _ASM_EXTABLE(12b,30b)
106 _ASM_EXTABLE(13b,30b)
107 _ASM_EXTABLE(14b,30b)
108 _ASM_EXTABLE(15b,30b)
109 _ASM_EXTABLE(16b,30b)
110 _ASM_EXTABLE(18b,40b)
111 _ASM_EXTABLE(19b,40b)
112 _ASM_EXTABLE(21b,50b)
113 _ASM_EXTABLE(22b,50b)
114 ENDPROC(copy_user_generic_unrolled)
115 EXPORT_SYMBOL(copy_user_generic_unrolled)
117 /* Some CPUs run faster using the string copy instructions.
118 * This is also a lot simpler. Use them when possible.
120 * Only 4GB of copy is supported. This shouldn't be a problem
121 * because the kernel normally only writes from/to page sized chunks
122 * even if user space passed a longer buffer.
123 * And more would be dangerous because both Intel and AMD have
124 * errata with rep movsq > 4GB. If someone feels the need to fix
125 * this please consider this.
133 * eax uncopied bytes or 0 if successful.
135 ENTRY(copy_user_generic_string)
138 jb 2f /* less than 8 bytes, go to byte copy loop */
153 11: leal (%rdx,%rcx,8),%ecx
154 12: movl %ecx,%edx /* ecx is zerorest also */
155 jmp copy_user_handle_tail
160 ENDPROC(copy_user_generic_string)
161 EXPORT_SYMBOL(copy_user_generic_string)
164 * Some CPUs are adding enhanced REP MOVSB/STOSB instructions.
165 * It's recommended to use enhanced REP MOVSB/STOSB if it's enabled.
173 * eax uncopied bytes or 0 if successful.
175 ENTRY(copy_user_enhanced_fast_string)
185 12: movl %ecx,%edx /* ecx is zerorest also */
186 jmp copy_user_handle_tail
190 ENDPROC(copy_user_enhanced_fast_string)
191 EXPORT_SYMBOL(copy_user_enhanced_fast_string)
194 * copy_user_nocache - Uncached memory copy with exception handling
195 * This will force destination out of cache for more performance.
197 * Note: Cached memory copy is used when destination or size is not
198 * naturally aligned. That is:
199 * - Require 8-byte alignment when size is 8 bytes or larger.
200 * - Require 4-byte alignment when size is 4 bytes.
202 ENTRY(__copy_user_nocache)
205 /* If size is less than 8 bytes, go to 4-byte copy */
207 jb .L_4b_nocache_copy_entry
209 /* If destination is not 8-byte aligned, "cache" copy to align it */
212 /* Set 4x8-byte copy count and remainder */
216 jz .L_8b_nocache_copy_entry /* jump if count is 0 */
218 /* Perform 4x8-byte nocache loop-copy */
219 .L_4x8b_nocache_copy_loop:
221 2: movq 1*8(%rsi),%r9
222 3: movq 2*8(%rsi),%r10
223 4: movq 3*8(%rsi),%r11
225 6: movnti %r9,1*8(%rdi)
226 7: movnti %r10,2*8(%rdi)
227 8: movnti %r11,3*8(%rdi)
228 9: movq 4*8(%rsi),%r8
229 10: movq 5*8(%rsi),%r9
230 11: movq 6*8(%rsi),%r10
231 12: movq 7*8(%rsi),%r11
232 13: movnti %r8,4*8(%rdi)
233 14: movnti %r9,5*8(%rdi)
234 15: movnti %r10,6*8(%rdi)
235 16: movnti %r11,7*8(%rdi)
239 jnz .L_4x8b_nocache_copy_loop
241 /* Set 8-byte copy count and remainder */
242 .L_8b_nocache_copy_entry:
246 jz .L_4b_nocache_copy_entry /* jump if count is 0 */
248 /* Perform 8-byte nocache loop-copy */
249 .L_8b_nocache_copy_loop:
251 21: movnti %r8,(%rdi)
255 jnz .L_8b_nocache_copy_loop
257 /* If no byte left, we're done */
258 .L_4b_nocache_copy_entry:
262 /* If destination is not 4-byte aligned, go to byte copy: */
265 jnz .L_1b_cache_copy_entry
267 /* Set 4-byte copy count (1 or 0) and remainder */
271 jz .L_1b_cache_copy_entry /* jump if count is 0 */
273 /* Perform 4-byte nocache copy: */
275 31: movnti %r8d,(%rdi)
279 /* If no bytes left, we're done: */
283 /* Perform byte "cache" loop-copy for the remainder */
284 .L_1b_cache_copy_entry:
286 .L_1b_cache_copy_loop:
292 jnz .L_1b_cache_copy_loop
294 /* Finished copying; fence the prior stores */
305 jmp .L_fixup_handle_tail
307 lea (%rdx,%rcx,8),%rdx
308 jmp .L_fixup_handle_tail
310 lea (%rdx,%rcx,4),%rdx
311 jmp .L_fixup_handle_tail
314 .L_fixup_handle_tail:
316 jmp copy_user_handle_tail
319 _ASM_EXTABLE(1b,.L_fixup_4x8b_copy)
320 _ASM_EXTABLE(2b,.L_fixup_4x8b_copy)
321 _ASM_EXTABLE(3b,.L_fixup_4x8b_copy)
322 _ASM_EXTABLE(4b,.L_fixup_4x8b_copy)
323 _ASM_EXTABLE(5b,.L_fixup_4x8b_copy)
324 _ASM_EXTABLE(6b,.L_fixup_4x8b_copy)
325 _ASM_EXTABLE(7b,.L_fixup_4x8b_copy)
326 _ASM_EXTABLE(8b,.L_fixup_4x8b_copy)
327 _ASM_EXTABLE(9b,.L_fixup_4x8b_copy)
328 _ASM_EXTABLE(10b,.L_fixup_4x8b_copy)
329 _ASM_EXTABLE(11b,.L_fixup_4x8b_copy)
330 _ASM_EXTABLE(12b,.L_fixup_4x8b_copy)
331 _ASM_EXTABLE(13b,.L_fixup_4x8b_copy)
332 _ASM_EXTABLE(14b,.L_fixup_4x8b_copy)
333 _ASM_EXTABLE(15b,.L_fixup_4x8b_copy)
334 _ASM_EXTABLE(16b,.L_fixup_4x8b_copy)
335 _ASM_EXTABLE(20b,.L_fixup_8b_copy)
336 _ASM_EXTABLE(21b,.L_fixup_8b_copy)
337 _ASM_EXTABLE(30b,.L_fixup_4b_copy)
338 _ASM_EXTABLE(31b,.L_fixup_4b_copy)
339 _ASM_EXTABLE(40b,.L_fixup_1b_copy)
340 _ASM_EXTABLE(41b,.L_fixup_1b_copy)
341 ENDPROC(__copy_user_nocache)
342 EXPORT_SYMBOL(__copy_user_nocache)