blob: 92848b00e3cd3969654d32541fe8973457fc5c5e [file] [log] [blame]
Catalin Marinas0aea86a2012-03-05 11:49:32 +00001/*
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
James Morse338d4f42015-07-22 19:05:54 +010027#include <asm/alternative.h>
28#include <asm/cpufeature.h>
Catalin Marinas0aea86a2012-03-05 11:49:32 +000029#include <asm/ptrace.h>
James Morse338d4f42015-07-22 19:05:54 +010030#include <asm/sysreg.h>
Catalin Marinas0aea86a2012-03-05 11:49:32 +000031#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/*
Ard Biesheuvel6c94f272016-01-01 15:02:12 +010039 * 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.
Catalin Marinas0aea86a2012-03-05 11:49:32 +000044 *
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{
Ard Biesheuvel6c94f272016-01-01 15:02:12 +010053 int insn, fixup;
Catalin Marinas0aea86a2012-03-05 11:49:32 +000054};
55
Ard Biesheuvel6c94f272016-01-01 15:02:12 +010056#define ARCH_HAS_RELATIVE_EXTABLE
57
Catalin Marinas0aea86a2012-03-05 11:49:32 +000058extern 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;
James Morse57f49592016-02-05 14:58:48 +000069
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));
Catalin Marinas0aea86a2012-03-05 11:49:32 +000079}
80
Michael S. Tsirkin967f0e52015-01-06 15:11:13 +020081#define segment_eq(a, b) ((a) == (b))
Catalin Marinas0aea86a2012-03-05 11:49:32 +000082
83/*
Catalin Marinas0aea86a2012-03-05 11:49:32 +000084 * 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:
Christopher Covington31b1e942014-03-19 16:29:37 +000088 * (u65)addr + (u65)size <= current->addr_limit
Catalin Marinas0aea86a2012-03-05 11:49:32 +000089 *
90 * This needs 65-bit arithmetic.
91 */
92#define __range_ok(addr, size) \
93({ \
94 unsigned long flag, roksum; \
95 __chk_user_ptr(addr); \
Christopher Covington31b1e942014-03-19 16:29:37 +000096 asm("adds %1, %1, %3; ccmp %1, %4, #2, cc; cset %0, ls" \
Catalin Marinas0aea86a2012-03-05 11:49:32 +000097 : "=&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)
Will Deacon12a0ef72013-11-06 17:20:22 +0000105#define user_addr_max get_fs
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000106
Ard Biesheuvel6c94f272016-01-01 15:02:12 +0100107#define _ASM_EXTABLE(from, to) \
108 " .pushsection __ex_table, \"a\"\n" \
109 " .align 3\n" \
110 " .long (" #from " - .), (" #to " - .)\n" \
111 " .popsection\n"
112
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000113/*
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 */
James Morse57f49592016-02-05 14:58:48 +0000121#define __get_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000122 asm volatile( \
James Morse57f49592016-02-05 14:58:48 +0000123 "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
124 alt_instr " " reg "1, [%2]\n", feature) \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000125 "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" \
Ard Biesheuvel6c94f272016-01-01 15:02:12 +0100132 _ASM_EXTABLE(1b, 3b) \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000133 : "+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); \
James Morse70544192016-02-05 14:58:50 +0000140 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_ALT_PAN_NOT_UAO,\
James Morse338d4f42015-07-22 19:05:54 +0100141 CONFIG_ARM64_PAN)); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000142 switch (sizeof(*(ptr))) { \
143 case 1: \
James Morse57f49592016-02-05 14:58:48 +0000144 __get_user_asm("ldrb", "ldtrb", "%w", __gu_val, (ptr), \
145 (err), ARM64_HAS_UAO); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000146 break; \
147 case 2: \
James Morse57f49592016-02-05 14:58:48 +0000148 __get_user_asm("ldrh", "ldtrh", "%w", __gu_val, (ptr), \
149 (err), ARM64_HAS_UAO); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000150 break; \
151 case 4: \
James Morse57f49592016-02-05 14:58:48 +0000152 __get_user_asm("ldr", "ldtr", "%w", __gu_val, (ptr), \
153 (err), ARM64_HAS_UAO); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000154 break; \
155 case 8: \
James Morse57f49592016-02-05 14:58:48 +0000156 __get_user_asm("ldr", "ldtr", "%", __gu_val, (ptr), \
157 (err), ARM64_HAS_UAO); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000158 break; \
159 default: \
160 BUILD_BUG(); \
161 } \
Michael S. Tsirkin58fff512014-12-12 01:56:04 +0200162 (x) = (__force __typeof__(*(ptr)))__gu_val; \
James Morse70544192016-02-05 14:58:50 +0000163 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_ALT_PAN_NOT_UAO,\
James Morse338d4f42015-07-22 19:05:54 +0100164 CONFIG_ARM64_PAN)); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000165} 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({ \
AKASHI Takahiro1f65c132013-09-24 10:00:50 +0100184 __typeof__(*(ptr)) __user *__p = (ptr); \
Michael S. Tsirkin56d2ef72013-05-26 17:30:42 +0300185 might_fault(); \
AKASHI Takahiro1f65c132013-09-24 10:00:50 +0100186 access_ok(VERIFY_READ, __p, sizeof(*__p)) ? \
187 __get_user((x), __p) : \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000188 ((x) = 0, -EFAULT); \
189})
190
James Morse57f49592016-02-05 14:58:48 +0000191#define __put_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000192 asm volatile( \
James Morse57f49592016-02-05 14:58:48 +0000193 "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
194 alt_instr " " reg "1, [%2]\n", feature) \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000195 "2:\n" \
196 " .section .fixup,\"ax\"\n" \
197 " .align 2\n" \
198 "3: mov %w0, %3\n" \
199 " b 2b\n" \
200 " .previous\n" \
Ard Biesheuvel6c94f272016-01-01 15:02:12 +0100201 _ASM_EXTABLE(1b, 3b) \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000202 : "+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); \
James Morse70544192016-02-05 14:58:50 +0000209 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_ALT_PAN_NOT_UAO,\
James Morse338d4f42015-07-22 19:05:54 +0100210 CONFIG_ARM64_PAN)); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000211 switch (sizeof(*(ptr))) { \
212 case 1: \
James Morse57f49592016-02-05 14:58:48 +0000213 __put_user_asm("strb", "sttrb", "%w", __pu_val, (ptr), \
214 (err), ARM64_HAS_UAO); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000215 break; \
216 case 2: \
James Morse57f49592016-02-05 14:58:48 +0000217 __put_user_asm("strh", "sttrh", "%w", __pu_val, (ptr), \
218 (err), ARM64_HAS_UAO); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000219 break; \
220 case 4: \
James Morse57f49592016-02-05 14:58:48 +0000221 __put_user_asm("str", "sttr", "%w", __pu_val, (ptr), \
222 (err), ARM64_HAS_UAO); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000223 break; \
224 case 8: \
James Morse57f49592016-02-05 14:58:48 +0000225 __put_user_asm("str", "sttr", "%", __pu_val, (ptr), \
226 (err), ARM64_HAS_UAO); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000227 break; \
228 default: \
229 BUILD_BUG(); \
230 } \
James Morse70544192016-02-05 14:58:50 +0000231 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_ALT_PAN_NOT_UAO,\
James Morse338d4f42015-07-22 19:05:54 +0100232 CONFIG_ARM64_PAN)); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000233} 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({ \
AKASHI Takahiro1f65c132013-09-24 10:00:50 +0100252 __typeof__(*(ptr)) __user *__p = (ptr); \
Michael S. Tsirkin56d2ef72013-05-26 17:30:42 +0300253 might_fault(); \
AKASHI Takahiro1f65c132013-09-24 10:00:50 +0100254 access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ? \
255 __put_user((x), __p) : \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000256 -EFAULT; \
257})
258
Kees Cookfaf5b632016-06-23 15:59:42 -0700259extern unsigned long __must_check __arch_copy_from_user(void *to, const void __user *from, unsigned long n);
260extern unsigned long __must_check __arch_copy_to_user(void __user *to, const void *from, unsigned long n);
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000261extern 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
Kees Cookfaf5b632016-06-23 15:59:42 -0700264static inline unsigned long __must_check __copy_from_user(void *to, const void __user *from, unsigned long n)
265{
266 check_object_size(to, n, false);
267 return __arch_copy_from_user(to, from, n);
268}
269
270static inline unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n)
271{
272 check_object_size(from, n, true);
273 return __arch_copy_to_user(to, from, n);
274}
275
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000276static inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n)
277{
Kees Cookfaf5b632016-06-23 15:59:42 -0700278 if (access_ok(VERIFY_READ, from, n)) {
279 check_object_size(to, n, false);
280 n = __arch_copy_from_user(to, from, n);
281 } else /* security hole - plug it */
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000282 memset(to, 0, n);
283 return n;
284}
285
286static inline unsigned long __must_check copy_to_user(void __user *to, const void *from, unsigned long n)
287{
Kees Cookfaf5b632016-06-23 15:59:42 -0700288 if (access_ok(VERIFY_WRITE, to, n)) {
289 check_object_size(from, n, true);
290 n = __arch_copy_to_user(to, from, n);
291 }
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000292 return n;
293}
294
295static inline unsigned long __must_check copy_in_user(void __user *to, const void __user *from, unsigned long n)
296{
297 if (access_ok(VERIFY_READ, from, n) && access_ok(VERIFY_WRITE, to, n))
298 n = __copy_in_user(to, from, n);
299 return n;
300}
301
302#define __copy_to_user_inatomic __copy_to_user
303#define __copy_from_user_inatomic __copy_from_user
304
305static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
306{
307 if (access_ok(VERIFY_WRITE, to, n))
308 n = __clear_user(to, n);
309 return n;
310}
311
Will Deacon12a0ef72013-11-06 17:20:22 +0000312extern long strncpy_from_user(char *dest, const char __user *src, long count);
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000313
Will Deacon12a0ef72013-11-06 17:20:22 +0000314extern __must_check long strlen_user(const char __user *str);
315extern __must_check long strnlen_user(const char __user *str, long n);
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000316
317#endif /* __ASM_UACCESS_H */