futex.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #ifndef _ASM_PARISC_FUTEX_H
  2. #define _ASM_PARISC_FUTEX_H
  3. #ifdef __KERNEL__
  4. #include <linux/futex.h>
  5. #include <linux/uaccess.h>
  6. #include <asm/atomic.h>
  7. #include <asm/errno.h>
  8. /* The following has to match the LWS code in syscall.S. We have
  9. sixteen four-word locks. */
  10. static inline void
  11. _futex_spin_lock_irqsave(u32 __user *uaddr, unsigned long int *flags)
  12. {
  13. extern u32 lws_lock_start[];
  14. long index = ((long)uaddr & 0xf0) >> 2;
  15. arch_spinlock_t *s = (arch_spinlock_t *)&lws_lock_start[index];
  16. local_irq_save(*flags);
  17. arch_spin_lock(s);
  18. }
  19. static inline void
  20. _futex_spin_unlock_irqrestore(u32 __user *uaddr, unsigned long int *flags)
  21. {
  22. extern u32 lws_lock_start[];
  23. long index = ((long)uaddr & 0xf0) >> 2;
  24. arch_spinlock_t *s = (arch_spinlock_t *)&lws_lock_start[index];
  25. arch_spin_unlock(s);
  26. local_irq_restore(*flags);
  27. }
  28. static inline int
  29. futex_atomic_op_inuser (int encoded_op, u32 __user *uaddr)
  30. {
  31. unsigned long int flags;
  32. u32 val;
  33. int op = (encoded_op >> 28) & 7;
  34. int cmp = (encoded_op >> 24) & 15;
  35. int oparg = (encoded_op << 8) >> 20;
  36. int cmparg = (encoded_op << 20) >> 20;
  37. int oldval = 0, ret;
  38. if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
  39. oparg = 1 << oparg;
  40. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(*uaddr)))
  41. return -EFAULT;
  42. pagefault_disable();
  43. _futex_spin_lock_irqsave(uaddr, &flags);
  44. switch (op) {
  45. case FUTEX_OP_SET:
  46. /* *(int *)UADDR2 = OPARG; */
  47. ret = get_user(oldval, uaddr);
  48. if (!ret)
  49. ret = put_user(oparg, uaddr);
  50. break;
  51. case FUTEX_OP_ADD:
  52. /* *(int *)UADDR2 += OPARG; */
  53. ret = get_user(oldval, uaddr);
  54. if (!ret) {
  55. val = oldval + oparg;
  56. ret = put_user(val, uaddr);
  57. }
  58. break;
  59. case FUTEX_OP_OR:
  60. /* *(int *)UADDR2 |= OPARG; */
  61. ret = get_user(oldval, uaddr);
  62. if (!ret) {
  63. val = oldval | oparg;
  64. ret = put_user(val, uaddr);
  65. }
  66. break;
  67. case FUTEX_OP_ANDN:
  68. /* *(int *)UADDR2 &= ~OPARG; */
  69. ret = get_user(oldval, uaddr);
  70. if (!ret) {
  71. val = oldval & ~oparg;
  72. ret = put_user(val, uaddr);
  73. }
  74. break;
  75. case FUTEX_OP_XOR:
  76. /* *(int *)UADDR2 ^= OPARG; */
  77. ret = get_user(oldval, uaddr);
  78. if (!ret) {
  79. val = oldval ^ oparg;
  80. ret = put_user(val, uaddr);
  81. }
  82. break;
  83. default:
  84. ret = -ENOSYS;
  85. }
  86. _futex_spin_unlock_irqrestore(uaddr, &flags);
  87. pagefault_enable();
  88. if (!ret) {
  89. switch (cmp) {
  90. case FUTEX_OP_CMP_EQ: ret = (oldval == cmparg); break;
  91. case FUTEX_OP_CMP_NE: ret = (oldval != cmparg); break;
  92. case FUTEX_OP_CMP_LT: ret = (oldval < cmparg); break;
  93. case FUTEX_OP_CMP_GE: ret = (oldval >= cmparg); break;
  94. case FUTEX_OP_CMP_LE: ret = (oldval <= cmparg); break;
  95. case FUTEX_OP_CMP_GT: ret = (oldval > cmparg); break;
  96. default: ret = -ENOSYS;
  97. }
  98. }
  99. return ret;
  100. }
  101. /* Non-atomic version */
  102. static inline int
  103. futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
  104. u32 oldval, u32 newval)
  105. {
  106. int ret;
  107. u32 val;
  108. unsigned long flags;
  109. /* futex.c wants to do a cmpxchg_inatomic on kernel NULL, which is
  110. * our gateway page, and causes no end of trouble...
  111. */
  112. if (segment_eq(KERNEL_DS, get_fs()) && !uaddr)
  113. return -EFAULT;
  114. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
  115. return -EFAULT;
  116. /* HPPA has no cmpxchg in hardware and therefore the
  117. * best we can do here is use an array of locks. The
  118. * lock selected is based on a hash of the userspace
  119. * address. This should scale to a couple of CPUs.
  120. */
  121. _futex_spin_lock_irqsave(uaddr, &flags);
  122. ret = get_user(val, uaddr);
  123. if (!ret && val == oldval)
  124. ret = put_user(newval, uaddr);
  125. *uval = val;
  126. _futex_spin_unlock_irqrestore(uaddr, &flags);
  127. return ret;
  128. }
  129. #endif /*__KERNEL__*/
  130. #endif /*_ASM_PARISC_FUTEX_H*/