GNU Linux-libre 4.9.314-gnu1
[releases.git] / arch / arm64 / include / asm / futex.h
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>
23
24 #include <asm/alternative.h>
25 #include <asm/cpufeature.h>
26 #include <asm/errno.h>
27 #include <asm/sysreg.h>
28
29 #define FUTEX_MAX_LOOPS 128 /* What's the largest number you can think of? */
30
31 #define __futex_atomic_op(insn, ret, oldval, uaddr, tmp, oparg)         \
32 do {                                                                    \
33         unsigned int loops = FUTEX_MAX_LOOPS;                           \
34                                                                         \
35         asm volatile(                                                   \
36         ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_HAS_PAN,            \
37                     CONFIG_ARM64_PAN)                                   \
38 "       prfm    pstl1strm, %2\n"                                        \
39 "1:     ldxr    %w1, %2\n"                                              \
40         insn "\n"                                                       \
41 "2:     stlxr   %w0, %w3, %2\n"                                         \
42 "       cbz     %w0, 3f\n"                                              \
43 "       sub     %w4, %w4, %w0\n"                                        \
44 "       cbnz    %w4, 1b\n"                                              \
45 "       mov     %w0, %w7\n"                                             \
46 "3:\n"                                                                  \
47 "       dmb     ish\n"                                                  \
48 "       .pushsection .fixup,\"ax\"\n"                                   \
49 "       .align  2\n"                                                    \
50 "4:     mov     %w0, %w6\n"                                             \
51 "       b       3b\n"                                                   \
52 "       .popsection\n"                                                  \
53         _ASM_EXTABLE(1b, 4b)                                            \
54         _ASM_EXTABLE(2b, 4b)                                            \
55         ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_HAS_PAN,            \
56                     CONFIG_ARM64_PAN)                                   \
57         : "=&r" (ret), "=&r" (oldval), "+Q" (*uaddr), "=&r" (tmp),      \
58           "+r" (loops)                                                  \
59         : "r" (oparg), "Ir" (-EFAULT), "Ir" (-EAGAIN)                   \
60         : "memory");                                                    \
61 } while (0)
62
63 static inline int
64 arch_futex_atomic_op_inuser(int op, int oparg, int *oval, u32 __user *uaddr)
65 {
66         int oldval = 0, ret, tmp;
67
68         pagefault_disable();
69
70         switch (op) {
71         case FUTEX_OP_SET:
72                 __futex_atomic_op("mov  %w3, %w5",
73                                   ret, oldval, uaddr, tmp, oparg);
74                 break;
75         case FUTEX_OP_ADD:
76                 __futex_atomic_op("add  %w3, %w1, %w5",
77                                   ret, oldval, uaddr, tmp, oparg);
78                 break;
79         case FUTEX_OP_OR:
80                 __futex_atomic_op("orr  %w3, %w1, %w5",
81                                   ret, oldval, uaddr, tmp, oparg);
82                 break;
83         case FUTEX_OP_ANDN:
84                 __futex_atomic_op("and  %w3, %w1, %w5",
85                                   ret, oldval, uaddr, tmp, ~oparg);
86                 break;
87         case FUTEX_OP_XOR:
88                 __futex_atomic_op("eor  %w3, %w1, %w5",
89                                   ret, oldval, uaddr, tmp, oparg);
90                 break;
91         default:
92                 ret = -ENOSYS;
93         }
94
95         pagefault_enable();
96
97         if (!ret)
98                 *oval = oldval;
99
100         return ret;
101 }
102
103 static inline int
104 futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *_uaddr,
105                               u32 oldval, u32 newval)
106 {
107         int ret = 0;
108         unsigned int loops = FUTEX_MAX_LOOPS;
109         u32 val, tmp;
110         u32 __user *uaddr;
111
112         if (!access_ok(VERIFY_WRITE, _uaddr, sizeof(u32)))
113                 return -EFAULT;
114
115         uaddr = __uaccess_mask_ptr(_uaddr);
116         asm volatile("// futex_atomic_cmpxchg_inatomic\n"
117 ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_HAS_PAN, CONFIG_ARM64_PAN)
118 "       prfm    pstl1strm, %2\n"
119 "1:     ldxr    %w1, %2\n"
120 "       sub     %w3, %w1, %w5\n"
121 "       cbnz    %w3, 4f\n"
122 "2:     stlxr   %w3, %w6, %2\n"
123 "       cbz     %w3, 3f\n"
124 "       sub     %w4, %w4, %w3\n"
125 "       cbnz    %w4, 1b\n"
126 "       mov     %w0, %w8\n"
127 "3:\n"
128 "       dmb     ish\n"
129 "4:\n"
130 "       .pushsection .fixup,\"ax\"\n"
131 "5:     mov     %w0, %w7\n"
132 "       b       4b\n"
133 "       .popsection\n"
134         _ASM_EXTABLE(1b, 5b)
135         _ASM_EXTABLE(2b, 5b)
136 ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_HAS_PAN, CONFIG_ARM64_PAN)
137         : "+r" (ret), "=&r" (val), "+Q" (*uaddr), "=&r" (tmp), "+r" (loops)
138         : "r" (oldval), "r" (newval), "Ir" (-EFAULT), "Ir" (-EAGAIN)
139         : "memory");
140
141         *uval = val;
142         return ret;
143 }
144
145 #endif /* __KERNEL__ */
146 #endif /* __ASM_FUTEX_H */