checksum.S 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /* SPDX-License-Identifier: GPL-2.0+
  2. *
  3. * $Id: checksum.S,v 1.10 2001/07/06 13:11:32 gniibe Exp $
  4. *
  5. * INET An implementation of the TCP/IP protocol suite for the LINUX
  6. * operating system. INET is implemented using the BSD Socket
  7. * interface as the means of communication with the user level.
  8. *
  9. * IP/TCP/UDP checksumming routines
  10. *
  11. * Authors: Jorge Cwik, <jorge@laser.satlink.net>
  12. * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
  13. * Tom May, <ftom@netcom.com>
  14. * Pentium Pro/II routines:
  15. * Alexander Kjeldaas <astor@guardian.no>
  16. * Finn Arne Gangstad <finnag@guardian.no>
  17. * Lots of code moved from tcp.c and ip.c; see those files
  18. * for more names.
  19. *
  20. * Changes: Ingo Molnar, converted csum_partial_copy() to 2.1 exception
  21. * handling.
  22. * Andi Kleen, add zeroing on error
  23. * converted to pure assembler
  24. *
  25. * SuperH version: Copyright (C) 1999 Niibe Yutaka
  26. */
  27. #include <asm/errno.h>
  28. #include <linux/linkage.h>
  29. /*
  30. * computes a partial checksum, e.g. for TCP/UDP fragments
  31. */
  32. /*
  33. * asmlinkage __wsum csum_partial(const void *buf, int len, __wsum sum);
  34. */
  35. .text
  36. ENTRY(csum_partial)
  37. /*
  38. * Experiments with Ethernet and SLIP connections show that buff
  39. * is aligned on either a 2-byte or 4-byte boundary. We get at
  40. * least a twofold speedup on 486 and Pentium if it is 4-byte aligned.
  41. * Fortunately, it is easy to convert 2-byte alignment to 4-byte
  42. * alignment for the unrolled loop.
  43. */
  44. mov r4, r0
  45. tst #3, r0 ! Check alignment.
  46. bt/s 2f ! Jump if alignment is ok.
  47. mov r4, r7 ! Keep a copy to check for alignment
  48. !
  49. tst #1, r0 ! Check alignment.
  50. bt 21f ! Jump if alignment is boundary of 2bytes.
  51. ! buf is odd
  52. tst r5, r5
  53. add #-1, r5
  54. bt 9f
  55. mov.b @r4+, r0
  56. extu.b r0, r0
  57. addc r0, r6 ! t=0 from previous tst
  58. mov r6, r0
  59. shll8 r6
  60. shlr16 r0
  61. shlr8 r0
  62. or r0, r6
  63. mov r4, r0
  64. tst #2, r0
  65. bt 2f
  66. 21:
  67. ! buf is 2 byte aligned (len could be 0)
  68. add #-2, r5 ! Alignment uses up two bytes.
  69. cmp/pz r5 !
  70. bt/s 1f ! Jump if we had at least two bytes.
  71. clrt
  72. bra 6f
  73. add #2, r5 ! r5 was < 2. Deal with it.
  74. 1:
  75. mov.w @r4+, r0
  76. extu.w r0, r0
  77. addc r0, r6
  78. bf 2f
  79. add #1, r6
  80. 2:
  81. ! buf is 4 byte aligned (len could be 0)
  82. mov r5, r1
  83. mov #-5, r0
  84. shld r0, r1
  85. tst r1, r1
  86. bt/s 4f ! if it's =0, go to 4f
  87. clrt
  88. .align 2
  89. 3:
  90. mov.l @r4+, r0
  91. mov.l @r4+, r2
  92. mov.l @r4+, r3
  93. addc r0, r6
  94. mov.l @r4+, r0
  95. addc r2, r6
  96. mov.l @r4+, r2
  97. addc r3, r6
  98. mov.l @r4+, r3
  99. addc r0, r6
  100. mov.l @r4+, r0
  101. addc r2, r6
  102. mov.l @r4+, r2
  103. addc r3, r6
  104. addc r0, r6
  105. addc r2, r6
  106. movt r0
  107. dt r1
  108. bf/s 3b
  109. cmp/eq #1, r0
  110. ! here, we know r1==0
  111. addc r1, r6 ! add carry to r6
  112. 4:
  113. mov r5, r0
  114. and #0x1c, r0
  115. tst r0, r0
  116. bt 6f
  117. ! 4 bytes or more remaining
  118. mov r0, r1
  119. shlr2 r1
  120. mov #0, r2
  121. 5:
  122. addc r2, r6
  123. mov.l @r4+, r2
  124. movt r0
  125. dt r1
  126. bf/s 5b
  127. cmp/eq #1, r0
  128. addc r2, r6
  129. addc r1, r6 ! r1==0 here, so it means add carry-bit
  130. 6:
  131. ! 3 bytes or less remaining
  132. mov #3, r0
  133. and r0, r5
  134. tst r5, r5
  135. bt 9f ! if it's =0 go to 9f
  136. mov #2, r1
  137. cmp/hs r1, r5
  138. bf 7f
  139. mov.w @r4+, r0
  140. extu.w r0, r0
  141. cmp/eq r1, r5
  142. bt/s 8f
  143. clrt
  144. shll16 r0
  145. addc r0, r6
  146. 7:
  147. mov.b @r4+, r0
  148. extu.b r0, r0
  149. #ifndef __LITTLE_ENDIAN__
  150. shll8 r0
  151. #endif
  152. 8:
  153. addc r0, r6
  154. mov #0, r0
  155. addc r0, r6
  156. 9:
  157. ! Check if the buffer was misaligned, if so realign sum
  158. mov r7, r0
  159. tst #1, r0
  160. bt 10f
  161. mov r6, r0
  162. shll8 r6
  163. shlr16 r0
  164. shlr8 r0
  165. or r0, r6
  166. 10:
  167. rts
  168. mov r6, r0
  169. /*
  170. unsigned int csum_partial_copy_generic (const char *src, char *dst, int len)
  171. */
  172. /*
  173. * Copy from ds while checksumming, otherwise like csum_partial with initial
  174. * sum being ~0U
  175. */
  176. #define EXC(...) \
  177. 9999: __VA_ARGS__ ; \
  178. .section __ex_table, "a"; \
  179. .long 9999b, 6001f ; \
  180. .previous
  181. !
  182. ! r4: const char *SRC
  183. ! r5: char *DST
  184. ! r6: int LEN
  185. !
  186. ENTRY(csum_partial_copy_generic)
  187. mov #-1,r7
  188. mov #3,r0 ! Check src and dest are equally aligned
  189. mov r4,r1
  190. and r0,r1
  191. and r5,r0
  192. cmp/eq r1,r0
  193. bf 3f ! Different alignments, use slow version
  194. tst #1,r0 ! Check dest word aligned
  195. bf 3f ! If not, do it the slow way
  196. mov #2,r0
  197. tst r0,r5 ! Check dest alignment.
  198. bt 2f ! Jump if alignment is ok.
  199. add #-2,r6 ! Alignment uses up two bytes.
  200. cmp/pz r6 ! Jump if we had at least two bytes.
  201. bt/s 1f
  202. clrt
  203. add #2,r6 ! r6 was < 2. Deal with it.
  204. bra 4f
  205. mov r6,r2
  206. 3: ! Handle different src and dest alignments.
  207. ! This is not common, so simple byte by byte copy will do.
  208. mov r6,r2
  209. shlr r6
  210. tst r6,r6
  211. bt 4f
  212. clrt
  213. .align 2
  214. 5:
  215. EXC( mov.b @r4+,r1 )
  216. EXC( mov.b @r4+,r0 )
  217. extu.b r1,r1
  218. EXC( mov.b r1,@r5 )
  219. EXC( mov.b r0,@(1,r5) )
  220. extu.b r0,r0
  221. add #2,r5
  222. #ifdef __LITTLE_ENDIAN__
  223. shll8 r0
  224. #else
  225. shll8 r1
  226. #endif
  227. or r1,r0
  228. addc r0,r7
  229. movt r0
  230. dt r6
  231. bf/s 5b
  232. cmp/eq #1,r0
  233. mov #0,r0
  234. addc r0, r7
  235. mov r2, r0
  236. tst #1, r0
  237. bt 7f
  238. bra 5f
  239. clrt
  240. ! src and dest equally aligned, but to a two byte boundary.
  241. ! Handle first two bytes as a special case
  242. .align 2
  243. 1:
  244. EXC( mov.w @r4+,r0 )
  245. EXC( mov.w r0,@r5 )
  246. add #2,r5
  247. extu.w r0,r0
  248. addc r0,r7
  249. mov #0,r0
  250. addc r0,r7
  251. 2:
  252. mov r6,r2
  253. mov #-5,r0
  254. shld r0,r6
  255. tst r6,r6
  256. bt/s 2f
  257. clrt
  258. .align 2
  259. 1:
  260. EXC( mov.l @r4+,r0 )
  261. EXC( mov.l @r4+,r1 )
  262. addc r0,r7
  263. EXC( mov.l r0,@r5 )
  264. EXC( mov.l r1,@(4,r5) )
  265. addc r1,r7
  266. EXC( mov.l @r4+,r0 )
  267. EXC( mov.l @r4+,r1 )
  268. addc r0,r7
  269. EXC( mov.l r0,@(8,r5) )
  270. EXC( mov.l r1,@(12,r5) )
  271. addc r1,r7
  272. EXC( mov.l @r4+,r0 )
  273. EXC( mov.l @r4+,r1 )
  274. addc r0,r7
  275. EXC( mov.l r0,@(16,r5) )
  276. EXC( mov.l r1,@(20,r5) )
  277. addc r1,r7
  278. EXC( mov.l @r4+,r0 )
  279. EXC( mov.l @r4+,r1 )
  280. addc r0,r7
  281. EXC( mov.l r0,@(24,r5) )
  282. EXC( mov.l r1,@(28,r5) )
  283. addc r1,r7
  284. add #32,r5
  285. movt r0
  286. dt r6
  287. bf/s 1b
  288. cmp/eq #1,r0
  289. mov #0,r0
  290. addc r0,r7
  291. 2: mov r2,r6
  292. mov #0x1c,r0
  293. and r0,r6
  294. cmp/pl r6
  295. bf/s 4f
  296. clrt
  297. shlr2 r6
  298. 3:
  299. EXC( mov.l @r4+,r0 )
  300. addc r0,r7
  301. EXC( mov.l r0,@r5 )
  302. add #4,r5
  303. movt r0
  304. dt r6
  305. bf/s 3b
  306. cmp/eq #1,r0
  307. mov #0,r0
  308. addc r0,r7
  309. 4: mov r2,r6
  310. mov #3,r0
  311. and r0,r6
  312. cmp/pl r6
  313. bf 7f
  314. mov #2,r1
  315. cmp/hs r1,r6
  316. bf 5f
  317. EXC( mov.w @r4+,r0 )
  318. EXC( mov.w r0,@r5 )
  319. extu.w r0,r0
  320. add #2,r5
  321. cmp/eq r1,r6
  322. bt/s 6f
  323. clrt
  324. shll16 r0
  325. addc r0,r7
  326. 5:
  327. EXC( mov.b @r4+,r0 )
  328. EXC( mov.b r0,@r5 )
  329. extu.b r0,r0
  330. #ifndef __LITTLE_ENDIAN__
  331. shll8 r0
  332. #endif
  333. 6: addc r0,r7
  334. mov #0,r0
  335. addc r0,r7
  336. 7:
  337. # Exception handler:
  338. .section .fixup, "ax"
  339. 6001:
  340. rts
  341. mov #0,r0
  342. .previous
  343. rts
  344. mov r7,r0