blob: 189344924a2bea065e28aef795cf0d26d36dc1b7 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Copyright 2002, 2003 Andi Kleen, SuSE Labs.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004 *
5 * Wrappers of assembly checksum functions for x86-64.
6 */
7#include <asm/checksum.h>
8#include <linux/export.h>
9#include <linux/uaccess.h>
10#include <asm/smap.h>
11
12/**
Olivier Deprez157378f2022-04-04 15:47:50 +020013 * csum_and_copy_from_user - Copy and checksum from user space.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000014 * @src: source address (user space)
15 * @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.
19 *
20 * Returns an 32bit unfolded checksum of the buffer.
21 * src and dst are best aligned to 64bits.
22 */
23__wsum
Olivier Deprez157378f2022-04-04 15:47:50 +020024csum_and_copy_from_user(const void __user *src, void *dst, int len)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000025{
Olivier Deprez157378f2022-04-04 15:47:50 +020026 __wsum sum;
27
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000028 might_sleep();
Olivier Deprez157378f2022-04-04 15:47:50 +020029 if (!user_access_begin(src, len))
30 return 0;
31 sum = csum_partial_copy_generic((__force const void *)src, dst, len);
32 user_access_end();
33 return sum;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000034}
Olivier Deprez157378f2022-04-04 15:47:50 +020035EXPORT_SYMBOL(csum_and_copy_from_user);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000036
37/**
Olivier Deprez157378f2022-04-04 15:47:50 +020038 * csum_and_copy_to_user - Copy and checksum to user space.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000039 * @src: source address
40 * @dst: destination address (user space)
41 * @len: number of bytes to be copied.
42 * @isum: initial sum that is added into the result (32bit unfolded)
43 * @errp: set to -EFAULT for an bad destination address.
44 *
45 * Returns an 32bit unfolded checksum of the buffer.
46 * src and dst are best aligned to 64bits.
47 */
48__wsum
Olivier Deprez157378f2022-04-04 15:47:50 +020049csum_and_copy_to_user(const void *src, void __user *dst, int len)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000050{
Olivier Deprez157378f2022-04-04 15:47:50 +020051 __wsum sum;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000052
53 might_sleep();
Olivier Deprez157378f2022-04-04 15:47:50 +020054 if (!user_access_begin(dst, len))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000055 return 0;
Olivier Deprez157378f2022-04-04 15:47:50 +020056 sum = csum_partial_copy_generic(src, (void __force *)dst, len);
57 user_access_end();
58 return sum;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000059}
Olivier Deprez157378f2022-04-04 15:47:50 +020060EXPORT_SYMBOL(csum_and_copy_to_user);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000061
62/**
63 * csum_partial_copy_nocheck - Copy and checksum.
64 * @src: source address
65 * @dst: destination address
66 * @len: number of bytes to be copied.
67 * @sum: initial sum that is added into the result (32bit unfolded)
68 *
69 * Returns an 32bit unfolded checksum of the buffer.
70 */
71__wsum
Olivier Deprez157378f2022-04-04 15:47:50 +020072csum_partial_copy_nocheck(const void *src, void *dst, int len)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000073{
Olivier Deprez157378f2022-04-04 15:47:50 +020074 return csum_partial_copy_generic(src, dst, len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000075}
76EXPORT_SYMBOL(csum_partial_copy_nocheck);
77
78__sum16 csum_ipv6_magic(const struct in6_addr *saddr,
79 const struct in6_addr *daddr,
80 __u32 len, __u8 proto, __wsum sum)
81{
82 __u64 rest, sum64;
83
84 rest = (__force __u64)htonl(len) + (__force __u64)htons(proto) +
85 (__force __u64)sum;
86
87 asm(" addq (%[saddr]),%[sum]\n"
88 " adcq 8(%[saddr]),%[sum]\n"
89 " adcq (%[daddr]),%[sum]\n"
90 " adcq 8(%[daddr]),%[sum]\n"
91 " adcq $0,%[sum]\n"
92
93 : [sum] "=r" (sum64)
94 : "[sum]" (rest), [saddr] "r" (saddr), [daddr] "r" (daddr));
95
96 return csum_fold(
97 (__force __wsum)add32_with_carry(sum64 & 0xffffffff, sum64>>32));
98}
99EXPORT_SYMBOL(csum_ipv6_magic);