blob: 49df14805a9b44bba6857ec4138fe62164e935ab [file] [log] [blame]
Carlos O'Donell342a0492006-09-07 13:05:17 -04001#ifndef _ASM_PARISC_FUTEX_H
2#define _ASM_PARISC_FUTEX_H
Jakub Jelinek4732efb2005-09-06 15:16:25 -07003
Carlos O'Donell342a0492006-09-07 13:05:17 -04004#ifdef __KERNEL__
Jakub Jelinek4732efb2005-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
John David Anglin8b232812011-10-09 16:40:10 -040011/* The following has to match the LWS code in syscall.S. We have
12 sixteen four-word locks. */
13
14static 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
24static 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'Donell342a0492006-09-07 13:05:17 -040034static inline int
Michel Lespinasse8d7718a2011-03-10 18:50:58 -080035futex_atomic_op_inuser (int encoded_op, u32 __user *uaddr)
Carlos O'Donell342a0492006-09-07 13:05:17 -040036{
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040037 unsigned long int flags;
38 u32 val;
Carlos O'Donell342a0492006-09-07 13:05:17 -040039 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'Donelld9ba5fe2011-07-08 17:27:00 -040047 if (!access_ok(VERIFY_WRITE, uaddr, sizeof(*uaddr)))
Carlos O'Donell342a0492006-09-07 13:05:17 -040048 return -EFAULT;
49
Peter Zijlstraa8663742006-12-06 20:32:20 -080050 pagefault_disable();
Carlos O'Donell342a0492006-09-07 13:05:17 -040051
John David Anglin8b232812011-10-09 16:40:10 -040052 _futex_spin_lock_irqsave(uaddr, &flags);
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040053
Carlos O'Donell342a0492006-09-07 13:05:17 -040054 switch (op) {
55 case FUTEX_OP_SET:
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040056 /* *(int *)UADDR2 = OPARG; */
57 ret = get_user(oldval, uaddr);
58 if (!ret)
59 ret = put_user(oparg, uaddr);
60 break;
Carlos O'Donell342a0492006-09-07 13:05:17 -040061 case FUTEX_OP_ADD:
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040062 /* *(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'Donell342a0492006-09-07 13:05:17 -040069 case FUTEX_OP_OR:
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040070 /* *(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'Donell342a0492006-09-07 13:05:17 -040077 case FUTEX_OP_ANDN:
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040078 /* *(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'Donell342a0492006-09-07 13:05:17 -040085 case FUTEX_OP_XOR:
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040086 /* *(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'Donell342a0492006-09-07 13:05:17 -040093 default:
94 ret = -ENOSYS;
95 }
96
John David Anglin8b232812011-10-09 16:40:10 -040097 _futex_spin_unlock_irqrestore(uaddr, &flags);
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040098
Peter Zijlstraa8663742006-12-06 20:32:20 -080099 pagefault_enable();
Carlos O'Donell342a0492006-09-07 13:05:17 -0400100
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 */
116static inline int
Michel Lespinasse8d7718a2011-03-10 18:50:58 -0800117futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
118 u32 oldval, u32 newval)
Carlos O'Donell342a0492006-09-07 13:05:17 -0400119{
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -0400120 int ret;
Michel Lespinasse8d7718a2011-03-10 18:50:58 -0800121 u32 val;
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -0400122 unsigned long flags;
Carlos O'Donell342a0492006-09-07 13:05:17 -0400123
Kyle McMartinc20a84c2008-03-01 10:25:52 -0800124 /* 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 Lespinasse8d7718a2011-03-10 18:50:58 -0800130 if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
Carlos O'Donell342a0492006-09-07 13:05:17 -0400131 return -EFAULT;
132
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -0400133 /* 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 Anglin8b232812011-10-09 16:40:10 -0400139 _futex_spin_lock_irqsave(uaddr, &flags);
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -0400140
141 ret = get_user(val, uaddr);
142
143 if (!ret && val == oldval)
144 ret = put_user(newval, uaddr);
145
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800146 *uval = val;
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -0400147
John David Anglin8b232812011-10-09 16:40:10 -0400148 _futex_spin_unlock_irqrestore(uaddr, &flags);
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -0400149
150 return ret;
Carlos O'Donell342a0492006-09-07 13:05:17 -0400151}
152
Kyle McMartinc20a84c2008-03-01 10:25:52 -0800153#endif /*__KERNEL__*/
154#endif /*_ASM_PARISC_FUTEX_H*/