futex.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/alternative.h>
  22. #include <asm/cpufeature.h>
  23. #include <asm/errno.h>
  24. #include <asm/sysreg.h>
  25. #define __futex_atomic_op(insn, ret, oldval, uaddr, tmp, oparg) \
  26. asm volatile( \
  27. ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_HAS_PAN, \
  28. CONFIG_ARM64_PAN) \
  29. " prfm pstl1strm, %2\n" \
  30. "1: ldxr %w1, %2\n" \
  31. insn "\n" \
  32. "2: stlxr %w3, %w0, %2\n" \
  33. " cbnz %w3, 1b\n" \
  34. " dmb ish\n" \
  35. "3:\n" \
  36. " .pushsection .fixup,\"ax\"\n" \
  37. " .align 2\n" \
  38. "4: mov %w0, %w5\n" \
  39. " b 3b\n" \
  40. " .popsection\n" \
  41. " .pushsection __ex_table,\"a\"\n" \
  42. " .align 3\n" \
  43. " .quad 1b, 4b, 2b, 4b\n" \
  44. " .popsection\n" \
  45. ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_HAS_PAN, \
  46. CONFIG_ARM64_PAN) \
  47. : "=&r" (ret), "=&r" (oldval), "+Q" (*uaddr), "=&r" (tmp) \
  48. : "r" (oparg), "Ir" (-EFAULT) \
  49. : "memory")
  50. static inline int
  51. futex_atomic_op_inuser (int encoded_op, u32 __user *uaddr)
  52. {
  53. int op = (encoded_op >> 28) & 7;
  54. int cmp = (encoded_op >> 24) & 15;
  55. int oparg = (encoded_op << 8) >> 20;
  56. int cmparg = (encoded_op << 20) >> 20;
  57. int oldval = 0, ret, tmp;
  58. if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
  59. oparg = 1 << oparg;
  60. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
  61. return -EFAULT;
  62. pagefault_disable();
  63. switch (op) {
  64. case FUTEX_OP_SET:
  65. __futex_atomic_op("mov %w0, %w4",
  66. ret, oldval, uaddr, tmp, oparg);
  67. break;
  68. case FUTEX_OP_ADD:
  69. __futex_atomic_op("add %w0, %w1, %w4",
  70. ret, oldval, uaddr, tmp, oparg);
  71. break;
  72. case FUTEX_OP_OR:
  73. __futex_atomic_op("orr %w0, %w1, %w4",
  74. ret, oldval, uaddr, tmp, oparg);
  75. break;
  76. case FUTEX_OP_ANDN:
  77. __futex_atomic_op("and %w0, %w1, %w4",
  78. ret, oldval, uaddr, tmp, ~oparg);
  79. break;
  80. case FUTEX_OP_XOR:
  81. __futex_atomic_op("eor %w0, %w1, %w4",
  82. ret, oldval, uaddr, tmp, oparg);
  83. break;
  84. default:
  85. ret = -ENOSYS;
  86. }
  87. pagefault_enable();
  88. if (!ret) {
  89. switch (cmp) {
  90. case FUTEX_OP_CMP_EQ: ret = (oldval == cmparg); break;
  91. case FUTEX_OP_CMP_NE: ret = (oldval != cmparg); break;
  92. case FUTEX_OP_CMP_LT: ret = (oldval < cmparg); break;
  93. case FUTEX_OP_CMP_GE: ret = (oldval >= cmparg); break;
  94. case FUTEX_OP_CMP_LE: ret = (oldval <= cmparg); break;
  95. case FUTEX_OP_CMP_GT: ret = (oldval > cmparg); break;
  96. default: ret = -ENOSYS;
  97. }
  98. }
  99. return ret;
  100. }
  101. static inline int
  102. futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
  103. u32 oldval, u32 newval)
  104. {
  105. int ret = 0;
  106. u32 val, tmp;
  107. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
  108. return -EFAULT;
  109. asm volatile("// futex_atomic_cmpxchg_inatomic\n"
  110. " prfm pstl1strm, %2\n"
  111. "1: ldxr %w1, %2\n"
  112. " sub %w3, %w1, %w4\n"
  113. " cbnz %w3, 3f\n"
  114. "2: stlxr %w3, %w5, %2\n"
  115. " cbnz %w3, 1b\n"
  116. " dmb ish\n"
  117. "3:\n"
  118. " .pushsection .fixup,\"ax\"\n"
  119. "4: mov %w0, %w6\n"
  120. " b 3b\n"
  121. " .popsection\n"
  122. " .pushsection __ex_table,\"a\"\n"
  123. " .align 3\n"
  124. " .quad 1b, 4b, 2b, 4b\n"
  125. " .popsection\n"
  126. : "+r" (ret), "=&r" (val), "+Q" (*uaddr), "=&r" (tmp)
  127. : "r" (oldval), "r" (newval), "Ir" (-EFAULT)
  128. : "memory");
  129. *uval = val;
  130. return ret;
  131. }
  132. #endif /* __KERNEL__ */
  133. #endif /* __ASM_FUTEX_H */