futex.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #ifndef _ASM_ARM_FUTEX_H
  2. #define _ASM_ARM_FUTEX_H
  3. #ifdef __KERNEL__
  4. #if defined(CONFIG_CPU_USE_DOMAINS) && defined(CONFIG_SMP)
  5. /* ARM doesn't provide unprivileged exclusive memory accessors */
  6. #include <asm-generic/futex.h>
  7. #else
  8. #include <linux/futex.h>
  9. #include <linux/uaccess.h>
  10. #include <asm/errno.h>
  11. #define __futex_atomic_ex_table(err_reg) \
  12. "3:\n" \
  13. " .pushsection __ex_table,\"a\"\n" \
  14. " .align 3\n" \
  15. " .long 1b, 4f, 2b, 4f\n" \
  16. " .popsection\n" \
  17. " .pushsection .fixup,\"ax\"\n" \
  18. "4: mov %0, " err_reg "\n" \
  19. " b 3b\n" \
  20. " .popsection"
  21. #ifdef CONFIG_SMP
  22. #define __futex_atomic_op(insn, ret, oldval, tmp, uaddr, oparg) \
  23. smp_mb(); \
  24. __asm__ __volatile__( \
  25. "1: ldrex %1, [%3]\n" \
  26. " " insn "\n" \
  27. "2: strex %2, %0, [%3]\n" \
  28. " teq %2, #0\n" \
  29. " bne 1b\n" \
  30. " mov %0, #0\n" \
  31. __futex_atomic_ex_table("%5") \
  32. : "=&r" (ret), "=&r" (oldval), "=&r" (tmp) \
  33. : "r" (uaddr), "r" (oparg), "Ir" (-EFAULT) \
  34. : "cc", "memory")
  35. static inline int
  36. futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
  37. u32 oldval, u32 newval)
  38. {
  39. int ret;
  40. u32 val;
  41. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
  42. return -EFAULT;
  43. smp_mb();
  44. __asm__ __volatile__("@futex_atomic_cmpxchg_inatomic\n"
  45. "1: ldrex %1, [%4]\n"
  46. " teq %1, %2\n"
  47. " ite eq @ explicit IT needed for the 2b label\n"
  48. "2: strexeq %0, %3, [%4]\n"
  49. " movne %0, #0\n"
  50. " teq %0, #0\n"
  51. " bne 1b\n"
  52. __futex_atomic_ex_table("%5")
  53. : "=&r" (ret), "=&r" (val)
  54. : "r" (oldval), "r" (newval), "r" (uaddr), "Ir" (-EFAULT)
  55. : "cc", "memory");
  56. smp_mb();
  57. *uval = val;
  58. return ret;
  59. }
  60. #else /* !SMP, we can work around lack of atomic ops by disabling preemption */
  61. #include <linux/preempt.h>
  62. #include <asm/domain.h>
  63. #define __futex_atomic_op(insn, ret, oldval, tmp, uaddr, oparg) \
  64. __asm__ __volatile__( \
  65. "1: " TUSER(ldr) " %1, [%3]\n" \
  66. " " insn "\n" \
  67. "2: " TUSER(str) " %0, [%3]\n" \
  68. " mov %0, #0\n" \
  69. __futex_atomic_ex_table("%5") \
  70. : "=&r" (ret), "=&r" (oldval), "=&r" (tmp) \
  71. : "r" (uaddr), "r" (oparg), "Ir" (-EFAULT) \
  72. : "cc", "memory")
  73. static inline int
  74. futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
  75. u32 oldval, u32 newval)
  76. {
  77. int ret = 0;
  78. u32 val;
  79. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
  80. return -EFAULT;
  81. __asm__ __volatile__("@futex_atomic_cmpxchg_inatomic\n"
  82. "1: " TUSER(ldr) " %1, [%4]\n"
  83. " teq %1, %2\n"
  84. " it eq @ explicit IT needed for the 2b label\n"
  85. "2: " TUSER(streq) " %3, [%4]\n"
  86. __futex_atomic_ex_table("%5")
  87. : "+r" (ret), "=&r" (val)
  88. : "r" (oldval), "r" (newval), "r" (uaddr), "Ir" (-EFAULT)
  89. : "cc", "memory");
  90. *uval = val;
  91. return ret;
  92. }
  93. #endif /* !SMP */
  94. static inline int
  95. futex_atomic_op_inuser (int encoded_op, u32 __user *uaddr)
  96. {
  97. int op = (encoded_op >> 28) & 7;
  98. int cmp = (encoded_op >> 24) & 15;
  99. int oparg = (encoded_op << 8) >> 20;
  100. int cmparg = (encoded_op << 20) >> 20;
  101. int oldval = 0, ret, tmp;
  102. if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
  103. oparg = 1 << oparg;
  104. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
  105. return -EFAULT;
  106. pagefault_disable(); /* implies preempt_disable() */
  107. switch (op) {
  108. case FUTEX_OP_SET:
  109. __futex_atomic_op("mov %0, %4", ret, oldval, tmp, uaddr, oparg);
  110. break;
  111. case FUTEX_OP_ADD:
  112. __futex_atomic_op("add %0, %1, %4", ret, oldval, tmp, uaddr, oparg);
  113. break;
  114. case FUTEX_OP_OR:
  115. __futex_atomic_op("orr %0, %1, %4", ret, oldval, tmp, uaddr, oparg);
  116. break;
  117. case FUTEX_OP_ANDN:
  118. __futex_atomic_op("and %0, %1, %4", ret, oldval, tmp, uaddr, ~oparg);
  119. break;
  120. case FUTEX_OP_XOR:
  121. __futex_atomic_op("eor %0, %1, %4", ret, oldval, tmp, uaddr, oparg);
  122. break;
  123. default:
  124. ret = -ENOSYS;
  125. }
  126. pagefault_enable(); /* subsumes preempt_enable() */
  127. if (!ret) {
  128. switch (cmp) {
  129. case FUTEX_OP_CMP_EQ: ret = (oldval == cmparg); break;
  130. case FUTEX_OP_CMP_NE: ret = (oldval != cmparg); break;
  131. case FUTEX_OP_CMP_LT: ret = (oldval < cmparg); break;
  132. case FUTEX_OP_CMP_GE: ret = (oldval >= cmparg); break;
  133. case FUTEX_OP_CMP_LE: ret = (oldval <= cmparg); break;
  134. case FUTEX_OP_CMP_GT: ret = (oldval > cmparg); break;
  135. default: ret = -ENOSYS;
  136. }
  137. }
  138. return ret;
  139. }
  140. #endif /* !(CPU_USE_DOMAINS && SMP) */
  141. #endif /* __KERNEL__ */
  142. #endif /* _ASM_ARM_FUTEX_H */