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> |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | /* |
| 9 | * memcpy - Copy a memory block. |
| 10 | * |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 11 | * Input: |
| 12 | * rdi destination |
| 13 | * rsi source |
| 14 | * rdx count |
| 15 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | * Output: |
| 17 | * rax original destination |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 18 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 20 | /* |
| 21 | * memcpy_c() - fast string ops (REP MOVSQ) based variant. |
| 22 | * |
Jan Beulich | 7269e88 | 2009-12-18 16:16:03 +0000 | [diff] [blame] | 23 | * This gets patched over the unrolled variant (below) via the |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 24 | * alternative instructions framework: |
| 25 | */ |
Jan Beulich | 7269e88 | 2009-12-18 16:16:03 +0000 | [diff] [blame] | 26 | .section .altinstr_replacement, "ax", @progbits |
| 27 | .Lmemcpy_c: |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 28 | movq %rdi, %rax |
| 29 | |
| 30 | movl %edx, %ecx |
| 31 | shrl $3, %ecx |
| 32 | andl $7, %edx |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 33 | rep movsq |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 34 | movl %edx, %ecx |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 35 | rep movsb |
| 36 | ret |
Jan Beulich | 7269e88 | 2009-12-18 16:16:03 +0000 | [diff] [blame] | 37 | .Lmemcpy_e: |
| 38 | .previous |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 39 | |
| 40 | ENTRY(__memcpy) |
| 41 | ENTRY(memcpy) |
| 42 | CFI_STARTPROC |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 43 | |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 44 | /* |
| 45 | * Put the number of full 64-byte blocks into %ecx. |
| 46 | * Tail portion is handled at the end: |
| 47 | */ |
| 48 | movq %rdi, %rax |
| 49 | movl %edx, %ecx |
| 50 | shrl $6, %ecx |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 51 | jz .Lhandle_tail |
| 52 | |
| 53 | .p2align 4 |
| 54 | .Lloop_64: |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 55 | /* |
| 56 | * We decrement the loop index here - and the zero-flag is |
| 57 | * checked at the end of the loop (instructions inbetween do |
| 58 | * not change the zero flag): |
| 59 | */ |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 60 | decl %ecx |
| 61 | |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 62 | /* |
| 63 | * Move in blocks of 4x16 bytes: |
| 64 | */ |
| 65 | movq 0*8(%rsi), %r11 |
| 66 | movq 1*8(%rsi), %r8 |
| 67 | movq %r11, 0*8(%rdi) |
| 68 | movq %r8, 1*8(%rdi) |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 69 | |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 70 | movq 2*8(%rsi), %r9 |
| 71 | movq 3*8(%rsi), %r10 |
| 72 | movq %r9, 2*8(%rdi) |
| 73 | movq %r10, 3*8(%rdi) |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 74 | |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 75 | movq 4*8(%rsi), %r11 |
| 76 | movq 5*8(%rsi), %r8 |
| 77 | movq %r11, 4*8(%rdi) |
| 78 | movq %r8, 5*8(%rdi) |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 79 | |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 80 | movq 6*8(%rsi), %r9 |
| 81 | movq 7*8(%rsi), %r10 |
| 82 | movq %r9, 6*8(%rdi) |
| 83 | movq %r10, 7*8(%rdi) |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 84 | |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 85 | leaq 64(%rsi), %rsi |
| 86 | leaq 64(%rdi), %rdi |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 87 | |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 88 | jnz .Lloop_64 |
| 89 | |
| 90 | .Lhandle_tail: |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 91 | movl %edx, %ecx |
| 92 | andl $63, %ecx |
| 93 | shrl $3, %ecx |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 94 | jz .Lhandle_7 |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 95 | |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 96 | .p2align 4 |
| 97 | .Lloop_8: |
| 98 | decl %ecx |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 99 | movq (%rsi), %r8 |
| 100 | movq %r8, (%rdi) |
| 101 | leaq 8(%rdi), %rdi |
| 102 | leaq 8(%rsi), %rsi |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 103 | jnz .Lloop_8 |
| 104 | |
| 105 | .Lhandle_7: |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 106 | movl %edx, %ecx |
| 107 | andl $7, %ecx |
| 108 | jz .Lend |
| 109 | |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 110 | .p2align 4 |
| 111 | .Lloop_1: |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 112 | movb (%rsi), %r8b |
| 113 | movb %r8b, (%rdi) |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 114 | incq %rdi |
| 115 | incq %rsi |
| 116 | decl %ecx |
| 117 | jnz .Lloop_1 |
| 118 | |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 119 | .Lend: |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 120 | ret |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 121 | CFI_ENDPROC |
| 122 | ENDPROC(memcpy) |
| 123 | ENDPROC(__memcpy) |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 124 | |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 125 | /* |
| 126 | * Some CPUs run faster using the string copy instructions. |
| 127 | * It is also a lot simpler. Use this when possible: |
| 128 | */ |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 129 | |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 130 | .section .altinstructions, "a" |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 131 | .align 8 |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 132 | .quad memcpy |
Jan Beulich | 7269e88 | 2009-12-18 16:16:03 +0000 | [diff] [blame] | 133 | .quad .Lmemcpy_c |
H. Peter Anvin | 83a7a2a | 2010-06-10 00:10:43 +0000 | [diff] [blame] | 134 | .word X86_FEATURE_REP_GOOD |
Ingo Molnar | f3b6eaf | 2009-03-12 12:20:17 +0100 | [diff] [blame] | 135 | |
| 136 | /* |
| 137 | * Replace only beginning, memcpy is used to apply alternatives, |
| 138 | * so it is silly to overwrite itself with nops - reboot is the |
| 139 | * only outcome... |
| 140 | */ |
Jan Beulich | 7269e88 | 2009-12-18 16:16:03 +0000 | [diff] [blame] | 141 | .byte .Lmemcpy_e - .Lmemcpy_c |
| 142 | .byte .Lmemcpy_e - .Lmemcpy_c |
Andi Kleen | 7bcd3f3 | 2006-02-03 21:51:02 +0100 | [diff] [blame] | 143 | .previous |