blob: d2c3e410685188d24d54263f08c3c7c35c154053 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Carlos O'Donell342a0492006-09-07 13:05:17 -04002#ifndef _ASM_PARISC_FUTEX_H
3#define _ASM_PARISC_FUTEX_H
Jakub Jelinek4732efbe2005-09-06 15:16:25 -07004
Carlos O'Donell342a0492006-09-07 13:05:17 -04005#ifdef __KERNEL__
Jakub Jelinek4732efbe2005-09-06 15:16:25 -07006
Carlos O'Donell342a0492006-09-07 13:05:17 -04007#include <linux/futex.h>
Jeff Dike730f4122008-04-30 00:54:49 -07008#include <linux/uaccess.h>
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -04009#include <asm/atomic.h>
Carlos O'Donell342a0492006-09-07 13:05:17 -040010#include <asm/errno.h>
Carlos O'Donell342a0492006-09-07 13:05:17 -040011
John David Anglin8b232812011-10-09 16:40:10 -040012/* The following has to match the LWS code in syscall.S. We have
13 sixteen four-word locks. */
14
15static inline void
16_futex_spin_lock_irqsave(u32 __user *uaddr, unsigned long int *flags)
17{
18 extern u32 lws_lock_start[];
19 long index = ((long)uaddr & 0xf0) >> 2;
20 arch_spinlock_t *s = (arch_spinlock_t *)&lws_lock_start[index];
21 local_irq_save(*flags);
22 arch_spin_lock(s);
23}
24
25static inline void
26_futex_spin_unlock_irqrestore(u32 __user *uaddr, unsigned long int *flags)
27{
28 extern u32 lws_lock_start[];
29 long index = ((long)uaddr & 0xf0) >> 2;
30 arch_spinlock_t *s = (arch_spinlock_t *)&lws_lock_start[index];
31 arch_spin_unlock(s);
32 local_irq_restore(*flags);
33}
34
Carlos O'Donell342a0492006-09-07 13:05:17 -040035static inline int
Jiri Slaby30d6e0a2017-08-24 09:31:05 +020036arch_futex_atomic_op_inuser(int op, int oparg, int *oval, u32 __user *uaddr)
Carlos O'Donell342a0492006-09-07 13:05:17 -040037{
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040038 unsigned long int flags;
John David Anglin99aed912016-05-21 15:03:54 -040039 int oldval, ret;
40 u32 tmp;
41
John David Anglin99aed912016-05-21 15:03:54 -040042 _futex_spin_lock_irqsave(uaddr, &flags);
Peter Zijlstraa8663742006-12-06 20:32:20 -080043 pagefault_disable();
Carlos O'Donell342a0492006-09-07 13:05:17 -040044
John David Anglin99aed912016-05-21 15:03:54 -040045 ret = -EFAULT;
46 if (unlikely(get_user(oldval, uaddr) != 0))
47 goto out_pagefault_enable;
48
49 ret = 0;
50 tmp = oldval;
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040051
Carlos O'Donell342a0492006-09-07 13:05:17 -040052 switch (op) {
53 case FUTEX_OP_SET:
John David Anglin99aed912016-05-21 15:03:54 -040054 tmp = oparg;
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040055 break;
Carlos O'Donell342a0492006-09-07 13:05:17 -040056 case FUTEX_OP_ADD:
John David Anglin99aed912016-05-21 15:03:54 -040057 tmp += oparg;
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040058 break;
Carlos O'Donell342a0492006-09-07 13:05:17 -040059 case FUTEX_OP_OR:
John David Anglin99aed912016-05-21 15:03:54 -040060 tmp |= oparg;
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040061 break;
Carlos O'Donell342a0492006-09-07 13:05:17 -040062 case FUTEX_OP_ANDN:
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_XOR:
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 default:
69 ret = -ENOSYS;
70 }
71
John David Anglin99aed912016-05-21 15:03:54 -040072 if (ret == 0 && unlikely(put_user(tmp, uaddr) != 0))
73 ret = -EFAULT;
74
75out_pagefault_enable:
76 pagefault_enable();
John David Anglin8b232812011-10-09 16:40:10 -040077 _futex_spin_unlock_irqrestore(uaddr, &flags);
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040078
Jiri Slaby30d6e0a2017-08-24 09:31:05 +020079 if (!ret)
80 *oval = oldval;
81
Carlos O'Donell342a0492006-09-07 13:05:17 -040082 return ret;
83}
84
Carlos O'Donell342a0492006-09-07 13:05:17 -040085static inline int
Michel Lespinasse8d7718a2011-03-10 18:50:58 -080086futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
87 u32 oldval, u32 newval)
Carlos O'Donell342a0492006-09-07 13:05:17 -040088{
Michel Lespinasse8d7718a2011-03-10 18:50:58 -080089 u32 val;
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -040090 unsigned long flags;
Carlos O'Donell342a0492006-09-07 13:05:17 -040091
Kyle McMartinc20a84c2008-03-01 10:25:52 -080092 /* futex.c wants to do a cmpxchg_inatomic on kernel NULL, which is
93 * our gateway page, and causes no end of trouble...
94 */
Al Virodb68ce12017-03-20 21:08:07 -040095 if (uaccess_kernel() && !uaddr)
Kyle McMartinc20a84c2008-03-01 10:25:52 -080096 return -EFAULT;
97
Linus Torvalds96d4f262019-01-03 18:57:57 -080098 if (!access_ok(uaddr, sizeof(u32)))
Carlos O'Donell342a0492006-09-07 13:05:17 -040099 return -EFAULT;
100
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -0400101 /* HPPA has no cmpxchg in hardware and therefore the
102 * best we can do here is use an array of locks. The
103 * lock selected is based on a hash of the userspace
104 * address. This should scale to a couple of CPUs.
105 */
106
John David Anglin8b232812011-10-09 16:40:10 -0400107 _futex_spin_lock_irqsave(uaddr, &flags);
John David Anglin99aed912016-05-21 15:03:54 -0400108 if (unlikely(get_user(val, uaddr) != 0)) {
109 _futex_spin_unlock_irqrestore(uaddr, &flags);
110 return -EFAULT;
111 }
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -0400112
John David Anglin99aed912016-05-21 15:03:54 -0400113 if (val == oldval && unlikely(put_user(newval, uaddr) != 0)) {
114 _futex_spin_unlock_irqrestore(uaddr, &flags);
115 return -EFAULT;
116 }
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -0400117
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800118 *uval = val;
John David Anglin8b232812011-10-09 16:40:10 -0400119 _futex_spin_unlock_irqrestore(uaddr, &flags);
Carlos O'Donelld9ba5fe2011-07-08 17:27:00 -0400120
John David Anglin99aed912016-05-21 15:03:54 -0400121 return 0;
Carlos O'Donell342a0492006-09-07 13:05:17 -0400122}
123
Kyle McMartinc20a84c2008-03-01 10:25:52 -0800124#endif /*__KERNEL__*/
125#endif /*_ASM_PARISC_FUTEX_H*/