blob: c94e4a3fe2ef3de09decfd1aa47bda3b77342f53 [file] [log] [blame]
David Gibson2ff2ae72005-11-02 13:58:22 +11001#ifndef _ASM_POWERPC_FUTEX_H
2#define _ASM_POWERPC_FUTEX_H
Jakub Jelinek4732efbe2005-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 Jelinek4732efbe2005-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 Jelinek4732efbe2005-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 ( \
Anton Blanchardf10e2e52010-02-10 01:04:06 +000014 PPC_RELEASE_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" \
20 "li %1,0\n" \
21"3: .section .fixup,\"ax\"\n" \
22"4: li %1,%3\n" \
23 "b 3b\n" \
24 ".previous\n" \
25 ".section __ex_table,\"a\"\n" \
26 ".align 3\n" \
David Gibson3ddfbcf2005-11-10 12:56:55 +110027 PPC_LONG "1b,4b,2b,4b\n" \
David Gibson2ff2ae72005-11-02 13:58:22 +110028 ".previous" \
29 : "=&r" (oldval), "=&r" (ret) \
Paul Mackerras306a8282009-04-13 14:09:09 +000030 : "b" (uaddr), "i" (-EFAULT), "r" (oparg) \
Jakub Jelinek4732efbe2005-09-06 15:16:25 -070031 : "cr0", "memory")
32
Michel Lespinasse8d7718a2011-03-10 18:50:58 -080033static inline int futex_atomic_op_inuser (int encoded_op, u32 __user *uaddr)
Jakub Jelinek4732efbe2005-09-06 15:16:25 -070034{
35 int op = (encoded_op >> 28) & 7;
36 int cmp = (encoded_op >> 24) & 15;
37 int oparg = (encoded_op << 8) >> 20;
38 int cmparg = (encoded_op << 20) >> 20;
39 int oldval = 0, ret;
40 if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
41 oparg = 1 << oparg;
42
Michel Lespinasse8d7718a2011-03-10 18:50:58 -080043 if (! access_ok (VERIFY_WRITE, uaddr, sizeof(u32)))
Jakub Jelinek4732efbe2005-09-06 15:16:25 -070044 return -EFAULT;
45
Peter Zijlstraa8663742006-12-06 20:32:20 -080046 pagefault_disable();
Jakub Jelinek4732efbe2005-09-06 15:16:25 -070047
48 switch (op) {
49 case FUTEX_OP_SET:
Paul Mackerras306a8282009-04-13 14:09:09 +000050 __futex_atomic_op("mr %1,%4\n", ret, oldval, uaddr, oparg);
Jakub Jelinek4732efbe2005-09-06 15:16:25 -070051 break;
52 case FUTEX_OP_ADD:
Paul Mackerras306a8282009-04-13 14:09:09 +000053 __futex_atomic_op("add %1,%0,%4\n", ret, oldval, uaddr, oparg);
Jakub Jelinek4732efbe2005-09-06 15:16:25 -070054 break;
55 case FUTEX_OP_OR:
Paul Mackerras306a8282009-04-13 14:09:09 +000056 __futex_atomic_op("or %1,%0,%4\n", ret, oldval, uaddr, oparg);
Jakub Jelinek4732efbe2005-09-06 15:16:25 -070057 break;
58 case FUTEX_OP_ANDN:
Paul Mackerras306a8282009-04-13 14:09:09 +000059 __futex_atomic_op("andc %1,%0,%4\n", ret, oldval, uaddr, oparg);
Jakub Jelinek4732efbe2005-09-06 15:16:25 -070060 break;
61 case FUTEX_OP_XOR:
Paul Mackerras306a8282009-04-13 14:09:09 +000062 __futex_atomic_op("xor %1,%0,%4\n", ret, oldval, uaddr, oparg);
Jakub Jelinek4732efbe2005-09-06 15:16:25 -070063 break;
64 default:
65 ret = -ENOSYS;
66 }
67
Peter Zijlstraa8663742006-12-06 20:32:20 -080068 pagefault_enable();
Jakub Jelinek4732efbe2005-09-06 15:16:25 -070069
70 if (!ret) {
71 switch (cmp) {
72 case FUTEX_OP_CMP_EQ: ret = (oldval == cmparg); break;
73 case FUTEX_OP_CMP_NE: ret = (oldval != cmparg); break;
74 case FUTEX_OP_CMP_LT: ret = (oldval < cmparg); break;
75 case FUTEX_OP_CMP_GE: ret = (oldval >= cmparg); break;
76 case FUTEX_OP_CMP_LE: ret = (oldval <= cmparg); break;
77 case FUTEX_OP_CMP_GT: ret = (oldval > cmparg); break;
78 default: ret = -ENOSYS;
79 }
80 }
81 return ret;
82}
83
Ingo Molnare9056f12006-03-27 01:16:21 -080084static inline int
Michel Lespinasse8d7718a2011-03-10 18:50:58 -080085futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
86 u32 oldval, u32 newval)
Ingo Molnare9056f12006-03-27 01:16:21 -080087{
Michel Lespinasse8d7718a2011-03-10 18:50:58 -080088 int ret = 0;
89 u32 prev;
David Woodhouse69588292006-09-04 21:53:14 -070090
Michel Lespinasse8d7718a2011-03-10 18:50:58 -080091 if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
David Woodhouse69588292006-09-04 21:53:14 -070092 return -EFAULT;
93
94 __asm__ __volatile__ (
Anton Blanchardf10e2e52010-02-10 01:04:06 +000095 PPC_RELEASE_BARRIER
Michel Lespinasse37a9d912011-03-10 18:48:51 -080096"1: lwarx %1,0,%3 # futex_atomic_cmpxchg_inatomic\n\
97 cmpw 0,%1,%4\n\
David Woodhouse69588292006-09-04 21:53:14 -070098 bne- 3f\n"
Michel Lespinasse37a9d912011-03-10 18:48:51 -080099 PPC405_ERR77(0,%3)
100"2: stwcx. %5,0,%3\n\
David Woodhouse69588292006-09-04 21:53:14 -0700101 bne- 1b\n"
Anton Blanchardf10e2e52010-02-10 01:04:06 +0000102 PPC_ACQUIRE_BARRIER
David Woodhouse69588292006-09-04 21:53:14 -0700103"3: .section .fixup,\"ax\"\n\
Michel Lespinasse37a9d912011-03-10 18:48:51 -08001044: li %0,%6\n\
David Woodhouse69588292006-09-04 21:53:14 -0700105 b 3b\n\
106 .previous\n\
107 .section __ex_table,\"a\"\n\
108 .align 3\n\
109 " PPC_LONG "1b,4b,2b,4b\n\
110 .previous" \
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800111 : "+r" (ret), "=&r" (prev), "+m" (*uaddr)
David Woodhouse69588292006-09-04 21:53:14 -0700112 : "r" (uaddr), "r" (oldval), "r" (newval), "i" (-EFAULT)
113 : "cc", "memory");
114
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800115 *uval = prev;
116 return ret;
Ingo Molnare9056f12006-03-27 01:16:21 -0800117}
118
David Gibson2ff2ae72005-11-02 13:58:22 +1100119#endif /* __KERNEL__ */
120#endif /* _ASM_POWERPC_FUTEX_H */