File: /usr/src/linux/include/asm-sparc/checksum.h

1     /* $Id: checksum.h,v 1.31 2000/01/31 01:26:52 davem Exp $ */
2     #ifndef __SPARC_CHECKSUM_H
3     #define __SPARC_CHECKSUM_H
4     
5     /*  checksum.h:  IP/UDP/TCP checksum routines on the Sparc.
6      *
7      *  Copyright(C) 1995 Linus Torvalds
8      *  Copyright(C) 1995 Miguel de Icaza
9      *  Copyright(C) 1996 David S. Miller
10      *  Copyright(C) 1996 Eddie C. Dost
11      *  Copyright(C) 1997 Jakub Jelinek
12      *
13      * derived from:
14      *	Alpha checksum c-code
15      *      ix86 inline assembly
16      *      RFC1071 Computing the Internet Checksum
17      */
18      
19     #include <asm/uaccess.h>
20     #include <asm/cprefix.h>
21     
22     /* computes the checksum of a memory block at buff, length len,
23      * and adds in "sum" (32-bit)
24      *
25      * returns a 32-bit number suitable for feeding into itself
26      * or csum_tcpudp_magic
27      *
28      * this function must be called with even lengths, except
29      * for the last fragment, which may be odd
30      *
31      * it's best to have buff aligned on a 32-bit boundary
32      */
33     extern unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum);
34     
35     /* the same as csum_partial, but copies from fs:src while it
36      * checksums
37      *
38      * here even more important to align src and dst on a 32-bit (or even
39      * better 64-bit) boundary
40      */
41     
42     /* FIXME: Remove these two macros ASAP */
43     #define csum_partial_copy(src, dst, len, sum) \
44      		       csum_partial_copy_nocheck(src,dst,len,sum)
45     #define csum_partial_copy_fromuser(s, d, l, w)  \
46                              csum_partial_copy((char *) (s), (d), (l), (w))
47       
48     extern unsigned int __csum_partial_copy_sparc_generic (const char *, char *);
49     
50     extern __inline__ unsigned int 
51     csum_partial_copy_nocheck (const char *src, char *dst, int len, 
52     			   unsigned int sum)
53     {
54     	register unsigned int ret asm("o0") = (unsigned int)src;
55     	register char *d asm("o1") = dst;
56     	register int l asm("g1") = len;
57     	
58     	__asm__ __volatile__ ("
59     		call " C_LABEL_STR(__csum_partial_copy_sparc_generic) "
60     		 mov %4, %%g7
61     	" : "=r" (ret) : "0" (ret), "r" (d), "r" (l), "r" (sum) :
62     	"o1", "o2", "o3", "o4", "o5", "o7", "g1", "g2", "g3", "g4", "g5", "g7");
63     	return ret;
64     }
65     
66     extern __inline__ unsigned int 
67     csum_partial_copy_from_user(const char *src, char *dst, int len, 
68     			    unsigned int sum, int *err)
69       {
70     	if (!access_ok (VERIFY_READ, src, len)) {
71     		*err = -EFAULT;
72     		memset (dst, 0, len);
73     		return sum;
74     	} else {
75     		register unsigned int ret asm("o0") = (unsigned int)src;
76     		register char *d asm("o1") = dst;
77     		register int l asm("g1") = len;
78     		register unsigned int s asm("g7") = sum;
79     
80     		__asm__ __volatile__ ("
81     		.section __ex_table,#alloc
82     		.align 4
83     		.word 1f,2
84     		.previous
85     1:
86     		call " C_LABEL_STR(__csum_partial_copy_sparc_generic) "
87     		 st %5, [%%sp + 64]
88     		" : "=r" (ret) : "0" (ret), "r" (d), "r" (l), "r" (s), "r" (err) :
89     		"o1", "o2", "o3", "o4", "o5", "o7", "g1", "g2", "g3", "g4", "g5", "g7");
90     		return ret;
91     	}
92       }
93       
94     extern __inline__ unsigned int 
95     csum_partial_copy_to_user(const char *src, char *dst, int len, 
96     			  unsigned int sum, int *err)
97     {
98     	if (!access_ok (VERIFY_WRITE, dst, len)) {
99     		*err = -EFAULT;
100     		return sum;
101     	} else {
102     		register unsigned int ret asm("o0") = (unsigned int)src;
103     		register char *d asm("o1") = dst;
104     		register int l asm("g1") = len;
105     		register unsigned int s asm("g7") = sum;
106     
107     		__asm__ __volatile__ ("
108     		.section __ex_table,#alloc
109     		.align 4
110     		.word 1f,1
111     		.previous
112     1:
113     		call " C_LABEL_STR(__csum_partial_copy_sparc_generic) "
114     		 st %5, [%%sp + 64]
115     		" : "=r" (ret) : "0" (ret), "r" (d), "r" (l), "r" (s), "r" (err) :
116     		"o1", "o2", "o3", "o4", "o5", "o7", "g1", "g2", "g3", "g4", "g5", "g7");
117     		return ret;
118     	}
119     }
120     
121     #define HAVE_CSUM_COPY_USER
122     #define csum_and_copy_to_user csum_partial_copy_to_user
123     
124     /* ihl is always 5 or greater, almost always is 5, and iph is word aligned
125      * the majority of the time.
126      */
127     extern __inline__ unsigned short ip_fast_csum(__const__ unsigned char *iph,
128     					      unsigned int ihl)
129     {
130     	unsigned short sum;
131     
132     	/* Note: We must read %2 before we touch %0 for the first time,
133     	 *       because GCC can legitimately use the same register for
134     	 *       both operands.
135     	 */
136     	__asm__ __volatile__("sub\t%2, 4, %%g4\n\t"
137     			     "ld\t[%1 + 0x00], %0\n\t"
138     			     "ld\t[%1 + 0x04], %%g2\n\t"
139     			     "ld\t[%1 + 0x08], %%g3\n\t"
140     			     "addcc\t%%g2, %0, %0\n\t"
141     			     "addxcc\t%%g3, %0, %0\n\t"
142     			     "ld\t[%1 + 0x0c], %%g2\n\t"
143     			     "ld\t[%1 + 0x10], %%g3\n\t"
144     			     "addxcc\t%%g2, %0, %0\n\t"
145     			     "addx\t%0, %%g0, %0\n"
146     			     "1:\taddcc\t%%g3, %0, %0\n\t"
147     			     "add\t%1, 4, %1\n\t"
148     			     "addxcc\t%0, %%g0, %0\n\t"
149     			     "subcc\t%%g4, 1, %%g4\n\t"
150     			     "be,a\t2f\n\t"
151     			     "sll\t%0, 16, %%g2\n\t"
152     			     "b\t1b\n\t"
153     			     "ld\t[%1 + 0x10], %%g3\n"
154     			     "2:\taddcc\t%0, %%g2, %%g2\n\t"
155     			     "srl\t%%g2, 16, %0\n\t"
156     			     "addx\t%0, %%g0, %0\n\t"
157     			     "xnor\t%%g0, %0, %0"
158     			     : "=r" (sum), "=&r" (iph)
159     			     : "r" (ihl), "1" (iph)
160     			     : "g2", "g3", "g4", "cc");
161     	return sum;
162     }
163     
164     /* Fold a partial checksum without adding pseudo headers. */
165     extern __inline__ unsigned int csum_fold(unsigned int sum)
166     {
167     	unsigned int tmp;
168     
169     	__asm__ __volatile__("addcc\t%0, %1, %1\n\t"
170     			     "srl\t%1, 16, %1\n\t"
171     			     "addx\t%1, %%g0, %1\n\t"
172     			     "xnor\t%%g0, %1, %0"
173     			     : "=&r" (sum), "=r" (tmp)
174     			     : "0" (sum), "1" (sum<<16)
175     			     : "cc");
176     	return sum;
177     }
178     
179     extern __inline__ unsigned long csum_tcpudp_nofold(unsigned long saddr,
180     						   unsigned long daddr,
181     						   unsigned int len,
182     						   unsigned short proto,
183     						   unsigned int sum)
184     {
185     	__asm__ __volatile__("addcc\t%1, %0, %0\n\t"
186     			     "addxcc\t%2, %0, %0\n\t"
187     			     "addxcc\t%3, %0, %0\n\t"
188     			     "addx\t%0, %%g0, %0\n\t"
189     			     : "=r" (sum), "=r" (saddr)
190     			     : "r" (daddr), "r" ((proto<<16)+len), "0" (sum),
191     			       "1" (saddr)
192     			     : "cc");
193     	return sum;
194     }
195     
196     /*
197      * computes the checksum of the TCP/UDP pseudo-header
198      * returns a 16-bit checksum, already complemented
199      */
200     static inline unsigned short int csum_tcpudp_magic(unsigned long saddr,
201     						   unsigned long daddr,
202     						   unsigned short len,
203     						   unsigned short proto,
204     						   unsigned int sum) 
205     {
206     	return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
207     }
208     
209     #define _HAVE_ARCH_IPV6_CSUM
210     
211     static __inline__ unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
212     						     struct in6_addr *daddr,
213     						     __u32 len,
214     						     unsigned short proto,
215     						     unsigned int sum) 
216     {
217     	__asm__ __volatile__ ("
218     		addcc	%3, %4, %%g4
219     		addxcc	%5, %%g4, %%g4
220     		ld	[%2 + 0x0c], %%g2
221     		ld	[%2 + 0x08], %%g3
222     		addxcc	%%g2, %%g4, %%g4
223     		ld	[%2 + 0x04], %%g2
224     		addxcc	%%g3, %%g4, %%g4
225     		ld	[%2 + 0x00], %%g3
226     		addxcc	%%g2, %%g4, %%g4
227     		ld	[%1 + 0x0c], %%g2
228     		addxcc	%%g3, %%g4, %%g4
229     		ld	[%1 + 0x08], %%g3
230     		addxcc	%%g2, %%g4, %%g4
231     		ld	[%1 + 0x04], %%g2
232     		addxcc	%%g3, %%g4, %%g4
233     		ld	[%1 + 0x00], %%g3
234     		addxcc	%%g2, %%g4, %%g4
235     		addxcc	%%g3, %%g4, %0
236     		addx	0, %0, %0
237     		"
238     		: "=&r" (sum)
239     		: "r" (saddr), "r" (daddr), 
240     		  "r"(htonl(len)), "r"(htonl(proto)), "r"(sum)
241     		: "g2", "g3", "g4", "cc");
242     
243     	return csum_fold(sum);
244     }
245     
246     /* this routine is used for miscellaneous IP-like checksums, mainly in icmp.c */
247     extern __inline__ unsigned short ip_compute_csum(unsigned char * buff, int len)
248     {
249     	return csum_fold(csum_partial(buff, len, 0));
250     }
251     
252     #endif /* !(__SPARC_CHECKSUM_H) */
253