vserver 2.0 rc7
[linux-2.6.git] / arch / um / include / sysdep-x86_64 / checksum.h
1 /*
2  * Licensed under the GPL
3  */
4
5 #ifndef __UM_SYSDEP_CHECKSUM_H
6 #define __UM_SYSDEP_CHECKSUM_H
7
8 #include "linux/string.h"
9 #include "linux/in6.h"
10 #include "asm/uaccess.h"
11
12 extern unsigned csum_partial(const unsigned char *buff, unsigned len,
13                              unsigned sum);
14
15 /*
16  *      Note: when you get a NULL pointer exception here this means someone
17  *      passed in an incorrect kernel address to one of these functions.
18  *
19  *      If you use these functions directly please don't forget the
20  *      access_ok().
21  */
22
23 static __inline__
24 unsigned int csum_partial_copy_nocheck(const unsigned char *src, unsigned char *dst,
25                                        int len, int sum)
26 {
27         memcpy(dst, src, len);
28         return(csum_partial(dst, len, sum));
29 }
30
31 static __inline__
32 unsigned int csum_partial_copy_from_user(const unsigned char *src,
33                                          unsigned char *dst, int len, int sum,
34                                          int *err_ptr)
35 {
36         if(copy_from_user(dst, src, len)){
37                 *err_ptr = -EFAULT;
38                 return(-1);
39         }
40         return csum_partial(dst, len, sum);
41 }
42
43 /**
44  * csum_fold - Fold and invert a 32bit checksum.
45  * sum: 32bit unfolded sum
46  *
47  * Fold a 32bit running checksum to 16bit and invert it. This is usually
48  * the last step before putting a checksum into a packet.
49  * Make sure not to mix with 64bit checksums.
50  */
51 static inline unsigned int csum_fold(unsigned int sum)
52 {
53         __asm__(
54                 "  addl %1,%0\n"
55                 "  adcl $0xffff,%0"
56                 : "=r" (sum)
57                 : "r" (sum << 16), "0" (sum & 0xffff0000)
58         );
59         return (~sum) >> 16;
60 }
61
62 /**
63  * csum_tcpup_nofold - Compute an IPv4 pseudo header checksum.
64  * @saddr: source address
65  * @daddr: destination address
66  * @len: length of packet
67  * @proto: ip protocol of packet
68  * @sum: initial sum to be added in (32bit unfolded)
69  *
70  * Returns the pseudo header checksum the input data. Result is
71  * 32bit unfolded.
72  */
73 static inline unsigned long
74 csum_tcpudp_nofold(unsigned saddr, unsigned daddr, unsigned short len,
75                    unsigned short proto, unsigned int sum)
76 {
77         asm("  addl %1, %0\n"
78             "  adcl %2, %0\n"
79             "  adcl %3, %0\n"
80             "  adcl $0, %0\n"
81                 : "=r" (sum)
82             : "g" (daddr), "g" (saddr), "g" ((ntohs(len)<<16)+proto*256), "0" (sum));
83     return sum;
84 }
85
86 /*
87  * computes the checksum of the TCP/UDP pseudo-header
88  * returns a 16-bit checksum, already complemented
89  */
90 static inline unsigned short int csum_tcpudp_magic(unsigned long saddr,
91                                                    unsigned long daddr,
92                                                    unsigned short len,
93                                                    unsigned short proto,
94                                                    unsigned int sum)
95 {
96         return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
97 }
98
99 /**
100  * ip_fast_csum - Compute the IPv4 header checksum efficiently.
101  * iph: ipv4 header
102  * ihl: length of header / 4
103  */
104 static inline unsigned short ip_fast_csum(unsigned char *iph, unsigned int ihl)
105 {
106         unsigned int sum;
107
108         asm(    "  movl (%1), %0\n"
109                 "  subl $4, %2\n"
110                 "  jbe 2f\n"
111                 "  addl 4(%1), %0\n"
112                 "  adcl 8(%1), %0\n"
113                 "  adcl 12(%1), %0\n"
114                 "1: adcl 16(%1), %0\n"
115                 "  lea 4(%1), %1\n"
116                 "  decl %2\n"
117                 "  jne  1b\n"
118                 "  adcl $0, %0\n"
119                 "  movl %0, %2\n"
120                 "  shrl $16, %0\n"
121                 "  addw %w2, %w0\n"
122                 "  adcl $0, %0\n"
123                 "  notl %0\n"
124                 "2:"
125         /* Since the input registers which are loaded with iph and ipl
126            are modified, we must also specify them as outputs, or gcc
127            will assume they contain their original values. */
128         : "=r" (sum), "=r" (iph), "=r" (ihl)
129         : "1" (iph), "2" (ihl)
130         : "memory");
131         return(sum);
132 }
133
134 static inline unsigned add32_with_carry(unsigned a, unsigned b)
135 {
136         asm("addl %2,%0\n\t"
137             "adcl $0,%0"
138             : "=r" (a)
139             : "0" (a), "r" (b));
140         return a;
141 }
142
143 extern unsigned short ip_compute_csum(unsigned char * buff, int len);
144
145 #endif