692a24bd83b7aa9b14a71c512edc76b549911f4f
[linux-3.10.git] / arch / mips / include / asm / futex.h
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (c) 2006  Ralf Baechle (ralf@linux-mips.org)
7  */
8 #ifndef _ASM_FUTEX_H
9 #define _ASM_FUTEX_H
10
11 #ifdef __KERNEL__
12
13 #include <linux/futex.h>
14 #include <linux/uaccess.h>
15 #include <asm/barrier.h>
16 #include <asm/errno.h>
17 #include <asm/war.h>
18
19 #define __futex_atomic_op(insn, ret, oldval, uaddr, oparg)              \
20 {                                                                       \
21         if (cpu_has_llsc && R10000_LLSC_WAR) {                          \
22                 __asm__ __volatile__(                                   \
23                 "       .set    push                            \n"     \
24                 "       .set    noat                            \n"     \
25                 "       .set    mips3                           \n"     \
26                 "1:     ll      %1, %4  # __futex_atomic_op     \n"     \
27                 "       .set    mips0                           \n"     \
28                 "       " insn  "                               \n"     \
29                 "       .set    mips3                           \n"     \
30                 "2:     sc      $1, %2                          \n"     \
31                 "       beqzl   $1, 1b                          \n"     \
32                 __WEAK_LLSC_MB                                          \
33                 "3:                                             \n"     \
34                 "       .set    pop                             \n"     \
35                 "       .set    mips0                           \n"     \
36                 "       .section .fixup,\"ax\"                  \n"     \
37                 "4:     li      %0, %6                          \n"     \
38                 "       j       3b                              \n"     \
39                 "       .previous                               \n"     \
40                 "       .section __ex_table,\"a\"               \n"     \
41                 "       "__UA_ADDR "\t1b, 4b                    \n"     \
42                 "       "__UA_ADDR "\t2b, 4b                    \n"     \
43                 "       .previous                               \n"     \
44                 : "=r" (ret), "=&r" (oldval), "=R" (*uaddr)             \
45                 : "0" (0), "R" (*uaddr), "Jr" (oparg), "i" (-EFAULT)    \
46                 : "memory");                                            \
47         } else if (cpu_has_llsc) {                                      \
48                 __asm__ __volatile__(                                   \
49                 "       .set    push                            \n"     \
50                 "       .set    noat                            \n"     \
51                 "       .set    mips3                           \n"     \
52                 "1:     ll      %1, %4  # __futex_atomic_op     \n"     \
53                 "       .set    mips0                           \n"     \
54                 "       " insn  "                               \n"     \
55                 "       .set    mips3                           \n"     \
56                 "2:     sc      $1, %2                          \n"     \
57                 "       beqz    $1, 1b                          \n"     \
58                 __WEAK_LLSC_MB                                          \
59                 "3:                                             \n"     \
60                 "       .set    pop                             \n"     \
61                 "       .set    mips0                           \n"     \
62                 "       .section .fixup,\"ax\"                  \n"     \
63                 "4:     li      %0, %6                          \n"     \
64                 "       j       3b                              \n"     \
65                 "       .previous                               \n"     \
66                 "       .section __ex_table,\"a\"               \n"     \
67                 "       "__UA_ADDR "\t1b, 4b                    \n"     \
68                 "       "__UA_ADDR "\t2b, 4b                    \n"     \
69                 "       .previous                               \n"     \
70                 : "=r" (ret), "=&r" (oldval), "=R" (*uaddr)             \
71                 : "0" (0), "R" (*uaddr), "Jr" (oparg), "i" (-EFAULT)    \
72                 : "memory");                                            \
73         } else                                                          \
74                 ret = -ENOSYS;                                          \
75 }
76
77 static inline int
78 futex_atomic_op_inuser(int encoded_op, int __user *uaddr)
79 {
80         int op = (encoded_op >> 28) & 7;
81         int cmp = (encoded_op >> 24) & 15;
82         int oparg = (encoded_op << 8) >> 20;
83         int cmparg = (encoded_op << 20) >> 20;
84         int oldval = 0, ret;
85         if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
86                 oparg = 1 << oparg;
87
88         if (! access_ok (VERIFY_WRITE, uaddr, sizeof(int)))
89                 return -EFAULT;
90
91         pagefault_disable();
92
93         switch (op) {
94         case FUTEX_OP_SET:
95                 __futex_atomic_op("move $1, %z5", ret, oldval, uaddr, oparg);
96                 break;
97
98         case FUTEX_OP_ADD:
99                 __futex_atomic_op("addu $1, %1, %z5",
100                                   ret, oldval, uaddr, oparg);
101                 break;
102         case FUTEX_OP_OR:
103                 __futex_atomic_op("or   $1, %1, %z5",
104                                   ret, oldval, uaddr, oparg);
105                 break;
106         case FUTEX_OP_ANDN:
107                 __futex_atomic_op("and  $1, %1, %z5",
108                                   ret, oldval, uaddr, ~oparg);
109                 break;
110         case FUTEX_OP_XOR:
111                 __futex_atomic_op("xor  $1, %1, %z5",
112                                   ret, oldval, uaddr, oparg);
113                 break;
114         default:
115                 ret = -ENOSYS;
116         }
117
118         pagefault_enable();
119
120         if (!ret) {
121                 switch (cmp) {
122                 case FUTEX_OP_CMP_EQ: ret = (oldval == cmparg); break;
123                 case FUTEX_OP_CMP_NE: ret = (oldval != cmparg); break;
124                 case FUTEX_OP_CMP_LT: ret = (oldval < cmparg); break;
125                 case FUTEX_OP_CMP_GE: ret = (oldval >= cmparg); break;
126                 case FUTEX_OP_CMP_LE: ret = (oldval <= cmparg); break;
127                 case FUTEX_OP_CMP_GT: ret = (oldval > cmparg); break;
128                 default: ret = -ENOSYS;
129                 }
130         }
131         return ret;
132 }
133
134 static inline int
135 futex_atomic_cmpxchg_inatomic(int *uval, int __user *uaddr,
136                               int oldval, int newval)
137 {
138         int ret = 0, val;
139
140         if (!access_ok(VERIFY_WRITE, uaddr, sizeof(int)))
141                 return -EFAULT;
142
143         if (cpu_has_llsc && R10000_LLSC_WAR) {
144                 __asm__ __volatile__(
145                 "# futex_atomic_cmpxchg_inatomic                        \n"
146                 "       .set    push                                    \n"
147                 "       .set    noat                                    \n"
148                 "       .set    mips3                                   \n"
149                 "1:     ll      %1, %3                                  \n"
150                 "       bne     %1, %z4, 3f                             \n"
151                 "       .set    mips0                                   \n"
152                 "       move    $1, %z5                                 \n"
153                 "       .set    mips3                                   \n"
154                 "2:     sc      $1, %2                                  \n"
155                 "       beqzl   $1, 1b                                  \n"
156                 __WEAK_LLSC_MB
157                 "3:                                                     \n"
158                 "       .set    pop                                     \n"
159                 "       .section .fixup,\"ax\"                          \n"
160                 "4:     li      %0, %6                                  \n"
161                 "       j       3b                                      \n"
162                 "       .previous                                       \n"
163                 "       .section __ex_table,\"a\"                       \n"
164                 "       "__UA_ADDR "\t1b, 4b                            \n"
165                 "       "__UA_ADDR "\t2b, 4b                            \n"
166                 "       .previous                                       \n"
167                 : "+r" (ret), "=&r" (val), "=R" (*uaddr)
168                 : "R" (*uaddr), "Jr" (oldval), "Jr" (newval), "i" (-EFAULT)
169                 : "memory");
170         } else if (cpu_has_llsc) {
171                 __asm__ __volatile__(
172                 "# futex_atomic_cmpxchg_inatomic                        \n"
173                 "       .set    push                                    \n"
174                 "       .set    noat                                    \n"
175                 "       .set    mips3                                   \n"
176                 "1:     ll      %1, %3                                  \n"
177                 "       bne     %1, %z4, 3f                             \n"
178                 "       .set    mips0                                   \n"
179                 "       move    $1, %z5                                 \n"
180                 "       .set    mips3                                   \n"
181                 "2:     sc      $1, %2                                  \n"
182                 "       beqz    $1, 1b                                  \n"
183                 __WEAK_LLSC_MB
184                 "3:                                                     \n"
185                 "       .set    pop                                     \n"
186                 "       .section .fixup,\"ax\"                          \n"
187                 "4:     li      %0, %6                                  \n"
188                 "       j       3b                                      \n"
189                 "       .previous                                       \n"
190                 "       .section __ex_table,\"a\"                       \n"
191                 "       "__UA_ADDR "\t1b, 4b                            \n"
192                 "       "__UA_ADDR "\t2b, 4b                            \n"
193                 "       .previous                                       \n"
194                 : "+r" (ret), "=&r" (val), "=R" (*uaddr)
195                 : "R" (*uaddr), "Jr" (oldval), "Jr" (newval), "i" (-EFAULT)
196                 : "memory");
197         } else
198                 return -ENOSYS;
199
200         *uval = val;
201         return ret;
202 }
203
204 #endif
205 #endif /* _ASM_FUTEX_H */