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 |
John David Anglin | d0585d7 | 2022-01-04 21:44:32 +0000 | [diff] [blame] | 11 | * 256 four-word locks. We use bits 20-27 of the futex virtual |
| 12 | * address for the hash index. |
| 13 | */ |
| 14 | |
| 15 | static inline unsigned long _futex_hash_index(unsigned long ua) |
| 16 | { |
| 17 | return (ua >> 2) & 0x3fc; |
| 18 | } |
John David Anglin | 8b23281 | 2011-10-09 16:40:10 -0400 | [diff] [blame] | 19 | |
| 20 | static inline void |
John David Anglin | d0585d7 | 2022-01-04 21:44:32 +0000 | [diff] [blame] | 21 | _futex_spin_lock_irqsave(arch_spinlock_t *s, unsigned long *flags) |
John David Anglin | 8b23281 | 2011-10-09 16:40:10 -0400 | [diff] [blame] | 22 | { |
John David Anglin | d0585d7 | 2022-01-04 21:44:32 +0000 | [diff] [blame] | 23 | local_irq_save(*flags); |
John David Anglin | 8b23281 | 2011-10-09 16:40:10 -0400 | [diff] [blame] | 24 | arch_spin_lock(s); |
| 25 | } |
| 26 | |
| 27 | static inline void |
John David Anglin | d0585d7 | 2022-01-04 21:44:32 +0000 | [diff] [blame] | 28 | _futex_spin_unlock_irqrestore(arch_spinlock_t *s, unsigned long *flags) |
John David Anglin | 8b23281 | 2011-10-09 16:40:10 -0400 | [diff] [blame] | 29 | { |
John David Anglin | 8b23281 | 2011-10-09 16:40:10 -0400 | [diff] [blame] | 30 | arch_spin_unlock(s); |
John David Anglin | d0585d7 | 2022-01-04 21:44:32 +0000 | [diff] [blame] | 31 | local_irq_restore(*flags); |
John David Anglin | 8b23281 | 2011-10-09 16:40:10 -0400 | [diff] [blame] | 32 | } |
| 33 | |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 34 | static inline int |
Jiri Slaby | 30d6e0a | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 35 | 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] | 36 | { |
John David Anglin | d0585d7 | 2022-01-04 21:44:32 +0000 | [diff] [blame] | 37 | extern u32 lws_lock_start[]; |
| 38 | unsigned long ua = (unsigned long)uaddr; |
| 39 | arch_spinlock_t *s; |
| 40 | unsigned long flags; |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 41 | int oldval, ret; |
| 42 | u32 tmp; |
| 43 | |
John David Anglin | d0585d7 | 2022-01-04 21:44:32 +0000 | [diff] [blame] | 44 | s = (arch_spinlock_t *)&lws_lock_start[_futex_hash_index(ua)]; |
| 45 | _futex_spin_lock_irqsave(s, &flags); |
Dave Anglin | 7e99271 | 2021-11-03 12:49:32 +0100 | [diff] [blame] | 46 | |
John David Anglin | d0585d7 | 2022-01-04 21:44:32 +0000 | [diff] [blame] | 47 | /* Return -EFAULT if we encounter a page fault or COW break */ |
| 48 | if (unlikely(get_user(oldval, uaddr) != 0)) { |
| 49 | ret = -EFAULT; |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 50 | goto out_pagefault_enable; |
John David Anglin | d0585d7 | 2022-01-04 21:44:32 +0000 | [diff] [blame] | 51 | } |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 52 | |
| 53 | ret = 0; |
| 54 | tmp = oldval; |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 55 | |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 56 | switch (op) { |
| 57 | case FUTEX_OP_SET: |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 58 | tmp = oparg; |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 59 | break; |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 60 | case FUTEX_OP_ADD: |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 61 | tmp += oparg; |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 62 | break; |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 63 | case FUTEX_OP_OR: |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 64 | tmp |= oparg; |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 65 | break; |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 66 | case FUTEX_OP_ANDN: |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 67 | tmp &= ~oparg; |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 68 | break; |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 69 | case FUTEX_OP_XOR: |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 70 | tmp ^= oparg; |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 71 | break; |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 72 | default: |
| 73 | ret = -ENOSYS; |
John David Anglin | d0585d7 | 2022-01-04 21:44:32 +0000 | [diff] [blame] | 74 | goto out_pagefault_enable; |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 75 | } |
| 76 | |
John David Anglin | d0585d7 | 2022-01-04 21:44:32 +0000 | [diff] [blame] | 77 | if (unlikely(put_user(tmp, uaddr) != 0)) |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 78 | ret = -EFAULT; |
| 79 | |
| 80 | out_pagefault_enable: |
John David Anglin | d0585d7 | 2022-01-04 21:44:32 +0000 | [diff] [blame] | 81 | _futex_spin_unlock_irqrestore(s, &flags); |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 82 | |
Jiri Slaby | 30d6e0a | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 83 | if (!ret) |
| 84 | *oval = oldval; |
| 85 | |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 86 | return ret; |
| 87 | } |
| 88 | |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 89 | static inline int |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 90 | futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr, |
| 91 | u32 oldval, u32 newval) |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 92 | { |
John David Anglin | d0585d7 | 2022-01-04 21:44:32 +0000 | [diff] [blame] | 93 | extern u32 lws_lock_start[]; |
| 94 | unsigned long ua = (unsigned long)uaddr; |
| 95 | arch_spinlock_t *s; |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 96 | u32 val; |
John David Anglin | d0585d7 | 2022-01-04 21:44:32 +0000 | [diff] [blame] | 97 | unsigned long flags; |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 98 | |
Kyle McMartin | c20a84c | 2008-03-01 10:25:52 -0800 | [diff] [blame] | 99 | /* futex.c wants to do a cmpxchg_inatomic on kernel NULL, which is |
| 100 | * our gateway page, and causes no end of trouble... |
| 101 | */ |
Al Viro | db68ce1 | 2017-03-20 21:08:07 -0400 | [diff] [blame] | 102 | if (uaccess_kernel() && !uaddr) |
Kyle McMartin | c20a84c | 2008-03-01 10:25:52 -0800 | [diff] [blame] | 103 | return -EFAULT; |
| 104 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 105 | if (!access_ok(uaddr, sizeof(u32))) |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 106 | return -EFAULT; |
| 107 | |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 108 | /* HPPA has no cmpxchg in hardware and therefore the |
| 109 | * best we can do here is use an array of locks. The |
John David Anglin | d0585d7 | 2022-01-04 21:44:32 +0000 | [diff] [blame] | 110 | * lock selected is based on a hash of the virtual |
| 111 | * address of the futex. This should scale to a couple |
| 112 | * of CPUs. |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 113 | */ |
| 114 | |
John David Anglin | d0585d7 | 2022-01-04 21:44:32 +0000 | [diff] [blame] | 115 | s = (arch_spinlock_t *)&lws_lock_start[_futex_hash_index(ua)]; |
| 116 | _futex_spin_lock_irqsave(s, &flags); |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 117 | if (unlikely(get_user(val, uaddr) != 0)) { |
John David Anglin | d0585d7 | 2022-01-04 21:44:32 +0000 | [diff] [blame] | 118 | _futex_spin_unlock_irqrestore(s, &flags); |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 119 | return -EFAULT; |
| 120 | } |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 121 | |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 122 | if (val == oldval && unlikely(put_user(newval, uaddr) != 0)) { |
John David Anglin | d0585d7 | 2022-01-04 21:44:32 +0000 | [diff] [blame] | 123 | _futex_spin_unlock_irqrestore(s, &flags); |
John David Anglin | 99aed91 | 2016-05-21 15:03:54 -0400 | [diff] [blame] | 124 | return -EFAULT; |
| 125 | } |
Carlos O'Donell | d9ba5fe | 2011-07-08 17:27:00 -0400 | [diff] [blame] | 126 | |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 127 | *uval = val; |
John David Anglin | d0585d7 | 2022-01-04 21:44:32 +0000 | [diff] [blame] | 128 | _futex_spin_unlock_irqrestore(s, &flags); |
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 | return 0; |
Carlos O'Donell | 342a049 | 2006-09-07 13:05:17 -0400 | [diff] [blame] | 131 | } |
| 132 | |
Kyle McMartin | c20a84c | 2008-03-01 10:25:52 -0800 | [diff] [blame] | 133 | #endif /*_ASM_PARISC_FUTEX_H*/ |