1 /* Copyright 2002,2003 Andi Kleen, SuSE Labs.
2 * Subject to the GNU Public License v.2
4 * Wrappers of assembly checksum functions for x86-64.
7 #include <asm/checksum.h>
8 #include <linux/module.h>
11 * csum_partial_copy_from_user - Copy and checksum from user space.
12 * @src: source address (user space)
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.
18 * Returns an 32bit unfolded checksum of the buffer.
19 * src and dst are best aligned to 64bits.
22 csum_partial_copy_from_user(const unsigned char __user *src, unsigned char *dst,
23 int len, unsigned int isum, int *errp)
27 if (likely(access_ok(VERIFY_READ,src, len))) {
28 /* Why 6, not 7? To handle odd addresses aligned we
29 would need to do considerable complications to fix the
30 checksum which is defined as an 16bit accumulator. The
31 fix alignment code is primarily for performance
32 compatibility with 32bit and that will handle odd
33 addresses slowly too. */
34 if (unlikely((unsigned long)src & 6)) {
35 while (((unsigned long)src & 6) && len >= 2) {
37 *errp = __get_user(val16, (__u16 __user *)src);
40 *(__u16 *)dst = val16;
41 isum = add32_with_carry(isum, val16);
47 isum = csum_partial_copy_generic((__force void *)src,dst,len,isum,errp,NULL);
48 if (likely(*errp == 0))
56 EXPORT_SYMBOL(csum_partial_copy_from_user);
59 * csum_partial_copy_to_user - Copy and checksum to user space.
60 * @src: source address
61 * @dst: destination address (user space)
62 * @len: number of bytes to be copied.
63 * @isum: initial sum that is added into the result (32bit unfolded)
64 * @errp: set to -EFAULT for an bad destination address.
66 * Returns an 32bit unfolded checksum of the buffer.
67 * src and dst are best aligned to 64bits.
70 csum_partial_copy_to_user(unsigned const char *src, unsigned char __user *dst,
71 int len, unsigned int isum, int *errp)
74 if (unlikely(!access_ok(VERIFY_WRITE, dst, len))) {
79 if (unlikely((unsigned long)dst & 6)) {
80 while (((unsigned long)dst & 6) && len >= 2) {
81 __u16 val16 = *(__u16 *)src;
82 isum = add32_with_carry(isum, val16);
83 *errp = __put_user(val16, (__u16 __user *)dst);
93 return csum_partial_copy_generic(src, (void __force *)dst,len,isum,NULL,errp);
96 EXPORT_SYMBOL(csum_partial_copy_to_user);
99 * csum_partial_copy_nocheck - Copy and checksum.
100 * @src: source address
101 * @dst: destination address
102 * @len: number of bytes to be copied.
103 * @isum: initial sum that is added into the result (32bit unfolded)
105 * Returns an 32bit unfolded checksum of the buffer.
108 csum_partial_copy_nocheck(const unsigned char *src, unsigned char *dst, int len, unsigned int sum)
110 return csum_partial_copy_generic(src,dst,len,sum,NULL,NULL);
112 EXPORT_SYMBOL(csum_partial_copy_nocheck);
114 unsigned short csum_ipv6_magic(struct in6_addr *saddr, struct in6_addr *daddr,
115 __u32 len, unsigned short proto, unsigned int sum)
119 rest = (__u64)htonl(len) + (__u64)htons(proto) + (__u64)sum;
120 asm(" addq (%[saddr]),%[sum]\n"
121 " adcq 8(%[saddr]),%[sum]\n"
122 " adcq (%[daddr]),%[sum]\n"
123 " adcq 8(%[daddr]),%[sum]\n"
126 : "[sum]" (rest),[saddr] "r" (saddr), [daddr] "r" (daddr));
127 return csum_fold(add32_with_carry(sum64 & 0xffffffff, sum64>>32));
130 EXPORT_SYMBOL(csum_ipv6_magic);