Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Copyright 2002 Andi Kleen */ |
Dave Jones | 038b0a6 | 2006-10-04 03:38:54 -0400 | [diff] [blame] | 2 | |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 3 | #include <linux/linkage.h> |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 4 | |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 5 | #include <asm/cpufeature.h> |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 6 | #include <asm/dwarf2.h> |
Fenghua Yu | 101068c | 2011-05-17 15:29:16 -0700 | [diff] [blame] | 7 | #include <asm/alternative-asm.h> |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | /* |
| 10 | * memcpy - Copy a memory block. |
| 11 | * |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 12 | * Input: |
| 13 | * rdi destination |
| 14 | * rsi source |
| 15 | * rdx count |
| 16 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * Output: |
| 18 | * rax original destination |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 19 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 21 | /* |
| 22 | * memcpy_c() - fast string ops (REP MOVSQ) based variant. |
| 23 | * |
Jan Beulich | 7269e88 | 2009-12-18 16:16:03 +0000 | [diff] [blame] | 24 | * This gets patched over the unrolled variant (below) via the |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 25 | * alternative instructions framework: |
| 26 | */ |
Jan Beulich | 7269e88 | 2009-12-18 16:16:03 +0000 | [diff] [blame] | 27 | .section .altinstr_replacement, "ax", @progbits |
| 28 | .Lmemcpy_c: |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 29 | movq %rdi, %rax |
| 30 | |
| 31 | movl %edx, %ecx |
| 32 | shrl $3, %ecx |
| 33 | andl $7, %edx |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 34 | rep movsq |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 35 | movl %edx, %ecx |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 36 | rep movsb |
| 37 | ret |
Jan Beulich | 7269e88 | 2009-12-18 16:16:03 +0000 | [diff] [blame] | 38 | .Lmemcpy_e: |
| 39 | .previous |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 40 | |
Fenghua Yu | 101068c | 2011-05-17 15:29:16 -0700 | [diff] [blame] | 41 | /* |
| 42 | * memcpy_c_e() - enhanced fast string memcpy. This is faster and simpler than |
| 43 | * memcpy_c. Use memcpy_c_e when possible. |
| 44 | * |
| 45 | * This gets patched over the unrolled variant (below) via the |
| 46 | * alternative instructions framework: |
| 47 | */ |
| 48 | .section .altinstr_replacement, "ax", @progbits |
| 49 | .Lmemcpy_c_e: |
| 50 | movq %rdi, %rax |
| 51 | |
| 52 | movl %edx, %ecx |
| 53 | rep movsb |
| 54 | ret |
| 55 | .Lmemcpy_e_e: |
| 56 | .previous |
| 57 | |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 58 | ENTRY(__memcpy) |
| 59 | ENTRY(memcpy) |
| 60 | CFI_STARTPROC |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 61 | movq %rdi, %rax |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 62 | |
Ma Ling | 59daa70 | 2010-06-29 03:24:25 +0800 | [diff] [blame] | 63 | /* |
| 64 | * Use 32bit CMP here to avoid long NOP padding. |
| 65 | */ |
| 66 | cmp $0x20, %edx |
| 67 | jb .Lhandle_tail |
| 68 | |
| 69 | /* |
Bart Van Assche | 9de4966 | 2011-05-01 14:09:21 +0200 | [diff] [blame] | 70 | * We check whether memory false dependence could occur, |
Ma Ling | 59daa70 | 2010-06-29 03:24:25 +0800 | [diff] [blame] | 71 | * then jump to corresponding copy mode. |
| 72 | */ |
| 73 | cmp %dil, %sil |
| 74 | jl .Lcopy_backward |
| 75 | subl $0x20, %edx |
| 76 | .Lcopy_forward_loop: |
| 77 | subq $0x20, %rdx |
| 78 | |
| 79 | /* |
| 80 | * Move in blocks of 4x8 bytes: |
| 81 | */ |
| 82 | movq 0*8(%rsi), %r8 |
| 83 | movq 1*8(%rsi), %r9 |
| 84 | movq 2*8(%rsi), %r10 |
| 85 | movq 3*8(%rsi), %r11 |
| 86 | leaq 4*8(%rsi), %rsi |
| 87 | |
| 88 | movq %r8, 0*8(%rdi) |
| 89 | movq %r9, 1*8(%rdi) |
| 90 | movq %r10, 2*8(%rdi) |
| 91 | movq %r11, 3*8(%rdi) |
| 92 | leaq 4*8(%rdi), %rdi |
| 93 | jae .Lcopy_forward_loop |
| 94 | addq $0x20, %rdx |
| 95 | jmp .Lhandle_tail |
| 96 | |
| 97 | .Lcopy_backward: |
| 98 | /* |
| 99 | * Calculate copy position to tail. |
| 100 | */ |
| 101 | addq %rdx, %rsi |
| 102 | addq %rdx, %rdi |
| 103 | subq $0x20, %rdx |
| 104 | /* |
| 105 | * At most 3 ALU operations in one cycle, |
| 106 | * so append NOPS in the same 16bytes trunk. |
| 107 | */ |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 108 | .p2align 4 |
Ma Ling | 59daa70 | 2010-06-29 03:24:25 +0800 | [diff] [blame] | 109 | .Lcopy_backward_loop: |
| 110 | subq $0x20, %rdx |
| 111 | movq -1*8(%rsi), %r8 |
| 112 | movq -2*8(%rsi), %r9 |
| 113 | movq -3*8(%rsi), %r10 |
| 114 | movq -4*8(%rsi), %r11 |
| 115 | leaq -4*8(%rsi), %rsi |
| 116 | movq %r8, -1*8(%rdi) |
| 117 | movq %r9, -2*8(%rdi) |
| 118 | movq %r10, -3*8(%rdi) |
| 119 | movq %r11, -4*8(%rdi) |
| 120 | leaq -4*8(%rdi), %rdi |
| 121 | jae .Lcopy_backward_loop |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 122 | |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 123 | /* |
Ma Ling | 59daa70 | 2010-06-29 03:24:25 +0800 | [diff] [blame] | 124 | * Calculate copy position to head. |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 125 | */ |
Ma Ling | 59daa70 | 2010-06-29 03:24:25 +0800 | [diff] [blame] | 126 | addq $0x20, %rdx |
| 127 | subq %rdx, %rsi |
| 128 | subq %rdx, %rdi |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 129 | .Lhandle_tail: |
Ma Ling | 59daa70 | 2010-06-29 03:24:25 +0800 | [diff] [blame] | 130 | cmpq $16, %rdx |
| 131 | jb .Lless_16bytes |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 132 | |
Ma Ling | 59daa70 | 2010-06-29 03:24:25 +0800 | [diff] [blame] | 133 | /* |
| 134 | * Move data from 16 bytes to 31 bytes. |
| 135 | */ |
| 136 | movq 0*8(%rsi), %r8 |
| 137 | movq 1*8(%rsi), %r9 |
| 138 | movq -2*8(%rsi, %rdx), %r10 |
| 139 | movq -1*8(%rsi, %rdx), %r11 |
| 140 | movq %r8, 0*8(%rdi) |
| 141 | movq %r9, 1*8(%rdi) |
| 142 | movq %r10, -2*8(%rdi, %rdx) |
| 143 | movq %r11, -1*8(%rdi, %rdx) |
| 144 | retq |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 145 | .p2align 4 |
Ma Ling | 59daa70 | 2010-06-29 03:24:25 +0800 | [diff] [blame] | 146 | .Lless_16bytes: |
| 147 | cmpq $8, %rdx |
| 148 | jb .Lless_8bytes |
| 149 | /* |
| 150 | * Move data from 8 bytes to 15 bytes. |
| 151 | */ |
| 152 | movq 0*8(%rsi), %r8 |
| 153 | movq -1*8(%rsi, %rdx), %r9 |
| 154 | movq %r8, 0*8(%rdi) |
| 155 | movq %r9, -1*8(%rdi, %rdx) |
| 156 | retq |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 157 | .p2align 4 |
Ma Ling | 59daa70 | 2010-06-29 03:24:25 +0800 | [diff] [blame] | 158 | .Lless_8bytes: |
| 159 | cmpq $4, %rdx |
| 160 | jb .Lless_3bytes |
| 161 | |
| 162 | /* |
| 163 | * Move data from 4 bytes to 7 bytes. |
| 164 | */ |
| 165 | movl (%rsi), %ecx |
| 166 | movl -4(%rsi, %rdx), %r8d |
| 167 | movl %ecx, (%rdi) |
| 168 | movl %r8d, -4(%rdi, %rdx) |
| 169 | retq |
| 170 | .p2align 4 |
| 171 | .Lless_3bytes: |
| 172 | cmpl $0, %edx |
| 173 | je .Lend |
| 174 | /* |
| 175 | * Move data from 1 bytes to 3 bytes. |
| 176 | */ |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 177 | .Lloop_1: |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 178 | movb (%rsi), %r8b |
| 179 | movb %r8b, (%rdi) |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 180 | incq %rdi |
| 181 | incq %rsi |
Ma Ling | 59daa70 | 2010-06-29 03:24:25 +0800 | [diff] [blame] | 182 | decl %edx |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 183 | jnz .Lloop_1 |
| 184 | |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 185 | .Lend: |
Ma Ling | 59daa70 | 2010-06-29 03:24:25 +0800 | [diff] [blame] | 186 | retq |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 187 | CFI_ENDPROC |
| 188 | ENDPROC(memcpy) |
| 189 | ENDPROC(__memcpy) |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 190 | |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 191 | /* |
Fenghua Yu | 101068c | 2011-05-17 15:29:16 -0700 | [diff] [blame] | 192 | * Some CPUs are adding enhanced REP MOVSB/STOSB feature |
| 193 | * If the feature is supported, memcpy_c_e() is the first choice. |
| 194 | * If enhanced rep movsb copy is not available, use fast string copy |
| 195 | * memcpy_c() when possible. This is faster and code is simpler than |
| 196 | * original memcpy(). |
| 197 | * Otherwise, original memcpy() is used. |
| 198 | * In .altinstructions section, ERMS feature is placed after REG_GOOD |
| 199 | * feature to implement the right patch order. |
| 200 | * |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 201 | * Replace only beginning, memcpy is used to apply alternatives, |
| 202 | * so it is silly to overwrite itself with nops - reboot is the |
| 203 | * only outcome... |
| 204 | */ |
Fenghua Yu | 101068c | 2011-05-17 15:29:16 -0700 | [diff] [blame] | 205 | .section .altinstructions, "a" |
| 206 | altinstruction_entry memcpy,.Lmemcpy_c,X86_FEATURE_REP_GOOD,\ |
| 207 | .Lmemcpy_e-.Lmemcpy_c,.Lmemcpy_e-.Lmemcpy_c |
| 208 | altinstruction_entry memcpy,.Lmemcpy_c_e,X86_FEATURE_ERMS, \ |
| 209 | .Lmemcpy_e_e-.Lmemcpy_c_e,.Lmemcpy_e_e-.Lmemcpy_c_e |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 210 | .previous |