2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * IP/TCP/UDP checksumming routines
8 * Authors: Jorge Cwik, <jorge@laser.satlink.net>
9 * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
10 * Tom May, <ftom@netcom.com>
11 * Pentium Pro/II routines:
12 * Alexander Kjeldaas <astor@guardian.no>
13 * Finn Arne Gangstad <finnag@guardian.no>
14 * Lots of code moved from tcp.c and ip.c; see those files
17 * Changes: Ingo Molnar, converted csum_partial_copy() to 2.1 exception
19 * Andi Kleen, add zeroing on error
20 * converted to pure assembler
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License
24 * as published by the Free Software Foundation; either version
25 * 2 of the License, or (at your option) any later version.
28 #include <linux/linkage.h>
29 #include <asm/errno.h>
31 #include <asm/export.h>
32 #include <asm/nospec-branch.h>
35 * computes a partial checksum, e.g. for TCP/UDP fragments
39 unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum)
44 #ifndef CONFIG_X86_USE_PPRO_CHECKSUM
47 * Experiments with Ethernet and SLIP connections show that buff
48 * is aligned on either a 2-byte or 4-byte boundary. We get at
49 * least a twofold speedup on 486 and Pentium if it is 4-byte aligned.
50 * Fortunately, it is easy to convert 2-byte alignment to 4-byte
51 * alignment for the unrolled loop.
56 movl 20(%esp),%eax # Function arg: unsigned int sum
57 movl 16(%esp),%ecx # Function arg: int len
58 movl 12(%esp),%esi # Function arg: unsigned char *buff
59 testl $3, %esi # Check alignment.
60 jz 2f # Jump if alignment is ok.
61 testl $1, %esi # Check alignment.
62 jz 10f # Jump if alignment is boundary of 2 bytes.
74 subl $2, %ecx # Alignment uses up two bytes.
75 jae 1f # Jump if we had at least two bytes.
76 addl $2, %ecx # ecx was < 2. Deal with it.
110 shrl $2, %edx # This clears CF
135 ENDPROC(csum_partial)
139 /* Version for PentiumII/PPro */
144 movl 20(%esp),%eax # Function arg: unsigned int sum
145 movl 16(%esp),%ecx # Function arg: int len
146 movl 12(%esp),%esi # Function arg: const unsigned char *buf
158 lea 45f(%ebx,%ebx,2), %ebx
162 # Handle 2-byte-aligned regions
186 movzbl (%esi),%ebx # csumming 1 byte, 2-aligned
191 addw (%esi), %ax # csumming 2 bytes, 2-aligned
196 addl -128(%esi), %eax
197 adcl -124(%esi), %eax
198 adcl -120(%esi), %eax
199 adcl -116(%esi), %eax
200 adcl -112(%esi), %eax
201 adcl -108(%esi), %eax
202 adcl -104(%esi), %eax
203 adcl -100(%esi), %eax
237 # Handle the last 1-3 bytes without jumping
238 notl %ecx # 1->2, 2->1, 3->0, higher bits are masked
239 movl $0xffffff,%ebx # by the shll and shrl instructions
242 andl -128(%esi),%ebx # esi is 4-aligned so should be ok
253 ENDPROC(csum_partial)
256 EXPORT_SYMBOL(csum_partial)
259 unsigned int csum_partial_copy_generic (const char *src, char *dst,
260 int len, int sum, int *src_err_ptr, int *dst_err_ptr)
264 * Copy from ds while checksumming, otherwise like csum_partial
266 * The macros SRC and DST specify the type of access for the instruction.
267 * thus we can call a custom exception handler for all access types.
269 * FIXME: could someone double-check whether I haven't mixed up some SRC and
270 * DST definitions? It's damn hard to trigger all cases. I hope I got
271 * them all but there's no guarantee.
276 _ASM_EXTABLE(9999b, 6001f)
280 _ASM_EXTABLE(9999b, 6002f)
282 #ifndef CONFIG_X86_USE_PPRO_CHECKSUM
287 ENTRY(csum_partial_copy_generic)
292 movl ARGBASE+16(%esp),%eax # sum
293 movl ARGBASE+12(%esp),%ecx # len
294 movl ARGBASE+4(%esp),%esi # src
295 movl ARGBASE+8(%esp),%edi # dst
297 testl $2, %edi # Check alignment.
298 jz 2f # Jump if alignment is ok.
299 subl $2, %ecx # Alignment uses up two bytes.
300 jae 1f # Jump if we had at least two bytes.
301 addl $2, %ecx # ecx was < 2. Deal with it.
303 SRC(1: movw (%esi), %bx )
305 DST( movw %bx, (%edi) )
314 SRC(1: movl (%esi), %ebx )
315 SRC( movl 4(%esi), %edx )
317 DST( movl %ebx, (%edi) )
319 DST( movl %edx, 4(%edi) )
321 SRC( movl 8(%esi), %ebx )
322 SRC( movl 12(%esi), %edx )
324 DST( movl %ebx, 8(%edi) )
326 DST( movl %edx, 12(%edi) )
328 SRC( movl 16(%esi), %ebx )
329 SRC( movl 20(%esi), %edx )
331 DST( movl %ebx, 16(%edi) )
333 DST( movl %edx, 20(%edi) )
335 SRC( movl 24(%esi), %ebx )
336 SRC( movl 28(%esi), %edx )
338 DST( movl %ebx, 24(%edi) )
340 DST( movl %edx, 28(%edi) )
347 2: movl FP(%esp), %edx
351 shrl $2, %edx # This clears CF
352 SRC(3: movl (%esi), %ebx )
354 DST( movl %ebx, (%edi) )
364 SRC( movw (%esi), %cx )
366 DST( movw %cx, (%edi) )
370 SRC(5: movb (%esi), %cl )
371 DST( movb %cl, (%edi) )
378 .section .fixup, "ax"
381 movl ARGBASE+20(%esp), %ebx # src_err_ptr
382 movl $-EFAULT, (%ebx)
384 # zero the complete destination - computing the rest
386 movl ARGBASE+8(%esp), %edi # dst
387 movl ARGBASE+12(%esp), %ecx # len
394 movl ARGBASE+24(%esp), %ebx # dst_err_ptr
403 popl %ecx # equivalent to addl $4,%esp
405 ENDPROC(csum_partial_copy_generic)
409 /* Version for PentiumII/PPro */
412 SRC(movl x(%esi), %ebx ) ; \
414 DST(movl %ebx, x(%edi) ) ;
417 SRC(movl x(%esi), %ebx ) ; \
419 DST(movl %ebx, x(%edi) ) ;
423 ENTRY(csum_partial_copy_generic)
427 movl ARGBASE+4(%esp),%esi #src
428 movl ARGBASE+8(%esp),%edi #dst
429 movl ARGBASE+12(%esp),%ecx #len
430 movl ARGBASE+16(%esp),%eax #sum
441 lea 3f(%ebx,%ebx), %ebx
446 SRC(movb -32(%edx),%bl) ; SRC(movb (%edx),%bl)
447 ROUND1(-64) ROUND(-60) ROUND(-56) ROUND(-52)
448 ROUND (-48) ROUND(-44) ROUND(-40) ROUND(-36)
449 ROUND (-32) ROUND(-28) ROUND(-24) ROUND(-20)
450 ROUND (-16) ROUND(-12) ROUND(-8) ROUND(-4)
455 4: movl ARGBASE+12(%esp),%edx #len
460 SRC( movw (%esi), %dx )
462 DST( movw %dx, (%edi) )
467 SRC( movb (%esi), %dl )
468 DST( movb %dl, (%edi) )
472 .section .fixup, "ax"
473 6001: movl ARGBASE+20(%esp), %ebx # src_err_ptr
474 movl $-EFAULT, (%ebx)
475 # zero the complete destination (computing the rest is too much work)
476 movl ARGBASE+8(%esp),%edi # dst
477 movl ARGBASE+12(%esp),%ecx # len
481 6002: movl ARGBASE+24(%esp), %ebx # dst_err_ptr
482 movl $-EFAULT, (%ebx)
490 ENDPROC(csum_partial_copy_generic)
496 EXPORT_SYMBOL(csum_partial_copy_generic)