Merge tag 'v4.4.70' into linux-linaro-lsk-v4.4
[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 /*
22  * User space memory access functions
23  */
24 #include <linux/string.h>
25 #include <linux/thread_info.h>
26
27 #include <asm/alternative.h>
28 #include <asm/cpufeature.h>
29 #include <asm/ptrace.h>
30 #include <asm/sysreg.h>
31 #include <asm/errno.h>
32 #include <asm/memory.h>
33 #include <asm/compiler.h>
34
35 #define VERIFY_READ 0
36 #define VERIFY_WRITE 1
37
38 /*
39  * The exception table consists of pairs of relative offsets: the first
40  * is the relative offset to an instruction that is allowed to fault,
41  * and the second is the relative offset at which the program should
42  * continue. No registers are modified, so it is entirely up to the
43  * continuation code to figure out what to do.
44  *
45  * All the routines below use bits of fixup code that are out of line
46  * with the main instruction path.  This means when everything is well,
47  * we don't even have to jump over them.  Further, they do not intrude
48  * on our cache or tlb entries.
49  */
50
51 struct exception_table_entry
52 {
53         int insn, fixup;
54 };
55
56 #define ARCH_HAS_RELATIVE_EXTABLE
57
58 extern int fixup_exception(struct pt_regs *regs);
59
60 #define KERNEL_DS       (-1UL)
61 #define get_ds()        (KERNEL_DS)
62
63 #define USER_DS         TASK_SIZE_64
64 #define get_fs()        (current_thread_info()->addr_limit)
65
66 static inline void set_fs(mm_segment_t fs)
67 {
68         current_thread_info()->addr_limit = fs;
69
70         /*
71          * Enable/disable UAO so that copy_to_user() etc can access
72          * kernel memory with the unprivileged instructions.
73          */
74         if (IS_ENABLED(CONFIG_ARM64_UAO) && fs == KERNEL_DS)
75                 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(1), ARM64_HAS_UAO));
76         else
77                 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(0), ARM64_HAS_UAO,
78                                 CONFIG_ARM64_UAO));
79 }
80
81 #define segment_eq(a, b)        ((a) == (b))
82
83 /*
84  * Return 1 if addr < current->addr_limit, 0 otherwise.
85  */
86 #define __addr_ok(addr)                                                 \
87 ({                                                                      \
88         unsigned long flag;                                             \
89         asm("cmp %1, %0; cset %0, lo"                                   \
90                 : "=&r" (flag)                                          \
91                 : "r" (addr), "0" (current_thread_info()->addr_limit)   \
92                 : "cc");                                                \
93         flag;                                                           \
94 })
95
96 /*
97  * Test whether a block of memory is a valid user space address.
98  * Returns 1 if the range is valid, 0 otherwise.
99  *
100  * This is equivalent to the following test:
101  * (u65)addr + (u65)size <= current->addr_limit
102  *
103  * This needs 65-bit arithmetic.
104  */
105 #define __range_ok(addr, size)                                          \
106 ({                                                                      \
107         unsigned long __addr = (unsigned long __force)(addr);           \
108         unsigned long flag, roksum;                                     \
109         __chk_user_ptr(addr);                                           \
110         asm("adds %1, %1, %3; ccmp %1, %4, #2, cc; cset %0, ls"         \
111                 : "=&r" (flag), "=&r" (roksum)                          \
112                 : "1" (__addr), "Ir" (size),                            \
113                   "r" (current_thread_info()->addr_limit)               \
114                 : "cc");                                                \
115         flag;                                                           \
116 })
117
118 #define access_ok(type, addr, size)     __range_ok(addr, size)
119 #define user_addr_max                   get_fs
120
121 #define _ASM_EXTABLE(from, to)                                          \
122         "       .pushsection    __ex_table, \"a\"\n"                    \
123         "       .align          3\n"                                    \
124         "       .long           (" #from " - .), (" #to " - .)\n"       \
125         "       .popsection\n"
126
127 /*
128  * The "__xxx" versions of the user access functions do not verify the address
129  * space - it must have been done previously with a separate "access_ok()"
130  * call.
131  *
132  * The "__xxx_error" versions set the third argument to -EFAULT if an error
133  * occurs, and leave it unchanged on success.
134  */
135 #define __get_user_asm(instr, alt_instr, reg, x, addr, err, feature)    \
136         asm volatile(                                                   \
137         "1:"ALTERNATIVE(instr "     " reg "1, [%2]\n",                  \
138                         alt_instr " " reg "1, [%2]\n", feature)         \
139         "2:\n"                                                          \
140         "       .section .fixup, \"ax\"\n"                              \
141         "       .align  2\n"                                            \
142         "3:     mov     %w0, %3\n"                                      \
143         "       mov     %1, #0\n"                                       \
144         "       b       2b\n"                                           \
145         "       .previous\n"                                            \
146         _ASM_EXTABLE(1b, 3b)                                            \
147         : "+r" (err), "=&r" (x)                                         \
148         : "r" (addr), "i" (-EFAULT))
149
150 #define __get_user_err(x, ptr, err)                                     \
151 do {                                                                    \
152         unsigned long __gu_val;                                         \
153         __chk_user_ptr(ptr);                                            \
154         asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_ALT_PAN_NOT_UAO,\
155                         CONFIG_ARM64_PAN));                             \
156         switch (sizeof(*(ptr))) {                                       \
157         case 1:                                                         \
158                 __get_user_asm("ldrb", "ldtrb", "%w", __gu_val, (ptr),  \
159                                (err), ARM64_HAS_UAO);                   \
160                 break;                                                  \
161         case 2:                                                         \
162                 __get_user_asm("ldrh", "ldtrh", "%w", __gu_val, (ptr),  \
163                                (err), ARM64_HAS_UAO);                   \
164                 break;                                                  \
165         case 4:                                                         \
166                 __get_user_asm("ldr", "ldtr", "%w", __gu_val, (ptr),    \
167                                (err), ARM64_HAS_UAO);                   \
168                 break;                                                  \
169         case 8:                                                         \
170                 __get_user_asm("ldr", "ldtr", "%",  __gu_val, (ptr),    \
171                                (err), ARM64_HAS_UAO);                   \
172                 break;                                                  \
173         default:                                                        \
174                 BUILD_BUG();                                            \
175         }                                                               \
176         (x) = (__force __typeof__(*(ptr)))__gu_val;                     \
177         asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_ALT_PAN_NOT_UAO,\
178                         CONFIG_ARM64_PAN));                             \
179 } while (0)
180
181 #define __get_user(x, ptr)                                              \
182 ({                                                                      \
183         int __gu_err = 0;                                               \
184         __get_user_err((x), (ptr), __gu_err);                           \
185         __gu_err;                                                       \
186 })
187
188 #define __get_user_error(x, ptr, err)                                   \
189 ({                                                                      \
190         __get_user_err((x), (ptr), (err));                              \
191         (void)0;                                                        \
192 })
193
194 #define __get_user_unaligned __get_user
195
196 #define get_user(x, ptr)                                                \
197 ({                                                                      \
198         __typeof__(*(ptr)) __user *__p = (ptr);                         \
199         might_fault();                                                  \
200         access_ok(VERIFY_READ, __p, sizeof(*__p)) ?                     \
201                 __get_user((x), __p) :                                  \
202                 ((x) = 0, -EFAULT);                                     \
203 })
204
205 #define __put_user_asm(instr, alt_instr, reg, x, addr, err, feature)    \
206         asm volatile(                                                   \
207         "1:"ALTERNATIVE(instr "     " reg "1, [%2]\n",                  \
208                         alt_instr " " reg "1, [%2]\n", feature)         \
209         "2:\n"                                                          \
210         "       .section .fixup,\"ax\"\n"                               \
211         "       .align  2\n"                                            \
212         "3:     mov     %w0, %3\n"                                      \
213         "       b       2b\n"                                           \
214         "       .previous\n"                                            \
215         _ASM_EXTABLE(1b, 3b)                                            \
216         : "+r" (err)                                                    \
217         : "r" (x), "r" (addr), "i" (-EFAULT))
218
219 #define __put_user_err(x, ptr, err)                                     \
220 do {                                                                    \
221         __typeof__(*(ptr)) __pu_val = (x);                              \
222         __chk_user_ptr(ptr);                                            \
223         asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_ALT_PAN_NOT_UAO,\
224                         CONFIG_ARM64_PAN));                             \
225         switch (sizeof(*(ptr))) {                                       \
226         case 1:                                                         \
227                 __put_user_asm("strb", "sttrb", "%w", __pu_val, (ptr),  \
228                                (err), ARM64_HAS_UAO);                   \
229                 break;                                                  \
230         case 2:                                                         \
231                 __put_user_asm("strh", "sttrh", "%w", __pu_val, (ptr),  \
232                                (err), ARM64_HAS_UAO);                   \
233                 break;                                                  \
234         case 4:                                                         \
235                 __put_user_asm("str", "sttr", "%w", __pu_val, (ptr),    \
236                                (err), ARM64_HAS_UAO);                   \
237                 break;                                                  \
238         case 8:                                                         \
239                 __put_user_asm("str", "sttr", "%", __pu_val, (ptr),     \
240                                (err), ARM64_HAS_UAO);                   \
241                 break;                                                  \
242         default:                                                        \
243                 BUILD_BUG();                                            \
244         }                                                               \
245         asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_ALT_PAN_NOT_UAO,\
246                         CONFIG_ARM64_PAN));                             \
247 } while (0)
248
249 #define __put_user(x, ptr)                                              \
250 ({                                                                      \
251         int __pu_err = 0;                                               \
252         __put_user_err((x), (ptr), __pu_err);                           \
253         __pu_err;                                                       \
254 })
255
256 #define __put_user_error(x, ptr, err)                                   \
257 ({                                                                      \
258         __put_user_err((x), (ptr), (err));                              \
259         (void)0;                                                        \
260 })
261
262 #define __put_user_unaligned __put_user
263
264 #define put_user(x, ptr)                                                \
265 ({                                                                      \
266         __typeof__(*(ptr)) __user *__p = (ptr);                         \
267         might_fault();                                                  \
268         access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ?                    \
269                 __put_user((x), __p) :                                  \
270                 -EFAULT;                                                \
271 })
272
273 extern unsigned long __must_check __arch_copy_from_user(void *to, const void __user *from, unsigned long n);
274 extern unsigned long __must_check __arch_copy_to_user(void __user *to, const void *from, unsigned long n);
275 extern unsigned long __must_check __copy_in_user(void __user *to, const void __user *from, unsigned long n);
276 extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
277
278 static inline unsigned long __must_check __copy_from_user(void *to, const void __user *from, unsigned long n)
279 {
280         check_object_size(to, n, false);
281         return __arch_copy_from_user(to, from, n);
282 }
283
284 static inline unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n)
285 {
286         check_object_size(from, n, true);
287         return __arch_copy_to_user(to, from, n);
288 }
289
290 static inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n)
291 {
292         if (access_ok(VERIFY_READ, from, n)) {
293                 check_object_size(to, n, false);
294                 n = __arch_copy_from_user(to, from, n);
295         } else /* security hole - plug it */
296                 memset(to, 0, n);
297         return n;
298 }
299
300 static inline unsigned long __must_check copy_to_user(void __user *to, const void *from, unsigned long n)
301 {
302         if (access_ok(VERIFY_WRITE, to, n)) {
303                 check_object_size(from, n, true);
304                 n = __arch_copy_to_user(to, from, n);
305         }
306         return n;
307 }
308
309 static inline unsigned long __must_check copy_in_user(void __user *to, const void __user *from, unsigned long n)
310 {
311         if (access_ok(VERIFY_READ, from, n) && access_ok(VERIFY_WRITE, to, n))
312                 n = __copy_in_user(to, from, n);
313         return n;
314 }
315
316 #define __copy_to_user_inatomic __copy_to_user
317 #define __copy_from_user_inatomic __copy_from_user
318
319 static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
320 {
321         if (access_ok(VERIFY_WRITE, to, n))
322                 n = __clear_user(to, n);
323         return n;
324 }
325
326 extern long strncpy_from_user(char *dest, const char __user *src, long count);
327
328 extern __must_check long strlen_user(const char __user *str);
329 extern __must_check long strnlen_user(const char __user *str, long n);
330
331 #endif /* __ASM_UACCESS_H */