checksum.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * INET An implementation of the TCP/IP protocol suite for the LINUX
  4. * operating system. INET is implemented using the BSD Socket
  5. * interface as the means of communication with the user level.
  6. *
  7. * IP/TCP/UDP checksumming routines
  8. *
  9. * Authors: Jorge Cwik, <[email protected]>
  10. * Arnt Gulbrandsen, <[email protected]>
  11. * Tom May, <[email protected]>
  12. * Andreas Schwab, <[email protected]>
  13. * Lots of code moved from tcp.c and ip.c; see those files
  14. * for more names.
  15. *
  16. * 03/02/96 Jes Sorensen, Andreas Schwab, Roman Hodek:
  17. * Fixed some nasty bugs, causing some horrible crashes.
  18. * A: At some points, the sum (%0) was used as
  19. * length-counter instead of the length counter
  20. * (%1). Thanks to Roman Hodek for pointing this out.
  21. * B: GCC seems to mess up if one uses too many
  22. * data-registers to hold input values and one tries to
  23. * specify d0 and d1 as scratch registers. Letting gcc
  24. * choose these registers itself solves the problem.
  25. *
  26. * 1998/8/31 Andreas Schwab:
  27. * Zero out rest of buffer on exception in
  28. * csum_partial_copy_from_user.
  29. */
  30. #include <linux/module.h>
  31. #include <net/checksum.h>
  32. /*
  33. * computes a partial checksum, e.g. for TCP/UDP fragments
  34. */
  35. __wsum csum_partial(const void *buff, int len, __wsum sum)
  36. {
  37. unsigned long tmp1, tmp2;
  38. /*
  39. * Experiments with ethernet and slip connections show that buff
  40. * is aligned on either a 2-byte or 4-byte boundary.
  41. */
  42. __asm__("movel %2,%3\n\t"
  43. "btst #1,%3\n\t" /* Check alignment */
  44. "jeq 2f\n\t"
  45. "subql #2,%1\n\t" /* buff%4==2: treat first word */
  46. "jgt 1f\n\t"
  47. "addql #2,%1\n\t" /* len was == 2, treat only rest */
  48. "jra 4f\n"
  49. "1:\t"
  50. "addw %2@+,%0\n\t" /* add first word to sum */
  51. "clrl %3\n\t"
  52. "addxl %3,%0\n" /* add X bit */
  53. "2:\t"
  54. /* unrolled loop for the main part: do 8 longs at once */
  55. "movel %1,%3\n\t" /* save len in tmp1 */
  56. "lsrl #5,%1\n\t" /* len/32 */
  57. "jeq 2f\n\t" /* not enough... */
  58. "subql #1,%1\n"
  59. "1:\t"
  60. "movel %2@+,%4\n\t"
  61. "addxl %4,%0\n\t"
  62. "movel %2@+,%4\n\t"
  63. "addxl %4,%0\n\t"
  64. "movel %2@+,%4\n\t"
  65. "addxl %4,%0\n\t"
  66. "movel %2@+,%4\n\t"
  67. "addxl %4,%0\n\t"
  68. "movel %2@+,%4\n\t"
  69. "addxl %4,%0\n\t"
  70. "movel %2@+,%4\n\t"
  71. "addxl %4,%0\n\t"
  72. "movel %2@+,%4\n\t"
  73. "addxl %4,%0\n\t"
  74. "movel %2@+,%4\n\t"
  75. "addxl %4,%0\n\t"
  76. "dbra %1,1b\n\t"
  77. "clrl %4\n\t"
  78. "addxl %4,%0\n\t" /* add X bit */
  79. "clrw %1\n\t"
  80. "subql #1,%1\n\t"
  81. "jcc 1b\n"
  82. "2:\t"
  83. "movel %3,%1\n\t" /* restore len from tmp1 */
  84. "andw #0x1c,%3\n\t" /* number of rest longs */
  85. "jeq 4f\n\t"
  86. "lsrw #2,%3\n\t"
  87. "subqw #1,%3\n"
  88. "3:\t"
  89. /* loop for rest longs */
  90. "movel %2@+,%4\n\t"
  91. "addxl %4,%0\n\t"
  92. "dbra %3,3b\n\t"
  93. "clrl %4\n\t"
  94. "addxl %4,%0\n" /* add X bit */
  95. "4:\t"
  96. /* now check for rest bytes that do not fit into longs */
  97. "andw #3,%1\n\t"
  98. "jeq 7f\n\t"
  99. "clrl %4\n\t" /* clear tmp2 for rest bytes */
  100. "subqw #2,%1\n\t"
  101. "jlt 5f\n\t"
  102. "movew %2@+,%4\n\t" /* have rest >= 2: get word */
  103. "swap %4\n\t" /* into bits 16..31 */
  104. "tstw %1\n\t" /* another byte? */
  105. "jeq 6f\n"
  106. "5:\t"
  107. "moveb %2@,%4\n\t" /* have odd rest: get byte */
  108. "lslw #8,%4\n\t" /* into bits 8..15; 16..31 untouched */
  109. "6:\t"
  110. "addl %4,%0\n\t" /* now add rest long to sum */
  111. "clrl %4\n\t"
  112. "addxl %4,%0\n" /* add X bit */
  113. "7:\t"
  114. : "=d" (sum), "=d" (len), "=a" (buff),
  115. "=&d" (tmp1), "=&d" (tmp2)
  116. : "0" (sum), "1" (len), "2" (buff)
  117. );
  118. return(sum);
  119. }
  120. EXPORT_SYMBOL(csum_partial);
  121. /*
  122. * copy from user space while checksumming, with exception handling.
  123. */
  124. __wsum
  125. csum_and_copy_from_user(const void __user *src, void *dst, int len)
  126. {
  127. /*
  128. * GCC doesn't like more than 10 operands for the asm
  129. * statements so we have to use tmp2 for the error
  130. * code.
  131. */
  132. unsigned long tmp1, tmp2;
  133. __wsum sum = ~0U;
  134. __asm__("movel %2,%4\n\t"
  135. "btst #1,%4\n\t" /* Check alignment */
  136. "jeq 2f\n\t"
  137. "subql #2,%1\n\t" /* buff%4==2: treat first word */
  138. "jgt 1f\n\t"
  139. "addql #2,%1\n\t" /* len was == 2, treat only rest */
  140. "jra 4f\n"
  141. "1:\n"
  142. "10:\t"
  143. "movesw %2@+,%4\n\t" /* add first word to sum */
  144. "addw %4,%0\n\t"
  145. "movew %4,%3@+\n\t"
  146. "clrl %4\n\t"
  147. "addxl %4,%0\n" /* add X bit */
  148. "2:\t"
  149. /* unrolled loop for the main part: do 8 longs at once */
  150. "movel %1,%4\n\t" /* save len in tmp1 */
  151. "lsrl #5,%1\n\t" /* len/32 */
  152. "jeq 2f\n\t" /* not enough... */
  153. "subql #1,%1\n"
  154. "1:\n"
  155. "11:\t"
  156. "movesl %2@+,%5\n\t"
  157. "addxl %5,%0\n\t"
  158. "movel %5,%3@+\n\t"
  159. "12:\t"
  160. "movesl %2@+,%5\n\t"
  161. "addxl %5,%0\n\t"
  162. "movel %5,%3@+\n\t"
  163. "13:\t"
  164. "movesl %2@+,%5\n\t"
  165. "addxl %5,%0\n\t"
  166. "movel %5,%3@+\n\t"
  167. "14:\t"
  168. "movesl %2@+,%5\n\t"
  169. "addxl %5,%0\n\t"
  170. "movel %5,%3@+\n\t"
  171. "15:\t"
  172. "movesl %2@+,%5\n\t"
  173. "addxl %5,%0\n\t"
  174. "movel %5,%3@+\n\t"
  175. "16:\t"
  176. "movesl %2@+,%5\n\t"
  177. "addxl %5,%0\n\t"
  178. "movel %5,%3@+\n\t"
  179. "17:\t"
  180. "movesl %2@+,%5\n\t"
  181. "addxl %5,%0\n\t"
  182. "movel %5,%3@+\n\t"
  183. "18:\t"
  184. "movesl %2@+,%5\n\t"
  185. "addxl %5,%0\n\t"
  186. "movel %5,%3@+\n\t"
  187. "dbra %1,1b\n\t"
  188. "clrl %5\n\t"
  189. "addxl %5,%0\n\t" /* add X bit */
  190. "clrw %1\n\t"
  191. "subql #1,%1\n\t"
  192. "jcc 1b\n"
  193. "2:\t"
  194. "movel %4,%1\n\t" /* restore len from tmp1 */
  195. "andw #0x1c,%4\n\t" /* number of rest longs */
  196. "jeq 4f\n\t"
  197. "lsrw #2,%4\n\t"
  198. "subqw #1,%4\n"
  199. "3:\n"
  200. /* loop for rest longs */
  201. "19:\t"
  202. "movesl %2@+,%5\n\t"
  203. "addxl %5,%0\n\t"
  204. "movel %5,%3@+\n\t"
  205. "dbra %4,3b\n\t"
  206. "clrl %5\n\t"
  207. "addxl %5,%0\n" /* add X bit */
  208. "4:\t"
  209. /* now check for rest bytes that do not fit into longs */
  210. "andw #3,%1\n\t"
  211. "jeq 7f\n\t"
  212. "clrl %5\n\t" /* clear tmp2 for rest bytes */
  213. "subqw #2,%1\n\t"
  214. "jlt 5f\n\t"
  215. "20:\t"
  216. "movesw %2@+,%5\n\t" /* have rest >= 2: get word */
  217. "movew %5,%3@+\n\t"
  218. "swap %5\n\t" /* into bits 16..31 */
  219. "tstw %1\n\t" /* another byte? */
  220. "jeq 6f\n"
  221. "5:\n"
  222. "21:\t"
  223. "movesb %2@,%5\n\t" /* have odd rest: get byte */
  224. "moveb %5,%3@+\n\t"
  225. "lslw #8,%5\n\t" /* into bits 8..15; 16..31 untouched */
  226. "6:\t"
  227. "addl %5,%0\n\t" /* now add rest long to sum */
  228. "clrl %5\n\t"
  229. "addxl %5,%0\n\t" /* add X bit */
  230. "7:\t"
  231. ".section .fixup,\"ax\"\n"
  232. ".even\n"
  233. /* If any exception occurs, return 0 */
  234. "90:\t"
  235. "clrl %0\n"
  236. "jra 7b\n"
  237. ".previous\n"
  238. ".section __ex_table,\"a\"\n"
  239. ".long 10b,90b\n"
  240. ".long 11b,90b\n"
  241. ".long 12b,90b\n"
  242. ".long 13b,90b\n"
  243. ".long 14b,90b\n"
  244. ".long 15b,90b\n"
  245. ".long 16b,90b\n"
  246. ".long 17b,90b\n"
  247. ".long 18b,90b\n"
  248. ".long 19b,90b\n"
  249. ".long 20b,90b\n"
  250. ".long 21b,90b\n"
  251. ".previous"
  252. : "=d" (sum), "=d" (len), "=a" (src), "=a" (dst),
  253. "=&d" (tmp1), "=d" (tmp2)
  254. : "0" (sum), "1" (len), "2" (src), "3" (dst)
  255. );
  256. return sum;
  257. }
  258. /*
  259. * copy from kernel space while checksumming, otherwise like csum_partial
  260. */
  261. __wsum
  262. csum_partial_copy_nocheck(const void *src, void *dst, int len)
  263. {
  264. unsigned long tmp1, tmp2;
  265. __wsum sum = 0;
  266. __asm__("movel %2,%4\n\t"
  267. "btst #1,%4\n\t" /* Check alignment */
  268. "jeq 2f\n\t"
  269. "subql #2,%1\n\t" /* buff%4==2: treat first word */
  270. "jgt 1f\n\t"
  271. "addql #2,%1\n\t" /* len was == 2, treat only rest */
  272. "jra 4f\n"
  273. "1:\t"
  274. "movew %2@+,%4\n\t" /* add first word to sum */
  275. "addw %4,%0\n\t"
  276. "movew %4,%3@+\n\t"
  277. "clrl %4\n\t"
  278. "addxl %4,%0\n" /* add X bit */
  279. "2:\t"
  280. /* unrolled loop for the main part: do 8 longs at once */
  281. "movel %1,%4\n\t" /* save len in tmp1 */
  282. "lsrl #5,%1\n\t" /* len/32 */
  283. "jeq 2f\n\t" /* not enough... */
  284. "subql #1,%1\n"
  285. "1:\t"
  286. "movel %2@+,%5\n\t"
  287. "addxl %5,%0\n\t"
  288. "movel %5,%3@+\n\t"
  289. "movel %2@+,%5\n\t"
  290. "addxl %5,%0\n\t"
  291. "movel %5,%3@+\n\t"
  292. "movel %2@+,%5\n\t"
  293. "addxl %5,%0\n\t"
  294. "movel %5,%3@+\n\t"
  295. "movel %2@+,%5\n\t"
  296. "addxl %5,%0\n\t"
  297. "movel %5,%3@+\n\t"
  298. "movel %2@+,%5\n\t"
  299. "addxl %5,%0\n\t"
  300. "movel %5,%3@+\n\t"
  301. "movel %2@+,%5\n\t"
  302. "addxl %5,%0\n\t"
  303. "movel %5,%3@+\n\t"
  304. "movel %2@+,%5\n\t"
  305. "addxl %5,%0\n\t"
  306. "movel %5,%3@+\n\t"
  307. "movel %2@+,%5\n\t"
  308. "addxl %5,%0\n\t"
  309. "movel %5,%3@+\n\t"
  310. "dbra %1,1b\n\t"
  311. "clrl %5\n\t"
  312. "addxl %5,%0\n\t" /* add X bit */
  313. "clrw %1\n\t"
  314. "subql #1,%1\n\t"
  315. "jcc 1b\n"
  316. "2:\t"
  317. "movel %4,%1\n\t" /* restore len from tmp1 */
  318. "andw #0x1c,%4\n\t" /* number of rest longs */
  319. "jeq 4f\n\t"
  320. "lsrw #2,%4\n\t"
  321. "subqw #1,%4\n"
  322. "3:\t"
  323. /* loop for rest longs */
  324. "movel %2@+,%5\n\t"
  325. "addxl %5,%0\n\t"
  326. "movel %5,%3@+\n\t"
  327. "dbra %4,3b\n\t"
  328. "clrl %5\n\t"
  329. "addxl %5,%0\n" /* add X bit */
  330. "4:\t"
  331. /* now check for rest bytes that do not fit into longs */
  332. "andw #3,%1\n\t"
  333. "jeq 7f\n\t"
  334. "clrl %5\n\t" /* clear tmp2 for rest bytes */
  335. "subqw #2,%1\n\t"
  336. "jlt 5f\n\t"
  337. "movew %2@+,%5\n\t" /* have rest >= 2: get word */
  338. "movew %5,%3@+\n\t"
  339. "swap %5\n\t" /* into bits 16..31 */
  340. "tstw %1\n\t" /* another byte? */
  341. "jeq 6f\n"
  342. "5:\t"
  343. "moveb %2@,%5\n\t" /* have odd rest: get byte */
  344. "moveb %5,%3@+\n\t"
  345. "lslw #8,%5\n" /* into bits 8..15; 16..31 untouched */
  346. "6:\t"
  347. "addl %5,%0\n\t" /* now add rest long to sum */
  348. "clrl %5\n\t"
  349. "addxl %5,%0\n" /* add X bit */
  350. "7:\t"
  351. : "=d" (sum), "=d" (len), "=a" (src), "=a" (dst),
  352. "=&d" (tmp1), "=&d" (tmp2)
  353. : "0" (sum), "1" (len), "2" (src), "3" (dst)
  354. );
  355. return(sum);
  356. }
  357. EXPORT_SYMBOL(csum_partial_copy_nocheck);