memcpy_user_64.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright 2011 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. *
  14. * Do memcpy(), but trap and return "n" when a load or store faults.
  15. *
  16. * Note: this idiom only works when memcpy() compiles to a leaf function.
  17. * If "sp" is updated during memcpy, the "jrp lr" will be incorrect.
  18. *
  19. * Also note that we are capturing "n" from the containing scope here.
  20. */
  21. #define _ST(p, inst, v) \
  22. ({ \
  23. asm("1: " #inst " %0, %1;" \
  24. ".pushsection .coldtext.memcpy,\"ax\";" \
  25. "2: { move r0, %2; jrp lr };" \
  26. ".section __ex_table,\"a\";" \
  27. ".quad 1b, 2b;" \
  28. ".popsection" \
  29. : "=m" (*(p)) : "r" (v), "r" (n)); \
  30. })
  31. #define _LD(p, inst) \
  32. ({ \
  33. unsigned long __v; \
  34. asm("1: " #inst " %0, %1;" \
  35. ".pushsection .coldtext.memcpy,\"ax\";" \
  36. "2: { move r0, %2; jrp lr };" \
  37. ".section __ex_table,\"a\";" \
  38. ".quad 1b, 2b;" \
  39. ".popsection" \
  40. : "=r" (__v) : "m" (*(p)), "r" (n)); \
  41. __v; \
  42. })
  43. #define USERCOPY_FUNC __copy_to_user_inatomic
  44. #define ST1(p, v) _ST((p), st1, (v))
  45. #define ST2(p, v) _ST((p), st2, (v))
  46. #define ST4(p, v) _ST((p), st4, (v))
  47. #define ST8(p, v) _ST((p), st, (v))
  48. #define LD1 LD
  49. #define LD2 LD
  50. #define LD4 LD
  51. #define LD8 LD
  52. #include "memcpy_64.c"
  53. #define USERCOPY_FUNC __copy_from_user_inatomic
  54. #define ST1 ST
  55. #define ST2 ST
  56. #define ST4 ST
  57. #define ST8 ST
  58. #define LD1(p) _LD((p), ld1u)
  59. #define LD2(p) _LD((p), ld2u)
  60. #define LD4(p) _LD((p), ld4u)
  61. #define LD8(p) _LD((p), ld)
  62. #include "memcpy_64.c"
  63. #define USERCOPY_FUNC __copy_in_user_inatomic
  64. #define ST1(p, v) _ST((p), st1, (v))
  65. #define ST2(p, v) _ST((p), st2, (v))
  66. #define ST4(p, v) _ST((p), st4, (v))
  67. #define ST8(p, v) _ST((p), st, (v))
  68. #define LD1(p) _LD((p), ld1u)
  69. #define LD2(p) _LD((p), ld2u)
  70. #define LD4(p) _LD((p), ld4u)
  71. #define LD8(p) _LD((p), ld)
  72. #include "memcpy_64.c"
  73. unsigned long __copy_from_user_zeroing(void *to, const void __user *from,
  74. unsigned long n)
  75. {
  76. unsigned long rc = __copy_from_user_inatomic(to, from, n);
  77. if (unlikely(rc))
  78. memset(to + n - rc, 0, rc);
  79. return rc;
  80. }