futex.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (C) 2012 ARM Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef __ASM_FUTEX_H
  17. #define __ASM_FUTEX_H
  18. #ifdef __KERNEL__
  19. #include <linux/futex.h>
  20. #include <linux/uaccess.h>
  21. #include <asm/errno.h>
  22. #define __futex_atomic_op(insn, ret, oldval, uaddr, tmp, oparg) \
  23. do { \
  24. uaccess_enable(); \
  25. asm volatile( \
  26. " prfm pstl1strm, %2\n" \
  27. "1: ldxr %w1, %2\n" \
  28. insn "\n" \
  29. "2: stlxr %w3, %w0, %2\n" \
  30. " cbnz %w3, 1b\n" \
  31. " dmb ish\n" \
  32. "3:\n" \
  33. " .pushsection .fixup,\"ax\"\n" \
  34. " .align 2\n" \
  35. "4: mov %w0, %w5\n" \
  36. " b 3b\n" \
  37. " .popsection\n" \
  38. _ASM_EXTABLE(1b, 4b) \
  39. _ASM_EXTABLE(2b, 4b) \
  40. : "=&r" (ret), "=&r" (oldval), "+Q" (*uaddr), "=&r" (tmp) \
  41. : "r" (oparg), "Ir" (-EFAULT) \
  42. : "memory"); \
  43. uaccess_disable(); \
  44. } while (0)
  45. static inline int
  46. arch_futex_atomic_op_inuser(int op, int oparg, int *oval, u32 __user *uaddr)
  47. {
  48. int oldval = 0, ret, tmp;
  49. pagefault_disable();
  50. switch (op) {
  51. case FUTEX_OP_SET:
  52. __futex_atomic_op("mov %w0, %w4",
  53. ret, oldval, uaddr, tmp, oparg);
  54. break;
  55. case FUTEX_OP_ADD:
  56. __futex_atomic_op("add %w0, %w1, %w4",
  57. ret, oldval, uaddr, tmp, oparg);
  58. break;
  59. case FUTEX_OP_OR:
  60. __futex_atomic_op("orr %w0, %w1, %w4",
  61. ret, oldval, uaddr, tmp, oparg);
  62. break;
  63. case FUTEX_OP_ANDN:
  64. __futex_atomic_op("and %w0, %w1, %w4",
  65. ret, oldval, uaddr, tmp, ~oparg);
  66. break;
  67. case FUTEX_OP_XOR:
  68. __futex_atomic_op("eor %w0, %w1, %w4",
  69. ret, oldval, uaddr, tmp, oparg);
  70. break;
  71. default:
  72. ret = -ENOSYS;
  73. }
  74. pagefault_enable();
  75. if (!ret)
  76. *oval = oldval;
  77. return ret;
  78. }
  79. static inline int
  80. futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
  81. u32 oldval, u32 newval)
  82. {
  83. int ret = 0;
  84. u32 val, tmp;
  85. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
  86. return -EFAULT;
  87. uaccess_enable();
  88. asm volatile("// futex_atomic_cmpxchg_inatomic\n"
  89. " prfm pstl1strm, %2\n"
  90. "1: ldxr %w1, %2\n"
  91. " sub %w3, %w1, %w4\n"
  92. " cbnz %w3, 3f\n"
  93. "2: stlxr %w3, %w5, %2\n"
  94. " cbnz %w3, 1b\n"
  95. " dmb ish\n"
  96. "3:\n"
  97. " .pushsection .fixup,\"ax\"\n"
  98. "4: mov %w0, %w6\n"
  99. " b 3b\n"
  100. " .popsection\n"
  101. _ASM_EXTABLE(1b, 4b)
  102. _ASM_EXTABLE(2b, 4b)
  103. : "+r" (ret), "=&r" (val), "+Q" (*uaddr), "=&r" (tmp)
  104. : "r" (oldval), "r" (newval), "Ir" (-EFAULT)
  105. : "memory");
  106. uaccess_disable();
  107. *uval = val;
  108. return ret;
  109. }
  110. #endif /* __KERNEL__ */
  111. #endif /* __ASM_FUTEX_H */