csum-wrappers_64.c 3.8 KB

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