blob: ce0786efd26c3485d28baa6b7857e35350c1336d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Russell King4baa9922008-08-02 10:55:55 +01002 * arch/arm/include/asm/uaccess.h
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8#ifndef _ASMARM_UACCESS_H
9#define _ASMARM_UACCESS_H
10
11/*
12 * User space memory access functions
13 */
Russell King87c52572008-11-29 17:35:51 +000014#include <linux/string.h>
15#include <linux/thread_info.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/errno.h>
17#include <asm/memory.h>
18#include <asm/domain.h>
Catalin Marinas8b592782009-07-24 12:32:57 +010019#include <asm/unified.h>
David Howells9f97da72012-03-28 18:30:01 +010020#include <asm/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Nicolas Pitreafdd3bb2014-02-04 13:23:48 +010022#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Nicolas Pitrec89efa72013-09-17 19:16:38 +010023#include <asm-generic/uaccess-unaligned.h>
24#else
25#define __get_user_unaligned __get_user
26#define __put_user_unaligned __put_user
27#endif
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#define VERIFY_READ 0
30#define VERIFY_WRITE 1
31
32/*
33 * The exception table consists of pairs of addresses: the first is the
34 * address of an instruction that is allowed to fault, and the second is
35 * the address at which the program should continue. No registers are
36 * modified, so it is entirely up to the continuation code to figure out
37 * what to do.
38 *
39 * All the routines below use bits of fixup code that are out of line
40 * with the main instruction path. This means when everything is well,
41 * we don't even have to jump over them. Further, they do not intrude
42 * on our cache or tlb entries.
43 */
44
45struct exception_table_entry
46{
47 unsigned long insn, fixup;
48};
49
50extern int fixup_exception(struct pt_regs *regs);
51
52/*
Russell King9641c7c2006-06-21 20:38:17 +010053 * These two are intentionally not defined anywhere - if the kernel
54 * code generates any references to them, that's a bug.
55 */
56extern int __get_user_bad(void);
57extern int __put_user_bad(void);
58
59/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 * Note that this is actually 0x1,0000,0000
61 */
62#define KERNEL_DS 0x00000000
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define get_ds() (KERNEL_DS)
Russell King9641c7c2006-06-21 20:38:17 +010064
65#ifdef CONFIG_MMU
66
67#define USER_DS TASK_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define get_fs() (current_thread_info()->addr_limit)
69
Russell King9641c7c2006-06-21 20:38:17 +010070static inline void set_fs(mm_segment_t fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 current_thread_info()->addr_limit = fs;
73 modify_domain(DOMAIN_KERNEL, fs ? DOMAIN_CLIENT : DOMAIN_MANAGER);
74}
75
Michael S. Tsirkin295bb012015-01-06 15:11:13 +020076#define segment_eq(a, b) ((a) == (b))
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78#define __addr_ok(addr) ({ \
79 unsigned long flag; \
80 __asm__("cmp %2, %0; movlo %0, #0" \
81 : "=&r" (flag) \
82 : "0" (current_thread_info()->addr_limit), "r" (addr) \
83 : "cc"); \
84 (flag == 0); })
85
86/* We use 33-bit arithmetic here... */
Michael S. Tsirkin295bb012015-01-06 15:11:13 +020087#define __range_ok(addr, size) ({ \
Tilman Schmidt16cf5b32007-02-10 01:45:41 -080088 unsigned long flag, roksum; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 __chk_user_ptr(addr); \
90 __asm__("adds %1, %2, %3; sbcccs %1, %1, %0; movcc %0, #0" \
Tilman Schmidt16cf5b32007-02-10 01:45:41 -080091 : "=&r" (flag), "=&r" (roksum) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 : "r" (addr), "Ir" (size), "0" (current_thread_info()->addr_limit) \
93 : "cc"); \
94 flag; })
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/*
97 * Single-value transfer routines. They automatically use the right
98 * size if we just have the right pointer type. Note that the functions
99 * which read from user space (*get_*) need to take care not to leak
100 * kernel data even if the calling code is buggy and fails to check
101 * the return value. This means zeroing out the destination variable
102 * or buffer on error. Normally this is done out of line by the
103 * fixup code, but there are a few places where it intrudes on the
104 * main code path. When we only write to user space, there is no
105 * problem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107extern int __get_user_1(void *);
108extern int __get_user_2(void *);
109extern int __get_user_4(void *);
Victor Kamenskyd9981382014-09-04 06:07:33 +0100110extern int __get_user_32t_8(void *);
Daniel Thompsone38361d2014-07-10 20:58:08 +0100111extern int __get_user_8(void *);
Victor Kamenskyd9981382014-09-04 06:07:33 +0100112extern int __get_user_64t_1(void *);
113extern int __get_user_64t_2(void *);
114extern int __get_user_64t_4(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Russell King84046632012-09-07 18:22:28 +0100116#define __GUP_CLOBBER_1 "lr", "cc"
117#ifdef CONFIG_CPU_USE_DOMAINS
118#define __GUP_CLOBBER_2 "ip", "lr", "cc"
119#else
120#define __GUP_CLOBBER_2 "lr", "cc"
121#endif
122#define __GUP_CLOBBER_4 "lr", "cc"
Victor Kamenskyd9981382014-09-04 06:07:33 +0100123#define __GUP_CLOBBER_32t_8 "lr", "cc"
Daniel Thompsone38361d2014-07-10 20:58:08 +0100124#define __GUP_CLOBBER_8 "lr", "cc"
Russell King84046632012-09-07 18:22:28 +0100125
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200126#define __get_user_x(__r2, __p, __e, __l, __s) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 __asm__ __volatile__ ( \
128 __asmeq("%0", "r0") __asmeq("%1", "r2") \
Russell King84046632012-09-07 18:22:28 +0100129 __asmeq("%3", "r1") \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 "bl __get_user_" #__s \
131 : "=&r" (__e), "=r" (__r2) \
Russell King84046632012-09-07 18:22:28 +0100132 : "0" (__p), "r" (__l) \
133 : __GUP_CLOBBER_##__s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Daniel Thompsone38361d2014-07-10 20:58:08 +0100135/* narrowing a double-word get into a single 32bit word register: */
136#ifdef __ARMEB__
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200137#define __get_user_x_32t(__r2, __p, __e, __l, __s) \
Victor Kamenskyd9981382014-09-04 06:07:33 +0100138 __get_user_x(__r2, __p, __e, __l, 32t_8)
Daniel Thompsone38361d2014-07-10 20:58:08 +0100139#else
Victor Kamenskyd9981382014-09-04 06:07:33 +0100140#define __get_user_x_32t __get_user_x
Daniel Thompsone38361d2014-07-10 20:58:08 +0100141#endif
142
Victor Kamenskyd9981382014-09-04 06:07:33 +0100143/*
144 * storing result into proper least significant word of 64bit target var,
145 * different only for big endian case where 64 bit __r2 lsw is r3:
146 */
147#ifdef __ARMEB__
148#define __get_user_x_64t(__r2, __p, __e, __l, __s) \
149 __asm__ __volatile__ ( \
150 __asmeq("%0", "r0") __asmeq("%1", "r2") \
151 __asmeq("%3", "r1") \
152 "bl __get_user_64t_" #__s \
153 : "=&r" (__e), "=r" (__r2) \
154 : "0" (__p), "r" (__l) \
155 : __GUP_CLOBBER_##__s)
156#else
157#define __get_user_x_64t __get_user_x
158#endif
159
160
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200161#define __get_user_check(x, p) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 ({ \
Russell King84046632012-09-07 18:22:28 +0100163 unsigned long __limit = current_thread_info()->addr_limit - 1; \
Tobias Klauserc5a69d52007-02-17 20:11:19 +0100164 register const typeof(*(p)) __user *__p asm("r0") = (p);\
Daniel Thompsone38361d2014-07-10 20:58:08 +0100165 register typeof(x) __r2 asm("r2"); \
Russell King84046632012-09-07 18:22:28 +0100166 register unsigned long __l asm("r1") = __limit; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 register int __e asm("r0"); \
168 switch (sizeof(*(__p))) { \
169 case 1: \
Victor Kamenskyd9981382014-09-04 06:07:33 +0100170 if (sizeof((x)) >= 8) \
171 __get_user_x_64t(__r2, __p, __e, __l, 1); \
172 else \
173 __get_user_x(__r2, __p, __e, __l, 1); \
Russell King84046632012-09-07 18:22:28 +0100174 break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 case 2: \
Victor Kamenskyd9981382014-09-04 06:07:33 +0100176 if (sizeof((x)) >= 8) \
177 __get_user_x_64t(__r2, __p, __e, __l, 2); \
178 else \
179 __get_user_x(__r2, __p, __e, __l, 2); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 break; \
181 case 4: \
Victor Kamenskyd9981382014-09-04 06:07:33 +0100182 if (sizeof((x)) >= 8) \
183 __get_user_x_64t(__r2, __p, __e, __l, 4); \
184 else \
185 __get_user_x(__r2, __p, __e, __l, 4); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 break; \
Daniel Thompsone38361d2014-07-10 20:58:08 +0100187 case 8: \
188 if (sizeof((x)) < 8) \
Victor Kamenskyd9981382014-09-04 06:07:33 +0100189 __get_user_x_32t(__r2, __p, __e, __l, 4); \
Daniel Thompsone38361d2014-07-10 20:58:08 +0100190 else \
191 __get_user_x(__r2, __p, __e, __l, 8); \
192 break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 default: __e = __get_user_bad(); break; \
194 } \
Russell Kingd2c5b692005-11-18 14:22:03 +0000195 x = (typeof(*(p))) __r2; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 __e; \
197 })
198
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200199#define get_user(x, p) \
Will Deaconad729072012-09-07 18:24:10 +0100200 ({ \
201 might_fault(); \
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200202 __get_user_check(x, p); \
Will Deaconad729072012-09-07 18:24:10 +0100203 })
204
Russell King9641c7c2006-06-21 20:38:17 +0100205extern int __put_user_1(void *, unsigned int);
206extern int __put_user_2(void *, unsigned int);
207extern int __put_user_4(void *, unsigned int);
208extern int __put_user_8(void *, unsigned long long);
209
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200210#define __put_user_x(__r2, __p, __e, __l, __s) \
Russell King9641c7c2006-06-21 20:38:17 +0100211 __asm__ __volatile__ ( \
212 __asmeq("%0", "r0") __asmeq("%2", "r2") \
Russell King84046632012-09-07 18:22:28 +0100213 __asmeq("%3", "r1") \
Russell King9641c7c2006-06-21 20:38:17 +0100214 "bl __put_user_" #__s \
215 : "=&r" (__e) \
Russell King84046632012-09-07 18:22:28 +0100216 : "0" (__p), "r" (__r2), "r" (__l) \
Russell King9641c7c2006-06-21 20:38:17 +0100217 : "ip", "lr", "cc")
218
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200219#define __put_user_check(x, p) \
Russell King9641c7c2006-06-21 20:38:17 +0100220 ({ \
Russell King84046632012-09-07 18:22:28 +0100221 unsigned long __limit = current_thread_info()->addr_limit - 1; \
Andrey Ryabinin537094b62014-05-07 08:07:25 +0100222 const typeof(*(p)) __user *__tmp_p = (p); \
Tobias Klauserc5a69d52007-02-17 20:11:19 +0100223 register const typeof(*(p)) __r2 asm("r2") = (x); \
Andrey Ryabinin537094b62014-05-07 08:07:25 +0100224 register const typeof(*(p)) __user *__p asm("r0") = __tmp_p; \
Russell King84046632012-09-07 18:22:28 +0100225 register unsigned long __l asm("r1") = __limit; \
Russell King9641c7c2006-06-21 20:38:17 +0100226 register int __e asm("r0"); \
227 switch (sizeof(*(__p))) { \
228 case 1: \
Russell King84046632012-09-07 18:22:28 +0100229 __put_user_x(__r2, __p, __e, __l, 1); \
Russell King9641c7c2006-06-21 20:38:17 +0100230 break; \
231 case 2: \
Russell King84046632012-09-07 18:22:28 +0100232 __put_user_x(__r2, __p, __e, __l, 2); \
Russell King9641c7c2006-06-21 20:38:17 +0100233 break; \
234 case 4: \
Russell King84046632012-09-07 18:22:28 +0100235 __put_user_x(__r2, __p, __e, __l, 4); \
Russell King9641c7c2006-06-21 20:38:17 +0100236 break; \
237 case 8: \
Russell King84046632012-09-07 18:22:28 +0100238 __put_user_x(__r2, __p, __e, __l, 8); \
Russell King9641c7c2006-06-21 20:38:17 +0100239 break; \
240 default: __e = __put_user_bad(); break; \
241 } \
242 __e; \
243 })
244
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200245#define put_user(x, p) \
Will Deaconad729072012-09-07 18:24:10 +0100246 ({ \
247 might_fault(); \
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200248 __put_user_check(x, p); \
Will Deaconad729072012-09-07 18:24:10 +0100249 })
250
Russell King9641c7c2006-06-21 20:38:17 +0100251#else /* CONFIG_MMU */
252
253/*
254 * uClinux has only one addr space, so has simplified address limits.
255 */
256#define USER_DS KERNEL_DS
257
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200258#define segment_eq(a, b) (1)
259#define __addr_ok(addr) ((void)(addr), 1)
260#define __range_ok(addr, size) ((void)(addr), 0)
Russell King9641c7c2006-06-21 20:38:17 +0100261#define get_fs() (KERNEL_DS)
262
263static inline void set_fs(mm_segment_t fs)
264{
265}
266
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200267#define get_user(x, p) __get_user(x, p)
268#define put_user(x, p) __put_user(x, p)
Russell King9641c7c2006-06-21 20:38:17 +0100269
270#endif /* CONFIG_MMU */
271
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200272#define access_ok(type, addr, size) (__range_ok(addr, size) == 0)
Russell King9641c7c2006-06-21 20:38:17 +0100273
Will Deacon8c56cc82012-07-06 15:45:39 +0100274#define user_addr_max() \
Uwe Kleine-König83de9112014-06-05 10:23:10 +0200275 (segment_eq(get_fs(), KERNEL_DS) ? ~0UL : get_fs())
Will Deacon8c56cc82012-07-06 15:45:39 +0100276
Russell King9641c7c2006-06-21 20:38:17 +0100277/*
278 * The "__xxx" versions of the user access functions do not verify the
279 * address space - it must have been done previously with a separate
280 * "access_ok()" call.
281 *
282 * The "xxx_error" versions set the third argument to EFAULT if an
283 * error occurs, and leave it unchanged on success. Note that these
284 * versions are void (ie, don't return a value as such).
285 */
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200286#define __get_user(x, ptr) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287({ \
288 long __gu_err = 0; \
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200289 __get_user_err((x), (ptr), __gu_err); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 __gu_err; \
291})
292
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200293#define __get_user_error(x, ptr, err) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294({ \
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200295 __get_user_err((x), (ptr), err); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 (void) 0; \
297})
298
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200299#define __get_user_err(x, ptr, err) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300do { \
301 unsigned long __gu_addr = (unsigned long)(ptr); \
302 unsigned long __gu_val; \
303 __chk_user_ptr(ptr); \
Will Deaconad729072012-09-07 18:24:10 +0100304 might_fault(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 switch (sizeof(*(ptr))) { \
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200306 case 1: __get_user_asm_byte(__gu_val, __gu_addr, err); break; \
307 case 2: __get_user_asm_half(__gu_val, __gu_addr, err); break; \
308 case 4: __get_user_asm_word(__gu_val, __gu_addr, err); break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 default: (__gu_val) = __get_user_bad(); \
310 } \
311 (x) = (__typeof__(*(ptr)))__gu_val; \
312} while (0)
313
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200314#define __get_user_asm_byte(x, addr, err) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 __asm__ __volatile__( \
Catalin Marinas4e7682d2012-01-25 11:38:13 +0100316 "1: " TUSER(ldrb) " %1,[%2],#0\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 "2:\n" \
Russell King42604152010-04-19 10:15:03 +0100318 " .pushsection .fixup,\"ax\"\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 " .align 2\n" \
320 "3: mov %0, %3\n" \
321 " mov %1, #0\n" \
322 " b 2b\n" \
Russell King42604152010-04-19 10:15:03 +0100323 " .popsection\n" \
324 " .pushsection __ex_table,\"a\"\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 " .align 3\n" \
326 " .long 1b, 3b\n" \
Russell King42604152010-04-19 10:15:03 +0100327 " .popsection" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 : "+r" (err), "=&r" (x) \
329 : "r" (addr), "i" (-EFAULT) \
330 : "cc")
331
332#ifndef __ARMEB__
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200333#define __get_user_asm_half(x, __gu_addr, err) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334({ \
335 unsigned long __b1, __b2; \
336 __get_user_asm_byte(__b1, __gu_addr, err); \
337 __get_user_asm_byte(__b2, __gu_addr + 1, err); \
338 (x) = __b1 | (__b2 << 8); \
339})
340#else
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200341#define __get_user_asm_half(x, __gu_addr, err) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342({ \
343 unsigned long __b1, __b2; \
344 __get_user_asm_byte(__b1, __gu_addr, err); \
345 __get_user_asm_byte(__b2, __gu_addr + 1, err); \
346 (x) = (__b1 << 8) | __b2; \
347})
348#endif
349
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200350#define __get_user_asm_word(x, addr, err) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 __asm__ __volatile__( \
Catalin Marinas4e7682d2012-01-25 11:38:13 +0100352 "1: " TUSER(ldr) " %1,[%2],#0\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 "2:\n" \
Russell King42604152010-04-19 10:15:03 +0100354 " .pushsection .fixup,\"ax\"\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 " .align 2\n" \
356 "3: mov %0, %3\n" \
357 " mov %1, #0\n" \
358 " b 2b\n" \
Russell King42604152010-04-19 10:15:03 +0100359 " .popsection\n" \
360 " .pushsection __ex_table,\"a\"\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 " .align 3\n" \
362 " .long 1b, 3b\n" \
Russell King42604152010-04-19 10:15:03 +0100363 " .popsection" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 : "+r" (err), "=&r" (x) \
365 : "r" (addr), "i" (-EFAULT) \
366 : "cc")
367
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200368#define __put_user(x, ptr) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369({ \
370 long __pu_err = 0; \
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200371 __put_user_err((x), (ptr), __pu_err); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 __pu_err; \
373})
374
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200375#define __put_user_error(x, ptr, err) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376({ \
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200377 __put_user_err((x), (ptr), err); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 (void) 0; \
379})
380
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200381#define __put_user_err(x, ptr, err) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382do { \
383 unsigned long __pu_addr = (unsigned long)(ptr); \
384 __typeof__(*(ptr)) __pu_val = (x); \
385 __chk_user_ptr(ptr); \
Will Deaconad729072012-09-07 18:24:10 +0100386 might_fault(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 switch (sizeof(*(ptr))) { \
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200388 case 1: __put_user_asm_byte(__pu_val, __pu_addr, err); break; \
389 case 2: __put_user_asm_half(__pu_val, __pu_addr, err); break; \
390 case 4: __put_user_asm_word(__pu_val, __pu_addr, err); break; \
391 case 8: __put_user_asm_dword(__pu_val, __pu_addr, err); break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 default: __put_user_bad(); \
393 } \
394} while (0)
395
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200396#define __put_user_asm_byte(x, __pu_addr, err) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 __asm__ __volatile__( \
Catalin Marinas4e7682d2012-01-25 11:38:13 +0100398 "1: " TUSER(strb) " %1,[%2],#0\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 "2:\n" \
Russell King42604152010-04-19 10:15:03 +0100400 " .pushsection .fixup,\"ax\"\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 " .align 2\n" \
402 "3: mov %0, %3\n" \
403 " b 2b\n" \
Russell King42604152010-04-19 10:15:03 +0100404 " .popsection\n" \
405 " .pushsection __ex_table,\"a\"\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 " .align 3\n" \
407 " .long 1b, 3b\n" \
Russell King42604152010-04-19 10:15:03 +0100408 " .popsection" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 : "+r" (err) \
410 : "r" (x), "r" (__pu_addr), "i" (-EFAULT) \
411 : "cc")
412
413#ifndef __ARMEB__
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200414#define __put_user_asm_half(x, __pu_addr, err) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415({ \
Michael S. Tsirkine8b94de2015-01-06 14:37:22 +0200416 unsigned long __temp = (__force unsigned long)(x); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 __put_user_asm_byte(__temp, __pu_addr, err); \
418 __put_user_asm_byte(__temp >> 8, __pu_addr + 1, err); \
419})
420#else
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200421#define __put_user_asm_half(x, __pu_addr, err) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422({ \
Michael S. Tsirkine8b94de2015-01-06 14:37:22 +0200423 unsigned long __temp = (__force unsigned long)(x); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 __put_user_asm_byte(__temp >> 8, __pu_addr, err); \
425 __put_user_asm_byte(__temp, __pu_addr + 1, err); \
426})
427#endif
428
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200429#define __put_user_asm_word(x, __pu_addr, err) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 __asm__ __volatile__( \
Catalin Marinas4e7682d2012-01-25 11:38:13 +0100431 "1: " TUSER(str) " %1,[%2],#0\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 "2:\n" \
Russell King42604152010-04-19 10:15:03 +0100433 " .pushsection .fixup,\"ax\"\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 " .align 2\n" \
435 "3: mov %0, %3\n" \
436 " b 2b\n" \
Russell King42604152010-04-19 10:15:03 +0100437 " .popsection\n" \
438 " .pushsection __ex_table,\"a\"\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 " .align 3\n" \
440 " .long 1b, 3b\n" \
Russell King42604152010-04-19 10:15:03 +0100441 " .popsection" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 : "+r" (err) \
443 : "r" (x), "r" (__pu_addr), "i" (-EFAULT) \
444 : "cc")
445
446#ifndef __ARMEB__
447#define __reg_oper0 "%R2"
448#define __reg_oper1 "%Q2"
449#else
450#define __reg_oper0 "%Q2"
451#define __reg_oper1 "%R2"
452#endif
453
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200454#define __put_user_asm_dword(x, __pu_addr, err) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 __asm__ __volatile__( \
Catalin Marinas4e7682d2012-01-25 11:38:13 +0100456 ARM( "1: " TUSER(str) " " __reg_oper1 ", [%1], #4\n" ) \
457 ARM( "2: " TUSER(str) " " __reg_oper0 ", [%1]\n" ) \
458 THUMB( "1: " TUSER(str) " " __reg_oper1 ", [%1]\n" ) \
459 THUMB( "2: " TUSER(str) " " __reg_oper0 ", [%1, #4]\n" ) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 "3:\n" \
Russell King42604152010-04-19 10:15:03 +0100461 " .pushsection .fixup,\"ax\"\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 " .align 2\n" \
463 "4: mov %0, %3\n" \
464 " b 3b\n" \
Russell King42604152010-04-19 10:15:03 +0100465 " .popsection\n" \
466 " .pushsection __ex_table,\"a\"\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 " .align 3\n" \
468 " .long 1b, 4b\n" \
469 " .long 2b, 4b\n" \
Russell King42604152010-04-19 10:15:03 +0100470 " .popsection" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 : "+r" (err), "+r" (__pu_addr) \
472 : "r" (x), "i" (-EFAULT) \
473 : "cc")
474
Russell King02fcb972006-06-21 14:44:52 +0100475
Russell King9641c7c2006-06-21 20:38:17 +0100476#ifdef CONFIG_MMU
Russell King99573292006-10-26 10:27:42 +0100477extern unsigned long __must_check __copy_from_user(void *to, const void __user *from, unsigned long n);
478extern unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n);
Nicolas Pitrea1f98842009-03-08 22:34:45 -0400479extern unsigned long __must_check __copy_to_user_std(void __user *to, const void *from, unsigned long n);
Russell King99573292006-10-26 10:27:42 +0100480extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
Nicolas Pitrea1f98842009-03-08 22:34:45 -0400481extern unsigned long __must_check __clear_user_std(void __user *addr, unsigned long n);
Russell King9641c7c2006-06-21 20:38:17 +0100482#else
Michael S. Tsirkin295bb012015-01-06 15:11:13 +0200483#define __copy_from_user(to, from, n) (memcpy(to, (void __force *)from, n), 0)
484#define __copy_to_user(to, from, n) (memcpy((void __force *)to, from, n), 0)
485#define __clear_user(addr, n) (memset((void __force *)addr, 0, n), 0)
Russell King9641c7c2006-06-21 20:38:17 +0100486#endif
487
Russell King99573292006-10-26 10:27:42 +0100488static inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 if (access_ok(VERIFY_READ, from, n))
Russell King02fcb972006-06-21 14:44:52 +0100491 n = __copy_from_user(to, from, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 else /* security hole - plug it */
Russell King59f0cb02008-10-27 11:24:09 +0000493 memset(to, 0, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return n;
495}
496
Russell King99573292006-10-26 10:27:42 +0100497static inline unsigned long __must_check copy_to_user(void __user *to, const void *from, unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 if (access_ok(VERIFY_WRITE, to, n))
Russell King02fcb972006-06-21 14:44:52 +0100500 n = __copy_to_user(to, from, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return n;
502}
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504#define __copy_to_user_inatomic __copy_to_user
505#define __copy_from_user_inatomic __copy_from_user
506
Russell King99573292006-10-26 10:27:42 +0100507static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 if (access_ok(VERIFY_WRITE, to, n))
Russell King02fcb972006-06-21 14:44:52 +0100510 n = __clear_user(to, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return n;
512}
513
Will Deacon8c56cc82012-07-06 15:45:39 +0100514extern long strncpy_from_user(char *dest, const char __user *src, long count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Will Deacon8c56cc82012-07-06 15:45:39 +0100516extern __must_check long strlen_user(const char __user *str);
517extern __must_check long strnlen_user(const char __user *str, long n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519#endif /* _ASMARM_UACCESS_H */