futex.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. int oldval, ret;
  33. u32 tmp;
  34. _futex_spin_lock_irqsave(uaddr, &flags);
  35. pagefault_disable();
  36. ret = -EFAULT;
  37. if (unlikely(get_user(oldval, uaddr) != 0))
  38. goto out_pagefault_enable;
  39. ret = 0;
  40. tmp = oldval;
  41. switch (op) {
  42. case FUTEX_OP_SET:
  43. tmp = oparg;
  44. break;
  45. case FUTEX_OP_ADD:
  46. tmp += oparg;
  47. break;
  48. case FUTEX_OP_OR:
  49. tmp |= oparg;
  50. break;
  51. case FUTEX_OP_ANDN:
  52. tmp &= ~oparg;
  53. break;
  54. case FUTEX_OP_XOR:
  55. tmp ^= oparg;
  56. break;
  57. default:
  58. ret = -ENOSYS;
  59. }
  60. if (ret == 0 && unlikely(put_user(tmp, uaddr) != 0))
  61. ret = -EFAULT;
  62. out_pagefault_enable:
  63. pagefault_enable();
  64. _futex_spin_unlock_irqrestore(uaddr, &flags);
  65. if (!ret)
  66. *oval = oldval;
  67. return ret;
  68. }
  69. static inline int
  70. futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
  71. u32 oldval, u32 newval)
  72. {
  73. u32 val;
  74. unsigned long flags;
  75. /* futex.c wants to do a cmpxchg_inatomic on kernel NULL, which is
  76. * our gateway page, and causes no end of trouble...
  77. */
  78. if (segment_eq(KERNEL_DS, get_fs()) && !uaddr)
  79. return -EFAULT;
  80. if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
  81. return -EFAULT;
  82. /* HPPA has no cmpxchg in hardware and therefore the
  83. * best we can do here is use an array of locks. The
  84. * lock selected is based on a hash of the userspace
  85. * address. This should scale to a couple of CPUs.
  86. */
  87. _futex_spin_lock_irqsave(uaddr, &flags);
  88. if (unlikely(get_user(val, uaddr) != 0)) {
  89. _futex_spin_unlock_irqrestore(uaddr, &flags);
  90. return -EFAULT;
  91. }
  92. if (val == oldval && unlikely(put_user(newval, uaddr) != 0)) {
  93. _futex_spin_unlock_irqrestore(uaddr, &flags);
  94. return -EFAULT;
  95. }
  96. *uval = val;
  97. _futex_spin_unlock_irqrestore(uaddr, &flags);
  98. return 0;
  99. }
  100. #endif /*__KERNEL__*/
  101. #endif /*_ASM_PARISC_FUTEX_H*/