Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Sam Ravnborg | f5e706a | 2008-07-17 21:55:51 -0700 | [diff] [blame] | 2 | #ifndef _SPARC64_FUTEX_H |
| 3 | #define _SPARC64_FUTEX_H |
| 4 | |
| 5 | #include <linux/futex.h> |
| 6 | #include <linux/uaccess.h> |
| 7 | #include <asm/errno.h> |
Sam Ravnborg | f5e706a | 2008-07-17 21:55:51 -0700 | [diff] [blame] | 8 | |
| 9 | #define __futex_cas_op(insn, ret, oldval, uaddr, oparg) \ |
| 10 | __asm__ __volatile__( \ |
| 11 | "\n1: lduwa [%3] %%asi, %2\n" \ |
| 12 | " " insn "\n" \ |
| 13 | "2: casa [%3] %%asi, %2, %1\n" \ |
| 14 | " cmp %2, %1\n" \ |
| 15 | " bne,pn %%icc, 1b\n" \ |
| 16 | " mov 0, %0\n" \ |
| 17 | "3:\n" \ |
| 18 | " .section .fixup,#alloc,#execinstr\n" \ |
| 19 | " .align 4\n" \ |
| 20 | "4: sethi %%hi(3b), %0\n" \ |
| 21 | " jmpl %0 + %%lo(3b), %%g0\n" \ |
| 22 | " mov %5, %0\n" \ |
| 23 | " .previous\n" \ |
| 24 | " .section __ex_table,\"a\"\n" \ |
| 25 | " .align 4\n" \ |
| 26 | " .word 1b, 4b\n" \ |
| 27 | " .word 2b, 4b\n" \ |
| 28 | " .previous\n" \ |
| 29 | : "=&r" (ret), "=&r" (oldval), "=&r" (tem) \ |
| 30 | : "r" (uaddr), "r" (oparg), "i" (-EFAULT) \ |
| 31 | : "memory") |
| 32 | |
Jiri Slaby | 30d6e0a | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 33 | static inline int arch_futex_atomic_op_inuser(int op, int oparg, int *oval, |
| 34 | u32 __user *uaddr) |
Sam Ravnborg | f5e706a | 2008-07-17 21:55:51 -0700 | [diff] [blame] | 35 | { |
Sam Ravnborg | f5e706a | 2008-07-17 21:55:51 -0700 | [diff] [blame] | 36 | int oldval = 0, ret, tem; |
| 37 | |
Sam Ravnborg | f5e706a | 2008-07-17 21:55:51 -0700 | [diff] [blame] | 38 | if (unlikely((((unsigned long) uaddr) & 0x3UL))) |
| 39 | return -EINVAL; |
| 40 | |
Sam Ravnborg | f5e706a | 2008-07-17 21:55:51 -0700 | [diff] [blame] | 41 | pagefault_disable(); |
| 42 | |
| 43 | switch (op) { |
| 44 | case FUTEX_OP_SET: |
| 45 | __futex_cas_op("mov\t%4, %1", ret, oldval, uaddr, oparg); |
| 46 | break; |
| 47 | case FUTEX_OP_ADD: |
| 48 | __futex_cas_op("add\t%2, %4, %1", ret, oldval, uaddr, oparg); |
| 49 | break; |
| 50 | case FUTEX_OP_OR: |
| 51 | __futex_cas_op("or\t%2, %4, %1", ret, oldval, uaddr, oparg); |
| 52 | break; |
| 53 | case FUTEX_OP_ANDN: |
Mikael Pettersson | d72609e | 2008-07-30 15:40:50 -0700 | [diff] [blame] | 54 | __futex_cas_op("andn\t%2, %4, %1", ret, oldval, uaddr, oparg); |
Sam Ravnborg | f5e706a | 2008-07-17 21:55:51 -0700 | [diff] [blame] | 55 | break; |
| 56 | case FUTEX_OP_XOR: |
| 57 | __futex_cas_op("xor\t%2, %4, %1", ret, oldval, uaddr, oparg); |
| 58 | break; |
| 59 | default: |
| 60 | ret = -ENOSYS; |
| 61 | } |
| 62 | |
| 63 | pagefault_enable(); |
| 64 | |
Jiri Slaby | 30d6e0a | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 65 | if (!ret) |
| 66 | *oval = oldval; |
| 67 | |
Sam Ravnborg | f5e706a | 2008-07-17 21:55:51 -0700 | [diff] [blame] | 68 | return ret; |
| 69 | } |
| 70 | |
| 71 | static inline int |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 72 | futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr, |
| 73 | u32 oldval, u32 newval) |
Sam Ravnborg | f5e706a | 2008-07-17 21:55:51 -0700 | [diff] [blame] | 74 | { |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 75 | int ret = 0; |
| 76 | |
Sam Ravnborg | f5e706a | 2008-07-17 21:55:51 -0700 | [diff] [blame] | 77 | __asm__ __volatile__( |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 78 | "\n1: casa [%4] %%asi, %3, %1\n" |
Sam Ravnborg | f5e706a | 2008-07-17 21:55:51 -0700 | [diff] [blame] | 79 | "2:\n" |
| 80 | " .section .fixup,#alloc,#execinstr\n" |
| 81 | " .align 4\n" |
| 82 | "3: sethi %%hi(2b), %0\n" |
| 83 | " jmpl %0 + %%lo(2b), %%g0\n" |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 84 | " mov %5, %0\n" |
Sam Ravnborg | f5e706a | 2008-07-17 21:55:51 -0700 | [diff] [blame] | 85 | " .previous\n" |
| 86 | " .section __ex_table,\"a\"\n" |
| 87 | " .align 4\n" |
| 88 | " .word 1b, 3b\n" |
| 89 | " .previous\n" |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 90 | : "+r" (ret), "=r" (newval) |
| 91 | : "1" (newval), "r" (oldval), "r" (uaddr), "i" (-EFAULT) |
Sam Ravnborg | f5e706a | 2008-07-17 21:55:51 -0700 | [diff] [blame] | 92 | : "memory"); |
| 93 | |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 94 | *uval = newval; |
| 95 | return ret; |
Sam Ravnborg | f5e706a | 2008-07-17 21:55:51 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | #endif /* !(_SPARC64_FUTEX_H) */ |