blob: e4b3beee83bd7f8e14c638bee500b398fc84dc52 [file] [log] [blame]
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +02001/*
2 * Copyright 2008 Vitaly Mayatskikh <vmayatsk@redhat.com>
3 * Copyright 2002 Andi Kleen, SuSE Labs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Subject to the GNU Public License v2.
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +02005 *
6 * Functions to copy from and to user space.
7 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
Jan Beulich8d379da2006-09-26 10:52:32 +02009#include <linux/linkage.h>
10#include <asm/dwarf2.h>
Andi Kleen3022d732006-09-26 10:52:39 +020011#include <asm/current.h>
12#include <asm/asm-offsets.h>
13#include <asm/thread_info.h>
14#include <asm/cpufeature.h>
Fenghua Yu4307bec2011-05-17 15:29:15 -070015#include <asm/alternative-asm.h>
H. Peter Anvin9732da82012-04-20 12:19:51 -070016#include <asm/asm.h>
H. Peter Anvin63bcff22012-09-21 12:43:12 -070017#include <asm/smap.h>
Andi Kleen3022d732006-09-26 10:52:39 +020018
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +020019/* Standard copy_to_user with segment limit checking */
Frederic Weisbecker3c93ca02009-11-16 15:42:18 +010020ENTRY(_copy_to_user)
Jan Beulich8d379da2006-09-26 10:52:32 +020021 CFI_STARTPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 GET_THREAD_INFO(%rax)
23 movq %rdi,%rcx
24 addq %rdx,%rcx
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +020025 jc bad_to_user
Glauber Costa26ccb8a2008-06-24 11:19:35 -030026 cmpq TI_addr_limit(%rax),%rcx
Jiri Olsa26afb7c2011-05-12 16:30:30 +020027 ja bad_to_user
Borislav Petkovde2ff882015-01-13 01:38:17 +010028 ALTERNATIVE_2 "jmp copy_user_generic_unrolled", \
29 "jmp copy_user_generic_string", \
30 X86_FEATURE_REP_GOOD, \
31 "jmp copy_user_enhanced_fast_string", \
32 X86_FEATURE_ERMS
Jan Beulich8d379da2006-09-26 10:52:32 +020033 CFI_ENDPROC
Frederic Weisbecker3c93ca02009-11-16 15:42:18 +010034ENDPROC(_copy_to_user)
Andi Kleen7bcd3f32006-02-03 21:51:02 +010035
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +020036/* Standard copy_from_user with segment limit checking */
Arjan van de Ven9f0cf4a2009-09-26 14:33:01 +020037ENTRY(_copy_from_user)
Jan Beulich8d379da2006-09-26 10:52:32 +020038 CFI_STARTPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 GET_THREAD_INFO(%rax)
40 movq %rsi,%rcx
41 addq %rdx,%rcx
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +020042 jc bad_from_user
Glauber Costa26ccb8a2008-06-24 11:19:35 -030043 cmpq TI_addr_limit(%rax),%rcx
Jiri Olsa26afb7c2011-05-12 16:30:30 +020044 ja bad_from_user
Borislav Petkovde2ff882015-01-13 01:38:17 +010045 ALTERNATIVE_2 "jmp copy_user_generic_unrolled", \
46 "jmp copy_user_generic_string", \
47 X86_FEATURE_REP_GOOD, \
48 "jmp copy_user_enhanced_fast_string", \
49 X86_FEATURE_ERMS
Jan Beulich8d379da2006-09-26 10:52:32 +020050 CFI_ENDPROC
Arjan van de Ven9f0cf4a2009-09-26 14:33:01 +020051ENDPROC(_copy_from_user)
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +020052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 .section .fixup,"ax"
54 /* must zero dest */
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +020055ENTRY(bad_from_user)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056bad_from_user:
Jan Beulich8d379da2006-09-26 10:52:32 +020057 CFI_STARTPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 movl %edx,%ecx
59 xorl %eax,%eax
60 rep
61 stosb
62bad_to_user:
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +020063 movl %edx,%eax
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 ret
Jan Beulich8d379da2006-09-26 10:52:32 +020065 CFI_ENDPROC
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +020066ENDPROC(bad_from_user)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 .previous
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +020068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/*
Andi Kleen3022d732006-09-26 10:52:39 +020070 * copy_user_generic_unrolled - memory copy with exception handling.
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +020071 * This version is for CPUs like P4 that don't have efficient micro
72 * code for rep movsq
73 *
74 * Input:
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * rdi destination
76 * rsi source
77 * rdx count
78 *
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +020079 * Output:
Lucas De Marchi0d2eb442011-03-17 16:24:16 -030080 * eax uncopied bytes or 0 if successful.
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
Andi Kleen3022d732006-09-26 10:52:39 +020082ENTRY(copy_user_generic_unrolled)
Jan Beulich8d379da2006-09-26 10:52:32 +020083 CFI_STARTPROC
H. Peter Anvin63bcff22012-09-21 12:43:12 -070084 ASM_STAC
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +020085 cmpl $8,%edx
86 jb 20f /* less then 8 bytes, go to byte copy loop */
87 ALIGN_DESTINATION
88 movl %edx,%ecx
89 andl $63,%edx
90 shrl $6,%ecx
91 jz 17f
921: movq (%rsi),%r8
932: movq 1*8(%rsi),%r9
943: movq 2*8(%rsi),%r10
954: movq 3*8(%rsi),%r11
965: movq %r8,(%rdi)
976: movq %r9,1*8(%rdi)
987: movq %r10,2*8(%rdi)
998: movq %r11,3*8(%rdi)
1009: movq 4*8(%rsi),%r8
10110: movq 5*8(%rsi),%r9
10211: movq 6*8(%rsi),%r10
10312: movq 7*8(%rsi),%r11
10413: movq %r8,4*8(%rdi)
10514: movq %r9,5*8(%rdi)
10615: movq %r10,6*8(%rdi)
10716: movq %r11,7*8(%rdi)
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100108 leaq 64(%rsi),%rsi
109 leaq 64(%rdi),%rdi
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200110 decl %ecx
111 jnz 1b
11217: movl %edx,%ecx
113 andl $7,%edx
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100114 shrl $3,%ecx
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200115 jz 20f
11618: movq (%rsi),%r8
11719: movq %r8,(%rdi)
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100118 leaq 8(%rsi),%rsi
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200119 leaq 8(%rdi),%rdi
120 decl %ecx
121 jnz 18b
12220: andl %edx,%edx
123 jz 23f
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100124 movl %edx,%ecx
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +020012521: movb (%rsi),%al
12622: movb %al,(%rdi)
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100127 incq %rsi
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200128 incq %rdi
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100129 decl %ecx
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200130 jnz 21b
13123: xor %eax,%eax
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700132 ASM_CLAC
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100133 ret
134
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200135 .section .fixup,"ax"
13630: shll $6,%ecx
137 addl %ecx,%edx
138 jmp 60f
H. Peter Anvin661c8012013-11-20 12:50:51 -080013940: leal (%rdx,%rcx,8),%edx
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200140 jmp 60f
14150: movl %ecx,%edx
14260: jmp copy_user_handle_tail /* ecx is zerorest also */
143 .previous
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100144
H. Peter Anvin9732da82012-04-20 12:19:51 -0700145 _ASM_EXTABLE(1b,30b)
146 _ASM_EXTABLE(2b,30b)
147 _ASM_EXTABLE(3b,30b)
148 _ASM_EXTABLE(4b,30b)
149 _ASM_EXTABLE(5b,30b)
150 _ASM_EXTABLE(6b,30b)
151 _ASM_EXTABLE(7b,30b)
152 _ASM_EXTABLE(8b,30b)
153 _ASM_EXTABLE(9b,30b)
154 _ASM_EXTABLE(10b,30b)
155 _ASM_EXTABLE(11b,30b)
156 _ASM_EXTABLE(12b,30b)
157 _ASM_EXTABLE(13b,30b)
158 _ASM_EXTABLE(14b,30b)
159 _ASM_EXTABLE(15b,30b)
160 _ASM_EXTABLE(16b,30b)
161 _ASM_EXTABLE(18b,40b)
162 _ASM_EXTABLE(19b,40b)
163 _ASM_EXTABLE(21b,50b)
164 _ASM_EXTABLE(22b,50b)
Jan Beulich8d379da2006-09-26 10:52:32 +0200165 CFI_ENDPROC
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200166ENDPROC(copy_user_generic_unrolled)
Jan Beulich8d379da2006-09-26 10:52:32 +0200167
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200168/* Some CPUs run faster using the string copy instructions.
169 * This is also a lot simpler. Use them when possible.
170 *
171 * Only 4GB of copy is supported. This shouldn't be a problem
172 * because the kernel normally only writes from/to page sized chunks
173 * even if user space passed a longer buffer.
174 * And more would be dangerous because both Intel and AMD have
175 * errata with rep movsq > 4GB. If someone feels the need to fix
176 * this please consider this.
177 *
178 * Input:
179 * rdi destination
180 * rsi source
181 * rdx count
182 *
183 * Output:
184 * eax uncopied bytes or 0 if successful.
185 */
Andi Kleen3022d732006-09-26 10:52:39 +0200186ENTRY(copy_user_generic_string)
Jan Beulich8d379da2006-09-26 10:52:32 +0200187 CFI_STARTPROC
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700188 ASM_STAC
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200189 cmpl $8,%edx
190 jb 2f /* less than 8 bytes, go to byte copy loop */
191 ALIGN_DESTINATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 movl %edx,%ecx
193 shrl $3,%ecx
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200194 andl $7,%edx
1951: rep
Andi Kleen3022d732006-09-26 10:52:39 +0200196 movsq
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +02001972: movl %edx,%ecx
1983: rep
199 movsb
Fenghua Yuf4cb1cc2013-11-16 12:37:01 -0800200 xorl %eax,%eax
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700201 ASM_CLAC
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100202 ret
Andi Kleen3022d732006-09-26 10:52:39 +0200203
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200204 .section .fixup,"ax"
H. Peter Anvin661c8012013-11-20 12:50:51 -080020511: leal (%rdx,%rcx,8),%ecx
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +020020612: movl %ecx,%edx /* ecx is zerorest also */
207 jmp copy_user_handle_tail
208 .previous
Andi Kleen2cbc9ee2006-01-11 22:44:45 +0100209
H. Peter Anvin9732da82012-04-20 12:19:51 -0700210 _ASM_EXTABLE(1b,11b)
211 _ASM_EXTABLE(3b,12b)
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200212 CFI_ENDPROC
213ENDPROC(copy_user_generic_string)
Fenghua Yu4307bec2011-05-17 15:29:15 -0700214
215/*
216 * Some CPUs are adding enhanced REP MOVSB/STOSB instructions.
217 * It's recommended to use enhanced REP MOVSB/STOSB if it's enabled.
218 *
219 * Input:
220 * rdi destination
221 * rsi source
222 * rdx count
223 *
224 * Output:
225 * eax uncopied bytes or 0 if successful.
226 */
227ENTRY(copy_user_enhanced_fast_string)
228 CFI_STARTPROC
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700229 ASM_STAC
Fenghua Yu4307bec2011-05-17 15:29:15 -0700230 movl %edx,%ecx
2311: rep
232 movsb
Fenghua Yuf4cb1cc2013-11-16 12:37:01 -0800233 xorl %eax,%eax
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700234 ASM_CLAC
Fenghua Yu4307bec2011-05-17 15:29:15 -0700235 ret
236
237 .section .fixup,"ax"
23812: movl %ecx,%edx /* ecx is zerorest also */
239 jmp copy_user_handle_tail
240 .previous
241
H. Peter Anvin9732da82012-04-20 12:19:51 -0700242 _ASM_EXTABLE(1b,12b)
Fenghua Yu4307bec2011-05-17 15:29:15 -0700243 CFI_ENDPROC
244ENDPROC(copy_user_enhanced_fast_string)
Borislav Petkovb41e6ec2015-05-13 19:42:24 +0200245
246/*
247 * copy_user_nocache - Uncached memory copy with exception handling
248 * This will force destination/source out of cache for more performance.
249 */
250ENTRY(__copy_user_nocache)
251 CFI_STARTPROC
252 ASM_STAC
253 cmpl $8,%edx
254 jb 20f /* less then 8 bytes, go to byte copy loop */
255 ALIGN_DESTINATION
256 movl %edx,%ecx
257 andl $63,%edx
258 shrl $6,%ecx
259 jz 17f
2601: movq (%rsi),%r8
2612: movq 1*8(%rsi),%r9
2623: movq 2*8(%rsi),%r10
2634: movq 3*8(%rsi),%r11
2645: movnti %r8,(%rdi)
2656: movnti %r9,1*8(%rdi)
2667: movnti %r10,2*8(%rdi)
2678: movnti %r11,3*8(%rdi)
2689: movq 4*8(%rsi),%r8
26910: movq 5*8(%rsi),%r9
27011: movq 6*8(%rsi),%r10
27112: movq 7*8(%rsi),%r11
27213: movnti %r8,4*8(%rdi)
27314: movnti %r9,5*8(%rdi)
27415: movnti %r10,6*8(%rdi)
27516: movnti %r11,7*8(%rdi)
276 leaq 64(%rsi),%rsi
277 leaq 64(%rdi),%rdi
278 decl %ecx
279 jnz 1b
28017: movl %edx,%ecx
281 andl $7,%edx
282 shrl $3,%ecx
283 jz 20f
28418: movq (%rsi),%r8
28519: movnti %r8,(%rdi)
286 leaq 8(%rsi),%rsi
287 leaq 8(%rdi),%rdi
288 decl %ecx
289 jnz 18b
29020: andl %edx,%edx
291 jz 23f
292 movl %edx,%ecx
29321: movb (%rsi),%al
29422: movb %al,(%rdi)
295 incq %rsi
296 incq %rdi
297 decl %ecx
298 jnz 21b
29923: xorl %eax,%eax
300 ASM_CLAC
301 sfence
302 ret
303
304 .section .fixup,"ax"
30530: shll $6,%ecx
306 addl %ecx,%edx
307 jmp 60f
30840: lea (%rdx,%rcx,8),%rdx
309 jmp 60f
31050: movl %ecx,%edx
31160: sfence
312 jmp copy_user_handle_tail
313 .previous
314
315 _ASM_EXTABLE(1b,30b)
316 _ASM_EXTABLE(2b,30b)
317 _ASM_EXTABLE(3b,30b)
318 _ASM_EXTABLE(4b,30b)
319 _ASM_EXTABLE(5b,30b)
320 _ASM_EXTABLE(6b,30b)
321 _ASM_EXTABLE(7b,30b)
322 _ASM_EXTABLE(8b,30b)
323 _ASM_EXTABLE(9b,30b)
324 _ASM_EXTABLE(10b,30b)
325 _ASM_EXTABLE(11b,30b)
326 _ASM_EXTABLE(12b,30b)
327 _ASM_EXTABLE(13b,30b)
328 _ASM_EXTABLE(14b,30b)
329 _ASM_EXTABLE(15b,30b)
330 _ASM_EXTABLE(16b,30b)
331 _ASM_EXTABLE(18b,40b)
332 _ASM_EXTABLE(19b,40b)
333 _ASM_EXTABLE(21b,50b)
334 _ASM_EXTABLE(22b,50b)
335 CFI_ENDPROC
336ENDPROC(__copy_user_nocache)