checksum_32.S 10 KB

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