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.
54 CFI_ADJUST_CFA_OFFSET 4
57 CFI_ADJUST_CFA_OFFSET 4
59 movl 20(%esp),%eax # Function arg: unsigned int sum
60 movl 16(%esp),%ecx # Function arg: int len
61 movl 12(%esp),%esi # Function arg: unsigned char *buff
62 testl $3, %esi # Check alignment.
63 jz 2f # Jump if alignment is ok.
64 testl $1, %esi # Check alignment.
65 jz 10f # Jump if alignment is boundary of 2bytes.
77 subl $2, %ecx # Alignment uses up two bytes.
78 jae 1f # Jump if we had at least two bytes.
79 addl $2, %ecx # ecx was < 2. Deal with it.
113 shrl $2, %edx # This clears CF
136 CFI_ADJUST_CFA_OFFSET -4
139 CFI_ADJUST_CFA_OFFSET -4
143 ENDPROC(csum_partial)
147 /* Version for PentiumII/PPro */
152 CFI_ADJUST_CFA_OFFSET 4
153 CFI_REL_OFFSET esi, 0
155 CFI_ADJUST_CFA_OFFSET 4
156 CFI_REL_OFFSET ebx, 0
157 movl 20(%esp),%eax # Function arg: unsigned int sum
158 movl 16(%esp),%ecx # Function arg: int len
159 movl 12(%esp),%esi # Function arg: const unsigned char *buf
171 lea 45f(%ebx,%ebx,2), %ebx
175 # Handle 2-byte-aligned regions
199 movzbl (%esi),%ebx # csumming 1 byte, 2-aligned
204 addw (%esi), %ax # csumming 2 bytes, 2-aligned
209 addl -128(%esi), %eax
210 adcl -124(%esi), %eax
211 adcl -120(%esi), %eax
212 adcl -116(%esi), %eax
213 adcl -112(%esi), %eax
214 adcl -108(%esi), %eax
215 adcl -104(%esi), %eax
216 adcl -100(%esi), %eax
250 # Handle the last 1-3 bytes without jumping
251 notl %ecx # 1->2, 2->1, 3->0, higher bits are masked
252 movl $0xffffff,%ebx # by the shll and shrl instructions
255 andl -128(%esi),%ebx # esi is 4-aligned so should be ok
264 CFI_ADJUST_CFA_OFFSET -4
267 CFI_ADJUST_CFA_OFFSET -4
271 ENDPROC(csum_partial)
276 unsigned int csum_partial_copy_generic (const char *src, char *dst,
277 int len, int sum, int *src_err_ptr, int *dst_err_ptr)
281 * Copy from ds while checksumming, otherwise like csum_partial
283 * The macros SRC and DST specify the type of access for the instruction.
284 * thus we can call a custom exception handler for all access types.
286 * FIXME: could someone double-check whether I haven't mixed up some SRC and
287 * DST definitions? It's damn hard to trigger all cases. I hope I got
288 * them all but there's no guarantee.
293 .section __ex_table, "a"; \
294 .long 9999b, 6001f ; \
299 .section __ex_table, "a"; \
300 .long 9999b, 6002f ; \
303 #ifndef CONFIG_X86_USE_PPRO_CHECKSUM
308 ENTRY(csum_partial_copy_generic)
311 CFI_ADJUST_CFA_OFFSET 4
313 CFI_ADJUST_CFA_OFFSET 4
314 CFI_REL_OFFSET edi, 0
316 CFI_ADJUST_CFA_OFFSET 4
317 CFI_REL_OFFSET esi, 0
319 CFI_ADJUST_CFA_OFFSET 4
320 CFI_REL_OFFSET ebx, 0
321 movl ARGBASE+16(%esp),%eax # sum
322 movl ARGBASE+12(%esp),%ecx # len
323 movl ARGBASE+4(%esp),%esi # src
324 movl ARGBASE+8(%esp),%edi # dst
326 testl $2, %edi # Check alignment.
327 jz 2f # Jump if alignment is ok.
328 subl $2, %ecx # Alignment uses up two bytes.
329 jae 1f # Jump if we had at least two bytes.
330 addl $2, %ecx # ecx was < 2. Deal with it.
332 SRC(1: movw (%esi), %bx )
334 DST( movw %bx, (%edi) )
343 SRC(1: movl (%esi), %ebx )
344 SRC( movl 4(%esi), %edx )
346 DST( movl %ebx, (%edi) )
348 DST( movl %edx, 4(%edi) )
350 SRC( movl 8(%esi), %ebx )
351 SRC( movl 12(%esi), %edx )
353 DST( movl %ebx, 8(%edi) )
355 DST( movl %edx, 12(%edi) )
357 SRC( movl 16(%esi), %ebx )
358 SRC( movl 20(%esi), %edx )
360 DST( movl %ebx, 16(%edi) )
362 DST( movl %edx, 20(%edi) )
364 SRC( movl 24(%esi), %ebx )
365 SRC( movl 28(%esi), %edx )
367 DST( movl %ebx, 24(%edi) )
369 DST( movl %edx, 28(%edi) )
376 2: movl FP(%esp), %edx
380 shrl $2, %edx # This clears CF
381 SRC(3: movl (%esi), %ebx )
383 DST( movl %ebx, (%edi) )
393 SRC( movw (%esi), %cx )
395 DST( movw %cx, (%edi) )
399 SRC(5: movb (%esi), %cl )
400 DST( movb %cl, (%edi) )
407 .section .fixup, "ax"
410 movl ARGBASE+20(%esp), %ebx # src_err_ptr
411 movl $-EFAULT, (%ebx)
413 # zero the complete destination - computing the rest
415 movl ARGBASE+8(%esp), %edi # dst
416 movl ARGBASE+12(%esp), %ecx # len
423 movl ARGBASE+24(%esp), %ebx # dst_err_ptr
430 CFI_ADJUST_CFA_OFFSET -4
433 CFI_ADJUST_CFA_OFFSET -4
436 CFI_ADJUST_CFA_OFFSET -4
438 popl %ecx # equivalent to addl $4,%esp
439 CFI_ADJUST_CFA_OFFSET -4
442 ENDPROC(csum_partial_copy_generic)
446 /* Version for PentiumII/PPro */
449 SRC(movl x(%esi), %ebx ) ; \
451 DST(movl %ebx, x(%edi) ) ;
454 SRC(movl x(%esi), %ebx ) ; \
456 DST(movl %ebx, x(%edi) ) ;
460 ENTRY(csum_partial_copy_generic)
463 CFI_ADJUST_CFA_OFFSET 4
464 CFI_REL_OFFSET ebx, 0
466 CFI_ADJUST_CFA_OFFSET 4
467 CFI_REL_OFFSET edi, 0
469 CFI_ADJUST_CFA_OFFSET 4
470 CFI_REL_OFFSET esi, 0
471 movl ARGBASE+4(%esp),%esi #src
472 movl ARGBASE+8(%esp),%edi #dst
473 movl ARGBASE+12(%esp),%ecx #len
474 movl ARGBASE+16(%esp),%eax #sum
485 lea 3f(%ebx,%ebx), %ebx
490 SRC(movb -32(%edx),%bl) ; SRC(movb (%edx),%bl)
491 ROUND1(-64) ROUND(-60) ROUND(-56) ROUND(-52)
492 ROUND (-48) ROUND(-44) ROUND(-40) ROUND(-36)
493 ROUND (-32) ROUND(-28) ROUND(-24) ROUND(-20)
494 ROUND (-16) ROUND(-12) ROUND(-8) ROUND(-4)
499 4: movl ARGBASE+12(%esp),%edx #len
504 SRC( movw (%esi), %dx )
506 DST( movw %dx, (%edi) )
511 SRC( movb (%esi), %dl )
512 DST( movb %dl, (%edi) )
516 .section .fixup, "ax"
517 6001: movl ARGBASE+20(%esp), %ebx # src_err_ptr
518 movl $-EFAULT, (%ebx)
519 # zero the complete destination (computing the rest is too much work)
520 movl ARGBASE+8(%esp),%edi # dst
521 movl ARGBASE+12(%esp),%ecx # len
525 6002: movl ARGBASE+24(%esp), %ebx # dst_err_ptr
526 movl $-EFAULT, (%ebx)
531 CFI_ADJUST_CFA_OFFSET -4
534 CFI_ADJUST_CFA_OFFSET -4
537 CFI_ADJUST_CFA_OFFSET -4
541 ENDPROC(csum_partial_copy_generic)