csum-wrappers_64.c 2.6 KB

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