futex.h 3.5 KB

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