Catalin Marinas | 6170a97 | 2012-03-05 11:49:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 ARM Ltd. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | #ifndef __ASM_FUTEX_H |
| 17 | #define __ASM_FUTEX_H |
| 18 | |
| 19 | #ifdef __KERNEL__ |
| 20 | |
| 21 | #include <linux/futex.h> |
| 22 | #include <linux/uaccess.h> |
James Morse | 338d4f4 | 2015-07-22 19:05:54 +0100 | [diff] [blame^] | 23 | |
| 24 | #include <asm/alternative.h> |
| 25 | #include <asm/cpufeature.h> |
Catalin Marinas | 6170a97 | 2012-03-05 11:49:29 +0000 | [diff] [blame] | 26 | #include <asm/errno.h> |
James Morse | 338d4f4 | 2015-07-22 19:05:54 +0100 | [diff] [blame^] | 27 | #include <asm/sysreg.h> |
Catalin Marinas | 6170a97 | 2012-03-05 11:49:29 +0000 | [diff] [blame] | 28 | |
| 29 | #define __futex_atomic_op(insn, ret, oldval, uaddr, tmp, oparg) \ |
| 30 | asm volatile( \ |
James Morse | 338d4f4 | 2015-07-22 19:05:54 +0100 | [diff] [blame^] | 31 | ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_HAS_PAN, \ |
| 32 | CONFIG_ARM64_PAN) \ |
Will Deacon | 8e86f0b | 2014-02-04 12:29:12 +0000 | [diff] [blame] | 33 | "1: ldxr %w1, %2\n" \ |
Catalin Marinas | 6170a97 | 2012-03-05 11:49:29 +0000 | [diff] [blame] | 34 | insn "\n" \ |
| 35 | "2: stlxr %w3, %w0, %2\n" \ |
| 36 | " cbnz %w3, 1b\n" \ |
Will Deacon | 8e86f0b | 2014-02-04 12:29:12 +0000 | [diff] [blame] | 37 | " dmb ish\n" \ |
Catalin Marinas | 6170a97 | 2012-03-05 11:49:29 +0000 | [diff] [blame] | 38 | "3:\n" \ |
| 39 | " .pushsection .fixup,\"ax\"\n" \ |
Will Deacon | 4da7a56 | 2013-11-06 19:31:24 +0000 | [diff] [blame] | 40 | " .align 2\n" \ |
Catalin Marinas | 6170a97 | 2012-03-05 11:49:29 +0000 | [diff] [blame] | 41 | "4: mov %w0, %w5\n" \ |
| 42 | " b 3b\n" \ |
| 43 | " .popsection\n" \ |
| 44 | " .pushsection __ex_table,\"a\"\n" \ |
| 45 | " .align 3\n" \ |
| 46 | " .quad 1b, 4b, 2b, 4b\n" \ |
| 47 | " .popsection\n" \ |
James Morse | 338d4f4 | 2015-07-22 19:05:54 +0100 | [diff] [blame^] | 48 | ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_HAS_PAN, \ |
| 49 | CONFIG_ARM64_PAN) \ |
Catalin Marinas | 6170a97 | 2012-03-05 11:49:29 +0000 | [diff] [blame] | 50 | : "=&r" (ret), "=&r" (oldval), "+Q" (*uaddr), "=&r" (tmp) \ |
| 51 | : "r" (oparg), "Ir" (-EFAULT) \ |
Will Deacon | 95c4189 | 2014-02-04 12:29:13 +0000 | [diff] [blame] | 52 | : "memory") |
Catalin Marinas | 6170a97 | 2012-03-05 11:49:29 +0000 | [diff] [blame] | 53 | |
| 54 | static inline int |
| 55 | futex_atomic_op_inuser (int encoded_op, u32 __user *uaddr) |
| 56 | { |
| 57 | int op = (encoded_op >> 28) & 7; |
| 58 | int cmp = (encoded_op >> 24) & 15; |
| 59 | int oparg = (encoded_op << 8) >> 20; |
| 60 | int cmparg = (encoded_op << 20) >> 20; |
| 61 | int oldval = 0, ret, tmp; |
| 62 | |
| 63 | if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28)) |
| 64 | oparg = 1 << oparg; |
| 65 | |
| 66 | if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32))) |
| 67 | return -EFAULT; |
| 68 | |
David Hildenbrand | 2f09b22 | 2015-05-11 17:52:17 +0200 | [diff] [blame] | 69 | pagefault_disable(); |
Catalin Marinas | 6170a97 | 2012-03-05 11:49:29 +0000 | [diff] [blame] | 70 | |
| 71 | switch (op) { |
| 72 | case FUTEX_OP_SET: |
| 73 | __futex_atomic_op("mov %w0, %w4", |
| 74 | ret, oldval, uaddr, tmp, oparg); |
| 75 | break; |
| 76 | case FUTEX_OP_ADD: |
| 77 | __futex_atomic_op("add %w0, %w1, %w4", |
| 78 | ret, oldval, uaddr, tmp, oparg); |
| 79 | break; |
| 80 | case FUTEX_OP_OR: |
| 81 | __futex_atomic_op("orr %w0, %w1, %w4", |
| 82 | ret, oldval, uaddr, tmp, oparg); |
| 83 | break; |
| 84 | case FUTEX_OP_ANDN: |
| 85 | __futex_atomic_op("and %w0, %w1, %w4", |
| 86 | ret, oldval, uaddr, tmp, ~oparg); |
| 87 | break; |
| 88 | case FUTEX_OP_XOR: |
| 89 | __futex_atomic_op("eor %w0, %w1, %w4", |
| 90 | ret, oldval, uaddr, tmp, oparg); |
| 91 | break; |
| 92 | default: |
| 93 | ret = -ENOSYS; |
| 94 | } |
| 95 | |
David Hildenbrand | 2f09b22 | 2015-05-11 17:52:17 +0200 | [diff] [blame] | 96 | pagefault_enable(); |
Catalin Marinas | 6170a97 | 2012-03-05 11:49:29 +0000 | [diff] [blame] | 97 | |
| 98 | if (!ret) { |
| 99 | switch (cmp) { |
| 100 | case FUTEX_OP_CMP_EQ: ret = (oldval == cmparg); break; |
| 101 | case FUTEX_OP_CMP_NE: ret = (oldval != cmparg); break; |
| 102 | case FUTEX_OP_CMP_LT: ret = (oldval < cmparg); break; |
| 103 | case FUTEX_OP_CMP_GE: ret = (oldval >= cmparg); break; |
| 104 | case FUTEX_OP_CMP_LE: ret = (oldval <= cmparg); break; |
| 105 | case FUTEX_OP_CMP_GT: ret = (oldval > cmparg); break; |
| 106 | default: ret = -ENOSYS; |
| 107 | } |
| 108 | } |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | static inline int |
| 113 | futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr, |
| 114 | u32 oldval, u32 newval) |
| 115 | { |
| 116 | int ret = 0; |
| 117 | u32 val, tmp; |
| 118 | |
| 119 | if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32))) |
| 120 | return -EFAULT; |
| 121 | |
| 122 | asm volatile("// futex_atomic_cmpxchg_inatomic\n" |
Will Deacon | 8e86f0b | 2014-02-04 12:29:12 +0000 | [diff] [blame] | 123 | "1: ldxr %w1, %2\n" |
Catalin Marinas | 6170a97 | 2012-03-05 11:49:29 +0000 | [diff] [blame] | 124 | " sub %w3, %w1, %w4\n" |
| 125 | " cbnz %w3, 3f\n" |
| 126 | "2: stlxr %w3, %w5, %2\n" |
| 127 | " cbnz %w3, 1b\n" |
Will Deacon | 8e86f0b | 2014-02-04 12:29:12 +0000 | [diff] [blame] | 128 | " dmb ish\n" |
Catalin Marinas | 6170a97 | 2012-03-05 11:49:29 +0000 | [diff] [blame] | 129 | "3:\n" |
| 130 | " .pushsection .fixup,\"ax\"\n" |
| 131 | "4: mov %w0, %w6\n" |
| 132 | " b 3b\n" |
| 133 | " .popsection\n" |
| 134 | " .pushsection __ex_table,\"a\"\n" |
| 135 | " .align 3\n" |
| 136 | " .quad 1b, 4b, 2b, 4b\n" |
| 137 | " .popsection\n" |
| 138 | : "+r" (ret), "=&r" (val), "+Q" (*uaddr), "=&r" (tmp) |
| 139 | : "r" (oldval), "r" (newval), "Ir" (-EFAULT) |
Will Deacon | 95c4189 | 2014-02-04 12:29:13 +0000 | [diff] [blame] | 140 | : "memory"); |
Catalin Marinas | 6170a97 | 2012-03-05 11:49:29 +0000 | [diff] [blame] | 141 | |
| 142 | *uval = val; |
| 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | #endif /* __KERNEL__ */ |
| 147 | #endif /* __ASM_FUTEX_H */ |