blob: 23318c338db0d3e27d42e9d0a5d99ae9656d4223 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
Jan Beulich00e065e2007-05-02 19:27:05 +020024#include <linux/linkage.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/errno.h>
H. Peter Anvin5f2e8a82012-04-20 12:19:50 -070026#include <asm/asm.h>
Al Viro784d5692016-01-11 11:04:34 -050027#include <asm/export.h>
David Woodhouse50967322018-01-11 21:46:32 +000028#include <asm/nospec-branch.h>
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/*
31 * computes a partial checksum, e.g. for TCP/UDP fragments
32 */
33
34/*
35unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum)
36 */
37
38.text
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#ifndef CONFIG_X86_USE_PPRO_CHECKSUM
41
42 /*
43 * Experiments with Ethernet and SLIP connections show that buff
44 * is aligned on either a 2-byte or 4-byte boundary. We get at
45 * least a twofold speedup on 486 and Pentium if it is 4-byte aligned.
46 * Fortunately, it is easy to convert 2-byte alignment to 4-byte
47 * alignment for the unrolled loop.
48 */
Jiri Slaby6d685e52019-10-11 13:51:07 +020049SYM_FUNC_START(csum_partial)
Ingo Molnar131484c2015-05-28 12:21:47 +020050 pushl %esi
51 pushl %ebx
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 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.
Andy Shevchenkod50ba362013-04-15 12:06:10 +030058 jz 10f # Jump if alignment is boundary of 2 bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 # buf is odd
61 dec %ecx
62 jl 8f
63 movzbl (%esi), %ebx
64 adcl %ebx, %eax
65 roll $8, %eax
66 inc %esi
67 testl $2, %esi
68 jz 2f
6910:
70 subl $2, %ecx # Alignment uses up two bytes.
71 jae 1f # Jump if we had at least two bytes.
72 addl $2, %ecx # ecx was < 2. Deal with it.
73 jmp 4f
741: movw (%esi), %bx
75 addl $2, %esi
76 addw %bx, %ax
77 adcl $0, %eax
782:
79 movl %ecx, %edx
80 shrl $5, %ecx
81 jz 2f
82 testl %esi, %esi
831: movl (%esi), %ebx
84 adcl %ebx, %eax
85 movl 4(%esi), %ebx
86 adcl %ebx, %eax
87 movl 8(%esi), %ebx
88 adcl %ebx, %eax
89 movl 12(%esi), %ebx
90 adcl %ebx, %eax
91 movl 16(%esi), %ebx
92 adcl %ebx, %eax
93 movl 20(%esi), %ebx
94 adcl %ebx, %eax
95 movl 24(%esi), %ebx
96 adcl %ebx, %eax
97 movl 28(%esi), %ebx
98 adcl %ebx, %eax
99 lea 32(%esi), %esi
100 dec %ecx
101 jne 1b
102 adcl $0, %eax
1032: movl %edx, %ecx
104 andl $0x1c, %edx
105 je 4f
106 shrl $2, %edx # This clears CF
1073: adcl (%esi), %eax
108 lea 4(%esi), %esi
109 dec %edx
110 jne 3b
111 adcl $0, %eax
1124: andl $3, %ecx
113 jz 7f
114 cmpl $2, %ecx
115 jb 5f
116 movw (%esi),%cx
117 leal 2(%esi),%esi
118 je 6f
119 shll $16,%ecx
1205: movb (%esi),%cl
1216: addl %ecx,%eax
122 adcl $0, %eax
1237:
Denys Vlasenko3e1aa7c2015-03-06 21:55:32 +0100124 testb $1, 12(%esp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 jz 8f
126 roll $8, %eax
1278:
Ingo Molnar131484c2015-05-28 12:21:47 +0200128 popl %ebx
129 popl %esi
Peter Zijlstraf94909c2021-12-04 14:43:40 +0100130 RET
Jiri Slaby6d685e52019-10-11 13:51:07 +0200131SYM_FUNC_END(csum_partial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133#else
134
135/* Version for PentiumII/PPro */
136
Jiri Slaby6d685e52019-10-11 13:51:07 +0200137SYM_FUNC_START(csum_partial)
Ingo Molnar131484c2015-05-28 12:21:47 +0200138 pushl %esi
139 pushl %ebx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 movl 20(%esp),%eax # Function arg: unsigned int sum
141 movl 16(%esp),%ecx # Function arg: int len
142 movl 12(%esp),%esi # Function arg: const unsigned char *buf
143
144 testl $3, %esi
145 jnz 25f
14610:
147 movl %ecx, %edx
148 movl %ecx, %ebx
149 andl $0x7c, %ebx
150 shrl $7, %ecx
151 addl %ebx,%esi
152 shrl $2, %ebx
153 negl %ebx
154 lea 45f(%ebx,%ebx,2), %ebx
155 testl %esi, %esi
Peter Zijlstra34fdce62020-04-22 17:16:40 +0200156 JMP_NOSPEC ebx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 # Handle 2-byte-aligned regions
15920: addw (%esi), %ax
160 lea 2(%esi), %esi
161 adcl $0, %eax
162 jmp 10b
16325:
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
17730: subl $2, %ecx
178 ja 20b
179 je 32f
180 addl $2, %ecx
181 jz 80f
182 movzbl (%esi),%ebx # csumming 1 byte, 2-aligned
183 addl %ebx, %eax
184 adcl $0, %eax
185 jmp 80f
18632:
187 addw (%esi), %ax # csumming 2 bytes, 2-aligned
188 adcl $0, %eax
189 jmp 80f
190
19140:
192 addl -128(%esi), %eax
193 adcl -124(%esi), %eax
194 adcl -120(%esi), %eax
195 adcl -116(%esi), %eax
196 adcl -112(%esi), %eax
197 adcl -108(%esi), %eax
198 adcl -104(%esi), %eax
199 adcl -100(%esi), %eax
200 adcl -96(%esi), %eax
201 adcl -92(%esi), %eax
202 adcl -88(%esi), %eax
203 adcl -84(%esi), %eax
204 adcl -80(%esi), %eax
205 adcl -76(%esi), %eax
206 adcl -72(%esi), %eax
207 adcl -68(%esi), %eax
208 adcl -64(%esi), %eax
209 adcl -60(%esi), %eax
210 adcl -56(%esi), %eax
211 adcl -52(%esi), %eax
212 adcl -48(%esi), %eax
213 adcl -44(%esi), %eax
214 adcl -40(%esi), %eax
215 adcl -36(%esi), %eax
216 adcl -32(%esi), %eax
217 adcl -28(%esi), %eax
218 adcl -24(%esi), %eax
219 adcl -20(%esi), %eax
220 adcl -16(%esi), %eax
221 adcl -12(%esi), %eax
222 adcl -8(%esi), %eax
223 adcl -4(%esi), %eax
22445:
225 lea 128(%esi), %esi
226 adcl $0, %eax
227 dec %ecx
228 jge 40b
229 movl %edx, %ecx
23050: andl $3, %ecx
231 jz 80f
232
233 # Handle the last 1-3 bytes without jumping
234 notl %ecx # 1->2, 2->1, 3->0, higher bits are masked
235 movl $0xffffff,%ebx # by the shll and shrl instructions
236 shll $3,%ecx
237 shrl %cl,%ebx
238 andl -128(%esi),%ebx # esi is 4-aligned so should be ok
239 addl %ebx,%eax
240 adcl $0,%eax
24180:
Denys Vlasenko3e1aa7c2015-03-06 21:55:32 +0100242 testb $1, 12(%esp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 jz 90f
244 roll $8, %eax
24590:
Ingo Molnar131484c2015-05-28 12:21:47 +0200246 popl %ebx
247 popl %esi
Peter Zijlstraf94909c2021-12-04 14:43:40 +0100248 RET
Jiri Slaby6d685e52019-10-11 13:51:07 +0200249SYM_FUNC_END(csum_partial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251#endif
Al Viro784d5692016-01-11 11:04:34 -0500252EXPORT_SYMBOL(csum_partial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254/*
255unsigned int csum_partial_copy_generic (const char *src, char *dst,
Al Viroe8b95082020-07-12 23:53:10 -0400256 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 */
258
259/*
260 * Copy from ds while checksumming, otherwise like csum_partial
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 */
262
Peter Zijlstrafedb24c2021-11-10 11:01:19 +0100263#define EXC(y...) \
264 9999: y; \
265 _ASM_EXTABLE_TYPE(9999b, 7f, EX_TYPE_UACCESS | EX_FLAG_CLEAR_AX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267#ifndef CONFIG_X86_USE_PPRO_CHECKSUM
268
269#define ARGBASE 16
270#define FP 12
271
Jiri Slaby6dcc5622019-10-11 13:51:04 +0200272SYM_FUNC_START(csum_partial_copy_generic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 subl $4,%esp
Ingo Molnar131484c2015-05-28 12:21:47 +0200274 pushl %edi
275 pushl %esi
276 pushl %ebx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 movl ARGBASE+12(%esp),%ecx # len
278 movl ARGBASE+4(%esp),%esi # src
279 movl ARGBASE+8(%esp),%edi # dst
280
Al Viroe8b95082020-07-12 23:53:10 -0400281 movl $-1, %eax # sum
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 testl $2, %edi # Check alignment.
283 jz 2f # Jump if alignment is ok.
284 subl $2, %ecx # Alignment uses up two bytes.
285 jae 1f # Jump if we had at least two bytes.
286 addl $2, %ecx # ecx was < 2. Deal with it.
287 jmp 4f
Al Viroe8b95082020-07-12 23:53:10 -0400288EXC(1: movw (%esi), %bx )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 addl $2, %esi
Al Viroe8b95082020-07-12 23:53:10 -0400290EXC( movw %bx, (%edi) )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 addl $2, %edi
292 addw %bx, %ax
293 adcl $0, %eax
2942:
295 movl %ecx, FP(%esp)
296 shrl $5, %ecx
297 jz 2f
Al Viroe8b95082020-07-12 23:53:10 -0400298 testl %esi, %esi # what's wrong with clc?
299EXC(1: movl (%esi), %ebx )
300EXC( movl 4(%esi), %edx )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 adcl %ebx, %eax
Al Viroe8b95082020-07-12 23:53:10 -0400302EXC( movl %ebx, (%edi) )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 adcl %edx, %eax
Al Viroe8b95082020-07-12 23:53:10 -0400304EXC( movl %edx, 4(%edi) )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Al Viroe8b95082020-07-12 23:53:10 -0400306EXC( movl 8(%esi), %ebx )
307EXC( movl 12(%esi), %edx )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 adcl %ebx, %eax
Al Viroe8b95082020-07-12 23:53:10 -0400309EXC( movl %ebx, 8(%edi) )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 adcl %edx, %eax
Al Viroe8b95082020-07-12 23:53:10 -0400311EXC( movl %edx, 12(%edi) )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Al Viroe8b95082020-07-12 23:53:10 -0400313EXC( movl 16(%esi), %ebx )
314EXC( movl 20(%esi), %edx )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 adcl %ebx, %eax
Al Viroe8b95082020-07-12 23:53:10 -0400316EXC( movl %ebx, 16(%edi) )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 adcl %edx, %eax
Al Viroe8b95082020-07-12 23:53:10 -0400318EXC( movl %edx, 20(%edi) )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Al Viroe8b95082020-07-12 23:53:10 -0400320EXC( movl 24(%esi), %ebx )
321EXC( movl 28(%esi), %edx )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 adcl %ebx, %eax
Al Viroe8b95082020-07-12 23:53:10 -0400323EXC( movl %ebx, 24(%edi) )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 adcl %edx, %eax
Al Viroe8b95082020-07-12 23:53:10 -0400325EXC( movl %edx, 28(%edi) )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 lea 32(%esi), %esi
328 lea 32(%edi), %edi
329 dec %ecx
330 jne 1b
331 adcl $0, %eax
3322: movl FP(%esp), %edx
333 movl %edx, %ecx
334 andl $0x1c, %edx
335 je 4f
336 shrl $2, %edx # This clears CF
Al Viroe8b95082020-07-12 23:53:10 -0400337EXC(3: movl (%esi), %ebx )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 adcl %ebx, %eax
Al Viroe8b95082020-07-12 23:53:10 -0400339EXC( movl %ebx, (%edi) )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 lea 4(%esi), %esi
341 lea 4(%edi), %edi
342 dec %edx
343 jne 3b
344 adcl $0, %eax
3454: andl $3, %ecx
346 jz 7f
347 cmpl $2, %ecx
348 jb 5f
Al Viroe8b95082020-07-12 23:53:10 -0400349EXC( movw (%esi), %cx )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 leal 2(%esi), %esi
Al Viroe8b95082020-07-12 23:53:10 -0400351EXC( movw %cx, (%edi) )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 leal 2(%edi), %edi
353 je 6f
354 shll $16,%ecx
Al Viroe8b95082020-07-12 23:53:10 -0400355EXC(5: movb (%esi), %cl )
356EXC( movb %cl, (%edi) )
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576: addl %ecx, %eax
358 adcl $0, %eax
3597:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Ingo Molnar131484c2015-05-28 12:21:47 +0200361 popl %ebx
362 popl %esi
363 popl %edi
364 popl %ecx # equivalent to addl $4,%esp
Peter Zijlstraf94909c2021-12-04 14:43:40 +0100365 RET
Jiri Slaby6dcc5622019-10-11 13:51:04 +0200366SYM_FUNC_END(csum_partial_copy_generic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368#else
369
370/* Version for PentiumII/PPro */
371
372#define ROUND1(x) \
Al Viroe8b95082020-07-12 23:53:10 -0400373 EXC(movl x(%esi), %ebx ) ; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 addl %ebx, %eax ; \
Al Viroe8b95082020-07-12 23:53:10 -0400375 EXC(movl %ebx, x(%edi) ) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377#define ROUND(x) \
Al Viroe8b95082020-07-12 23:53:10 -0400378 EXC(movl x(%esi), %ebx ) ; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 adcl %ebx, %eax ; \
Al Viroe8b95082020-07-12 23:53:10 -0400380 EXC(movl %ebx, x(%edi) ) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382#define ARGBASE 12
383
Jiri Slaby6dcc5622019-10-11 13:51:04 +0200384SYM_FUNC_START(csum_partial_copy_generic)
Ingo Molnar131484c2015-05-28 12:21:47 +0200385 pushl %ebx
386 pushl %edi
387 pushl %esi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 movl ARGBASE+4(%esp),%esi #src
389 movl ARGBASE+8(%esp),%edi #dst
390 movl ARGBASE+12(%esp),%ecx #len
Al Viroe8b95082020-07-12 23:53:10 -0400391 movl $-1, %eax #sum
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392# movl %ecx, %edx
393 movl %ecx, %ebx
394 movl %esi, %edx
395 shrl $6, %ecx
396 andl $0x3c, %ebx
397 negl %ebx
398 subl %ebx, %esi
399 subl %ebx, %edi
400 lea -1(%esi),%edx
401 andl $-32,%edx
402 lea 3f(%ebx,%ebx), %ebx
403 testl %esi, %esi
Peter Zijlstra34fdce62020-04-22 17:16:40 +0200404 JMP_NOSPEC ebx
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051: addl $64,%esi
406 addl $64,%edi
Al Viroe8b95082020-07-12 23:53:10 -0400407 EXC(movb -32(%edx),%bl) ; EXC(movb (%edx),%bl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 ROUND1(-64) ROUND(-60) ROUND(-56) ROUND(-52)
409 ROUND (-48) ROUND(-44) ROUND(-40) ROUND(-36)
410 ROUND (-32) ROUND(-28) ROUND(-24) ROUND(-20)
411 ROUND (-16) ROUND(-12) ROUND(-8) ROUND(-4)
4123: adcl $0,%eax
413 addl $64, %edx
414 dec %ecx
415 jge 1b
4164: movl ARGBASE+12(%esp),%edx #len
417 andl $3, %edx
418 jz 7f
419 cmpl $2, %edx
420 jb 5f
Al Viroe8b95082020-07-12 23:53:10 -0400421EXC( movw (%esi), %dx )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 leal 2(%esi), %esi
Al Viroe8b95082020-07-12 23:53:10 -0400423EXC( movw %dx, (%edi) )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 leal 2(%edi), %edi
425 je 6f
426 shll $16,%edx
4275:
Al Viroe8b95082020-07-12 23:53:10 -0400428EXC( movb (%esi), %dl )
429EXC( movb %dl, (%edi) )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306: addl %edx, %eax
431 adcl $0, %eax
4327:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Ingo Molnar131484c2015-05-28 12:21:47 +0200434 popl %esi
435 popl %edi
436 popl %ebx
Peter Zijlstraf94909c2021-12-04 14:43:40 +0100437 RET
Jiri Slaby6dcc5622019-10-11 13:51:04 +0200438SYM_FUNC_END(csum_partial_copy_generic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440#undef ROUND
441#undef ROUND1
442
443#endif
Al Viro784d5692016-01-11 11:04:34 -0500444EXPORT_SYMBOL(csum_partial_copy_generic)