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/system.h>
22 #define VERIFY_WRITE 1
25 * The exception table consists of pairs of addresses: the first is the
26 * address of an instruction that is allowed to fault, and the second is
27 * the address at which the program should continue. No registers are
28 * modified, so it is entirely up to the continuation code to figure out
31 * All the routines below use bits of fixup code that are out of line
32 * with the main instruction path. This means when everything is well,
33 * we don't even have to jump over them. Further, they do not intrude
34 * on our cache or tlb entries.
37 struct exception_table_entry
39 unsigned long insn, fixup;
42 extern int fixup_exception(struct pt_regs *regs);
45 * These two are intentionally not defined anywhere - if the kernel
46 * code generates any references to them, that's a bug.
48 extern int __get_user_bad(void);
49 extern int __put_user_bad(void);
52 * Note that this is actually 0x1,0000,0000
54 #define KERNEL_DS 0x00000000
55 #define get_ds() (KERNEL_DS)
59 #define USER_DS TASK_SIZE
60 #define get_fs() (current_thread_info()->addr_limit)
62 static inline void set_fs(mm_segment_t fs)
64 current_thread_info()->addr_limit = fs;
65 modify_domain(DOMAIN_KERNEL, fs ? DOMAIN_CLIENT : DOMAIN_MANAGER);
68 #define segment_eq(a,b) ((a) == (b))
70 #define __addr_ok(addr) ({ \
72 __asm__("cmp %2, %0; movlo %0, #0" \
74 : "0" (current_thread_info()->addr_limit), "r" (addr) \
78 /* We use 33-bit arithmetic here... */
79 #define __range_ok(addr,size) ({ \
80 unsigned long flag, roksum; \
81 __chk_user_ptr(addr); \
82 __asm__("adds %1, %2, %3; sbcccs %1, %1, %0; movcc %0, #0" \
83 : "=&r" (flag), "=&r" (roksum) \
84 : "r" (addr), "Ir" (size), "0" (current_thread_info()->addr_limit) \
89 * Single-value transfer routines. They automatically use the right
90 * size if we just have the right pointer type. Note that the functions
91 * which read from user space (*get_*) need to take care not to leak
92 * kernel data even if the calling code is buggy and fails to check
93 * the return value. This means zeroing out the destination variable
94 * or buffer on error. Normally this is done out of line by the
95 * fixup code, but there are a few places where it intrudes on the
96 * main code path. When we only write to user space, there is no
99 extern int __get_user_1(void *);
100 extern int __get_user_2(void *);
101 extern int __get_user_4(void *);
103 #define __get_user_x(__r2,__p,__e,__s,__i...) \
104 __asm__ __volatile__ ( \
105 __asmeq("%0", "r0") __asmeq("%1", "r2") \
106 "bl __get_user_" #__s \
107 : "=&r" (__e), "=r" (__r2) \
111 #define get_user(x,p) \
113 register const typeof(*(p)) __user *__p asm("r0") = (p);\
114 register unsigned long __r2 asm("r2"); \
115 register int __e asm("r0"); \
116 switch (sizeof(*(__p))) { \
118 __get_user_x(__r2, __p, __e, 1, "lr"); \
121 __get_user_x(__r2, __p, __e, 2, "r3", "lr"); \
124 __get_user_x(__r2, __p, __e, 4, "lr"); \
126 default: __e = __get_user_bad(); break; \
128 x = (typeof(*(p))) __r2; \
132 extern int __put_user_1(void *, unsigned int);
133 extern int __put_user_2(void *, unsigned int);
134 extern int __put_user_4(void *, unsigned int);
135 extern int __put_user_8(void *, unsigned long long);
137 #define __put_user_x(__r2,__p,__e,__s) \
138 __asm__ __volatile__ ( \
139 __asmeq("%0", "r0") __asmeq("%2", "r2") \
140 "bl __put_user_" #__s \
142 : "0" (__p), "r" (__r2) \
145 #define put_user(x,p) \
147 register const typeof(*(p)) __r2 asm("r2") = (x); \
148 register const typeof(*(p)) __user *__p asm("r0") = (p);\
149 register int __e asm("r0"); \
150 switch (sizeof(*(__p))) { \
152 __put_user_x(__r2, __p, __e, 1); \
155 __put_user_x(__r2, __p, __e, 2); \
158 __put_user_x(__r2, __p, __e, 4); \
161 __put_user_x(__r2, __p, __e, 8); \
163 default: __e = __put_user_bad(); break; \
168 #else /* CONFIG_MMU */
171 * uClinux has only one addr space, so has simplified address limits.
173 #define USER_DS KERNEL_DS
175 #define segment_eq(a,b) (1)
176 #define __addr_ok(addr) (1)
177 #define __range_ok(addr,size) (0)
178 #define get_fs() (KERNEL_DS)
180 static inline void set_fs(mm_segment_t fs)
184 #define get_user(x,p) __get_user(x,p)
185 #define put_user(x,p) __put_user(x,p)
187 #endif /* CONFIG_MMU */
189 #define access_ok(type,addr,size) (__range_ok(addr,size) == 0)
192 * The "__xxx" versions of the user access functions do not verify the
193 * address space - it must have been done previously with a separate
194 * "access_ok()" call.
196 * The "xxx_error" versions set the third argument to EFAULT if an
197 * error occurs, and leave it unchanged on success. Note that these
198 * versions are void (ie, don't return a value as such).
200 #define __get_user(x,ptr) \
203 __get_user_err((x),(ptr),__gu_err); \
207 #define __get_user_error(x,ptr,err) \
209 __get_user_err((x),(ptr),err); \
213 #define __get_user_err(x,ptr,err) \
215 unsigned long __gu_addr = (unsigned long)(ptr); \
216 unsigned long __gu_val; \
217 __chk_user_ptr(ptr); \
218 switch (sizeof(*(ptr))) { \
219 case 1: __get_user_asm_byte(__gu_val,__gu_addr,err); break; \
220 case 2: __get_user_asm_half(__gu_val,__gu_addr,err); break; \
221 case 4: __get_user_asm_word(__gu_val,__gu_addr,err); break; \
222 default: (__gu_val) = __get_user_bad(); \
224 (x) = (__typeof__(*(ptr)))__gu_val; \
227 #define __get_user_asm_byte(x,addr,err) \
228 __asm__ __volatile__( \
229 "1: ldrbt %1,[%2]\n" \
231 " .section .fixup,\"ax\"\n" \
237 " .section __ex_table,\"a\"\n" \
241 : "+r" (err), "=&r" (x) \
242 : "r" (addr), "i" (-EFAULT) \
246 #define __get_user_asm_half(x,__gu_addr,err) \
248 unsigned long __b1, __b2; \
249 __get_user_asm_byte(__b1, __gu_addr, err); \
250 __get_user_asm_byte(__b2, __gu_addr + 1, err); \
251 (x) = __b1 | (__b2 << 8); \
254 #define __get_user_asm_half(x,__gu_addr,err) \
256 unsigned long __b1, __b2; \
257 __get_user_asm_byte(__b1, __gu_addr, err); \
258 __get_user_asm_byte(__b2, __gu_addr + 1, err); \
259 (x) = (__b1 << 8) | __b2; \
263 #define __get_user_asm_word(x,addr,err) \
264 __asm__ __volatile__( \
265 "1: ldrt %1,[%2]\n" \
267 " .section .fixup,\"ax\"\n" \
273 " .section __ex_table,\"a\"\n" \
277 : "+r" (err), "=&r" (x) \
278 : "r" (addr), "i" (-EFAULT) \
281 #define __put_user(x,ptr) \
284 __put_user_err((x),(ptr),__pu_err); \
288 #define __put_user_error(x,ptr,err) \
290 __put_user_err((x),(ptr),err); \
294 #define __put_user_err(x,ptr,err) \
296 unsigned long __pu_addr = (unsigned long)(ptr); \
297 __typeof__(*(ptr)) __pu_val = (x); \
298 __chk_user_ptr(ptr); \
299 switch (sizeof(*(ptr))) { \
300 case 1: __put_user_asm_byte(__pu_val,__pu_addr,err); break; \
301 case 2: __put_user_asm_half(__pu_val,__pu_addr,err); break; \
302 case 4: __put_user_asm_word(__pu_val,__pu_addr,err); break; \
303 case 8: __put_user_asm_dword(__pu_val,__pu_addr,err); break; \
304 default: __put_user_bad(); \
308 #define __put_user_asm_byte(x,__pu_addr,err) \
309 __asm__ __volatile__( \
310 "1: strbt %1,[%2]\n" \
312 " .section .fixup,\"ax\"\n" \
317 " .section __ex_table,\"a\"\n" \
322 : "r" (x), "r" (__pu_addr), "i" (-EFAULT) \
326 #define __put_user_asm_half(x,__pu_addr,err) \
328 unsigned long __temp = (unsigned long)(x); \
329 __put_user_asm_byte(__temp, __pu_addr, err); \
330 __put_user_asm_byte(__temp >> 8, __pu_addr + 1, err); \
333 #define __put_user_asm_half(x,__pu_addr,err) \
335 unsigned long __temp = (unsigned long)(x); \
336 __put_user_asm_byte(__temp >> 8, __pu_addr, err); \
337 __put_user_asm_byte(__temp, __pu_addr + 1, err); \
341 #define __put_user_asm_word(x,__pu_addr,err) \
342 __asm__ __volatile__( \
343 "1: strt %1,[%2]\n" \
345 " .section .fixup,\"ax\"\n" \
350 " .section __ex_table,\"a\"\n" \
355 : "r" (x), "r" (__pu_addr), "i" (-EFAULT) \
359 #define __reg_oper0 "%R2"
360 #define __reg_oper1 "%Q2"
362 #define __reg_oper0 "%Q2"
363 #define __reg_oper1 "%R2"
366 #define __put_user_asm_dword(x,__pu_addr,err) \
367 __asm__ __volatile__( \
368 "1: strt " __reg_oper1 ", [%1], #4\n" \
369 "2: strt " __reg_oper0 ", [%1]\n" \
371 " .section .fixup,\"ax\"\n" \
376 " .section __ex_table,\"a\"\n" \
381 : "+r" (err), "+r" (__pu_addr) \
382 : "r" (x), "i" (-EFAULT) \
387 extern unsigned long __must_check __copy_from_user(void *to, const void __user *from, unsigned long n);
388 extern unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n);
389 extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
391 #define __copy_from_user(to,from,n) (memcpy(to, (void __force *)from, n), 0)
392 #define __copy_to_user(to,from,n) (memcpy((void __force *)to, from, n), 0)
393 #define __clear_user(addr,n) (memset((void __force *)addr, 0, n), 0)
396 extern unsigned long __must_check __strncpy_from_user(char *to, const char __user *from, unsigned long count);
397 extern unsigned long __must_check __strnlen_user(const char __user *s, long n);
399 static inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n)
401 if (access_ok(VERIFY_READ, from, n))
402 n = __copy_from_user(to, from, n);
403 else /* security hole - plug it */
408 static inline unsigned long __must_check copy_to_user(void __user *to, const void *from, unsigned long n)
410 if (access_ok(VERIFY_WRITE, to, n))
411 n = __copy_to_user(to, from, n);
415 #define __copy_to_user_inatomic __copy_to_user
416 #define __copy_from_user_inatomic __copy_from_user
418 static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
420 if (access_ok(VERIFY_WRITE, to, n))
421 n = __clear_user(to, n);
425 static inline long __must_check strncpy_from_user(char *dst, const char __user *src, long count)
428 if (access_ok(VERIFY_READ, src, 1))
429 res = __strncpy_from_user(dst, src, count);
433 #define strlen_user(s) strnlen_user(s, ~0UL >> 1)
435 static inline long __must_check strnlen_user(const char __user *s, long n)
437 unsigned long res = 0;
440 res = __strnlen_user(s, n);
445 #endif /* _ASMARM_UACCESS_H */