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 | 4732efbe | 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 | 4732efbe | 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 | 4732efbe | 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 | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 85 | static inline int |
Jiri Slaby | 30d6e0a | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 86 | arch_futex_atomic_op_inuser(int op, int oparg, int *oval, u32 __user *uaddr) |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 87 | { |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 88 | int oldval = 0, ret; |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 89 | |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 90 | pagefault_disable(); |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 91 | |
| 92 | switch (op) { |
| 93 | case FUTEX_OP_SET: |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 94 | __futex_atomic_op("move $1, %z5", ret, oldval, uaddr, oparg); |
Ralf Baechle | ebfaeba | 2005-09-15 08:52:34 +0000 | [diff] [blame] | 95 | break; |
| 96 | |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 97 | case FUTEX_OP_ADD: |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 98 | __futex_atomic_op("addu $1, %1, %z5", |
| 99 | ret, oldval, uaddr, oparg); |
Ralf Baechle | ebfaeba | 2005-09-15 08:52:34 +0000 | [diff] [blame] | 100 | break; |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 101 | case FUTEX_OP_OR: |
Atsushi Nemoto | 0307e8d | 2006-06-11 23:25:43 +0900 | [diff] [blame] | 102 | __futex_atomic_op("or $1, %1, %z5", |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 103 | ret, oldval, uaddr, oparg); |
Ralf Baechle | ebfaeba | 2005-09-15 08:52:34 +0000 | [diff] [blame] | 104 | break; |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 105 | case FUTEX_OP_ANDN: |
Atsushi Nemoto | 0307e8d | 2006-06-11 23:25:43 +0900 | [diff] [blame] | 106 | __futex_atomic_op("and $1, %1, %z5", |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 107 | ret, oldval, uaddr, ~oparg); |
Ralf Baechle | ebfaeba | 2005-09-15 08:52:34 +0000 | [diff] [blame] | 108 | break; |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 109 | case FUTEX_OP_XOR: |
Atsushi Nemoto | 0307e8d | 2006-06-11 23:25:43 +0900 | [diff] [blame] | 110 | __futex_atomic_op("xor $1, %1, %z5", |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 111 | ret, oldval, uaddr, oparg); |
Ralf Baechle | ebfaeba | 2005-09-15 08:52:34 +0000 | [diff] [blame] | 112 | break; |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 113 | default: |
| 114 | ret = -ENOSYS; |
| 115 | } |
| 116 | |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 117 | pagefault_enable(); |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 118 | |
Jiri Slaby | 30d6e0a | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 119 | if (!ret) |
| 120 | *oval = oldval; |
| 121 | |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 122 | return ret; |
| 123 | } |
| 124 | |
Ingo Molnar | e9056f1 | 2006-03-27 01:16:21 -0800 | [diff] [blame] | 125 | static inline int |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 126 | futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr, |
| 127 | u32 oldval, u32 newval) |
Ingo Molnar | e9056f1 | 2006-03-27 01:16:21 -0800 | [diff] [blame] | 128 | { |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 129 | int ret = 0; |
| 130 | u32 val; |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 131 | |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 132 | if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32))) |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 133 | return -EFAULT; |
| 134 | |
| 135 | if (cpu_has_llsc && R10000_LLSC_WAR) { |
| 136 | __asm__ __volatile__( |
| 137 | "# futex_atomic_cmpxchg_inatomic \n" |
| 138 | " .set push \n" |
| 139 | " .set noat \n" |
Ralf Baechle | a809d46 | 2014-03-30 13:20:10 +0200 | [diff] [blame] | 140 | " .set arch=r4000 \n" |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 141 | "1: ll %1, %3 \n" |
| 142 | " bne %1, %z4, 3f \n" |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 143 | " .set mips0 \n" |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 144 | " move $1, %z5 \n" |
Ralf Baechle | a809d46 | 2014-03-30 13:20:10 +0200 | [diff] [blame] | 145 | " .set arch=r4000 \n" |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 146 | "2: sc $1, %2 \n" |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 147 | " beqzl $1, 1b \n" |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 148 | __WEAK_LLSC_MB |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 149 | "3: \n" |
Maciej W. Rozycki | 0e525e4 | 2014-11-15 22:09:31 +0000 | [diff] [blame] | 150 | " .insn \n" |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 151 | " .set pop \n" |
| 152 | " .section .fixup,\"ax\" \n" |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 153 | "4: li %0, %6 \n" |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 154 | " j 3b \n" |
| 155 | " .previous \n" |
| 156 | " .section __ex_table,\"a\" \n" |
| 157 | " "__UA_ADDR "\t1b, 4b \n" |
| 158 | " "__UA_ADDR "\t2b, 4b \n" |
| 159 | " .previous \n" |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 160 | : "+r" (ret), "=&r" (val), "=" GCC_OFF_SMALL_ASM() (*uaddr) |
| 161 | : GCC_OFF_SMALL_ASM() (*uaddr), "Jr" (oldval), "Jr" (newval), |
Maciej W. Rozycki | b0984c4 | 2014-11-15 22:08:48 +0000 | [diff] [blame] | 162 | "i" (-EFAULT) |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 163 | : "memory"); |
| 164 | } else if (cpu_has_llsc) { |
| 165 | __asm__ __volatile__( |
| 166 | "# futex_atomic_cmpxchg_inatomic \n" |
| 167 | " .set push \n" |
| 168 | " .set noat \n" |
Markos Chandras | 1922c35 | 2014-11-19 11:09:55 +0000 | [diff] [blame] | 169 | " .set "MIPS_ISA_ARCH_LEVEL" \n" |
Markos Chandras | a6813fe | 2013-12-04 09:58:36 +0000 | [diff] [blame] | 170 | "1: "user_ll("%1", "%3")" \n" |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 171 | " bne %1, %z4, 3f \n" |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 172 | " .set mips0 \n" |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 173 | " move $1, %z5 \n" |
Markos Chandras | 1922c35 | 2014-11-19 11:09:55 +0000 | [diff] [blame] | 174 | " .set "MIPS_ISA_ARCH_LEVEL" \n" |
Markos Chandras | a6813fe | 2013-12-04 09:58:36 +0000 | [diff] [blame] | 175 | "2: "user_sc("$1", "%2")" \n" |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 176 | " beqz $1, 1b \n" |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 177 | __WEAK_LLSC_MB |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 178 | "3: \n" |
Maciej W. Rozycki | 0e525e4 | 2014-11-15 22:09:31 +0000 | [diff] [blame] | 179 | " .insn \n" |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 180 | " .set pop \n" |
| 181 | " .section .fixup,\"ax\" \n" |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 182 | "4: li %0, %6 \n" |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 183 | " j 3b \n" |
| 184 | " .previous \n" |
| 185 | " .section __ex_table,\"a\" \n" |
| 186 | " "__UA_ADDR "\t1b, 4b \n" |
| 187 | " "__UA_ADDR "\t2b, 4b \n" |
| 188 | " .previous \n" |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 189 | : "+r" (ret), "=&r" (val), "=" GCC_OFF_SMALL_ASM() (*uaddr) |
| 190 | : GCC_OFF_SMALL_ASM() (*uaddr), "Jr" (oldval), "Jr" (newval), |
Maciej W. Rozycki | b0984c4 | 2014-11-15 22:08:48 +0000 | [diff] [blame] | 191 | "i" (-EFAULT) |
Ralf Baechle | 6ee1da9 | 2006-05-03 20:42:39 +0100 | [diff] [blame] | 192 | : "memory"); |
| 193 | } else |
| 194 | return -ENOSYS; |
| 195 | |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 196 | *uval = val; |
| 197 | return ret; |
Ingo Molnar | e9056f1 | 2006-03-27 01:16:21 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 200 | #endif |
Ralf Baechle | 0f67e90 | 2007-11-20 10:44:18 +0000 | [diff] [blame] | 201 | #endif /* _ASM_FUTEX_H */ |