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/config.h>
29 #include <asm/errno.h>
32 * computes a partial checksum, e.g. for TCP/UDP fragments
36 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 $2, %esi # Check alignment.
59 jz 2f # Jump if alignment is ok.
60 subl $2, %ecx # Alignment uses up two bytes.
61 jae 1f # Jump if we had at least two bytes.
62 addl $2, %ecx # ecx was < 2. Deal with it.
96 shrl $2, %edx # This clears CF
120 /* Version for PentiumII/PPro */
125 movl 20(%esp),%eax # Function arg: unsigned int sum
126 movl 16(%esp),%ecx # Function arg: int len
127 movl 12(%esp),%esi # Function arg: const unsigned char *buf
139 lea 45f(%ebx,%ebx,2), %ebx
143 # Handle 2-byte-aligned regions
152 movzbl (%esi),%ebx # csumming 1 byte, 2-aligned
157 addw (%esi), %ax # csumming 2 bytes, 2-aligned
162 addl -128(%esi), %eax
163 adcl -124(%esi), %eax
164 adcl -120(%esi), %eax
165 adcl -116(%esi), %eax
166 adcl -112(%esi), %eax
167 adcl -108(%esi), %eax
168 adcl -104(%esi), %eax
169 adcl -100(%esi), %eax
203 # Handle the last 1-3 bytes without jumping
204 notl %ecx # 1->2, 2->1, 3->0, higher bits are masked
205 movl $0xffffff,%ebx # by the shll and shrl instructions
208 andl -128(%esi),%ebx # esi is 4-aligned so should be ok
219 unsigned int csum_partial_copy_generic (const char *src, char *dst,
220 int len, int sum, int *src_err_ptr, int *dst_err_ptr)
224 * Copy from ds while checksumming, otherwise like csum_partial
226 * The macros SRC and DST specify the type of access for the instruction.
227 * thus we can call a custom exception handler for all access types.
229 * FIXME: could someone double-check whether I haven't mixed up some SRC and
230 * DST definitions? It's damn hard to trigger all cases. I hope I got
231 * them all but there's no guarantee.
236 .section __ex_table, "a"; \
237 .long 9999b, 6001f ; \
242 .section __ex_table, "a"; \
243 .long 9999b, 6002f ; \
247 .globl csum_partial_copy_generic_i386
249 #ifndef CONFIG_X86_USE_PPRO_CHECKSUM
254 csum_partial_copy_generic_i386:
259 movl ARGBASE+16(%esp),%eax # sum
260 movl ARGBASE+12(%esp),%ecx # len
261 movl ARGBASE+4(%esp),%esi # src
262 movl ARGBASE+8(%esp),%edi # dst
264 testl $2, %edi # Check alignment.
265 jz 2f # Jump if alignment is ok.
266 subl $2, %ecx # Alignment uses up two bytes.
267 jae 1f # Jump if we had at least two bytes.
268 addl $2, %ecx # ecx was < 2. Deal with it.
270 SRC(1: movw (%esi), %bx )
272 DST( movw %bx, (%edi) )
281 SRC(1: movl (%esi), %ebx )
282 SRC( movl 4(%esi), %edx )
284 DST( movl %ebx, (%edi) )
286 DST( movl %edx, 4(%edi) )
288 SRC( movl 8(%esi), %ebx )
289 SRC( movl 12(%esi), %edx )
291 DST( movl %ebx, 8(%edi) )
293 DST( movl %edx, 12(%edi) )
295 SRC( movl 16(%esi), %ebx )
296 SRC( movl 20(%esi), %edx )
298 DST( movl %ebx, 16(%edi) )
300 DST( movl %edx, 20(%edi) )
302 SRC( movl 24(%esi), %ebx )
303 SRC( movl 28(%esi), %edx )
305 DST( movl %ebx, 24(%edi) )
307 DST( movl %edx, 28(%edi) )
314 2: movl FP(%esp), %edx
318 shrl $2, %edx # This clears CF
319 SRC(3: movl (%esi), %ebx )
321 DST( movl %ebx, (%edi) )
331 SRC( movw (%esi), %cx )
333 DST( movw %cx, (%edi) )
337 SRC(5: movb (%esi), %cl )
338 DST( movb %cl, (%edi) )
345 .section .fixup, "ax"
348 movl ARGBASE+20(%esp), %ebx # src_err_ptr
349 movl $-EFAULT, (%ebx)
351 # zero the complete destination - computing the rest
353 movl ARGBASE+8(%esp), %edi # dst
354 movl ARGBASE+12(%esp), %ecx # len
361 movl ARGBASE+24(%esp), %ebx # dst_err_ptr
370 popl %ecx # equivalent to addl $4,%esp
375 /* Version for PentiumII/PPro */
378 SRC(movl x(%esi), %ebx ) ; \
380 DST(movl %ebx, x(%edi) ) ;
383 SRC(movl x(%esi), %ebx ) ; \
385 DST(movl %ebx, x(%edi) ) ;
389 csum_partial_copy_generic_i386:
393 movl ARGBASE+4(%esp),%esi #src
394 movl ARGBASE+8(%esp),%edi #dst
395 movl ARGBASE+12(%esp),%ecx #len
396 movl ARGBASE+16(%esp),%eax #sum
407 lea 3f(%ebx,%ebx), %ebx
412 SRC(movb -32(%edx),%bl) ; SRC(movb (%edx),%bl)
413 ROUND1(-64) ROUND(-60) ROUND(-56) ROUND(-52)
414 ROUND (-48) ROUND(-44) ROUND(-40) ROUND(-36)
415 ROUND (-32) ROUND(-28) ROUND(-24) ROUND(-20)
416 ROUND (-16) ROUND(-12) ROUND(-8) ROUND(-4)
421 4: movl ARGBASE+12(%esp),%edx #len
426 SRC( movw (%esi), %dx )
428 DST( movw %dx, (%edi) )
433 SRC( movb (%esi), %dl )
434 DST( movb %dl, (%edi) )
438 .section .fixup, "ax"
439 6001: movl ARGBASE+20(%esp), %ebx # src_err_ptr
440 movl $-EFAULT, (%ebx)
441 # zero the complete destination (computing the rest is too much work)
442 movl ARGBASE+8(%esp),%edi # dst
443 movl ARGBASE+12(%esp),%ecx # len
447 6002: movl ARGBASE+24(%esp), %ebx # dst_err_ptr
448 movl $-EFAULT, (%ebx)