futex.h 2.5 KB

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