2 * arch/arm/include/asm/uaccess.h
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.
8 #ifndef _ASMARM_UACCESS_H
9 #define _ASMARM_UACCESS_H
12 * User space memory access functions
14 #include <linux/string.h>
15 #include <linux/thread_info.h>
16 #include <asm/errno.h>
17 #include <asm/memory.h>
18 #include <asm/domain.h>
19 #include <asm/unified.h>
20 #include <asm/compiler.h>
22 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
23 #include <asm-generic/uaccess-unaligned.h>
25 #define __get_user_unaligned __get_user
26 #define __put_user_unaligned __put_user
30 #define VERIFY_WRITE 1
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
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.
45 struct exception_table_entry
47 unsigned long insn
, fixup
;
50 extern int fixup_exception(struct pt_regs
*regs
);
53 * These two functions allow hooking accesses to userspace to increase
54 * system integrity by ensuring that the kernel can not inadvertantly
55 * perform such accesses (eg, via list poison values) which could then
56 * be exploited for priviledge escalation.
58 static inline unsigned int uaccess_save_and_enable(void)
60 #ifdef CONFIG_CPU_SW_DOMAIN_PAN
61 unsigned int old_domain
= get_domain();
63 /* Set the current domain access to permit user accesses */
64 set_domain((old_domain
& ~domain_mask(DOMAIN_USER
)) |
65 domain_val(DOMAIN_USER
, DOMAIN_CLIENT
));
73 static inline void uaccess_restore(unsigned int flags
)
75 #ifdef CONFIG_CPU_SW_DOMAIN_PAN
76 /* Restore the user access mask */
82 * These two are intentionally not defined anywhere - if the kernel
83 * code generates any references to them, that's a bug.
85 extern int __get_user_bad(void);
86 extern int __put_user_bad(void);
89 * Note that this is actually 0x1,0000,0000
91 #define KERNEL_DS 0x00000000
92 #define get_ds() (KERNEL_DS)
96 #define USER_DS TASK_SIZE
97 #define get_fs() (current_thread_info()->addr_limit)
99 static inline void set_fs(mm_segment_t fs
)
101 current_thread_info()->addr_limit
= fs
;
102 modify_domain(DOMAIN_KERNEL
, fs
? DOMAIN_CLIENT
: DOMAIN_MANAGER
);
105 #define segment_eq(a, b) ((a) == (b))
107 #define __addr_ok(addr) ({ \
108 unsigned long flag; \
109 __asm__("cmp %2, %0; movlo %0, #0" \
111 : "0" (current_thread_info()->addr_limit), "r" (addr) \
115 /* We use 33-bit arithmetic here... */
116 #define __range_ok(addr, size) ({ \
117 unsigned long flag, roksum; \
118 __chk_user_ptr(addr); \
119 __asm__("adds %1, %2, %3; sbcccs %1, %1, %0; movcc %0, #0" \
120 : "=&r" (flag), "=&r" (roksum) \
121 : "r" (addr), "Ir" (size), "0" (current_thread_info()->addr_limit) \
126 * Single-value transfer routines. They automatically use the right
127 * size if we just have the right pointer type. Note that the functions
128 * which read from user space (*get_*) need to take care not to leak
129 * kernel data even if the calling code is buggy and fails to check
130 * the return value. This means zeroing out the destination variable
131 * or buffer on error. Normally this is done out of line by the
132 * fixup code, but there are a few places where it intrudes on the
133 * main code path. When we only write to user space, there is no
136 extern int __get_user_1(void *);
137 extern int __get_user_2(void *);
138 extern int __get_user_4(void *);
139 extern int __get_user_32t_8(void *);
140 extern int __get_user_8(void *);
141 extern int __get_user_64t_1(void *);
142 extern int __get_user_64t_2(void *);
143 extern int __get_user_64t_4(void *);
145 #define __GUP_CLOBBER_1 "lr", "cc"
146 #ifdef CONFIG_CPU_USE_DOMAINS
147 #define __GUP_CLOBBER_2 "ip", "lr", "cc"
149 #define __GUP_CLOBBER_2 "lr", "cc"
151 #define __GUP_CLOBBER_4 "lr", "cc"
152 #define __GUP_CLOBBER_32t_8 "lr", "cc"
153 #define __GUP_CLOBBER_8 "lr", "cc"
155 #define __get_user_x(__r2, __p, __e, __l, __s) \
156 __asm__ __volatile__ ( \
157 __asmeq("%0", "r0") __asmeq("%1", "r2") \
158 __asmeq("%3", "r1") \
159 "bl __get_user_" #__s \
160 : "=&r" (__e), "=r" (__r2) \
161 : "0" (__p), "r" (__l) \
162 : __GUP_CLOBBER_##__s)
164 /* narrowing a double-word get into a single 32bit word register: */
166 #define __get_user_x_32t(__r2, __p, __e, __l, __s) \
167 __get_user_x(__r2, __p, __e, __l, 32t_8)
169 #define __get_user_x_32t __get_user_x
173 * storing result into proper least significant word of 64bit target var,
174 * different only for big endian case where 64 bit __r2 lsw is r3:
177 #define __get_user_x_64t(__r2, __p, __e, __l, __s) \
178 __asm__ __volatile__ ( \
179 __asmeq("%0", "r0") __asmeq("%1", "r2") \
180 __asmeq("%3", "r1") \
181 "bl __get_user_64t_" #__s \
182 : "=&r" (__e), "=r" (__r2) \
183 : "0" (__p), "r" (__l) \
184 : __GUP_CLOBBER_##__s)
186 #define __get_user_x_64t __get_user_x
190 #define __get_user_check(x, p) \
192 unsigned long __limit = current_thread_info()->addr_limit - 1; \
193 register const typeof(*(p)) __user *__p asm("r0") = (p);\
194 register typeof(x) __r2 asm("r2"); \
195 register unsigned long __l asm("r1") = __limit; \
196 register int __e asm("r0"); \
197 unsigned int __ua_flags = uaccess_save_and_enable(); \
198 switch (sizeof(*(__p))) { \
200 if (sizeof((x)) >= 8) \
201 __get_user_x_64t(__r2, __p, __e, __l, 1); \
203 __get_user_x(__r2, __p, __e, __l, 1); \
206 if (sizeof((x)) >= 8) \
207 __get_user_x_64t(__r2, __p, __e, __l, 2); \
209 __get_user_x(__r2, __p, __e, __l, 2); \
212 if (sizeof((x)) >= 8) \
213 __get_user_x_64t(__r2, __p, __e, __l, 4); \
215 __get_user_x(__r2, __p, __e, __l, 4); \
218 if (sizeof((x)) < 8) \
219 __get_user_x_32t(__r2, __p, __e, __l, 4); \
221 __get_user_x(__r2, __p, __e, __l, 8); \
223 default: __e = __get_user_bad(); break; \
225 uaccess_restore(__ua_flags); \
226 x = (typeof(*(p))) __r2; \
230 #define get_user(x, p) \
233 __get_user_check(x, p); \
236 extern int __put_user_1(void *, unsigned int);
237 extern int __put_user_2(void *, unsigned int);
238 extern int __put_user_4(void *, unsigned int);
239 extern int __put_user_8(void *, unsigned long long);
241 #define __put_user_x(__r2, __p, __e, __l, __s) \
242 __asm__ __volatile__ ( \
243 __asmeq("%0", "r0") __asmeq("%2", "r2") \
244 __asmeq("%3", "r1") \
245 "bl __put_user_" #__s \
247 : "0" (__p), "r" (__r2), "r" (__l) \
250 #define __put_user_check(x, p) \
252 unsigned long __limit = current_thread_info()->addr_limit - 1; \
253 const typeof(*(p)) __user *__tmp_p = (p); \
254 register const typeof(*(p)) __r2 asm("r2") = (x); \
255 register const typeof(*(p)) __user *__p asm("r0") = __tmp_p; \
256 register unsigned long __l asm("r1") = __limit; \
257 register int __e asm("r0"); \
258 unsigned int __ua_flags = uaccess_save_and_enable(); \
259 switch (sizeof(*(__p))) { \
261 __put_user_x(__r2, __p, __e, __l, 1); \
264 __put_user_x(__r2, __p, __e, __l, 2); \
267 __put_user_x(__r2, __p, __e, __l, 4); \
270 __put_user_x(__r2, __p, __e, __l, 8); \
272 default: __e = __put_user_bad(); break; \
274 uaccess_restore(__ua_flags); \
278 #define put_user(x, p) \
281 __put_user_check(x, p); \
284 #else /* CONFIG_MMU */
287 * uClinux has only one addr space, so has simplified address limits.
289 #define USER_DS KERNEL_DS
291 #define segment_eq(a, b) (1)
292 #define __addr_ok(addr) ((void)(addr), 1)
293 #define __range_ok(addr, size) ((void)(addr), 0)
294 #define get_fs() (KERNEL_DS)
296 static inline void set_fs(mm_segment_t fs
)
300 #define get_user(x, p) __get_user(x, p)
301 #define put_user(x, p) __put_user(x, p)
303 #endif /* CONFIG_MMU */
305 #define access_ok(type, addr, size) (__range_ok(addr, size) == 0)
307 #define user_addr_max() \
308 (segment_eq(get_fs(), KERNEL_DS) ? ~0UL : get_fs())
311 * The "__xxx" versions of the user access functions do not verify the
312 * address space - it must have been done previously with a separate
313 * "access_ok()" call.
315 * The "xxx_error" versions set the third argument to EFAULT if an
316 * error occurs, and leave it unchanged on success. Note that these
317 * versions are void (ie, don't return a value as such).
319 #define __get_user(x, ptr) \
322 __get_user_err((x), (ptr), __gu_err); \
326 #define __get_user_error(x, ptr, err) \
328 __get_user_err((x), (ptr), err); \
332 #define __get_user_err(x, ptr, err) \
334 unsigned long __gu_addr = (unsigned long)(ptr); \
335 unsigned long __gu_val; \
336 unsigned int __ua_flags; \
337 __chk_user_ptr(ptr); \
339 __ua_flags = uaccess_save_and_enable(); \
340 switch (sizeof(*(ptr))) { \
341 case 1: __get_user_asm_byte(__gu_val, __gu_addr, err); break; \
342 case 2: __get_user_asm_half(__gu_val, __gu_addr, err); break; \
343 case 4: __get_user_asm_word(__gu_val, __gu_addr, err); break; \
344 default: (__gu_val) = __get_user_bad(); \
346 uaccess_restore(__ua_flags); \
347 (x) = (__typeof__(*(ptr)))__gu_val; \
350 #define __get_user_asm(x, addr, err, instr) \
351 __asm__ __volatile__( \
352 "1: " TUSER(instr) " %1, [%2], #0\n" \
354 " .pushsection .text.fixup,\"ax\"\n" \
360 " .pushsection __ex_table,\"a\"\n" \
364 : "+r" (err), "=&r" (x) \
365 : "r" (addr), "i" (-EFAULT) \
368 #define __get_user_asm_byte(x, addr, err) \
369 __get_user_asm(x, addr, err, ldrb)
372 #define __get_user_asm_half(x, __gu_addr, err) \
374 unsigned long __b1, __b2; \
375 __get_user_asm_byte(__b1, __gu_addr, err); \
376 __get_user_asm_byte(__b2, __gu_addr + 1, err); \
377 (x) = __b1 | (__b2 << 8); \
380 #define __get_user_asm_half(x, __gu_addr, err) \
382 unsigned long __b1, __b2; \
383 __get_user_asm_byte(__b1, __gu_addr, err); \
384 __get_user_asm_byte(__b2, __gu_addr + 1, err); \
385 (x) = (__b1 << 8) | __b2; \
389 #define __get_user_asm_word(x, addr, err) \
390 __get_user_asm(x, addr, err, ldr)
392 #define __put_user(x, ptr) \
395 __put_user_err((x), (ptr), __pu_err); \
399 #define __put_user_error(x, ptr, err) \
401 __put_user_err((x), (ptr), err); \
405 #define __put_user_err(x, ptr, err) \
407 unsigned long __pu_addr = (unsigned long)(ptr); \
408 unsigned int __ua_flags; \
409 __typeof__(*(ptr)) __pu_val = (x); \
410 __chk_user_ptr(ptr); \
412 __ua_flags = uaccess_save_and_enable(); \
413 switch (sizeof(*(ptr))) { \
414 case 1: __put_user_asm_byte(__pu_val, __pu_addr, err); break; \
415 case 2: __put_user_asm_half(__pu_val, __pu_addr, err); break; \
416 case 4: __put_user_asm_word(__pu_val, __pu_addr, err); break; \
417 case 8: __put_user_asm_dword(__pu_val, __pu_addr, err); break; \
418 default: __put_user_bad(); \
420 uaccess_restore(__ua_flags); \
423 #define __put_user_asm(x, __pu_addr, err, instr) \
424 __asm__ __volatile__( \
425 "1: " TUSER(instr) " %1, [%2], #0\n" \
427 " .pushsection .text.fixup,\"ax\"\n" \
432 " .pushsection __ex_table,\"a\"\n" \
437 : "r" (x), "r" (__pu_addr), "i" (-EFAULT) \
440 #define __put_user_asm_byte(x, __pu_addr, err) \
441 __put_user_asm(x, __pu_addr, err, strb)
444 #define __put_user_asm_half(x, __pu_addr, err) \
446 unsigned long __temp = (__force unsigned long)(x); \
447 __put_user_asm_byte(__temp, __pu_addr, err); \
448 __put_user_asm_byte(__temp >> 8, __pu_addr + 1, err); \
451 #define __put_user_asm_half(x, __pu_addr, err) \
453 unsigned long __temp = (__force unsigned long)(x); \
454 __put_user_asm_byte(__temp >> 8, __pu_addr, err); \
455 __put_user_asm_byte(__temp, __pu_addr + 1, err); \
459 #define __put_user_asm_word(x, __pu_addr, err) \
460 __put_user_asm(x, __pu_addr, err, str)
463 #define __reg_oper0 "%R2"
464 #define __reg_oper1 "%Q2"
466 #define __reg_oper0 "%Q2"
467 #define __reg_oper1 "%R2"
470 #define __put_user_asm_dword(x, __pu_addr, err) \
471 __asm__ __volatile__( \
472 ARM( "1: " TUSER(str) " " __reg_oper1 ", [%1], #4\n" ) \
473 ARM( "2: " TUSER(str) " " __reg_oper0 ", [%1]\n" ) \
474 THUMB( "1: " TUSER(str) " " __reg_oper1 ", [%1]\n" ) \
475 THUMB( "2: " TUSER(str) " " __reg_oper0 ", [%1, #4]\n" ) \
477 " .pushsection .text.fixup,\"ax\"\n" \
482 " .pushsection __ex_table,\"a\"\n" \
487 : "+r" (err), "+r" (__pu_addr) \
488 : "r" (x), "i" (-EFAULT) \
493 extern unsigned long __must_check
494 arm_copy_from_user(void *to
, const void __user
*from
, unsigned long n
);
496 static inline unsigned long __must_check
497 __copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
499 unsigned int __ua_flags
= uaccess_save_and_enable();
500 n
= arm_copy_from_user(to
, from
, n
);
501 uaccess_restore(__ua_flags
);
505 extern unsigned long __must_check
506 arm_copy_to_user(void __user
*to
, const void *from
, unsigned long n
);
507 extern unsigned long __must_check
508 __copy_to_user_std(void __user
*to
, const void *from
, unsigned long n
);
510 static inline unsigned long __must_check
511 __copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
513 #ifndef CONFIG_UACCESS_WITH_MEMCPY
514 unsigned int __ua_flags
= uaccess_save_and_enable();
515 n
= arm_copy_to_user(to
, from
, n
);
516 uaccess_restore(__ua_flags
);
519 return arm_copy_to_user(to
, from
, n
);
523 extern unsigned long __must_check
524 arm_clear_user(void __user
*addr
, unsigned long n
);
525 extern unsigned long __must_check
526 __clear_user_std(void __user
*addr
, unsigned long n
);
528 static inline unsigned long __must_check
529 __clear_user(void __user
*addr
, unsigned long n
)
531 unsigned int __ua_flags
= uaccess_save_and_enable();
532 n
= arm_clear_user(addr
, n
);
533 uaccess_restore(__ua_flags
);
538 #define __copy_from_user(to, from, n) (memcpy(to, (void __force *)from, n), 0)
539 #define __copy_to_user(to, from, n) (memcpy((void __force *)to, from, n), 0)
540 #define __clear_user(addr, n) (memset((void __force *)addr, 0, n), 0)
543 static inline unsigned long __must_check
copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
545 if (access_ok(VERIFY_READ
, from
, n
))
546 n
= __copy_from_user(to
, from
, n
);
547 else /* security hole - plug it */
552 static inline unsigned long __must_check
copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
554 if (access_ok(VERIFY_WRITE
, to
, n
))
555 n
= __copy_to_user(to
, from
, n
);
559 #define __copy_to_user_inatomic __copy_to_user
560 #define __copy_from_user_inatomic __copy_from_user
562 static inline unsigned long __must_check
clear_user(void __user
*to
, unsigned long n
)
564 if (access_ok(VERIFY_WRITE
, to
, n
))
565 n
= __clear_user(to
, n
);
569 /* These are from lib/ code, and use __get_user() and friends */
570 extern long strncpy_from_user(char *dest
, const char __user
*src
, long count
);
572 extern __must_check
long strlen_user(const char __user
*str
);
573 extern __must_check
long strnlen_user(const char __user
*str
, long n
);
575 #endif /* _ASMARM_UACCESS_H */