David Gibson | 2ff2ae7 | 2005-11-02 13:58:22 +1100 | [diff] [blame] | 1 | #ifndef _ASM_POWERPC_FUTEX_H |
| 2 | #define _ASM_POWERPC_FUTEX_H |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 3 | |
| 4 | #ifdef __KERNEL__ |
| 5 | |
| 6 | #include <linux/futex.h> |
Jeff Dike | 730f412 | 2008-04-30 00:54:49 -0700 | [diff] [blame] | 7 | #include <linux/uaccess.h> |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 8 | #include <asm/errno.h> |
Becky Bruce | feaf7cf | 2005-09-22 14:20:04 -0500 | [diff] [blame] | 9 | #include <asm/synch.h> |
David Gibson | 3ddfbcf | 2005-11-10 12:56:55 +1100 | [diff] [blame] | 10 | #include <asm/asm-compat.h> |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 11 | |
| 12 | #define __futex_atomic_op(insn, ret, oldval, uaddr, oparg) \ |
David Gibson | 2ff2ae7 | 2005-11-02 13:58:22 +1100 | [diff] [blame] | 13 | __asm__ __volatile ( \ |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 14 | PPC_ATOMIC_ENTRY_BARRIER \ |
David Gibson | 2ff2ae7 | 2005-11-02 13:58:22 +1100 | [diff] [blame] | 15 | "1: lwarx %0,0,%2\n" \ |
| 16 | insn \ |
David Gibson | 3ddfbcf | 2005-11-10 12:56:55 +1100 | [diff] [blame] | 17 | PPC405_ERR77(0, %2) \ |
David Gibson | 2ff2ae7 | 2005-11-02 13:58:22 +1100 | [diff] [blame] | 18 | "2: stwcx. %1,0,%2\n" \ |
| 19 | "bne- 1b\n" \ |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 20 | PPC_ATOMIC_EXIT_BARRIER \ |
David Gibson | 2ff2ae7 | 2005-11-02 13:58:22 +1100 | [diff] [blame] | 21 | "li %1,0\n" \ |
| 22 | "3: .section .fixup,\"ax\"\n" \ |
| 23 | "4: li %1,%3\n" \ |
| 24 | "b 3b\n" \ |
| 25 | ".previous\n" \ |
Nicholas Piggin | 24bfa6a | 2016-10-13 16:42:53 +1100 | [diff] [blame] | 26 | EX_TABLE(1b, 4b) \ |
| 27 | EX_TABLE(2b, 4b) \ |
David Gibson | 2ff2ae7 | 2005-11-02 13:58:22 +1100 | [diff] [blame] | 28 | : "=&r" (oldval), "=&r" (ret) \ |
Paul Mackerras | 306a828 | 2009-04-13 14:09:09 +0000 | [diff] [blame] | 29 | : "b" (uaddr), "i" (-EFAULT), "r" (oparg) \ |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 30 | : "cr0", "memory") |
| 31 | |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 32 | static inline int futex_atomic_op_inuser (int encoded_op, u32 __user *uaddr) |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 33 | { |
| 34 | int op = (encoded_op >> 28) & 7; |
| 35 | int cmp = (encoded_op >> 24) & 15; |
| 36 | int oparg = (encoded_op << 8) >> 20; |
| 37 | int cmparg = (encoded_op << 20) >> 20; |
| 38 | int oldval = 0, ret; |
| 39 | if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28)) |
| 40 | oparg = 1 << oparg; |
| 41 | |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 42 | if (! access_ok (VERIFY_WRITE, uaddr, sizeof(u32))) |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 43 | return -EFAULT; |
| 44 | |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 45 | pagefault_disable(); |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 46 | |
| 47 | switch (op) { |
| 48 | case FUTEX_OP_SET: |
Paul Mackerras | 306a828 | 2009-04-13 14:09:09 +0000 | [diff] [blame] | 49 | __futex_atomic_op("mr %1,%4\n", ret, oldval, uaddr, oparg); |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 50 | break; |
| 51 | case FUTEX_OP_ADD: |
Paul Mackerras | 306a828 | 2009-04-13 14:09:09 +0000 | [diff] [blame] | 52 | __futex_atomic_op("add %1,%0,%4\n", ret, oldval, uaddr, oparg); |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 53 | break; |
| 54 | case FUTEX_OP_OR: |
Paul Mackerras | 306a828 | 2009-04-13 14:09:09 +0000 | [diff] [blame] | 55 | __futex_atomic_op("or %1,%0,%4\n", ret, oldval, uaddr, oparg); |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 56 | break; |
| 57 | case FUTEX_OP_ANDN: |
Paul Mackerras | 306a828 | 2009-04-13 14:09:09 +0000 | [diff] [blame] | 58 | __futex_atomic_op("andc %1,%0,%4\n", ret, oldval, uaddr, oparg); |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 59 | break; |
| 60 | case FUTEX_OP_XOR: |
Paul Mackerras | 306a828 | 2009-04-13 14:09:09 +0000 | [diff] [blame] | 61 | __futex_atomic_op("xor %1,%0,%4\n", ret, oldval, uaddr, oparg); |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 62 | break; |
| 63 | default: |
| 64 | ret = -ENOSYS; |
| 65 | } |
| 66 | |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 67 | pagefault_enable(); |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 68 | |
| 69 | if (!ret) { |
| 70 | switch (cmp) { |
| 71 | case FUTEX_OP_CMP_EQ: ret = (oldval == cmparg); break; |
| 72 | case FUTEX_OP_CMP_NE: ret = (oldval != cmparg); break; |
| 73 | case FUTEX_OP_CMP_LT: ret = (oldval < cmparg); break; |
| 74 | case FUTEX_OP_CMP_GE: ret = (oldval >= cmparg); break; |
| 75 | case FUTEX_OP_CMP_LE: ret = (oldval <= cmparg); break; |
| 76 | case FUTEX_OP_CMP_GT: ret = (oldval > cmparg); break; |
| 77 | default: ret = -ENOSYS; |
| 78 | } |
| 79 | } |
| 80 | return ret; |
| 81 | } |
| 82 | |
Ingo Molnar | e9056f1 | 2006-03-27 01:16:21 -0800 | [diff] [blame] | 83 | static inline int |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 84 | futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr, |
| 85 | u32 oldval, u32 newval) |
Ingo Molnar | e9056f1 | 2006-03-27 01:16:21 -0800 | [diff] [blame] | 86 | { |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 87 | int ret = 0; |
| 88 | u32 prev; |
David Woodhouse | 6958829 | 2006-09-04 21:53:14 -0700 | [diff] [blame] | 89 | |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 90 | if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32))) |
David Woodhouse | 6958829 | 2006-09-04 21:53:14 -0700 | [diff] [blame] | 91 | return -EFAULT; |
| 92 | |
| 93 | __asm__ __volatile__ ( |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 94 | PPC_ATOMIC_ENTRY_BARRIER |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 95 | "1: lwarx %1,0,%3 # futex_atomic_cmpxchg_inatomic\n\ |
| 96 | cmpw 0,%1,%4\n\ |
David Woodhouse | 6958829 | 2006-09-04 21:53:14 -0700 | [diff] [blame] | 97 | bne- 3f\n" |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 98 | PPC405_ERR77(0,%3) |
| 99 | "2: stwcx. %5,0,%3\n\ |
David Woodhouse | 6958829 | 2006-09-04 21:53:14 -0700 | [diff] [blame] | 100 | bne- 1b\n" |
Benjamin Herrenschmidt | b97021f | 2011-11-15 17:11:27 +0000 | [diff] [blame] | 101 | PPC_ATOMIC_EXIT_BARRIER |
David Woodhouse | 6958829 | 2006-09-04 21:53:14 -0700 | [diff] [blame] | 102 | "3: .section .fixup,\"ax\"\n\ |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 103 | 4: li %0,%6\n\ |
David Woodhouse | 6958829 | 2006-09-04 21:53:14 -0700 | [diff] [blame] | 104 | b 3b\n\ |
Nicholas Piggin | 24bfa6a | 2016-10-13 16:42:53 +1100 | [diff] [blame] | 105 | .previous\n" |
| 106 | EX_TABLE(1b, 4b) |
| 107 | EX_TABLE(2b, 4b) |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 108 | : "+r" (ret), "=&r" (prev), "+m" (*uaddr) |
David Woodhouse | 6958829 | 2006-09-04 21:53:14 -0700 | [diff] [blame] | 109 | : "r" (uaddr), "r" (oldval), "r" (newval), "i" (-EFAULT) |
| 110 | : "cc", "memory"); |
| 111 | |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 112 | *uval = prev; |
| 113 | return ret; |
Ingo Molnar | e9056f1 | 2006-03-27 01:16:21 -0800 | [diff] [blame] | 114 | } |
| 115 | |
David Gibson | 2ff2ae7 | 2005-11-02 13:58:22 +1100 | [diff] [blame] | 116 | #endif /* __KERNEL__ */ |
| 117 | #endif /* _ASM_POWERPC_FUTEX_H */ |