futex.h 3.9 KB

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