checksum_32.S 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. * Pentium Pro/II routines:
  12. * Alexander Kjeldaas <astor@guardian.no>
  13. * Finn Arne Gangstad <finnag@guardian.no>
  14. * Lots of code moved from tcp.c and ip.c; see those files
  15. * for more names.
  16. *
  17. * Changes: Ingo Molnar, converted csum_partial_copy() to 2.1 exception
  18. * handling.
  19. * Andi Kleen, add zeroing on error
  20. * converted to pure assembler
  21. *
  22. * This program is free software; you can redistribute it and/or
  23. * modify it under the terms of the GNU General Public License
  24. * as published by the Free Software Foundation; either version
  25. * 2 of the License, or (at your option) any later version.
  26. */
  27. #include <asm/errno.h>
  28. /*
  29. * computes a partial checksum, e.g. for TCP/UDP fragments
  30. */
  31. /*
  32. unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum)
  33. */
  34. .text
  35. .align 4
  36. .globl csum_partial
  37. #ifndef CONFIG_X86_USE_PPRO_CHECKSUM
  38. /*
  39. * Experiments with Ethernet and SLIP connections show that buff
  40. * is aligned on either a 2-byte or 4-byte boundary. We get at
  41. * least a twofold speedup on 486 and Pentium if it is 4-byte aligned.
  42. * Fortunately, it is easy to convert 2-byte alignment to 4-byte
  43. * alignment for the unrolled loop.
  44. */
  45. csum_partial:
  46. pushl %esi
  47. pushl %ebx
  48. movl 20(%esp),%eax # Function arg: unsigned int sum
  49. movl 16(%esp),%ecx # Function arg: int len
  50. movl 12(%esp),%esi # Function arg: unsigned char *buff
  51. testl $2, %esi # Check alignment.
  52. jz 2f # Jump if alignment is ok.
  53. subl $2, %ecx # Alignment uses up two bytes.
  54. jae 1f # Jump if we had at least two bytes.
  55. addl $2, %ecx # ecx was < 2. Deal with it.
  56. jmp 4f
  57. 1: movw (%esi), %bx
  58. addl $2, %esi
  59. addw %bx, %ax
  60. adcl $0, %eax
  61. 2:
  62. movl %ecx, %edx
  63. shrl $5, %ecx
  64. jz 2f
  65. testl %esi, %esi
  66. 1: movl (%esi), %ebx
  67. adcl %ebx, %eax
  68. movl 4(%esi), %ebx
  69. adcl %ebx, %eax
  70. movl 8(%esi), %ebx
  71. adcl %ebx, %eax
  72. movl 12(%esi), %ebx
  73. adcl %ebx, %eax
  74. movl 16(%esi), %ebx
  75. adcl %ebx, %eax
  76. movl 20(%esi), %ebx
  77. adcl %ebx, %eax
  78. movl 24(%esi), %ebx
  79. adcl %ebx, %eax
  80. movl 28(%esi), %ebx
  81. adcl %ebx, %eax
  82. lea 32(%esi), %esi
  83. dec %ecx
  84. jne 1b
  85. adcl $0, %eax
  86. 2: movl %edx, %ecx
  87. andl $0x1c, %edx
  88. je 4f
  89. shrl $2, %edx # This clears CF
  90. 3: adcl (%esi), %eax
  91. lea 4(%esi), %esi
  92. dec %edx
  93. jne 3b
  94. adcl $0, %eax
  95. 4: andl $3, %ecx
  96. jz 7f
  97. cmpl $2, %ecx
  98. jb 5f
  99. movw (%esi),%cx
  100. leal 2(%esi),%esi
  101. je 6f
  102. shll $16,%ecx
  103. 5: movb (%esi),%cl
  104. 6: addl %ecx,%eax
  105. adcl $0, %eax
  106. 7:
  107. popl %ebx
  108. popl %esi
  109. ret
  110. #else
  111. /* Version for PentiumII/PPro */
  112. csum_partial:
  113. pushl %esi
  114. pushl %ebx
  115. movl 20(%esp),%eax # Function arg: unsigned int sum
  116. movl 16(%esp),%ecx # Function arg: int len
  117. movl 12(%esp),%esi # Function arg: const unsigned char *buf
  118. testl $2, %esi
  119. jnz 30f
  120. 10:
  121. movl %ecx, %edx
  122. movl %ecx, %ebx
  123. andl $0x7c, %ebx
  124. shrl $7, %ecx
  125. addl %ebx,%esi
  126. shrl $2, %ebx
  127. negl %ebx
  128. lea 45f(%ebx,%ebx,2), %ebx
  129. testl %esi, %esi
  130. jmp *%ebx
  131. # Handle 2-byte-aligned regions
  132. 20: addw (%esi), %ax
  133. lea 2(%esi), %esi
  134. adcl $0, %eax
  135. jmp 10b
  136. 30: subl $2, %ecx
  137. ja 20b
  138. je 32f
  139. movzbl (%esi),%ebx # csumming 1 byte, 2-aligned
  140. addl %ebx, %eax
  141. adcl $0, %eax
  142. jmp 80f
  143. 32:
  144. addw (%esi), %ax # csumming 2 bytes, 2-aligned
  145. adcl $0, %eax
  146. jmp 80f
  147. 40:
  148. addl -128(%esi), %eax
  149. adcl -124(%esi), %eax
  150. adcl -120(%esi), %eax
  151. adcl -116(%esi), %eax
  152. adcl -112(%esi), %eax
  153. adcl -108(%esi), %eax
  154. adcl -104(%esi), %eax
  155. adcl -100(%esi), %eax
  156. adcl -96(%esi), %eax
  157. adcl -92(%esi), %eax
  158. adcl -88(%esi), %eax
  159. adcl -84(%esi), %eax
  160. adcl -80(%esi), %eax
  161. adcl -76(%esi), %eax
  162. adcl -72(%esi), %eax
  163. adcl -68(%esi), %eax
  164. adcl -64(%esi), %eax
  165. adcl -60(%esi), %eax
  166. adcl -56(%esi), %eax
  167. adcl -52(%esi), %eax
  168. adcl -48(%esi), %eax
  169. adcl -44(%esi), %eax
  170. adcl -40(%esi), %eax
  171. adcl -36(%esi), %eax
  172. adcl -32(%esi), %eax
  173. adcl -28(%esi), %eax
  174. adcl -24(%esi), %eax
  175. adcl -20(%esi), %eax
  176. adcl -16(%esi), %eax
  177. adcl -12(%esi), %eax
  178. adcl -8(%esi), %eax
  179. adcl -4(%esi), %eax
  180. 45:
  181. lea 128(%esi), %esi
  182. adcl $0, %eax
  183. dec %ecx
  184. jge 40b
  185. movl %edx, %ecx
  186. 50: andl $3, %ecx
  187. jz 80f
  188. # Handle the last 1-3 bytes without jumping
  189. notl %ecx # 1->2, 2->1, 3->0, higher bits are masked
  190. movl $0xffffff,%ebx # by the shll and shrl instructions
  191. shll $3,%ecx
  192. shrl %cl,%ebx
  193. andl -128(%esi),%ebx # esi is 4-aligned so should be ok
  194. addl %ebx,%eax
  195. adcl $0,%eax
  196. 80:
  197. popl %ebx
  198. popl %esi
  199. ret
  200. #endif
  201. /*
  202. unsigned int csum_partial_copy_generic (const char *src, char *dst,
  203. int len, int sum, int *src_err_ptr, int *dst_err_ptr)
  204. */
  205. /*
  206. * Copy from ds while checksumming, otherwise like csum_partial
  207. *
  208. * The macros SRC and DST specify the type of access for the instruction.
  209. * thus we can call a custom exception handler for all access types.
  210. *
  211. * FIXME: could someone double-check whether I haven't mixed up some SRC and
  212. * DST definitions? It's damn hard to trigger all cases. I hope I got
  213. * them all but there's no guarantee.
  214. */
  215. #define SRC(y...) \
  216. 9999: y; \
  217. .section __ex_table, "a"; \
  218. .long 9999b, 6001f ; \
  219. .previous
  220. #define DST(y...) \
  221. 9999: y; \
  222. .section __ex_table, "a"; \
  223. .long 9999b, 6002f ; \
  224. .previous
  225. .align 4
  226. #ifndef CONFIG_X86_USE_PPRO_CHECKSUM
  227. #define ARGBASE 16
  228. #define FP 12
  229. csum_partial_copy_generic_i386:
  230. subl $4,%esp
  231. pushl %edi
  232. pushl %esi
  233. pushl %ebx
  234. movl ARGBASE+16(%esp),%eax # sum
  235. movl ARGBASE+12(%esp),%ecx # len
  236. movl ARGBASE+4(%esp),%esi # src
  237. movl ARGBASE+8(%esp),%edi # dst
  238. testl $2, %edi # Check alignment.
  239. jz 2f # Jump if alignment is ok.
  240. subl $2, %ecx # Alignment uses up two bytes.
  241. jae 1f # Jump if we had at least two bytes.
  242. addl $2, %ecx # ecx was < 2. Deal with it.
  243. jmp 4f
  244. SRC(1: movw (%esi), %bx )
  245. addl $2, %esi
  246. DST( movw %bx, (%edi) )
  247. addl $2, %edi
  248. addw %bx, %ax
  249. adcl $0, %eax
  250. 2:
  251. movl %ecx, FP(%esp)
  252. shrl $5, %ecx
  253. jz 2f
  254. testl %esi, %esi
  255. SRC(1: movl (%esi), %ebx )
  256. SRC( movl 4(%esi), %edx )
  257. adcl %ebx, %eax
  258. DST( movl %ebx, (%edi) )
  259. adcl %edx, %eax
  260. DST( movl %edx, 4(%edi) )
  261. SRC( movl 8(%esi), %ebx )
  262. SRC( movl 12(%esi), %edx )
  263. adcl %ebx, %eax
  264. DST( movl %ebx, 8(%edi) )
  265. adcl %edx, %eax
  266. DST( movl %edx, 12(%edi) )
  267. SRC( movl 16(%esi), %ebx )
  268. SRC( movl 20(%esi), %edx )
  269. adcl %ebx, %eax
  270. DST( movl %ebx, 16(%edi) )
  271. adcl %edx, %eax
  272. DST( movl %edx, 20(%edi) )
  273. SRC( movl 24(%esi), %ebx )
  274. SRC( movl 28(%esi), %edx )
  275. adcl %ebx, %eax
  276. DST( movl %ebx, 24(%edi) )
  277. adcl %edx, %eax
  278. DST( movl %edx, 28(%edi) )
  279. lea 32(%esi), %esi
  280. lea 32(%edi), %edi
  281. dec %ecx
  282. jne 1b
  283. adcl $0, %eax
  284. 2: movl FP(%esp), %edx
  285. movl %edx, %ecx
  286. andl $0x1c, %edx
  287. je 4f
  288. shrl $2, %edx # This clears CF
  289. SRC(3: movl (%esi), %ebx )
  290. adcl %ebx, %eax
  291. DST( movl %ebx, (%edi) )
  292. lea 4(%esi), %esi
  293. lea 4(%edi), %edi
  294. dec %edx
  295. jne 3b
  296. adcl $0, %eax
  297. 4: andl $3, %ecx
  298. jz 7f
  299. cmpl $2, %ecx
  300. jb 5f
  301. SRC( movw (%esi), %cx )
  302. leal 2(%esi), %esi
  303. DST( movw %cx, (%edi) )
  304. leal 2(%edi), %edi
  305. je 6f
  306. shll $16,%ecx
  307. SRC(5: movb (%esi), %cl )
  308. DST( movb %cl, (%edi) )
  309. 6: addl %ecx, %eax
  310. adcl $0, %eax
  311. 7:
  312. 5000:
  313. # Exception handler:
  314. .section .fixup, "ax"
  315. 6001:
  316. movl ARGBASE+20(%esp), %ebx # src_err_ptr
  317. movl $-EFAULT, (%ebx)
  318. # zero the complete destination - computing the rest
  319. # is too much work
  320. movl ARGBASE+8(%esp), %edi # dst
  321. movl ARGBASE+12(%esp), %ecx # len
  322. xorl %eax,%eax
  323. rep ; stosb
  324. jmp 5000b
  325. 6002:
  326. movl ARGBASE+24(%esp), %ebx # dst_err_ptr
  327. movl $-EFAULT,(%ebx)
  328. jmp 5000b
  329. .previous
  330. popl %ebx
  331. popl %esi
  332. popl %edi
  333. popl %ecx # equivalent to addl $4,%esp
  334. ret
  335. #else
  336. /* Version for PentiumII/PPro */
  337. #define ROUND1(x) \
  338. SRC(movl x(%esi), %ebx ) ; \
  339. addl %ebx, %eax ; \
  340. DST(movl %ebx, x(%edi) ) ;
  341. #define ROUND(x) \
  342. SRC(movl x(%esi), %ebx ) ; \
  343. adcl %ebx, %eax ; \
  344. DST(movl %ebx, x(%edi) ) ;
  345. #define ARGBASE 12
  346. csum_partial_copy_generic_i386:
  347. pushl %ebx
  348. pushl %edi
  349. pushl %esi
  350. movl ARGBASE+4(%esp),%esi #src
  351. movl ARGBASE+8(%esp),%edi #dst
  352. movl ARGBASE+12(%esp),%ecx #len
  353. movl ARGBASE+16(%esp),%eax #sum
  354. # movl %ecx, %edx
  355. movl %ecx, %ebx
  356. movl %esi, %edx
  357. shrl $6, %ecx
  358. andl $0x3c, %ebx
  359. negl %ebx
  360. subl %ebx, %esi
  361. subl %ebx, %edi
  362. lea -1(%esi),%edx
  363. andl $-32,%edx
  364. lea 3f(%ebx,%ebx), %ebx
  365. testl %esi, %esi
  366. jmp *%ebx
  367. 1: addl $64,%esi
  368. addl $64,%edi
  369. SRC(movb -32(%edx),%bl) ; SRC(movb (%edx),%bl)
  370. ROUND1(-64) ROUND(-60) ROUND(-56) ROUND(-52)
  371. ROUND (-48) ROUND(-44) ROUND(-40) ROUND(-36)
  372. ROUND (-32) ROUND(-28) ROUND(-24) ROUND(-20)
  373. ROUND (-16) ROUND(-12) ROUND(-8) ROUND(-4)
  374. 3: adcl $0,%eax
  375. addl $64, %edx
  376. dec %ecx
  377. jge 1b
  378. 4: movl ARGBASE+12(%esp),%edx #len
  379. andl $3, %edx
  380. jz 7f
  381. cmpl $2, %edx
  382. jb 5f
  383. SRC( movw (%esi), %dx )
  384. leal 2(%esi), %esi
  385. DST( movw %dx, (%edi) )
  386. leal 2(%edi), %edi
  387. je 6f
  388. shll $16,%edx
  389. 5:
  390. SRC( movb (%esi), %dl )
  391. DST( movb %dl, (%edi) )
  392. 6: addl %edx, %eax
  393. adcl $0, %eax
  394. 7:
  395. .section .fixup, "ax"
  396. 6001: movl ARGBASE+20(%esp), %ebx # src_err_ptr
  397. movl $-EFAULT, (%ebx)
  398. # zero the complete destination (computing the rest is too much work)
  399. movl ARGBASE+8(%esp),%edi # dst
  400. movl ARGBASE+12(%esp),%ecx # len
  401. xorl %eax,%eax
  402. rep; stosb
  403. jmp 7b
  404. 6002: movl ARGBASE+24(%esp), %ebx # dst_err_ptr
  405. movl $-EFAULT, (%ebx)
  406. jmp 7b
  407. .previous
  408. popl %esi
  409. popl %edi
  410. popl %ebx
  411. ret
  412. #undef ROUND
  413. #undef ROUND1
  414. #endif