blob: 2388bdb3283283870016fc03448e8ac569011d57 [file] [log] [blame]
Carlos O'Donell342a0492006-09-07 13:05:17 -04001#ifndef _ASM_PARISC_FUTEX_H
2#define _ASM_PARISC_FUTEX_H
Jakub Jelinek4732efbe2005-09-06 15:16:25 -07003
Carlos O'Donell342a0492006-09-07 13:05:17 -04004#ifdef __KERNEL__
Jakub Jelinek4732efbe2005-09-06 15:16:25 -07005
Carlos O'Donell342a0492006-09-07 13:05:17 -04006#include <linux/futex.h>
Jeff Dike730f4122008-04-30 00:54:49 -07007#include <linux/uaccess.h>
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -04008#include <asm/atomic.h>
Carlos O'Donell342a0492006-09-07 13:05:17 -04009#include <asm/errno.h>
Carlos O'Donell342a0492006-09-07 13:05:17 -040010
11static inline int
Michel Lespinasse8d7718a2011-03-10 18:50:58 -080012futex_atomic_op_inuser (int encoded_op, u32 __user *uaddr)
Carlos O'Donell342a0492006-09-07 13:05:17 -040013{
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040014 unsigned long int flags;
15 u32 val;
Carlos O'Donell342a0492006-09-07 13:05:17 -040016 int op = (encoded_op >> 28) & 7;
17 int cmp = (encoded_op >> 24) & 15;
18 int oparg = (encoded_op << 8) >> 20;
19 int cmparg = (encoded_op << 20) >> 20;
20 int oldval = 0, ret;
21 if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
22 oparg = 1 << oparg;
23
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040024 if (!access_ok(VERIFY_WRITE, uaddr, sizeof(*uaddr)))
Carlos O'Donell342a0492006-09-07 13:05:17 -040025 return -EFAULT;
26
Peter Zijlstraa8663742006-12-06 20:32:20 -080027 pagefault_disable();
Carlos O'Donell342a0492006-09-07 13:05:17 -040028
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040029 _atomic_spin_lock_irqsave(uaddr, flags);
30
Carlos O'Donell342a0492006-09-07 13:05:17 -040031 switch (op) {
32 case FUTEX_OP_SET:
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040033 /* *(int *)UADDR2 = OPARG; */
34 ret = get_user(oldval, uaddr);
35 if (!ret)
36 ret = put_user(oparg, uaddr);
37 break;
Carlos O'Donell342a0492006-09-07 13:05:17 -040038 case FUTEX_OP_ADD:
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040039 /* *(int *)UADDR2 += OPARG; */
40 ret = get_user(oldval, uaddr);
41 if (!ret) {
42 val = oldval + oparg;
43 ret = put_user(val, uaddr);
44 }
45 break;
Carlos O'Donell342a0492006-09-07 13:05:17 -040046 case FUTEX_OP_OR:
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040047 /* *(int *)UADDR2 |= OPARG; */
48 ret = get_user(oldval, uaddr);
49 if (!ret) {
50 val = oldval | oparg;
51 ret = put_user(val, uaddr);
52 }
53 break;
Carlos O'Donell342a0492006-09-07 13:05:17 -040054 case FUTEX_OP_ANDN:
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040055 /* *(int *)UADDR2 &= ~OPARG; */
56 ret = get_user(oldval, uaddr);
57 if (!ret) {
58 val = oldval & ~oparg;
59 ret = put_user(val, uaddr);
60 }
61 break;
Carlos O'Donell342a0492006-09-07 13:05:17 -040062 case FUTEX_OP_XOR:
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040063 /* *(int *)UADDR2 ^= OPARG; */
64 ret = get_user(oldval, uaddr);
65 if (!ret) {
66 val = oldval ^ oparg;
67 ret = put_user(val, uaddr);
68 }
69 break;
Carlos O'Donell342a0492006-09-07 13:05:17 -040070 default:
71 ret = -ENOSYS;
72 }
73
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040074 _atomic_spin_unlock_irqrestore(uaddr, flags);
75
Peter Zijlstraa8663742006-12-06 20:32:20 -080076 pagefault_enable();
Carlos O'Donell342a0492006-09-07 13:05:17 -040077
78 if (!ret) {
79 switch (cmp) {
80 case FUTEX_OP_CMP_EQ: ret = (oldval == cmparg); break;
81 case FUTEX_OP_CMP_NE: ret = (oldval != cmparg); break;
82 case FUTEX_OP_CMP_LT: ret = (oldval < cmparg); break;
83 case FUTEX_OP_CMP_GE: ret = (oldval >= cmparg); break;
84 case FUTEX_OP_CMP_LE: ret = (oldval <= cmparg); break;
85 case FUTEX_OP_CMP_GT: ret = (oldval > cmparg); break;
86 default: ret = -ENOSYS;
87 }
88 }
89 return ret;
90}
91
92/* Non-atomic version */
93static inline int
Michel Lespinasse8d7718a2011-03-10 18:50:58 -080094futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
95 u32 oldval, u32 newval)
Carlos O'Donell342a0492006-09-07 13:05:17 -040096{
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040097 int ret;
Michel Lespinasse8d7718a2011-03-10 18:50:58 -080098 u32 val;
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040099 unsigned long flags;
Carlos O'Donell342a0492006-09-07 13:05:17 -0400100
Kyle McMartinc20a84c2008-03-01 10:25:52 -0800101 /* futex.c wants to do a cmpxchg_inatomic on kernel NULL, which is
102 * our gateway page, and causes no end of trouble...
103 */
104 if (segment_eq(KERNEL_DS, get_fs()) && !uaddr)
105 return -EFAULT;
106
Michel Lespinasse8d7718a2011-03-10 18:50:58 -0800107 if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
Carlos O'Donell342a0492006-09-07 13:05:17 -0400108 return -EFAULT;
109
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -0400110 /* HPPA has no cmpxchg in hardware and therefore the
111 * best we can do here is use an array of locks. The
112 * lock selected is based on a hash of the userspace
113 * address. This should scale to a couple of CPUs.
114 */
115
116 _atomic_spin_lock_irqsave(uaddr, flags);
117
118 ret = get_user(val, uaddr);
119
120 if (!ret && val == oldval)
121 ret = put_user(newval, uaddr);
122
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800123 *uval = val;
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -0400124
125 _atomic_spin_unlock_irqrestore(uaddr, flags);
126
127 return ret;
Carlos O'Donell342a0492006-09-07 13:05:17 -0400128}
129
Kyle McMartinc20a84c2008-03-01 10:25:52 -0800130#endif /*__KERNEL__*/
131#endif /*_ASM_PARISC_FUTEX_H*/