Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __SPARC_CHECKSUM_H
0003 #define __SPARC_CHECKSUM_H
0004 
0005 /*  checksum.h:  IP/UDP/TCP checksum routines on the Sparc.
0006  *
0007  *  Copyright(C) 1995 Linus Torvalds
0008  *  Copyright(C) 1995 Miguel de Icaza
0009  *  Copyright(C) 1996 David S. Miller
0010  *  Copyright(C) 1996 Eddie C. Dost
0011  *  Copyright(C) 1997 Jakub Jelinek
0012  *
0013  * derived from:
0014  *  Alpha checksum c-code
0015  *      ix86 inline assembly
0016  *      RFC1071 Computing the Internet Checksum
0017  */
0018 
0019 #include <linux/in6.h>
0020 #include <linux/uaccess.h>
0021 
0022 /* computes the checksum of a memory block at buff, length len,
0023  * and adds in "sum" (32-bit)
0024  *
0025  * returns a 32-bit number suitable for feeding into itself
0026  * or csum_tcpudp_magic
0027  *
0028  * this function must be called with even lengths, except
0029  * for the last fragment, which may be odd
0030  *
0031  * it's best to have buff aligned on a 32-bit boundary
0032  */
0033 __wsum csum_partial(const void *buff, int len, __wsum sum);
0034 
0035 /* the same as csum_partial, but copies from fs:src while it
0036  * checksums
0037  *
0038  * here even more important to align src and dst on a 32-bit (or even
0039  * better 64-bit) boundary
0040  */
0041 
0042 unsigned int __csum_partial_copy_sparc_generic (const unsigned char *, unsigned char *);
0043 
0044 static inline __wsum
0045 csum_partial_copy_nocheck(const void *src, void *dst, int len)
0046 {
0047     register unsigned int ret asm("o0") = (unsigned int)src;
0048     register char *d asm("o1") = dst;
0049     register int l asm("g1") = len;
0050 
0051     __asm__ __volatile__ (
0052         "call __csum_partial_copy_sparc_generic\n\t"
0053         " mov -1, %%g7\n"
0054     : "=&r" (ret), "=&r" (d), "=&r" (l)
0055     : "0" (ret), "1" (d), "2" (l)
0056     : "o2", "o3", "o4", "o5", "o7",
0057       "g2", "g3", "g4", "g5", "g7",
0058       "memory", "cc");
0059     return (__force __wsum)ret;
0060 }
0061 
0062 static inline __wsum
0063 csum_and_copy_from_user(const void __user *src, void *dst, int len)
0064 {
0065     if (unlikely(!access_ok(src, len)))
0066         return 0;
0067     return csum_partial_copy_nocheck((__force void *)src, dst, len);
0068 }
0069 
0070 static inline __wsum
0071 csum_and_copy_to_user(const void *src, void __user *dst, int len)
0072 {
0073     if (!access_ok(dst, len))
0074         return 0;
0075     return csum_partial_copy_nocheck(src, (__force void *)dst, len);
0076 }
0077 
0078 /* ihl is always 5 or greater, almost always is 5, and iph is word aligned
0079  * the majority of the time.
0080  */
0081 static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
0082 {
0083     __sum16 sum;
0084 
0085     /* Note: We must read %2 before we touch %0 for the first time,
0086      *       because GCC can legitimately use the same register for
0087      *       both operands.
0088      */
0089     __asm__ __volatile__("sub\t%2, 4, %%g4\n\t"
0090                  "ld\t[%1 + 0x00], %0\n\t"
0091                  "ld\t[%1 + 0x04], %%g2\n\t"
0092                  "ld\t[%1 + 0x08], %%g3\n\t"
0093                  "addcc\t%%g2, %0, %0\n\t"
0094                  "addxcc\t%%g3, %0, %0\n\t"
0095                  "ld\t[%1 + 0x0c], %%g2\n\t"
0096                  "ld\t[%1 + 0x10], %%g3\n\t"
0097                  "addxcc\t%%g2, %0, %0\n\t"
0098                  "addx\t%0, %%g0, %0\n"
0099                  "1:\taddcc\t%%g3, %0, %0\n\t"
0100                  "add\t%1, 4, %1\n\t"
0101                  "addxcc\t%0, %%g0, %0\n\t"
0102                  "subcc\t%%g4, 1, %%g4\n\t"
0103                  "be,a\t2f\n\t"
0104                  "sll\t%0, 16, %%g2\n\t"
0105                  "b\t1b\n\t"
0106                  "ld\t[%1 + 0x10], %%g3\n"
0107                  "2:\taddcc\t%0, %%g2, %%g2\n\t"
0108                  "srl\t%%g2, 16, %0\n\t"
0109                  "addx\t%0, %%g0, %0\n\t"
0110                  "xnor\t%%g0, %0, %0"
0111                  : "=r" (sum), "=&r" (iph)
0112                  : "r" (ihl), "1" (iph)
0113                  : "g2", "g3", "g4", "cc", "memory");
0114     return sum;
0115 }
0116 
0117 /* Fold a partial checksum without adding pseudo headers. */
0118 static inline __sum16 csum_fold(__wsum sum)
0119 {
0120     unsigned int tmp;
0121 
0122     __asm__ __volatile__("addcc\t%0, %1, %1\n\t"
0123                  "srl\t%1, 16, %1\n\t"
0124                  "addx\t%1, %%g0, %1\n\t"
0125                  "xnor\t%%g0, %1, %0"
0126                  : "=&r" (sum), "=r" (tmp)
0127                  : "0" (sum), "1" ((__force u32)sum<<16)
0128                  : "cc");
0129     return (__force __sum16)sum;
0130 }
0131 
0132 static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
0133                     __u32 len, __u8 proto,
0134                     __wsum sum)
0135 {
0136     __asm__ __volatile__("addcc\t%1, %0, %0\n\t"
0137                  "addxcc\t%2, %0, %0\n\t"
0138                  "addxcc\t%3, %0, %0\n\t"
0139                  "addx\t%0, %%g0, %0\n\t"
0140                  : "=r" (sum), "=r" (saddr)
0141                  : "r" (daddr), "r" (proto + len), "0" (sum),
0142                    "1" (saddr)
0143                  : "cc");
0144     return sum;
0145 }
0146 
0147 /*
0148  * computes the checksum of the TCP/UDP pseudo-header
0149  * returns a 16-bit checksum, already complemented
0150  */
0151 static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
0152                     __u32 len, __u8 proto,
0153                     __wsum sum)
0154 {
0155     return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
0156 }
0157 
0158 #define _HAVE_ARCH_IPV6_CSUM
0159 
0160 static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
0161                       const struct in6_addr *daddr,
0162                       __u32 len, __u8 proto, __wsum sum)
0163 {
0164     __asm__ __volatile__ (
0165         "addcc  %3, %4, %%g4\n\t"
0166         "addxcc %5, %%g4, %%g4\n\t"
0167         "ld [%2 + 0x0c], %%g2\n\t"
0168         "ld [%2 + 0x08], %%g3\n\t"
0169         "addxcc %%g2, %%g4, %%g4\n\t"
0170         "ld [%2 + 0x04], %%g2\n\t"
0171         "addxcc %%g3, %%g4, %%g4\n\t"
0172         "ld [%2 + 0x00], %%g3\n\t"
0173         "addxcc %%g2, %%g4, %%g4\n\t"
0174         "ld [%1 + 0x0c], %%g2\n\t"
0175         "addxcc %%g3, %%g4, %%g4\n\t"
0176         "ld [%1 + 0x08], %%g3\n\t"
0177         "addxcc %%g2, %%g4, %%g4\n\t"
0178         "ld [%1 + 0x04], %%g2\n\t"
0179         "addxcc %%g3, %%g4, %%g4\n\t"
0180         "ld [%1 + 0x00], %%g3\n\t"
0181         "addxcc %%g2, %%g4, %%g4\n\t"
0182         "addxcc %%g3, %%g4, %0\n\t"
0183         "addx   0, %0, %0\n"
0184         : "=&r" (sum)
0185         : "r" (saddr), "r" (daddr),
0186           "r"(htonl(len)), "r"(htonl(proto)), "r"(sum)
0187         : "g2", "g3", "g4", "cc");
0188 
0189     return csum_fold(sum);
0190 }
0191 
0192 /* this routine is used for miscellaneous IP-like checksums, mainly in icmp.c */
0193 static inline __sum16 ip_compute_csum(const void *buff, int len)
0194 {
0195     return csum_fold(csum_partial(buff, len, 0));
0196 }
0197 
0198 #define HAVE_ARCH_CSUM_ADD
0199 static inline __wsum csum_add(__wsum csum, __wsum addend)
0200 {
0201     __asm__ __volatile__(
0202         "addcc   %0, %1, %0\n"
0203         "addx    %0, %%g0, %0"
0204         : "=r" (csum)
0205         : "r" (addend), "0" (csum));
0206 
0207     return csum;
0208 }
0209 
0210 #endif /* !(__SPARC_CHECKSUM_H) */