1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_ARM_FUTEX_H
3 #define _ASM_ARM_FUTEX_H
4
5 #ifdef __KERNEL__
6
7 #include <linux/futex.h>
8 #include <linux/uaccess.h>
9 #include <asm/errno.h>
10
11 #define __futex_atomic_ex_table(err_reg) \
12 "3:\n" \
13 " ex_entry 1b, 4f\n" \
14 " ex_entry 2b, 4f\n" \
15 " .pushsection .text.fixup,\"ax\"\n" \
16 " .align 2\n" \
17 "4: mov %0, " err_reg "\n" \
18 " b 3b\n" \
19 " .popsection"
20
21 #ifdef CONFIG_SMP
22
23 #define __futex_atomic_op(insn, ret, oldval, tmp, uaddr, oparg) \
24 ({ \
25 unsigned int __ua_flags; \
26 smp_mb(); \
27 prefetchw(uaddr); \
28 __ua_flags = uaccess_save_and_enable(); \
29 __asm__ __volatile__( \
30 "1: ldrex %1, [%3]\n" \
31 " " insn "\n" \
32 "2: strex %2, %0, [%3]\n" \
33 " teq %2, #0\n" \
34 " bne 1b\n" \
35 " mov %0, #0\n" \
36 __futex_atomic_ex_table("%5") \
37 : "=&r" (ret), "=&r" (oldval), "=&r" (tmp) \
38 : "r" (uaddr), "r" (oparg), "Ir" (-EFAULT) \
39 : "cc", "memory"); \
40 uaccess_restore(__ua_flags); \
41 })
42
43 static inline int
futex_atomic_cmpxchg_inatomic(u32 * uval,u32 __user * uaddr,u32 oldval,u32 newval)44 futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
45 u32 oldval, u32 newval)
46 {
47 unsigned int __ua_flags;
48 int ret;
49 u32 val;
50
51 if (!access_ok(uaddr, sizeof(u32)))
52 return -EFAULT;
53
54 smp_mb();
55 /* Prefetching cannot fault */
56 prefetchw(uaddr);
57 __ua_flags = uaccess_save_and_enable();
58 __asm__ __volatile__("@futex_atomic_cmpxchg_inatomic\n"
59 "1: ldrex %1, [%4]\n"
60 " teq %1, %2\n"
61 " ite eq @ explicit IT needed for the 2b label\n"
62 "2: strexeq %0, %3, [%4]\n"
63 " movne %0, #0\n"
64 " teq %0, #0\n"
65 " bne 1b\n"
66 __futex_atomic_ex_table("%5")
67 : "=&r" (ret), "=&r" (val)
68 : "r" (oldval), "r" (newval), "r" (uaddr), "Ir" (-EFAULT)
69 : "cc", "memory");
70 uaccess_restore(__ua_flags);
71 smp_mb();
72
73 *uval = val;
74 return ret;
75 }
76
77 #else /* !SMP, we can work around lack of atomic ops by disabling preemption */
78
79 #include <linux/preempt.h>
80 #include <asm/domain.h>
81
82 #define __futex_atomic_op(insn, ret, oldval, tmp, uaddr, oparg) \
83 ({ \
84 unsigned int __ua_flags = uaccess_save_and_enable(); \
85 __asm__ __volatile__( \
86 "1: " TUSER(ldr) " %1, [%3]\n" \
87 " " insn "\n" \
88 "2: " TUSER(str) " %0, [%3]\n" \
89 " mov %0, #0\n" \
90 __futex_atomic_ex_table("%5") \
91 : "=&r" (ret), "=&r" (oldval), "=&r" (tmp) \
92 : "r" (uaddr), "r" (oparg), "Ir" (-EFAULT) \
93 : "cc", "memory"); \
94 uaccess_restore(__ua_flags); \
95 })
96
97 static inline int
futex_atomic_cmpxchg_inatomic(u32 * uval,u32 __user * uaddr,u32 oldval,u32 newval)98 futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
99 u32 oldval, u32 newval)
100 {
101 unsigned int __ua_flags;
102 int ret = 0;
103 u32 val;
104
105 if (!access_ok(uaddr, sizeof(u32)))
106 return -EFAULT;
107
108 preempt_disable();
109 __ua_flags = uaccess_save_and_enable();
110 __asm__ __volatile__("@futex_atomic_cmpxchg_inatomic\n"
111 " .syntax unified\n"
112 "1: " TUSER(ldr) " %1, [%4]\n"
113 " teq %1, %2\n"
114 " it eq @ explicit IT needed for the 2b label\n"
115 "2: " TUSERCOND(str, eq) " %3, [%4]\n"
116 __futex_atomic_ex_table("%5")
117 : "+r" (ret), "=&r" (val)
118 : "r" (oldval), "r" (newval), "r" (uaddr), "Ir" (-EFAULT)
119 : "cc", "memory");
120 uaccess_restore(__ua_flags);
121
122 *uval = val;
123 preempt_enable();
124
125 return ret;
126 }
127
128 #endif /* !SMP */
129
130 static inline int
arch_futex_atomic_op_inuser(int op,int oparg,int * oval,u32 __user * uaddr)131 arch_futex_atomic_op_inuser(int op, int oparg, int *oval, u32 __user *uaddr)
132 {
133 int oldval = 0, ret, tmp;
134
135 if (!access_ok(uaddr, sizeof(u32)))
136 return -EFAULT;
137
138 #ifndef CONFIG_SMP
139 preempt_disable();
140 #endif
141
142 switch (op) {
143 case FUTEX_OP_SET:
144 __futex_atomic_op("mov %0, %4", ret, oldval, tmp, uaddr, oparg);
145 break;
146 case FUTEX_OP_ADD:
147 __futex_atomic_op("add %0, %1, %4", ret, oldval, tmp, uaddr, oparg);
148 break;
149 case FUTEX_OP_OR:
150 __futex_atomic_op("orr %0, %1, %4", ret, oldval, tmp, uaddr, oparg);
151 break;
152 case FUTEX_OP_ANDN:
153 __futex_atomic_op("and %0, %1, %4", ret, oldval, tmp, uaddr, ~oparg);
154 break;
155 case FUTEX_OP_XOR:
156 __futex_atomic_op("eor %0, %1, %4", ret, oldval, tmp, uaddr, oparg);
157 break;
158 default:
159 ret = -ENOSYS;
160 }
161
162 #ifndef CONFIG_SMP
163 preempt_enable();
164 #endif
165
166 /*
167 * Store unconditionally. If ret != 0 the extra store is the least
168 * of the worries but GCC cannot figure out that __futex_atomic_op()
169 * is either setting ret to -EFAULT or storing the old value in
170 * oldval which results in a uninitialized warning at the call site.
171 */
172 *oval = oldval;
173
174 return ret;
175 }
176
177 #endif /* __KERNEL__ */
178 #endif /* _ASM_ARM_FUTEX_H */
179