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/dwarf2.h>
30 #include <asm/errno.h>
34 * computes a partial checksum, e.g. for TCP/UDP fragments
38 unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum)
43 #ifndef CONFIG_X86_USE_PPRO_CHECKSUM
46 * Experiments with Ethernet and SLIP connections show that buff
47 * is aligned on either a 2-byte or 4-byte boundary. We get at
48 * least a twofold speedup on 486 and Pentium if it is 4-byte aligned.
49 * Fortunately, it is easy to convert 2-byte alignment to 4-byte
50 * 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
136 ENDPROC(csum_partial)
140 /* Version for PentiumII/PPro */
146 movl 20(%esp),%eax # Function arg: unsigned int sum
147 movl 16(%esp),%ecx # Function arg: int len
148 movl 12(%esp),%esi # Function arg: const unsigned char *buf
160 lea 45f(%ebx,%ebx,2), %ebx
164 # Handle 2-byte-aligned regions
188 movzbl (%esi),%ebx # csumming 1 byte, 2-aligned
193 addw (%esi), %ax # csumming 2 bytes, 2-aligned
198 addl -128(%esi), %eax
199 adcl -124(%esi), %eax
200 adcl -120(%esi), %eax
201 adcl -116(%esi), %eax
202 adcl -112(%esi), %eax
203 adcl -108(%esi), %eax
204 adcl -104(%esi), %eax
205 adcl -100(%esi), %eax
239 # Handle the last 1-3 bytes without jumping
240 notl %ecx # 1->2, 2->1, 3->0, higher bits are masked
241 movl $0xffffff,%ebx # by the shll and shrl instructions
244 andl -128(%esi),%ebx # esi is 4-aligned so should be ok
256 ENDPROC(csum_partial)
261 unsigned int csum_partial_copy_generic (const char *src, char *dst,
262 int len, int sum, int *src_err_ptr, int *dst_err_ptr)
266 * Copy from ds while checksumming, otherwise like csum_partial
268 * The macros SRC and DST specify the type of access for the instruction.
269 * thus we can call a custom exception handler for all access types.
271 * FIXME: could someone double-check whether I haven't mixed up some SRC and
272 * DST definitions? It's damn hard to trigger all cases. I hope I got
273 * them all but there's no guarantee.
278 _ASM_EXTABLE(9999b, 6001f)
282 _ASM_EXTABLE(9999b, 6002f)
284 #ifndef CONFIG_X86_USE_PPRO_CHECKSUM
289 ENTRY(csum_partial_copy_generic)
292 CFI_ADJUST_CFA_OFFSET 4
296 movl ARGBASE+16(%esp),%eax # sum
297 movl ARGBASE+12(%esp),%ecx # len
298 movl ARGBASE+4(%esp),%esi # src
299 movl ARGBASE+8(%esp),%edi # dst
301 testl $2, %edi # Check alignment.
302 jz 2f # Jump if alignment is ok.
303 subl $2, %ecx # Alignment uses up two bytes.
304 jae 1f # Jump if we had at least two bytes.
305 addl $2, %ecx # ecx was < 2. Deal with it.
307 SRC(1: movw (%esi), %bx )
309 DST( movw %bx, (%edi) )
318 SRC(1: movl (%esi), %ebx )
319 SRC( movl 4(%esi), %edx )
321 DST( movl %ebx, (%edi) )
323 DST( movl %edx, 4(%edi) )
325 SRC( movl 8(%esi), %ebx )
326 SRC( movl 12(%esi), %edx )
328 DST( movl %ebx, 8(%edi) )
330 DST( movl %edx, 12(%edi) )
332 SRC( movl 16(%esi), %ebx )
333 SRC( movl 20(%esi), %edx )
335 DST( movl %ebx, 16(%edi) )
337 DST( movl %edx, 20(%edi) )
339 SRC( movl 24(%esi), %ebx )
340 SRC( movl 28(%esi), %edx )
342 DST( movl %ebx, 24(%edi) )
344 DST( movl %edx, 28(%edi) )
351 2: movl FP(%esp), %edx
355 shrl $2, %edx # This clears CF
356 SRC(3: movl (%esi), %ebx )
358 DST( movl %ebx, (%edi) )
368 SRC( movw (%esi), %cx )
370 DST( movw %cx, (%edi) )
374 SRC(5: movb (%esi), %cl )
375 DST( movb %cl, (%edi) )
382 .section .fixup, "ax"
385 movl ARGBASE+20(%esp), %ebx # src_err_ptr
386 movl $-EFAULT, (%ebx)
388 # zero the complete destination - computing the rest
390 movl ARGBASE+8(%esp), %edi # dst
391 movl ARGBASE+12(%esp), %ecx # len
398 movl ARGBASE+24(%esp), %ebx # dst_err_ptr
407 popl_cfi %ecx # equivalent to addl $4,%esp
410 ENDPROC(csum_partial_copy_generic)
414 /* Version for PentiumII/PPro */
417 SRC(movl x(%esi), %ebx ) ; \
419 DST(movl %ebx, x(%edi) ) ;
422 SRC(movl x(%esi), %ebx ) ; \
424 DST(movl %ebx, x(%edi) ) ;
428 ENTRY(csum_partial_copy_generic)
433 movl ARGBASE+4(%esp),%esi #src
434 movl ARGBASE+8(%esp),%edi #dst
435 movl ARGBASE+12(%esp),%ecx #len
436 movl ARGBASE+16(%esp),%eax #sum
447 lea 3f(%ebx,%ebx), %ebx
452 SRC(movb -32(%edx),%bl) ; SRC(movb (%edx),%bl)
453 ROUND1(-64) ROUND(-60) ROUND(-56) ROUND(-52)
454 ROUND (-48) ROUND(-44) ROUND(-40) ROUND(-36)
455 ROUND (-32) ROUND(-28) ROUND(-24) ROUND(-20)
456 ROUND (-16) ROUND(-12) ROUND(-8) ROUND(-4)
461 4: movl ARGBASE+12(%esp),%edx #len
466 SRC( movw (%esi), %dx )
468 DST( movw %dx, (%edi) )
473 SRC( movb (%esi), %dl )
474 DST( movb %dl, (%edi) )
478 .section .fixup, "ax"
479 6001: movl ARGBASE+20(%esp), %ebx # src_err_ptr
480 movl $-EFAULT, (%ebx)
481 # zero the complete destination (computing the rest is too much work)
482 movl ARGBASE+8(%esp),%edi # dst
483 movl ARGBASE+12(%esp),%ecx # len
487 6002: movl ARGBASE+24(%esp), %ebx # dst_err_ptr
488 movl $-EFAULT, (%ebx)
497 ENDPROC(csum_partial_copy_generic)