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