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.
58 movl 20(%esp),%eax # Function arg: unsigned int sum
59 movl 16(%esp),%ecx # Function arg: int len
60 movl 12(%esp),%esi # Function arg: unsigned char *buff
61 testl $3, %esi # Check alignment.
62 jz 2f # Jump if alignment is ok.
63 testl $1, %esi # Check alignment.
64 jz 10f # Jump if alignment is boundary of 2 bytes.
76 subl $2, %ecx # Alignment uses up two bytes.
77 jae 1f # Jump if we had at least two bytes.
78 addl $2, %ecx # ecx was < 2. Deal with it.
112 shrl $2, %edx # This clears CF
140 ENDPROC(csum_partial)
144 /* Version for PentiumII/PPro */
149 CFI_REL_OFFSET esi, 0
151 CFI_REL_OFFSET ebx, 0
152 movl 20(%esp),%eax # Function arg: unsigned int sum
153 movl 16(%esp),%ecx # Function arg: int len
154 movl 12(%esp),%esi # Function arg: const unsigned char *buf
166 lea 45f(%ebx,%ebx,2), %ebx
170 # Handle 2-byte-aligned regions
194 movzbl (%esi),%ebx # csumming 1 byte, 2-aligned
199 addw (%esi), %ax # csumming 2 bytes, 2-aligned
204 addl -128(%esi), %eax
205 adcl -124(%esi), %eax
206 adcl -120(%esi), %eax
207 adcl -116(%esi), %eax
208 adcl -112(%esi), %eax
209 adcl -108(%esi), %eax
210 adcl -104(%esi), %eax
211 adcl -100(%esi), %eax
245 # Handle the last 1-3 bytes without jumping
246 notl %ecx # 1->2, 2->1, 3->0, higher bits are masked
247 movl $0xffffff,%ebx # by the shll and shrl instructions
250 andl -128(%esi),%ebx # esi is 4-aligned so should be ok
264 ENDPROC(csum_partial)
269 unsigned int csum_partial_copy_generic (const char *src, char *dst,
270 int len, int sum, int *src_err_ptr, int *dst_err_ptr)
274 * Copy from ds while checksumming, otherwise like csum_partial
276 * The macros SRC and DST specify the type of access for the instruction.
277 * thus we can call a custom exception handler for all access types.
279 * FIXME: could someone double-check whether I haven't mixed up some SRC and
280 * DST definitions? It's damn hard to trigger all cases. I hope I got
281 * them all but there's no guarantee.
286 _ASM_EXTABLE(9999b, 6001f)
290 _ASM_EXTABLE(9999b, 6002f)
292 #ifndef CONFIG_X86_USE_PPRO_CHECKSUM
297 ENTRY(csum_partial_copy_generic)
300 CFI_ADJUST_CFA_OFFSET 4
302 CFI_REL_OFFSET edi, 0
304 CFI_REL_OFFSET esi, 0
306 CFI_REL_OFFSET ebx, 0
307 movl ARGBASE+16(%esp),%eax # sum
308 movl ARGBASE+12(%esp),%ecx # len
309 movl ARGBASE+4(%esp),%esi # src
310 movl ARGBASE+8(%esp),%edi # dst
312 testl $2, %edi # Check alignment.
313 jz 2f # Jump if alignment is ok.
314 subl $2, %ecx # Alignment uses up two bytes.
315 jae 1f # Jump if we had at least two bytes.
316 addl $2, %ecx # ecx was < 2. Deal with it.
318 SRC(1: movw (%esi), %bx )
320 DST( movw %bx, (%edi) )
329 SRC(1: movl (%esi), %ebx )
330 SRC( movl 4(%esi), %edx )
332 DST( movl %ebx, (%edi) )
334 DST( movl %edx, 4(%edi) )
336 SRC( movl 8(%esi), %ebx )
337 SRC( movl 12(%esi), %edx )
339 DST( movl %ebx, 8(%edi) )
341 DST( movl %edx, 12(%edi) )
343 SRC( movl 16(%esi), %ebx )
344 SRC( movl 20(%esi), %edx )
346 DST( movl %ebx, 16(%edi) )
348 DST( movl %edx, 20(%edi) )
350 SRC( movl 24(%esi), %ebx )
351 SRC( movl 28(%esi), %edx )
353 DST( movl %ebx, 24(%edi) )
355 DST( movl %edx, 28(%edi) )
362 2: movl FP(%esp), %edx
366 shrl $2, %edx # This clears CF
367 SRC(3: movl (%esi), %ebx )
369 DST( movl %ebx, (%edi) )
379 SRC( movw (%esi), %cx )
381 DST( movw %cx, (%edi) )
385 SRC(5: movb (%esi), %cl )
386 DST( movb %cl, (%edi) )
393 .section .fixup, "ax"
396 movl ARGBASE+20(%esp), %ebx # src_err_ptr
397 movl $-EFAULT, (%ebx)
399 # zero the complete destination - computing the rest
401 movl ARGBASE+8(%esp), %edi # dst
402 movl ARGBASE+12(%esp), %ecx # len
409 movl ARGBASE+24(%esp), %ebx # dst_err_ptr
421 popl_cfi %ecx # equivalent to addl $4,%esp
424 ENDPROC(csum_partial_copy_generic)
428 /* Version for PentiumII/PPro */
431 SRC(movl x(%esi), %ebx ) ; \
433 DST(movl %ebx, x(%edi) ) ;
436 SRC(movl x(%esi), %ebx ) ; \
438 DST(movl %ebx, x(%edi) ) ;
442 ENTRY(csum_partial_copy_generic)
445 CFI_REL_OFFSET ebx, 0
447 CFI_REL_OFFSET edi, 0
449 CFI_REL_OFFSET esi, 0
450 movl ARGBASE+4(%esp),%esi #src
451 movl ARGBASE+8(%esp),%edi #dst
452 movl ARGBASE+12(%esp),%ecx #len
453 movl ARGBASE+16(%esp),%eax #sum
464 lea 3f(%ebx,%ebx), %ebx
469 SRC(movb -32(%edx),%bl) ; SRC(movb (%edx),%bl)
470 ROUND1(-64) ROUND(-60) ROUND(-56) ROUND(-52)
471 ROUND (-48) ROUND(-44) ROUND(-40) ROUND(-36)
472 ROUND (-32) ROUND(-28) ROUND(-24) ROUND(-20)
473 ROUND (-16) ROUND(-12) ROUND(-8) ROUND(-4)
478 4: movl ARGBASE+12(%esp),%edx #len
483 SRC( movw (%esi), %dx )
485 DST( movw %dx, (%edi) )
490 SRC( movb (%esi), %dl )
491 DST( movb %dl, (%edi) )
495 .section .fixup, "ax"
496 6001: movl ARGBASE+20(%esp), %ebx # src_err_ptr
497 movl $-EFAULT, (%ebx)
498 # zero the complete destination (computing the rest is too much work)
499 movl ARGBASE+8(%esp),%edi # dst
500 movl ARGBASE+12(%esp),%ecx # len
504 6002: movl ARGBASE+24(%esp), %ebx # dst_err_ptr
505 movl $-EFAULT, (%ebx)
517 ENDPROC(csum_partial_copy_generic)