checksum_32.S 11 KB

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