blob: 0ba14300cd8e8bf09b0331ac004c98e601aa3bb2 [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;
Carlos O'Donell342a0492006-09-07 13:05:17 -040038 int op = (encoded_op >> 28) & 7;
39 int cmp = (encoded_op >> 24) & 15;
40 int oparg = (encoded_op << 8) >> 20;
41 int cmparg = (encoded_op << 20) >> 20;
John David Anglin99aed912016-05-21 15:03:54 -040042 int oldval, ret;
43 u32 tmp;
44
Carlos O'Donell342a0492006-09-07 13:05:17 -040045 if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
46 oparg = 1 << oparg;
47
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040048 if (!access_ok(VERIFY_WRITE, uaddr, sizeof(*uaddr)))
Carlos O'Donell342a0492006-09-07 13:05:17 -040049 return -EFAULT;
50
John David Anglin99aed912016-05-21 15:03:54 -040051 _futex_spin_lock_irqsave(uaddr, &flags);
Peter Zijlstraa8663742006-12-06 20:32:20 -080052 pagefault_disable();
Carlos O'Donell342a0492006-09-07 13:05:17 -040053
John David Anglin99aed912016-05-21 15:03:54 -040054 ret = -EFAULT;
55 if (unlikely(get_user(oldval, uaddr) != 0))
56 goto out_pagefault_enable;
57
58 ret = 0;
59 tmp = oldval;
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040060
Carlos O'Donell342a0492006-09-07 13:05:17 -040061 switch (op) {
62 case FUTEX_OP_SET:
John David Anglin99aed912016-05-21 15:03:54 -040063 tmp = oparg;
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040064 break;
Carlos O'Donell342a0492006-09-07 13:05:17 -040065 case FUTEX_OP_ADD:
John David Anglin99aed912016-05-21 15:03:54 -040066 tmp += oparg;
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040067 break;
Carlos O'Donell342a0492006-09-07 13:05:17 -040068 case FUTEX_OP_OR:
John David Anglin99aed912016-05-21 15:03:54 -040069 tmp |= oparg;
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040070 break;
Carlos O'Donell342a0492006-09-07 13:05:17 -040071 case FUTEX_OP_ANDN:
John David Anglin99aed912016-05-21 15:03:54 -040072 tmp &= ~oparg;
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040073 break;
Carlos O'Donell342a0492006-09-07 13:05:17 -040074 case FUTEX_OP_XOR:
John David Anglin99aed912016-05-21 15:03:54 -040075 tmp ^= oparg;
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040076 break;
Carlos O'Donell342a0492006-09-07 13:05:17 -040077 default:
78 ret = -ENOSYS;
79 }
80
John David Anglin99aed912016-05-21 15:03:54 -040081 if (ret == 0 && unlikely(put_user(tmp, uaddr) != 0))
82 ret = -EFAULT;
83
84out_pagefault_enable:
85 pagefault_enable();
John David Anglin8b232812011-10-09 16:40:10 -040086 _futex_spin_unlock_irqrestore(uaddr, &flags);
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040087
John David Anglin99aed912016-05-21 15:03:54 -040088 if (ret == 0) {
Carlos O'Donell342a0492006-09-07 13:05:17 -040089 switch (cmp) {
90 case FUTEX_OP_CMP_EQ: ret = (oldval == cmparg); break;
91 case FUTEX_OP_CMP_NE: ret = (oldval != cmparg); break;
92 case FUTEX_OP_CMP_LT: ret = (oldval < cmparg); break;
93 case FUTEX_OP_CMP_GE: ret = (oldval >= cmparg); break;
94 case FUTEX_OP_CMP_LE: ret = (oldval <= cmparg); break;
95 case FUTEX_OP_CMP_GT: ret = (oldval > cmparg); break;
96 default: ret = -ENOSYS;
97 }
98 }
99 return ret;
100}
101
Carlos O'Donell342a0492006-09-07 13:05:17 -0400102static inline int
Michel Lespinasse8d7718a2011-03-10 18:50:58 -0800103futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
104 u32 oldval, u32 newval)
Carlos O'Donell342a0492006-09-07 13:05:17 -0400105{
Michel Lespinasse8d7718a2011-03-10 18:50:58 -0800106 u32 val;
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -0400107 unsigned long flags;
Carlos O'Donell342a0492006-09-07 13:05:17 -0400108
Kyle McMartinc20a84c2008-03-01 10:25:52 -0800109 /* futex.c wants to do a cmpxchg_inatomic on kernel NULL, which is
110 * our gateway page, and causes no end of trouble...
111 */
Al Virodb68ce12017-03-20 21:08:07 -0400112 if (uaccess_kernel() && !uaddr)
Kyle McMartinc20a84c2008-03-01 10:25:52 -0800113 return -EFAULT;
114
Michel Lespinasse8d7718a2011-03-10 18:50:58 -0800115 if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
Carlos O'Donell342a0492006-09-07 13:05:17 -0400116 return -EFAULT;
117
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -0400118 /* HPPA has no cmpxchg in hardware and therefore the
119 * best we can do here is use an array of locks. The
120 * lock selected is based on a hash of the userspace
121 * address. This should scale to a couple of CPUs.
122 */
123
John David Anglin8b232812011-10-09 16:40:10 -0400124 _futex_spin_lock_irqsave(uaddr, &flags);
John David Anglin99aed912016-05-21 15:03:54 -0400125 if (unlikely(get_user(val, uaddr) != 0)) {
126 _futex_spin_unlock_irqrestore(uaddr, &flags);
127 return -EFAULT;
128 }
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -0400129
John David Anglin99aed912016-05-21 15:03:54 -0400130 if (val == oldval && unlikely(put_user(newval, uaddr) != 0)) {
131 _futex_spin_unlock_irqrestore(uaddr, &flags);
132 return -EFAULT;
133 }
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -0400134
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800135 *uval = val;
John David Anglin8b232812011-10-09 16:40:10 -0400136 _futex_spin_unlock_irqrestore(uaddr, &flags);
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -0400137
John David Anglin99aed912016-05-21 15:03:54 -0400138 return 0;
Carlos O'Donell342a0492006-09-07 13:05:17 -0400139}
140
Kyle McMartinc20a84c2008-03-01 10:25:52 -0800141#endif /*__KERNEL__*/
142#endif /*_ASM_PARISC_FUTEX_H*/