Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 2 | * Copyright 2002, 2003 Andi Kleen, SuSE Labs. |
| 3 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * This file is subject to the terms and conditions of the GNU General Public |
| 5 | * License. See the file COPYING in the main directory of this archive |
| 6 | * for more details. No warranty for anything given at all. |
| 7 | */ |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 8 | #include <linux/linkage.h> |
| 9 | #include <asm/dwarf2.h> |
| 10 | #include <asm/errno.h> |
H. Peter Anvin | 015e6f1 | 2012-04-20 12:19:51 -0700 | [diff] [blame] | 11 | #include <asm/asm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
| 13 | /* |
| 14 | * Checksum copy with exception handling. |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 15 | * On exceptions src_err_ptr or dst_err_ptr is set to -EFAULT and the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | * destination is zeroed. |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 17 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | * Input |
| 19 | * rdi source |
| 20 | * rsi destination |
| 21 | * edx len (32bit) |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 22 | * ecx sum (32bit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | * r8 src_err_ptr (int) |
| 24 | * r9 dst_err_ptr (int) |
| 25 | * |
| 26 | * Output |
| 27 | * eax 64bit sum. undefined in case of exception. |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 28 | * |
| 29 | * Wrappers need to take care of valid exception sum and zeroing. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | * They also should align source or destination to 8 bytes. |
| 31 | */ |
| 32 | |
| 33 | .macro source |
| 34 | 10: |
H. Peter Anvin | 015e6f1 | 2012-04-20 12:19:51 -0700 | [diff] [blame] | 35 | _ASM_EXTABLE(10b, .Lbad_source) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | .endm |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | .macro dest |
| 39 | 20: |
H. Peter Anvin | 015e6f1 | 2012-04-20 12:19:51 -0700 | [diff] [blame] | 40 | _ASM_EXTABLE(20b, .Lbad_dest) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | .endm |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | .macro ignore L=.Lignore |
| 44 | 30: |
H. Peter Anvin | 015e6f1 | 2012-04-20 12:19:51 -0700 | [diff] [blame] | 45 | _ASM_EXTABLE(30b, \L) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | .endm |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 47 | |
| 48 | |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 49 | ENTRY(csum_partial_copy_generic) |
| 50 | CFI_STARTPROC |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 51 | cmpl $3*64, %edx |
| 52 | jle .Lignore |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 54 | .Lignore: |
| 55 | subq $7*8, %rsp |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 56 | CFI_ADJUST_CFA_OFFSET 7*8 |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 57 | movq %rbx, 2*8(%rsp) |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 58 | CFI_REL_OFFSET rbx, 2*8 |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 59 | movq %r12, 3*8(%rsp) |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 60 | CFI_REL_OFFSET r12, 3*8 |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 61 | movq %r14, 4*8(%rsp) |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 62 | CFI_REL_OFFSET r14, 4*8 |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 63 | movq %r13, 5*8(%rsp) |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 64 | CFI_REL_OFFSET r13, 5*8 |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 65 | movq %rbp, 6*8(%rsp) |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 66 | CFI_REL_OFFSET rbp, 6*8 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 68 | movq %r8, (%rsp) |
| 69 | movq %r9, 1*8(%rsp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 71 | movl %ecx, %eax |
| 72 | movl %edx, %ecx |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 74 | xorl %r9d, %r9d |
| 75 | movq %rcx, %r12 |
| 76 | |
| 77 | shrq $6, %r12 |
| 78 | jz .Lhandle_tail /* < 64 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | clc |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | /* main loop. clear in 64 byte blocks */ |
| 83 | /* r9: zero, r8: temp2, rbx: temp1, rax: sum, rcx: saved length */ |
| 84 | /* r11: temp3, rdx: temp4, r12 loopcnt */ |
| 85 | /* r10: temp5, rbp: temp6, r14 temp7, r13 temp8 */ |
| 86 | .p2align 4 |
| 87 | .Lloop: |
| 88 | source |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 89 | movq (%rdi), %rbx |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | source |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 91 | movq 8(%rdi), %r8 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | source |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 93 | movq 16(%rdi), %r11 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | source |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 95 | movq 24(%rdi), %rdx |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
| 97 | source |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 98 | movq 32(%rdi), %r10 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | source |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 100 | movq 40(%rdi), %rbp |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | source |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 102 | movq 48(%rdi), %r14 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | source |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 104 | movq 56(%rdi), %r13 |
| 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | ignore 2f |
| 107 | prefetcht0 5*64(%rdi) |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 108 | 2: |
| 109 | adcq %rbx, %rax |
| 110 | adcq %r8, %rax |
| 111 | adcq %r11, %rax |
| 112 | adcq %rdx, %rax |
| 113 | adcq %r10, %rax |
| 114 | adcq %rbp, %rax |
| 115 | adcq %r14, %rax |
| 116 | adcq %r13, %rax |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | decl %r12d |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
| 120 | dest |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 121 | movq %rbx, (%rsi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | dest |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 123 | movq %r8, 8(%rsi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | dest |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 125 | movq %r11, 16(%rsi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | dest |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 127 | movq %rdx, 24(%rsi) |
| 128 | |
| 129 | dest |
| 130 | movq %r10, 32(%rsi) |
| 131 | dest |
| 132 | movq %rbp, 40(%rsi) |
| 133 | dest |
| 134 | movq %r14, 48(%rsi) |
| 135 | dest |
| 136 | movq %r13, 56(%rsi) |
| 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | 3: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 140 | leaq 64(%rdi), %rdi |
| 141 | leaq 64(%rsi), %rsi |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 143 | jnz .Lloop |
| 144 | |
| 145 | adcq %r9, %rax |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Lucas De Marchi | 0d2eb44 | 2011-03-17 16:24:16 -0300 | [diff] [blame] | 147 | /* do last up to 56 bytes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | .Lhandle_tail: |
| 149 | /* ecx: count */ |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 150 | movl %ecx, %r10d |
| 151 | andl $63, %ecx |
| 152 | shrl $3, %ecx |
| 153 | jz .Lfold |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | clc |
| 155 | .p2align 4 |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 156 | .Lloop_8: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | source |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 158 | movq (%rdi), %rbx |
| 159 | adcq %rbx, %rax |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | decl %ecx |
| 161 | dest |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 162 | movq %rbx, (%rsi) |
| 163 | leaq 8(%rsi), %rsi /* preserve carry */ |
| 164 | leaq 8(%rdi), %rdi |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | jnz .Lloop_8 |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 166 | adcq %r9, %rax /* add in carry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
| 168 | .Lfold: |
| 169 | /* reduce checksum to 32bits */ |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 170 | movl %eax, %ebx |
| 171 | shrq $32, %rax |
| 172 | addl %ebx, %eax |
| 173 | adcl %r9d, %eax |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 175 | /* do last up to 6 bytes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | .Lhandle_7: |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 177 | movl %r10d, %ecx |
| 178 | andl $7, %ecx |
| 179 | shrl $1, %ecx |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | jz .Lhandle_1 |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 181 | movl $2, %edx |
| 182 | xorl %ebx, %ebx |
| 183 | clc |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | .p2align 4 |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 185 | .Lloop_1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | source |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 187 | movw (%rdi), %bx |
| 188 | adcl %ebx, %eax |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | decl %ecx |
Dave Peterson | 92ed022 | 2005-07-29 22:59:20 -0700 | [diff] [blame] | 190 | dest |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 191 | movw %bx, (%rsi) |
| 192 | leaq 2(%rdi), %rdi |
| 193 | leaq 2(%rsi), %rsi |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | jnz .Lloop_1 |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 195 | adcl %r9d, %eax /* add in carry */ |
| 196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | /* handle last odd byte */ |
| 198 | .Lhandle_1: |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 199 | testl $1, %r10d |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | jz .Lende |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 201 | xorl %ebx, %ebx |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | source |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 203 | movb (%rdi), %bl |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | dest |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 205 | movb %bl, (%rsi) |
| 206 | addl %ebx, %eax |
| 207 | adcl %r9d, %eax /* carry */ |
| 208 | |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 209 | CFI_REMEMBER_STATE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | .Lende: |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 211 | movq 2*8(%rsp), %rbx |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 212 | CFI_RESTORE rbx |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 213 | movq 3*8(%rsp), %r12 |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 214 | CFI_RESTORE r12 |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 215 | movq 4*8(%rsp), %r14 |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 216 | CFI_RESTORE r14 |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 217 | movq 5*8(%rsp), %r13 |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 218 | CFI_RESTORE r13 |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 219 | movq 6*8(%rsp), %rbp |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 220 | CFI_RESTORE rbp |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 221 | addq $7*8, %rsp |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 222 | CFI_ADJUST_CFA_OFFSET -7*8 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | ret |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 224 | CFI_RESTORE_STATE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
| 226 | /* Exception handlers. Very simple, zeroing is done in the wrappers */ |
| 227 | .Lbad_source: |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 228 | movq (%rsp), %rax |
| 229 | testq %rax, %rax |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | jz .Lende |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 231 | movl $-EFAULT, (%rax) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | jmp .Lende |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | .Lbad_dest: |
Ingo Molnar | 2c76397 | 2011-03-18 10:42:11 +0100 | [diff] [blame] | 235 | movq 8(%rsp), %rax |
| 236 | testq %rax, %rax |
| 237 | jz .Lende |
| 238 | movl $-EFAULT, (%rax) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | jmp .Lende |
Jan Beulich | 8d379da | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 240 | CFI_ENDPROC |
| 241 | ENDPROC(csum_partial_copy_generic) |