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