1 /* futex.c: futex operations
3 * Copyright (C) 2005 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/futex.h>
13 #include <linux/uaccess.h>
14 #include <asm/futex.h>
15 #include <asm/errno.h>
18 * the various futex operations; MMU fault checking is ignored under no-MMU
21 static inline int atomic_futex_op_xchg_set(int oparg
, int __user
*uaddr
, int *_oldval
)
26 " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
28 "1: ld.p %M0,%1 \n" /* LD.P/ORCR must be atomic */
29 " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
30 "2: cst.p %3,%M0 ,cc3,#1 \n"
31 " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* clear ICC3.Z if store happens */
39 ".section __ex_table,\"a\" \n"
44 : "+U"(*uaddr
), "=&r"(oldval
), "=&r"(ret
), "=r"(oparg
)
45 : "3"(oparg
), "i"(-EFAULT
)
46 : "memory", "cc7", "cc3", "icc3"
53 static inline int atomic_futex_op_xchg_add(int oparg
, int __user
*uaddr
, int *_oldval
)
58 " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
60 "1: ld.p %M0,%1 \n" /* LD.P/ORCR must be atomic */
61 " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
63 "2: cst.p %3,%M0 ,cc3,#1 \n"
64 " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* clear ICC3.Z if store happens */
72 ".section __ex_table,\"a\" \n"
77 : "+U"(*uaddr
), "=&r"(oldval
), "=&r"(ret
), "=r"(oparg
)
78 : "3"(oparg
), "i"(-EFAULT
)
79 : "memory", "cc7", "cc3", "icc3"
86 static inline int atomic_futex_op_xchg_or(int oparg
, int __user
*uaddr
, int *_oldval
)
91 " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
93 "1: ld.p %M0,%1 \n" /* LD.P/ORCR must be atomic */
94 " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
96 "2: cst.p %3,%M0 ,cc3,#1 \n"
97 " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* clear ICC3.Z if store happens */
105 ".section __ex_table,\"a\" \n"
110 : "+U"(*uaddr
), "=&r"(oldval
), "=&r"(ret
), "=r"(oparg
)
111 : "3"(oparg
), "i"(-EFAULT
)
112 : "memory", "cc7", "cc3", "icc3"
119 static inline int atomic_futex_op_xchg_and(int oparg
, int __user
*uaddr
, int *_oldval
)
124 " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
126 "1: ld.p %M0,%1 \n" /* LD.P/ORCR must be atomic */
127 " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
129 "2: cst.p %3,%M0 ,cc3,#1 \n"
130 " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* clear ICC3.Z if store happens */
138 ".section __ex_table,\"a\" \n"
143 : "+U"(*uaddr
), "=&r"(oldval
), "=&r"(ret
), "=r"(oparg
)
144 : "3"(oparg
), "i"(-EFAULT
)
145 : "memory", "cc7", "cc3", "icc3"
152 static inline int atomic_futex_op_xchg_xor(int oparg
, int __user
*uaddr
, int *_oldval
)
157 " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
159 "1: ld.p %M0,%1 \n" /* LD.P/ORCR must be atomic */
160 " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
162 "2: cst.p %3,%M0 ,cc3,#1 \n"
163 " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* clear ICC3.Z if store happens */
171 ".section __ex_table,\"a\" \n"
176 : "+U"(*uaddr
), "=&r"(oldval
), "=&r"(ret
), "=r"(oparg
)
177 : "3"(oparg
), "i"(-EFAULT
)
178 : "memory", "cc7", "cc3", "icc3"
185 /*****************************************************************************/
187 * do the futex operations
189 int futex_atomic_op_inuser(int encoded_op
, int __user
*uaddr
)
191 int op
= (encoded_op
>> 28) & 7;
192 int cmp
= (encoded_op
>> 24) & 15;
193 int oparg
= (encoded_op
<< 8) >> 20;
194 int cmparg
= (encoded_op
<< 20) >> 20;
197 if (encoded_op
& (FUTEX_OP_OPARG_SHIFT
<< 28))
200 if (!access_ok(VERIFY_WRITE
, uaddr
, sizeof(int)))
207 ret
= atomic_futex_op_xchg_set(oparg
, uaddr
, &oldval
);
210 ret
= atomic_futex_op_xchg_add(oparg
, uaddr
, &oldval
);
213 ret
= atomic_futex_op_xchg_or(oparg
, uaddr
, &oldval
);
216 ret
= atomic_futex_op_xchg_and(~oparg
, uaddr
, &oldval
);
219 ret
= atomic_futex_op_xchg_xor(oparg
, uaddr
, &oldval
);
230 case FUTEX_OP_CMP_EQ
: ret
= (oldval
== cmparg
); break;
231 case FUTEX_OP_CMP_NE
: ret
= (oldval
!= cmparg
); break;
232 case FUTEX_OP_CMP_LT
: ret
= (oldval
< cmparg
); break;
233 case FUTEX_OP_CMP_GE
: ret
= (oldval
>= cmparg
); break;
234 case FUTEX_OP_CMP_LE
: ret
= (oldval
<= cmparg
); break;
235 case FUTEX_OP_CMP_GT
: ret
= (oldval
> cmparg
); break;
236 default: ret
= -ENOSYS
; break;
242 } /* end futex_atomic_op_inuser() */