Merge tag 'for-f2fs-4.8' of git://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk...
[cascardo/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 /*
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  * Test whether a block of memory is a valid user space address.
85  * Returns 1 if the range is valid, 0 otherwise.
86  *
87  * This is equivalent to the following test:
88  * (u65)addr + (u65)size <= current->addr_limit
89  *
90  * This needs 65-bit arithmetic.
91  */
92 #define __range_ok(addr, size)                                          \
93 ({                                                                      \
94         unsigned long flag, roksum;                                     \
95         __chk_user_ptr(addr);                                           \
96         asm("adds %1, %1, %3; ccmp %1, %4, #2, cc; cset %0, ls"         \
97                 : "=&r" (flag), "=&r" (roksum)                          \
98                 : "1" (addr), "Ir" (size),                              \
99                   "r" (current_thread_info()->addr_limit)               \
100                 : "cc");                                                \
101         flag;                                                           \
102 })
103
104 #define access_ok(type, addr, size)     __range_ok(addr, size)
105 #define user_addr_max                   get_fs
106
107 #define _ASM_EXTABLE(from, to)                                          \
108         "       .pushsection    __ex_table, \"a\"\n"                    \
109         "       .align          3\n"                                    \
110         "       .long           (" #from " - .), (" #to " - .)\n"       \
111         "       .popsection\n"
112
113 /*
114  * The "__xxx" versions of the user access functions do not verify the address
115  * space - it must have been done previously with a separate "access_ok()"
116  * call.
117  *
118  * The "__xxx_error" versions set the third argument to -EFAULT if an error
119  * occurs, and leave it unchanged on success.
120  */
121 #define __get_user_asm(instr, alt_instr, reg, x, addr, err, feature)    \
122         asm volatile(                                                   \
123         "1:"ALTERNATIVE(instr "     " reg "1, [%2]\n",                  \
124                         alt_instr " " reg "1, [%2]\n", feature)         \
125         "2:\n"                                                          \
126         "       .section .fixup, \"ax\"\n"                              \
127         "       .align  2\n"                                            \
128         "3:     mov     %w0, %3\n"                                      \
129         "       mov     %1, #0\n"                                       \
130         "       b       2b\n"                                           \
131         "       .previous\n"                                            \
132         _ASM_EXTABLE(1b, 3b)                                            \
133         : "+r" (err), "=&r" (x)                                         \
134         : "r" (addr), "i" (-EFAULT))
135
136 #define __get_user_err(x, ptr, err)                                     \
137 do {                                                                    \
138         unsigned long __gu_val;                                         \
139         __chk_user_ptr(ptr);                                            \
140         asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_ALT_PAN_NOT_UAO,\
141                         CONFIG_ARM64_PAN));                             \
142         switch (sizeof(*(ptr))) {                                       \
143         case 1:                                                         \
144                 __get_user_asm("ldrb", "ldtrb", "%w", __gu_val, (ptr),  \
145                                (err), ARM64_HAS_UAO);                   \
146                 break;                                                  \
147         case 2:                                                         \
148                 __get_user_asm("ldrh", "ldtrh", "%w", __gu_val, (ptr),  \
149                                (err), ARM64_HAS_UAO);                   \
150                 break;                                                  \
151         case 4:                                                         \
152                 __get_user_asm("ldr", "ldtr", "%w", __gu_val, (ptr),    \
153                                (err), ARM64_HAS_UAO);                   \
154                 break;                                                  \
155         case 8:                                                         \
156                 __get_user_asm("ldr", "ldtr", "%",  __gu_val, (ptr),    \
157                                (err), ARM64_HAS_UAO);                   \
158                 break;                                                  \
159         default:                                                        \
160                 BUILD_BUG();                                            \
161         }                                                               \
162         (x) = (__force __typeof__(*(ptr)))__gu_val;                     \
163         asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_ALT_PAN_NOT_UAO,\
164                         CONFIG_ARM64_PAN));                             \
165 } while (0)
166
167 #define __get_user(x, ptr)                                              \
168 ({                                                                      \
169         int __gu_err = 0;                                               \
170         __get_user_err((x), (ptr), __gu_err);                           \
171         __gu_err;                                                       \
172 })
173
174 #define __get_user_error(x, ptr, err)                                   \
175 ({                                                                      \
176         __get_user_err((x), (ptr), (err));                              \
177         (void)0;                                                        \
178 })
179
180 #define __get_user_unaligned __get_user
181
182 #define get_user(x, ptr)                                                \
183 ({                                                                      \
184         __typeof__(*(ptr)) __user *__p = (ptr);                         \
185         might_fault();                                                  \
186         access_ok(VERIFY_READ, __p, sizeof(*__p)) ?                     \
187                 __get_user((x), __p) :                                  \
188                 ((x) = 0, -EFAULT);                                     \
189 })
190
191 #define __put_user_asm(instr, alt_instr, reg, x, addr, err, feature)    \
192         asm volatile(                                                   \
193         "1:"ALTERNATIVE(instr "     " reg "1, [%2]\n",                  \
194                         alt_instr " " reg "1, [%2]\n", feature)         \
195         "2:\n"                                                          \
196         "       .section .fixup,\"ax\"\n"                               \
197         "       .align  2\n"                                            \
198         "3:     mov     %w0, %3\n"                                      \
199         "       b       2b\n"                                           \
200         "       .previous\n"                                            \
201         _ASM_EXTABLE(1b, 3b)                                            \
202         : "+r" (err)                                                    \
203         : "r" (x), "r" (addr), "i" (-EFAULT))
204
205 #define __put_user_err(x, ptr, err)                                     \
206 do {                                                                    \
207         __typeof__(*(ptr)) __pu_val = (x);                              \
208         __chk_user_ptr(ptr);                                            \
209         asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_ALT_PAN_NOT_UAO,\
210                         CONFIG_ARM64_PAN));                             \
211         switch (sizeof(*(ptr))) {                                       \
212         case 1:                                                         \
213                 __put_user_asm("strb", "sttrb", "%w", __pu_val, (ptr),  \
214                                (err), ARM64_HAS_UAO);                   \
215                 break;                                                  \
216         case 2:                                                         \
217                 __put_user_asm("strh", "sttrh", "%w", __pu_val, (ptr),  \
218                                (err), ARM64_HAS_UAO);                   \
219                 break;                                                  \
220         case 4:                                                         \
221                 __put_user_asm("str", "sttr", "%w", __pu_val, (ptr),    \
222                                (err), ARM64_HAS_UAO);                   \
223                 break;                                                  \
224         case 8:                                                         \
225                 __put_user_asm("str", "sttr", "%", __pu_val, (ptr),     \
226                                (err), ARM64_HAS_UAO);                   \
227                 break;                                                  \
228         default:                                                        \
229                 BUILD_BUG();                                            \
230         }                                                               \
231         asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_ALT_PAN_NOT_UAO,\
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 */