1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright 2002, 2003 Andi Kleen, SuSE Labs. 4 * 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 /** 13 * csum_and_copy_from_user - Copy and checksum from user space. 14 * @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 24 csum_and_copy_from_user(const void __user *src, void *dst, int len) 25 { 26 __wsum sum; 27 28 might_sleep(); 29 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; 34 } 35 36 /** 37 * csum_and_copy_to_user - Copy and checksum to user space. 38 * @src: source address 39 * @dst: destination address (user space) 40 * @len: number of bytes to be copied. 41 * @isum: initial sum that is added into the result (32bit unfolded) 42 * @errp: set to -EFAULT for an bad destination address. 43 * 44 * Returns an 32bit unfolded checksum of the buffer. 45 * src and dst are best aligned to 64bits. 46 */ 47 __wsum 48 csum_and_copy_to_user(const void *src, void __user *dst, int len) 49 { 50 __wsum sum; 51 52 might_sleep(); 53 if (!user_access_begin(dst, len)) 54 return 0; 55 sum = csum_partial_copy_generic(src, (void __force *)dst, len); 56 user_access_end(); 57 return sum; 58 } 59 60 /** 61 * csum_partial_copy_nocheck - Copy and checksum. 62 * @src: source address 63 * @dst: destination address 64 * @len: number of bytes to be copied. 65 * @sum: initial sum that is added into the result (32bit unfolded) 66 * 67 * Returns an 32bit unfolded checksum of the buffer. 68 */ 69 __wsum 70 csum_partial_copy_nocheck(const void *src, void *dst, int len) 71 { 72 return csum_partial_copy_generic(src, dst, len); 73 } 74 EXPORT_SYMBOL(csum_partial_copy_nocheck); 75 76 __sum16 csum_ipv6_magic(const struct in6_addr *saddr, 77 const struct in6_addr *daddr, 78 __u32 len, __u8 proto, __wsum sum) 79 { 80 __u64 rest, sum64; 81 82 rest = (__force __u64)htonl(len) + (__force __u64)htons(proto) + 83 (__force __u64)sum; 84 85 asm(" addq (%[saddr]),%[sum]\n" 86 " adcq 8(%[saddr]),%[sum]\n" 87 " adcq (%[daddr]),%[sum]\n" 88 " adcq 8(%[daddr]),%[sum]\n" 89 " adcq $0,%[sum]\n" 90 91 : [sum] "=r" (sum64) 92 : "[sum]" (rest), [saddr] "r" (saddr), [daddr] "r" (daddr)); 93 94 return csum_fold( 95 (__force __wsum)add32_with_carry(sum64 & 0xffffffff, sum64>>32)); 96 } 97 EXPORT_SYMBOL(csum_ipv6_magic); 98