blob: 6ebf1734b411b5150412b21a6744ec7c177c2088 [file] [log] [blame]
Ralf Baechle0004a9d2006-10-31 03:45:07 +00001/*
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 */
Jakub Jelinek4732efb2005-09-06 15:16:25 -07008#ifndef _ASM_FUTEX_H
9#define _ASM_FUTEX_H
10
11#ifdef __KERNEL__
12
13#include <linux/futex.h>
Jeff Dike730f4122008-04-30 00:54:49 -070014#include <linux/uaccess.h>
Ralf Baechle0004a9d2006-10-31 03:45:07 +000015#include <asm/barrier.h>
Jakub Jelinek4732efb2005-09-06 15:16:25 -070016#include <asm/errno.h>
Ralf Baechle6ee1da92006-05-03 20:42:39 +010017#include <asm/war.h>
Jakub Jelinek4732efb2005-09-06 15:16:25 -070018
Ralf Baechleebfaeba2005-09-15 08:52:34 +000019#define __futex_atomic_op(insn, ret, oldval, uaddr, oparg) \
20{ \
Ralf Baechle6ee1da92006-05-03 20:42:39 +010021 if (cpu_has_llsc && R10000_LLSC_WAR) { \
22 __asm__ __volatile__( \
23 " .set push \n" \
24 " .set noat \n" \
25 " .set mips3 \n" \
Atsushi Nemoto0307e8d2006-06-11 23:25:43 +090026 "1: ll %1, %4 # __futex_atomic_op \n" \
Ralf Baechle6ee1da92006-05-03 20:42:39 +010027 " .set mips0 \n" \
28 " " insn " \n" \
29 " .set mips3 \n" \
Atsushi Nemoto0307e8d2006-06-11 23:25:43 +090030 "2: sc $1, %2 \n" \
Ralf Baechle6ee1da92006-05-03 20:42:39 +010031 " beqzl $1, 1b \n" \
Ralf Baechle17099b12007-07-14 13:24:05 +010032 __WEAK_LLSC_MB \
Ralf Baechle6ee1da92006-05-03 20:42:39 +010033 "3: \n" \
34 " .set pop \n" \
35 " .set mips0 \n" \
36 " .section .fixup,\"ax\" \n" \
Atsushi Nemoto0307e8d2006-06-11 23:25:43 +090037 "4: li %0, %6 \n" \
Ralf Baechle0f67e902007-11-20 10:44:18 +000038 " j 3b \n" \
Ralf Baechle6ee1da92006-05-03 20:42:39 +010039 " .previous \n" \
40 " .section __ex_table,\"a\" \n" \
41 " "__UA_ADDR "\t1b, 4b \n" \
42 " "__UA_ADDR "\t2b, 4b \n" \
43 " .previous \n" \
Atsushi Nemoto0307e8d2006-06-11 23:25:43 +090044 : "=r" (ret), "=&r" (oldval), "=R" (*uaddr) \
45 : "0" (0), "R" (*uaddr), "Jr" (oparg), "i" (-EFAULT) \
46 : "memory"); \
Ralf Baechle6ee1da92006-05-03 20:42:39 +010047 } else if (cpu_has_llsc) { \
48 __asm__ __volatile__( \
49 " .set push \n" \
50 " .set noat \n" \
51 " .set mips3 \n" \
Atsushi Nemoto0307e8d2006-06-11 23:25:43 +090052 "1: ll %1, %4 # __futex_atomic_op \n" \
Ralf Baechle6ee1da92006-05-03 20:42:39 +010053 " .set mips0 \n" \
54 " " insn " \n" \
55 " .set mips3 \n" \
Atsushi Nemoto0307e8d2006-06-11 23:25:43 +090056 "2: sc $1, %2 \n" \
Ralf Baechle6ee1da92006-05-03 20:42:39 +010057 " beqz $1, 1b \n" \
Ralf Baechle17099b12007-07-14 13:24:05 +010058 __WEAK_LLSC_MB \
Ralf Baechle6ee1da92006-05-03 20:42:39 +010059 "3: \n" \
60 " .set pop \n" \
61 " .set mips0 \n" \
62 " .section .fixup,\"ax\" \n" \
Atsushi Nemoto0307e8d2006-06-11 23:25:43 +090063 "4: li %0, %6 \n" \
Ralf Baechle0f67e902007-11-20 10:44:18 +000064 " j 3b \n" \
Ralf Baechle6ee1da92006-05-03 20:42:39 +010065 " .previous \n" \
66 " .section __ex_table,\"a\" \n" \
67 " "__UA_ADDR "\t1b, 4b \n" \
68 " "__UA_ADDR "\t2b, 4b \n" \
69 " .previous \n" \
Atsushi Nemoto0307e8d2006-06-11 23:25:43 +090070 : "=r" (ret), "=&r" (oldval), "=R" (*uaddr) \
71 : "0" (0), "R" (*uaddr), "Jr" (oparg), "i" (-EFAULT) \
72 : "memory"); \
Ralf Baechle6ee1da92006-05-03 20:42:39 +010073 } else \
74 ret = -ENOSYS; \
Ralf Baechleebfaeba2005-09-15 08:52:34 +000075}
76
Jakub Jelinek4732efb2005-09-06 15:16:25 -070077static inline int
Michel Lespinasse8d7718a2011-03-10 18:50:58 -080078futex_atomic_op_inuser(int encoded_op, u32 __user *uaddr)
Jakub Jelinek4732efb2005-09-06 15:16:25 -070079{
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
Michel Lespinasse8d7718a2011-03-10 18:50:58 -080088 if (! access_ok (VERIFY_WRITE, uaddr, sizeof(u32)))
Jakub Jelinek4732efb2005-09-06 15:16:25 -070089 return -EFAULT;
90
Peter Zijlstraa8663742006-12-06 20:32:20 -080091 pagefault_disable();
Jakub Jelinek4732efb2005-09-06 15:16:25 -070092
93 switch (op) {
94 case FUTEX_OP_SET:
Atsushi Nemoto0307e8d2006-06-11 23:25:43 +090095 __futex_atomic_op("move $1, %z5", ret, oldval, uaddr, oparg);
Ralf Baechleebfaeba2005-09-15 08:52:34 +000096 break;
97
Jakub Jelinek4732efb2005-09-06 15:16:25 -070098 case FUTEX_OP_ADD:
Atsushi Nemoto0307e8d2006-06-11 23:25:43 +090099 __futex_atomic_op("addu $1, %1, %z5",
Ralf Baechleebfaeba2005-09-15 08:52:34 +0000100 ret, oldval, uaddr, oparg);
101 break;
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700102 case FUTEX_OP_OR:
Atsushi Nemoto0307e8d2006-06-11 23:25:43 +0900103 __futex_atomic_op("or $1, %1, %z5",
Ralf Baechleebfaeba2005-09-15 08:52:34 +0000104 ret, oldval, uaddr, oparg);
105 break;
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700106 case FUTEX_OP_ANDN:
Atsushi Nemoto0307e8d2006-06-11 23:25:43 +0900107 __futex_atomic_op("and $1, %1, %z5",
Ralf Baechleebfaeba2005-09-15 08:52:34 +0000108 ret, oldval, uaddr, ~oparg);
109 break;
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700110 case FUTEX_OP_XOR:
Atsushi Nemoto0307e8d2006-06-11 23:25:43 +0900111 __futex_atomic_op("xor $1, %1, %z5",
Ralf Baechleebfaeba2005-09-15 08:52:34 +0000112 ret, oldval, uaddr, oparg);
113 break;
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700114 default:
115 ret = -ENOSYS;
116 }
117
Peter Zijlstraa8663742006-12-06 20:32:20 -0800118 pagefault_enable();
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700119
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
Ingo Molnare9056f12006-03-27 01:16:21 -0800134static inline int
Michel Lespinasse8d7718a2011-03-10 18:50:58 -0800135futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
136 u32 oldval, u32 newval)
Ingo Molnare9056f12006-03-27 01:16:21 -0800137{
Michel Lespinasse8d7718a2011-03-10 18:50:58 -0800138 int ret = 0;
139 u32 val;
Ralf Baechle6ee1da92006-05-03 20:42:39 +0100140
Michel Lespinasse8d7718a2011-03-10 18:50:58 -0800141 if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
Ralf Baechle6ee1da92006-05-03 20:42:39 +0100142 return -EFAULT;
143
144 if (cpu_has_llsc && R10000_LLSC_WAR) {
145 __asm__ __volatile__(
146 "# futex_atomic_cmpxchg_inatomic \n"
147 " .set push \n"
148 " .set noat \n"
149 " .set mips3 \n"
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800150 "1: ll %1, %3 \n"
151 " bne %1, %z4, 3f \n"
Ralf Baechle6ee1da92006-05-03 20:42:39 +0100152 " .set mips0 \n"
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800153 " move $1, %z5 \n"
Ralf Baechle6ee1da92006-05-03 20:42:39 +0100154 " .set mips3 \n"
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800155 "2: sc $1, %2 \n"
Ralf Baechle6ee1da92006-05-03 20:42:39 +0100156 " beqzl $1, 1b \n"
Ralf Baechle17099b12007-07-14 13:24:05 +0100157 __WEAK_LLSC_MB
Ralf Baechle6ee1da92006-05-03 20:42:39 +0100158 "3: \n"
159 " .set pop \n"
160 " .section .fixup,\"ax\" \n"
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800161 "4: li %0, %6 \n"
Ralf Baechle6ee1da92006-05-03 20:42:39 +0100162 " j 3b \n"
163 " .previous \n"
164 " .section __ex_table,\"a\" \n"
165 " "__UA_ADDR "\t1b, 4b \n"
166 " "__UA_ADDR "\t2b, 4b \n"
167 " .previous \n"
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800168 : "+r" (ret), "=&r" (val), "=R" (*uaddr)
Ralf Baechle6ee1da92006-05-03 20:42:39 +0100169 : "R" (*uaddr), "Jr" (oldval), "Jr" (newval), "i" (-EFAULT)
170 : "memory");
171 } else if (cpu_has_llsc) {
172 __asm__ __volatile__(
173 "# futex_atomic_cmpxchg_inatomic \n"
174 " .set push \n"
175 " .set noat \n"
176 " .set mips3 \n"
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800177 "1: ll %1, %3 \n"
178 " bne %1, %z4, 3f \n"
Ralf Baechle6ee1da92006-05-03 20:42:39 +0100179 " .set mips0 \n"
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800180 " move $1, %z5 \n"
Ralf Baechle6ee1da92006-05-03 20:42:39 +0100181 " .set mips3 \n"
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800182 "2: sc $1, %2 \n"
Ralf Baechle6ee1da92006-05-03 20:42:39 +0100183 " beqz $1, 1b \n"
Ralf Baechle17099b12007-07-14 13:24:05 +0100184 __WEAK_LLSC_MB
Ralf Baechle6ee1da92006-05-03 20:42:39 +0100185 "3: \n"
186 " .set pop \n"
187 " .section .fixup,\"ax\" \n"
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800188 "4: li %0, %6 \n"
Ralf Baechle6ee1da92006-05-03 20:42:39 +0100189 " j 3b \n"
190 " .previous \n"
191 " .section __ex_table,\"a\" \n"
192 " "__UA_ADDR "\t1b, 4b \n"
193 " "__UA_ADDR "\t2b, 4b \n"
194 " .previous \n"
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800195 : "+r" (ret), "=&r" (val), "=R" (*uaddr)
Ralf Baechle6ee1da92006-05-03 20:42:39 +0100196 : "R" (*uaddr), "Jr" (oldval), "Jr" (newval), "i" (-EFAULT)
197 : "memory");
198 } else
199 return -ENOSYS;
200
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800201 *uval = val;
202 return ret;
Ingo Molnare9056f12006-03-27 01:16:21 -0800203}
204
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700205#endif
Ralf Baechle0f67e902007-11-20 10:44:18 +0000206#endif /* _ASM_FUTEX_H */