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>
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.
55 movl 20(%esp),%eax # Function arg: unsigned int sum
56 movl 16(%esp),%ecx # Function arg: int len
57 movl 12(%esp),%esi # Function arg: unsigned char *buff
58 testl $3, %esi # Check alignment.
59 jz 2f # Jump if alignment is ok.
60 testl $1, %esi # Check alignment.
61 jz 10f # Jump if alignment is boundary of 2 bytes.
73 subl $2, %ecx # Alignment uses up two bytes.
74 jae 1f # Jump if we had at least two bytes.
75 addl $2, %ecx # ecx was < 2. Deal with it.
109 shrl $2, %edx # This clears CF
134 ENDPROC(csum_partial)
138 /* Version for PentiumII/PPro */
143 movl 20(%esp),%eax # Function arg: unsigned int sum
144 movl 16(%esp),%ecx # Function arg: int len
145 movl 12(%esp),%esi # Function arg: const unsigned char *buf
157 lea 45f(%ebx,%ebx,2), %ebx
161 # Handle 2-byte-aligned regions
185 movzbl (%esi),%ebx # csumming 1 byte, 2-aligned
190 addw (%esi), %ax # csumming 2 bytes, 2-aligned
195 addl -128(%esi), %eax
196 adcl -124(%esi), %eax
197 adcl -120(%esi), %eax
198 adcl -116(%esi), %eax
199 adcl -112(%esi), %eax
200 adcl -108(%esi), %eax
201 adcl -104(%esi), %eax
202 adcl -100(%esi), %eax
236 # Handle the last 1-3 bytes without jumping
237 notl %ecx # 1->2, 2->1, 3->0, higher bits are masked
238 movl $0xffffff,%ebx # by the shll and shrl instructions
241 andl -128(%esi),%ebx # esi is 4-aligned so should be ok
252 ENDPROC(csum_partial)
255 EXPORT_SYMBOL(csum_partial)
258 unsigned int csum_partial_copy_generic (const char *src, char *dst,
259 int len, int sum, int *src_err_ptr, int *dst_err_ptr)
263 * Copy from ds while checksumming, otherwise like csum_partial
265 * The macros SRC and DST specify the type of access for the instruction.
266 * thus we can call a custom exception handler for all access types.
268 * FIXME: could someone double-check whether I haven't mixed up some SRC and
269 * DST definitions? It's damn hard to trigger all cases. I hope I got
270 * them all but there's no guarantee.
275 _ASM_EXTABLE(9999b, 6001f)
279 _ASM_EXTABLE(9999b, 6002f)
281 #ifndef CONFIG_X86_USE_PPRO_CHECKSUM
286 ENTRY(csum_partial_copy_generic)
291 movl ARGBASE+16(%esp),%eax # sum
292 movl ARGBASE+12(%esp),%ecx # len
293 movl ARGBASE+4(%esp),%esi # src
294 movl ARGBASE+8(%esp),%edi # dst
296 testl $2, %edi # Check alignment.
297 jz 2f # Jump if alignment is ok.
298 subl $2, %ecx # Alignment uses up two bytes.
299 jae 1f # Jump if we had at least two bytes.
300 addl $2, %ecx # ecx was < 2. Deal with it.
302 SRC(1: movw (%esi), %bx )
304 DST( movw %bx, (%edi) )
313 SRC(1: movl (%esi), %ebx )
314 SRC( movl 4(%esi), %edx )
316 DST( movl %ebx, (%edi) )
318 DST( movl %edx, 4(%edi) )
320 SRC( movl 8(%esi), %ebx )
321 SRC( movl 12(%esi), %edx )
323 DST( movl %ebx, 8(%edi) )
325 DST( movl %edx, 12(%edi) )
327 SRC( movl 16(%esi), %ebx )
328 SRC( movl 20(%esi), %edx )
330 DST( movl %ebx, 16(%edi) )
332 DST( movl %edx, 20(%edi) )
334 SRC( movl 24(%esi), %ebx )
335 SRC( movl 28(%esi), %edx )
337 DST( movl %ebx, 24(%edi) )
339 DST( movl %edx, 28(%edi) )
346 2: movl FP(%esp), %edx
350 shrl $2, %edx # This clears CF
351 SRC(3: movl (%esi), %ebx )
353 DST( movl %ebx, (%edi) )
363 SRC( movw (%esi), %cx )
365 DST( movw %cx, (%edi) )
369 SRC(5: movb (%esi), %cl )
370 DST( movb %cl, (%edi) )
377 .section .fixup, "ax"
380 movl ARGBASE+20(%esp), %ebx # src_err_ptr
381 movl $-EFAULT, (%ebx)
383 # zero the complete destination - computing the rest
385 movl ARGBASE+8(%esp), %edi # dst
386 movl ARGBASE+12(%esp), %ecx # len
393 movl ARGBASE+24(%esp), %ebx # dst_err_ptr
402 popl %ecx # equivalent to addl $4,%esp
404 ENDPROC(csum_partial_copy_generic)
408 /* Version for PentiumII/PPro */
411 SRC(movl x(%esi), %ebx ) ; \
413 DST(movl %ebx, x(%edi) ) ;
416 SRC(movl x(%esi), %ebx ) ; \
418 DST(movl %ebx, x(%edi) ) ;
422 ENTRY(csum_partial_copy_generic)
426 movl ARGBASE+4(%esp),%esi #src
427 movl ARGBASE+8(%esp),%edi #dst
428 movl ARGBASE+12(%esp),%ecx #len
429 movl ARGBASE+16(%esp),%eax #sum
440 lea 3f(%ebx,%ebx), %ebx
445 SRC(movb -32(%edx),%bl) ; SRC(movb (%edx),%bl)
446 ROUND1(-64) ROUND(-60) ROUND(-56) ROUND(-52)
447 ROUND (-48) ROUND(-44) ROUND(-40) ROUND(-36)
448 ROUND (-32) ROUND(-28) ROUND(-24) ROUND(-20)
449 ROUND (-16) ROUND(-12) ROUND(-8) ROUND(-4)
454 4: movl ARGBASE+12(%esp),%edx #len
459 SRC( movw (%esi), %dx )
461 DST( movw %dx, (%edi) )
466 SRC( movb (%esi), %dl )
467 DST( movb %dl, (%edi) )
471 .section .fixup, "ax"
472 6001: movl ARGBASE+20(%esp), %ebx # src_err_ptr
473 movl $-EFAULT, (%ebx)
474 # zero the complete destination (computing the rest is too much work)
475 movl ARGBASE+8(%esp),%edi # dst
476 movl ARGBASE+12(%esp),%ecx # len
480 6002: movl ARGBASE+24(%esp), %ebx # dst_err_ptr
481 movl $-EFAULT, (%ebx)
489 ENDPROC(csum_partial_copy_generic)
495 EXPORT_SYMBOL(csum_partial_copy_generic)