Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 2 | #ifndef _ASM_PARISC_FUTEX_H |
| 3 | #define _ASM_PARISC_FUTEX_H |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 4 | |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 5 | #include <linux/futex.h> |
Jeff Dike | 730f412 | 2008-04-30 00:54:49 -0700 | [diff] [blame] | 6 | #include <linux/uaccess.h> |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 7 | #include <asm/atomic.h> |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 8 | #include <asm/errno.h> |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 9 | |
John David Anglin | 8b23281 | 2011-10-09 16:40:10 -0400 | [diff] [blame] | 10 | /* The following has to match the LWS code in syscall.S. We have |
| 11 | sixteen four-word locks. */ |
| 12 | |
| 13 | static inline void |
Dave Anglin | 7e99271 | 2021-11-03 12:49:32 +0100 | [diff] [blame] | 14 | _futex_spin_lock(u32 __user *uaddr) |
John David Anglin | 8b23281 | 2011-10-09 16:40:10 -0400 | [diff] [blame] | 15 | { |
| 16 | extern u32 lws_lock_start[]; |
John David Anglin | d3a5a68 | 2021-12-21 13:33:16 -0500 | [diff] [blame] | 17 | long index = ((long)uaddr & 0x7f8) >> 1; |
John David Anglin | 8b23281 | 2011-10-09 16:40:10 -0400 | [diff] [blame] | 18 | arch_spinlock_t *s = (arch_spinlock_t *)&lws_lock_start[index]; |
Dave Anglin | 7e99271 | 2021-11-03 12:49:32 +0100 | [diff] [blame] | 19 | preempt_disable(); |
John David Anglin | 8b23281 | 2011-10-09 16:40:10 -0400 | [diff] [blame] | 20 | arch_spin_lock(s); |
| 21 | } |
| 22 | |
| 23 | static inline void |
Dave Anglin | 7e99271 | 2021-11-03 12:49:32 +0100 | [diff] [blame] | 24 | _futex_spin_unlock(u32 __user *uaddr) |
John David Anglin | 8b23281 | 2011-10-09 16:40:10 -0400 | [diff] [blame] | 25 | { |
| 26 | extern u32 lws_lock_start[]; |
John David Anglin | d3a5a68 | 2021-12-21 13:33:16 -0500 | [diff] [blame] | 27 | long index = ((long)uaddr & 0x7f8) >> 1; |
John David Anglin | 8b23281 | 2011-10-09 16:40:10 -0400 | [diff] [blame] | 28 | arch_spinlock_t *s = (arch_spinlock_t *)&lws_lock_start[index]; |
| 29 | arch_spin_unlock(s); |
Dave Anglin | 7e99271 | 2021-11-03 12:49:32 +0100 | [diff] [blame] | 30 | preempt_enable(); |
John David Anglin | 8b23281 | 2011-10-09 16:40:10 -0400 | [diff] [blame] | 31 | } |
| 32 | |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 33 | static inline int |
Jiri Slaby | 30d6e0a | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 34 | arch_futex_atomic_op_inuser(int op, int oparg, int *oval, u32 __user *uaddr) |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 35 | { |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 36 | int oldval, ret; |
| 37 | u32 tmp; |
| 38 | |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 39 | ret = -EFAULT; |
Dave Anglin | 7e99271 | 2021-11-03 12:49:32 +0100 | [diff] [blame] | 40 | |
| 41 | _futex_spin_lock(uaddr); |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 42 | if (unlikely(get_user(oldval, uaddr) != 0)) |
| 43 | goto out_pagefault_enable; |
| 44 | |
| 45 | ret = 0; |
| 46 | tmp = oldval; |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 47 | |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 48 | switch (op) { |
| 49 | case FUTEX_OP_SET: |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 50 | tmp = oparg; |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 51 | break; |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 52 | case FUTEX_OP_ADD: |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 53 | tmp += oparg; |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 54 | break; |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 55 | case FUTEX_OP_OR: |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 56 | tmp |= oparg; |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 57 | break; |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 58 | case FUTEX_OP_ANDN: |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 59 | tmp &= ~oparg; |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 60 | break; |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 61 | case FUTEX_OP_XOR: |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 62 | tmp ^= oparg; |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 63 | break; |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 64 | default: |
| 65 | ret = -ENOSYS; |
| 66 | } |
| 67 | |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 68 | if (ret == 0 && unlikely(put_user(tmp, uaddr) != 0)) |
| 69 | ret = -EFAULT; |
| 70 | |
| 71 | out_pagefault_enable: |
Dave Anglin | 7e99271 | 2021-11-03 12:49:32 +0100 | [diff] [blame] | 72 | _futex_spin_unlock(uaddr); |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 73 | |
Jiri Slaby | 30d6e0a | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 74 | if (!ret) |
| 75 | *oval = oldval; |
| 76 | |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 77 | return ret; |
| 78 | } |
| 79 | |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 80 | static inline int |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 81 | futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr, |
| 82 | u32 oldval, u32 newval) |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 83 | { |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 84 | u32 val; |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 85 | |
Kyle McMartin | c20a84c | 2008-03-01 10:25:52 -0800 | [diff] [blame] | 86 | /* futex.c wants to do a cmpxchg_inatomic on kernel NULL, which is |
| 87 | * our gateway page, and causes no end of trouble... |
| 88 | */ |
Al Viro | db68ce1 | 2017-03-20 21:08:07 -0400 | [diff] [blame] | 89 | if (uaccess_kernel() && !uaddr) |
Kyle McMartin | c20a84c | 2008-03-01 10:25:52 -0800 | [diff] [blame] | 90 | return -EFAULT; |
| 91 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 92 | if (!access_ok(uaddr, sizeof(u32))) |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 93 | return -EFAULT; |
| 94 | |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 95 | /* HPPA has no cmpxchg in hardware and therefore the |
| 96 | * best we can do here is use an array of locks. The |
| 97 | * lock selected is based on a hash of the userspace |
| 98 | * address. This should scale to a couple of CPUs. |
| 99 | */ |
| 100 | |
Dave Anglin | 7e99271 | 2021-11-03 12:49:32 +0100 | [diff] [blame] | 101 | _futex_spin_lock(uaddr); |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 102 | if (unlikely(get_user(val, uaddr) != 0)) { |
Dave Anglin | 7e99271 | 2021-11-03 12:49:32 +0100 | [diff] [blame] | 103 | _futex_spin_unlock(uaddr); |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 104 | return -EFAULT; |
| 105 | } |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 106 | |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 107 | if (val == oldval && unlikely(put_user(newval, uaddr) != 0)) { |
Dave Anglin | 7e99271 | 2021-11-03 12:49:32 +0100 | [diff] [blame] | 108 | _futex_spin_unlock(uaddr); |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 109 | return -EFAULT; |
| 110 | } |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 111 | |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 112 | *uval = val; |
Dave Anglin | 7e99271 | 2021-11-03 12:49:32 +0100 | [diff] [blame] | 113 | _futex_spin_unlock(uaddr); |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 114 | |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 115 | return 0; |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 116 | } |
| 117 | |
Kyle McMartin | c20a84c | 2008-03-01 10:25:52 -0800 | [diff] [blame] | 118 | #endif /*_ASM_PARISC_FUTEX_H*/ |