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)\
24 __asm__ __volatile__( \
25 "1: llock %1, [%2] \n" \
27 "2: scond %0, [%2] \n" \
31 " .section .fixup,\"ax\" \n" \
36 " .section __ex_table,\"a\" \n" \
42 : "=&r" (ret), "=&r" (oldval) \
43 : "r" (uaddr), "r" (oparg), "ir" (-EFAULT) \
47 #else /* !CONFIG_ARC_HAS_LLSC */
49 #define __futex_atomic_op(insn, ret, oldval, uaddr, oparg)\
52 __asm__ __volatile__( \
58 " .section .fixup,\"ax\" \n" \
63 " .section __ex_table,\"a\" \n" \
69 : "=&r" (ret), "=&r" (oldval) \
70 : "r" (uaddr), "r" (oparg), "ir" (-EFAULT) \
76 static inline int futex_atomic_op_inuser(int encoded_op
, u32 __user
*uaddr
)
78 int op
= (encoded_op
>> 28) & 7;
79 int cmp
= (encoded_op
>> 24) & 15;
80 int oparg
= (encoded_op
<< 8) >> 20;
81 int cmparg
= (encoded_op
<< 20) >> 20;
84 if (encoded_op
& (FUTEX_OP_OPARG_SHIFT
<< 28))
87 if (!access_ok(VERIFY_WRITE
, uaddr
, sizeof(int)))
90 #ifndef CONFIG_ARC_HAS_LLSC
91 preempt_disable(); /* to guarantee atomic r-m-w of futex op */
97 __futex_atomic_op("mov %0, %3", ret
, oldval
, uaddr
, oparg
);
100 /* oldval = *uaddr; *uaddr += oparg ; ret = *uaddr */
101 __futex_atomic_op("add %0, %1, %3", ret
, oldval
, uaddr
, oparg
);
104 __futex_atomic_op("or %0, %1, %3", ret
, oldval
, uaddr
, oparg
);
107 __futex_atomic_op("bic %0, %1, %3", ret
, oldval
, uaddr
, oparg
);
110 __futex_atomic_op("xor %0, %1, %3", ret
, oldval
, uaddr
, oparg
);
117 #ifndef CONFIG_ARC_HAS_LLSC
123 case FUTEX_OP_CMP_EQ
:
124 ret
= (oldval
== cmparg
);
126 case FUTEX_OP_CMP_NE
:
127 ret
= (oldval
!= cmparg
);
129 case FUTEX_OP_CMP_LT
:
130 ret
= (oldval
< cmparg
);
132 case FUTEX_OP_CMP_GE
:
133 ret
= (oldval
>= cmparg
);
135 case FUTEX_OP_CMP_LE
:
136 ret
= (oldval
<= cmparg
);
138 case FUTEX_OP_CMP_GT
:
139 ret
= (oldval
> cmparg
);
149 * cmpxchg of futex (pagefaults disabled by caller)
150 * Return 0 for success, -EFAULT otherwise
153 futex_atomic_cmpxchg_inatomic(u32
*uval
, u32 __user
*uaddr
, u32 expval
,
159 if (!access_ok(VERIFY_WRITE
, uaddr
, sizeof(u32
)))
162 #ifndef CONFIG_ARC_HAS_LLSC
163 preempt_disable(); /* to guarantee atomic r-m-w of futex op */
167 __asm__
__volatile__(
168 #ifdef CONFIG_ARC_HAS_LLSC
169 "1: llock %1, [%4] \n"
170 " brne %1, %2, 3f \n"
171 "2: scond %3, [%4] \n"
175 " brne %1, %2, 3f \n"
179 " .section .fixup,\"ax\" \n"
183 " .section __ex_table,\"a\" \n"
188 : "+&r"(ret
), "=&r"(existval
)
189 : "r"(expval
), "r"(newval
), "r"(uaddr
), "ir"(-EFAULT
)
194 #ifndef CONFIG_ARC_HAS_LLSC