1 // SPDX-License-Identifier: GPL-2.0
3 * csum_partial_copy - do IP checksumming and copy
5 * (C) Copyright 1996 Linus Torvalds
6 * accelerated versions (and 21264 assembly versions ) contributed by
7 * Rick Gorton <rick.gorton@alpha-processor.com>
9 * Don't look at this too closely - you'll go mad. The things
10 * we do for performance..
13 #include <linux/types.h>
14 #include <linux/string.h>
15 #include <linux/uaccess.h>
16 #include <net/checksum.h>
20 __asm__ __volatile__("ldq_u %0,%1":"=r" (x):"m" (*(const unsigned long *)(y)))
23 __asm__ __volatile__("stq_u %1,%0":"=m" (*(unsigned long *)(y)):"r" (x))
25 #define extql(x,y,z) \
26 __asm__ __volatile__("extql %1,%2,%0":"=r" (z):"r" (x),"r" (y))
28 #define extqh(x,y,z) \
29 __asm__ __volatile__("extqh %1,%2,%0":"=r" (z):"r" (x),"r" (y))
31 #define mskql(x,y,z) \
32 __asm__ __volatile__("mskql %1,%2,%0":"=r" (z):"r" (x),"r" (y))
34 #define mskqh(x,y,z) \
35 __asm__ __volatile__("mskqh %1,%2,%0":"=r" (z):"r" (x),"r" (y))
37 #define insql(x,y,z) \
38 __asm__ __volatile__("insql %1,%2,%0":"=r" (z):"r" (x),"r" (y))
40 #define insqh(x,y,z) \
41 __asm__ __volatile__("insqh %1,%2,%0":"=r" (z):"r" (x),"r" (y))
43 #define __get_word(insn,x,ptr) \
46 __asm__ __volatile__( \
47 "1: "#insn" %0,%2\n" \
50 : "=r"(x), "=r"(__guu_err) \
51 : "m"(__m(ptr)), "1"(0)); \
55 static inline unsigned short from64to16(unsigned long x)
57 /* Using extract instructions is a bit more efficient
58 than the original shift/bitmask version. */
67 tmp_v.ul = (unsigned long) in_v.ui[0] + (unsigned long) in_v.ui[1];
69 /* Since the bits of tmp_v.sh[3] are going to always be zero,
70 we don't have to bother to add that in. */
71 out_v.ul = (unsigned long) tmp_v.us[0] + (unsigned long) tmp_v.us[1]
72 + (unsigned long) tmp_v.us[2];
74 /* Similarly, out_v.us[2] is always zero for the final add. */
75 return out_v.us[0] + out_v.us[1];
81 * Ok. This isn't fun, but this is the EASY case.
83 static inline unsigned long
84 csum_partial_cfu_aligned(const unsigned long __user *src, unsigned long *dst,
87 unsigned long checksum = ~0U;
88 unsigned long carry = 0;
92 if (__get_word(ldq, word, src))
98 carry = checksum < word;
105 unsigned long word, tmp;
106 if (__get_word(ldq, word, src))
109 mskql(word, len, word);
111 mskqh(tmp, len, tmp);
112 carry = checksum < word;
120 * This is even less fun, but this is still reasonably
123 static inline unsigned long
124 csum_partial_cfu_dest_aligned(const unsigned long __user *src,
130 unsigned long word, carry;
131 unsigned long lastsrc = 7+len+(unsigned long)src;
132 unsigned long checksum = ~0U;
134 if (__get_word(ldq_u, first,src))
138 unsigned long second;
140 if (__get_word(ldq_u, second, src+1))
142 extql(first, soff, word);
145 extqh(second, soff, first);
152 carry = checksum < word;
158 unsigned long second;
159 if (__get_word(ldq_u, second, lastsrc))
162 extql(first, soff, word);
163 extqh(second, soff, first);
165 mskql(word, len, word);
167 mskqh(tmp, len, tmp);
168 carry = checksum < word;
176 * This is slightly less fun than the above..
178 static inline unsigned long
179 csum_partial_cfu_src_aligned(const unsigned long __user *src,
183 unsigned long partial_dest)
185 unsigned long carry = 0;
187 unsigned long second_dest;
188 unsigned long checksum = ~0U;
190 mskql(partial_dest, doff, partial_dest);
192 if (__get_word(ldq, word, src))
195 insql(word, doff, second_dest);
197 stq_u(partial_dest | second_dest, dst);
200 insqh(word, doff, partial_dest);
201 carry = checksum < word;
207 if (__get_word(ldq, word, src))
209 mskql(word, len, word);
212 insql(word, doff, second_dest);
214 carry = checksum < word;
215 partial_dest |= second_dest;
217 stq_u(partial_dest, dst);
220 insqh(word, doff, partial_dest);
224 ldq_u(second_dest, dst);
225 mskqh(second_dest, doff, second_dest);
226 stq_u(partial_dest | second_dest, dst);
233 * This is so totally un-fun that it's frightening. Don't
234 * look at this too closely, you'll go blind.
236 static inline unsigned long
237 csum_partial_cfu_unaligned(const unsigned long __user * src,
239 unsigned long soff, unsigned long doff,
240 long len, unsigned long partial_dest)
242 unsigned long carry = 0;
244 unsigned long lastsrc;
245 unsigned long checksum = ~0U;
247 if (__get_word(ldq_u, first, src))
249 lastsrc = 7+len+(unsigned long)src;
250 mskql(partial_dest, doff, partial_dest);
252 unsigned long second, word;
253 unsigned long second_dest;
255 if (__get_word(ldq_u, second, src+1))
257 extql(first, soff, word);
260 extqh(second, soff, first);
264 insql(word, doff, second_dest);
266 stq_u(partial_dest | second_dest, dst);
267 carry = checksum < word;
268 insqh(word, doff, partial_dest);
274 unsigned long second, word;
275 unsigned long second_dest;
277 if (__get_word(ldq_u, second, lastsrc))
279 extql(first, soff, word);
280 extqh(second, soff, first);
283 mskql(word, len-doff, word);
285 insql(word, doff, second_dest);
286 carry = checksum < word;
287 stq_u(partial_dest | second_dest, dst);
289 ldq_u(second_dest, dst+1);
290 insqh(word, doff, partial_dest);
291 mskqh(second_dest, len, second_dest);
292 stq_u(partial_dest | second_dest, dst+1);
296 unsigned long second, word;
297 unsigned long second_dest;
299 if (__get_word(ldq_u, second, lastsrc))
301 extql(first, soff, word);
302 extqh(second, soff, first);
304 ldq_u(second_dest, dst);
305 mskql(word, len-doff, word);
307 mskqh(second_dest, len, second_dest);
308 carry = checksum < word;
309 insql(word, doff, word);
310 stq_u(partial_dest | word | second_dest, dst);
316 static __wsum __csum_and_copy(const void __user *src, void *dst, int len)
318 unsigned long soff = 7 & (unsigned long) src;
319 unsigned long doff = 7 & (unsigned long) dst;
320 unsigned long checksum;
324 checksum = csum_partial_cfu_aligned(
325 (const unsigned long __user *) src,
326 (unsigned long *) dst, len-8);
328 checksum = csum_partial_cfu_dest_aligned(
329 (const unsigned long __user *) src,
330 (unsigned long *) dst,
333 unsigned long partial_dest;
334 ldq_u(partial_dest, dst);
336 checksum = csum_partial_cfu_src_aligned(
337 (const unsigned long __user *) src,
338 (unsigned long *) dst,
339 doff, len-8, partial_dest);
341 checksum = csum_partial_cfu_unaligned(
342 (const unsigned long __user *) src,
343 (unsigned long *) dst,
344 soff, doff, len-8, partial_dest);
346 return (__force __wsum)from64to16 (checksum);
350 csum_and_copy_from_user(const void __user *src, void *dst, int len)
352 if (!access_ok(src, len))
354 return __csum_and_copy(src, dst, len);
358 csum_partial_copy_nocheck(const void *src, void *dst, int len)
360 return __csum_and_copy((__force const void __user *)src,
363 EXPORT_SYMBOL(csum_partial_copy_nocheck);