futex.h 4.3 KB

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