Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 2 | #ifndef _ASM_FUTEX_H |
| 3 | #define _ASM_FUTEX_H |
| 4 | |
Jakub Jelinek | a192dc1 | 2006-09-26 14:00:56 -0700 | [diff] [blame] | 5 | #include <linux/futex.h> |
Jeff Dike | 730f412 | 2008-04-30 00:54:49 -0700 | [diff] [blame] | 6 | #include <linux/uaccess.h> |
Jakub Jelinek | a192dc1 | 2006-09-26 14:00:56 -0700 | [diff] [blame] | 7 | #include <asm/errno.h> |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 8 | |
Jakub Jelinek | a192dc1 | 2006-09-26 14:00:56 -0700 | [diff] [blame] | 9 | #define __futex_atomic_op1(insn, ret, oldval, uaddr, oparg) \ |
| 10 | do { \ |
| 11 | register unsigned long r8 __asm ("r8") = 0; \ |
| 12 | __asm__ __volatile__( \ |
| 13 | " mf;; \n" \ |
| 14 | "[1:] " insn ";; \n" \ |
| 15 | " .xdata4 \"__ex_table\", 1b-., 2f-. \n" \ |
| 16 | "[2:]" \ |
| 17 | : "+r" (r8), "=r" (oldval) \ |
| 18 | : "r" (uaddr), "r" (oparg) \ |
| 19 | : "memory"); \ |
| 20 | ret = r8; \ |
| 21 | } while (0) |
| 22 | |
| 23 | #define __futex_atomic_op2(insn, ret, oldval, uaddr, oparg) \ |
| 24 | do { \ |
| 25 | register unsigned long r8 __asm ("r8") = 0; \ |
| 26 | int val, newval; \ |
| 27 | do { \ |
| 28 | __asm__ __volatile__( \ |
| 29 | " mf;; \n" \ |
| 30 | "[1:] ld4 %3=[%4];; \n" \ |
| 31 | " mov %2=%3 \n" \ |
| 32 | insn ";; \n" \ |
| 33 | " mov ar.ccv=%2;; \n" \ |
| 34 | "[2:] cmpxchg4.acq %1=[%4],%3,ar.ccv;; \n" \ |
| 35 | " .xdata4 \"__ex_table\", 1b-., 3f-.\n" \ |
| 36 | " .xdata4 \"__ex_table\", 2b-., 3f-.\n" \ |
| 37 | "[3:]" \ |
| 38 | : "+r" (r8), "=r" (val), "=&r" (oldval), \ |
| 39 | "=&r" (newval) \ |
| 40 | : "r" (uaddr), "r" (oparg) \ |
| 41 | : "memory"); \ |
| 42 | if (unlikely (r8)) \ |
| 43 | break; \ |
| 44 | } while (unlikely (val != oldval)); \ |
| 45 | ret = r8; \ |
| 46 | } while (0) |
| 47 | |
| 48 | static inline int |
Jiri Slaby | 30d6e0a | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 49 | arch_futex_atomic_op_inuser(int op, int oparg, int *oval, u32 __user *uaddr) |
Jakub Jelinek | a192dc1 | 2006-09-26 14:00:56 -0700 | [diff] [blame] | 50 | { |
Jakub Jelinek | a192dc1 | 2006-09-26 14:00:56 -0700 | [diff] [blame] | 51 | int oldval = 0, ret; |
Jakub Jelinek | a192dc1 | 2006-09-26 14:00:56 -0700 | [diff] [blame] | 52 | |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 53 | pagefault_disable(); |
Jakub Jelinek | a192dc1 | 2006-09-26 14:00:56 -0700 | [diff] [blame] | 54 | |
| 55 | switch (op) { |
| 56 | case FUTEX_OP_SET: |
| 57 | __futex_atomic_op1("xchg4 %1=[%2],%3", ret, oldval, uaddr, |
| 58 | oparg); |
| 59 | break; |
| 60 | case FUTEX_OP_ADD: |
| 61 | __futex_atomic_op2("add %3=%3,%5", ret, oldval, uaddr, oparg); |
| 62 | break; |
| 63 | case FUTEX_OP_OR: |
| 64 | __futex_atomic_op2("or %3=%3,%5", ret, oldval, uaddr, oparg); |
| 65 | break; |
| 66 | case FUTEX_OP_ANDN: |
| 67 | __futex_atomic_op2("and %3=%3,%5", ret, oldval, uaddr, |
| 68 | ~oparg); |
| 69 | break; |
| 70 | case FUTEX_OP_XOR: |
| 71 | __futex_atomic_op2("xor %3=%3,%5", ret, oldval, uaddr, oparg); |
| 72 | break; |
| 73 | default: |
| 74 | ret = -ENOSYS; |
| 75 | } |
| 76 | |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 77 | pagefault_enable(); |
Jakub Jelinek | a192dc1 | 2006-09-26 14:00:56 -0700 | [diff] [blame] | 78 | |
Jiri Slaby | 30d6e0a | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 79 | if (!ret) |
| 80 | *oval = oldval; |
| 81 | |
Jakub Jelinek | a192dc1 | 2006-09-26 14:00:56 -0700 | [diff] [blame] | 82 | return ret; |
| 83 | } |
| 84 | |
| 85 | static inline int |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 86 | futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr, |
| 87 | u32 oldval, u32 newval) |
Jakub Jelinek | a192dc1 | 2006-09-26 14:00:56 -0700 | [diff] [blame] | 88 | { |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 89 | if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32))) |
Jakub Jelinek | a192dc1 | 2006-09-26 14:00:56 -0700 | [diff] [blame] | 90 | return -EFAULT; |
| 91 | |
| 92 | { |
Stephan Schreiber | 136f39d | 2013-03-19 15:22:27 -0700 | [diff] [blame] | 93 | register unsigned long r8 __asm ("r8") = 0; |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 94 | unsigned long prev; |
Jakub Jelinek | a192dc1 | 2006-09-26 14:00:56 -0700 | [diff] [blame] | 95 | __asm__ __volatile__( |
| 96 | " mf;; \n" |
Luck, Tony | c76f39b | 2012-04-16 16:28:01 -0700 | [diff] [blame] | 97 | " mov ar.ccv=%4;; \n" |
| 98 | "[1:] cmpxchg4.acq %1=[%2],%3,ar.ccv \n" |
Jakub Jelinek | a192dc1 | 2006-09-26 14:00:56 -0700 | [diff] [blame] | 99 | " .xdata4 \"__ex_table\", 1b-., 2f-. \n" |
| 100 | "[2:]" |
Stephan Schreiber | 136f39d | 2013-03-19 15:22:27 -0700 | [diff] [blame] | 101 | : "+r" (r8), "=&r" (prev) |
Jakub Jelinek | a192dc1 | 2006-09-26 14:00:56 -0700 | [diff] [blame] | 102 | : "r" (uaddr), "r" (newval), |
| 103 | "rO" ((long) (unsigned) oldval) |
| 104 | : "memory"); |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 105 | *uval = prev; |
Jakub Jelinek | a192dc1 | 2006-09-26 14:00:56 -0700 | [diff] [blame] | 106 | return r8; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | #endif /* _ASM_FUTEX_H */ |