checksum_32.S 10 KB

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