checksum_32.S 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * INET An implementation of the TCP/IP protocol suite for the LINUX
  4. * operating system. INET is implemented using the BSD Socket
  5. * interface as the means of communication with the user level.
  6. *
  7. * IP/TCP/UDP checksumming routines
  8. *
  9. * Authors: Jorge Cwik, <jorge@laser.satlink.net>
  10. * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
  11. * Tom May, <ftom@netcom.com>
  12. * Pentium Pro/II routines:
  13. * Alexander Kjeldaas <astor@guardian.no>
  14. * Finn Arne Gangstad <finnag@guardian.no>
  15. * Lots of code moved from tcp.c and ip.c; see those files
  16. * for more names.
  17. *
  18. * Changes: Ingo Molnar, converted csum_partial_copy() to 2.1 exception
  19. * handling.
  20. * Andi Kleen, add zeroing on error
  21. * converted to pure assembler
  22. */
  23. #include <asm/errno.h>
  24. #include <asm/asm.h>
  25. #include <asm/export.h>
  26. /*
  27. * computes a partial checksum, e.g. for TCP/UDP fragments
  28. */
  29. /*
  30. unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum)
  31. */
  32. .text
  33. .align 4
  34. .globl csum_partial
  35. #ifndef CONFIG_X86_USE_PPRO_CHECKSUM
  36. /*
  37. * Experiments with Ethernet and SLIP connections show that buff
  38. * is aligned on either a 2-byte or 4-byte boundary. We get at
  39. * least a twofold speedup on 486 and Pentium if it is 4-byte aligned.
  40. * Fortunately, it is easy to convert 2-byte alignment to 4-byte
  41. * alignment for the unrolled loop.
  42. */
  43. csum_partial:
  44. pushl %esi
  45. pushl %ebx
  46. movl 20(%esp),%eax # Function arg: unsigned int sum
  47. movl 16(%esp),%ecx # Function arg: int len
  48. movl 12(%esp),%esi # Function arg: unsigned char *buff
  49. testl $2, %esi # Check alignment.
  50. jz 2f # Jump if alignment is ok.
  51. subl $2, %ecx # Alignment uses up two bytes.
  52. jae 1f # Jump if we had at least two bytes.
  53. addl $2, %ecx # ecx was < 2. Deal with it.
  54. jmp 4f
  55. 1: movw (%esi), %bx
  56. addl $2, %esi
  57. addw %bx, %ax
  58. adcl $0, %eax
  59. 2:
  60. movl %ecx, %edx
  61. shrl $5, %ecx
  62. jz 2f
  63. testl %esi, %esi
  64. 1: movl (%esi), %ebx
  65. adcl %ebx, %eax
  66. movl 4(%esi), %ebx
  67. adcl %ebx, %eax
  68. movl 8(%esi), %ebx
  69. adcl %ebx, %eax
  70. movl 12(%esi), %ebx
  71. adcl %ebx, %eax
  72. movl 16(%esi), %ebx
  73. adcl %ebx, %eax
  74. movl 20(%esi), %ebx
  75. adcl %ebx, %eax
  76. movl 24(%esi), %ebx
  77. adcl %ebx, %eax
  78. movl 28(%esi), %ebx
  79. adcl %ebx, %eax
  80. lea 32(%esi), %esi
  81. dec %ecx
  82. jne 1b
  83. adcl $0, %eax
  84. 2: movl %edx, %ecx
  85. andl $0x1c, %edx
  86. je 4f
  87. shrl $2, %edx # This clears CF
  88. 3: adcl (%esi), %eax
  89. lea 4(%esi), %esi
  90. dec %edx
  91. jne 3b
  92. adcl $0, %eax
  93. 4: andl $3, %ecx
  94. jz 7f
  95. cmpl $2, %ecx
  96. jb 5f
  97. movw (%esi),%cx
  98. leal 2(%esi),%esi
  99. je 6f
  100. shll $16,%ecx
  101. 5: movb (%esi),%cl
  102. 6: addl %ecx,%eax
  103. adcl $0, %eax
  104. 7:
  105. popl %ebx
  106. popl %esi
  107. ret
  108. #else
  109. /* Version for PentiumII/PPro */
  110. csum_partial:
  111. pushl %esi
  112. pushl %ebx
  113. movl 20(%esp),%eax # Function arg: unsigned int sum
  114. movl 16(%esp),%ecx # Function arg: int len
  115. movl 12(%esp),%esi # Function arg: const unsigned char *buf
  116. testl $2, %esi
  117. jnz 30f
  118. 10:
  119. movl %ecx, %edx
  120. movl %ecx, %ebx
  121. andl $0x7c, %ebx
  122. shrl $7, %ecx
  123. addl %ebx,%esi
  124. shrl $2, %ebx
  125. negl %ebx
  126. lea 45f(%ebx,%ebx,2), %ebx
  127. testl %esi, %esi
  128. jmp *%ebx
  129. # Handle 2-byte-aligned regions
  130. 20: addw (%esi), %ax
  131. lea 2(%esi), %esi
  132. adcl $0, %eax
  133. jmp 10b
  134. 30: subl $2, %ecx
  135. ja 20b
  136. je 32f
  137. movzbl (%esi),%ebx # csumming 1 byte, 2-aligned
  138. addl %ebx, %eax
  139. adcl $0, %eax
  140. jmp 80f
  141. 32:
  142. addw (%esi), %ax # csumming 2 bytes, 2-aligned
  143. adcl $0, %eax
  144. jmp 80f
  145. 40:
  146. addl -128(%esi), %eax
  147. adcl -124(%esi), %eax
  148. adcl -120(%esi), %eax
  149. adcl -116(%esi), %eax
  150. adcl -112(%esi), %eax
  151. adcl -108(%esi), %eax
  152. adcl -104(%esi), %eax
  153. adcl -100(%esi), %eax
  154. adcl -96(%esi), %eax
  155. adcl -92(%esi), %eax
  156. adcl -88(%esi), %eax
  157. adcl -84(%esi), %eax
  158. adcl -80(%esi), %eax
  159. adcl -76(%esi), %eax
  160. adcl -72(%esi), %eax
  161. adcl -68(%esi), %eax
  162. adcl -64(%esi), %eax
  163. adcl -60(%esi), %eax
  164. adcl -56(%esi), %eax
  165. adcl -52(%esi), %eax
  166. adcl -48(%esi), %eax
  167. adcl -44(%esi), %eax
  168. adcl -40(%esi), %eax
  169. adcl -36(%esi), %eax
  170. adcl -32(%esi), %eax
  171. adcl -28(%esi), %eax
  172. adcl -24(%esi), %eax
  173. adcl -20(%esi), %eax
  174. adcl -16(%esi), %eax
  175. adcl -12(%esi), %eax
  176. adcl -8(%esi), %eax
  177. adcl -4(%esi), %eax
  178. 45:
  179. lea 128(%esi), %esi
  180. adcl $0, %eax
  181. dec %ecx
  182. jge 40b
  183. movl %edx, %ecx
  184. 50: andl $3, %ecx
  185. jz 80f
  186. # Handle the last 1-3 bytes without jumping
  187. notl %ecx # 1->2, 2->1, 3->0, higher bits are masked
  188. movl $0xffffff,%ebx # by the shll and shrl instructions
  189. shll $3,%ecx
  190. shrl %cl,%ebx
  191. andl -128(%esi),%ebx # esi is 4-aligned so should be ok
  192. addl %ebx,%eax
  193. adcl $0,%eax
  194. 80:
  195. popl %ebx
  196. popl %esi
  197. ret
  198. #endif
  199. EXPORT_SYMBOL(csum_partial)