Merge tag 'v4.7' into for-linus/pstore
[deliverable/linux.git] / arch / arm64 / include / asm / uaccess.h
CommitLineData
0aea86a2
CM
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
338d4f49
JM
27#include <asm/alternative.h>
28#include <asm/cpufeature.h>
0aea86a2 29#include <asm/ptrace.h>
338d4f49 30#include <asm/sysreg.h>
0aea86a2
CM
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/*
6c94f27a
AB
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.
0aea86a2
CM
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
51struct exception_table_entry
52{
6c94f27a 53 int insn, fixup;
0aea86a2
CM
54};
55
6c94f27a
AB
56#define ARCH_HAS_RELATIVE_EXTABLE
57
0aea86a2
CM
58extern 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
66static inline void set_fs(mm_segment_t fs)
67{
68 current_thread_info()->addr_limit = fs;
57f4959b
JM
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));
0aea86a2
CM
79}
80
967f0e5d 81#define segment_eq(a, b) ((a) == (b))
0aea86a2 82
0aea86a2
CM
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:
31b1e940 88 * (u65)addr + (u65)size <= current->addr_limit
0aea86a2
CM
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); \
31b1e940 96 asm("adds %1, %1, %3; ccmp %1, %4, #2, cc; cset %0, ls" \
0aea86a2
CM
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)
12a0ef7b 105#define user_addr_max get_fs
0aea86a2 106
6c94f27a
AB
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
0aea86a2
CM
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 */
57f4959b 121#define __get_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
0aea86a2 122 asm volatile( \
57f4959b
JM
123 "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
124 alt_instr " " reg "1, [%2]\n", feature) \
0aea86a2
CM
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" \
6c94f27a 132 _ASM_EXTABLE(1b, 3b) \
0aea86a2
CM
133 : "+r" (err), "=&r" (x) \
134 : "r" (addr), "i" (-EFAULT))
135
136#define __get_user_err(x, ptr, err) \
137do { \
138 unsigned long __gu_val; \
139 __chk_user_ptr(ptr); \
70544196 140 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_ALT_PAN_NOT_UAO,\
338d4f49 141 CONFIG_ARM64_PAN)); \
0aea86a2
CM
142 switch (sizeof(*(ptr))) { \
143 case 1: \
57f4959b
JM
144 __get_user_asm("ldrb", "ldtrb", "%w", __gu_val, (ptr), \
145 (err), ARM64_HAS_UAO); \
0aea86a2
CM
146 break; \
147 case 2: \
57f4959b
JM
148 __get_user_asm("ldrh", "ldtrh", "%w", __gu_val, (ptr), \
149 (err), ARM64_HAS_UAO); \
0aea86a2
CM
150 break; \
151 case 4: \
57f4959b
JM
152 __get_user_asm("ldr", "ldtr", "%w", __gu_val, (ptr), \
153 (err), ARM64_HAS_UAO); \
0aea86a2
CM
154 break; \
155 case 8: \
57f4959b
JM
156 __get_user_asm("ldr", "ldtr", "%", __gu_val, (ptr), \
157 (err), ARM64_HAS_UAO); \
0aea86a2
CM
158 break; \
159 default: \
160 BUILD_BUG(); \
161 } \
58fff517 162 (x) = (__force __typeof__(*(ptr)))__gu_val; \
70544196 163 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_ALT_PAN_NOT_UAO,\
338d4f49 164 CONFIG_ARM64_PAN)); \
0aea86a2
CM
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({ \
1f65c13e 184 __typeof__(*(ptr)) __user *__p = (ptr); \
56d2ef78 185 might_fault(); \
1f65c13e
AT
186 access_ok(VERIFY_READ, __p, sizeof(*__p)) ? \
187 __get_user((x), __p) : \
0aea86a2
CM
188 ((x) = 0, -EFAULT); \
189})
190
57f4959b 191#define __put_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
0aea86a2 192 asm volatile( \
57f4959b
JM
193 "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
194 alt_instr " " reg "1, [%2]\n", feature) \
0aea86a2
CM
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" \
6c94f27a 201 _ASM_EXTABLE(1b, 3b) \
0aea86a2
CM
202 : "+r" (err) \
203 : "r" (x), "r" (addr), "i" (-EFAULT))
204
205#define __put_user_err(x, ptr, err) \
206do { \
207 __typeof__(*(ptr)) __pu_val = (x); \
208 __chk_user_ptr(ptr); \
70544196 209 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_ALT_PAN_NOT_UAO,\
338d4f49 210 CONFIG_ARM64_PAN)); \
0aea86a2
CM
211 switch (sizeof(*(ptr))) { \
212 case 1: \
57f4959b
JM
213 __put_user_asm("strb", "sttrb", "%w", __pu_val, (ptr), \
214 (err), ARM64_HAS_UAO); \
0aea86a2
CM
215 break; \
216 case 2: \
57f4959b
JM
217 __put_user_asm("strh", "sttrh", "%w", __pu_val, (ptr), \
218 (err), ARM64_HAS_UAO); \
0aea86a2
CM
219 break; \
220 case 4: \
57f4959b
JM
221 __put_user_asm("str", "sttr", "%w", __pu_val, (ptr), \
222 (err), ARM64_HAS_UAO); \
0aea86a2
CM
223 break; \
224 case 8: \
57f4959b
JM
225 __put_user_asm("str", "sttr", "%", __pu_val, (ptr), \
226 (err), ARM64_HAS_UAO); \
0aea86a2
CM
227 break; \
228 default: \
229 BUILD_BUG(); \
230 } \
70544196 231 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_ALT_PAN_NOT_UAO,\
338d4f49 232 CONFIG_ARM64_PAN)); \
0aea86a2
CM
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({ \
1f65c13e 252 __typeof__(*(ptr)) __user *__p = (ptr); \
56d2ef78 253 might_fault(); \
1f65c13e
AT
254 access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ? \
255 __put_user((x), __p) : \
0aea86a2
CM
256 -EFAULT; \
257})
258
259extern unsigned long __must_check __copy_from_user(void *to, const void __user *from, unsigned long n);
260extern unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n);
261extern unsigned long __must_check __copy_in_user(void __user *to, const void __user *from, unsigned long n);
262extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
263
0aea86a2
CM
264static 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
273static 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
280static 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
290static 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
12a0ef7b 297extern long strncpy_from_user(char *dest, const char __user *src, long count);
0aea86a2 298
12a0ef7b
WD
299extern __must_check long strlen_user(const char __user *str);
300extern __must_check long strnlen_user(const char __user *str, long n);
0aea86a2
CM
301
302#endif /* __ASM_UACCESS_H */
This page took 0.216091 seconds and 5 git commands to generate.