checksum.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 __wsum csum_partial(const void *buff, int len, __wsum 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 __wsum
  39. csum_partial_copy_nocheck(const void *src, void *dst, int len, __wsum sum)
  40. {
  41. register unsigned int ret asm("o0") = (unsigned int)src;
  42. register char *d asm("o1") = dst;
  43. register int l asm("g1") = len;
  44. __asm__ __volatile__ (
  45. "call __csum_partial_copy_sparc_generic\n\t"
  46. " mov %6, %%g7\n"
  47. : "=&r" (ret), "=&r" (d), "=&r" (l)
  48. : "0" (ret), "1" (d), "2" (l), "r" (sum)
  49. : "o2", "o3", "o4", "o5", "o7",
  50. "g2", "g3", "g4", "g5", "g7",
  51. "memory", "cc");
  52. return (__force __wsum)ret;
  53. }
  54. static inline __wsum
  55. csum_partial_copy_from_user(const void __user *src, void *dst, int len,
  56. __wsum sum, int *err)
  57. {
  58. register unsigned long ret asm("o0") = (unsigned long)src;
  59. register char *d asm("o1") = dst;
  60. register int l asm("g1") = len;
  61. register __wsum s asm("g7") = sum;
  62. __asm__ __volatile__ (
  63. ".section __ex_table,#alloc\n\t"
  64. ".align 4\n\t"
  65. ".word 1f,2\n\t"
  66. ".previous\n"
  67. "1:\n\t"
  68. "call __csum_partial_copy_sparc_generic\n\t"
  69. " st %8, [%%sp + 64]\n"
  70. : "=&r" (ret), "=&r" (d), "=&r" (l), "=&r" (s)
  71. : "0" (ret), "1" (d), "2" (l), "3" (s), "r" (err)
  72. : "o2", "o3", "o4", "o5", "o7", "g2", "g3", "g4", "g5",
  73. "cc", "memory");
  74. return (__force __wsum)ret;
  75. }
  76. static inline __wsum
  77. csum_partial_copy_to_user(const void *src, void __user *dst, int len,
  78. __wsum sum, int *err)
  79. {
  80. if (!access_ok (VERIFY_WRITE, dst, len)) {
  81. *err = -EFAULT;
  82. return sum;
  83. } else {
  84. register unsigned long ret asm("o0") = (unsigned long)src;
  85. register char __user *d asm("o1") = dst;
  86. register int l asm("g1") = len;
  87. register __wsum s asm("g7") = sum;
  88. __asm__ __volatile__ (
  89. ".section __ex_table,#alloc\n\t"
  90. ".align 4\n\t"
  91. ".word 1f,1\n\t"
  92. ".previous\n"
  93. "1:\n\t"
  94. "call __csum_partial_copy_sparc_generic\n\t"
  95. " st %8, [%%sp + 64]\n"
  96. : "=&r" (ret), "=&r" (d), "=&r" (l), "=&r" (s)
  97. : "0" (ret), "1" (d), "2" (l), "3" (s), "r" (err)
  98. : "o2", "o3", "o4", "o5", "o7",
  99. "g2", "g3", "g4", "g5",
  100. "cc", "memory");
  101. return (__force __wsum)ret;
  102. }
  103. }
  104. #define HAVE_CSUM_COPY_USER
  105. #define csum_and_copy_to_user csum_partial_copy_to_user
  106. /* ihl is always 5 or greater, almost always is 5, and iph is word aligned
  107. * the majority of the time.
  108. */
  109. static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  110. {
  111. __sum16 sum;
  112. /* Note: We must read %2 before we touch %0 for the first time,
  113. * because GCC can legitimately use the same register for
  114. * both operands.
  115. */
  116. __asm__ __volatile__("sub\t%2, 4, %%g4\n\t"
  117. "ld\t[%1 + 0x00], %0\n\t"
  118. "ld\t[%1 + 0x04], %%g2\n\t"
  119. "ld\t[%1 + 0x08], %%g3\n\t"
  120. "addcc\t%%g2, %0, %0\n\t"
  121. "addxcc\t%%g3, %0, %0\n\t"
  122. "ld\t[%1 + 0x0c], %%g2\n\t"
  123. "ld\t[%1 + 0x10], %%g3\n\t"
  124. "addxcc\t%%g2, %0, %0\n\t"
  125. "addx\t%0, %%g0, %0\n"
  126. "1:\taddcc\t%%g3, %0, %0\n\t"
  127. "add\t%1, 4, %1\n\t"
  128. "addxcc\t%0, %%g0, %0\n\t"
  129. "subcc\t%%g4, 1, %%g4\n\t"
  130. "be,a\t2f\n\t"
  131. "sll\t%0, 16, %%g2\n\t"
  132. "b\t1b\n\t"
  133. "ld\t[%1 + 0x10], %%g3\n"
  134. "2:\taddcc\t%0, %%g2, %%g2\n\t"
  135. "srl\t%%g2, 16, %0\n\t"
  136. "addx\t%0, %%g0, %0\n\t"
  137. "xnor\t%%g0, %0, %0"
  138. : "=r" (sum), "=&r" (iph)
  139. : "r" (ihl), "1" (iph)
  140. : "g2", "g3", "g4", "cc", "memory");
  141. return sum;
  142. }
  143. /* Fold a partial checksum without adding pseudo headers. */
  144. static inline __sum16 csum_fold(__wsum sum)
  145. {
  146. unsigned int tmp;
  147. __asm__ __volatile__("addcc\t%0, %1, %1\n\t"
  148. "srl\t%1, 16, %1\n\t"
  149. "addx\t%1, %%g0, %1\n\t"
  150. "xnor\t%%g0, %1, %0"
  151. : "=&r" (sum), "=r" (tmp)
  152. : "0" (sum), "1" ((__force u32)sum<<16)
  153. : "cc");
  154. return (__force __sum16)sum;
  155. }
  156. static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
  157. unsigned short len,
  158. unsigned short proto,
  159. __wsum sum)
  160. {
  161. __asm__ __volatile__("addcc\t%1, %0, %0\n\t"
  162. "addxcc\t%2, %0, %0\n\t"
  163. "addxcc\t%3, %0, %0\n\t"
  164. "addx\t%0, %%g0, %0\n\t"
  165. : "=r" (sum), "=r" (saddr)
  166. : "r" (daddr), "r" (proto + len), "0" (sum),
  167. "1" (saddr)
  168. : "cc");
  169. return sum;
  170. }
  171. /*
  172. * computes the checksum of the TCP/UDP pseudo-header
  173. * returns a 16-bit checksum, already complemented
  174. */
  175. static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
  176. unsigned short len,
  177. unsigned short proto,
  178. __wsum sum)
  179. {
  180. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  181. }
  182. #define _HAVE_ARCH_IPV6_CSUM
  183. static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
  184. const struct in6_addr *daddr,
  185. __u32 len, unsigned short proto,
  186. __wsum sum)
  187. {
  188. __asm__ __volatile__ (
  189. "addcc %3, %4, %%g4\n\t"
  190. "addxcc %5, %%g4, %%g4\n\t"
  191. "ld [%2 + 0x0c], %%g2\n\t"
  192. "ld [%2 + 0x08], %%g3\n\t"
  193. "addxcc %%g2, %%g4, %%g4\n\t"
  194. "ld [%2 + 0x04], %%g2\n\t"
  195. "addxcc %%g3, %%g4, %%g4\n\t"
  196. "ld [%2 + 0x00], %%g3\n\t"
  197. "addxcc %%g2, %%g4, %%g4\n\t"
  198. "ld [%1 + 0x0c], %%g2\n\t"
  199. "addxcc %%g3, %%g4, %%g4\n\t"
  200. "ld [%1 + 0x08], %%g3\n\t"
  201. "addxcc %%g2, %%g4, %%g4\n\t"
  202. "ld [%1 + 0x04], %%g2\n\t"
  203. "addxcc %%g3, %%g4, %%g4\n\t"
  204. "ld [%1 + 0x00], %%g3\n\t"
  205. "addxcc %%g2, %%g4, %%g4\n\t"
  206. "addxcc %%g3, %%g4, %0\n\t"
  207. "addx 0, %0, %0\n"
  208. : "=&r" (sum)
  209. : "r" (saddr), "r" (daddr),
  210. "r"(htonl(len)), "r"(htonl(proto)), "r"(sum)
  211. : "g2", "g3", "g4", "cc");
  212. return csum_fold(sum);
  213. }
  214. /* this routine is used for miscellaneous IP-like checksums, mainly in icmp.c */
  215. static inline __sum16 ip_compute_csum(const void *buff, int len)
  216. {
  217. return csum_fold(csum_partial(buff, len, 0));
  218. }
  219. #endif /* !(__SPARC_CHECKSUM_H) */