futex.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (C) 2012 ARM Ltd.
  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. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef __ASM_FUTEX_H
  17. #define __ASM_FUTEX_H
  18. #ifdef __KERNEL__
  19. #include <linux/futex.h>
  20. #include <linux/uaccess.h>
  21. #include <asm/alternative.h>
  22. #include <asm/cpufeature.h>
  23. #include <asm/errno.h>
  24. #include <asm/sysreg.h>
  25. #define __futex_atomic_op(insn, ret, oldval, uaddr, tmp, oparg) \
  26. asm volatile( \
  27. ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_HAS_PAN, \
  28. CONFIG_ARM64_PAN) \
  29. " prfm pstl1strm, %2\n" \
  30. "1: ldxr %w1, %2\n" \
  31. insn "\n" \
  32. "2: stlxr %w3, %w0, %2\n" \
  33. " cbnz %w3, 1b\n" \
  34. " dmb ish\n" \
  35. "3:\n" \
  36. " .pushsection .fixup,\"ax\"\n" \
  37. " .align 2\n" \
  38. "4: mov %w0, %w5\n" \
  39. " b 3b\n" \
  40. " .popsection\n" \
  41. _ASM_EXTABLE(1b, 4b) \
  42. _ASM_EXTABLE(2b, 4b) \
  43. ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_HAS_PAN, \
  44. CONFIG_ARM64_PAN) \
  45. : "=&r" (ret), "=&r" (oldval), "+Q" (*uaddr), "=&r" (tmp) \
  46. : "r" (oparg), "Ir" (-EFAULT) \
  47. : "memory")
  48. static inline int
  49. arch_futex_atomic_op_inuser(int op, int oparg, int *oval, u32 __user *uaddr)
  50. {
  51. int oldval = 0, ret, tmp;
  52. pagefault_disable();
  53. switch (op) {
  54. case FUTEX_OP_SET:
  55. __futex_atomic_op("mov %w0, %w4",
  56. ret, oldval, uaddr, tmp, oparg);
  57. break;
  58. case FUTEX_OP_ADD:
  59. __futex_atomic_op("add %w0, %w1, %w4",
  60. ret, oldval, uaddr, tmp, oparg);
  61. break;
  62. case FUTEX_OP_OR:
  63. __futex_atomic_op("orr %w0, %w1, %w4",
  64. ret, oldval, uaddr, tmp, oparg);
  65. break;
  66. case FUTEX_OP_ANDN:
  67. __futex_atomic_op("and %w0, %w1, %w4",
  68. ret, oldval, uaddr, tmp, ~oparg);
  69. break;
  70. case FUTEX_OP_XOR:
  71. __futex_atomic_op("eor %w0, %w1, %w4",
  72. ret, oldval, uaddr, tmp, oparg);
  73. break;
  74. default:
  75. ret = -ENOSYS;
  76. }
  77. pagefault_enable();
  78. if (!ret)
  79. *oval = oldval;
  80. return ret;
  81. }
  82. static inline int
  83. futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *_uaddr,
  84. u32 oldval, u32 newval)
  85. {
  86. int ret = 0;
  87. u32 val, tmp;
  88. u32 __user *uaddr;
  89. if (!access_ok(VERIFY_WRITE, _uaddr, sizeof(u32)))
  90. return -EFAULT;
  91. uaddr = __uaccess_mask_ptr(_uaddr);
  92. asm volatile("// futex_atomic_cmpxchg_inatomic\n"
  93. ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_HAS_PAN, CONFIG_ARM64_PAN)
  94. " prfm pstl1strm, %2\n"
  95. "1: ldxr %w1, %2\n"
  96. " sub %w3, %w1, %w4\n"
  97. " cbnz %w3, 3f\n"
  98. "2: stlxr %w3, %w5, %2\n"
  99. " cbnz %w3, 1b\n"
  100. " dmb ish\n"
  101. "3:\n"
  102. " .pushsection .fixup,\"ax\"\n"
  103. "4: mov %w0, %w6\n"
  104. " b 3b\n"
  105. " .popsection\n"
  106. _ASM_EXTABLE(1b, 4b)
  107. _ASM_EXTABLE(2b, 4b)
  108. ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_HAS_PAN, CONFIG_ARM64_PAN)
  109. : "+r" (ret), "=&r" (val), "+Q" (*uaddr), "=&r" (tmp)
  110. : "r" (oldval), "r" (newval), "Ir" (-EFAULT)
  111. : "memory");
  112. *uval = val;
  113. return ret;
  114. }
  115. #endif /* __KERNEL__ */
  116. #endif /* __ASM_FUTEX_H */