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; |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 38 | 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 Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 42 | int oldval, ret; |
| 43 | u32 tmp; |
| 44 | |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 45 | if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28)) |
| 46 | oparg = 1 << oparg; |
| 47 | |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 48 | if (!access_ok(VERIFY_WRITE, uaddr, sizeof(*uaddr))) |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 49 | return -EFAULT; |
| 50 | |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 51 | _futex_spin_lock_irqsave(uaddr, &flags); |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 52 | pagefault_disable(); |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 53 | |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 54 | ret = -EFAULT; |
| 55 | if (unlikely(get_user(oldval, uaddr) != 0)) |
| 56 | goto out_pagefault_enable; |
| 57 | |
| 58 | ret = 0; |
| 59 | tmp = oldval; |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 60 | |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 61 | switch (op) { |
| 62 | case FUTEX_OP_SET: |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 63 | tmp = oparg; |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 64 | break; |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 65 | case FUTEX_OP_ADD: |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 66 | tmp += oparg; |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 67 | break; |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 68 | case FUTEX_OP_OR: |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 69 | tmp |= oparg; |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 70 | break; |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 71 | case FUTEX_OP_ANDN: |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 72 | tmp &= ~oparg; |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 73 | break; |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 74 | case FUTEX_OP_XOR: |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 75 | tmp ^= oparg; |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 76 | break; |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 77 | default: |
| 78 | ret = -ENOSYS; |
| 79 | } |
| 80 | |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 81 | if (ret == 0 && unlikely(put_user(tmp, uaddr) != 0)) |
| 82 | ret = -EFAULT; |
| 83 | |
| 84 | out_pagefault_enable: |
| 85 | pagefault_enable(); |
John David Anglin | 8b23281 | 2011-10-09 16:40:10 -0400 | [diff] [blame] | 86 | _futex_spin_unlock_irqrestore(uaddr, &flags); |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 87 | |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 88 | if (ret == 0) { |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 89 | 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'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 102 | static inline int |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 103 | futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr, |
| 104 | u32 oldval, u32 newval) |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 105 | { |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 106 | u32 val; |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 107 | unsigned long flags; |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 108 | |
Kyle McMartin | c20a84c | 2008-03-01 10:25:52 -0800 | [diff] [blame] | 109 | /* 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 Viro | db68ce1 | 2017-03-20 21:08:07 -0400 | [diff] [blame] | 112 | if (uaccess_kernel() && !uaddr) |
Kyle McMartin | c20a84c | 2008-03-01 10:25:52 -0800 | [diff] [blame] | 113 | return -EFAULT; |
| 114 | |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 115 | if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32))) |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 116 | return -EFAULT; |
| 117 | |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 118 | /* 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 Anglin | 8b23281 | 2011-10-09 16:40:10 -0400 | [diff] [blame] | 124 | _futex_spin_lock_irqsave(uaddr, &flags); |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 125 | if (unlikely(get_user(val, uaddr) != 0)) { |
| 126 | _futex_spin_unlock_irqrestore(uaddr, &flags); |
| 127 | return -EFAULT; |
| 128 | } |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 129 | |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 130 | if (val == oldval && unlikely(put_user(newval, uaddr) != 0)) { |
| 131 | _futex_spin_unlock_irqrestore(uaddr, &flags); |
| 132 | return -EFAULT; |
| 133 | } |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 134 | |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 135 | *uval = val; |
John David Anglin | 8b23281 | 2011-10-09 16:40:10 -0400 | [diff] [blame] | 136 | _futex_spin_unlock_irqrestore(uaddr, &flags); |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 137 | |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 138 | return 0; |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 139 | } |
| 140 | |
Kyle McMartin | c20a84c | 2008-03-01 10:25:52 -0800 | [diff] [blame] | 141 | #endif /*__KERNEL__*/ |
| 142 | #endif /*_ASM_PARISC_FUTEX_H*/ |