blob: 2a9cf845473bb51a4aea7d5317dde0a4b5ab37ed [file] [log] [blame]
David Gibson2ff2ae72005-11-02 13:58:22 +11001#ifndef _ASM_POWERPC_FUTEX_H
2#define _ASM_POWERPC_FUTEX_H
Jakub Jelinek4732efb2005-09-06 15:16:25 -07003
4#ifdef __KERNEL__
5
6#include <linux/futex.h>
Jeff Dike730f4122008-04-30 00:54:49 -07007#include <linux/uaccess.h>
Jakub Jelinek4732efb2005-09-06 15:16:25 -07008#include <asm/errno.h>
Becky Brucefeaf7cf2005-09-22 14:20:04 -05009#include <asm/synch.h>
David Gibson3ddfbcf2005-11-10 12:56:55 +110010#include <asm/asm-compat.h>
Jakub Jelinek4732efb2005-09-06 15:16:25 -070011
12#define __futex_atomic_op(insn, ret, oldval, uaddr, oparg) \
David Gibson2ff2ae72005-11-02 13:58:22 +110013 __asm__ __volatile ( \
Benjamin Herrenschmidtb97021f2011-11-15 17:11:27 +000014 PPC_ATOMIC_ENTRY_BARRIER \
David Gibson2ff2ae72005-11-02 13:58:22 +110015"1: lwarx %0,0,%2\n" \
16 insn \
David Gibson3ddfbcf2005-11-10 12:56:55 +110017 PPC405_ERR77(0, %2) \
David Gibson2ff2ae72005-11-02 13:58:22 +110018"2: stwcx. %1,0,%2\n" \
19 "bne- 1b\n" \
Benjamin Herrenschmidtb97021f2011-11-15 17:11:27 +000020 PPC_ATOMIC_EXIT_BARRIER \
David Gibson2ff2ae72005-11-02 13:58:22 +110021 "li %1,0\n" \
22"3: .section .fixup,\"ax\"\n" \
23"4: li %1,%3\n" \
24 "b 3b\n" \
25 ".previous\n" \
26 ".section __ex_table,\"a\"\n" \
27 ".align 3\n" \
David Gibson3ddfbcf2005-11-10 12:56:55 +110028 PPC_LONG "1b,4b,2b,4b\n" \
David Gibson2ff2ae72005-11-02 13:58:22 +110029 ".previous" \
30 : "=&r" (oldval), "=&r" (ret) \
Paul Mackerras306a8282009-04-13 14:09:09 +000031 : "b" (uaddr), "i" (-EFAULT), "r" (oparg) \
Jakub Jelinek4732efb2005-09-06 15:16:25 -070032 : "cr0", "memory")
33
Michel Lespinasse8d7718a2011-03-10 18:50:58 -080034static inline int futex_atomic_op_inuser (int encoded_op, u32 __user *uaddr)
Jakub Jelinek4732efb2005-09-06 15:16:25 -070035{
36 int op = (encoded_op >> 28) & 7;
37 int cmp = (encoded_op >> 24) & 15;
38 int oparg = (encoded_op << 8) >> 20;
39 int cmparg = (encoded_op << 20) >> 20;
40 int oldval = 0, ret;
41 if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
42 oparg = 1 << oparg;
43
Michel Lespinasse8d7718a2011-03-10 18:50:58 -080044 if (! access_ok (VERIFY_WRITE, uaddr, sizeof(u32)))
Jakub Jelinek4732efb2005-09-06 15:16:25 -070045 return -EFAULT;
46
Peter Zijlstraa8663742006-12-06 20:32:20 -080047 pagefault_disable();
Jakub Jelinek4732efb2005-09-06 15:16:25 -070048
49 switch (op) {
50 case FUTEX_OP_SET:
Paul Mackerras306a8282009-04-13 14:09:09 +000051 __futex_atomic_op("mr %1,%4\n", ret, oldval, uaddr, oparg);
Jakub Jelinek4732efb2005-09-06 15:16:25 -070052 break;
53 case FUTEX_OP_ADD:
Paul Mackerras306a8282009-04-13 14:09:09 +000054 __futex_atomic_op("add %1,%0,%4\n", ret, oldval, uaddr, oparg);
Jakub Jelinek4732efb2005-09-06 15:16:25 -070055 break;
56 case FUTEX_OP_OR:
Paul Mackerras306a8282009-04-13 14:09:09 +000057 __futex_atomic_op("or %1,%0,%4\n", ret, oldval, uaddr, oparg);
Jakub Jelinek4732efb2005-09-06 15:16:25 -070058 break;
59 case FUTEX_OP_ANDN:
Paul Mackerras306a8282009-04-13 14:09:09 +000060 __futex_atomic_op("andc %1,%0,%4\n", ret, oldval, uaddr, oparg);
Jakub Jelinek4732efb2005-09-06 15:16:25 -070061 break;
62 case FUTEX_OP_XOR:
Paul Mackerras306a8282009-04-13 14:09:09 +000063 __futex_atomic_op("xor %1,%0,%4\n", ret, oldval, uaddr, oparg);
Jakub Jelinek4732efb2005-09-06 15:16:25 -070064 break;
65 default:
66 ret = -ENOSYS;
67 }
68
Peter Zijlstraa8663742006-12-06 20:32:20 -080069 pagefault_enable();
Jakub Jelinek4732efb2005-09-06 15:16:25 -070070
71 if (!ret) {
72 switch (cmp) {
73 case FUTEX_OP_CMP_EQ: ret = (oldval == cmparg); break;
74 case FUTEX_OP_CMP_NE: ret = (oldval != cmparg); break;
75 case FUTEX_OP_CMP_LT: ret = (oldval < cmparg); break;
76 case FUTEX_OP_CMP_GE: ret = (oldval >= cmparg); break;
77 case FUTEX_OP_CMP_LE: ret = (oldval <= cmparg); break;
78 case FUTEX_OP_CMP_GT: ret = (oldval > cmparg); break;
79 default: ret = -ENOSYS;
80 }
81 }
82 return ret;
83}
84
Ingo Molnare9056f12006-03-27 01:16:21 -080085static inline int
Michel Lespinasse8d7718a2011-03-10 18:50:58 -080086futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
87 u32 oldval, u32 newval)
Ingo Molnare9056f12006-03-27 01:16:21 -080088{
Michel Lespinasse8d7718a2011-03-10 18:50:58 -080089 int ret = 0;
90 u32 prev;
David Woodhouse69588292006-09-04 21:53:14 -070091
Michel Lespinasse8d7718a2011-03-10 18:50:58 -080092 if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
David Woodhouse69588292006-09-04 21:53:14 -070093 return -EFAULT;
94
95 __asm__ __volatile__ (
Benjamin Herrenschmidtb97021f2011-11-15 17:11:27 +000096 PPC_ATOMIC_ENTRY_BARRIER
Michel Lespinasse37a9d912011-03-10 18:48:51 -080097"1: lwarx %1,0,%3 # futex_atomic_cmpxchg_inatomic\n\
98 cmpw 0,%1,%4\n\
David Woodhouse69588292006-09-04 21:53:14 -070099 bne- 3f\n"
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800100 PPC405_ERR77(0,%3)
101"2: stwcx. %5,0,%3\n\
David Woodhouse69588292006-09-04 21:53:14 -0700102 bne- 1b\n"
Benjamin Herrenschmidtb97021f2011-11-15 17:11:27 +0000103 PPC_ATOMIC_EXIT_BARRIER
David Woodhouse69588292006-09-04 21:53:14 -0700104"3: .section .fixup,\"ax\"\n\
Michel Lespinasse37a9d912011-03-10 18:48:51 -08001054: li %0,%6\n\
David Woodhouse69588292006-09-04 21:53:14 -0700106 b 3b\n\
107 .previous\n\
108 .section __ex_table,\"a\"\n\
109 .align 3\n\
110 " PPC_LONG "1b,4b,2b,4b\n\
111 .previous" \
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800112 : "+r" (ret), "=&r" (prev), "+m" (*uaddr)
David Woodhouse69588292006-09-04 21:53:14 -0700113 : "r" (uaddr), "r" (oldval), "r" (newval), "i" (-EFAULT)
114 : "cc", "memory");
115
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800116 *uval = prev;
117 return ret;
Ingo Molnare9056f12006-03-27 01:16:21 -0800118}
119
David Gibson2ff2ae72005-11-02 13:58:22 +1100120#endif /* __KERNEL__ */
121#endif /* _ASM_POWERPC_FUTEX_H */