Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * csum_partial_copy - do IP checksumming and copy |
| 4 | * |
| 5 | * (C) Copyright 1996 Linus Torvalds |
Simon Arlott | c3a2dde | 2007-10-20 01:04:37 +0200 | [diff] [blame] | 6 | * accelerated versions (and 21264 assembly versions ) contributed by |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * Rick Gorton <rick.gorton@alpha-processor.com> |
| 8 | * |
| 9 | * Don't look at this too closely - you'll go mad. The things |
| 10 | * we do for performance.. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/string.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 15 | #include <linux/uaccess.h> |
Randy Dunlap | 0214967 | 2021-05-06 18:02:07 -0700 | [diff] [blame] | 16 | #include <net/checksum.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | |
| 19 | #define ldq_u(x,y) \ |
| 20 | __asm__ __volatile__("ldq_u %0,%1":"=r" (x):"m" (*(const unsigned long *)(y))) |
| 21 | |
| 22 | #define stq_u(x,y) \ |
| 23 | __asm__ __volatile__("stq_u %1,%0":"=m" (*(unsigned long *)(y)):"r" (x)) |
| 24 | |
| 25 | #define extql(x,y,z) \ |
| 26 | __asm__ __volatile__("extql %1,%2,%0":"=r" (z):"r" (x),"r" (y)) |
| 27 | |
| 28 | #define extqh(x,y,z) \ |
| 29 | __asm__ __volatile__("extqh %1,%2,%0":"=r" (z):"r" (x),"r" (y)) |
| 30 | |
| 31 | #define mskql(x,y,z) \ |
| 32 | __asm__ __volatile__("mskql %1,%2,%0":"=r" (z):"r" (x),"r" (y)) |
| 33 | |
| 34 | #define mskqh(x,y,z) \ |
| 35 | __asm__ __volatile__("mskqh %1,%2,%0":"=r" (z):"r" (x),"r" (y)) |
| 36 | |
| 37 | #define insql(x,y,z) \ |
| 38 | __asm__ __volatile__("insql %1,%2,%0":"=r" (z):"r" (x),"r" (y)) |
| 39 | |
| 40 | #define insqh(x,y,z) \ |
| 41 | __asm__ __volatile__("insqh %1,%2,%0":"=r" (z):"r" (x),"r" (y)) |
| 42 | |
Al Viro | b712139 | 2020-07-12 22:55:27 -0400 | [diff] [blame] | 43 | #define __get_word(insn,x,ptr) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | ({ \ |
| 45 | long __guu_err; \ |
| 46 | __asm__ __volatile__( \ |
Al Viro | b712139 | 2020-07-12 22:55:27 -0400 | [diff] [blame] | 47 | "1: "#insn" %0,%2\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | "2:\n" \ |
Al Viro | ca282f6 | 2017-03-07 04:08:46 -0500 | [diff] [blame] | 49 | EXC(1b,2b,%0,%1) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | : "=r"(x), "=r"(__guu_err) \ |
| 51 | : "m"(__m(ptr)), "1"(0)); \ |
| 52 | __guu_err; \ |
| 53 | }) |
| 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | static inline unsigned short from64to16(unsigned long x) |
| 56 | { |
| 57 | /* Using extract instructions is a bit more efficient |
| 58 | than the original shift/bitmask version. */ |
| 59 | |
| 60 | union { |
| 61 | unsigned long ul; |
| 62 | unsigned int ui[2]; |
| 63 | unsigned short us[4]; |
| 64 | } in_v, tmp_v, out_v; |
| 65 | |
| 66 | in_v.ul = x; |
| 67 | tmp_v.ul = (unsigned long) in_v.ui[0] + (unsigned long) in_v.ui[1]; |
| 68 | |
| 69 | /* Since the bits of tmp_v.sh[3] are going to always be zero, |
| 70 | we don't have to bother to add that in. */ |
| 71 | out_v.ul = (unsigned long) tmp_v.us[0] + (unsigned long) tmp_v.us[1] |
| 72 | + (unsigned long) tmp_v.us[2]; |
| 73 | |
| 74 | /* Similarly, out_v.us[2] is always zero for the final add. */ |
| 75 | return out_v.us[0] + out_v.us[1]; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | |
| 80 | /* |
| 81 | * Ok. This isn't fun, but this is the EASY case. |
| 82 | */ |
| 83 | static inline unsigned long |
| 84 | csum_partial_cfu_aligned(const unsigned long __user *src, unsigned long *dst, |
Al Viro | b712139 | 2020-07-12 22:55:27 -0400 | [diff] [blame] | 85 | long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
Al Viro | b712139 | 2020-07-12 22:55:27 -0400 | [diff] [blame] | 87 | unsigned long checksum = ~0U; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | unsigned long carry = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | while (len >= 0) { |
| 91 | unsigned long word; |
Al Viro | b712139 | 2020-07-12 22:55:27 -0400 | [diff] [blame] | 92 | if (__get_word(ldq, word, src)) |
| 93 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | checksum += carry; |
| 95 | src++; |
| 96 | checksum += word; |
| 97 | len -= 8; |
| 98 | carry = checksum < word; |
| 99 | *dst = word; |
| 100 | dst++; |
| 101 | } |
| 102 | len += 8; |
| 103 | checksum += carry; |
| 104 | if (len) { |
| 105 | unsigned long word, tmp; |
Al Viro | b712139 | 2020-07-12 22:55:27 -0400 | [diff] [blame] | 106 | if (__get_word(ldq, word, src)) |
| 107 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | tmp = *dst; |
| 109 | mskql(word, len, word); |
| 110 | checksum += word; |
| 111 | mskqh(tmp, len, tmp); |
| 112 | carry = checksum < word; |
| 113 | *dst = word | tmp; |
| 114 | checksum += carry; |
| 115 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | return checksum; |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * This is even less fun, but this is still reasonably |
| 121 | * easy. |
| 122 | */ |
| 123 | static inline unsigned long |
| 124 | csum_partial_cfu_dest_aligned(const unsigned long __user *src, |
| 125 | unsigned long *dst, |
| 126 | unsigned long soff, |
Al Viro | b712139 | 2020-07-12 22:55:27 -0400 | [diff] [blame] | 127 | long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
| 129 | unsigned long first; |
| 130 | unsigned long word, carry; |
| 131 | unsigned long lastsrc = 7+len+(unsigned long)src; |
Al Viro | b712139 | 2020-07-12 22:55:27 -0400 | [diff] [blame] | 132 | unsigned long checksum = ~0U; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
Al Viro | b712139 | 2020-07-12 22:55:27 -0400 | [diff] [blame] | 134 | if (__get_word(ldq_u, first,src)) |
| 135 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | carry = 0; |
| 137 | while (len >= 0) { |
| 138 | unsigned long second; |
| 139 | |
Al Viro | b712139 | 2020-07-12 22:55:27 -0400 | [diff] [blame] | 140 | if (__get_word(ldq_u, second, src+1)) |
| 141 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | extql(first, soff, word); |
| 143 | len -= 8; |
| 144 | src++; |
| 145 | extqh(second, soff, first); |
| 146 | checksum += carry; |
| 147 | word |= first; |
| 148 | first = second; |
| 149 | checksum += word; |
| 150 | *dst = word; |
| 151 | dst++; |
| 152 | carry = checksum < word; |
| 153 | } |
| 154 | len += 8; |
| 155 | checksum += carry; |
| 156 | if (len) { |
| 157 | unsigned long tmp; |
| 158 | unsigned long second; |
Al Viro | b712139 | 2020-07-12 22:55:27 -0400 | [diff] [blame] | 159 | if (__get_word(ldq_u, second, lastsrc)) |
| 160 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | tmp = *dst; |
| 162 | extql(first, soff, word); |
| 163 | extqh(second, soff, first); |
| 164 | word |= first; |
| 165 | mskql(word, len, word); |
| 166 | checksum += word; |
| 167 | mskqh(tmp, len, tmp); |
| 168 | carry = checksum < word; |
| 169 | *dst = word | tmp; |
| 170 | checksum += carry; |
| 171 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | return checksum; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * This is slightly less fun than the above.. |
| 177 | */ |
| 178 | static inline unsigned long |
| 179 | csum_partial_cfu_src_aligned(const unsigned long __user *src, |
| 180 | unsigned long *dst, |
| 181 | unsigned long doff, |
Al Viro | b712139 | 2020-07-12 22:55:27 -0400 | [diff] [blame] | 182 | long len, |
| 183 | unsigned long partial_dest) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { |
| 185 | unsigned long carry = 0; |
| 186 | unsigned long word; |
| 187 | unsigned long second_dest; |
Al Viro | b712139 | 2020-07-12 22:55:27 -0400 | [diff] [blame] | 188 | unsigned long checksum = ~0U; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
| 190 | mskql(partial_dest, doff, partial_dest); |
| 191 | while (len >= 0) { |
Al Viro | b712139 | 2020-07-12 22:55:27 -0400 | [diff] [blame] | 192 | if (__get_word(ldq, word, src)) |
| 193 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | len -= 8; |
| 195 | insql(word, doff, second_dest); |
| 196 | checksum += carry; |
| 197 | stq_u(partial_dest | second_dest, dst); |
| 198 | src++; |
| 199 | checksum += word; |
| 200 | insqh(word, doff, partial_dest); |
| 201 | carry = checksum < word; |
| 202 | dst++; |
| 203 | } |
| 204 | len += 8; |
| 205 | if (len) { |
| 206 | checksum += carry; |
Al Viro | b712139 | 2020-07-12 22:55:27 -0400 | [diff] [blame] | 207 | if (__get_word(ldq, word, src)) |
| 208 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | mskql(word, len, word); |
| 210 | len -= 8; |
| 211 | checksum += word; |
| 212 | insql(word, doff, second_dest); |
| 213 | len += doff; |
| 214 | carry = checksum < word; |
| 215 | partial_dest |= second_dest; |
| 216 | if (len >= 0) { |
| 217 | stq_u(partial_dest, dst); |
| 218 | if (!len) goto out; |
| 219 | dst++; |
| 220 | insqh(word, doff, partial_dest); |
| 221 | } |
| 222 | doff = len; |
| 223 | } |
| 224 | ldq_u(second_dest, dst); |
| 225 | mskqh(second_dest, doff, second_dest); |
| 226 | stq_u(partial_dest | second_dest, dst); |
| 227 | out: |
| 228 | checksum += carry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | return checksum; |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * This is so totally un-fun that it's frightening. Don't |
| 234 | * look at this too closely, you'll go blind. |
| 235 | */ |
| 236 | static inline unsigned long |
| 237 | csum_partial_cfu_unaligned(const unsigned long __user * src, |
| 238 | unsigned long * dst, |
| 239 | unsigned long soff, unsigned long doff, |
Al Viro | b712139 | 2020-07-12 22:55:27 -0400 | [diff] [blame] | 240 | long len, unsigned long partial_dest) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | { |
| 242 | unsigned long carry = 0; |
| 243 | unsigned long first; |
| 244 | unsigned long lastsrc; |
Al Viro | b712139 | 2020-07-12 22:55:27 -0400 | [diff] [blame] | 245 | unsigned long checksum = ~0U; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
Al Viro | b712139 | 2020-07-12 22:55:27 -0400 | [diff] [blame] | 247 | if (__get_word(ldq_u, first, src)) |
| 248 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | lastsrc = 7+len+(unsigned long)src; |
| 250 | mskql(partial_dest, doff, partial_dest); |
| 251 | while (len >= 0) { |
| 252 | unsigned long second, word; |
| 253 | unsigned long second_dest; |
| 254 | |
Al Viro | b712139 | 2020-07-12 22:55:27 -0400 | [diff] [blame] | 255 | if (__get_word(ldq_u, second, src+1)) |
| 256 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | extql(first, soff, word); |
| 258 | checksum += carry; |
| 259 | len -= 8; |
| 260 | extqh(second, soff, first); |
| 261 | src++; |
| 262 | word |= first; |
| 263 | first = second; |
| 264 | insql(word, doff, second_dest); |
| 265 | checksum += word; |
| 266 | stq_u(partial_dest | second_dest, dst); |
| 267 | carry = checksum < word; |
| 268 | insqh(word, doff, partial_dest); |
| 269 | dst++; |
| 270 | } |
| 271 | len += doff; |
| 272 | checksum += carry; |
| 273 | if (len >= 0) { |
| 274 | unsigned long second, word; |
| 275 | unsigned long second_dest; |
| 276 | |
Al Viro | b712139 | 2020-07-12 22:55:27 -0400 | [diff] [blame] | 277 | if (__get_word(ldq_u, second, lastsrc)) |
| 278 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | extql(first, soff, word); |
| 280 | extqh(second, soff, first); |
| 281 | word |= first; |
| 282 | first = second; |
| 283 | mskql(word, len-doff, word); |
| 284 | checksum += word; |
| 285 | insql(word, doff, second_dest); |
| 286 | carry = checksum < word; |
| 287 | stq_u(partial_dest | second_dest, dst); |
| 288 | if (len) { |
| 289 | ldq_u(second_dest, dst+1); |
| 290 | insqh(word, doff, partial_dest); |
| 291 | mskqh(second_dest, len, second_dest); |
| 292 | stq_u(partial_dest | second_dest, dst+1); |
| 293 | } |
| 294 | checksum += carry; |
| 295 | } else { |
| 296 | unsigned long second, word; |
| 297 | unsigned long second_dest; |
| 298 | |
Al Viro | b712139 | 2020-07-12 22:55:27 -0400 | [diff] [blame] | 299 | if (__get_word(ldq_u, second, lastsrc)) |
| 300 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | extql(first, soff, word); |
| 302 | extqh(second, soff, first); |
| 303 | word |= first; |
| 304 | ldq_u(second_dest, dst); |
| 305 | mskql(word, len-doff, word); |
| 306 | checksum += word; |
| 307 | mskqh(second_dest, len, second_dest); |
| 308 | carry = checksum < word; |
| 309 | insql(word, doff, word); |
| 310 | stq_u(partial_dest | word | second_dest, dst); |
| 311 | checksum += carry; |
| 312 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | return checksum; |
| 314 | } |
| 315 | |
Al Viro | b712139 | 2020-07-12 22:55:27 -0400 | [diff] [blame] | 316 | static __wsum __csum_and_copy(const void __user *src, void *dst, int len) |
| 317 | { |
| 318 | unsigned long soff = 7 & (unsigned long) src; |
| 319 | unsigned long doff = 7 & (unsigned long) dst; |
| 320 | unsigned long checksum; |
| 321 | |
| 322 | if (!doff) { |
| 323 | if (!soff) |
| 324 | checksum = csum_partial_cfu_aligned( |
| 325 | (const unsigned long __user *) src, |
| 326 | (unsigned long *) dst, len-8); |
| 327 | else |
| 328 | checksum = csum_partial_cfu_dest_aligned( |
| 329 | (const unsigned long __user *) src, |
| 330 | (unsigned long *) dst, |
| 331 | soff, len-8); |
| 332 | } else { |
| 333 | unsigned long partial_dest; |
| 334 | ldq_u(partial_dest, dst); |
| 335 | if (!soff) |
| 336 | checksum = csum_partial_cfu_src_aligned( |
| 337 | (const unsigned long __user *) src, |
| 338 | (unsigned long *) dst, |
| 339 | doff, len-8, partial_dest); |
| 340 | else |
| 341 | checksum = csum_partial_cfu_unaligned( |
| 342 | (const unsigned long __user *) src, |
| 343 | (unsigned long *) dst, |
| 344 | soff, doff, len-8, partial_dest); |
| 345 | } |
| 346 | return (__force __wsum)from64to16 (checksum); |
| 347 | } |
| 348 | |
Al Viro | 9be259a | 2006-11-14 21:14:53 -0800 | [diff] [blame] | 349 | __wsum |
Al Viro | c693cc4 | 2020-07-11 00:27:49 -0400 | [diff] [blame] | 350 | csum_and_copy_from_user(const void __user *src, void *dst, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | { |
Al Viro | b712139 | 2020-07-12 22:55:27 -0400 | [diff] [blame] | 352 | if (!access_ok(src, len)) |
| 353 | return 0; |
| 354 | return __csum_and_copy(src, dst, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | } |
Al Viro | 808b49d | 2020-02-18 12:49:07 -0500 | [diff] [blame] | 356 | EXPORT_SYMBOL(csum_and_copy_from_user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
Al Viro | 9be259a | 2006-11-14 21:14:53 -0800 | [diff] [blame] | 358 | __wsum |
Al Viro | cc44c17 | 2020-07-11 00:12:07 -0400 | [diff] [blame] | 359 | csum_partial_copy_nocheck(const void *src, void *dst, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | { |
Al Viro | b712139 | 2020-07-12 22:55:27 -0400 | [diff] [blame] | 361 | return __csum_and_copy((__force const void __user *)src, |
Al Viro | c693cc4 | 2020-07-11 00:27:49 -0400 | [diff] [blame] | 362 | dst, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | } |
Al Viro | 00fc0e0 | 2016-01-11 09:51:29 -0500 | [diff] [blame] | 364 | EXPORT_SYMBOL(csum_partial_copy_nocheck); |