checksum.c 10 KB

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