in_cksum.h revision 180073
1/*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29/*- 30 * Copyright (c) 2001 by Thomas Moestl <tmm@FreeBSD.org>. 31 * All rights reserved. 32 * 33 * Redistribution and use in source and binary forms, with or without 34 * modification, are permitted provided that the following conditions 35 * are met: 36 * 1. Redistributions of source code must retain the above copyright 37 * notice, this list of conditions and the following disclaimer. 38 * 2. Redistributions in binary form must reproduce the above copyright 39 * notice, this list of conditions and the following disclaimer in the 40 * documentation and/or other materials provided with the distribution. 41 * 42 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 43 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 44 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 45 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 46 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 47 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 48 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 49 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 51 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 52 * 53 * from tahoe: in_cksum.c 1.2 86/01/05 54 * from: @(#)in_cksum.c 1.3 (Berkeley) 1/19/91 55 * from: Id: in_cksum.c,v 1.8 1995/12/03 18:35:19 bde Exp 56 * from: FreeBSD: src/sys/alpha/include/in_cksum.h,v 1.5 2000/05/06 57 * 58 * $FreeBSD: head/sys/sparc64/include/in_cksum.h 180073 2008-06-27 22:17:14Z marius $ 59 */ 60 61#ifndef _MACHINE_IN_CKSUM_H_ 62#define _MACHINE_IN_CKSUM_H_ 1 63 64#include <sys/cdefs.h> 65 66#define in_cksum(m, len) in_cksum_skip(m, len, 0) 67 68static __inline void 69in_cksum_update(struct ip *ip) 70{ 71 int __tmp; 72 73 __tmp = (int)ip->ip_sum + 1; 74 ip->ip_sum = __tmp + (__tmp >> 16); 75} 76 77static __inline u_short 78in_addword(u_short sum, u_short b) 79{ 80 u_long __ret, __tmp; 81 82 __asm __volatile( 83 "sll %2, 16, %0\n" 84 "sll %3, 16, %1\n" 85 "addcc %0, %1, %0\n" 86 "srl %0, 16, %0\n" 87 "addc %0, 0, %0\n" 88 : "=&r" (__ret), "=&r" (__tmp) : "r" (sum), "r" (b) : "cc"); 89 return (__ret); 90} 91 92static __inline u_short 93in_pseudo(u_int sum, u_int b, u_int c) 94{ 95 u_long __tmp; 96 97 __asm __volatile( 98 "addcc %0, %3, %0\n" 99 "addccc %0, %4, %0\n" 100 "addc %0, 0, %0\n" 101 "sll %0, 16, %1\n" 102 "addcc %0, %1, %0\n" 103 "srl %0, 16, %0\n" 104 "addc %0, 0, %0\n" 105 : "=r" (sum), "=&r" (__tmp) : "0" (sum), "r" (b), "r" (c) : "cc"); 106 return (sum); 107} 108 109static __inline u_int 110in_cksum_hdr(struct ip *ip) 111{ 112 u_long __ret, __tmp1, __tmp2, __tmp3, __tmp4; 113 114 /* 115 * Use 32 bit memory accesses and additions - addition with carry only 116 * works for 32 bits, and fixing up alignment issues for 64 is probably 117 * more trouble than it's worth. 118 * This may read outside of the ip header, but does not cross a page 119 * boundary in doing so, so that should be OK. 120 * Actually, this specialized implementation might be overkill - using 121 * a generic implementation for both in_cksum_skip and in_cksum_hdr 122 * should not be too much more expensive. 123 */ 124#define __LD_ADD(addr, tmp, sum, offs, mod) \ 125 "lduw [" #addr " + " #offs "], " #tmp "\n" \ 126 "add" # mod " " #sum ", " #tmp ", " #sum "\n" 127 128 __asm __volatile( 129 "and %5, 3, %3\n" 130 "andn %5, 3, %1\n" 131 "brz,pt %3, 0f\n" 132 " lduw [%1], %0\n" 133 "mov 4, %4\n" 134 "sub %4, %3, %4\n" 135 "sll %4, 3, %4\n" /* fix up unaligned buffers */ 136 "mov 1, %2\n" 137 "sll %2, %4, %4\n" 138 "sub %4, 1, %4\n" 139 "lduw [%1 + 20], %2\n" 140 "andn %2, %4, %2\n" 141 "and %0, %4, %0\n" 142 "or %0, %2, %0\n" 143 "0:\n" 144 __LD_ADD(%1, %2, %0, 4, cc) 145 __LD_ADD(%1, %2, %0, 8, ccc) 146 __LD_ADD(%1, %2, %0, 12, ccc) 147 __LD_ADD(%1, %2, %0, 16, ccc) 148 "addc %0, 0, %0\n" /* reduce */ 149 "1:\n" 150 "sll %0, 16, %2\n" 151 "addcc %0, %2, %0\n" 152 "srl %0, 16, %0\n" 153 "addc %0, 0, %0\n" 154 "andcc %3, 1, %3\n" /* need to byte-swap? */ 155 "clr %3\n" 156 "bne,a,pn %%xcc, 1b\n" 157 " sll %0, 8, %0\n" 158 "not %0\n" 159 "sll %0, 16, %0\n" 160 "srl %0, 16, %0\n" 161 : "=&r" (__ret), "=r" (__tmp1), "=&r" (__tmp2), "=&r" (__tmp3), 162 "=&r" (__tmp4) : "1" (ip) : "cc"); 163#undef __LD_ADD 164 return (__ret); 165} 166 167u_short in_cksum_skip(struct mbuf *m, int len, int skip); 168 169#endif /* _MACHINE_IN_CKSUM_H_ */ 170