checksum_32.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* SPDX-License-Identifier: GPL-2.0 */
  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 <linux/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. __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. 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)
  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 -1, %%g7\n"
  47. : "=&r" (ret), "=&r" (d), "=&r" (l)
  48. : "0" (ret), "1" (d), "2" (l)
  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_and_copy_from_user(const void __user *src, void *dst, int len)
  56. {
  57. if (unlikely(!access_ok(src, len)))
  58. return 0;
  59. return csum_partial_copy_nocheck((__force void *)src, dst, len);
  60. }
  61. static inline __wsum
  62. csum_and_copy_to_user(const void *src, void __user *dst, int len)
  63. {
  64. if (!access_ok(dst, len))
  65. return 0;
  66. return csum_partial_copy_nocheck(src, (__force void *)dst, len);
  67. }
  68. /* ihl is always 5 or greater, almost always is 5, and iph is word aligned
  69. * the majority of the time.
  70. */
  71. static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  72. {
  73. __sum16 sum;
  74. /* Note: We must read %2 before we touch %0 for the first time,
  75. * because GCC can legitimately use the same register for
  76. * both operands.
  77. */
  78. __asm__ __volatile__("sub\t%2, 4, %%g4\n\t"
  79. "ld\t[%1 + 0x00], %0\n\t"
  80. "ld\t[%1 + 0x04], %%g2\n\t"
  81. "ld\t[%1 + 0x08], %%g3\n\t"
  82. "addcc\t%%g2, %0, %0\n\t"
  83. "addxcc\t%%g3, %0, %0\n\t"
  84. "ld\t[%1 + 0x0c], %%g2\n\t"
  85. "ld\t[%1 + 0x10], %%g3\n\t"
  86. "addxcc\t%%g2, %0, %0\n\t"
  87. "addx\t%0, %%g0, %0\n"
  88. "1:\taddcc\t%%g3, %0, %0\n\t"
  89. "add\t%1, 4, %1\n\t"
  90. "addxcc\t%0, %%g0, %0\n\t"
  91. "subcc\t%%g4, 1, %%g4\n\t"
  92. "be,a\t2f\n\t"
  93. "sll\t%0, 16, %%g2\n\t"
  94. "b\t1b\n\t"
  95. "ld\t[%1 + 0x10], %%g3\n"
  96. "2:\taddcc\t%0, %%g2, %%g2\n\t"
  97. "srl\t%%g2, 16, %0\n\t"
  98. "addx\t%0, %%g0, %0\n\t"
  99. "xnor\t%%g0, %0, %0"
  100. : "=r" (sum), "=&r" (iph)
  101. : "r" (ihl), "1" (iph)
  102. : "g2", "g3", "g4", "cc", "memory");
  103. return sum;
  104. }
  105. /* Fold a partial checksum without adding pseudo headers. */
  106. static inline __sum16 csum_fold(__wsum sum)
  107. {
  108. unsigned int tmp;
  109. __asm__ __volatile__("addcc\t%0, %1, %1\n\t"
  110. "srl\t%1, 16, %1\n\t"
  111. "addx\t%1, %%g0, %1\n\t"
  112. "xnor\t%%g0, %1, %0"
  113. : "=&r" (sum), "=r" (tmp)
  114. : "0" (sum), "1" ((__force u32)sum<<16)
  115. : "cc");
  116. return (__force __sum16)sum;
  117. }
  118. static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
  119. __u32 len, __u8 proto,
  120. __wsum sum)
  121. {
  122. __asm__ __volatile__("addcc\t%1, %0, %0\n\t"
  123. "addxcc\t%2, %0, %0\n\t"
  124. "addxcc\t%3, %0, %0\n\t"
  125. "addx\t%0, %%g0, %0\n\t"
  126. : "=r" (sum), "=r" (saddr)
  127. : "r" (daddr), "r" (proto + len), "0" (sum),
  128. "1" (saddr)
  129. : "cc");
  130. return sum;
  131. }
  132. /*
  133. * computes the checksum of the TCP/UDP pseudo-header
  134. * returns a 16-bit checksum, already complemented
  135. */
  136. static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
  137. __u32 len, __u8 proto,
  138. __wsum sum)
  139. {
  140. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  141. }
  142. #define _HAVE_ARCH_IPV6_CSUM
  143. static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
  144. const struct in6_addr *daddr,
  145. __u32 len, __u8 proto, __wsum sum)
  146. {
  147. __asm__ __volatile__ (
  148. "addcc %3, %4, %%g4\n\t"
  149. "addxcc %5, %%g4, %%g4\n\t"
  150. "ld [%2 + 0x0c], %%g2\n\t"
  151. "ld [%2 + 0x08], %%g3\n\t"
  152. "addxcc %%g2, %%g4, %%g4\n\t"
  153. "ld [%2 + 0x04], %%g2\n\t"
  154. "addxcc %%g3, %%g4, %%g4\n\t"
  155. "ld [%2 + 0x00], %%g3\n\t"
  156. "addxcc %%g2, %%g4, %%g4\n\t"
  157. "ld [%1 + 0x0c], %%g2\n\t"
  158. "addxcc %%g3, %%g4, %%g4\n\t"
  159. "ld [%1 + 0x08], %%g3\n\t"
  160. "addxcc %%g2, %%g4, %%g4\n\t"
  161. "ld [%1 + 0x04], %%g2\n\t"
  162. "addxcc %%g3, %%g4, %%g4\n\t"
  163. "ld [%1 + 0x00], %%g3\n\t"
  164. "addxcc %%g2, %%g4, %%g4\n\t"
  165. "addxcc %%g3, %%g4, %0\n\t"
  166. "addx 0, %0, %0\n"
  167. : "=&r" (sum)
  168. : "r" (saddr), "r" (daddr),
  169. "r"(htonl(len)), "r"(htonl(proto)), "r"(sum)
  170. : "g2", "g3", "g4", "cc");
  171. return csum_fold(sum);
  172. }
  173. /* this routine is used for miscellaneous IP-like checksums, mainly in icmp.c */
  174. static inline __sum16 ip_compute_csum(const void *buff, int len)
  175. {
  176. return csum_fold(csum_partial(buff, len, 0));
  177. }
  178. #define HAVE_ARCH_CSUM_ADD
  179. static inline __wsum csum_add(__wsum csum, __wsum addend)
  180. {
  181. __asm__ __volatile__(
  182. "addcc %0, %1, %0\n"
  183. "addx %0, %%g0, %0"
  184. : "=r" (csum)
  185. : "r" (addend), "0" (csum));
  186. return csum;
  187. }
  188. #endif /* !(__SPARC_CHECKSUM_H) */