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