checksum_32.S 11 KB

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