1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
7 * IP/TCP/UDP checksumming routines
9 * Authors: Jorge Cwik, <jorge@laser.satlink.net>
10 * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
11 * Tom May, <ftom@netcom.com>
12 * Pentium Pro/II routines:
13 * Alexander Kjeldaas <astor@guardian.no>
14 * Finn Arne Gangstad <finnag@guardian.no>
15 * Lots of code moved from tcp.c and ip.c; see those files
18 * Changes: Ingo Molnar, converted csum_partial_copy() to 2.1 exception
20 * Andi Kleen, add zeroing on error
21 * converted to pure assembler
24 #include <linux/linkage.h>
25 #include <asm/errno.h>
27 #include <asm/export.h>
28 #include <asm/nospec-branch.h>
31 * computes a partial checksum, e.g. for TCP/UDP fragments
35 unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum)
40 #ifndef CONFIG_X86_USE_PPRO_CHECKSUM
43 * Experiments with Ethernet and SLIP connections show that buff
44 * is aligned on either a 2-byte or 4-byte boundary. We get at
45 * least a twofold speedup on 486 and Pentium if it is 4-byte aligned.
46 * Fortunately, it is easy to convert 2-byte alignment to 4-byte
47 * alignment for the unrolled loop.
49 SYM_FUNC_START(csum_partial)
52 movl 20(%esp),%eax # Function arg: unsigned int sum
53 movl 16(%esp),%ecx # Function arg: int len
54 movl 12(%esp),%esi # Function arg: unsigned char *buff
55 testl $3, %esi # Check alignment.
56 jz 2f # Jump if alignment is ok.
57 testl $1, %esi # Check alignment.
58 jz 10f # Jump if alignment is boundary of 2 bytes.
70 subl $2, %ecx # Alignment uses up two bytes.
71 jae 1f # Jump if we had at least two bytes.
72 addl $2, %ecx # ecx was < 2. Deal with it.
106 shrl $2, %edx # This clears CF
131 SYM_FUNC_END(csum_partial)
135 /* Version for PentiumII/PPro */
137 SYM_FUNC_START(csum_partial)
140 movl 20(%esp),%eax # Function arg: unsigned int sum
141 movl 16(%esp),%ecx # Function arg: int len
142 movl 12(%esp),%esi # Function arg: const unsigned char *buf
154 lea 45f(%ebx,%ebx,2), %ebx
158 # Handle 2-byte-aligned regions
182 movzbl (%esi),%ebx # csumming 1 byte, 2-aligned
187 addw (%esi), %ax # csumming 2 bytes, 2-aligned
192 addl -128(%esi), %eax
193 adcl -124(%esi), %eax
194 adcl -120(%esi), %eax
195 adcl -116(%esi), %eax
196 adcl -112(%esi), %eax
197 adcl -108(%esi), %eax
198 adcl -104(%esi), %eax
199 adcl -100(%esi), %eax
233 # Handle the last 1-3 bytes without jumping
234 notl %ecx # 1->2, 2->1, 3->0, higher bits are masked
235 movl $0xffffff,%ebx # by the shll and shrl instructions
238 andl -128(%esi),%ebx # esi is 4-aligned so should be ok
249 SYM_FUNC_END(csum_partial)
252 EXPORT_SYMBOL(csum_partial)
255 unsigned int csum_partial_copy_generic (const char *src, char *dst,
256 int len, int sum, int *src_err_ptr, int *dst_err_ptr)
260 * Copy from ds while checksumming, otherwise like csum_partial
262 * The macros SRC and DST specify the type of access for the instruction.
263 * thus we can call a custom exception handler for all access types.
265 * FIXME: could someone double-check whether I haven't mixed up some SRC and
266 * DST definitions? It's damn hard to trigger all cases. I hope I got
267 * them all but there's no guarantee.
272 _ASM_EXTABLE_UA(9999b, 6001f)
276 _ASM_EXTABLE_UA(9999b, 6002f)
278 #ifndef CONFIG_X86_USE_PPRO_CHECKSUM
283 SYM_FUNC_START(csum_partial_copy_generic)
288 movl ARGBASE+16(%esp),%eax # sum
289 movl ARGBASE+12(%esp),%ecx # len
290 movl ARGBASE+4(%esp),%esi # src
291 movl ARGBASE+8(%esp),%edi # dst
293 testl $2, %edi # Check alignment.
294 jz 2f # Jump if alignment is ok.
295 subl $2, %ecx # Alignment uses up two bytes.
296 jae 1f # Jump if we had at least two bytes.
297 addl $2, %ecx # ecx was < 2. Deal with it.
299 SRC(1: movw (%esi), %bx )
301 DST( movw %bx, (%edi) )
310 SRC(1: movl (%esi), %ebx )
311 SRC( movl 4(%esi), %edx )
313 DST( movl %ebx, (%edi) )
315 DST( movl %edx, 4(%edi) )
317 SRC( movl 8(%esi), %ebx )
318 SRC( movl 12(%esi), %edx )
320 DST( movl %ebx, 8(%edi) )
322 DST( movl %edx, 12(%edi) )
324 SRC( movl 16(%esi), %ebx )
325 SRC( movl 20(%esi), %edx )
327 DST( movl %ebx, 16(%edi) )
329 DST( movl %edx, 20(%edi) )
331 SRC( movl 24(%esi), %ebx )
332 SRC( movl 28(%esi), %edx )
334 DST( movl %ebx, 24(%edi) )
336 DST( movl %edx, 28(%edi) )
343 2: movl FP(%esp), %edx
347 shrl $2, %edx # This clears CF
348 SRC(3: movl (%esi), %ebx )
350 DST( movl %ebx, (%edi) )
360 SRC( movw (%esi), %cx )
362 DST( movw %cx, (%edi) )
366 SRC(5: movb (%esi), %cl )
367 DST( movb %cl, (%edi) )
374 .section .fixup, "ax"
377 movl ARGBASE+20(%esp), %ebx # src_err_ptr
378 movl $-EFAULT, (%ebx)
380 # zero the complete destination - computing the rest
382 movl ARGBASE+8(%esp), %edi # dst
383 movl ARGBASE+12(%esp), %ecx # len
390 movl ARGBASE+24(%esp), %ebx # dst_err_ptr
399 popl %ecx # equivalent to addl $4,%esp
401 SYM_FUNC_END(csum_partial_copy_generic)
405 /* Version for PentiumII/PPro */
408 SRC(movl x(%esi), %ebx ) ; \
410 DST(movl %ebx, x(%edi) ) ;
413 SRC(movl x(%esi), %ebx ) ; \
415 DST(movl %ebx, x(%edi) ) ;
419 SYM_FUNC_START(csum_partial_copy_generic)
423 movl ARGBASE+4(%esp),%esi #src
424 movl ARGBASE+8(%esp),%edi #dst
425 movl ARGBASE+12(%esp),%ecx #len
426 movl ARGBASE+16(%esp),%eax #sum
437 lea 3f(%ebx,%ebx), %ebx
442 SRC(movb -32(%edx),%bl) ; SRC(movb (%edx),%bl)
443 ROUND1(-64) ROUND(-60) ROUND(-56) ROUND(-52)
444 ROUND (-48) ROUND(-44) ROUND(-40) ROUND(-36)
445 ROUND (-32) ROUND(-28) ROUND(-24) ROUND(-20)
446 ROUND (-16) ROUND(-12) ROUND(-8) ROUND(-4)
451 4: movl ARGBASE+12(%esp),%edx #len
456 SRC( movw (%esi), %dx )
458 DST( movw %dx, (%edi) )
463 SRC( movb (%esi), %dl )
464 DST( movb %dl, (%edi) )
468 .section .fixup, "ax"
469 6001: movl ARGBASE+20(%esp), %ebx # src_err_ptr
470 movl $-EFAULT, (%ebx)
471 # zero the complete destination (computing the rest is too much work)
472 movl ARGBASE+8(%esp),%edi # dst
473 movl ARGBASE+12(%esp),%ecx # len
477 6002: movl ARGBASE+24(%esp), %ebx # dst_err_ptr
478 movl $-EFAULT, (%ebx)
486 SYM_FUNC_END(csum_partial_copy_generic)
492 EXPORT_SYMBOL(csum_partial_copy_generic)