csum-wrappers_64.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Copyright 2002,2003 Andi Kleen, SuSE Labs.
  2. * Subject to the GNU Public License v.2
  3. *
  4. * Wrappers of assembly checksum functions for x86-64.
  5. */
  6. #include <asm/checksum.h>
  7. #include <linux/module.h>
  8. /**
  9. * csum_partial_copy_from_user - Copy and checksum from user space.
  10. * @src: source address (user space)
  11. * @dst: destination address
  12. * @len: number of bytes to be copied.
  13. * @isum: initial sum that is added into the result (32bit unfolded)
  14. * @errp: set to -EFAULT for an bad source address.
  15. *
  16. * Returns an 32bit unfolded checksum of the buffer.
  17. * src and dst are best aligned to 64bits.
  18. */
  19. __wsum
  20. csum_partial_copy_from_user(const void __user *src, void *dst,
  21. int len, __wsum isum, int *errp)
  22. {
  23. might_sleep();
  24. *errp = 0;
  25. if (likely(access_ok(VERIFY_READ,src, len))) {
  26. /* Why 6, not 7? To handle odd addresses aligned we
  27. would need to do considerable complications to fix the
  28. checksum which is defined as an 16bit accumulator. The
  29. fix alignment code is primarily for performance
  30. compatibility with 32bit and that will handle odd
  31. addresses slowly too. */
  32. if (unlikely((unsigned long)src & 6)) {
  33. while (((unsigned long)src & 6) && len >= 2) {
  34. __u16 val16;
  35. *errp = __get_user(val16, (const __u16 __user *)src);
  36. if (*errp)
  37. return isum;
  38. *(__u16 *)dst = val16;
  39. isum = (__force __wsum)add32_with_carry(
  40. (__force unsigned)isum, val16);
  41. src += 2;
  42. dst += 2;
  43. len -= 2;
  44. }
  45. }
  46. isum = csum_partial_copy_generic((__force const void *)src,
  47. dst, len, isum, errp, NULL);
  48. if (likely(*errp == 0))
  49. return isum;
  50. }
  51. *errp = -EFAULT;
  52. memset(dst,0,len);
  53. return isum;
  54. }
  55. EXPORT_SYMBOL(csum_partial_copy_from_user);
  56. /**
  57. * csum_partial_copy_to_user - Copy and checksum to user space.
  58. * @src: source address
  59. * @dst: destination address (user space)
  60. * @len: number of bytes to be copied.
  61. * @isum: initial sum that is added into the result (32bit unfolded)
  62. * @errp: set to -EFAULT for an bad destination address.
  63. *
  64. * Returns an 32bit unfolded checksum of the buffer.
  65. * src and dst are best aligned to 64bits.
  66. */
  67. __wsum
  68. csum_partial_copy_to_user(const void *src, void __user *dst,
  69. int len, __wsum isum, int *errp)
  70. {
  71. might_sleep();
  72. if (unlikely(!access_ok(VERIFY_WRITE, dst, len))) {
  73. *errp = -EFAULT;
  74. return 0;
  75. }
  76. if (unlikely((unsigned long)dst & 6)) {
  77. while (((unsigned long)dst & 6) && len >= 2) {
  78. __u16 val16 = *(__u16 *)src;
  79. isum = (__force __wsum)add32_with_carry(
  80. (__force unsigned)isum, val16);
  81. *errp = __put_user(val16, (__u16 __user *)dst);
  82. if (*errp)
  83. return isum;
  84. src += 2;
  85. dst += 2;
  86. len -= 2;
  87. }
  88. }
  89. *errp = 0;
  90. return csum_partial_copy_generic(src, (void __force *)dst,len,isum,NULL,errp);
  91. }
  92. EXPORT_SYMBOL(csum_partial_copy_to_user);
  93. /**
  94. * csum_partial_copy_nocheck - Copy and checksum.
  95. * @src: source address
  96. * @dst: destination address
  97. * @len: number of bytes to be copied.
  98. * @isum: initial sum that is added into the result (32bit unfolded)
  99. *
  100. * Returns an 32bit unfolded checksum of the buffer.
  101. */
  102. __wsum
  103. csum_partial_copy_nocheck(const void *src, void *dst, int len, __wsum sum)
  104. {
  105. return csum_partial_copy_generic(src,dst,len,sum,NULL,NULL);
  106. }
  107. EXPORT_SYMBOL(csum_partial_copy_nocheck);
  108. __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
  109. const struct in6_addr *daddr,
  110. __u32 len, unsigned short proto, __wsum sum)
  111. {
  112. __u64 rest, sum64;
  113. rest = (__force __u64)htonl(len) + (__force __u64)htons(proto) +
  114. (__force __u64)sum;
  115. asm(" addq (%[saddr]),%[sum]\n"
  116. " adcq 8(%[saddr]),%[sum]\n"
  117. " adcq (%[daddr]),%[sum]\n"
  118. " adcq 8(%[daddr]),%[sum]\n"
  119. " adcq $0,%[sum]\n"
  120. : [sum] "=r" (sum64)
  121. : "[sum]" (rest),[saddr] "r" (saddr), [daddr] "r" (daddr));
  122. return csum_fold((__force __wsum)add32_with_carry(sum64 & 0xffffffff, sum64>>32));
  123. }
  124. EXPORT_SYMBOL(csum_ipv6_magic);