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>
33 * computes a partial checksum, e.g. for TCP/UDP fragments
37 unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum)
42 #ifndef CONFIG_X86_USE_PPRO_CHECKSUM
45 * Experiments with Ethernet and SLIP connections show that buff
46 * is aligned on either a 2-byte or 4-byte boundary. We get at
47 * least a twofold speedup on 486 and Pentium if it is 4-byte aligned.
48 * Fortunately, it is easy to convert 2-byte alignment to 4-byte
49 * alignment for the unrolled loop.
57 movl 20(%esp),%eax # Function arg: unsigned int sum
58 movl 16(%esp),%ecx # Function arg: int len
59 movl 12(%esp),%esi # Function arg: unsigned char *buff
60 testl $3, %esi # Check alignment.
61 jz 2f # Jump if alignment is ok.
62 testl $1, %esi # Check alignment.
63 jz 10f # Jump if alignment is boundary of 2bytes.
75 subl $2, %ecx # Alignment uses up two bytes.
76 jae 1f # Jump if we had at least two bytes.
77 addl $2, %ecx # ecx was < 2. Deal with it.
111 shrl $2, %edx # This clears CF
139 ENDPROC(csum_partial)
143 /* Version for PentiumII/PPro */
148 CFI_REL_OFFSET esi, 0
150 CFI_REL_OFFSET ebx, 0
151 movl 20(%esp),%eax # Function arg: unsigned int sum
152 movl 16(%esp),%ecx # Function arg: int len
153 movl 12(%esp),%esi # Function arg: const unsigned char *buf
165 lea 45f(%ebx,%ebx,2), %ebx
169 # Handle 2-byte-aligned regions
193 movzbl (%esi),%ebx # csumming 1 byte, 2-aligned
198 addw (%esi), %ax # csumming 2 bytes, 2-aligned
203 addl -128(%esi), %eax
204 adcl -124(%esi), %eax
205 adcl -120(%esi), %eax
206 adcl -116(%esi), %eax
207 adcl -112(%esi), %eax
208 adcl -108(%esi), %eax
209 adcl -104(%esi), %eax
210 adcl -100(%esi), %eax
244 # Handle the last 1-3 bytes without jumping
245 notl %ecx # 1->2, 2->1, 3->0, higher bits are masked
246 movl $0xffffff,%ebx # by the shll and shrl instructions
249 andl -128(%esi),%ebx # esi is 4-aligned so should be ok
263 ENDPROC(csum_partial)
268 unsigned int csum_partial_copy_generic (const char *src, char *dst,
269 int len, int sum, int *src_err_ptr, int *dst_err_ptr)
273 * Copy from ds while checksumming, otherwise like csum_partial
275 * The macros SRC and DST specify the type of access for the instruction.
276 * thus we can call a custom exception handler for all access types.
278 * FIXME: could someone double-check whether I haven't mixed up some SRC and
279 * DST definitions? It's damn hard to trigger all cases. I hope I got
280 * them all but there's no guarantee.
285 .section __ex_table, "a"; \
286 .long 9999b, 6001f ; \
291 .section __ex_table, "a"; \
292 .long 9999b, 6002f ; \
295 #ifndef CONFIG_X86_USE_PPRO_CHECKSUM
300 ENTRY(csum_partial_copy_generic)
303 CFI_ADJUST_CFA_OFFSET 4
305 CFI_REL_OFFSET edi, 0
307 CFI_REL_OFFSET esi, 0
309 CFI_REL_OFFSET ebx, 0
310 movl ARGBASE+16(%esp),%eax # sum
311 movl ARGBASE+12(%esp),%ecx # len
312 movl ARGBASE+4(%esp),%esi # src
313 movl ARGBASE+8(%esp),%edi # dst
315 testl $2, %edi # Check alignment.
316 jz 2f # Jump if alignment is ok.
317 subl $2, %ecx # Alignment uses up two bytes.
318 jae 1f # Jump if we had at least two bytes.
319 addl $2, %ecx # ecx was < 2. Deal with it.
321 SRC(1: movw (%esi), %bx )
323 DST( movw %bx, (%edi) )
332 SRC(1: movl (%esi), %ebx )
333 SRC( movl 4(%esi), %edx )
335 DST( movl %ebx, (%edi) )
337 DST( movl %edx, 4(%edi) )
339 SRC( movl 8(%esi), %ebx )
340 SRC( movl 12(%esi), %edx )
342 DST( movl %ebx, 8(%edi) )
344 DST( movl %edx, 12(%edi) )
346 SRC( movl 16(%esi), %ebx )
347 SRC( movl 20(%esi), %edx )
349 DST( movl %ebx, 16(%edi) )
351 DST( movl %edx, 20(%edi) )
353 SRC( movl 24(%esi), %ebx )
354 SRC( movl 28(%esi), %edx )
356 DST( movl %ebx, 24(%edi) )
358 DST( movl %edx, 28(%edi) )
365 2: movl FP(%esp), %edx
369 shrl $2, %edx # This clears CF
370 SRC(3: movl (%esi), %ebx )
372 DST( movl %ebx, (%edi) )
382 SRC( movw (%esi), %cx )
384 DST( movw %cx, (%edi) )
388 SRC(5: movb (%esi), %cl )
389 DST( movb %cl, (%edi) )
396 .section .fixup, "ax"
399 movl ARGBASE+20(%esp), %ebx # src_err_ptr
400 movl $-EFAULT, (%ebx)
402 # zero the complete destination - computing the rest
404 movl ARGBASE+8(%esp), %edi # dst
405 movl ARGBASE+12(%esp), %ecx # len
412 movl ARGBASE+24(%esp), %ebx # dst_err_ptr
424 popl_cfi %ecx # equivalent to addl $4,%esp
427 ENDPROC(csum_partial_copy_generic)
431 /* Version for PentiumII/PPro */
434 SRC(movl x(%esi), %ebx ) ; \
436 DST(movl %ebx, x(%edi) ) ;
439 SRC(movl x(%esi), %ebx ) ; \
441 DST(movl %ebx, x(%edi) ) ;
445 ENTRY(csum_partial_copy_generic)
448 CFI_REL_OFFSET ebx, 0
450 CFI_REL_OFFSET edi, 0
452 CFI_REL_OFFSET esi, 0
453 movl ARGBASE+4(%esp),%esi #src
454 movl ARGBASE+8(%esp),%edi #dst
455 movl ARGBASE+12(%esp),%ecx #len
456 movl ARGBASE+16(%esp),%eax #sum
467 lea 3f(%ebx,%ebx), %ebx
472 SRC(movb -32(%edx),%bl) ; SRC(movb (%edx),%bl)
473 ROUND1(-64) ROUND(-60) ROUND(-56) ROUND(-52)
474 ROUND (-48) ROUND(-44) ROUND(-40) ROUND(-36)
475 ROUND (-32) ROUND(-28) ROUND(-24) ROUND(-20)
476 ROUND (-16) ROUND(-12) ROUND(-8) ROUND(-4)
481 4: movl ARGBASE+12(%esp),%edx #len
486 SRC( movw (%esi), %dx )
488 DST( movw %dx, (%edi) )
493 SRC( movb (%esi), %dl )
494 DST( movb %dl, (%edi) )
498 .section .fixup, "ax"
499 6001: movl ARGBASE+20(%esp), %ebx # src_err_ptr
500 movl $-EFAULT, (%ebx)
501 # zero the complete destination (computing the rest is too much work)
502 movl ARGBASE+8(%esp),%edi # dst
503 movl ARGBASE+12(%esp),%ecx # len
507 6002: movl ARGBASE+24(%esp), %ebx # dst_err_ptr
508 movl $-EFAULT, (%ebx)
520 ENDPROC(csum_partial_copy_generic)