2008-07-02 21:53:13 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2008 Vitaly Mayatskikh <vmayatsk@redhat.com>
|
|
|
|
* Copyright 2002 Andi Kleen, SuSE Labs.
|
2005-04-17 06:20:36 +08:00
|
|
|
* Subject to the GNU Public License v2.
|
2008-07-02 21:53:13 +08:00
|
|
|
*
|
|
|
|
* Functions to copy from and to user space.
|
|
|
|
*/
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-09-26 16:52:32 +08:00
|
|
|
#include <linux/linkage.h>
|
2006-09-26 16:52:39 +08:00
|
|
|
#include <asm/current.h>
|
|
|
|
#include <asm/asm-offsets.h>
|
|
|
|
#include <asm/thread_info.h>
|
2016-01-27 05:12:04 +08:00
|
|
|
#include <asm/cpufeatures.h>
|
2011-05-18 06:29:15 +08:00
|
|
|
#include <asm/alternative-asm.h>
|
2012-04-21 03:19:51 +08:00
|
|
|
#include <asm/asm.h>
|
2012-09-22 03:43:12 +08:00
|
|
|
#include <asm/smap.h>
|
2016-01-12 00:04:34 +08:00
|
|
|
#include <asm/export.h>
|
2006-09-26 16:52:39 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
2006-09-26 16:52:39 +08:00
|
|
|
* copy_user_generic_unrolled - memory copy with exception handling.
|
2008-07-02 21:53:13 +08:00
|
|
|
* This version is for CPUs like P4 that don't have efficient micro
|
|
|
|
* code for rep movsq
|
|
|
|
*
|
|
|
|
* Input:
|
2005-04-17 06:20:36 +08:00
|
|
|
* rdi destination
|
|
|
|
* rsi source
|
|
|
|
* rdx count
|
|
|
|
*
|
2008-07-02 21:53:13 +08:00
|
|
|
* Output:
|
2011-03-18 03:24:16 +08:00
|
|
|
* eax uncopied bytes or 0 if successful.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2006-09-26 16:52:39 +08:00
|
|
|
ENTRY(copy_user_generic_unrolled)
|
2012-09-22 03:43:12 +08:00
|
|
|
ASM_STAC
|
2008-07-02 21:53:13 +08:00
|
|
|
cmpl $8,%edx
|
|
|
|
jb 20f /* less then 8 bytes, go to byte copy loop */
|
|
|
|
ALIGN_DESTINATION
|
|
|
|
movl %edx,%ecx
|
|
|
|
andl $63,%edx
|
|
|
|
shrl $6,%ecx
|
2017-06-29 21:55:58 +08:00
|
|
|
jz .L_copy_short_string
|
2008-07-02 21:53:13 +08:00
|
|
|
1: movq (%rsi),%r8
|
|
|
|
2: movq 1*8(%rsi),%r9
|
|
|
|
3: movq 2*8(%rsi),%r10
|
|
|
|
4: movq 3*8(%rsi),%r11
|
|
|
|
5: movq %r8,(%rdi)
|
|
|
|
6: movq %r9,1*8(%rdi)
|
|
|
|
7: movq %r10,2*8(%rdi)
|
|
|
|
8: movq %r11,3*8(%rdi)
|
|
|
|
9: movq 4*8(%rsi),%r8
|
|
|
|
10: movq 5*8(%rsi),%r9
|
|
|
|
11: movq 6*8(%rsi),%r10
|
|
|
|
12: movq 7*8(%rsi),%r11
|
|
|
|
13: movq %r8,4*8(%rdi)
|
|
|
|
14: movq %r9,5*8(%rdi)
|
|
|
|
15: movq %r10,6*8(%rdi)
|
|
|
|
16: movq %r11,7*8(%rdi)
|
2006-02-04 04:51:02 +08:00
|
|
|
leaq 64(%rsi),%rsi
|
|
|
|
leaq 64(%rdi),%rdi
|
|
|
|
decl %ecx
|
2008-07-02 21:53:13 +08:00
|
|
|
jnz 1b
|
2017-06-29 21:55:58 +08:00
|
|
|
.L_copy_short_string:
|
|
|
|
movl %edx,%ecx
|
2008-07-02 21:53:13 +08:00
|
|
|
andl $7,%edx
|
|
|
|
shrl $3,%ecx
|
|
|
|
jz 20f
|
|
|
|
18: movq (%rsi),%r8
|
|
|
|
19: movq %r8,(%rdi)
|
2006-02-04 04:51:02 +08:00
|
|
|
leaq 8(%rsi),%rsi
|
2008-07-02 21:53:13 +08:00
|
|
|
leaq 8(%rdi),%rdi
|
|
|
|
decl %ecx
|
|
|
|
jnz 18b
|
|
|
|
20: andl %edx,%edx
|
|
|
|
jz 23f
|
2006-02-04 04:51:02 +08:00
|
|
|
movl %edx,%ecx
|
2008-07-02 21:53:13 +08:00
|
|
|
21: movb (%rsi),%al
|
|
|
|
22: movb %al,(%rdi)
|
2006-02-04 04:51:02 +08:00
|
|
|
incq %rsi
|
2008-07-02 21:53:13 +08:00
|
|
|
incq %rdi
|
2006-02-04 04:51:02 +08:00
|
|
|
decl %ecx
|
2008-07-02 21:53:13 +08:00
|
|
|
jnz 21b
|
|
|
|
23: xor %eax,%eax
|
2012-09-22 03:43:12 +08:00
|
|
|
ASM_CLAC
|
2006-02-04 04:51:02 +08:00
|
|
|
ret
|
|
|
|
|
2008-07-02 21:53:13 +08:00
|
|
|
.section .fixup,"ax"
|
|
|
|
30: shll $6,%ecx
|
|
|
|
addl %ecx,%edx
|
|
|
|
jmp 60f
|
2013-11-21 04:50:51 +08:00
|
|
|
40: leal (%rdx,%rcx,8),%edx
|
2008-07-02 21:53:13 +08:00
|
|
|
jmp 60f
|
|
|
|
50: movl %ecx,%edx
|
|
|
|
60: jmp copy_user_handle_tail /* ecx is zerorest also */
|
|
|
|
.previous
|
2006-02-04 04:51:02 +08:00
|
|
|
|
2012-04-21 03:19:51 +08:00
|
|
|
_ASM_EXTABLE(1b,30b)
|
|
|
|
_ASM_EXTABLE(2b,30b)
|
|
|
|
_ASM_EXTABLE(3b,30b)
|
|
|
|
_ASM_EXTABLE(4b,30b)
|
|
|
|
_ASM_EXTABLE(5b,30b)
|
|
|
|
_ASM_EXTABLE(6b,30b)
|
|
|
|
_ASM_EXTABLE(7b,30b)
|
|
|
|
_ASM_EXTABLE(8b,30b)
|
|
|
|
_ASM_EXTABLE(9b,30b)
|
|
|
|
_ASM_EXTABLE(10b,30b)
|
|
|
|
_ASM_EXTABLE(11b,30b)
|
|
|
|
_ASM_EXTABLE(12b,30b)
|
|
|
|
_ASM_EXTABLE(13b,30b)
|
|
|
|
_ASM_EXTABLE(14b,30b)
|
|
|
|
_ASM_EXTABLE(15b,30b)
|
|
|
|
_ASM_EXTABLE(16b,30b)
|
|
|
|
_ASM_EXTABLE(18b,40b)
|
|
|
|
_ASM_EXTABLE(19b,40b)
|
|
|
|
_ASM_EXTABLE(21b,50b)
|
|
|
|
_ASM_EXTABLE(22b,50b)
|
2008-07-02 21:53:13 +08:00
|
|
|
ENDPROC(copy_user_generic_unrolled)
|
2016-01-12 00:04:34 +08:00
|
|
|
EXPORT_SYMBOL(copy_user_generic_unrolled)
|
2006-09-26 16:52:32 +08:00
|
|
|
|
2008-07-02 21:53:13 +08:00
|
|
|
/* Some CPUs run faster using the string copy instructions.
|
|
|
|
* This is also a lot simpler. Use them when possible.
|
|
|
|
*
|
|
|
|
* Only 4GB of copy is supported. This shouldn't be a problem
|
|
|
|
* because the kernel normally only writes from/to page sized chunks
|
|
|
|
* even if user space passed a longer buffer.
|
|
|
|
* And more would be dangerous because both Intel and AMD have
|
|
|
|
* errata with rep movsq > 4GB. If someone feels the need to fix
|
|
|
|
* this please consider this.
|
|
|
|
*
|
|
|
|
* Input:
|
|
|
|
* rdi destination
|
|
|
|
* rsi source
|
|
|
|
* rdx count
|
|
|
|
*
|
|
|
|
* Output:
|
|
|
|
* eax uncopied bytes or 0 if successful.
|
|
|
|
*/
|
2006-09-26 16:52:39 +08:00
|
|
|
ENTRY(copy_user_generic_string)
|
2012-09-22 03:43:12 +08:00
|
|
|
ASM_STAC
|
2008-07-02 21:53:13 +08:00
|
|
|
cmpl $8,%edx
|
|
|
|
jb 2f /* less than 8 bytes, go to byte copy loop */
|
|
|
|
ALIGN_DESTINATION
|
2005-04-17 06:20:36 +08:00
|
|
|
movl %edx,%ecx
|
|
|
|
shrl $3,%ecx
|
2008-07-02 21:53:13 +08:00
|
|
|
andl $7,%edx
|
|
|
|
1: rep
|
2006-09-26 16:52:39 +08:00
|
|
|
movsq
|
2008-07-02 21:53:13 +08:00
|
|
|
2: movl %edx,%ecx
|
|
|
|
3: rep
|
|
|
|
movsb
|
2013-11-17 04:37:01 +08:00
|
|
|
xorl %eax,%eax
|
2012-09-22 03:43:12 +08:00
|
|
|
ASM_CLAC
|
2005-04-17 06:20:36 +08:00
|
|
|
ret
|
2006-09-26 16:52:39 +08:00
|
|
|
|
2008-07-02 21:53:13 +08:00
|
|
|
.section .fixup,"ax"
|
2013-11-21 04:50:51 +08:00
|
|
|
11: leal (%rdx,%rcx,8),%ecx
|
2008-07-02 21:53:13 +08:00
|
|
|
12: movl %ecx,%edx /* ecx is zerorest also */
|
|
|
|
jmp copy_user_handle_tail
|
|
|
|
.previous
|
2006-01-12 05:44:45 +08:00
|
|
|
|
2012-04-21 03:19:51 +08:00
|
|
|
_ASM_EXTABLE(1b,11b)
|
|
|
|
_ASM_EXTABLE(3b,12b)
|
2008-07-02 21:53:13 +08:00
|
|
|
ENDPROC(copy_user_generic_string)
|
2016-01-12 00:04:34 +08:00
|
|
|
EXPORT_SYMBOL(copy_user_generic_string)
|
2011-05-18 06:29:15 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Some CPUs are adding enhanced REP MOVSB/STOSB instructions.
|
|
|
|
* It's recommended to use enhanced REP MOVSB/STOSB if it's enabled.
|
|
|
|
*
|
|
|
|
* Input:
|
|
|
|
* rdi destination
|
|
|
|
* rsi source
|
|
|
|
* rdx count
|
|
|
|
*
|
|
|
|
* Output:
|
|
|
|
* eax uncopied bytes or 0 if successful.
|
|
|
|
*/
|
|
|
|
ENTRY(copy_user_enhanced_fast_string)
|
2012-09-22 03:43:12 +08:00
|
|
|
ASM_STAC
|
2017-06-29 21:55:58 +08:00
|
|
|
cmpl $64,%edx
|
|
|
|
jb .L_copy_short_string /* less then 64 bytes, avoid the costly 'rep' */
|
2011-05-18 06:29:15 +08:00
|
|
|
movl %edx,%ecx
|
|
|
|
1: rep
|
|
|
|
movsb
|
2013-11-17 04:37:01 +08:00
|
|
|
xorl %eax,%eax
|
2012-09-22 03:43:12 +08:00
|
|
|
ASM_CLAC
|
2011-05-18 06:29:15 +08:00
|
|
|
ret
|
|
|
|
|
|
|
|
.section .fixup,"ax"
|
|
|
|
12: movl %ecx,%edx /* ecx is zerorest also */
|
|
|
|
jmp copy_user_handle_tail
|
|
|
|
.previous
|
|
|
|
|
2012-04-21 03:19:51 +08:00
|
|
|
_ASM_EXTABLE(1b,12b)
|
2011-05-18 06:29:15 +08:00
|
|
|
ENDPROC(copy_user_enhanced_fast_string)
|
2016-01-12 00:04:34 +08:00
|
|
|
EXPORT_SYMBOL(copy_user_enhanced_fast_string)
|
2015-05-14 01:42:24 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* copy_user_nocache - Uncached memory copy with exception handling
|
2016-02-12 05:24:16 +08:00
|
|
|
* This will force destination out of cache for more performance.
|
|
|
|
*
|
|
|
|
* Note: Cached memory copy is used when destination or size is not
|
|
|
|
* naturally aligned. That is:
|
|
|
|
* - Require 8-byte alignment when size is 8 bytes or larger.
|
2016-02-12 05:24:17 +08:00
|
|
|
* - Require 4-byte alignment when size is 4 bytes.
|
2015-05-14 01:42:24 +08:00
|
|
|
*/
|
|
|
|
ENTRY(__copy_user_nocache)
|
|
|
|
ASM_STAC
|
2016-02-12 05:24:16 +08:00
|
|
|
|
2016-02-12 05:24:17 +08:00
|
|
|
/* If size is less than 8 bytes, go to 4-byte copy */
|
2015-05-14 01:42:24 +08:00
|
|
|
cmpl $8,%edx
|
2016-02-12 05:24:17 +08:00
|
|
|
jb .L_4b_nocache_copy_entry
|
2016-02-12 05:24:16 +08:00
|
|
|
|
|
|
|
/* If destination is not 8-byte aligned, "cache" copy to align it */
|
2015-05-14 01:42:24 +08:00
|
|
|
ALIGN_DESTINATION
|
2016-02-12 05:24:16 +08:00
|
|
|
|
|
|
|
/* Set 4x8-byte copy count and remainder */
|
2015-05-14 01:42:24 +08:00
|
|
|
movl %edx,%ecx
|
|
|
|
andl $63,%edx
|
|
|
|
shrl $6,%ecx
|
2016-02-12 05:24:16 +08:00
|
|
|
jz .L_8b_nocache_copy_entry /* jump if count is 0 */
|
|
|
|
|
|
|
|
/* Perform 4x8-byte nocache loop-copy */
|
|
|
|
.L_4x8b_nocache_copy_loop:
|
2015-05-14 01:42:24 +08:00
|
|
|
1: movq (%rsi),%r8
|
|
|
|
2: movq 1*8(%rsi),%r9
|
|
|
|
3: movq 2*8(%rsi),%r10
|
|
|
|
4: movq 3*8(%rsi),%r11
|
|
|
|
5: movnti %r8,(%rdi)
|
|
|
|
6: movnti %r9,1*8(%rdi)
|
|
|
|
7: movnti %r10,2*8(%rdi)
|
|
|
|
8: movnti %r11,3*8(%rdi)
|
|
|
|
9: movq 4*8(%rsi),%r8
|
|
|
|
10: movq 5*8(%rsi),%r9
|
|
|
|
11: movq 6*8(%rsi),%r10
|
|
|
|
12: movq 7*8(%rsi),%r11
|
|
|
|
13: movnti %r8,4*8(%rdi)
|
|
|
|
14: movnti %r9,5*8(%rdi)
|
|
|
|
15: movnti %r10,6*8(%rdi)
|
|
|
|
16: movnti %r11,7*8(%rdi)
|
|
|
|
leaq 64(%rsi),%rsi
|
|
|
|
leaq 64(%rdi),%rdi
|
|
|
|
decl %ecx
|
2016-02-12 05:24:16 +08:00
|
|
|
jnz .L_4x8b_nocache_copy_loop
|
|
|
|
|
|
|
|
/* Set 8-byte copy count and remainder */
|
|
|
|
.L_8b_nocache_copy_entry:
|
|
|
|
movl %edx,%ecx
|
2015-05-14 01:42:24 +08:00
|
|
|
andl $7,%edx
|
|
|
|
shrl $3,%ecx
|
2016-02-12 05:24:17 +08:00
|
|
|
jz .L_4b_nocache_copy_entry /* jump if count is 0 */
|
2016-02-12 05:24:16 +08:00
|
|
|
|
|
|
|
/* Perform 8-byte nocache loop-copy */
|
|
|
|
.L_8b_nocache_copy_loop:
|
|
|
|
20: movq (%rsi),%r8
|
|
|
|
21: movnti %r8,(%rdi)
|
2015-05-14 01:42:24 +08:00
|
|
|
leaq 8(%rsi),%rsi
|
|
|
|
leaq 8(%rdi),%rdi
|
|
|
|
decl %ecx
|
2016-02-12 05:24:16 +08:00
|
|
|
jnz .L_8b_nocache_copy_loop
|
|
|
|
|
|
|
|
/* If no byte left, we're done */
|
2016-02-12 05:24:17 +08:00
|
|
|
.L_4b_nocache_copy_entry:
|
|
|
|
andl %edx,%edx
|
|
|
|
jz .L_finish_copy
|
|
|
|
|
|
|
|
/* If destination is not 4-byte aligned, go to byte copy: */
|
|
|
|
movl %edi,%ecx
|
|
|
|
andl $3,%ecx
|
|
|
|
jnz .L_1b_cache_copy_entry
|
|
|
|
|
|
|
|
/* Set 4-byte copy count (1 or 0) and remainder */
|
2015-05-14 01:42:24 +08:00
|
|
|
movl %edx,%ecx
|
2016-02-12 05:24:17 +08:00
|
|
|
andl $3,%edx
|
|
|
|
shrl $2,%ecx
|
|
|
|
jz .L_1b_cache_copy_entry /* jump if count is 0 */
|
|
|
|
|
|
|
|
/* Perform 4-byte nocache copy: */
|
|
|
|
30: movl (%rsi),%r8d
|
|
|
|
31: movnti %r8d,(%rdi)
|
|
|
|
leaq 4(%rsi),%rsi
|
|
|
|
leaq 4(%rdi),%rdi
|
|
|
|
|
|
|
|
/* If no bytes left, we're done: */
|
2016-02-12 05:24:16 +08:00
|
|
|
andl %edx,%edx
|
|
|
|
jz .L_finish_copy
|
|
|
|
|
|
|
|
/* Perform byte "cache" loop-copy for the remainder */
|
2016-02-12 05:24:17 +08:00
|
|
|
.L_1b_cache_copy_entry:
|
2015-05-14 01:42:24 +08:00
|
|
|
movl %edx,%ecx
|
2016-02-12 05:24:16 +08:00
|
|
|
.L_1b_cache_copy_loop:
|
|
|
|
40: movb (%rsi),%al
|
|
|
|
41: movb %al,(%rdi)
|
2015-05-14 01:42:24 +08:00
|
|
|
incq %rsi
|
|
|
|
incq %rdi
|
|
|
|
decl %ecx
|
2016-02-12 05:24:16 +08:00
|
|
|
jnz .L_1b_cache_copy_loop
|
|
|
|
|
|
|
|
/* Finished copying; fence the prior stores */
|
|
|
|
.L_finish_copy:
|
|
|
|
xorl %eax,%eax
|
2015-05-14 01:42:24 +08:00
|
|
|
ASM_CLAC
|
|
|
|
sfence
|
|
|
|
ret
|
|
|
|
|
|
|
|
.section .fixup,"ax"
|
2016-02-12 05:24:16 +08:00
|
|
|
.L_fixup_4x8b_copy:
|
|
|
|
shll $6,%ecx
|
2015-05-14 01:42:24 +08:00
|
|
|
addl %ecx,%edx
|
2016-02-12 05:24:16 +08:00
|
|
|
jmp .L_fixup_handle_tail
|
|
|
|
.L_fixup_8b_copy:
|
|
|
|
lea (%rdx,%rcx,8),%rdx
|
|
|
|
jmp .L_fixup_handle_tail
|
2016-02-12 05:24:17 +08:00
|
|
|
.L_fixup_4b_copy:
|
|
|
|
lea (%rdx,%rcx,4),%rdx
|
|
|
|
jmp .L_fixup_handle_tail
|
2016-02-12 05:24:16 +08:00
|
|
|
.L_fixup_1b_copy:
|
|
|
|
movl %ecx,%edx
|
|
|
|
.L_fixup_handle_tail:
|
|
|
|
sfence
|
2015-05-14 01:42:24 +08:00
|
|
|
jmp copy_user_handle_tail
|
|
|
|
.previous
|
|
|
|
|
2016-02-12 05:24:16 +08:00
|
|
|
_ASM_EXTABLE(1b,.L_fixup_4x8b_copy)
|
|
|
|
_ASM_EXTABLE(2b,.L_fixup_4x8b_copy)
|
|
|
|
_ASM_EXTABLE(3b,.L_fixup_4x8b_copy)
|
|
|
|
_ASM_EXTABLE(4b,.L_fixup_4x8b_copy)
|
|
|
|
_ASM_EXTABLE(5b,.L_fixup_4x8b_copy)
|
|
|
|
_ASM_EXTABLE(6b,.L_fixup_4x8b_copy)
|
|
|
|
_ASM_EXTABLE(7b,.L_fixup_4x8b_copy)
|
|
|
|
_ASM_EXTABLE(8b,.L_fixup_4x8b_copy)
|
|
|
|
_ASM_EXTABLE(9b,.L_fixup_4x8b_copy)
|
|
|
|
_ASM_EXTABLE(10b,.L_fixup_4x8b_copy)
|
|
|
|
_ASM_EXTABLE(11b,.L_fixup_4x8b_copy)
|
|
|
|
_ASM_EXTABLE(12b,.L_fixup_4x8b_copy)
|
|
|
|
_ASM_EXTABLE(13b,.L_fixup_4x8b_copy)
|
|
|
|
_ASM_EXTABLE(14b,.L_fixup_4x8b_copy)
|
|
|
|
_ASM_EXTABLE(15b,.L_fixup_4x8b_copy)
|
|
|
|
_ASM_EXTABLE(16b,.L_fixup_4x8b_copy)
|
|
|
|
_ASM_EXTABLE(20b,.L_fixup_8b_copy)
|
|
|
|
_ASM_EXTABLE(21b,.L_fixup_8b_copy)
|
2016-02-12 05:24:17 +08:00
|
|
|
_ASM_EXTABLE(30b,.L_fixup_4b_copy)
|
|
|
|
_ASM_EXTABLE(31b,.L_fixup_4b_copy)
|
2016-02-12 05:24:16 +08:00
|
|
|
_ASM_EXTABLE(40b,.L_fixup_1b_copy)
|
|
|
|
_ASM_EXTABLE(41b,.L_fixup_1b_copy)
|
2015-05-14 01:42:24 +08:00
|
|
|
ENDPROC(__copy_user_nocache)
|
2016-01-12 00:04:34 +08:00
|
|
|
EXPORT_SYMBOL(__copy_user_nocache)
|