Commit | Line | Data |
---|---|---|
d76c1ae4 IM |
1 | /* |
2 | * Copyright 2002, 2003 Andi Kleen, SuSE Labs. | |
1da177e4 | 3 | * Subject to the GNU Public License v.2 |
0df025b7 | 4 | * |
1da177e4 LT |
5 | * Wrappers of assembly checksum functions for x86-64. |
6 | */ | |
1da177e4 | 7 | #include <asm/checksum.h> |
e683014c | 8 | #include <linux/export.h> |
7263dda4 | 9 | #include <asm/smap.h> |
1da177e4 | 10 | |
0df025b7 PC |
11 | /** |
12 | * csum_partial_copy_from_user - Copy and checksum from user space. | |
13 | * @src: source address (user space) | |
1da177e4 LT |
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. | |
0df025b7 | 18 | * |
1da177e4 | 19 | * Returns an 32bit unfolded checksum of the buffer. |
0df025b7 PC |
20 | * src and dst are best aligned to 64bits. |
21 | */ | |
a4f89fb7 AV |
22 | __wsum |
23 | csum_partial_copy_from_user(const void __user *src, void *dst, | |
24 | int len, __wsum isum, int *errp) | |
0df025b7 | 25 | { |
1da177e4 LT |
26 | might_sleep(); |
27 | *errp = 0; | |
d76c1ae4 IM |
28 | |
29 | if (!likely(access_ok(VERIFY_READ, src, len))) | |
30 | goto out_err; | |
31 | ||
32 | /* | |
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. | |
39 | */ | |
40 | if (unlikely((unsigned long)src & 6)) { | |
41 | while (((unsigned long)src & 6) && len >= 2) { | |
42 | __u16 val16; | |
43 | ||
3b91270a LT |
44 | if (__get_user(val16, (const __u16 __user *)src)) |
45 | goto out_err; | |
d76c1ae4 IM |
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; | |
1da177e4 | 53 | } |
0df025b7 | 54 | } |
7263dda4 | 55 | stac(); |
d76c1ae4 IM |
56 | isum = csum_partial_copy_generic((__force const void *)src, |
57 | dst, len, isum, errp, NULL); | |
7263dda4 | 58 | clac(); |
d76c1ae4 IM |
59 | if (unlikely(*errp)) |
60 | goto out_err; | |
61 | ||
62 | return isum; | |
63 | ||
64 | out_err: | |
1da177e4 | 65 | *errp = -EFAULT; |
0df025b7 | 66 | memset(dst, 0, len); |
d76c1ae4 | 67 | |
0df025b7 PC |
68 | return isum; |
69 | } | |
1da177e4 LT |
70 | EXPORT_SYMBOL(csum_partial_copy_from_user); |
71 | ||
0df025b7 PC |
72 | /** |
73 | * csum_partial_copy_to_user - Copy and checksum to user space. | |
1da177e4 LT |
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. | |
0df025b7 | 79 | * |
1da177e4 LT |
80 | * Returns an 32bit unfolded checksum of the buffer. |
81 | * src and dst are best aligned to 64bits. | |
0df025b7 | 82 | */ |
a4f89fb7 AV |
83 | __wsum |
84 | csum_partial_copy_to_user(const void *src, void __user *dst, | |
85 | int len, __wsum isum, int *errp) | |
0df025b7 | 86 | { |
7263dda4 PA |
87 | __wsum ret; |
88 | ||
1da177e4 | 89 | might_sleep(); |
d76c1ae4 | 90 | |
1da177e4 LT |
91 | if (unlikely(!access_ok(VERIFY_WRITE, dst, len))) { |
92 | *errp = -EFAULT; | |
0df025b7 | 93 | return 0; |
1da177e4 LT |
94 | } |
95 | ||
96 | if (unlikely((unsigned long)dst & 6)) { | |
0df025b7 | 97 | while (((unsigned long)dst & 6) && len >= 2) { |
1da177e4 | 98 | __u16 val16 = *(__u16 *)src; |
d76c1ae4 | 99 | |
a4f89fb7 AV |
100 | isum = (__force __wsum)add32_with_carry( |
101 | (__force unsigned)isum, val16); | |
1da177e4 LT |
102 | *errp = __put_user(val16, (__u16 __user *)dst); |
103 | if (*errp) | |
104 | return isum; | |
0df025b7 PC |
105 | src += 2; |
106 | dst += 2; | |
1da177e4 LT |
107 | len -= 2; |
108 | } | |
109 | } | |
110 | ||
111 | *errp = 0; | |
7263dda4 PA |
112 | stac(); |
113 | ret = csum_partial_copy_generic(src, (void __force *)dst, | |
114 | len, isum, NULL, errp); | |
115 | clac(); | |
116 | return ret; | |
0df025b7 | 117 | } |
1da177e4 LT |
118 | EXPORT_SYMBOL(csum_partial_copy_to_user); |
119 | ||
0df025b7 | 120 | /** |
1da177e4 LT |
121 | * csum_partial_copy_nocheck - Copy and checksum. |
122 | * @src: source address | |
123 | * @dst: destination address | |
124 | * @len: number of bytes to be copied. | |
c15acff3 | 125 | * @sum: initial sum that is added into the result (32bit unfolded) |
0df025b7 | 126 | * |
1da177e4 | 127 | * Returns an 32bit unfolded checksum of the buffer. |
0df025b7 | 128 | */ |
a4f89fb7 AV |
129 | __wsum |
130 | csum_partial_copy_nocheck(const void *src, void *dst, int len, __wsum sum) | |
0df025b7 PC |
131 | { |
132 | return csum_partial_copy_generic(src, dst, len, sum, NULL, NULL); | |
133 | } | |
2ee60e17 | 134 | EXPORT_SYMBOL(csum_partial_copy_nocheck); |
1da177e4 | 135 | |
a4f89fb7 AV |
136 | __sum16 csum_ipv6_magic(const struct in6_addr *saddr, |
137 | const struct in6_addr *daddr, | |
1e940829 | 138 | __u32 len, __u8 proto, __wsum sum) |
1da177e4 LT |
139 | { |
140 | __u64 rest, sum64; | |
0df025b7 | 141 | |
a4f89fb7 AV |
142 | rest = (__force __u64)htonl(len) + (__force __u64)htons(proto) + |
143 | (__force __u64)sum; | |
d76c1ae4 IM |
144 | |
145 | asm(" addq (%[saddr]),%[sum]\n" | |
146 | " adcq 8(%[saddr]),%[sum]\n" | |
147 | " adcq (%[daddr]),%[sum]\n" | |
148 | " adcq 8(%[daddr]),%[sum]\n" | |
149 | " adcq $0,%[sum]\n" | |
150 | ||
0df025b7 PC |
151 | : [sum] "=r" (sum64) |
152 | : "[sum]" (rest), [saddr] "r" (saddr), [daddr] "r" (daddr)); | |
1da177e4 | 153 | |
d76c1ae4 IM |
154 | return csum_fold( |
155 | (__force __wsum)add32_with_carry(sum64 & 0xffffffff, sum64>>32)); | |
156 | } | |
1da177e4 | 157 | EXPORT_SYMBOL(csum_ipv6_magic); |