blob: 2661fad0582716f780af9904dc5b7c62199a5c58 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Copyright 2002 Andi Kleen, SuSE Labs */
Jan Beulich8d379da2006-09-26 10:52:32 +02002
Jan Beulich8d379da2006-09-26 10:52:32 +02003#include <linux/linkage.h>
Fenghua Yu2f19e062011-05-17 15:29:18 -07004#include <asm/cpufeature.h>
5#include <asm/alternative-asm.h>
Jan Beulich8d379da2006-09-26 10:52:32 +02006
Borislav Petkov84d95ad2015-02-04 08:57:00 +01007.weak memset
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009/*
Fenghua Yu2f19e062011-05-17 15:29:18 -070010 * ISO C memset - set a memory block to a byte value. This function uses fast
11 * string to get better performance than the original function. The code is
12 * simpler and shorter than the orignal function as well.
Borislav Petkov84d95ad2015-02-04 08:57:00 +010013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * rdi destination
Borislav Petkov84d95ad2015-02-04 08:57:00 +010015 * rsi value (char)
16 * rdx count (bytes)
17 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * rax original destination
Borislav Petkov84d95ad2015-02-04 08:57:00 +010019 */
20ENTRY(memset)
21ENTRY(__memset)
22 /*
23 * Some CPUs support enhanced REP MOVSB/STOSB feature. It is recommended
24 * to use it when possible. If not available, use fast string instructions.
25 *
26 * Otherwise, use original memset function.
27 */
28 ALTERNATIVE_2 "jmp memset_orig", "", X86_FEATURE_REP_GOOD, \
29 "jmp memset_erms", X86_FEATURE_ERMS
30
Jan Beulich8d379da2006-09-26 10:52:32 +020031 movq %rdi,%r9
Jan Beulich5d7244e2012-01-05 16:10:42 +000032 movq %rdx,%rcx
33 andl $7,%edx
34 shrq $3,%rcx
Jan Beulich8d379da2006-09-26 10:52:32 +020035 /* expand byte value */
36 movzbl %sil,%esi
37 movabs $0x0101010101010101,%rax
Jan Beulich5d7244e2012-01-05 16:10:42 +000038 imulq %rsi,%rax
Jan Beulich8d379da2006-09-26 10:52:32 +020039 rep stosq
Jan Beulich5d7244e2012-01-05 16:10:42 +000040 movl %edx,%ecx
Jan Beulich8d379da2006-09-26 10:52:32 +020041 rep stosb
42 movq %r9,%rax
43 ret
Borislav Petkov84d95ad2015-02-04 08:57:00 +010044ENDPROC(memset)
45ENDPROC(__memset)
Jan Beulich8d379da2006-09-26 10:52:32 +020046
Fenghua Yu2f19e062011-05-17 15:29:18 -070047/*
48 * ISO C memset - set a memory block to a byte value. This function uses
49 * enhanced rep stosb to override the fast string function.
50 * The code is simpler and shorter than the fast string function as well.
51 *
52 * rdi destination
53 * rsi value (char)
54 * rdx count (bytes)
55 *
56 * rax original destination
57 */
Borislav Petkov84d95ad2015-02-04 08:57:00 +010058ENTRY(memset_erms)
Fenghua Yu2f19e062011-05-17 15:29:18 -070059 movq %rdi,%r9
60 movb %sil,%al
Jan Beulich5d7244e2012-01-05 16:10:42 +000061 movq %rdx,%rcx
Fenghua Yu2f19e062011-05-17 15:29:18 -070062 rep stosb
63 movq %r9,%rax
64 ret
Borislav Petkov84d95ad2015-02-04 08:57:00 +010065ENDPROC(memset_erms)
Fenghua Yu2f19e062011-05-17 15:29:18 -070066
Borislav Petkov84d95ad2015-02-04 08:57:00 +010067ENTRY(memset_orig)
Andi Kleen7bcd3f32006-02-03 21:51:02 +010068 movq %rdi,%r10
Andi Kleen7bcd3f32006-02-03 21:51:02 +010069
70 /* expand byte value */
71 movzbl %sil,%ecx
72 movabs $0x0101010101010101,%rax
Jan Beulich5d7244e2012-01-05 16:10:42 +000073 imulq %rcx,%rax
Andi Kleen7bcd3f32006-02-03 21:51:02 +010074
75 /* align dst */
76 movl %edi,%r9d
77 andl $7,%r9d
78 jnz .Lbad_alignment
79.Lafter_bad_alignment:
80
Jan Beulich5d7244e2012-01-05 16:10:42 +000081 movq %rdx,%rcx
82 shrq $6,%rcx
Andi Kleen7bcd3f32006-02-03 21:51:02 +010083 jz .Lhandle_tail
84
85 .p2align 4
86.Lloop_64:
Jan Beulich5d7244e2012-01-05 16:10:42 +000087 decq %rcx
Andi Kleen7bcd3f32006-02-03 21:51:02 +010088 movq %rax,(%rdi)
89 movq %rax,8(%rdi)
90 movq %rax,16(%rdi)
91 movq %rax,24(%rdi)
92 movq %rax,32(%rdi)
93 movq %rax,40(%rdi)
94 movq %rax,48(%rdi)
95 movq %rax,56(%rdi)
96 leaq 64(%rdi),%rdi
97 jnz .Lloop_64
98
99 /* Handle tail in loops. The loops should be faster than hard
100 to predict jump tables. */
101 .p2align 4
102.Lhandle_tail:
Jan Beulich5d7244e2012-01-05 16:10:42 +0000103 movl %edx,%ecx
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100104 andl $63&(~7),%ecx
105 jz .Lhandle_7
106 shrl $3,%ecx
107 .p2align 4
108.Lloop_8:
109 decl %ecx
110 movq %rax,(%rdi)
111 leaq 8(%rdi),%rdi
112 jnz .Lloop_8
113
114.Lhandle_7:
Jan Beulich5d7244e2012-01-05 16:10:42 +0000115 andl $7,%edx
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100116 jz .Lende
117 .p2align 4
118.Lloop_1:
Jan Beulich5d7244e2012-01-05 16:10:42 +0000119 decl %edx
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100120 movb %al,(%rdi)
121 leaq 1(%rdi),%rdi
122 jnz .Lloop_1
123
124.Lende:
125 movq %r10,%rax
126 ret
127
128.Lbad_alignment:
Jan Beulich5d7244e2012-01-05 16:10:42 +0000129 cmpq $7,%rdx
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100130 jbe .Lhandle_7
131 movq %rax,(%rdi) /* unaligned store */
132 movq $8,%r8
133 subq %r9,%r8
134 addq %r8,%rdi
Jan Beulich5d7244e2012-01-05 16:10:42 +0000135 subq %r8,%rdx
Andi Kleen7bcd3f32006-02-03 21:51:02 +0100136 jmp .Lafter_bad_alignment
Jan Beulich8d379da2006-09-26 10:52:32 +0200137.Lfinal:
Borislav Petkov84d95ad2015-02-04 08:57:00 +0100138ENDPROC(memset_orig)