Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Copyright 2002 Andi Kleen, SuSE Labs */ |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 2 | |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 3 | #include <linux/linkage.h> |
Fenghua Yu | 2f19e06 | 2011-05-17 15:29:18 -0700 | [diff] [blame] | 4 | #include <asm/cpufeature.h> |
| 5 | #include <asm/alternative-asm.h> |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 6 | |
Borislav Petkov | 84d95ad | 2015-02-04 08:57:00 +0100 | [diff] [blame] | 7 | .weak memset |
| 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | /* |
Fenghua Yu | 2f19e06 | 2011-05-17 15:29:18 -0700 | [diff] [blame] | 10 | * 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 Petkov | 84d95ad | 2015-02-04 08:57:00 +0100 | [diff] [blame] | 13 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * rdi destination |
Borislav Petkov | 84d95ad | 2015-02-04 08:57:00 +0100 | [diff] [blame] | 15 | * rsi value (char) |
| 16 | * rdx count (bytes) |
| 17 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | * rax original destination |
Borislav Petkov | 84d95ad | 2015-02-04 08:57:00 +0100 | [diff] [blame] | 19 | */ |
| 20 | ENTRY(memset) |
| 21 | ENTRY(__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 Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 31 | movq %rdi,%r9 |
Jan Beulich | 5d7244e | 2012-01-05 16:10:42 +0000 | [diff] [blame] | 32 | movq %rdx,%rcx |
| 33 | andl $7,%edx |
| 34 | shrq $3,%rcx |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 35 | /* expand byte value */ |
| 36 | movzbl %sil,%esi |
| 37 | movabs $0x0101010101010101,%rax |
Jan Beulich | 5d7244e | 2012-01-05 16:10:42 +0000 | [diff] [blame] | 38 | imulq %rsi,%rax |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 39 | rep stosq |
Jan Beulich | 5d7244e | 2012-01-05 16:10:42 +0000 | [diff] [blame] | 40 | movl %edx,%ecx |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 41 | rep stosb |
| 42 | movq %r9,%rax |
| 43 | ret |
Borislav Petkov | 84d95ad | 2015-02-04 08:57:00 +0100 | [diff] [blame] | 44 | ENDPROC(memset) |
| 45 | ENDPROC(__memset) |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 46 | |
Fenghua Yu | 2f19e06 | 2011-05-17 15:29:18 -0700 | [diff] [blame] | 47 | /* |
| 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 Petkov | 84d95ad | 2015-02-04 08:57:00 +0100 | [diff] [blame] | 58 | ENTRY(memset_erms) |
Fenghua Yu | 2f19e06 | 2011-05-17 15:29:18 -0700 | [diff] [blame] | 59 | movq %rdi,%r9 |
| 60 | movb %sil,%al |
Jan Beulich | 5d7244e | 2012-01-05 16:10:42 +0000 | [diff] [blame] | 61 | movq %rdx,%rcx |
Fenghua Yu | 2f19e06 | 2011-05-17 15:29:18 -0700 | [diff] [blame] | 62 | rep stosb |
| 63 | movq %r9,%rax |
| 64 | ret |
Borislav Petkov | 84d95ad | 2015-02-04 08:57:00 +0100 | [diff] [blame] | 65 | ENDPROC(memset_erms) |
Fenghua Yu | 2f19e06 | 2011-05-17 15:29:18 -0700 | [diff] [blame] | 66 | |
Borislav Petkov | 84d95ad | 2015-02-04 08:57:00 +0100 | [diff] [blame] | 67 | ENTRY(memset_orig) |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 68 | movq %rdi,%r10 |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 69 | |
| 70 | /* expand byte value */ |
| 71 | movzbl %sil,%ecx |
| 72 | movabs $0x0101010101010101,%rax |
Jan Beulich | 5d7244e | 2012-01-05 16:10:42 +0000 | [diff] [blame] | 73 | imulq %rcx,%rax |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 74 | |
| 75 | /* align dst */ |
| 76 | movl %edi,%r9d |
| 77 | andl $7,%r9d |
| 78 | jnz .Lbad_alignment |
| 79 | .Lafter_bad_alignment: |
| 80 | |
Jan Beulich | 5d7244e | 2012-01-05 16:10:42 +0000 | [diff] [blame] | 81 | movq %rdx,%rcx |
| 82 | shrq $6,%rcx |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 83 | jz .Lhandle_tail |
| 84 | |
| 85 | .p2align 4 |
| 86 | .Lloop_64: |
Jan Beulich | 5d7244e | 2012-01-05 16:10:42 +0000 | [diff] [blame] | 87 | decq %rcx |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 88 | 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 Beulich | 5d7244e | 2012-01-05 16:10:42 +0000 | [diff] [blame] | 103 | movl %edx,%ecx |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 104 | 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 Beulich | 5d7244e | 2012-01-05 16:10:42 +0000 | [diff] [blame] | 115 | andl $7,%edx |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 116 | jz .Lende |
| 117 | .p2align 4 |
| 118 | .Lloop_1: |
Jan Beulich | 5d7244e | 2012-01-05 16:10:42 +0000 | [diff] [blame] | 119 | decl %edx |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 120 | 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 Beulich | 5d7244e | 2012-01-05 16:10:42 +0000 | [diff] [blame] | 129 | cmpq $7,%rdx |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 130 | jbe .Lhandle_7 |
| 131 | movq %rax,(%rdi) /* unaligned store */ |
| 132 | movq $8,%r8 |
| 133 | subq %r9,%r8 |
| 134 | addq %r8,%rdi |
Jan Beulich | 5d7244e | 2012-01-05 16:10:42 +0000 | [diff] [blame] | 135 | subq %r8,%rdx |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 136 | jmp .Lafter_bad_alignment |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 137 | .Lfinal: |
Borislav Petkov | 84d95ad | 2015-02-04 08:57:00 +0100 | [diff] [blame] | 138 | ENDPROC(memset_orig) |