Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 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 | */ |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 8 | #ifndef _ASM_FUTEX_H |
| 9 | #define _ASM_FUTEX_H |
| 10 | |
| 11 | #ifdef __KERNEL__ |
| 12 | |
| 13 | #include <linux/futex.h> |
Jeff Dike | 730f412 | 2008-04-30 00:54:49 -0700 | [diff] [blame] | 14 | #include <linux/uaccess.h> |
Markos Chandras | a6813fe | 2013-12-04 09:58:36 +0000 | [diff] [blame] | 15 | #include <asm/asm-eva.h> |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 16 | #include <asm/barrier.h> |
Maciej W. Rozycki | b0984c4 | 2014-11-15 22:08:48 +0000 | [diff] [blame] | 17 | #include <asm/compiler.h> |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 18 | #include <asm/errno.h> |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 19 | #include <asm/war.h> |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 20 | |
Ralf Baechle | ebfaeba | 2005-09-15 08:52:34 +0000 | [diff] [blame] | 21 | #define __futex_atomic_op(insn, ret, oldval, uaddr, oparg) \ |
| 22 | { \ |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 23 | if (cpu_has_llsc && R10000_LLSC_WAR) { \ |
| 24 | __asm__ __volatile__( \ |
| 25 | " .set push \n" \ |
| 26 | " .set noat \n" \ |
Ralf Baechle | a809d46 | 2014-03-30 13:20:10 +0200 | [diff] [blame] | 27 | " .set arch=r4000 \n" \ |
Atsushi Nemoto | 0307e8d | 2006-06-11 23:25:43 +0900 | [diff] [blame] | 28 | "1: ll %1, %4 # __futex_atomic_op \n" \ |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 29 | " .set mips0 \n" \ |
| 30 | " " insn " \n" \ |
Ralf Baechle | a809d46 | 2014-03-30 13:20:10 +0200 | [diff] [blame] | 31 | " .set arch=r4000 \n" \ |
Atsushi Nemoto | 0307e8d | 2006-06-11 23:25:43 +0900 | [diff] [blame] | 32 | "2: sc $1, %2 \n" \ |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 33 | " beqzl $1, 1b \n" \ |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 34 | __WEAK_LLSC_MB \ |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 35 | "3: \n" \ |
Maciej W. Rozycki | 0e525e4 | 2014-11-15 22:09:31 +0000 | [diff] [blame] | 36 | " .insn \n" \ |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 37 | " .set pop \n" \ |
| 38 | " .set mips0 \n" \ |
| 39 | " .section .fixup,\"ax\" \n" \ |
Atsushi Nemoto | 0307e8d | 2006-06-11 23:25:43 +0900 | [diff] [blame] | 40 | "4: li %0, %6 \n" \ |
Ralf Baechle | 0f67e90 | 2007-11-20 10:44:18 +0000 | [diff] [blame] | 41 | " j 3b \n" \ |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 42 | " .previous \n" \ |
| 43 | " .section __ex_table,\"a\" \n" \ |
| 44 | " "__UA_ADDR "\t1b, 4b \n" \ |
| 45 | " "__UA_ADDR "\t2b, 4b \n" \ |
| 46 | " .previous \n" \ |
Maciej W. Rozycki | b0984c4 | 2014-11-15 22:08:48 +0000 | [diff] [blame] | 47 | : "=r" (ret), "=&r" (oldval), \ |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 48 | "=" GCC_OFF_SMALL_ASM() (*uaddr) \ |
| 49 | : "0" (0), GCC_OFF_SMALL_ASM() (*uaddr), "Jr" (oparg), \ |
Maciej W. Rozycki | b0984c4 | 2014-11-15 22:08:48 +0000 | [diff] [blame] | 50 | "i" (-EFAULT) \ |
Atsushi Nemoto | 0307e8d | 2006-06-11 23:25:43 +0900 | [diff] [blame] | 51 | : "memory"); \ |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 52 | } else if (cpu_has_llsc) { \ |
| 53 | __asm__ __volatile__( \ |
| 54 | " .set push \n" \ |
| 55 | " .set noat \n" \ |
Markos Chandras | 1922c35 | 2014-11-19 11:09:55 +0000 | [diff] [blame] | 56 | " .set "MIPS_ISA_ARCH_LEVEL" \n" \ |
Markos Chandras | a6813fe | 2013-12-04 09:58:36 +0000 | [diff] [blame] | 57 | "1: "user_ll("%1", "%4")" # __futex_atomic_op\n" \ |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 58 | " .set mips0 \n" \ |
| 59 | " " insn " \n" \ |
Markos Chandras | 1922c35 | 2014-11-19 11:09:55 +0000 | [diff] [blame] | 60 | " .set "MIPS_ISA_ARCH_LEVEL" \n" \ |
Markos Chandras | a6813fe | 2013-12-04 09:58:36 +0000 | [diff] [blame] | 61 | "2: "user_sc("$1", "%2")" \n" \ |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 62 | " beqz $1, 1b \n" \ |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 63 | __WEAK_LLSC_MB \ |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 64 | "3: \n" \ |
Maciej W. Rozycki | 0e525e4 | 2014-11-15 22:09:31 +0000 | [diff] [blame] | 65 | " .insn \n" \ |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 66 | " .set pop \n" \ |
| 67 | " .set mips0 \n" \ |
| 68 | " .section .fixup,\"ax\" \n" \ |
Atsushi Nemoto | 0307e8d | 2006-06-11 23:25:43 +0900 | [diff] [blame] | 69 | "4: li %0, %6 \n" \ |
Ralf Baechle | 0f67e90 | 2007-11-20 10:44:18 +0000 | [diff] [blame] | 70 | " j 3b \n" \ |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 71 | " .previous \n" \ |
| 72 | " .section __ex_table,\"a\" \n" \ |
| 73 | " "__UA_ADDR "\t1b, 4b \n" \ |
| 74 | " "__UA_ADDR "\t2b, 4b \n" \ |
| 75 | " .previous \n" \ |
Maciej W. Rozycki | b0984c4 | 2014-11-15 22:08:48 +0000 | [diff] [blame] | 76 | : "=r" (ret), "=&r" (oldval), \ |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 77 | "=" GCC_OFF_SMALL_ASM() (*uaddr) \ |
| 78 | : "0" (0), GCC_OFF_SMALL_ASM() (*uaddr), "Jr" (oparg), \ |
Maciej W. Rozycki | b0984c4 | 2014-11-15 22:08:48 +0000 | [diff] [blame] | 79 | "i" (-EFAULT) \ |
Atsushi Nemoto | 0307e8d | 2006-06-11 23:25:43 +0900 | [diff] [blame] | 80 | : "memory"); \ |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 81 | } else \ |
| 82 | ret = -ENOSYS; \ |
Ralf Baechle | ebfaeba | 2005-09-15 08:52:34 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 85 | static inline int |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 86 | futex_atomic_op_inuser(int encoded_op, u32 __user *uaddr) |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 87 | { |
| 88 | int op = (encoded_op >> 28) & 7; |
| 89 | int cmp = (encoded_op >> 24) & 15; |
| 90 | int oparg = (encoded_op << 8) >> 20; |
| 91 | int cmparg = (encoded_op << 20) >> 20; |
| 92 | int oldval = 0, ret; |
| 93 | if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28)) |
| 94 | oparg = 1 << oparg; |
| 95 | |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 96 | if (! access_ok (VERIFY_WRITE, uaddr, sizeof(u32))) |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 97 | return -EFAULT; |
| 98 | |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 99 | pagefault_disable(); |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 100 | |
| 101 | switch (op) { |
| 102 | case FUTEX_OP_SET: |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 103 | __futex_atomic_op("move $1, %z5", ret, oldval, uaddr, oparg); |
Ralf Baechle | ebfaeba | 2005-09-15 08:52:34 +0000 | [diff] [blame] | 104 | break; |
| 105 | |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 106 | case FUTEX_OP_ADD: |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 107 | __futex_atomic_op("addu $1, %1, %z5", |
| 108 | ret, oldval, uaddr, oparg); |
Ralf Baechle | ebfaeba | 2005-09-15 08:52:34 +0000 | [diff] [blame] | 109 | break; |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 110 | case FUTEX_OP_OR: |
Atsushi Nemoto | 0307e8d | 2006-06-11 23:25:43 +0900 | [diff] [blame] | 111 | __futex_atomic_op("or $1, %1, %z5", |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 112 | ret, oldval, uaddr, oparg); |
Ralf Baechle | ebfaeba | 2005-09-15 08:52:34 +0000 | [diff] [blame] | 113 | break; |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 114 | case FUTEX_OP_ANDN: |
Atsushi Nemoto | 0307e8d | 2006-06-11 23:25:43 +0900 | [diff] [blame] | 115 | __futex_atomic_op("and $1, %1, %z5", |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 116 | ret, oldval, uaddr, ~oparg); |
Ralf Baechle | ebfaeba | 2005-09-15 08:52:34 +0000 | [diff] [blame] | 117 | break; |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 118 | case FUTEX_OP_XOR: |
Atsushi Nemoto | 0307e8d | 2006-06-11 23:25:43 +0900 | [diff] [blame] | 119 | __futex_atomic_op("xor $1, %1, %z5", |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 120 | ret, oldval, uaddr, oparg); |
Ralf Baechle | ebfaeba | 2005-09-15 08:52:34 +0000 | [diff] [blame] | 121 | break; |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 122 | default: |
| 123 | ret = -ENOSYS; |
| 124 | } |
| 125 | |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 126 | pagefault_enable(); |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 127 | |
| 128 | if (!ret) { |
| 129 | switch (cmp) { |
| 130 | case FUTEX_OP_CMP_EQ: ret = (oldval == cmparg); break; |
| 131 | case FUTEX_OP_CMP_NE: ret = (oldval != cmparg); break; |
| 132 | case FUTEX_OP_CMP_LT: ret = (oldval < cmparg); break; |
| 133 | case FUTEX_OP_CMP_GE: ret = (oldval >= cmparg); break; |
| 134 | case FUTEX_OP_CMP_LE: ret = (oldval <= cmparg); break; |
| 135 | case FUTEX_OP_CMP_GT: ret = (oldval > cmparg); break; |
| 136 | default: ret = -ENOSYS; |
| 137 | } |
| 138 | } |
| 139 | return ret; |
| 140 | } |
| 141 | |
Ingo Molnar | e9056f1 | 2006-03-27 01:16:21 -0800 | [diff] [blame] | 142 | static inline int |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 143 | futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr, |
| 144 | u32 oldval, u32 newval) |
Ingo Molnar | e9056f1 | 2006-03-27 01:16:21 -0800 | [diff] [blame] | 145 | { |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 146 | int ret = 0; |
| 147 | u32 val; |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 148 | |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 149 | if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32))) |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 150 | return -EFAULT; |
| 151 | |
| 152 | if (cpu_has_llsc && R10000_LLSC_WAR) { |
| 153 | __asm__ __volatile__( |
| 154 | "# futex_atomic_cmpxchg_inatomic \n" |
| 155 | " .set push \n" |
| 156 | " .set noat \n" |
Ralf Baechle | a809d46 | 2014-03-30 13:20:10 +0200 | [diff] [blame] | 157 | " .set arch=r4000 \n" |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 158 | "1: ll %1, %3 \n" |
| 159 | " bne %1, %z4, 3f \n" |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 160 | " .set mips0 \n" |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 161 | " move $1, %z5 \n" |
Ralf Baechle | a809d46 | 2014-03-30 13:20:10 +0200 | [diff] [blame] | 162 | " .set arch=r4000 \n" |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 163 | "2: sc $1, %2 \n" |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 164 | " beqzl $1, 1b \n" |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 165 | __WEAK_LLSC_MB |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 166 | "3: \n" |
Maciej W. Rozycki | 0e525e4 | 2014-11-15 22:09:31 +0000 | [diff] [blame] | 167 | " .insn \n" |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 168 | " .set pop \n" |
| 169 | " .section .fixup,\"ax\" \n" |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 170 | "4: li %0, %6 \n" |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 171 | " j 3b \n" |
| 172 | " .previous \n" |
| 173 | " .section __ex_table,\"a\" \n" |
| 174 | " "__UA_ADDR "\t1b, 4b \n" |
| 175 | " "__UA_ADDR "\t2b, 4b \n" |
| 176 | " .previous \n" |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 177 | : "+r" (ret), "=&r" (val), "=" GCC_OFF_SMALL_ASM() (*uaddr) |
| 178 | : GCC_OFF_SMALL_ASM() (*uaddr), "Jr" (oldval), "Jr" (newval), |
Maciej W. Rozycki | b0984c4 | 2014-11-15 22:08:48 +0000 | [diff] [blame] | 179 | "i" (-EFAULT) |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 180 | : "memory"); |
| 181 | } else if (cpu_has_llsc) { |
| 182 | __asm__ __volatile__( |
| 183 | "# futex_atomic_cmpxchg_inatomic \n" |
| 184 | " .set push \n" |
| 185 | " .set noat \n" |
Markos Chandras | 1922c35 | 2014-11-19 11:09:55 +0000 | [diff] [blame] | 186 | " .set "MIPS_ISA_ARCH_LEVEL" \n" |
Markos Chandras | a6813fe | 2013-12-04 09:58:36 +0000 | [diff] [blame] | 187 | "1: "user_ll("%1", "%3")" \n" |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 188 | " bne %1, %z4, 3f \n" |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 189 | " .set mips0 \n" |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 190 | " move $1, %z5 \n" |
Markos Chandras | 1922c35 | 2014-11-19 11:09:55 +0000 | [diff] [blame] | 191 | " .set "MIPS_ISA_ARCH_LEVEL" \n" |
Markos Chandras | a6813fe | 2013-12-04 09:58:36 +0000 | [diff] [blame] | 192 | "2: "user_sc("$1", "%2")" \n" |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 193 | " beqz $1, 1b \n" |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 194 | __WEAK_LLSC_MB |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 195 | "3: \n" |
Maciej W. Rozycki | 0e525e4 | 2014-11-15 22:09:31 +0000 | [diff] [blame] | 196 | " .insn \n" |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 197 | " .set pop \n" |
| 198 | " .section .fixup,\"ax\" \n" |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 199 | "4: li %0, %6 \n" |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 200 | " j 3b \n" |
| 201 | " .previous \n" |
| 202 | " .section __ex_table,\"a\" \n" |
| 203 | " "__UA_ADDR "\t1b, 4b \n" |
| 204 | " "__UA_ADDR "\t2b, 4b \n" |
| 205 | " .previous \n" |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 206 | : "+r" (ret), "=&r" (val), "=" GCC_OFF_SMALL_ASM() (*uaddr) |
| 207 | : GCC_OFF_SMALL_ASM() (*uaddr), "Jr" (oldval), "Jr" (newval), |
Maciej W. Rozycki | b0984c4 | 2014-11-15 22:08:48 +0000 | [diff] [blame] | 208 | "i" (-EFAULT) |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 209 | : "memory"); |
| 210 | } else |
| 211 | return -ENOSYS; |
| 212 | |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 213 | *uval = val; |
| 214 | return ret; |
Ingo Molnar | e9056f1 | 2006-03-27 01:16:21 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 217 | #endif |
Ralf Baechle | 0f67e90 | 2007-11-20 10:44:18 +0000 | [diff] [blame] | 218 | #endif /* _ASM_FUTEX_H */ |