blob: 8bd53589ecfb93337c920d254e9307617bc21a32 [file] [log] [blame]
Ingo Molnard76c1ae2008-02-17 16:48:25 +01001/*
2 * Copyright 2002, 2003 Andi Kleen, SuSE Labs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Subject to the GNU Public License v.2
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +01004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Wrappers of assembly checksum functions for x86-64.
6 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <asm/checksum.h>
Paul Gortmakere6830142016-07-13 20:18:57 -04008#include <linux/export.h>
Andy Lutomirski13d4ea02016-07-14 13:22:57 -07009#include <linux/uaccess.h>
H. Peter Anvin7263dda2013-08-30 15:43:03 -070010#include <asm/smap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +010012/**
13 * csum_partial_copy_from_user - Copy and checksum from user space.
14 * @src: source address (user space)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * @dst: destination address
16 * @len: number of bytes to be copied.
17 * @isum: initial sum that is added into the result (32bit unfolded)
18 * @errp: set to -EFAULT for an bad source address.
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +010019 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * Returns an 32bit unfolded checksum of the buffer.
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +010021 * src and dst are best aligned to 64bits.
22 */
Al Viroa4f89fb2006-11-14 21:20:08 -080023__wsum
24csum_partial_copy_from_user(const void __user *src, void *dst,
25 int len, __wsum isum, int *errp)
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +010026{
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 might_sleep();
28 *errp = 0;
Ingo Molnard76c1ae2008-02-17 16:48:25 +010029
30 if (!likely(access_ok(VERIFY_READ, src, len)))
31 goto out_err;
32
33 /*
34 * Why 6, not 7? To handle odd addresses aligned we
35 * would need to do considerable complications to fix the
36 * checksum which is defined as an 16bit accumulator. The
37 * fix alignment code is primarily for performance
38 * compatibility with 32bit and that will handle odd
39 * addresses slowly too.
40 */
41 if (unlikely((unsigned long)src & 6)) {
42 while (((unsigned long)src & 6) && len >= 2) {
43 __u16 val16;
44
Linus Torvalds3b912702014-11-16 11:00:42 -080045 if (__get_user(val16, (const __u16 __user *)src))
46 goto out_err;
Ingo Molnard76c1ae2008-02-17 16:48:25 +010047
48 *(__u16 *)dst = val16;
49 isum = (__force __wsum)add32_with_carry(
50 (__force unsigned)isum, val16);
51 src += 2;
52 dst += 2;
53 len -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 }
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +010055 }
H. Peter Anvin7263dda2013-08-30 15:43:03 -070056 stac();
Ingo Molnard76c1ae2008-02-17 16:48:25 +010057 isum = csum_partial_copy_generic((__force const void *)src,
58 dst, len, isum, errp, NULL);
H. Peter Anvin7263dda2013-08-30 15:43:03 -070059 clac();
Ingo Molnard76c1ae2008-02-17 16:48:25 +010060 if (unlikely(*errp))
61 goto out_err;
62
63 return isum;
64
65out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 *errp = -EFAULT;
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +010067 memset(dst, 0, len);
Ingo Molnard76c1ae2008-02-17 16:48:25 +010068
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +010069 return isum;
70}
Linus Torvalds1da177e2005-04-16 15:20:36 -070071EXPORT_SYMBOL(csum_partial_copy_from_user);
72
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +010073/**
74 * csum_partial_copy_to_user - Copy and checksum to user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * @src: source address
76 * @dst: destination address (user space)
77 * @len: number of bytes to be copied.
78 * @isum: initial sum that is added into the result (32bit unfolded)
79 * @errp: set to -EFAULT for an bad destination address.
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +010080 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * Returns an 32bit unfolded checksum of the buffer.
82 * src and dst are best aligned to 64bits.
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +010083 */
Al Viroa4f89fb2006-11-14 21:20:08 -080084__wsum
85csum_partial_copy_to_user(const void *src, void __user *dst,
86 int len, __wsum isum, int *errp)
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +010087{
H. Peter Anvin7263dda2013-08-30 15:43:03 -070088 __wsum ret;
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 might_sleep();
Ingo Molnard76c1ae2008-02-17 16:48:25 +010091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (unlikely(!access_ok(VERIFY_WRITE, dst, len))) {
93 *errp = -EFAULT;
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +010094 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 }
96
97 if (unlikely((unsigned long)dst & 6)) {
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +010098 while (((unsigned long)dst & 6) && len >= 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 __u16 val16 = *(__u16 *)src;
Ingo Molnard76c1ae2008-02-17 16:48:25 +0100100
Al Viroa4f89fb2006-11-14 21:20:08 -0800101 isum = (__force __wsum)add32_with_carry(
102 (__force unsigned)isum, val16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 *errp = __put_user(val16, (__u16 __user *)dst);
104 if (*errp)
105 return isum;
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +0100106 src += 2;
107 dst += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 len -= 2;
109 }
110 }
111
112 *errp = 0;
H. Peter Anvin7263dda2013-08-30 15:43:03 -0700113 stac();
114 ret = csum_partial_copy_generic(src, (void __force *)dst,
115 len, isum, NULL, errp);
116 clac();
117 return ret;
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +0100118}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119EXPORT_SYMBOL(csum_partial_copy_to_user);
120
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +0100121/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * csum_partial_copy_nocheck - Copy and checksum.
123 * @src: source address
124 * @dst: destination address
125 * @len: number of bytes to be copied.
Wanpeng Lic15acff2012-06-12 12:53:21 +0800126 * @sum: initial sum that is added into the result (32bit unfolded)
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +0100127 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 * Returns an 32bit unfolded checksum of the buffer.
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +0100129 */
Al Viroa4f89fb2006-11-14 21:20:08 -0800130__wsum
131csum_partial_copy_nocheck(const void *src, void *dst, int len, __wsum sum)
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +0100132{
133 return csum_partial_copy_generic(src, dst, len, sum, NULL, NULL);
134}
Andi Kleen2ee60e172006-06-26 13:59:44 +0200135EXPORT_SYMBOL(csum_partial_copy_nocheck);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Al Viroa4f89fb2006-11-14 21:20:08 -0800137__sum16 csum_ipv6_magic(const struct in6_addr *saddr,
138 const struct in6_addr *daddr,
Alexander Duyck1e940822016-03-11 14:05:41 -0800139 __u32 len, __u8 proto, __wsum sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 __u64 rest, sum64;
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +0100142
Al Viroa4f89fb2006-11-14 21:20:08 -0800143 rest = (__force __u64)htonl(len) + (__force __u64)htons(proto) +
144 (__force __u64)sum;
Ingo Molnard76c1ae2008-02-17 16:48:25 +0100145
146 asm(" addq (%[saddr]),%[sum]\n"
147 " adcq 8(%[saddr]),%[sum]\n"
148 " adcq (%[daddr]),%[sum]\n"
149 " adcq 8(%[daddr]),%[sum]\n"
150 " adcq $0,%[sum]\n"
151
Paolo Ciarrocchi0df025b2008-02-17 14:56:50 +0100152 : [sum] "=r" (sum64)
153 : "[sum]" (rest), [saddr] "r" (saddr), [daddr] "r" (daddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Ingo Molnard76c1ae2008-02-17 16:48:25 +0100155 return csum_fold(
156 (__force __wsum)add32_with_carry(sum64 & 0xffffffff, sum64>>32));
157}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158EXPORT_SYMBOL(csum_ipv6_magic);