checksum.h 7.0 KB

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