Ingo Molnar | d76c1ae | 2008-02-17 16:48:25 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2002, 2003 Andi Kleen, SuSE Labs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Subject to the GNU Public License v.2 |
Paolo Ciarrocchi | 0df025b | 2008-02-17 14:56:50 +0100 | [diff] [blame] | 4 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Wrappers of assembly checksum functions for x86-64. |
| 6 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <asm/checksum.h> |
Paul Gortmaker | e683014 | 2016-07-13 20:18:57 -0400 | [diff] [blame] | 8 | #include <linux/export.h> |
Andy Lutomirski | 13d4ea0 | 2016-07-14 13:22:57 -0700 | [diff] [blame] | 9 | #include <linux/uaccess.h> |
H. Peter Anvin | 7263dda | 2013-08-30 15:43:03 -0700 | [diff] [blame] | 10 | #include <asm/smap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | |
Paolo Ciarrocchi | 0df025b | 2008-02-17 14:56:50 +0100 | [diff] [blame] | 12 | /** |
| 13 | * csum_partial_copy_from_user - Copy and checksum from user space. |
| 14 | * @src: source address (user space) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * @dst: destination address |
| 16 | * @len: number of bytes to be copied. |
| 17 | * @isum: initial sum that is added into the result (32bit unfolded) |
| 18 | * @errp: set to -EFAULT for an bad source address. |
Paolo Ciarrocchi | 0df025b | 2008-02-17 14:56:50 +0100 | [diff] [blame] | 19 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | * Returns an 32bit unfolded checksum of the buffer. |
Paolo Ciarrocchi | 0df025b | 2008-02-17 14:56:50 +0100 | [diff] [blame] | 21 | * src and dst are best aligned to 64bits. |
| 22 | */ |
Al Viro | a4f89fb | 2006-11-14 21:20:08 -0800 | [diff] [blame] | 23 | __wsum |
| 24 | csum_partial_copy_from_user(const void __user *src, void *dst, |
| 25 | int len, __wsum isum, int *errp) |
Paolo Ciarrocchi | 0df025b | 2008-02-17 14:56:50 +0100 | [diff] [blame] | 26 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | might_sleep(); |
| 28 | *errp = 0; |
Ingo Molnar | d76c1ae | 2008-02-17 16:48:25 +0100 | [diff] [blame] | 29 | |
| 30 | if (!likely(access_ok(VERIFY_READ, src, len))) |
| 31 | goto out_err; |
| 32 | |
| 33 | /* |
| 34 | * Why 6, not 7? To handle odd addresses aligned we |
| 35 | * would need to do considerable complications to fix the |
| 36 | * checksum which is defined as an 16bit accumulator. The |
| 37 | * fix alignment code is primarily for performance |
| 38 | * compatibility with 32bit and that will handle odd |
| 39 | * addresses slowly too. |
| 40 | */ |
| 41 | if (unlikely((unsigned long)src & 6)) { |
| 42 | while (((unsigned long)src & 6) && len >= 2) { |
| 43 | __u16 val16; |
| 44 | |
Linus Torvalds | 3b91270 | 2014-11-16 11:00:42 -0800 | [diff] [blame] | 45 | if (__get_user(val16, (const __u16 __user *)src)) |
| 46 | goto out_err; |
Ingo Molnar | d76c1ae | 2008-02-17 16:48:25 +0100 | [diff] [blame] | 47 | |
| 48 | *(__u16 *)dst = val16; |
| 49 | isum = (__force __wsum)add32_with_carry( |
| 50 | (__force unsigned)isum, val16); |
| 51 | src += 2; |
| 52 | dst += 2; |
| 53 | len -= 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } |
Paolo Ciarrocchi | 0df025b | 2008-02-17 14:56:50 +0100 | [diff] [blame] | 55 | } |
H. Peter Anvin | 7263dda | 2013-08-30 15:43:03 -0700 | [diff] [blame] | 56 | stac(); |
Ingo Molnar | d76c1ae | 2008-02-17 16:48:25 +0100 | [diff] [blame] | 57 | isum = csum_partial_copy_generic((__force const void *)src, |
| 58 | dst, len, isum, errp, NULL); |
H. Peter Anvin | 7263dda | 2013-08-30 15:43:03 -0700 | [diff] [blame] | 59 | clac(); |
Ingo Molnar | d76c1ae | 2008-02-17 16:48:25 +0100 | [diff] [blame] | 60 | if (unlikely(*errp)) |
| 61 | goto out_err; |
| 62 | |
| 63 | return isum; |
| 64 | |
| 65 | out_err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | *errp = -EFAULT; |
Paolo Ciarrocchi | 0df025b | 2008-02-17 14:56:50 +0100 | [diff] [blame] | 67 | memset(dst, 0, len); |
Ingo Molnar | d76c1ae | 2008-02-17 16:48:25 +0100 | [diff] [blame] | 68 | |
Paolo Ciarrocchi | 0df025b | 2008-02-17 14:56:50 +0100 | [diff] [blame] | 69 | return isum; |
| 70 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | EXPORT_SYMBOL(csum_partial_copy_from_user); |
| 72 | |
Paolo Ciarrocchi | 0df025b | 2008-02-17 14:56:50 +0100 | [diff] [blame] | 73 | /** |
| 74 | * csum_partial_copy_to_user - Copy and checksum to user space. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | * @src: source address |
| 76 | * @dst: destination address (user space) |
| 77 | * @len: number of bytes to be copied. |
| 78 | * @isum: initial sum that is added into the result (32bit unfolded) |
| 79 | * @errp: set to -EFAULT for an bad destination address. |
Paolo Ciarrocchi | 0df025b | 2008-02-17 14:56:50 +0100 | [diff] [blame] | 80 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | * Returns an 32bit unfolded checksum of the buffer. |
| 82 | * src and dst are best aligned to 64bits. |
Paolo Ciarrocchi | 0df025b | 2008-02-17 14:56:50 +0100 | [diff] [blame] | 83 | */ |
Al Viro | a4f89fb | 2006-11-14 21:20:08 -0800 | [diff] [blame] | 84 | __wsum |
| 85 | csum_partial_copy_to_user(const void *src, void __user *dst, |
| 86 | int len, __wsum isum, int *errp) |
Paolo Ciarrocchi | 0df025b | 2008-02-17 14:56:50 +0100 | [diff] [blame] | 87 | { |
H. Peter Anvin | 7263dda | 2013-08-30 15:43:03 -0700 | [diff] [blame] | 88 | __wsum ret; |
| 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | might_sleep(); |
Ingo Molnar | d76c1ae | 2008-02-17 16:48:25 +0100 | [diff] [blame] | 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | if (unlikely(!access_ok(VERIFY_WRITE, dst, len))) { |
| 93 | *errp = -EFAULT; |
Paolo Ciarrocchi | 0df025b | 2008-02-17 14:56:50 +0100 | [diff] [blame] | 94 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | if (unlikely((unsigned long)dst & 6)) { |
Paolo Ciarrocchi | 0df025b | 2008-02-17 14:56:50 +0100 | [diff] [blame] | 98 | while (((unsigned long)dst & 6) && len >= 2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | __u16 val16 = *(__u16 *)src; |
Ingo Molnar | d76c1ae | 2008-02-17 16:48:25 +0100 | [diff] [blame] | 100 | |
Al Viro | a4f89fb | 2006-11-14 21:20:08 -0800 | [diff] [blame] | 101 | isum = (__force __wsum)add32_with_carry( |
| 102 | (__force unsigned)isum, val16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | *errp = __put_user(val16, (__u16 __user *)dst); |
| 104 | if (*errp) |
| 105 | return isum; |
Paolo Ciarrocchi | 0df025b | 2008-02-17 14:56:50 +0100 | [diff] [blame] | 106 | src += 2; |
| 107 | dst += 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | len -= 2; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | *errp = 0; |
H. Peter Anvin | 7263dda | 2013-08-30 15:43:03 -0700 | [diff] [blame] | 113 | stac(); |
| 114 | ret = csum_partial_copy_generic(src, (void __force *)dst, |
| 115 | len, isum, NULL, errp); |
| 116 | clac(); |
| 117 | return ret; |
Paolo Ciarrocchi | 0df025b | 2008-02-17 14:56:50 +0100 | [diff] [blame] | 118 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | EXPORT_SYMBOL(csum_partial_copy_to_user); |
| 120 | |
Paolo Ciarrocchi | 0df025b | 2008-02-17 14:56:50 +0100 | [diff] [blame] | 121 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | * csum_partial_copy_nocheck - Copy and checksum. |
| 123 | * @src: source address |
| 124 | * @dst: destination address |
| 125 | * @len: number of bytes to be copied. |
Wanpeng Li | c15acff | 2012-06-12 12:53:21 +0800 | [diff] [blame] | 126 | * @sum: initial sum that is added into the result (32bit unfolded) |
Paolo Ciarrocchi | 0df025b | 2008-02-17 14:56:50 +0100 | [diff] [blame] | 127 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | * Returns an 32bit unfolded checksum of the buffer. |
Paolo Ciarrocchi | 0df025b | 2008-02-17 14:56:50 +0100 | [diff] [blame] | 129 | */ |
Al Viro | a4f89fb | 2006-11-14 21:20:08 -0800 | [diff] [blame] | 130 | __wsum |
| 131 | csum_partial_copy_nocheck(const void *src, void *dst, int len, __wsum sum) |
Paolo Ciarrocchi | 0df025b | 2008-02-17 14:56:50 +0100 | [diff] [blame] | 132 | { |
| 133 | return csum_partial_copy_generic(src, dst, len, sum, NULL, NULL); |
| 134 | } |
Andi Kleen | 2ee60e17 | 2006-06-26 13:59:44 +0200 | [diff] [blame] | 135 | EXPORT_SYMBOL(csum_partial_copy_nocheck); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
Al Viro | a4f89fb | 2006-11-14 21:20:08 -0800 | [diff] [blame] | 137 | __sum16 csum_ipv6_magic(const struct in6_addr *saddr, |
| 138 | const struct in6_addr *daddr, |
Alexander Duyck | 1e94082 | 2016-03-11 14:05:41 -0800 | [diff] [blame] | 139 | __u32 len, __u8 proto, __wsum sum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | { |
| 141 | __u64 rest, sum64; |
Paolo Ciarrocchi | 0df025b | 2008-02-17 14:56:50 +0100 | [diff] [blame] | 142 | |
Al Viro | a4f89fb | 2006-11-14 21:20:08 -0800 | [diff] [blame] | 143 | rest = (__force __u64)htonl(len) + (__force __u64)htons(proto) + |
| 144 | (__force __u64)sum; |
Ingo Molnar | d76c1ae | 2008-02-17 16:48:25 +0100 | [diff] [blame] | 145 | |
| 146 | asm(" addq (%[saddr]),%[sum]\n" |
| 147 | " adcq 8(%[saddr]),%[sum]\n" |
| 148 | " adcq (%[daddr]),%[sum]\n" |
| 149 | " adcq 8(%[daddr]),%[sum]\n" |
| 150 | " adcq $0,%[sum]\n" |
| 151 | |
Paolo Ciarrocchi | 0df025b | 2008-02-17 14:56:50 +0100 | [diff] [blame] | 152 | : [sum] "=r" (sum64) |
| 153 | : "[sum]" (rest), [saddr] "r" (saddr), [daddr] "r" (daddr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
Ingo Molnar | d76c1ae | 2008-02-17 16:48:25 +0100 | [diff] [blame] | 155 | return csum_fold( |
| 156 | (__force __wsum)add32_with_carry(sum64 & 0xffffffff, sum64>>32)); |
| 157 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | EXPORT_SYMBOL(csum_ipv6_magic); |