checksum_32.S 10 KB

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