Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 Vitaly Mayatskikh <vmayatsk@redhat.com> |
| 3 | * Copyright 2002 Andi Kleen, SuSE Labs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Subject to the GNU Public License v2. |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 5 | * |
| 6 | * Functions to copy from and to user space. |
| 7 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 9 | #include <linux/linkage.h> |
| 10 | #include <asm/dwarf2.h> |
Andi Kleen | 3022d73 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 11 | #include <asm/current.h> |
| 12 | #include <asm/asm-offsets.h> |
| 13 | #include <asm/thread_info.h> |
| 14 | #include <asm/cpufeature.h> |
Fenghua Yu | 4307bec | 2011-05-17 15:29:15 -0700 | [diff] [blame] | 15 | #include <asm/alternative-asm.h> |
H. Peter Anvin | 9732da8 | 2012-04-20 12:19:51 -0700 | [diff] [blame] | 16 | #include <asm/asm.h> |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 17 | #include <asm/smap.h> |
Andi Kleen | 3022d73 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 18 | |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 19 | .macro ALIGN_DESTINATION |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 20 | /* check for bad alignment of destination */ |
| 21 | movl %edi,%ecx |
| 22 | andl $7,%ecx |
| 23 | jz 102f /* already aligned */ |
| 24 | subl $8,%ecx |
| 25 | negl %ecx |
| 26 | subl %ecx,%edx |
| 27 | 100: movb (%rsi),%al |
| 28 | 101: movb %al,(%rdi) |
| 29 | incq %rsi |
| 30 | incq %rdi |
| 31 | decl %ecx |
| 32 | jnz 100b |
| 33 | 102: |
| 34 | .section .fixup,"ax" |
Vitaly Mayatskikh | afd962a | 2008-07-30 13:30:14 +0200 | [diff] [blame] | 35 | 103: addl %ecx,%edx /* ecx is zerorest also */ |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 36 | jmp copy_user_handle_tail |
| 37 | .previous |
| 38 | |
H. Peter Anvin | 9732da8 | 2012-04-20 12:19:51 -0700 | [diff] [blame] | 39 | _ASM_EXTABLE(100b,103b) |
| 40 | _ASM_EXTABLE(101b,103b) |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 41 | .endm |
| 42 | |
| 43 | /* Standard copy_to_user with segment limit checking */ |
Frederic Weisbecker | 3c93ca0 | 2009-11-16 15:42:18 +0100 | [diff] [blame] | 44 | ENTRY(_copy_to_user) |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 45 | CFI_STARTPROC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | GET_THREAD_INFO(%rax) |
| 47 | movq %rdi,%rcx |
| 48 | addq %rdx,%rcx |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 49 | jc bad_to_user |
Glauber Costa | 26ccb8a | 2008-06-24 11:19:35 -0300 | [diff] [blame] | 50 | cmpq TI_addr_limit(%rax),%rcx |
Jiri Olsa | 26afb7c | 2011-05-12 16:30:30 +0200 | [diff] [blame] | 51 | ja bad_to_user |
Borislav Petkov | de2ff88 | 2015-01-13 01:38:17 +0100 | [diff] [blame^] | 52 | ALTERNATIVE_2 "jmp copy_user_generic_unrolled", \ |
| 53 | "jmp copy_user_generic_string", \ |
| 54 | X86_FEATURE_REP_GOOD, \ |
| 55 | "jmp copy_user_enhanced_fast_string", \ |
| 56 | X86_FEATURE_ERMS |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 57 | CFI_ENDPROC |
Frederic Weisbecker | 3c93ca0 | 2009-11-16 15:42:18 +0100 | [diff] [blame] | 58 | ENDPROC(_copy_to_user) |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 59 | |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 60 | /* Standard copy_from_user with segment limit checking */ |
Arjan van de Ven | 9f0cf4a | 2009-09-26 14:33:01 +0200 | [diff] [blame] | 61 | ENTRY(_copy_from_user) |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 62 | CFI_STARTPROC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | GET_THREAD_INFO(%rax) |
| 64 | movq %rsi,%rcx |
| 65 | addq %rdx,%rcx |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 66 | jc bad_from_user |
Glauber Costa | 26ccb8a | 2008-06-24 11:19:35 -0300 | [diff] [blame] | 67 | cmpq TI_addr_limit(%rax),%rcx |
Jiri Olsa | 26afb7c | 2011-05-12 16:30:30 +0200 | [diff] [blame] | 68 | ja bad_from_user |
Borislav Petkov | de2ff88 | 2015-01-13 01:38:17 +0100 | [diff] [blame^] | 69 | ALTERNATIVE_2 "jmp copy_user_generic_unrolled", \ |
| 70 | "jmp copy_user_generic_string", \ |
| 71 | X86_FEATURE_REP_GOOD, \ |
| 72 | "jmp copy_user_enhanced_fast_string", \ |
| 73 | X86_FEATURE_ERMS |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 74 | CFI_ENDPROC |
Arjan van de Ven | 9f0cf4a | 2009-09-26 14:33:01 +0200 | [diff] [blame] | 75 | ENDPROC(_copy_from_user) |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | .section .fixup,"ax" |
| 78 | /* must zero dest */ |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 79 | ENTRY(bad_from_user) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | bad_from_user: |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 81 | CFI_STARTPROC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | movl %edx,%ecx |
| 83 | xorl %eax,%eax |
| 84 | rep |
| 85 | stosb |
| 86 | bad_to_user: |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 87 | movl %edx,%eax |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | ret |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 89 | CFI_ENDPROC |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 90 | ENDPROC(bad_from_user) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | .previous |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | /* |
Andi Kleen | 3022d73 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 94 | * copy_user_generic_unrolled - memory copy with exception handling. |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 95 | * This version is for CPUs like P4 that don't have efficient micro |
| 96 | * code for rep movsq |
| 97 | * |
| 98 | * Input: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | * rdi destination |
| 100 | * rsi source |
| 101 | * rdx count |
| 102 | * |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 103 | * Output: |
Lucas De Marchi | 0d2eb44 | 2011-03-17 16:24:16 -0300 | [diff] [blame] | 104 | * eax uncopied bytes or 0 if successful. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | */ |
Andi Kleen | 3022d73 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 106 | ENTRY(copy_user_generic_unrolled) |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 107 | CFI_STARTPROC |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 108 | ASM_STAC |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 109 | cmpl $8,%edx |
| 110 | jb 20f /* less then 8 bytes, go to byte copy loop */ |
| 111 | ALIGN_DESTINATION |
| 112 | movl %edx,%ecx |
| 113 | andl $63,%edx |
| 114 | shrl $6,%ecx |
| 115 | jz 17f |
| 116 | 1: movq (%rsi),%r8 |
| 117 | 2: movq 1*8(%rsi),%r9 |
| 118 | 3: movq 2*8(%rsi),%r10 |
| 119 | 4: movq 3*8(%rsi),%r11 |
| 120 | 5: movq %r8,(%rdi) |
| 121 | 6: movq %r9,1*8(%rdi) |
| 122 | 7: movq %r10,2*8(%rdi) |
| 123 | 8: movq %r11,3*8(%rdi) |
| 124 | 9: movq 4*8(%rsi),%r8 |
| 125 | 10: movq 5*8(%rsi),%r9 |
| 126 | 11: movq 6*8(%rsi),%r10 |
| 127 | 12: movq 7*8(%rsi),%r11 |
| 128 | 13: movq %r8,4*8(%rdi) |
| 129 | 14: movq %r9,5*8(%rdi) |
| 130 | 15: movq %r10,6*8(%rdi) |
| 131 | 16: movq %r11,7*8(%rdi) |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 132 | leaq 64(%rsi),%rsi |
| 133 | leaq 64(%rdi),%rdi |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 134 | decl %ecx |
| 135 | jnz 1b |
| 136 | 17: movl %edx,%ecx |
| 137 | andl $7,%edx |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 138 | shrl $3,%ecx |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 139 | jz 20f |
| 140 | 18: movq (%rsi),%r8 |
| 141 | 19: movq %r8,(%rdi) |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 142 | leaq 8(%rsi),%rsi |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 143 | leaq 8(%rdi),%rdi |
| 144 | decl %ecx |
| 145 | jnz 18b |
| 146 | 20: andl %edx,%edx |
| 147 | jz 23f |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 148 | movl %edx,%ecx |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 149 | 21: movb (%rsi),%al |
| 150 | 22: movb %al,(%rdi) |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 151 | incq %rsi |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 152 | incq %rdi |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 153 | decl %ecx |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 154 | jnz 21b |
| 155 | 23: xor %eax,%eax |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 156 | ASM_CLAC |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 157 | ret |
| 158 | |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 159 | .section .fixup,"ax" |
| 160 | 30: shll $6,%ecx |
| 161 | addl %ecx,%edx |
| 162 | jmp 60f |
H. Peter Anvin | 661c801 | 2013-11-20 12:50:51 -0800 | [diff] [blame] | 163 | 40: leal (%rdx,%rcx,8),%edx |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 164 | jmp 60f |
| 165 | 50: movl %ecx,%edx |
| 166 | 60: jmp copy_user_handle_tail /* ecx is zerorest also */ |
| 167 | .previous |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 168 | |
H. Peter Anvin | 9732da8 | 2012-04-20 12:19:51 -0700 | [diff] [blame] | 169 | _ASM_EXTABLE(1b,30b) |
| 170 | _ASM_EXTABLE(2b,30b) |
| 171 | _ASM_EXTABLE(3b,30b) |
| 172 | _ASM_EXTABLE(4b,30b) |
| 173 | _ASM_EXTABLE(5b,30b) |
| 174 | _ASM_EXTABLE(6b,30b) |
| 175 | _ASM_EXTABLE(7b,30b) |
| 176 | _ASM_EXTABLE(8b,30b) |
| 177 | _ASM_EXTABLE(9b,30b) |
| 178 | _ASM_EXTABLE(10b,30b) |
| 179 | _ASM_EXTABLE(11b,30b) |
| 180 | _ASM_EXTABLE(12b,30b) |
| 181 | _ASM_EXTABLE(13b,30b) |
| 182 | _ASM_EXTABLE(14b,30b) |
| 183 | _ASM_EXTABLE(15b,30b) |
| 184 | _ASM_EXTABLE(16b,30b) |
| 185 | _ASM_EXTABLE(18b,40b) |
| 186 | _ASM_EXTABLE(19b,40b) |
| 187 | _ASM_EXTABLE(21b,50b) |
| 188 | _ASM_EXTABLE(22b,50b) |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 189 | CFI_ENDPROC |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 190 | ENDPROC(copy_user_generic_unrolled) |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 191 | |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 192 | /* Some CPUs run faster using the string copy instructions. |
| 193 | * This is also a lot simpler. Use them when possible. |
| 194 | * |
| 195 | * Only 4GB of copy is supported. This shouldn't be a problem |
| 196 | * because the kernel normally only writes from/to page sized chunks |
| 197 | * even if user space passed a longer buffer. |
| 198 | * And more would be dangerous because both Intel and AMD have |
| 199 | * errata with rep movsq > 4GB. If someone feels the need to fix |
| 200 | * this please consider this. |
| 201 | * |
| 202 | * Input: |
| 203 | * rdi destination |
| 204 | * rsi source |
| 205 | * rdx count |
| 206 | * |
| 207 | * Output: |
| 208 | * eax uncopied bytes or 0 if successful. |
| 209 | */ |
Andi Kleen | 3022d73 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 210 | ENTRY(copy_user_generic_string) |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 211 | CFI_STARTPROC |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 212 | ASM_STAC |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 213 | cmpl $8,%edx |
| 214 | jb 2f /* less than 8 bytes, go to byte copy loop */ |
| 215 | ALIGN_DESTINATION |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | movl %edx,%ecx |
| 217 | shrl $3,%ecx |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 218 | andl $7,%edx |
| 219 | 1: rep |
Andi Kleen | 3022d73 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 220 | movsq |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 221 | 2: movl %edx,%ecx |
| 222 | 3: rep |
| 223 | movsb |
Fenghua Yu | f4cb1cc | 2013-11-16 12:37:01 -0800 | [diff] [blame] | 224 | xorl %eax,%eax |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 225 | ASM_CLAC |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 226 | ret |
Andi Kleen | 3022d73 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 227 | |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 228 | .section .fixup,"ax" |
H. Peter Anvin | 661c801 | 2013-11-20 12:50:51 -0800 | [diff] [blame] | 229 | 11: leal (%rdx,%rcx,8),%ecx |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 230 | 12: movl %ecx,%edx /* ecx is zerorest also */ |
| 231 | jmp copy_user_handle_tail |
| 232 | .previous |
Andi Kleen | 2cbc9ee | 2006-01-11 22:44:45 +0100 | [diff] [blame] | 233 | |
H. Peter Anvin | 9732da8 | 2012-04-20 12:19:51 -0700 | [diff] [blame] | 234 | _ASM_EXTABLE(1b,11b) |
| 235 | _ASM_EXTABLE(3b,12b) |
Vitaly Mayatskikh | ad2fc2c | 2008-07-02 15:53:13 +0200 | [diff] [blame] | 236 | CFI_ENDPROC |
| 237 | ENDPROC(copy_user_generic_string) |
Fenghua Yu | 4307bec | 2011-05-17 15:29:15 -0700 | [diff] [blame] | 238 | |
| 239 | /* |
| 240 | * Some CPUs are adding enhanced REP MOVSB/STOSB instructions. |
| 241 | * It's recommended to use enhanced REP MOVSB/STOSB if it's enabled. |
| 242 | * |
| 243 | * Input: |
| 244 | * rdi destination |
| 245 | * rsi source |
| 246 | * rdx count |
| 247 | * |
| 248 | * Output: |
| 249 | * eax uncopied bytes or 0 if successful. |
| 250 | */ |
| 251 | ENTRY(copy_user_enhanced_fast_string) |
| 252 | CFI_STARTPROC |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 253 | ASM_STAC |
Fenghua Yu | 4307bec | 2011-05-17 15:29:15 -0700 | [diff] [blame] | 254 | movl %edx,%ecx |
| 255 | 1: rep |
| 256 | movsb |
Fenghua Yu | f4cb1cc | 2013-11-16 12:37:01 -0800 | [diff] [blame] | 257 | xorl %eax,%eax |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 258 | ASM_CLAC |
Fenghua Yu | 4307bec | 2011-05-17 15:29:15 -0700 | [diff] [blame] | 259 | ret |
| 260 | |
| 261 | .section .fixup,"ax" |
| 262 | 12: movl %ecx,%edx /* ecx is zerorest also */ |
| 263 | jmp copy_user_handle_tail |
| 264 | .previous |
| 265 | |
H. Peter Anvin | 9732da8 | 2012-04-20 12:19:51 -0700 | [diff] [blame] | 266 | _ASM_EXTABLE(1b,12b) |
Fenghua Yu | 4307bec | 2011-05-17 15:29:15 -0700 | [diff] [blame] | 267 | CFI_ENDPROC |
| 268 | ENDPROC(copy_user_enhanced_fast_string) |