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