csum_partial_copy.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * csum_partial_copy - do IP checksumming and copy
  4. *
  5. * (C) Copyright 1996 Linus Torvalds
  6. * accelerated versions (and 21264 assembly versions ) contributed by
  7. * Rick Gorton <[email protected]>
  8. *
  9. * Don't look at this too closely - you'll go mad. The things
  10. * we do for performance..
  11. */
  12. #include <linux/types.h>
  13. #include <linux/string.h>
  14. #include <linux/uaccess.h>
  15. #include <net/checksum.h>
  16. #define ldq_u(x,y) \
  17. __asm__ __volatile__("ldq_u %0,%1":"=r" (x):"m" (*(const unsigned long *)(y)))
  18. #define stq_u(x,y) \
  19. __asm__ __volatile__("stq_u %1,%0":"=m" (*(unsigned long *)(y)):"r" (x))
  20. #define extql(x,y,z) \
  21. __asm__ __volatile__("extql %1,%2,%0":"=r" (z):"r" (x),"r" (y))
  22. #define extqh(x,y,z) \
  23. __asm__ __volatile__("extqh %1,%2,%0":"=r" (z):"r" (x),"r" (y))
  24. #define mskql(x,y,z) \
  25. __asm__ __volatile__("mskql %1,%2,%0":"=r" (z):"r" (x),"r" (y))
  26. #define mskqh(x,y,z) \
  27. __asm__ __volatile__("mskqh %1,%2,%0":"=r" (z):"r" (x),"r" (y))
  28. #define insql(x,y,z) \
  29. __asm__ __volatile__("insql %1,%2,%0":"=r" (z):"r" (x),"r" (y))
  30. #define insqh(x,y,z) \
  31. __asm__ __volatile__("insqh %1,%2,%0":"=r" (z):"r" (x),"r" (y))
  32. #define __get_word(insn,x,ptr) \
  33. ({ \
  34. long __guu_err; \
  35. __asm__ __volatile__( \
  36. "1: "#insn" %0,%2\n" \
  37. "2:\n" \
  38. EXC(1b,2b,%0,%1) \
  39. : "=r"(x), "=r"(__guu_err) \
  40. : "m"(__m(ptr)), "1"(0)); \
  41. __guu_err; \
  42. })
  43. static inline unsigned short from64to16(unsigned long x)
  44. {
  45. /* Using extract instructions is a bit more efficient
  46. than the original shift/bitmask version. */
  47. union {
  48. unsigned long ul;
  49. unsigned int ui[2];
  50. unsigned short us[4];
  51. } in_v, tmp_v, out_v;
  52. in_v.ul = x;
  53. tmp_v.ul = (unsigned long) in_v.ui[0] + (unsigned long) in_v.ui[1];
  54. /* Since the bits of tmp_v.sh[3] are going to always be zero,
  55. we don't have to bother to add that in. */
  56. out_v.ul = (unsigned long) tmp_v.us[0] + (unsigned long) tmp_v.us[1]
  57. + (unsigned long) tmp_v.us[2];
  58. /* Similarly, out_v.us[2] is always zero for the final add. */
  59. return out_v.us[0] + out_v.us[1];
  60. }
  61. /*
  62. * Ok. This isn't fun, but this is the EASY case.
  63. */
  64. static inline unsigned long
  65. csum_partial_cfu_aligned(const unsigned long __user *src, unsigned long *dst,
  66. long len)
  67. {
  68. unsigned long checksum = ~0U;
  69. unsigned long carry = 0;
  70. while (len >= 0) {
  71. unsigned long word;
  72. if (__get_word(ldq, word, src))
  73. return 0;
  74. checksum += carry;
  75. src++;
  76. checksum += word;
  77. len -= 8;
  78. carry = checksum < word;
  79. *dst = word;
  80. dst++;
  81. }
  82. len += 8;
  83. checksum += carry;
  84. if (len) {
  85. unsigned long word, tmp;
  86. if (__get_word(ldq, word, src))
  87. return 0;
  88. tmp = *dst;
  89. mskql(word, len, word);
  90. checksum += word;
  91. mskqh(tmp, len, tmp);
  92. carry = checksum < word;
  93. *dst = word | tmp;
  94. checksum += carry;
  95. }
  96. return checksum;
  97. }
  98. /*
  99. * This is even less fun, but this is still reasonably
  100. * easy.
  101. */
  102. static inline unsigned long
  103. csum_partial_cfu_dest_aligned(const unsigned long __user *src,
  104. unsigned long *dst,
  105. unsigned long soff,
  106. long len)
  107. {
  108. unsigned long first;
  109. unsigned long word, carry;
  110. unsigned long lastsrc = 7+len+(unsigned long)src;
  111. unsigned long checksum = ~0U;
  112. if (__get_word(ldq_u, first,src))
  113. return 0;
  114. carry = 0;
  115. while (len >= 0) {
  116. unsigned long second;
  117. if (__get_word(ldq_u, second, src+1))
  118. return 0;
  119. extql(first, soff, word);
  120. len -= 8;
  121. src++;
  122. extqh(second, soff, first);
  123. checksum += carry;
  124. word |= first;
  125. first = second;
  126. checksum += word;
  127. *dst = word;
  128. dst++;
  129. carry = checksum < word;
  130. }
  131. len += 8;
  132. checksum += carry;
  133. if (len) {
  134. unsigned long tmp;
  135. unsigned long second;
  136. if (__get_word(ldq_u, second, lastsrc))
  137. return 0;
  138. tmp = *dst;
  139. extql(first, soff, word);
  140. extqh(second, soff, first);
  141. word |= first;
  142. mskql(word, len, word);
  143. checksum += word;
  144. mskqh(tmp, len, tmp);
  145. carry = checksum < word;
  146. *dst = word | tmp;
  147. checksum += carry;
  148. }
  149. return checksum;
  150. }
  151. /*
  152. * This is slightly less fun than the above..
  153. */
  154. static inline unsigned long
  155. csum_partial_cfu_src_aligned(const unsigned long __user *src,
  156. unsigned long *dst,
  157. unsigned long doff,
  158. long len,
  159. unsigned long partial_dest)
  160. {
  161. unsigned long carry = 0;
  162. unsigned long word;
  163. unsigned long second_dest;
  164. unsigned long checksum = ~0U;
  165. mskql(partial_dest, doff, partial_dest);
  166. while (len >= 0) {
  167. if (__get_word(ldq, word, src))
  168. return 0;
  169. len -= 8;
  170. insql(word, doff, second_dest);
  171. checksum += carry;
  172. stq_u(partial_dest | second_dest, dst);
  173. src++;
  174. checksum += word;
  175. insqh(word, doff, partial_dest);
  176. carry = checksum < word;
  177. dst++;
  178. }
  179. len += 8;
  180. if (len) {
  181. checksum += carry;
  182. if (__get_word(ldq, word, src))
  183. return 0;
  184. mskql(word, len, word);
  185. len -= 8;
  186. checksum += word;
  187. insql(word, doff, second_dest);
  188. len += doff;
  189. carry = checksum < word;
  190. partial_dest |= second_dest;
  191. if (len >= 0) {
  192. stq_u(partial_dest, dst);
  193. if (!len) goto out;
  194. dst++;
  195. insqh(word, doff, partial_dest);
  196. }
  197. doff = len;
  198. }
  199. ldq_u(second_dest, dst);
  200. mskqh(second_dest, doff, second_dest);
  201. stq_u(partial_dest | second_dest, dst);
  202. out:
  203. checksum += carry;
  204. return checksum;
  205. }
  206. /*
  207. * This is so totally un-fun that it's frightening. Don't
  208. * look at this too closely, you'll go blind.
  209. */
  210. static inline unsigned long
  211. csum_partial_cfu_unaligned(const unsigned long __user * src,
  212. unsigned long * dst,
  213. unsigned long soff, unsigned long doff,
  214. long len, unsigned long partial_dest)
  215. {
  216. unsigned long carry = 0;
  217. unsigned long first;
  218. unsigned long lastsrc;
  219. unsigned long checksum = ~0U;
  220. if (__get_word(ldq_u, first, src))
  221. return 0;
  222. lastsrc = 7+len+(unsigned long)src;
  223. mskql(partial_dest, doff, partial_dest);
  224. while (len >= 0) {
  225. unsigned long second, word;
  226. unsigned long second_dest;
  227. if (__get_word(ldq_u, second, src+1))
  228. return 0;
  229. extql(first, soff, word);
  230. checksum += carry;
  231. len -= 8;
  232. extqh(second, soff, first);
  233. src++;
  234. word |= first;
  235. first = second;
  236. insql(word, doff, second_dest);
  237. checksum += word;
  238. stq_u(partial_dest | second_dest, dst);
  239. carry = checksum < word;
  240. insqh(word, doff, partial_dest);
  241. dst++;
  242. }
  243. len += doff;
  244. checksum += carry;
  245. if (len >= 0) {
  246. unsigned long second, word;
  247. unsigned long second_dest;
  248. if (__get_word(ldq_u, second, lastsrc))
  249. return 0;
  250. extql(first, soff, word);
  251. extqh(second, soff, first);
  252. word |= first;
  253. first = second;
  254. mskql(word, len-doff, word);
  255. checksum += word;
  256. insql(word, doff, second_dest);
  257. carry = checksum < word;
  258. stq_u(partial_dest | second_dest, dst);
  259. if (len) {
  260. ldq_u(second_dest, dst+1);
  261. insqh(word, doff, partial_dest);
  262. mskqh(second_dest, len, second_dest);
  263. stq_u(partial_dest | second_dest, dst+1);
  264. }
  265. checksum += carry;
  266. } else {
  267. unsigned long second, word;
  268. unsigned long second_dest;
  269. if (__get_word(ldq_u, second, lastsrc))
  270. return 0;
  271. extql(first, soff, word);
  272. extqh(second, soff, first);
  273. word |= first;
  274. ldq_u(second_dest, dst);
  275. mskql(word, len-doff, word);
  276. checksum += word;
  277. mskqh(second_dest, len, second_dest);
  278. carry = checksum < word;
  279. insql(word, doff, word);
  280. stq_u(partial_dest | word | second_dest, dst);
  281. checksum += carry;
  282. }
  283. return checksum;
  284. }
  285. static __wsum __csum_and_copy(const void __user *src, void *dst, int len)
  286. {
  287. unsigned long soff = 7 & (unsigned long) src;
  288. unsigned long doff = 7 & (unsigned long) dst;
  289. unsigned long checksum;
  290. if (!doff) {
  291. if (!soff)
  292. checksum = csum_partial_cfu_aligned(
  293. (const unsigned long __user *) src,
  294. (unsigned long *) dst, len-8);
  295. else
  296. checksum = csum_partial_cfu_dest_aligned(
  297. (const unsigned long __user *) src,
  298. (unsigned long *) dst,
  299. soff, len-8);
  300. } else {
  301. unsigned long partial_dest;
  302. ldq_u(partial_dest, dst);
  303. if (!soff)
  304. checksum = csum_partial_cfu_src_aligned(
  305. (const unsigned long __user *) src,
  306. (unsigned long *) dst,
  307. doff, len-8, partial_dest);
  308. else
  309. checksum = csum_partial_cfu_unaligned(
  310. (const unsigned long __user *) src,
  311. (unsigned long *) dst,
  312. soff, doff, len-8, partial_dest);
  313. }
  314. return (__force __wsum)from64to16 (checksum);
  315. }
  316. __wsum
  317. csum_and_copy_from_user(const void __user *src, void *dst, int len)
  318. {
  319. if (!access_ok(src, len))
  320. return 0;
  321. return __csum_and_copy(src, dst, len);
  322. }
  323. __wsum
  324. csum_partial_copy_nocheck(const void *src, void *dst, int len)
  325. {
  326. return __csum_and_copy((__force const void __user *)src,
  327. dst, len);
  328. }
  329. EXPORT_SYMBOL(csum_partial_copy_nocheck);