futex.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * Vineetg: August 2010: From Android kernel work
  9. */
  10. #ifndef _ASM_FUTEX_H
  11. #define _ASM_FUTEX_H
  12. #include <linux/futex.h>
  13. #include <linux/preempt.h>
  14. #include <linux/uaccess.h>
  15. #include <asm/errno.h>
  16. #ifdef CONFIG_ARC_HAS_LLSC
  17. #define __futex_atomic_op(insn, ret, oldval, uaddr, oparg)\
  18. \
  19. smp_mb(); \
  20. __asm__ __volatile__( \
  21. "1: llock %1, [%2] \n" \
  22. insn "\n" \
  23. "2: scond %0, [%2] \n" \
  24. " bnz 1b \n" \
  25. " mov %0, 0 \n" \
  26. "3: \n" \
  27. " .section .fixup,\"ax\" \n" \
  28. " .align 4 \n" \
  29. "4: mov %0, %4 \n" \
  30. " j 3b \n" \
  31. " .previous \n" \
  32. " .section __ex_table,\"a\" \n" \
  33. " .align 4 \n" \
  34. " .word 1b, 4b \n" \
  35. " .word 2b, 4b \n" \
  36. " .previous \n" \
  37. \
  38. : "=&r" (ret), "=&r" (oldval) \
  39. : "r" (uaddr), "r" (oparg), "ir" (-EFAULT) \
  40. : "cc", "memory"); \
  41. smp_mb() \
  42. #else /* !CONFIG_ARC_HAS_LLSC */
  43. #define __futex_atomic_op(insn, ret, oldval, uaddr, oparg)\
  44. \
  45. smp_mb(); \
  46. __asm__ __volatile__( \
  47. "1: ld %1, [%2] \n" \
  48. insn "\n" \
  49. "2: st %0, [%2] \n" \
  50. " mov %0, 0 \n" \
  51. "3: \n" \
  52. " .section .fixup,\"ax\" \n" \
  53. " .align 4 \n" \
  54. "4: mov %0, %4 \n" \
  55. " j 3b \n" \
  56. " .previous \n" \
  57. " .section __ex_table,\"a\" \n" \
  58. " .align 4 \n" \
  59. " .word 1b, 4b \n" \
  60. " .word 2b, 4b \n" \
  61. " .previous \n" \
  62. \
  63. : "=&r" (ret), "=&r" (oldval) \
  64. : "r" (uaddr), "r" (oparg), "ir" (-EFAULT) \
  65. : "cc", "memory"); \
  66. smp_mb() \
  67. #endif
  68. static inline int arch_futex_atomic_op_inuser(int op, int oparg, int *oval,
  69. u32 __user *uaddr)
  70. {
  71. int oldval = 0, ret;
  72. #ifndef CONFIG_ARC_HAS_LLSC
  73. preempt_disable(); /* to guarantee atomic r-m-w of futex op */
  74. #endif
  75. pagefault_disable();
  76. switch (op) {
  77. case FUTEX_OP_SET:
  78. __futex_atomic_op("mov %0, %3", ret, oldval, uaddr, oparg);
  79. break;
  80. case FUTEX_OP_ADD:
  81. /* oldval = *uaddr; *uaddr += oparg ; ret = *uaddr */
  82. __futex_atomic_op("add %0, %1, %3", ret, oldval, uaddr, oparg);
  83. break;
  84. case FUTEX_OP_OR:
  85. __futex_atomic_op("or %0, %1, %3", ret, oldval, uaddr, oparg);
  86. break;
  87. case FUTEX_OP_ANDN:
  88. __futex_atomic_op("bic %0, %1, %3", ret, oldval, uaddr, oparg);
  89. break;
  90. case FUTEX_OP_XOR:
  91. __futex_atomic_op("xor %0, %1, %3", ret, oldval, uaddr, oparg);
  92. break;
  93. default:
  94. ret = -ENOSYS;
  95. }
  96. pagefault_enable();
  97. #ifndef CONFIG_ARC_HAS_LLSC
  98. preempt_enable();
  99. #endif
  100. if (!ret)
  101. *oval = oldval;
  102. return ret;
  103. }
  104. /*
  105. * cmpxchg of futex (pagefaults disabled by caller)
  106. * Return 0 for success, -EFAULT otherwise
  107. */
  108. static inline int
  109. futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr, u32 expval,
  110. u32 newval)
  111. {
  112. int ret = 0;
  113. u32 existval;
  114. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
  115. return -EFAULT;
  116. #ifndef CONFIG_ARC_HAS_LLSC
  117. preempt_disable(); /* to guarantee atomic r-m-w of futex op */
  118. #endif
  119. smp_mb();
  120. __asm__ __volatile__(
  121. #ifdef CONFIG_ARC_HAS_LLSC
  122. "1: llock %1, [%4] \n"
  123. " brne %1, %2, 3f \n"
  124. "2: scond %3, [%4] \n"
  125. " bnz 1b \n"
  126. #else
  127. "1: ld %1, [%4] \n"
  128. " brne %1, %2, 3f \n"
  129. "2: st %3, [%4] \n"
  130. #endif
  131. "3: \n"
  132. " .section .fixup,\"ax\" \n"
  133. "4: mov %0, %5 \n"
  134. " j 3b \n"
  135. " .previous \n"
  136. " .section __ex_table,\"a\" \n"
  137. " .align 4 \n"
  138. " .word 1b, 4b \n"
  139. " .word 2b, 4b \n"
  140. " .previous\n"
  141. : "+&r"(ret), "=&r"(existval)
  142. : "r"(expval), "r"(newval), "r"(uaddr), "ir"(-EFAULT)
  143. : "cc", "memory");
  144. smp_mb();
  145. #ifndef CONFIG_ARC_HAS_LLSC
  146. preempt_enable();
  147. #endif
  148. *uval = existval;
  149. return ret;
  150. }
  151. #endif