diff options
author | obrien <obrien@FreeBSD.org> | 2004-08-25 18:28:15 +0000 |
---|---|---|
committer | obrien <obrien@FreeBSD.org> | 2004-08-25 18:28:15 +0000 |
commit | cb4ed356122a2f5dc0a6f8bb63b829e814f51b0e (patch) | |
tree | 368045e7d687fd630bdc4ccb72edd6a3ecda10a2 /sys/i386/include | |
parent | 39a52b107cd9e7a4e84a37e51045a309f4bcce42 (diff) | |
download | FreeBSD-src-cb4ed356122a2f5dc0a6f8bb63b829e814f51b0e.zip FreeBSD-src-cb4ed356122a2f5dc0a6f8bb63b829e814f51b0e.tar.gz |
Fix a bug in in_cksum_hdr w/o -O.
The C code assumes that the carry bit is always kept from the previous
operation. However, the pointer indexing requires another add operation.
Thus, the carry bit from the first operation is tromped over by the
"addl" operation that ends up following it, so the "adcl" that follows
that has no effect because the carry bit is cleared before it.
The result is checksum failure on received packets.
The larger issue is that there isn't any other way of preventing the compiler
inserting arbitrary instructions between different __asm statements (and
that the commit message in revision 1.13 of in_cksum.h is wrong on
this point). From
http://developer.apple.com/documentation/DeveloperTools/gcc-3.3/gcc/Extended-Asm.html
---8<---8<---8<---
You can't expect a sequence of volatile asm instructions to remain
perfectly consecutive. If you want consecutive output, use a single
asm. Also, GCC will perform some optimizations across a volatile
asm instruction; GCC does not "forget everything" when it encounters
a volatile asm instruction the way some other compilers do.
---8<---8<---8<---
Also, this change also makes the ASM code much easier to read.
PR: 69257
Submitted by: Mike Bristow <mike@urgle.com>, Qing Li <qing.li@bluecoat.com>
Diffstat (limited to 'sys/i386/include')
-rw-r--r-- | sys/i386/include/in_cksum.h | 30 |
1 files changed, 14 insertions, 16 deletions
diff --git a/sys/i386/include/in_cksum.h b/sys/i386/include/in_cksum.h index d954f5e..27d1195 100644 --- a/sys/i386/include/in_cksum.h +++ b/sys/i386/include/in_cksum.h @@ -55,22 +55,20 @@ in_cksum_hdr(const struct ip *ip) { register u_int sum = 0; -/* __volatile is necessary here because the condition codes are used. */ -#define ADD(n) __asm __volatile ("addl %1, %0" : "+r" (sum) : \ - "g" (((const u_int32_t *)ip)[n / 4])) -#define ADDC(n) __asm __volatile ("adcl %1, %0" : "+r" (sum) : \ - "g" (((const u_int32_t *)ip)[n / 4])) -#define MOP __asm __volatile ("adcl $0, %0" : "+r" (sum)) - - ADD(0); - ADDC(4); - ADDC(8); - ADDC(12); - ADDC(16); - MOP; -#undef ADD -#undef ADDC -#undef MOP + __asm __volatile ( + "addl %1, %0\n" + "adcl %2, %0\n" + "adcl %3, %0\n" + "adcl %4, %0\n" + "adcl %5, %0\n" + "adcl $0, %0" + : "+r" (sum) + : "g" (((const u_int32_t *)ip)[0]), + "g" (((const u_int32_t *)ip)[1]), + "g" (((const u_int32_t *)ip)[2]), + "g" (((const u_int32_t *)ip)[3]), + "g" (((const u_int32_t *)ip)[4]) + ); sum = (sum & 0xffff) + (sum >> 16); if (sum > 0xffff) sum -= 0xffff; |