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 #define __futex_atomic_op(insn, ret, oldval, uaddr, oparg)\
21 __asm__ __volatile__( \
27 " .section .fixup,\"ax\" \n" \
32 " .section __ex_table,\"a\" \n" \
38 : "=&r" (ret), "=&r" (oldval) \
39 : "r" (uaddr), "r" (oparg), "ir" (-EFAULT) \
42 static inline int futex_atomic_op_inuser(int encoded_op, u32 __user *uaddr)
44 int op = (encoded_op >> 28) & 7;
45 int cmp = (encoded_op >> 24) & 15;
46 int oparg = (encoded_op << 8) >> 20;
47 int cmparg = (encoded_op << 20) >> 20;
50 if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
53 if (!access_ok(VERIFY_WRITE, uaddr, sizeof(int)))
60 __futex_atomic_op("mov %0, %3", ret, oldval, uaddr, oparg);
63 __futex_atomic_op("add %0, %1, %3", ret, oldval, uaddr, oparg);
66 __futex_atomic_op("or %0, %1, %3", ret, oldval, uaddr, oparg);
69 __futex_atomic_op("bic %0, %1, %3", ret, oldval, uaddr, oparg);
72 __futex_atomic_op("xor %0, %1, %3", ret, oldval, uaddr, oparg);
83 ret = (oldval == cmparg);
86 ret = (oldval != cmparg);
89 ret = (oldval < cmparg);
92 ret = (oldval >= cmparg);
95 ret = (oldval <= cmparg);
98 ret = (oldval > cmparg);
107 /* Compare-xchg with pagefaults disabled.
109 * -Best-Effort: Exchg happens only if compare succeeds.
110 * If compare fails, returns; leaving retry/looping to upper layers
111 * -successful cmp-xchg: return orig value in @addr (same as cmp val)
112 * -Compare fails: return orig value in @addr
113 * -user access r/w fails: return -EFAULT
116 futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr, u32 oldval,
121 if (!access_ok(VERIFY_WRITE, uaddr, sizeof(int)))
126 /* TBD : can use llock/scond */
127 __asm__ __volatile__(
129 " brne %0, %1, 3f \n"
132 " .section .fixup,\"ax\" \n"
136 " .section __ex_table,\"a\" \n"
142 : "r"(oldval), "r"(newval), "r"(uaddr), "ir"(-EFAULT)