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