futex.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #ifndef _ASM_ALPHA_FUTEX_H
  2. #define _ASM_ALPHA_FUTEX_H
  3. #ifdef __KERNEL__
  4. #include <linux/futex.h>
  5. #include <linux/uaccess.h>
  6. #include <asm/errno.h>
  7. #include <asm/barrier.h>
  8. #define __futex_atomic_op(insn, ret, oldval, uaddr, oparg) \
  9. __asm__ __volatile__( \
  10. __ASM_SMP_MB \
  11. "1: ldl_l %0,0(%2)\n" \
  12. insn \
  13. "2: stl_c %1,0(%2)\n" \
  14. " beq %1,4f\n" \
  15. " mov $31,%1\n" \
  16. "3: .subsection 2\n" \
  17. "4: br 1b\n" \
  18. " .previous\n" \
  19. " .section __ex_table,\"a\"\n" \
  20. " .long 1b-.\n" \
  21. " lda $31,3b-1b(%1)\n" \
  22. " .long 2b-.\n" \
  23. " lda $31,3b-2b(%1)\n" \
  24. " .previous\n" \
  25. : "=&r" (oldval), "=&r"(ret) \
  26. : "r" (uaddr), "r"(oparg) \
  27. : "memory")
  28. static inline int futex_atomic_op_inuser (int encoded_op, u32 __user *uaddr)
  29. {
  30. int op = (encoded_op >> 28) & 7;
  31. int cmp = (encoded_op >> 24) & 15;
  32. int oparg = (encoded_op << 8) >> 20;
  33. int cmparg = (encoded_op << 20) >> 20;
  34. int oldval = 0, ret;
  35. if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
  36. oparg = 1 << oparg;
  37. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
  38. return -EFAULT;
  39. pagefault_disable();
  40. switch (op) {
  41. case FUTEX_OP_SET:
  42. __futex_atomic_op("mov %3,%1\n", ret, oldval, uaddr, oparg);
  43. break;
  44. case FUTEX_OP_ADD:
  45. __futex_atomic_op("addl %0,%3,%1\n", ret, oldval, uaddr, oparg);
  46. break;
  47. case FUTEX_OP_OR:
  48. __futex_atomic_op("or %0,%3,%1\n", ret, oldval, uaddr, oparg);
  49. break;
  50. case FUTEX_OP_ANDN:
  51. __futex_atomic_op("andnot %0,%3,%1\n", ret, oldval, uaddr, oparg);
  52. break;
  53. case FUTEX_OP_XOR:
  54. __futex_atomic_op("xor %0,%3,%1\n", ret, oldval, uaddr, oparg);
  55. break;
  56. default:
  57. ret = -ENOSYS;
  58. }
  59. pagefault_enable();
  60. if (!ret) {
  61. switch (cmp) {
  62. case FUTEX_OP_CMP_EQ: ret = (oldval == cmparg); break;
  63. case FUTEX_OP_CMP_NE: ret = (oldval != cmparg); break;
  64. case FUTEX_OP_CMP_LT: ret = (oldval < cmparg); break;
  65. case FUTEX_OP_CMP_GE: ret = (oldval >= cmparg); break;
  66. case FUTEX_OP_CMP_LE: ret = (oldval <= cmparg); break;
  67. case FUTEX_OP_CMP_GT: ret = (oldval > cmparg); break;
  68. default: ret = -ENOSYS;
  69. }
  70. }
  71. return ret;
  72. }
  73. static inline int
  74. futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
  75. u32 oldval, u32 newval)
  76. {
  77. int ret = 0, cmp;
  78. u32 prev;
  79. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
  80. return -EFAULT;
  81. __asm__ __volatile__ (
  82. __ASM_SMP_MB
  83. "1: ldl_l %1,0(%3)\n"
  84. " cmpeq %1,%4,%2\n"
  85. " beq %2,3f\n"
  86. " mov %5,%2\n"
  87. "2: stl_c %2,0(%3)\n"
  88. " beq %2,4f\n"
  89. "3: .subsection 2\n"
  90. "4: br 1b\n"
  91. " .previous\n"
  92. " .section __ex_table,\"a\"\n"
  93. " .long 1b-.\n"
  94. " lda $31,3b-1b(%0)\n"
  95. " .long 2b-.\n"
  96. " lda $31,3b-2b(%0)\n"
  97. " .previous\n"
  98. : "+r"(ret), "=&r"(prev), "=&r"(cmp)
  99. : "r"(uaddr), "r"((long)(int)oldval), "r"(newval)
  100. : "memory");
  101. *uval = prev;
  102. return ret;
  103. }
  104. #endif /* __KERNEL__ */
  105. #endif /* _ASM_ALPHA_FUTEX_H */