2 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * Vineetg: August 2010: From Android kernel work
14 #include <linux/futex.h>
15 #include <linux/preempt.h>
16 #include <linux/uaccess.h>
17 #include <asm/errno.h>
19 #ifdef CONFIG_ARC_HAS_LLSC
21 #define __futex_atomic_op(insn, ret, oldval, uaddr, oparg)\
23 __asm__ __volatile__( \
24 "1: llock %1, [%2] \n" \
26 "2: scond %0, [%2] \n" \
30 " .section .fixup,\"ax\" \n" \
35 " .section __ex_table,\"a\" \n" \
41 : "=&r" (ret), "=&r" (oldval) \
42 : "r" (uaddr), "r" (oparg), "ir" (-EFAULT) \
45 #else /* !CONFIG_ARC_HAS_LLSC */
47 #define __futex_atomic_op(insn, ret, oldval, uaddr, oparg)\
49 __asm__ __volatile__( \
55 " .section .fixup,\"ax\" \n" \
60 " .section __ex_table,\"a\" \n" \
66 : "=&r" (ret), "=&r" (oldval) \
67 : "r" (uaddr), "r" (oparg), "ir" (-EFAULT) \
72 static inline int futex_atomic_op_inuser(int encoded_op
, u32 __user
*uaddr
)
74 int op
= (encoded_op
>> 28) & 7;
75 int cmp
= (encoded_op
>> 24) & 15;
76 int oparg
= (encoded_op
<< 8) >> 20;
77 int cmparg
= (encoded_op
<< 20) >> 20;
80 if (encoded_op
& (FUTEX_OP_OPARG_SHIFT
<< 28))
83 if (!access_ok(VERIFY_WRITE
, uaddr
, sizeof(int)))
90 __futex_atomic_op("mov %0, %3", ret
, oldval
, uaddr
, oparg
);
93 __futex_atomic_op("add %0, %1, %3", ret
, oldval
, uaddr
, oparg
);
96 __futex_atomic_op("or %0, %1, %3", ret
, oldval
, uaddr
, oparg
);
99 __futex_atomic_op("bic %0, %1, %3", ret
, oldval
, uaddr
, oparg
);
102 __futex_atomic_op("xor %0, %1, %3", ret
, oldval
, uaddr
, oparg
);
112 case FUTEX_OP_CMP_EQ
:
113 ret
= (oldval
== cmparg
);
115 case FUTEX_OP_CMP_NE
:
116 ret
= (oldval
!= cmparg
);
118 case FUTEX_OP_CMP_LT
:
119 ret
= (oldval
< cmparg
);
121 case FUTEX_OP_CMP_GE
:
122 ret
= (oldval
>= cmparg
);
124 case FUTEX_OP_CMP_LE
:
125 ret
= (oldval
<= cmparg
);
127 case FUTEX_OP_CMP_GT
:
128 ret
= (oldval
> cmparg
);
137 /* Compare-xchg with pagefaults disabled.
139 * -Best-Effort: Exchg happens only if compare succeeds.
140 * If compare fails, returns; leaving retry/looping to upper layers
141 * -successful cmp-xchg: return orig value in @addr (same as cmp val)
142 * -Compare fails: return orig value in @addr
143 * -user access r/w fails: return -EFAULT
146 futex_atomic_cmpxchg_inatomic(u32
*uval
, u32 __user
*uaddr
, u32 oldval
,
151 if (!access_ok(VERIFY_WRITE
, uaddr
, sizeof(int)))
156 __asm__
__volatile__(
157 #ifdef CONFIG_ARC_HAS_LLSC
158 "1: llock %0, [%3] \n"
159 " brne %0, %1, 3f \n"
160 "2: scond %2, [%3] \n"
164 " brne %0, %1, 3f \n"
168 " .section .fixup,\"ax\" \n"
172 " .section __ex_table,\"a\" \n"
178 : "r"(oldval
), "r"(newval
), "r"(uaddr
), "ir"(-EFAULT
)