futex.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. arch_futex_atomic_op_inuser(int op, int oparg, int *oval, u32 __user *uaddr)
  30. {
  31. unsigned long int flags;
  32. u32 val;
  33. int oldval = 0, ret;
  34. pagefault_disable();
  35. _futex_spin_lock_irqsave(uaddr, &flags);
  36. switch (op) {
  37. case FUTEX_OP_SET:
  38. /* *(int *)UADDR2 = OPARG; */
  39. ret = get_user(oldval, uaddr);
  40. if (!ret)
  41. ret = put_user(oparg, uaddr);
  42. break;
  43. case FUTEX_OP_ADD:
  44. /* *(int *)UADDR2 += OPARG; */
  45. ret = get_user(oldval, uaddr);
  46. if (!ret) {
  47. val = oldval + oparg;
  48. ret = put_user(val, uaddr);
  49. }
  50. break;
  51. case FUTEX_OP_OR:
  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_ANDN:
  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_XOR:
  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. default:
  76. ret = -ENOSYS;
  77. }
  78. _futex_spin_unlock_irqrestore(uaddr, &flags);
  79. pagefault_enable();
  80. if (!ret)
  81. *oval = oldval;
  82. return ret;
  83. }
  84. /* Non-atomic version */
  85. static inline int
  86. futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
  87. u32 oldval, u32 newval)
  88. {
  89. int ret;
  90. u32 val;
  91. unsigned long flags;
  92. /* futex.c wants to do a cmpxchg_inatomic on kernel NULL, which is
  93. * our gateway page, and causes no end of trouble...
  94. */
  95. if (segment_eq(KERNEL_DS, get_fs()) && !uaddr)
  96. return -EFAULT;
  97. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
  98. return -EFAULT;
  99. /* HPPA has no cmpxchg in hardware and therefore the
  100. * best we can do here is use an array of locks. The
  101. * lock selected is based on a hash of the userspace
  102. * address. This should scale to a couple of CPUs.
  103. */
  104. _futex_spin_lock_irqsave(uaddr, &flags);
  105. ret = get_user(val, uaddr);
  106. if (!ret && val == oldval)
  107. ret = put_user(newval, uaddr);
  108. *uval = val;
  109. _futex_spin_unlock_irqrestore(uaddr, &flags);
  110. return ret;
  111. }
  112. #endif /*__KERNEL__*/
  113. #endif /*_ASM_PARISC_FUTEX_H*/