]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/arm64/include/asm/uaccess.h
f5e1e090b4d23261697baa2809cdd732df26c636
[karo-tx-linux.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 #include <asm/alternative.h>
22 #include <asm/kernel-pgtable.h>
23 #include <asm/sysreg.h>
24
25 /*
26  * User space memory access functions
27  */
28 #include <linux/bitops.h>
29 #include <linux/kasan-checks.h>
30 #include <linux/string.h>
31 #include <linux/thread_info.h>
32
33 #include <asm/cpufeature.h>
34 #include <asm/ptrace.h>
35 #include <asm/errno.h>
36 #include <asm/memory.h>
37 #include <asm/compiler.h>
38
39 /*
40  * The exception table consists of pairs of relative offsets: the first
41  * is the relative offset to an instruction that is allowed to fault,
42  * and the second is the relative offset at which the program should
43  * continue. No registers are modified, so it is entirely up to the
44  * continuation code to figure out what to do.
45  *
46  * All the routines below use bits of fixup code that are out of line
47  * with the main instruction path.  This means when everything is well,
48  * we don't even have to jump over them.  Further, they do not intrude
49  * on our cache or tlb entries.
50  */
51
52 struct exception_table_entry
53 {
54         int insn, fixup;
55 };
56
57 #define ARCH_HAS_RELATIVE_EXTABLE
58
59 extern int fixup_exception(struct pt_regs *regs);
60
61 #define KERNEL_DS       (-1UL)
62 #define get_ds()        (KERNEL_DS)
63
64 #define USER_DS         TASK_SIZE_64
65 #define get_fs()        (current_thread_info()->addr_limit)
66
67 static inline void set_fs(mm_segment_t fs)
68 {
69         current_thread_info()->addr_limit = fs;
70
71         /*
72          * Enable/disable UAO so that copy_to_user() etc can access
73          * kernel memory with the unprivileged instructions.
74          */
75         if (IS_ENABLED(CONFIG_ARM64_UAO) && fs == KERNEL_DS)
76                 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(1), ARM64_HAS_UAO));
77         else
78                 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(0), ARM64_HAS_UAO,
79                                 CONFIG_ARM64_UAO));
80 }
81
82 #define segment_eq(a, b)        ((a) == (b))
83
84 /*
85  * Test whether a block of memory is a valid user space address.
86  * Returns 1 if the range is valid, 0 otherwise.
87  *
88  * This is equivalent to the following test:
89  * (u65)addr + (u65)size <= current->addr_limit
90  *
91  * This needs 65-bit arithmetic.
92  */
93 #define __range_ok(addr, size)                                          \
94 ({                                                                      \
95         unsigned long flag, roksum;                                     \
96         __chk_user_ptr(addr);                                           \
97         asm("adds %1, %1, %3; ccmp %1, %4, #2, cc; cset %0, ls"         \
98                 : "=&r" (flag), "=&r" (roksum)                          \
99                 : "1" (addr), "Ir" (size),                              \
100                   "r" (current_thread_info()->addr_limit)               \
101                 : "cc");                                                \
102         flag;                                                           \
103 })
104
105 /*
106  * When dealing with data aborts or instruction traps we may end up with
107  * a tagged userland pointer. Clear the tag to get a sane pointer to pass
108  * on to access_ok(), for instance.
109  */
110 #define untagged_addr(addr)             sign_extend64(addr, 55)
111
112 #define access_ok(type, addr, size)     __range_ok(addr, size)
113 #define user_addr_max                   get_fs
114
115 #define _ASM_EXTABLE(from, to)                                          \
116         "       .pushsection    __ex_table, \"a\"\n"                    \
117         "       .align          3\n"                                    \
118         "       .long           (" #from " - .), (" #to " - .)\n"       \
119         "       .popsection\n"
120
121 /*
122  * User access enabling/disabling.
123  */
124 #ifdef CONFIG_ARM64_SW_TTBR0_PAN
125 static inline void __uaccess_ttbr0_disable(void)
126 {
127         unsigned long ttbr;
128
129         /* reserved_ttbr0 placed at the end of swapper_pg_dir */
130         ttbr = read_sysreg(ttbr1_el1) + SWAPPER_DIR_SIZE;
131         write_sysreg(ttbr, ttbr0_el1);
132         isb();
133 }
134
135 static inline void __uaccess_ttbr0_enable(void)
136 {
137         unsigned long flags;
138
139         /*
140          * Disable interrupts to avoid preemption between reading the 'ttbr0'
141          * variable and the MSR. A context switch could trigger an ASID
142          * roll-over and an update of 'ttbr0'.
143          */
144         local_irq_save(flags);
145         write_sysreg(current_thread_info()->ttbr0, ttbr0_el1);
146         isb();
147         local_irq_restore(flags);
148 }
149
150 static inline bool uaccess_ttbr0_disable(void)
151 {
152         if (!system_uses_ttbr0_pan())
153                 return false;
154         __uaccess_ttbr0_disable();
155         return true;
156 }
157
158 static inline bool uaccess_ttbr0_enable(void)
159 {
160         if (!system_uses_ttbr0_pan())
161                 return false;
162         __uaccess_ttbr0_enable();
163         return true;
164 }
165 #else
166 static inline bool uaccess_ttbr0_disable(void)
167 {
168         return false;
169 }
170
171 static inline bool uaccess_ttbr0_enable(void)
172 {
173         return false;
174 }
175 #endif
176
177 #define __uaccess_disable(alt)                                          \
178 do {                                                                    \
179         if (!uaccess_ttbr0_disable())                                   \
180                 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), alt,          \
181                                 CONFIG_ARM64_PAN));                     \
182 } while (0)
183
184 #define __uaccess_enable(alt)                                           \
185 do {                                                                    \
186         if (!uaccess_ttbr0_enable())                                    \
187                 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), alt,          \
188                                 CONFIG_ARM64_PAN));                     \
189 } while (0)
190
191 static inline void uaccess_disable(void)
192 {
193         __uaccess_disable(ARM64_HAS_PAN);
194 }
195
196 static inline void uaccess_enable(void)
197 {
198         __uaccess_enable(ARM64_HAS_PAN);
199 }
200
201 /*
202  * These functions are no-ops when UAO is present.
203  */
204 static inline void uaccess_disable_not_uao(void)
205 {
206         __uaccess_disable(ARM64_ALT_PAN_NOT_UAO);
207 }
208
209 static inline void uaccess_enable_not_uao(void)
210 {
211         __uaccess_enable(ARM64_ALT_PAN_NOT_UAO);
212 }
213
214 /*
215  * The "__xxx" versions of the user access functions do not verify the address
216  * space - it must have been done previously with a separate "access_ok()"
217  * call.
218  *
219  * The "__xxx_error" versions set the third argument to -EFAULT if an error
220  * occurs, and leave it unchanged on success.
221  */
222 #define __get_user_asm(instr, alt_instr, reg, x, addr, err, feature)    \
223         asm volatile(                                                   \
224         "1:"ALTERNATIVE(instr "     " reg "1, [%2]\n",                  \
225                         alt_instr " " reg "1, [%2]\n", feature)         \
226         "2:\n"                                                          \
227         "       .section .fixup, \"ax\"\n"                              \
228         "       .align  2\n"                                            \
229         "3:     mov     %w0, %3\n"                                      \
230         "       mov     %1, #0\n"                                       \
231         "       b       2b\n"                                           \
232         "       .previous\n"                                            \
233         _ASM_EXTABLE(1b, 3b)                                            \
234         : "+r" (err), "=&r" (x)                                         \
235         : "r" (addr), "i" (-EFAULT))
236
237 #define __get_user_err(x, ptr, err)                                     \
238 do {                                                                    \
239         unsigned long __gu_val;                                         \
240         __chk_user_ptr(ptr);                                            \
241         uaccess_enable_not_uao();                                       \
242         switch (sizeof(*(ptr))) {                                       \
243         case 1:                                                         \
244                 __get_user_asm("ldrb", "ldtrb", "%w", __gu_val, (ptr),  \
245                                (err), ARM64_HAS_UAO);                   \
246                 break;                                                  \
247         case 2:                                                         \
248                 __get_user_asm("ldrh", "ldtrh", "%w", __gu_val, (ptr),  \
249                                (err), ARM64_HAS_UAO);                   \
250                 break;                                                  \
251         case 4:                                                         \
252                 __get_user_asm("ldr", "ldtr", "%w", __gu_val, (ptr),    \
253                                (err), ARM64_HAS_UAO);                   \
254                 break;                                                  \
255         case 8:                                                         \
256                 __get_user_asm("ldr", "ldtr", "%",  __gu_val, (ptr),    \
257                                (err), ARM64_HAS_UAO);                   \
258                 break;                                                  \
259         default:                                                        \
260                 BUILD_BUG();                                            \
261         }                                                               \
262         uaccess_disable_not_uao();                                      \
263         (x) = (__force __typeof__(*(ptr)))__gu_val;                     \
264 } while (0)
265
266 #define __get_user(x, ptr)                                              \
267 ({                                                                      \
268         int __gu_err = 0;                                               \
269         __get_user_err((x), (ptr), __gu_err);                           \
270         __gu_err;                                                       \
271 })
272
273 #define __get_user_error(x, ptr, err)                                   \
274 ({                                                                      \
275         __get_user_err((x), (ptr), (err));                              \
276         (void)0;                                                        \
277 })
278
279 #define __get_user_unaligned __get_user
280
281 #define get_user(x, ptr)                                                \
282 ({                                                                      \
283         __typeof__(*(ptr)) __user *__p = (ptr);                         \
284         might_fault();                                                  \
285         access_ok(VERIFY_READ, __p, sizeof(*__p)) ?                     \
286                 __get_user((x), __p) :                                  \
287                 ((x) = 0, -EFAULT);                                     \
288 })
289
290 #define __put_user_asm(instr, alt_instr, reg, x, addr, err, feature)    \
291         asm volatile(                                                   \
292         "1:"ALTERNATIVE(instr "     " reg "1, [%2]\n",                  \
293                         alt_instr " " reg "1, [%2]\n", feature)         \
294         "2:\n"                                                          \
295         "       .section .fixup,\"ax\"\n"                               \
296         "       .align  2\n"                                            \
297         "3:     mov     %w0, %3\n"                                      \
298         "       b       2b\n"                                           \
299         "       .previous\n"                                            \
300         _ASM_EXTABLE(1b, 3b)                                            \
301         : "+r" (err)                                                    \
302         : "r" (x), "r" (addr), "i" (-EFAULT))
303
304 #define __put_user_err(x, ptr, err)                                     \
305 do {                                                                    \
306         __typeof__(*(ptr)) __pu_val = (x);                              \
307         __chk_user_ptr(ptr);                                            \
308         uaccess_enable_not_uao();                                       \
309         switch (sizeof(*(ptr))) {                                       \
310         case 1:                                                         \
311                 __put_user_asm("strb", "sttrb", "%w", __pu_val, (ptr),  \
312                                (err), ARM64_HAS_UAO);                   \
313                 break;                                                  \
314         case 2:                                                         \
315                 __put_user_asm("strh", "sttrh", "%w", __pu_val, (ptr),  \
316                                (err), ARM64_HAS_UAO);                   \
317                 break;                                                  \
318         case 4:                                                         \
319                 __put_user_asm("str", "sttr", "%w", __pu_val, (ptr),    \
320                                (err), ARM64_HAS_UAO);                   \
321                 break;                                                  \
322         case 8:                                                         \
323                 __put_user_asm("str", "sttr", "%", __pu_val, (ptr),     \
324                                (err), ARM64_HAS_UAO);                   \
325                 break;                                                  \
326         default:                                                        \
327                 BUILD_BUG();                                            \
328         }                                                               \
329         uaccess_disable_not_uao();                                      \
330 } while (0)
331
332 #define __put_user(x, ptr)                                              \
333 ({                                                                      \
334         int __pu_err = 0;                                               \
335         __put_user_err((x), (ptr), __pu_err);                           \
336         __pu_err;                                                       \
337 })
338
339 #define __put_user_error(x, ptr, err)                                   \
340 ({                                                                      \
341         __put_user_err((x), (ptr), (err));                              \
342         (void)0;                                                        \
343 })
344
345 #define __put_user_unaligned __put_user
346
347 #define put_user(x, ptr)                                                \
348 ({                                                                      \
349         __typeof__(*(ptr)) __user *__p = (ptr);                         \
350         might_fault();                                                  \
351         access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ?                    \
352                 __put_user((x), __p) :                                  \
353                 -EFAULT;                                                \
354 })
355
356 extern unsigned long __must_check __arch_copy_from_user(void *to, const void __user *from, unsigned long n);
357 extern unsigned long __must_check __arch_copy_to_user(void __user *to, const void *from, unsigned long n);
358 extern unsigned long __must_check __copy_in_user(void __user *to, const void __user *from, unsigned long n);
359 extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
360
361 static inline unsigned long __must_check __copy_from_user(void *to, const void __user *from, unsigned long n)
362 {
363         kasan_check_write(to, n);
364         check_object_size(to, n, false);
365         return __arch_copy_from_user(to, from, n);
366 }
367
368 static inline unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n)
369 {
370         kasan_check_read(from, n);
371         check_object_size(from, n, true);
372         return __arch_copy_to_user(to, from, n);
373 }
374
375 static inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n)
376 {
377         unsigned long res = n;
378         kasan_check_write(to, n);
379         check_object_size(to, n, false);
380
381         if (access_ok(VERIFY_READ, from, n)) {
382                 res = __arch_copy_from_user(to, from, n);
383         }
384         if (unlikely(res))
385                 memset(to + (n - res), 0, res);
386         return res;
387 }
388
389 static inline unsigned long __must_check copy_to_user(void __user *to, const void *from, unsigned long n)
390 {
391         kasan_check_read(from, n);
392         check_object_size(from, n, true);
393
394         if (access_ok(VERIFY_WRITE, to, n)) {
395                 n = __arch_copy_to_user(to, from, n);
396         }
397         return n;
398 }
399
400 static inline unsigned long __must_check copy_in_user(void __user *to, const void __user *from, unsigned long n)
401 {
402         if (access_ok(VERIFY_READ, from, n) && access_ok(VERIFY_WRITE, to, n))
403                 n = __copy_in_user(to, from, n);
404         return n;
405 }
406
407 #define __copy_to_user_inatomic __copy_to_user
408 #define __copy_from_user_inatomic __copy_from_user
409
410 static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
411 {
412         if (access_ok(VERIFY_WRITE, to, n))
413                 n = __clear_user(to, n);
414         return n;
415 }
416
417 extern long strncpy_from_user(char *dest, const char __user *src, long count);
418
419 extern __must_check long strlen_user(const char __user *str);
420 extern __must_check long strnlen_user(const char __user *str, long n);
421
422 #endif /* __ASM_UACCESS_H */