memcpy_user_64.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. * Here leaf function not only means it does not have calls, but also
  18. * requires no stack operations (sp, stack frame pointer) and no
  19. * use of callee-saved registers, else "jrp lr" will be incorrect since
  20. * unwinding stack frame is bypassed. Since memcpy() is not complex so
  21. * these conditions are satisfied here, but we need to be careful when
  22. * modifying this file. This is not a clean solution but is the best
  23. * one so far.
  24. *
  25. * Also note that we are capturing "n" from the containing scope here.
  26. */
  27. #define _ST(p, inst, v) \
  28. ({ \
  29. asm("1: " #inst " %0, %1;" \
  30. ".pushsection .coldtext,\"ax\";" \
  31. "2: { move r0, %2; jrp lr };" \
  32. ".section __ex_table,\"a\";" \
  33. ".align 8;" \
  34. ".quad 1b, 2b;" \
  35. ".popsection" \
  36. : "=m" (*(p)) : "r" (v), "r" (n)); \
  37. })
  38. #define _LD(p, inst) \
  39. ({ \
  40. unsigned long __v; \
  41. asm("1: " #inst " %0, %1;" \
  42. ".pushsection .coldtext,\"ax\";" \
  43. "2: { move r0, %2; jrp lr };" \
  44. ".section __ex_table,\"a\";" \
  45. ".align 8;" \
  46. ".quad 1b, 2b;" \
  47. ".popsection" \
  48. : "=r" (__v) : "m" (*(p)), "r" (n)); \
  49. __v; \
  50. })
  51. #define USERCOPY_FUNC __copy_to_user_inatomic
  52. #define ST1(p, v) _ST((p), st1, (v))
  53. #define ST2(p, v) _ST((p), st2, (v))
  54. #define ST4(p, v) _ST((p), st4, (v))
  55. #define ST8(p, v) _ST((p), st, (v))
  56. #define LD1 LD
  57. #define LD2 LD
  58. #define LD4 LD
  59. #define LD8 LD
  60. #include "memcpy_64.c"
  61. #define USERCOPY_FUNC __copy_from_user_inatomic
  62. #define ST1 ST
  63. #define ST2 ST
  64. #define ST4 ST
  65. #define ST8 ST
  66. #define LD1(p) _LD((p), ld1u)
  67. #define LD2(p) _LD((p), ld2u)
  68. #define LD4(p) _LD((p), ld4u)
  69. #define LD8(p) _LD((p), ld)
  70. #include "memcpy_64.c"
  71. #define USERCOPY_FUNC __copy_in_user_inatomic
  72. #define ST1(p, v) _ST((p), st1, (v))
  73. #define ST2(p, v) _ST((p), st2, (v))
  74. #define ST4(p, v) _ST((p), st4, (v))
  75. #define ST8(p, v) _ST((p), st, (v))
  76. #define LD1(p) _LD((p), ld1u)
  77. #define LD2(p) _LD((p), ld2u)
  78. #define LD4(p) _LD((p), ld4u)
  79. #define LD8(p) _LD((p), ld)
  80. #include "memcpy_64.c"
  81. unsigned long __copy_from_user_zeroing(void *to, const void __user *from,
  82. unsigned long n)
  83. {
  84. unsigned long rc = __copy_from_user_inatomic(to, from, n);
  85. if (unlikely(rc))
  86. memset(to + n - rc, 0, rc);
  87. return rc;
  88. }