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 are intentionally not defined anywhere - if the kernel
54 * code generates any references to them, that's a bug.
56 extern int __get_user_bad(void);
57 extern int __put_user_bad(void);
60 * Note that this is actually 0x1,0000,0000
62 #define KERNEL_DS 0x00000000
63 #define get_ds() (KERNEL_DS)
67 #define USER_DS TASK_SIZE
68 #define get_fs() (current_thread_info()->addr_limit)
70 static inline void set_fs(mm_segment_t fs
)
72 current_thread_info()->addr_limit
= fs
;
73 modify_domain(DOMAIN_KERNEL
, fs
? DOMAIN_CLIENT
: DOMAIN_MANAGER
);
76 #define segment_eq(a, b) ((a) == (b))
78 #define __addr_ok(addr) ({ \
80 __asm__("cmp %2, %0; movlo %0, #0" \
82 : "0" (current_thread_info()->addr_limit), "r" (addr) \
86 /* We use 33-bit arithmetic here... */
87 #define __range_ok(addr, size) ({ \
88 unsigned long flag, roksum; \
89 __chk_user_ptr(addr); \
90 __asm__("adds %1, %2, %3; sbcccs %1, %1, %0; movcc %0, #0" \
91 : "=&r" (flag), "=&r" (roksum) \
92 : "r" (addr), "Ir" (size), "0" (current_thread_info()->addr_limit) \
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
107 extern int __get_user_1(void *);
108 extern int __get_user_2(void *);
109 extern int __get_user_4(void *);
110 extern int __get_user_32t_8(void *);
111 extern int __get_user_8(void *);
112 extern int __get_user_64t_1(void *);
113 extern int __get_user_64t_2(void *);
114 extern int __get_user_64t_4(void *);
116 #define __GUP_CLOBBER_1 "lr", "cc"
117 #ifdef CONFIG_CPU_USE_DOMAINS
118 #define __GUP_CLOBBER_2 "ip", "lr", "cc"
120 #define __GUP_CLOBBER_2 "lr", "cc"
122 #define __GUP_CLOBBER_4 "lr", "cc"
123 #define __GUP_CLOBBER_32t_8 "lr", "cc"
124 #define __GUP_CLOBBER_8 "lr", "cc"
126 #define __get_user_x(__r2, __p, __e, __l, __s) \
127 __asm__ __volatile__ ( \
128 __asmeq("%0", "r0") __asmeq("%1", "r2") \
129 __asmeq("%3", "r1") \
130 "bl __get_user_" #__s \
131 : "=&r" (__e), "=r" (__r2) \
132 : "0" (__p), "r" (__l) \
133 : __GUP_CLOBBER_##__s)
135 /* narrowing a double-word get into a single 32bit word register: */
137 #define __get_user_x_32t(__r2, __p, __e, __l, __s) \
138 __get_user_x(__r2, __p, __e, __l, 32t_8)
140 #define __get_user_x_32t __get_user_x
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:
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)
157 #define __get_user_x_64t __get_user_x
161 #define __get_user_check(x, p) \
163 unsigned long __limit = current_thread_info()->addr_limit - 1; \
164 register const typeof(*(p)) __user *__p asm("r0") = (p);\
165 register typeof(x) __r2 asm("r2"); \
166 register unsigned long __l asm("r1") = __limit; \
167 register int __e asm("r0"); \
168 switch (sizeof(*(__p))) { \
170 if (sizeof((x)) >= 8) \
171 __get_user_x_64t(__r2, __p, __e, __l, 1); \
173 __get_user_x(__r2, __p, __e, __l, 1); \
176 if (sizeof((x)) >= 8) \
177 __get_user_x_64t(__r2, __p, __e, __l, 2); \
179 __get_user_x(__r2, __p, __e, __l, 2); \
182 if (sizeof((x)) >= 8) \
183 __get_user_x_64t(__r2, __p, __e, __l, 4); \
185 __get_user_x(__r2, __p, __e, __l, 4); \
188 if (sizeof((x)) < 8) \
189 __get_user_x_32t(__r2, __p, __e, __l, 4); \
191 __get_user_x(__r2, __p, __e, __l, 8); \
193 default: __e = __get_user_bad(); break; \
195 x = (typeof(*(p))) __r2; \
199 #define get_user(x, p) \
202 __get_user_check(x, p); \
205 extern int __put_user_1(void *, unsigned int);
206 extern int __put_user_2(void *, unsigned int);
207 extern int __put_user_4(void *, unsigned int);
208 extern int __put_user_8(void *, unsigned long long);
210 #define __put_user_x(__r2, __p, __e, __l, __s) \
211 __asm__ __volatile__ ( \
212 __asmeq("%0", "r0") __asmeq("%2", "r2") \
213 __asmeq("%3", "r1") \
214 "bl __put_user_" #__s \
216 : "0" (__p), "r" (__r2), "r" (__l) \
219 #define __put_user_check(x, p) \
221 unsigned long __limit = current_thread_info()->addr_limit - 1; \
222 const typeof(*(p)) __user *__tmp_p = (p); \
223 register const typeof(*(p)) __r2 asm("r2") = (x); \
224 register const typeof(*(p)) __user *__p asm("r0") = __tmp_p; \
225 register unsigned long __l asm("r1") = __limit; \
226 register int __e asm("r0"); \
227 switch (sizeof(*(__p))) { \
229 __put_user_x(__r2, __p, __e, __l, 1); \
232 __put_user_x(__r2, __p, __e, __l, 2); \
235 __put_user_x(__r2, __p, __e, __l, 4); \
238 __put_user_x(__r2, __p, __e, __l, 8); \
240 default: __e = __put_user_bad(); break; \
245 #define put_user(x, p) \
248 __put_user_check(x, p); \
251 #else /* CONFIG_MMU */
254 * uClinux has only one addr space, so has simplified address limits.
256 #define USER_DS KERNEL_DS
258 #define segment_eq(a, b) (1)
259 #define __addr_ok(addr) ((void)(addr), 1)
260 #define __range_ok(addr, size) ((void)(addr), 0)
261 #define get_fs() (KERNEL_DS)
263 static inline void set_fs(mm_segment_t fs
)
267 #define get_user(x, p) __get_user(x, p)
268 #define put_user(x, p) __put_user(x, p)
270 #endif /* CONFIG_MMU */
272 #define access_ok(type, addr, size) (__range_ok(addr, size) == 0)
274 #define user_addr_max() \
275 (segment_eq(get_fs(), KERNEL_DS) ? ~0UL : get_fs())
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.
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).
286 #define __get_user(x, ptr) \
289 __get_user_err((x), (ptr), __gu_err); \
293 #define __get_user_error(x, ptr, err) \
295 __get_user_err((x), (ptr), err); \
299 #define __get_user_err(x, ptr, err) \
301 unsigned long __gu_addr = (unsigned long)(ptr); \
302 unsigned long __gu_val; \
303 __chk_user_ptr(ptr); \
305 switch (sizeof(*(ptr))) { \
306 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; \
309 default: (__gu_val) = __get_user_bad(); \
311 (x) = (__typeof__(*(ptr)))__gu_val; \
314 #define __get_user_asm_byte(x, addr, err) \
315 __asm__ __volatile__( \
316 "1: " TUSER(ldrb) " %1,[%2],#0\n" \
318 " .pushsection .text.fixup,\"ax\"\n" \
324 " .pushsection __ex_table,\"a\"\n" \
328 : "+r" (err), "=&r" (x) \
329 : "r" (addr), "i" (-EFAULT) \
333 #define __get_user_asm_half(x, __gu_addr, err) \
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); \
341 #define __get_user_asm_half(x, __gu_addr, err) \
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; \
350 #define __get_user_asm_word(x, addr, err) \
351 __asm__ __volatile__( \
352 "1: " TUSER(ldr) " %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 __put_user(x, ptr) \
371 __put_user_err((x), (ptr), __pu_err); \
375 #define __put_user_error(x, ptr, err) \
377 __put_user_err((x), (ptr), err); \
381 #define __put_user_err(x, ptr, err) \
383 unsigned long __pu_addr = (unsigned long)(ptr); \
384 __typeof__(*(ptr)) __pu_val = (x); \
385 __chk_user_ptr(ptr); \
387 switch (sizeof(*(ptr))) { \
388 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; \
392 default: __put_user_bad(); \
396 #define __put_user_asm_byte(x, __pu_addr, err) \
397 __asm__ __volatile__( \
398 "1: " TUSER(strb) " %1,[%2],#0\n" \
400 " .pushsection .text.fixup,\"ax\"\n" \
405 " .pushsection __ex_table,\"a\"\n" \
410 : "r" (x), "r" (__pu_addr), "i" (-EFAULT) \
414 #define __put_user_asm_half(x, __pu_addr, err) \
416 unsigned long __temp = (__force unsigned long)(x); \
417 __put_user_asm_byte(__temp, __pu_addr, err); \
418 __put_user_asm_byte(__temp >> 8, __pu_addr + 1, err); \
421 #define __put_user_asm_half(x, __pu_addr, err) \
423 unsigned long __temp = (__force unsigned long)(x); \
424 __put_user_asm_byte(__temp >> 8, __pu_addr, err); \
425 __put_user_asm_byte(__temp, __pu_addr + 1, err); \
429 #define __put_user_asm_word(x, __pu_addr, err) \
430 __asm__ __volatile__( \
431 "1: " TUSER(str) " %1,[%2],#0\n" \
433 " .pushsection .text.fixup,\"ax\"\n" \
438 " .pushsection __ex_table,\"a\"\n" \
443 : "r" (x), "r" (__pu_addr), "i" (-EFAULT) \
447 #define __reg_oper0 "%R2"
448 #define __reg_oper1 "%Q2"
450 #define __reg_oper0 "%Q2"
451 #define __reg_oper1 "%R2"
454 #define __put_user_asm_dword(x, __pu_addr, err) \
455 __asm__ __volatile__( \
456 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" ) \
461 " .pushsection .text.fixup,\"ax\"\n" \
466 " .pushsection __ex_table,\"a\"\n" \
471 : "+r" (err), "+r" (__pu_addr) \
472 : "r" (x), "i" (-EFAULT) \
477 extern unsigned long __must_check
__copy_from_user(void *to
, const void __user
*from
, unsigned long n
);
478 extern unsigned long __must_check
__copy_to_user(void __user
*to
, const void *from
, unsigned long n
);
479 extern unsigned long __must_check
__copy_to_user_std(void __user
*to
, const void *from
, unsigned long n
);
480 extern unsigned long __must_check
__clear_user(void __user
*addr
, unsigned long n
);
481 extern unsigned long __must_check
__clear_user_std(void __user
*addr
, unsigned long n
);
483 #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)
488 static inline unsigned long __must_check
copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
490 if (access_ok(VERIFY_READ
, from
, n
))
491 n
= __copy_from_user(to
, from
, n
);
492 else /* security hole - plug it */
497 static inline unsigned long __must_check
copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
499 if (access_ok(VERIFY_WRITE
, to
, n
))
500 n
= __copy_to_user(to
, from
, n
);
504 #define __copy_to_user_inatomic __copy_to_user
505 #define __copy_from_user_inatomic __copy_from_user
507 static inline unsigned long __must_check
clear_user(void __user
*to
, unsigned long n
)
509 if (access_ok(VERIFY_WRITE
, to
, n
))
510 n
= __clear_user(to
, n
);
514 extern long strncpy_from_user(char *dest
, const char __user
*src
, long count
);
516 extern __must_check
long strlen_user(const char __user
*str
);
517 extern __must_check
long strnlen_user(const char __user
*str
, long n
);
519 #endif /* _ASMARM_UACCESS_H */