GNU Linux-libre 4.4.285-gnu1
[releases.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/bitops.h>
25 #include <linux/string.h>
26 #include <linux/thread_info.h>
27
28 #include <asm/alternative.h>
29 #include <asm/cpufeature.h>
30 #include <asm/ptrace.h>
31 #include <asm/sysreg.h>
32 #include <asm/errno.h>
33 #include <asm/memory.h>
34 #include <asm/compiler.h>
35
36 #define VERIFY_READ 0
37 #define VERIFY_WRITE 1
38
39 /*
40  * The exception table consists of pairs of addresses: the first is the
41  * address of an instruction that is allowed to fault, and the second is
42  * the address at which the program should continue.  No registers are
43  * modified, so it is entirely up to the continuation code to figure out
44  * 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         unsigned long insn, fixup;
55 };
56
57 extern int fixup_exception(struct pt_regs *regs);
58
59 #define KERNEL_DS       (-1UL)
60 #define get_ds()        (KERNEL_DS)
61
62 #define USER_DS         TASK_SIZE_64
63 #define get_fs()        (current_thread_info()->addr_limit)
64
65 static inline void set_fs(mm_segment_t fs)
66 {
67         current_thread_info()->addr_limit = fs;
68 }
69
70 #define segment_eq(a, b)        ((a) == (b))
71
72 /*
73  * Return 1 if addr < current->addr_limit, 0 otherwise.
74  */
75 #define __addr_ok(addr)                                                 \
76 ({                                                                      \
77         unsigned long flag;                                             \
78         asm("cmp %1, %0; cset %0, lo"                                   \
79                 : "=&r" (flag)                                          \
80                 : "r" (addr), "0" (current_thread_info()->addr_limit)   \
81                 : "cc");                                                \
82         flag;                                                           \
83 })
84
85 /*
86  * Test whether a block of memory is a valid user space address.
87  * Returns 1 if the range is valid, 0 otherwise.
88  *
89  * This is equivalent to the following test:
90  * (u65)addr + (u65)size <= current->addr_limit
91  *
92  * This needs 65-bit arithmetic.
93  */
94 #define __range_ok(addr, size)                                          \
95 ({                                                                      \
96         unsigned long __addr = (unsigned long __force)(addr);           \
97         unsigned long flag, roksum;                                     \
98         __chk_user_ptr(addr);                                           \
99         asm("adds %1, %1, %3; ccmp %1, %4, #2, cc; cset %0, ls"         \
100                 : "=&r" (flag), "=&r" (roksum)                          \
101                 : "1" (__addr), "Ir" (size),                            \
102                   "r" (current_thread_info()->addr_limit)               \
103                 : "cc");                                                \
104         flag;                                                           \
105 })
106
107 /*
108  * When dealing with data aborts, watchpoints, or instruction traps we may end
109  * up with a tagged userland pointer. Clear the tag to get a sane pointer to
110  * pass on to access_ok(), for instance.
111  */
112 #define untagged_addr(addr)             sign_extend64(addr, 55)
113
114 #define access_ok(type, addr, size)     __range_ok(addr, size)
115 #define user_addr_max                   get_fs
116
117 /*
118  * The "__xxx" versions of the user access functions do not verify the address
119  * space - it must have been done previously with a separate "access_ok()"
120  * call.
121  *
122  * The "__xxx_error" versions set the third argument to -EFAULT if an error
123  * occurs, and leave it unchanged on success.
124  */
125 #define __get_user_asm(instr, reg, x, addr, err)                        \
126         asm volatile(                                                   \
127         "1:     " instr "       " reg "1, [%2]\n"                       \
128         "2:\n"                                                          \
129         "       .section .fixup, \"ax\"\n"                              \
130         "       .align  2\n"                                            \
131         "3:     mov     %w0, %3\n"                                      \
132         "       mov     %1, #0\n"                                       \
133         "       b       2b\n"                                           \
134         "       .previous\n"                                            \
135         "       .section __ex_table,\"a\"\n"                            \
136         "       .align  3\n"                                            \
137         "       .quad   1b, 3b\n"                                       \
138         "       .previous"                                              \
139         : "+r" (err), "=&r" (x)                                         \
140         : "r" (addr), "i" (-EFAULT))
141
142 #define __get_user_err(x, ptr, err)                                     \
143 do {                                                                    \
144         unsigned long __gu_val;                                         \
145         __chk_user_ptr(ptr);                                            \
146         asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_HAS_PAN,        \
147                         CONFIG_ARM64_PAN));                             \
148         switch (sizeof(*(ptr))) {                                       \
149         case 1:                                                         \
150                 __get_user_asm("ldrb", "%w", __gu_val, (ptr), (err));   \
151                 break;                                                  \
152         case 2:                                                         \
153                 __get_user_asm("ldrh", "%w", __gu_val, (ptr), (err));   \
154                 break;                                                  \
155         case 4:                                                         \
156                 __get_user_asm("ldr", "%w", __gu_val, (ptr), (err));    \
157                 break;                                                  \
158         case 8:                                                         \
159                 __get_user_asm("ldr", "%",  __gu_val, (ptr), (err));    \
160                 break;                                                  \
161         default:                                                        \
162                 BUILD_BUG();                                            \
163         }                                                               \
164         (x) = (__force __typeof__(*(ptr)))__gu_val;                     \
165         asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_HAS_PAN,        \
166                         CONFIG_ARM64_PAN));                             \
167 } while (0)
168
169 #define __get_user(x, ptr)                                              \
170 ({                                                                      \
171         int __gu_err = 0;                                               \
172         __get_user_err((x), (ptr), __gu_err);                           \
173         __gu_err;                                                       \
174 })
175
176 #define __get_user_error(x, ptr, err)                                   \
177 ({                                                                      \
178         __get_user_err((x), (ptr), (err));                              \
179         (void)0;                                                        \
180 })
181
182 #define __get_user_unaligned __get_user
183
184 #define get_user(x, ptr)                                                \
185 ({                                                                      \
186         __typeof__(*(ptr)) __user *__p = (ptr);                         \
187         might_fault();                                                  \
188         access_ok(VERIFY_READ, __p, sizeof(*__p)) ?                     \
189                 __get_user((x), __p) :                                  \
190                 ((x) = 0, -EFAULT);                                     \
191 })
192
193 #define __put_user_asm(instr, reg, x, addr, err)                        \
194         asm volatile(                                                   \
195         "1:     " instr "       " reg "1, [%2]\n"                       \
196         "2:\n"                                                          \
197         "       .section .fixup,\"ax\"\n"                               \
198         "       .align  2\n"                                            \
199         "3:     mov     %w0, %3\n"                                      \
200         "       b       2b\n"                                           \
201         "       .previous\n"                                            \
202         "       .section __ex_table,\"a\"\n"                            \
203         "       .align  3\n"                                            \
204         "       .quad   1b, 3b\n"                                       \
205         "       .previous"                                              \
206         : "+r" (err)                                                    \
207         : "r" (x), "r" (addr), "i" (-EFAULT))
208
209 #define __put_user_err(x, ptr, err)                                     \
210 do {                                                                    \
211         __typeof__(*(ptr)) __pu_val = (x);                              \
212         __chk_user_ptr(ptr);                                            \
213         asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_HAS_PAN,        \
214                         CONFIG_ARM64_PAN));                             \
215         switch (sizeof(*(ptr))) {                                       \
216         case 1:                                                         \
217                 __put_user_asm("strb", "%w", __pu_val, (ptr), (err));   \
218                 break;                                                  \
219         case 2:                                                         \
220                 __put_user_asm("strh", "%w", __pu_val, (ptr), (err));   \
221                 break;                                                  \
222         case 4:                                                         \
223                 __put_user_asm("str",  "%w", __pu_val, (ptr), (err));   \
224                 break;                                                  \
225         case 8:                                                         \
226                 __put_user_asm("str",  "%", __pu_val, (ptr), (err));    \
227                 break;                                                  \
228         default:                                                        \
229                 BUILD_BUG();                                            \
230         }                                                               \
231         asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_HAS_PAN,        \
232                         CONFIG_ARM64_PAN));                             \
233 } while (0)
234
235 #define __put_user(x, ptr)                                              \
236 ({                                                                      \
237         int __pu_err = 0;                                               \
238         __put_user_err((x), (ptr), __pu_err);                           \
239         __pu_err;                                                       \
240 })
241
242 #define __put_user_error(x, ptr, err)                                   \
243 ({                                                                      \
244         __put_user_err((x), (ptr), (err));                              \
245         (void)0;                                                        \
246 })
247
248 #define __put_user_unaligned __put_user
249
250 #define put_user(x, ptr)                                                \
251 ({                                                                      \
252         __typeof__(*(ptr)) __user *__p = (ptr);                         \
253         might_fault();                                                  \
254         access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ?                    \
255                 __put_user((x), __p) :                                  \
256                 -EFAULT;                                                \
257 })
258
259 extern unsigned long __must_check __copy_from_user(void *to, const void __user *from, unsigned long n);
260 extern unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n);
261 extern unsigned long __must_check __copy_in_user(void __user *to, const void __user *from, unsigned long n);
262 extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
263
264 static inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n)
265 {
266         if (access_ok(VERIFY_READ, from, n))
267                 n = __copy_from_user(to, from, n);
268         else /* security hole - plug it */
269                 memset(to, 0, n);
270         return n;
271 }
272
273 static inline unsigned long __must_check copy_to_user(void __user *to, const void *from, unsigned long n)
274 {
275         if (access_ok(VERIFY_WRITE, to, n))
276                 n = __copy_to_user(to, from, n);
277         return n;
278 }
279
280 static inline unsigned long __must_check copy_in_user(void __user *to, const void __user *from, unsigned long n)
281 {
282         if (access_ok(VERIFY_READ, from, n) && access_ok(VERIFY_WRITE, to, n))
283                 n = __copy_in_user(to, from, n);
284         return n;
285 }
286
287 #define __copy_to_user_inatomic __copy_to_user
288 #define __copy_from_user_inatomic __copy_from_user
289
290 static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
291 {
292         if (access_ok(VERIFY_WRITE, to, n))
293                 n = __clear_user(to, n);
294         return n;
295 }
296
297 extern long strncpy_from_user(char *dest, const char __user *src, long count);
298
299 extern __must_check long strlen_user(const char __user *str);
300 extern __must_check long strnlen_user(const char __user *str, long n);
301
302 #endif /* __ASM_UACCESS_H */