futex.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2005-2017 Andes Technology Corporation
  3. #ifndef __NDS32_FUTEX_H__
  4. #define __NDS32_FUTEX_H__
  5. #include <linux/futex.h>
  6. #include <linux/uaccess.h>
  7. #include <asm/errno.h>
  8. #define __futex_atomic_ex_table(err_reg) \
  9. " .pushsection __ex_table,\"a\"\n" \
  10. " .align 3\n" \
  11. " .long 1b, 4f\n" \
  12. " .long 2b, 4f\n" \
  13. " .popsection\n" \
  14. " .pushsection .fixup,\"ax\"\n" \
  15. "4: move %0, " err_reg "\n" \
  16. " b 3b\n" \
  17. " .popsection"
  18. #define __futex_atomic_op(insn, ret, oldval, tmp, uaddr, oparg) \
  19. smp_mb(); \
  20. asm volatile( \
  21. " movi $ta, #0\n" \
  22. "1: llw %1, [%2+$ta]\n" \
  23. " " insn "\n" \
  24. "2: scw %0, [%2+$ta]\n" \
  25. " beqz %0, 1b\n" \
  26. " movi %0, #0\n" \
  27. "3:\n" \
  28. __futex_atomic_ex_table("%4") \
  29. : "=&r" (ret), "=&r" (oldval) \
  30. : "r" (uaddr), "r" (oparg), "i" (-EFAULT) \
  31. : "cc", "memory")
  32. static inline int
  33. futex_atomic_cmpxchg_inatomic(u32 * uval, u32 __user * uaddr,
  34. u32 oldval, u32 newval)
  35. {
  36. int ret = 0;
  37. u32 val, tmp, flags;
  38. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
  39. return -EFAULT;
  40. smp_mb();
  41. asm volatile (" movi $ta, #0\n"
  42. "1: llw %1, [%6 + $ta]\n"
  43. " sub %3, %1, %4\n"
  44. " cmovz %2, %5, %3\n"
  45. " cmovn %2, %1, %3\n"
  46. "2: scw %2, [%6 + $ta]\n"
  47. " beqz %2, 1b\n"
  48. "3:\n " __futex_atomic_ex_table("%7")
  49. :"+&r"(ret), "=&r"(val), "=&r"(tmp), "=&r"(flags)
  50. :"r"(oldval), "r"(newval), "r"(uaddr), "i"(-EFAULT)
  51. :"$ta", "memory");
  52. smp_mb();
  53. *uval = val;
  54. return ret;
  55. }
  56. static inline int
  57. arch_futex_atomic_op_inuser(int op, int oparg, int *oval, u32 __user *uaddr)
  58. {
  59. int oldval = 0, ret;
  60. pagefault_disable();
  61. switch (op) {
  62. case FUTEX_OP_SET:
  63. __futex_atomic_op("move %0, %3", ret, oldval, tmp, uaddr,
  64. oparg);
  65. break;
  66. case FUTEX_OP_ADD:
  67. __futex_atomic_op("add %0, %1, %3", ret, oldval, tmp, uaddr,
  68. oparg);
  69. break;
  70. case FUTEX_OP_OR:
  71. __futex_atomic_op("or %0, %1, %3", ret, oldval, tmp, uaddr,
  72. oparg);
  73. break;
  74. case FUTEX_OP_ANDN:
  75. __futex_atomic_op("and %0, %1, %3", ret, oldval, tmp, uaddr,
  76. ~oparg);
  77. break;
  78. case FUTEX_OP_XOR:
  79. __futex_atomic_op("xor %0, %1, %3", ret, oldval, tmp, uaddr,
  80. oparg);
  81. break;
  82. default:
  83. ret = -ENOSYS;
  84. }
  85. pagefault_enable();
  86. if (!ret)
  87. *oval = oldval;
  88. return ret;
  89. }
  90. #endif /* __NDS32_FUTEX_H__ */