csum-wrappers_64.c 3.9 KB

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