FROMLIST: arm64: Factor out PAN enabling/disabling into separate uaccess_* macros
[firefly-linux-kernel-4.4.55.git] / arch / arm64 / include / asm / uaccess.h
1 /*
2  * Based on arch/arm/include/asm/uaccess.h
3  *
4  * Copyright (C) 2012 ARM Ltd.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 #ifndef __ASM_UACCESS_H
19 #define __ASM_UACCESS_H
20
21 #ifndef __ASSEMBLY__
22
23 /*
24  * User space memory access functions
25  */
26 #include <linux/string.h>
27 #include <linux/thread_info.h>
28
29 #include <asm/alternative.h>
30 #include <asm/cpufeature.h>
31 #include <asm/ptrace.h>
32 #include <asm/sysreg.h>
33 #include <asm/errno.h>
34 #include <asm/memory.h>
35 #include <asm/compiler.h>
36
37 #define VERIFY_READ 0
38 #define VERIFY_WRITE 1
39
40 /*
41  * The exception table consists of pairs of relative offsets: the first
42  * is the relative offset to an instruction that is allowed to fault,
43  * and the second is the relative offset at which the program should
44  * continue. No registers are modified, so it is entirely up to the
45  * continuation code to figure out what to do.
46  *
47  * All the routines below use bits of fixup code that are out of line
48  * with the main instruction path.  This means when everything is well,
49  * we don't even have to jump over them.  Further, they do not intrude
50  * on our cache or tlb entries.
51  */
52
53 struct exception_table_entry
54 {
55         int insn, fixup;
56 };
57
58 #define ARCH_HAS_RELATIVE_EXTABLE
59
60 extern int fixup_exception(struct pt_regs *regs);
61
62 #define KERNEL_DS       (-1UL)
63 #define get_ds()        (KERNEL_DS)
64
65 #define USER_DS         TASK_SIZE_64
66 #define get_fs()        (current_thread_info()->addr_limit)
67
68 static inline void set_fs(mm_segment_t fs)
69 {
70         current_thread_info()->addr_limit = fs;
71
72         /*
73          * Enable/disable UAO so that copy_to_user() etc can access
74          * kernel memory with the unprivileged instructions.
75          */
76         if (IS_ENABLED(CONFIG_ARM64_UAO) && fs == KERNEL_DS)
77                 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(1), ARM64_HAS_UAO));
78         else
79                 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(0), ARM64_HAS_UAO,
80                                 CONFIG_ARM64_UAO));
81 }
82
83 #define segment_eq(a, b)        ((a) == (b))
84
85 /*
86  * Return 1 if addr < current->addr_limit, 0 otherwise.
87  */
88 #define __addr_ok(addr)                                                 \
89 ({                                                                      \
90         unsigned long flag;                                             \
91         asm("cmp %1, %0; cset %0, lo"                                   \
92                 : "=&r" (flag)                                          \
93                 : "r" (addr), "0" (current_thread_info()->addr_limit)   \
94                 : "cc");                                                \
95         flag;                                                           \
96 })
97
98 /*
99  * Test whether a block of memory is a valid user space address.
100  * Returns 1 if the range is valid, 0 otherwise.
101  *
102  * This is equivalent to the following test:
103  * (u65)addr + (u65)size <= current->addr_limit
104  *
105  * This needs 65-bit arithmetic.
106  */
107 #define __range_ok(addr, size)                                          \
108 ({                                                                      \
109         unsigned long flag, roksum;                                     \
110         __chk_user_ptr(addr);                                           \
111         asm("adds %1, %1, %3; ccmp %1, %4, #2, cc; cset %0, ls"         \
112                 : "=&r" (flag), "=&r" (roksum)                          \
113                 : "1" (addr), "Ir" (size),                              \
114                   "r" (current_thread_info()->addr_limit)               \
115                 : "cc");                                                \
116         flag;                                                           \
117 })
118
119 #define access_ok(type, addr, size)     __range_ok(addr, size)
120 #define user_addr_max                   get_fs
121
122 #define _ASM_EXTABLE(from, to)                                          \
123         "       .pushsection    __ex_table, \"a\"\n"                    \
124         "       .align          3\n"                                    \
125         "       .long           (" #from " - .), (" #to " - .)\n"       \
126         "       .popsection\n"
127
128 /*
129  * User access enabling/disabling.
130  */
131 #define __uaccess_disable(alt)                                          \
132 do {                                                                    \
133         asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), alt,                  \
134                         CONFIG_ARM64_PAN));                             \
135 } while (0)
136
137 #define __uaccess_enable(alt)                                           \
138 do {                                                                    \
139         asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), alt,                  \
140                         CONFIG_ARM64_PAN));                             \
141 } while (0)
142
143 static inline void uaccess_disable(void)
144 {
145         __uaccess_disable(ARM64_HAS_PAN);
146 }
147
148 static inline void uaccess_enable(void)
149 {
150         __uaccess_enable(ARM64_HAS_PAN);
151 }
152
153 /*
154  * These functions are no-ops when UAO is present.
155  */
156 static inline void uaccess_disable_not_uao(void)
157 {
158         __uaccess_disable(ARM64_ALT_PAN_NOT_UAO);
159 }
160
161 static inline void uaccess_enable_not_uao(void)
162 {
163         __uaccess_enable(ARM64_ALT_PAN_NOT_UAO);
164 }
165
166 /*
167  * The "__xxx" versions of the user access functions do not verify the address
168  * space - it must have been done previously with a separate "access_ok()"
169  * call.
170  *
171  * The "__xxx_error" versions set the third argument to -EFAULT if an error
172  * occurs, and leave it unchanged on success.
173  */
174 #define __get_user_asm(instr, alt_instr, reg, x, addr, err, feature)    \
175         asm volatile(                                                   \
176         "1:"ALTERNATIVE(instr "     " reg "1, [%2]\n",                  \
177                         alt_instr " " reg "1, [%2]\n", feature)         \
178         "2:\n"                                                          \
179         "       .section .fixup, \"ax\"\n"                              \
180         "       .align  2\n"                                            \
181         "3:     mov     %w0, %3\n"                                      \
182         "       mov     %1, #0\n"                                       \
183         "       b       2b\n"                                           \
184         "       .previous\n"                                            \
185         _ASM_EXTABLE(1b, 3b)                                            \
186         : "+r" (err), "=&r" (x)                                         \
187         : "r" (addr), "i" (-EFAULT))
188
189 #define __get_user_err(x, ptr, err)                                     \
190 do {                                                                    \
191         unsigned long __gu_val;                                         \
192         __chk_user_ptr(ptr);                                            \
193         uaccess_enable_not_uao();                                       \
194         switch (sizeof(*(ptr))) {                                       \
195         case 1:                                                         \
196                 __get_user_asm("ldrb", "ldtrb", "%w", __gu_val, (ptr),  \
197                                (err), ARM64_HAS_UAO);                   \
198                 break;                                                  \
199         case 2:                                                         \
200                 __get_user_asm("ldrh", "ldtrh", "%w", __gu_val, (ptr),  \
201                                (err), ARM64_HAS_UAO);                   \
202                 break;                                                  \
203         case 4:                                                         \
204                 __get_user_asm("ldr", "ldtr", "%w", __gu_val, (ptr),    \
205                                (err), ARM64_HAS_UAO);                   \
206                 break;                                                  \
207         case 8:                                                         \
208                 __get_user_asm("ldr", "ldtr", "%",  __gu_val, (ptr),    \
209                                (err), ARM64_HAS_UAO);                   \
210                 break;                                                  \
211         default:                                                        \
212                 BUILD_BUG();                                            \
213         }                                                               \
214         uaccess_disable_not_uao();                                      \
215         (x) = (__force __typeof__(*(ptr)))__gu_val;                     \
216 } while (0)
217
218 #define __get_user(x, ptr)                                              \
219 ({                                                                      \
220         int __gu_err = 0;                                               \
221         __get_user_err((x), (ptr), __gu_err);                           \
222         __gu_err;                                                       \
223 })
224
225 #define __get_user_error(x, ptr, err)                                   \
226 ({                                                                      \
227         __get_user_err((x), (ptr), (err));                              \
228         (void)0;                                                        \
229 })
230
231 #define __get_user_unaligned __get_user
232
233 #define get_user(x, ptr)                                                \
234 ({                                                                      \
235         __typeof__(*(ptr)) __user *__p = (ptr);                         \
236         might_fault();                                                  \
237         access_ok(VERIFY_READ, __p, sizeof(*__p)) ?                     \
238                 __get_user((x), __p) :                                  \
239                 ((x) = 0, -EFAULT);                                     \
240 })
241
242 #define __put_user_asm(instr, alt_instr, reg, x, addr, err, feature)    \
243         asm volatile(                                                   \
244         "1:"ALTERNATIVE(instr "     " reg "1, [%2]\n",                  \
245                         alt_instr " " reg "1, [%2]\n", feature)         \
246         "2:\n"                                                          \
247         "       .section .fixup,\"ax\"\n"                               \
248         "       .align  2\n"                                            \
249         "3:     mov     %w0, %3\n"                                      \
250         "       b       2b\n"                                           \
251         "       .previous\n"                                            \
252         _ASM_EXTABLE(1b, 3b)                                            \
253         : "+r" (err)                                                    \
254         : "r" (x), "r" (addr), "i" (-EFAULT))
255
256 #define __put_user_err(x, ptr, err)                                     \
257 do {                                                                    \
258         __typeof__(*(ptr)) __pu_val = (x);                              \
259         __chk_user_ptr(ptr);                                            \
260         uaccess_enable_not_uao();                                       \
261         switch (sizeof(*(ptr))) {                                       \
262         case 1:                                                         \
263                 __put_user_asm("strb", "sttrb", "%w", __pu_val, (ptr),  \
264                                (err), ARM64_HAS_UAO);                   \
265                 break;                                                  \
266         case 2:                                                         \
267                 __put_user_asm("strh", "sttrh", "%w", __pu_val, (ptr),  \
268                                (err), ARM64_HAS_UAO);                   \
269                 break;                                                  \
270         case 4:                                                         \
271                 __put_user_asm("str", "sttr", "%w", __pu_val, (ptr),    \
272                                (err), ARM64_HAS_UAO);                   \
273                 break;                                                  \
274         case 8:                                                         \
275                 __put_user_asm("str", "sttr", "%", __pu_val, (ptr),     \
276                                (err), ARM64_HAS_UAO);                   \
277                 break;                                                  \
278         default:                                                        \
279                 BUILD_BUG();                                            \
280         }                                                               \
281         uaccess_disable_not_uao();                                      \
282 } while (0)
283
284 #define __put_user(x, ptr)                                              \
285 ({                                                                      \
286         int __pu_err = 0;                                               \
287         __put_user_err((x), (ptr), __pu_err);                           \
288         __pu_err;                                                       \
289 })
290
291 #define __put_user_error(x, ptr, err)                                   \
292 ({                                                                      \
293         __put_user_err((x), (ptr), (err));                              \
294         (void)0;                                                        \
295 })
296
297 #define __put_user_unaligned __put_user
298
299 #define put_user(x, ptr)                                                \
300 ({                                                                      \
301         __typeof__(*(ptr)) __user *__p = (ptr);                         \
302         might_fault();                                                  \
303         access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ?                    \
304                 __put_user((x), __p) :                                  \
305                 -EFAULT;                                                \
306 })
307
308 extern unsigned long __must_check __arch_copy_from_user(void *to, const void __user *from, unsigned long n);
309 extern unsigned long __must_check __arch_copy_to_user(void __user *to, const void *from, unsigned long n);
310 extern unsigned long __must_check __copy_in_user(void __user *to, const void __user *from, unsigned long n);
311 extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
312
313 static inline unsigned long __must_check __copy_from_user(void *to, const void __user *from, unsigned long n)
314 {
315         check_object_size(to, n, false);
316         return __arch_copy_from_user(to, from, n);
317 }
318
319 static inline unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n)
320 {
321         check_object_size(from, n, true);
322         return __arch_copy_to_user(to, from, n);
323 }
324
325 static inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n)
326 {
327         if (access_ok(VERIFY_READ, from, n)) {
328                 check_object_size(to, n, false);
329                 n = __arch_copy_from_user(to, from, n);
330         } else /* security hole - plug it */
331                 memset(to, 0, n);
332         return n;
333 }
334
335 static inline unsigned long __must_check copy_to_user(void __user *to, const void *from, unsigned long n)
336 {
337         if (access_ok(VERIFY_WRITE, to, n)) {
338                 check_object_size(from, n, true);
339                 n = __arch_copy_to_user(to, from, n);
340         }
341         return n;
342 }
343
344 static inline unsigned long __must_check copy_in_user(void __user *to, const void __user *from, unsigned long n)
345 {
346         if (access_ok(VERIFY_READ, from, n) && access_ok(VERIFY_WRITE, to, n))
347                 n = __copy_in_user(to, from, n);
348         return n;
349 }
350
351 #define __copy_to_user_inatomic __copy_to_user
352 #define __copy_from_user_inatomic __copy_from_user
353
354 static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
355 {
356         if (access_ok(VERIFY_WRITE, to, n))
357                 n = __clear_user(to, n);
358         return n;
359 }
360
361 extern long strncpy_from_user(char *dest, const char __user *src, long count);
362
363 extern __must_check long strlen_user(const char __user *str);
364 extern __must_check long strnlen_user(const char __user *str, long n);
365
366 #else   /* __ASSEMBLY__ */
367
368 #include <asm/alternative.h>
369 #include <asm/assembler.h>
370
371 /*
372  * User access enabling/disabling macros. These are no-ops when UAO is
373  * present.
374  */
375         .macro  uaccess_disable_not_uao, tmp1
376 alternative_if_not ARM64_ALT_PAN_NOT_UAO
377         nop
378 alternative_else
379         SET_PSTATE_PAN(1)
380 alternative_endif
381         .endm
382
383         .macro  uaccess_enable_not_uao, tmp1, tmp2
384 alternative_if_not ARM64_ALT_PAN_NOT_UAO
385         nop
386 alternative_else
387         SET_PSTATE_PAN(0)
388 alternative_endif
389         .endm
390
391 #endif  /* __ASSEMBLY__ */
392
393 #endif /* __ASM_UACCESS_H */