2 * Copyright 2002, 2003 Andi Kleen, SuSE Labs.
3 * Subject to the GNU Public License v.2
5 * Wrappers of assembly checksum functions for x86-64.
7 #include <asm/checksum.h>
8 #include <linux/module.h>
12 * csum_partial_copy_from_user - Copy and checksum from user space.
13 * @src: source address (user space)
14 * @dst: destination address
15 * @len: number of bytes to be copied.
16 * @isum: initial sum that is added into the result (32bit unfolded)
17 * @errp: set to -EFAULT for an bad source address.
19 * Returns an 32bit unfolded checksum of the buffer.
20 * src and dst are best aligned to 64bits.
23 csum_partial_copy_from_user(const void __user *src, void *dst,
24 int len, __wsum isum, int *errp)
29 if (!likely(access_ok(VERIFY_READ, src, len)))
33 * Why 6, not 7? To handle odd addresses aligned we
34 * would need to do considerable complications to fix the
35 * checksum which is defined as an 16bit accumulator. The
36 * fix alignment code is primarily for performance
37 * compatibility with 32bit and that will handle odd
38 * addresses slowly too.
40 if (unlikely((unsigned long)src & 6)) {
41 while (((unsigned long)src & 6) && len >= 2) {
44 if (__get_user(val16, (const __u16 __user *)src))
47 *(__u16 *)dst = val16;
48 isum = (__force __wsum)add32_with_carry(
49 (__force unsigned)isum, val16);
56 isum = csum_partial_copy_generic((__force const void *)src,
57 dst, len, isum, errp, NULL);
70 EXPORT_SYMBOL(csum_partial_copy_from_user);
73 * csum_partial_copy_to_user - Copy and checksum to user space.
74 * @src: source address
75 * @dst: destination address (user space)
76 * @len: number of bytes to be copied.
77 * @isum: initial sum that is added into the result (32bit unfolded)
78 * @errp: set to -EFAULT for an bad destination address.
80 * Returns an 32bit unfolded checksum of the buffer.
81 * src and dst are best aligned to 64bits.
84 csum_partial_copy_to_user(const void *src, void __user *dst,
85 int len, __wsum isum, int *errp)
91 if (unlikely(!access_ok(VERIFY_WRITE, dst, len))) {
96 if (unlikely((unsigned long)dst & 6)) {
97 while (((unsigned long)dst & 6) && len >= 2) {
98 __u16 val16 = *(__u16 *)src;
100 isum = (__force __wsum)add32_with_carry(
101 (__force unsigned)isum, val16);
102 *errp = __put_user(val16, (__u16 __user *)dst);
113 ret = csum_partial_copy_generic(src, (void __force *)dst,
114 len, isum, NULL, errp);
118 EXPORT_SYMBOL(csum_partial_copy_to_user);
121 * csum_partial_copy_nocheck - Copy and checksum.
122 * @src: source address
123 * @dst: destination address
124 * @len: number of bytes to be copied.
125 * @sum: initial sum that is added into the result (32bit unfolded)
127 * Returns an 32bit unfolded checksum of the buffer.
130 csum_partial_copy_nocheck(const void *src, void *dst, int len, __wsum sum)
132 return csum_partial_copy_generic(src, dst, len, sum, NULL, NULL);
134 EXPORT_SYMBOL(csum_partial_copy_nocheck);
136 __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
137 const struct in6_addr *daddr,
138 __u32 len, unsigned short proto, __wsum sum)
142 rest = (__force __u64)htonl(len) + (__force __u64)htons(proto) +
145 asm(" addq (%[saddr]),%[sum]\n"
146 " adcq 8(%[saddr]),%[sum]\n"
147 " adcq (%[daddr]),%[sum]\n"
148 " adcq 8(%[daddr]),%[sum]\n"
152 : "[sum]" (rest), [saddr] "r" (saddr), [daddr] "r" (daddr));
155 (__force __wsum)add32_with_carry(sum64 & 0xffffffff, sum64>>32));
157 EXPORT_SYMBOL(csum_ipv6_magic);