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>
19 __asm__ __volatile__("ldq_u %0,%1":"=r" (x):"m" (*(const unsigned long *)(y)))
22 __asm__ __volatile__("stq_u %1,%0":"=m" (*(unsigned long *)(y)):"r" (x))
24 #define extql(x,y,z) \
25 __asm__ __volatile__("extql %1,%2,%0":"=r" (z):"r" (x),"r" (y))
27 #define extqh(x,y,z) \
28 __asm__ __volatile__("extqh %1,%2,%0":"=r" (z):"r" (x),"r" (y))
30 #define mskql(x,y,z) \
31 __asm__ __volatile__("mskql %1,%2,%0":"=r" (z):"r" (x),"r" (y))
33 #define mskqh(x,y,z) \
34 __asm__ __volatile__("mskqh %1,%2,%0":"=r" (z):"r" (x),"r" (y))
36 #define insql(x,y,z) \
37 __asm__ __volatile__("insql %1,%2,%0":"=r" (z):"r" (x),"r" (y))
39 #define insqh(x,y,z) \
40 __asm__ __volatile__("insqh %1,%2,%0":"=r" (z):"r" (x),"r" (y))
43 #define __get_user_u(x,ptr) \
46 __asm__ __volatile__( \
50 : "=r"(x), "=r"(__guu_err) \
51 : "m"(__m(ptr)), "1"(0)); \
55 #define __put_user_u(x,ptr) \
58 __asm__ __volatile__( \
63 : "m"(__m(addr)), "rJ"(x), "0"(0)); \
68 static inline unsigned short from64to16(unsigned long x
)
70 /* Using extract instructions is a bit more efficient
71 than the original shift/bitmask version. */
80 tmp_v
.ul
= (unsigned long) in_v
.ui
[0] + (unsigned long) in_v
.ui
[1];
82 /* Since the bits of tmp_v.sh[3] are going to always be zero,
83 we don't have to bother to add that in. */
84 out_v
.ul
= (unsigned long) tmp_v
.us
[0] + (unsigned long) tmp_v
.us
[1]
85 + (unsigned long) tmp_v
.us
[2];
87 /* Similarly, out_v.us[2] is always zero for the final add. */
88 return out_v
.us
[0] + out_v
.us
[1];
94 * Ok. This isn't fun, but this is the EASY case.
96 static inline unsigned long
97 csum_partial_cfu_aligned(const unsigned long __user
*src
, unsigned long *dst
,
98 long len
, unsigned long checksum
,
101 unsigned long carry
= 0;
106 err
|= __get_user(word
, src
);
111 carry
= checksum
< word
;
118 unsigned long word
, tmp
;
119 err
|= __get_user(word
, src
);
121 mskql(word
, len
, word
);
123 mskqh(tmp
, len
, tmp
);
124 carry
= checksum
< word
;
128 if (err
&& errp
) *errp
= err
;
133 * This is even less fun, but this is still reasonably
136 static inline unsigned long
137 csum_partial_cfu_dest_aligned(const unsigned long __user
*src
,
140 long len
, unsigned long checksum
,
144 unsigned long word
, carry
;
145 unsigned long lastsrc
= 7+len
+(unsigned long)src
;
148 err
|= __get_user_u(first
,src
);
151 unsigned long second
;
153 err
|= __get_user_u(second
, src
+1);
154 extql(first
, soff
, word
);
157 extqh(second
, soff
, first
);
164 carry
= checksum
< word
;
170 unsigned long second
;
171 err
|= __get_user_u(second
, lastsrc
);
173 extql(first
, soff
, word
);
174 extqh(second
, soff
, first
);
176 mskql(word
, len
, word
);
178 mskqh(tmp
, len
, tmp
);
179 carry
= checksum
< word
;
183 if (err
&& errp
) *errp
= err
;
188 * This is slightly less fun than the above..
190 static inline unsigned long
191 csum_partial_cfu_src_aligned(const unsigned long __user
*src
,
194 long len
, unsigned long checksum
,
195 unsigned long partial_dest
,
198 unsigned long carry
= 0;
200 unsigned long second_dest
;
203 mskql(partial_dest
, doff
, partial_dest
);
205 err
|= __get_user(word
, src
);
207 insql(word
, doff
, second_dest
);
209 stq_u(partial_dest
| second_dest
, dst
);
212 insqh(word
, doff
, partial_dest
);
213 carry
= checksum
< word
;
219 err
|= __get_user(word
, src
);
220 mskql(word
, len
, word
);
223 insql(word
, doff
, second_dest
);
225 carry
= checksum
< word
;
226 partial_dest
|= second_dest
;
228 stq_u(partial_dest
, dst
);
231 insqh(word
, doff
, partial_dest
);
235 ldq_u(second_dest
, dst
);
236 mskqh(second_dest
, doff
, second_dest
);
237 stq_u(partial_dest
| second_dest
, dst
);
240 if (err
&& errp
) *errp
= err
;
245 * This is so totally un-fun that it's frightening. Don't
246 * look at this too closely, you'll go blind.
248 static inline unsigned long
249 csum_partial_cfu_unaligned(const unsigned long __user
* src
,
251 unsigned long soff
, unsigned long doff
,
252 long len
, unsigned long checksum
,
253 unsigned long partial_dest
,
256 unsigned long carry
= 0;
258 unsigned long lastsrc
;
261 err
|= __get_user_u(first
, src
);
262 lastsrc
= 7+len
+(unsigned long)src
;
263 mskql(partial_dest
, doff
, partial_dest
);
265 unsigned long second
, word
;
266 unsigned long second_dest
;
268 err
|= __get_user_u(second
, src
+1);
269 extql(first
, soff
, word
);
272 extqh(second
, soff
, first
);
276 insql(word
, doff
, second_dest
);
278 stq_u(partial_dest
| second_dest
, dst
);
279 carry
= checksum
< word
;
280 insqh(word
, doff
, partial_dest
);
286 unsigned long second
, word
;
287 unsigned long second_dest
;
289 err
|= __get_user_u(second
, lastsrc
);
290 extql(first
, soff
, word
);
291 extqh(second
, soff
, first
);
294 mskql(word
, len
-doff
, word
);
296 insql(word
, doff
, second_dest
);
297 carry
= checksum
< word
;
298 stq_u(partial_dest
| second_dest
, dst
);
300 ldq_u(second_dest
, dst
+1);
301 insqh(word
, doff
, partial_dest
);
302 mskqh(second_dest
, len
, second_dest
);
303 stq_u(partial_dest
| second_dest
, dst
+1);
307 unsigned long second
, word
;
308 unsigned long second_dest
;
310 err
|= __get_user_u(second
, lastsrc
);
311 extql(first
, soff
, word
);
312 extqh(second
, soff
, first
);
314 ldq_u(second_dest
, dst
);
315 mskql(word
, len
-doff
, word
);
317 mskqh(second_dest
, len
, second_dest
);
318 carry
= checksum
< word
;
319 insql(word
, doff
, word
);
320 stq_u(partial_dest
| word
| second_dest
, dst
);
323 if (err
&& errp
) *errp
= err
;
328 csum_partial_copy_from_user(const void __user
*src
, void *dst
, int len
,
329 __wsum sum
, int *errp
)
331 unsigned long checksum
= (__force u32
) sum
;
332 unsigned long soff
= 7 & (unsigned long) src
;
333 unsigned long doff
= 7 & (unsigned long) dst
;
336 if (!access_ok(src
, len
)) {
337 if (errp
) *errp
= -EFAULT
;
343 checksum
= csum_partial_cfu_aligned(
344 (const unsigned long __user
*) src
,
345 (unsigned long *) dst
,
346 len
-8, checksum
, errp
);
348 checksum
= csum_partial_cfu_dest_aligned(
349 (const unsigned long __user
*) src
,
350 (unsigned long *) dst
,
351 soff
, len
-8, checksum
, errp
);
353 unsigned long partial_dest
;
354 ldq_u(partial_dest
, dst
);
356 checksum
= csum_partial_cfu_src_aligned(
357 (const unsigned long __user
*) src
,
358 (unsigned long *) dst
,
359 doff
, len
-8, checksum
,
362 checksum
= csum_partial_cfu_unaligned(
363 (const unsigned long __user
*) src
,
364 (unsigned long *) dst
,
365 soff
, doff
, len
-8, checksum
,
368 checksum
= from64to16 (checksum
);
370 return (__force __wsum
)checksum
;
372 EXPORT_SYMBOL(csum_partial_copy_from_user
);
375 csum_partial_copy_nocheck(const void *src
, void *dst
, int len
, __wsum sum
)
378 mm_segment_t oldfs
= get_fs();
380 checksum
= csum_partial_copy_from_user((__force
const void __user
*)src
,
381 dst
, len
, sum
, NULL
);
385 EXPORT_SYMBOL(csum_partial_copy_nocheck
);