checksum_32.S 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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/nospec-branch.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_NOSPEC %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. /*
  240. unsigned int csum_partial_copy_generic (const char *src, char *dst,
  241. int len, int sum, int *src_err_ptr, int *dst_err_ptr)
  242. */
  243. /*
  244. * Copy from ds while checksumming, otherwise like csum_partial
  245. *
  246. * The macros SRC and DST specify the type of access for the instruction.
  247. * thus we can call a custom exception handler for all access types.
  248. *
  249. * FIXME: could someone double-check whether I haven't mixed up some SRC and
  250. * DST definitions? It's damn hard to trigger all cases. I hope I got
  251. * them all but there's no guarantee.
  252. */
  253. #define SRC(y...) \
  254. 9999: y; \
  255. _ASM_EXTABLE(9999b, 6001f)
  256. #define DST(y...) \
  257. 9999: y; \
  258. _ASM_EXTABLE(9999b, 6002f)
  259. #ifndef CONFIG_X86_USE_PPRO_CHECKSUM
  260. #define ARGBASE 16
  261. #define FP 12
  262. ENTRY(csum_partial_copy_generic)
  263. subl $4,%esp
  264. pushl %edi
  265. pushl %esi
  266. pushl %ebx
  267. movl ARGBASE+16(%esp),%eax # sum
  268. movl ARGBASE+12(%esp),%ecx # len
  269. movl ARGBASE+4(%esp),%esi # src
  270. movl ARGBASE+8(%esp),%edi # dst
  271. testl $2, %edi # Check alignment.
  272. jz 2f # Jump if alignment is ok.
  273. subl $2, %ecx # Alignment uses up two bytes.
  274. jae 1f # Jump if we had at least two bytes.
  275. addl $2, %ecx # ecx was < 2. Deal with it.
  276. jmp 4f
  277. SRC(1: movw (%esi), %bx )
  278. addl $2, %esi
  279. DST( movw %bx, (%edi) )
  280. addl $2, %edi
  281. addw %bx, %ax
  282. adcl $0, %eax
  283. 2:
  284. movl %ecx, FP(%esp)
  285. shrl $5, %ecx
  286. jz 2f
  287. testl %esi, %esi
  288. SRC(1: movl (%esi), %ebx )
  289. SRC( movl 4(%esi), %edx )
  290. adcl %ebx, %eax
  291. DST( movl %ebx, (%edi) )
  292. adcl %edx, %eax
  293. DST( movl %edx, 4(%edi) )
  294. SRC( movl 8(%esi), %ebx )
  295. SRC( movl 12(%esi), %edx )
  296. adcl %ebx, %eax
  297. DST( movl %ebx, 8(%edi) )
  298. adcl %edx, %eax
  299. DST( movl %edx, 12(%edi) )
  300. SRC( movl 16(%esi), %ebx )
  301. SRC( movl 20(%esi), %edx )
  302. adcl %ebx, %eax
  303. DST( movl %ebx, 16(%edi) )
  304. adcl %edx, %eax
  305. DST( movl %edx, 20(%edi) )
  306. SRC( movl 24(%esi), %ebx )
  307. SRC( movl 28(%esi), %edx )
  308. adcl %ebx, %eax
  309. DST( movl %ebx, 24(%edi) )
  310. adcl %edx, %eax
  311. DST( movl %edx, 28(%edi) )
  312. lea 32(%esi), %esi
  313. lea 32(%edi), %edi
  314. dec %ecx
  315. jne 1b
  316. adcl $0, %eax
  317. 2: movl FP(%esp), %edx
  318. movl %edx, %ecx
  319. andl $0x1c, %edx
  320. je 4f
  321. shrl $2, %edx # This clears CF
  322. SRC(3: movl (%esi), %ebx )
  323. adcl %ebx, %eax
  324. DST( movl %ebx, (%edi) )
  325. lea 4(%esi), %esi
  326. lea 4(%edi), %edi
  327. dec %edx
  328. jne 3b
  329. adcl $0, %eax
  330. 4: andl $3, %ecx
  331. jz 7f
  332. cmpl $2, %ecx
  333. jb 5f
  334. SRC( movw (%esi), %cx )
  335. leal 2(%esi), %esi
  336. DST( movw %cx, (%edi) )
  337. leal 2(%edi), %edi
  338. je 6f
  339. shll $16,%ecx
  340. SRC(5: movb (%esi), %cl )
  341. DST( movb %cl, (%edi) )
  342. 6: addl %ecx, %eax
  343. adcl $0, %eax
  344. 7:
  345. 5000:
  346. # Exception handler:
  347. .section .fixup, "ax"
  348. 6001:
  349. movl ARGBASE+20(%esp), %ebx # src_err_ptr
  350. movl $-EFAULT, (%ebx)
  351. # zero the complete destination - computing the rest
  352. # is too much work
  353. movl ARGBASE+8(%esp), %edi # dst
  354. movl ARGBASE+12(%esp), %ecx # len
  355. xorl %eax,%eax
  356. rep ; stosb
  357. jmp 5000b
  358. 6002:
  359. movl ARGBASE+24(%esp), %ebx # dst_err_ptr
  360. movl $-EFAULT,(%ebx)
  361. jmp 5000b
  362. .previous
  363. popl %ebx
  364. popl %esi
  365. popl %edi
  366. popl %ecx # equivalent to addl $4,%esp
  367. ret
  368. ENDPROC(csum_partial_copy_generic)
  369. #else
  370. /* Version for PentiumII/PPro */
  371. #define ROUND1(x) \
  372. SRC(movl x(%esi), %ebx ) ; \
  373. addl %ebx, %eax ; \
  374. DST(movl %ebx, x(%edi) ) ;
  375. #define ROUND(x) \
  376. SRC(movl x(%esi), %ebx ) ; \
  377. adcl %ebx, %eax ; \
  378. DST(movl %ebx, x(%edi) ) ;
  379. #define ARGBASE 12
  380. ENTRY(csum_partial_copy_generic)
  381. pushl %ebx
  382. pushl %edi
  383. pushl %esi
  384. movl ARGBASE+4(%esp),%esi #src
  385. movl ARGBASE+8(%esp),%edi #dst
  386. movl ARGBASE+12(%esp),%ecx #len
  387. movl ARGBASE+16(%esp),%eax #sum
  388. # movl %ecx, %edx
  389. movl %ecx, %ebx
  390. movl %esi, %edx
  391. shrl $6, %ecx
  392. andl $0x3c, %ebx
  393. negl %ebx
  394. subl %ebx, %esi
  395. subl %ebx, %edi
  396. lea -1(%esi),%edx
  397. andl $-32,%edx
  398. lea 3f(%ebx,%ebx), %ebx
  399. testl %esi, %esi
  400. JMP_NOSPEC %ebx
  401. 1: addl $64,%esi
  402. addl $64,%edi
  403. SRC(movb -32(%edx),%bl) ; SRC(movb (%edx),%bl)
  404. ROUND1(-64) ROUND(-60) ROUND(-56) ROUND(-52)
  405. ROUND (-48) ROUND(-44) ROUND(-40) ROUND(-36)
  406. ROUND (-32) ROUND(-28) ROUND(-24) ROUND(-20)
  407. ROUND (-16) ROUND(-12) ROUND(-8) ROUND(-4)
  408. 3: adcl $0,%eax
  409. addl $64, %edx
  410. dec %ecx
  411. jge 1b
  412. 4: movl ARGBASE+12(%esp),%edx #len
  413. andl $3, %edx
  414. jz 7f
  415. cmpl $2, %edx
  416. jb 5f
  417. SRC( movw (%esi), %dx )
  418. leal 2(%esi), %esi
  419. DST( movw %dx, (%edi) )
  420. leal 2(%edi), %edi
  421. je 6f
  422. shll $16,%edx
  423. 5:
  424. SRC( movb (%esi), %dl )
  425. DST( movb %dl, (%edi) )
  426. 6: addl %edx, %eax
  427. adcl $0, %eax
  428. 7:
  429. .section .fixup, "ax"
  430. 6001: movl ARGBASE+20(%esp), %ebx # src_err_ptr
  431. movl $-EFAULT, (%ebx)
  432. # zero the complete destination (computing the rest is too much work)
  433. movl ARGBASE+8(%esp),%edi # dst
  434. movl ARGBASE+12(%esp),%ecx # len
  435. xorl %eax,%eax
  436. rep; stosb
  437. jmp 7b
  438. 6002: movl ARGBASE+24(%esp), %ebx # dst_err_ptr
  439. movl $-EFAULT, (%ebx)
  440. jmp 7b
  441. .previous
  442. popl %esi
  443. popl %edi
  444. popl %ebx
  445. ret
  446. ENDPROC(csum_partial_copy_generic)
  447. #undef ROUND
  448. #undef ROUND1
  449. #endif