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