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 <asm/errno.h>
31 * computes a partial checksum, e.g. for TCP/UDP fragments
35 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 movl 20(%esp),%eax # Function arg: unsigned int sum
55 movl 16(%esp),%ecx # Function arg: int len
56 movl 12(%esp),%esi # Function arg: unsigned char *buff
57 testl $2, %esi # Check alignment.
58 jz 2f # Jump if alignment is ok.
59 subl $2, %ecx # Alignment uses up two bytes.
60 jae 1f # Jump if we had at least two bytes.
61 addl $2, %ecx # ecx was < 2. Deal with it.
95 shrl $2, %edx # This clears CF
119 /* Version for PentiumII/PPro */
124 movl 20(%esp),%eax # Function arg: unsigned int sum
125 movl 16(%esp),%ecx # Function arg: int len
126 movl 12(%esp),%esi # Function arg: const unsigned char *buf
138 lea 45f(%ebx,%ebx,2), %ebx
142 # Handle 2-byte-aligned regions
151 movzbl (%esi),%ebx # csumming 1 byte, 2-aligned
156 addw (%esi), %ax # csumming 2 bytes, 2-aligned
161 addl -128(%esi), %eax
162 adcl -124(%esi), %eax
163 adcl -120(%esi), %eax
164 adcl -116(%esi), %eax
165 adcl -112(%esi), %eax
166 adcl -108(%esi), %eax
167 adcl -104(%esi), %eax
168 adcl -100(%esi), %eax
202 # Handle the last 1-3 bytes without jumping
203 notl %ecx # 1->2, 2->1, 3->0, higher bits are masked
204 movl $0xffffff,%ebx # by the shll and shrl instructions
207 andl -128(%esi),%ebx # esi is 4-aligned so should be ok
218 unsigned int csum_partial_copy_generic (const char *src, char *dst,
219 int len, int sum, int *src_err_ptr, int *dst_err_ptr)
225 .section __ex_table, "a"; \
226 .long 9999b, 6001f ; \
231 .section __ex_table, "a"; \
232 .long 9999b, 6002f ; \
237 #ifndef CONFIG_X86_USE_PPRO_CHECKSUM
242 csum_partial_copy_generic_i386:
247 movl ARGBASE+16(%esp),%eax # sum
248 movl ARGBASE+12(%esp),%ecx # len
249 movl ARGBASE+4(%esp),%esi # src
250 movl ARGBASE+8(%esp),%edi # dst
252 testl $2, %edi # Check alignment.
253 jz 2f # Jump if alignment is ok.
254 subl $2, %ecx # Alignment uses up two bytes.
255 jae 1f # Jump if we had at least two bytes.
256 addl $2, %ecx # ecx was < 2. Deal with it.
258 SRC(1: movw (%esi), %bx )
260 DST( movw %bx, (%edi) )
269 SRC(1: movl (%esi), %ebx )
270 SRC( movl 4(%esi), %edx )
272 DST( movl %ebx, (%edi) )
274 DST( movl %edx, 4(%edi) )
276 SRC( movl 8(%esi), %ebx )
277 SRC( movl 12(%esi), %edx )
279 DST( movl %ebx, 8(%edi) )
281 DST( movl %edx, 12(%edi) )
283 SRC( movl 16(%esi), %ebx )
284 SRC( movl 20(%esi), %edx )
286 DST( movl %ebx, 16(%edi) )
288 DST( movl %edx, 20(%edi) )
290 SRC( movl 24(%esi), %ebx )
291 SRC( movl 28(%esi), %edx )
293 DST( movl %ebx, 24(%edi) )
295 DST( movl %edx, 28(%edi) )
302 2: movl FP(%esp), %edx
306 shrl $2, %edx # This clears CF
307 SRC(3: movl (%esi), %ebx )
309 DST( movl %ebx, (%edi) )
319 SRC( movw (%esi), %cx )
321 DST( movw %cx, (%edi) )
325 SRC(5: movb (%esi), %cl )
326 DST( movb %cl, (%edi) )
333 .section .fixup, "ax"
336 movl ARGBASE+20(%esp), %ebx # src_err_ptr
337 movl $-EFAULT, (%ebx)
339 # zero the complete destination - computing the rest
341 movl ARGBASE+8(%esp), %edi # dst
342 movl ARGBASE+12(%esp), %ecx # len
349 movl ARGBASE+24(%esp), %ebx # dst_err_ptr
358 popl %ecx # equivalent to addl $4,%esp
363 /* Version for PentiumII/PPro */
366 SRC(movl x(%esi), %ebx ) ; \
368 DST(movl %ebx, x(%edi) ) ;
371 SRC(movl x(%esi), %ebx ) ; \
373 DST(movl %ebx, x(%edi) ) ;
377 csum_partial_copy_generic_i386:
381 movl ARGBASE+4(%esp),%esi #src
382 movl ARGBASE+8(%esp),%edi #dst
383 movl ARGBASE+12(%esp),%ecx #len
384 movl ARGBASE+16(%esp),%eax #sum
395 lea 3f(%ebx,%ebx), %ebx
400 SRC(movb -32(%edx),%bl) ; SRC(movb (%edx),%bl)
401 ROUND1(-64) ROUND(-60) ROUND(-56) ROUND(-52)
402 ROUND (-48) ROUND(-44) ROUND(-40) ROUND(-36)
403 ROUND (-32) ROUND(-28) ROUND(-24) ROUND(-20)
404 ROUND (-16) ROUND(-12) ROUND(-8) ROUND(-4)
409 4: movl ARGBASE+12(%esp),%edx #len
414 SRC( movw (%esi), %dx )
416 DST( movw %dx, (%edi) )
421 SRC( movb (%esi), %dl )
422 DST( movb %dl, (%edi) )
426 .section .fixup, "ax"
427 6001: movl ARGBASE+20(%esp), %ebx # src_err_ptr
428 movl $-EFAULT, (%ebx)
429 # zero the complete destination (computing the rest is too much work)
430 movl ARGBASE+8(%esp),%edi # dst
431 movl ARGBASE+12(%esp),%ecx # len
435 6002: movl ARGBASE+24(%esp), %ebx # dst_err_ptr
436 movl $-EFAULT, (%ebx)