blob: a30ca15be21c8a182b3bb937ac612d1863a046ee [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>
11
Andi Kleen7bcd3f32006-02-03 21:51:02 +010012#define FIX_ALIGNMENT 1
13
Andi Kleen3022d732006-09-26 10:52:39 +020014#include <asm/current.h>
15#include <asm/asm-offsets.h>
16#include <asm/thread_info.h>
17#include <asm/cpufeature.h>
Fenghua Yu4307bec2011-05-17 15:29:15 -070018#include <asm/alternative-asm.h>
H. Peter Anvin9732da82012-04-20 12:19:51 -070019#include <asm/asm.h>
H. Peter Anvin63bcff22012-09-21 12:43:12 -070020#include <asm/smap.h>
Andi Kleen3022d732006-09-26 10:52:39 +020021
Fenghua Yu4307bec2011-05-17 15:29:15 -070022/*
23 * By placing feature2 after feature1 in altinstructions section, we logically
24 * implement:
25 * If CPU has feature2, jmp to alt2 is used
26 * else if CPU has feature1, jmp to alt1 is used
27 * else jmp to orig is used.
28 */
29 .macro ALTERNATIVE_JUMP feature1,feature2,orig,alt1,alt2
Andi Kleen3022d732006-09-26 10:52:39 +0200300:
31 .byte 0xe9 /* 32bit jump */
32 .long \orig-1f /* by default jump to orig */
331:
34 .section .altinstr_replacement,"ax"
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200352: .byte 0xe9 /* near jump with 32bit immediate */
Fenghua Yu4307bec2011-05-17 15:29:15 -070036 .long \alt1-1b /* offset */ /* or alternatively to alt1 */
373: .byte 0xe9 /* near jump with 32bit immediate */
38 .long \alt2-1b /* offset */ /* or alternatively to alt2 */
Andi Kleen3022d732006-09-26 10:52:39 +020039 .previous
Fenghua Yu4307bec2011-05-17 15:29:15 -070040
Andi Kleen3022d732006-09-26 10:52:39 +020041 .section .altinstructions,"a"
Fenghua Yu4307bec2011-05-17 15:29:15 -070042 altinstruction_entry 0b,2b,\feature1,5,5
43 altinstruction_entry 0b,3b,\feature2,5,5
Andi Kleen3022d732006-09-26 10:52:39 +020044 .previous
45 .endm
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +020047 .macro ALIGN_DESTINATION
48#ifdef FIX_ALIGNMENT
49 /* check for bad alignment of destination */
50 movl %edi,%ecx
51 andl $7,%ecx
52 jz 102f /* already aligned */
53 subl $8,%ecx
54 negl %ecx
55 subl %ecx,%edx
56100: movb (%rsi),%al
57101: movb %al,(%rdi)
58 incq %rsi
59 incq %rdi
60 decl %ecx
61 jnz 100b
62102:
63 .section .fixup,"ax"
Vitaly Mayatskikhafd962a2008-07-30 13:30:14 +020064103: addl %ecx,%edx /* ecx is zerorest also */
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +020065 jmp copy_user_handle_tail
66 .previous
67
H. Peter Anvin9732da82012-04-20 12:19:51 -070068 _ASM_EXTABLE(100b,103b)
69 _ASM_EXTABLE(101b,103b)
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +020070#endif
71 .endm
72
73/* Standard copy_to_user with segment limit checking */
Frederic Weisbecker3c93ca02009-11-16 15:42:18 +010074ENTRY(_copy_to_user)
Jan Beulich8d379da2006-09-26 10:52:32 +020075 CFI_STARTPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 GET_THREAD_INFO(%rax)
77 movq %rdi,%rcx
78 addq %rdx,%rcx
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +020079 jc bad_to_user
Glauber Costa26ccb8a2008-06-24 11:19:35 -030080 cmpq TI_addr_limit(%rax),%rcx
Jiri Olsa26afb7c2011-05-12 16:30:30 +020081 ja bad_to_user
Fenghua Yu4307bec2011-05-17 15:29:15 -070082 ALTERNATIVE_JUMP X86_FEATURE_REP_GOOD,X86_FEATURE_ERMS, \
83 copy_user_generic_unrolled,copy_user_generic_string, \
84 copy_user_enhanced_fast_string
Jan Beulich8d379da2006-09-26 10:52:32 +020085 CFI_ENDPROC
Frederic Weisbecker3c93ca02009-11-16 15:42:18 +010086ENDPROC(_copy_to_user)
Andi Kleen7bcd3f32006-02-03 21:51:02 +010087
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +020088/* Standard copy_from_user with segment limit checking */
Arjan van de Ven9f0cf4a2009-09-26 14:33:01 +020089ENTRY(_copy_from_user)
Jan Beulich8d379da2006-09-26 10:52:32 +020090 CFI_STARTPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 GET_THREAD_INFO(%rax)
92 movq %rsi,%rcx
93 addq %rdx,%rcx
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +020094 jc bad_from_user
Glauber Costa26ccb8a2008-06-24 11:19:35 -030095 cmpq TI_addr_limit(%rax),%rcx
Jiri Olsa26afb7c2011-05-12 16:30:30 +020096 ja bad_from_user
Fenghua Yu4307bec2011-05-17 15:29:15 -070097 ALTERNATIVE_JUMP X86_FEATURE_REP_GOOD,X86_FEATURE_ERMS, \
98 copy_user_generic_unrolled,copy_user_generic_string, \
99 copy_user_enhanced_fast_string
Jan Beulich8d379da2006-09-26 10:52:32 +0200100 CFI_ENDPROC
Arjan van de Ven9f0cf4a2009-09-26 14:33:01 +0200101ENDPROC(_copy_from_user)
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 .section .fixup,"ax"
104 /* must zero dest */
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200105ENTRY(bad_from_user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106bad_from_user:
Jan Beulich8d379da2006-09-26 10:52:32 +0200107 CFI_STARTPROC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 movl %edx,%ecx
109 xorl %eax,%eax
110 rep
111 stosb
112bad_to_user:
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200113 movl %edx,%eax
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 ret
Jan Beulich8d379da2006-09-26 10:52:32 +0200115 CFI_ENDPROC
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200116ENDPROC(bad_from_user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 .previous
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/*
Andi Kleen3022d732006-09-26 10:52:39 +0200120 * copy_user_generic_unrolled - memory copy with exception handling.
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200121 * This version is for CPUs like P4 that don't have efficient micro
122 * code for rep movsq
123 *
124 * Input:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 * rdi destination
126 * rsi source
127 * rdx count
128 *
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200129 * Output:
Lucas De Marchi0d2eb442011-03-17 16:24:16 -0300130 * eax uncopied bytes or 0 if successful.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 */
Andi Kleen3022d732006-09-26 10:52:39 +0200132ENTRY(copy_user_generic_unrolled)
Jan Beulich8d379da2006-09-26 10:52:32 +0200133 CFI_STARTPROC
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700134 ASM_STAC
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200135 cmpl $8,%edx
136 jb 20f /* less then 8 bytes, go to byte copy loop */
137 ALIGN_DESTINATION
138 movl %edx,%ecx
139 andl $63,%edx
140 shrl $6,%ecx
141 jz 17f
1421: movq (%rsi),%r8
1432: movq 1*8(%rsi),%r9
1443: movq 2*8(%rsi),%r10
1454: movq 3*8(%rsi),%r11
1465: movq %r8,(%rdi)
1476: movq %r9,1*8(%rdi)
1487: movq %r10,2*8(%rdi)
1498: movq %r11,3*8(%rdi)
1509: movq 4*8(%rsi),%r8
15110: movq 5*8(%rsi),%r9
15211: movq 6*8(%rsi),%r10
15312: movq 7*8(%rsi),%r11
15413: movq %r8,4*8(%rdi)
15514: movq %r9,5*8(%rdi)
15615: movq %r10,6*8(%rdi)
15716: movq %r11,7*8(%rdi)
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100158 leaq 64(%rsi),%rsi
159 leaq 64(%rdi),%rdi
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200160 decl %ecx
161 jnz 1b
16217: movl %edx,%ecx
163 andl $7,%edx
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100164 shrl $3,%ecx
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200165 jz 20f
16618: movq (%rsi),%r8
16719: movq %r8,(%rdi)
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100168 leaq 8(%rsi),%rsi
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200169 leaq 8(%rdi),%rdi
170 decl %ecx
171 jnz 18b
17220: andl %edx,%edx
173 jz 23f
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100174 movl %edx,%ecx
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +020017521: movb (%rsi),%al
17622: movb %al,(%rdi)
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100177 incq %rsi
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200178 incq %rdi
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100179 decl %ecx
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200180 jnz 21b
18123: xor %eax,%eax
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700182 ASM_CLAC
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100183 ret
184
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200185 .section .fixup,"ax"
18630: shll $6,%ecx
187 addl %ecx,%edx
188 jmp 60f
Jeremy Fitzhardinge27cb0a72008-07-10 12:52:52 -070018940: lea (%rdx,%rcx,8),%rdx
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200190 jmp 60f
19150: movl %ecx,%edx
19260: jmp copy_user_handle_tail /* ecx is zerorest also */
193 .previous
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100194
H. Peter Anvin9732da82012-04-20 12:19:51 -0700195 _ASM_EXTABLE(1b,30b)
196 _ASM_EXTABLE(2b,30b)
197 _ASM_EXTABLE(3b,30b)
198 _ASM_EXTABLE(4b,30b)
199 _ASM_EXTABLE(5b,30b)
200 _ASM_EXTABLE(6b,30b)
201 _ASM_EXTABLE(7b,30b)
202 _ASM_EXTABLE(8b,30b)
203 _ASM_EXTABLE(9b,30b)
204 _ASM_EXTABLE(10b,30b)
205 _ASM_EXTABLE(11b,30b)
206 _ASM_EXTABLE(12b,30b)
207 _ASM_EXTABLE(13b,30b)
208 _ASM_EXTABLE(14b,30b)
209 _ASM_EXTABLE(15b,30b)
210 _ASM_EXTABLE(16b,30b)
211 _ASM_EXTABLE(18b,40b)
212 _ASM_EXTABLE(19b,40b)
213 _ASM_EXTABLE(21b,50b)
214 _ASM_EXTABLE(22b,50b)
Jan Beulich8d379da2006-09-26 10:52:32 +0200215 CFI_ENDPROC
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200216ENDPROC(copy_user_generic_unrolled)
Jan Beulich8d379da2006-09-26 10:52:32 +0200217
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200218/* Some CPUs run faster using the string copy instructions.
219 * This is also a lot simpler. Use them when possible.
220 *
221 * Only 4GB of copy is supported. This shouldn't be a problem
222 * because the kernel normally only writes from/to page sized chunks
223 * even if user space passed a longer buffer.
224 * And more would be dangerous because both Intel and AMD have
225 * errata with rep movsq > 4GB. If someone feels the need to fix
226 * this please consider this.
227 *
228 * Input:
229 * rdi destination
230 * rsi source
231 * rdx count
232 *
233 * Output:
234 * eax uncopied bytes or 0 if successful.
235 */
Andi Kleen3022d732006-09-26 10:52:39 +0200236ENTRY(copy_user_generic_string)
Jan Beulich8d379da2006-09-26 10:52:32 +0200237 CFI_STARTPROC
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700238 ASM_STAC
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200239 andl %edx,%edx
240 jz 4f
241 cmpl $8,%edx
242 jb 2f /* less than 8 bytes, go to byte copy loop */
243 ALIGN_DESTINATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 movl %edx,%ecx
245 shrl $3,%ecx
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200246 andl $7,%edx
2471: rep
Andi Kleen3022d732006-09-26 10:52:39 +0200248 movsq
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +02002492: movl %edx,%ecx
2503: rep
251 movsb
2524: xorl %eax,%eax
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700253 ASM_CLAC
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100254 ret
Andi Kleen3022d732006-09-26 10:52:39 +0200255
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200256 .section .fixup,"ax"
Jeremy Fitzhardinge27cb0a72008-07-10 12:52:52 -070025711: lea (%rdx,%rcx,8),%rcx
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +020025812: movl %ecx,%edx /* ecx is zerorest also */
259 jmp copy_user_handle_tail
260 .previous
Andi Kleen2cbc9ee2006-01-11 22:44:45 +0100261
H. Peter Anvin9732da82012-04-20 12:19:51 -0700262 _ASM_EXTABLE(1b,11b)
263 _ASM_EXTABLE(3b,12b)
Vitaly Mayatskikhad2fc2c2008-07-02 15:53:13 +0200264 CFI_ENDPROC
265ENDPROC(copy_user_generic_string)
Fenghua Yu4307bec2011-05-17 15:29:15 -0700266
267/*
268 * Some CPUs are adding enhanced REP MOVSB/STOSB instructions.
269 * It's recommended to use enhanced REP MOVSB/STOSB if it's enabled.
270 *
271 * Input:
272 * rdi destination
273 * rsi source
274 * rdx count
275 *
276 * Output:
277 * eax uncopied bytes or 0 if successful.
278 */
279ENTRY(copy_user_enhanced_fast_string)
280 CFI_STARTPROC
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700281 ASM_STAC
Fenghua Yu4307bec2011-05-17 15:29:15 -0700282 andl %edx,%edx
283 jz 2f
284 movl %edx,%ecx
2851: rep
286 movsb
2872: xorl %eax,%eax
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700288 ASM_CLAC
Fenghua Yu4307bec2011-05-17 15:29:15 -0700289 ret
290
291 .section .fixup,"ax"
29212: movl %ecx,%edx /* ecx is zerorest also */
293 jmp copy_user_handle_tail
294 .previous
295
H. Peter Anvin9732da82012-04-20 12:19:51 -0700296 _ASM_EXTABLE(1b,12b)
Fenghua Yu4307bec2011-05-17 15:29:15 -0700297 CFI_ENDPROC
298ENDPROC(copy_user_enhanced_fast_string)