futex.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_FUTEX_H
  3. #define _ASM_FUTEX_H
  4. #include <linux/futex.h>
  5. #include <linux/uaccess.h>
  6. #include <asm/errno.h>
  7. #define __futex_atomic_op1(insn, ret, oldval, uaddr, oparg) \
  8. do { \
  9. register unsigned long r8 __asm ("r8") = 0; \
  10. __asm__ __volatile__( \
  11. " mf;; \n" \
  12. "[1:] " insn ";; \n" \
  13. " .xdata4 \"__ex_table\", 1b-., 2f-. \n" \
  14. "[2:]" \
  15. : "+r" (r8), "=r" (oldval) \
  16. : "r" (uaddr), "r" (oparg) \
  17. : "memory"); \
  18. ret = r8; \
  19. } while (0)
  20. #define __futex_atomic_op2(insn, ret, oldval, uaddr, oparg) \
  21. do { \
  22. register unsigned long r8 __asm ("r8") = 0; \
  23. int val, newval; \
  24. do { \
  25. __asm__ __volatile__( \
  26. " mf;; \n" \
  27. "[1:] ld4 %3=[%4];; \n" \
  28. " mov %2=%3 \n" \
  29. insn ";; \n" \
  30. " mov ar.ccv=%2;; \n" \
  31. "[2:] cmpxchg4.acq %1=[%4],%3,ar.ccv;; \n" \
  32. " .xdata4 \"__ex_table\", 1b-., 3f-.\n" \
  33. " .xdata4 \"__ex_table\", 2b-., 3f-.\n" \
  34. "[3:]" \
  35. : "+r" (r8), "=r" (val), "=&r" (oldval), \
  36. "=&r" (newval) \
  37. : "r" (uaddr), "r" (oparg) \
  38. : "memory"); \
  39. if (unlikely (r8)) \
  40. break; \
  41. } while (unlikely (val != oldval)); \
  42. ret = r8; \
  43. } while (0)
  44. static inline int
  45. arch_futex_atomic_op_inuser(int op, int oparg, int *oval, u32 __user *uaddr)
  46. {
  47. int oldval = 0, ret;
  48. pagefault_disable();
  49. switch (op) {
  50. case FUTEX_OP_SET:
  51. __futex_atomic_op1("xchg4 %1=[%2],%3", ret, oldval, uaddr,
  52. oparg);
  53. break;
  54. case FUTEX_OP_ADD:
  55. __futex_atomic_op2("add %3=%3,%5", ret, oldval, uaddr, oparg);
  56. break;
  57. case FUTEX_OP_OR:
  58. __futex_atomic_op2("or %3=%3,%5", ret, oldval, uaddr, oparg);
  59. break;
  60. case FUTEX_OP_ANDN:
  61. __futex_atomic_op2("and %3=%3,%5", ret, oldval, uaddr,
  62. ~oparg);
  63. break;
  64. case FUTEX_OP_XOR:
  65. __futex_atomic_op2("xor %3=%3,%5", ret, oldval, uaddr, oparg);
  66. break;
  67. default:
  68. ret = -ENOSYS;
  69. }
  70. pagefault_enable();
  71. if (!ret)
  72. *oval = oldval;
  73. return ret;
  74. }
  75. static inline int
  76. futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
  77. u32 oldval, u32 newval)
  78. {
  79. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
  80. return -EFAULT;
  81. {
  82. register unsigned long r8 __asm ("r8") = 0;
  83. unsigned long prev;
  84. __asm__ __volatile__(
  85. " mf;; \n"
  86. " mov ar.ccv=%4;; \n"
  87. "[1:] cmpxchg4.acq %1=[%2],%3,ar.ccv \n"
  88. " .xdata4 \"__ex_table\", 1b-., 2f-. \n"
  89. "[2:]"
  90. : "+r" (r8), "=&r" (prev)
  91. : "r" (uaddr), "r" (newval),
  92. "rO" ((long) (unsigned) oldval)
  93. : "memory");
  94. *uval = prev;
  95. return r8;
  96. }
  97. }
  98. #endif /* _ASM_FUTEX_H */