futex.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. u32 __user *uaddr = __uaccess_mask_ptr(_uaddr);
  50. pagefault_disable();
  51. switch (op) {
  52. case FUTEX_OP_SET:
  53. __futex_atomic_op("mov %w0, %w4",
  54. ret, oldval, uaddr, tmp, oparg);
  55. break;
  56. case FUTEX_OP_ADD:
  57. __futex_atomic_op("add %w0, %w1, %w4",
  58. ret, oldval, uaddr, tmp, oparg);
  59. break;
  60. case FUTEX_OP_OR:
  61. __futex_atomic_op("orr %w0, %w1, %w4",
  62. ret, oldval, uaddr, tmp, oparg);
  63. break;
  64. case FUTEX_OP_ANDN:
  65. __futex_atomic_op("and %w0, %w1, %w4",
  66. ret, oldval, uaddr, tmp, ~oparg);
  67. break;
  68. case FUTEX_OP_XOR:
  69. __futex_atomic_op("eor %w0, %w1, %w4",
  70. ret, oldval, uaddr, tmp, oparg);
  71. break;
  72. default:
  73. ret = -ENOSYS;
  74. }
  75. pagefault_enable();
  76. if (!ret)
  77. *oval = oldval;
  78. return ret;
  79. }
  80. static inline int
  81. futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *_uaddr,
  82. u32 oldval, u32 newval)
  83. {
  84. int ret = 0;
  85. u32 val, tmp;
  86. u32 __user *uaddr;
  87. if (!access_ok(VERIFY_WRITE, _uaddr, sizeof(u32)))
  88. return -EFAULT;
  89. uaddr = __uaccess_mask_ptr(_uaddr);
  90. uaccess_enable();
  91. asm volatile("// futex_atomic_cmpxchg_inatomic\n"
  92. " prfm pstl1strm, %2\n"
  93. "1: ldxr %w1, %2\n"
  94. " sub %w3, %w1, %w4\n"
  95. " cbnz %w3, 3f\n"
  96. "2: stlxr %w3, %w5, %2\n"
  97. " cbnz %w3, 1b\n"
  98. " dmb ish\n"
  99. "3:\n"
  100. " .pushsection .fixup,\"ax\"\n"
  101. "4: mov %w0, %w6\n"
  102. " b 3b\n"
  103. " .popsection\n"
  104. _ASM_EXTABLE(1b, 4b)
  105. _ASM_EXTABLE(2b, 4b)
  106. : "+r" (ret), "=&r" (val), "+Q" (*uaddr), "=&r" (tmp)
  107. : "r" (oldval), "r" (newval), "Ir" (-EFAULT)
  108. : "memory");
  109. uaccess_disable();
  110. *uval = val;
  111. return ret;
  112. }
  113. #endif /* __KERNEL__ */
  114. #endif /* __ASM_FUTEX_H */