futex.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #define __futex_atomic_op(insn, ret, oldval, uaddr, oparg)\
  17. \
  18. __asm__ __volatile__( \
  19. "1: ld %1, [%2] \n" \
  20. insn "\n" \
  21. "2: st %0, [%2] \n" \
  22. " mov %0, 0 \n" \
  23. "3: \n" \
  24. " .section .fixup,\"ax\" \n" \
  25. " .align 4 \n" \
  26. "4: mov %0, %4 \n" \
  27. " b 3b \n" \
  28. " .previous \n" \
  29. " .section __ex_table,\"a\" \n" \
  30. " .align 4 \n" \
  31. " .word 1b, 4b \n" \
  32. " .word 2b, 4b \n" \
  33. " .previous \n" \
  34. \
  35. : "=&r" (ret), "=&r" (oldval) \
  36. : "r" (uaddr), "r" (oparg), "ir" (-EFAULT) \
  37. : "cc", "memory")
  38. static inline int futex_atomic_op_inuser(int encoded_op, u32 __user *uaddr)
  39. {
  40. int op = (encoded_op >> 28) & 7;
  41. int cmp = (encoded_op >> 24) & 15;
  42. int oparg = (encoded_op << 8) >> 20;
  43. int cmparg = (encoded_op << 20) >> 20;
  44. int oldval = 0, ret;
  45. if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
  46. oparg = 1 << oparg;
  47. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(int)))
  48. return -EFAULT;
  49. pagefault_disable();
  50. switch (op) {
  51. case FUTEX_OP_SET:
  52. __futex_atomic_op("mov %0, %3", ret, oldval, uaddr, oparg);
  53. break;
  54. case FUTEX_OP_ADD:
  55. __futex_atomic_op("add %0, %1, %3", ret, oldval, uaddr, oparg);
  56. break;
  57. case FUTEX_OP_OR:
  58. __futex_atomic_op("or %0, %1, %3", ret, oldval, uaddr, oparg);
  59. break;
  60. case FUTEX_OP_ANDN:
  61. __futex_atomic_op("bic %0, %1, %3", ret, oldval, uaddr, oparg);
  62. break;
  63. case FUTEX_OP_XOR:
  64. __futex_atomic_op("xor %0, %1, %3", ret, oldval, uaddr, oparg);
  65. break;
  66. default:
  67. ret = -ENOSYS;
  68. }
  69. pagefault_enable();
  70. if (!ret) {
  71. switch (cmp) {
  72. case FUTEX_OP_CMP_EQ:
  73. ret = (oldval == cmparg);
  74. break;
  75. case FUTEX_OP_CMP_NE:
  76. ret = (oldval != cmparg);
  77. break;
  78. case FUTEX_OP_CMP_LT:
  79. ret = (oldval < cmparg);
  80. break;
  81. case FUTEX_OP_CMP_GE:
  82. ret = (oldval >= cmparg);
  83. break;
  84. case FUTEX_OP_CMP_LE:
  85. ret = (oldval <= cmparg);
  86. break;
  87. case FUTEX_OP_CMP_GT:
  88. ret = (oldval > cmparg);
  89. break;
  90. default:
  91. ret = -ENOSYS;
  92. }
  93. }
  94. return ret;
  95. }
  96. /* Compare-xchg with pagefaults disabled.
  97. * Notes:
  98. * -Best-Effort: Exchg happens only if compare succeeds.
  99. * If compare fails, returns; leaving retry/looping to upper layers
  100. * -successful cmp-xchg: return orig value in @addr (same as cmp val)
  101. * -Compare fails: return orig value in @addr
  102. * -user access r/w fails: return -EFAULT
  103. */
  104. static inline int
  105. futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr, u32 oldval,
  106. u32 newval)
  107. {
  108. u32 val;
  109. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(int)))
  110. return -EFAULT;
  111. pagefault_disable();
  112. /* TBD : can use llock/scond */
  113. __asm__ __volatile__(
  114. "1: ld %0, [%3] \n"
  115. " brne %0, %1, 3f \n"
  116. "2: st %2, [%3] \n"
  117. "3: \n"
  118. " .section .fixup,\"ax\" \n"
  119. "4: mov %0, %4 \n"
  120. " b 3b \n"
  121. " .previous \n"
  122. " .section __ex_table,\"a\" \n"
  123. " .align 4 \n"
  124. " .word 1b, 4b \n"
  125. " .word 2b, 4b \n"
  126. " .previous\n"
  127. : "=&r"(val)
  128. : "r"(oldval), "r"(newval), "r"(uaddr), "ir"(-EFAULT)
  129. : "cc", "memory");
  130. pagefault_enable();
  131. *uval = val;
  132. return val;
  133. }
  134. #endif