blob: 459b58a8a15cecd12c52ca0b48d19c0b57e4a4f0 [file] [log] [blame]
Ingo Molnard76c1ae2008-02-17 16:48:25 +01001/*
2 * Copyright 2002, 2003 Andi Kleen, SuSE Labs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Subject to the GNU Public License v.2
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +01004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Wrappers of assembly checksum functions for x86-64.
6 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <asm/checksum.h>
8#include <linux/module.h>
9
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +010010/**
11 * csum_partial_copy_from_user - Copy and checksum from user space.
12 * @src: source address (user space)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * @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 Ciarrocchi0df025b2008-02-17 14:56:50 +010017 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * Returns an 32bit unfolded checksum of the buffer.
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +010019 * src and dst are best aligned to 64bits.
20 */
Al Viroa4f89fb2006-11-14 21:20:08 -080021__wsum
22csum_partial_copy_from_user(const void __user *src, void *dst,
23 int len, __wsum isum, int *errp)
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +010024{
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 might_sleep();
26 *errp = 0;
Ingo Molnard76c1ae2008-02-17 16:48:25 +010027
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 Torvalds1da177e2005-04-16 15:20:36 -070053 }
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +010054 }
Ingo Molnard76c1ae2008-02-17 16:48:25 +010055 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
62out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 *errp = -EFAULT;
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +010064 memset(dst, 0, len);
Ingo Molnard76c1ae2008-02-17 16:48:25 +010065
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +010066 return isum;
67}
Linus Torvalds1da177e2005-04-16 15:20:36 -070068EXPORT_SYMBOL(csum_partial_copy_from_user);
69
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +010070/**
71 * csum_partial_copy_to_user - Copy and checksum to user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * @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 Ciarrocchi0df025b2008-02-17 14:56:50 +010077 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * Returns an 32bit unfolded checksum of the buffer.
79 * src and dst are best aligned to 64bits.
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +010080 */
Al Viroa4f89fb2006-11-14 21:20:08 -080081__wsum
82csum_partial_copy_to_user(const void *src, void __user *dst,
83 int len, __wsum isum, int *errp)
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +010084{
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 might_sleep();
Ingo Molnard76c1ae2008-02-17 16:48:25 +010086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (unlikely(!access_ok(VERIFY_WRITE, dst, len))) {
88 *errp = -EFAULT;
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +010089 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
91
92 if (unlikely((unsigned long)dst & 6)) {
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +010093 while (((unsigned long)dst & 6) && len >= 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 __u16 val16 = *(__u16 *)src;
Ingo Molnard76c1ae2008-02-17 16:48:25 +010095
Al Viroa4f89fb2006-11-14 21:20:08 -080096 isum = (__force __wsum)add32_with_carry(
97 (__force unsigned)isum, val16);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 *errp = __put_user(val16, (__u16 __user *)dst);
99 if (*errp)
100 return isum;
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +0100101 src += 2;
102 dst += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 len -= 2;
104 }
105 }
106
107 *errp = 0;
Ingo Molnard76c1ae2008-02-17 16:48:25 +0100108 return csum_partial_copy_generic(src, (void __force *)dst,
109 len, isum, NULL, errp);
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +0100110}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111EXPORT_SYMBOL(csum_partial_copy_to_user);
112
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +0100113/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * 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 Ciarrocchi0df025b2008-02-17 14:56:50 +0100119 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 * Returns an 32bit unfolded checksum of the buffer.
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +0100121 */
Al Viroa4f89fb2006-11-14 21:20:08 -0800122__wsum
123csum_partial_copy_nocheck(const void *src, void *dst, int len, __wsum sum)
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +0100124{
125 return csum_partial_copy_generic(src, dst, len, sum, NULL, NULL);
126}
Andi Kleen2ee60e172006-06-26 13:59:44 +0200127EXPORT_SYMBOL(csum_partial_copy_nocheck);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Al Viroa4f89fb2006-11-14 21:20:08 -0800129__sum16 csum_ipv6_magic(const struct in6_addr *saddr,
130 const struct in6_addr *daddr,
131 __u32 len, unsigned short proto, __wsum sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 __u64 rest, sum64;
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +0100134
Al Viroa4f89fb2006-11-14 21:20:08 -0800135 rest = (__force __u64)htonl(len) + (__force __u64)htons(proto) +
136 (__force __u64)sum;
Ingo Molnard76c1ae2008-02-17 16:48:25 +0100137
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 Ciarrocchi0df025b2008-02-17 14:56:50 +0100144 : [sum] "=r" (sum64)
145 : "[sum]" (rest), [saddr] "r" (saddr), [daddr] "r" (daddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Ingo Molnard76c1ae2008-02-17 16:48:25 +0100147 return csum_fold(
148 (__force __wsum)add32_with_carry(sum64 & 0xffffffff, sum64>>32));
149}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150EXPORT_SYMBOL(csum_ipv6_magic);