2 * linux/include/asm-arm/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/sched.h>
15 #include <asm/errno.h>
16 #include <asm/memory.h>
17 #include <asm/domain.h>
18 #include <asm/system.h>
21 #define VERIFY_WRITE 1
24 * The exception table consists of pairs of addresses: the first is the
25 * address of an instruction that is allowed to fault, and the second is
26 * the address at which the program should continue. No registers are
27 * modified, so it is entirely up to the continuation code to figure out
30 * All the routines below use bits of fixup code that are out of line
31 * with the main instruction path. This means when everything is well,
32 * we don't even have to jump over them. Further, they do not intrude
33 * on our cache or tlb entries.
36 struct exception_table_entry
38 unsigned long insn, fixup;
41 extern int fixup_exception(struct pt_regs *regs);
44 * Note that this is actually 0x1,0000,0000
46 #define KERNEL_DS 0x00000000
47 #define USER_DS TASK_SIZE
49 #define get_ds() (KERNEL_DS)
50 #define get_fs() (current_thread_info()->addr_limit)
52 static inline void set_fs (mm_segment_t fs)
54 current_thread_info()->addr_limit = fs;
55 modify_domain(DOMAIN_KERNEL, fs ? DOMAIN_CLIENT : DOMAIN_MANAGER);
58 #define segment_eq(a,b) ((a) == (b))
60 #define __addr_ok(addr) ({ \
62 __asm__("cmp %2, %0; movlo %0, #0" \
64 : "0" (current_thread_info()->addr_limit), "r" (addr) \
68 /* We use 33-bit arithmetic here... */
69 #define __range_ok(addr,size) ({ \
70 unsigned long flag, sum; \
71 __chk_user_ptr(addr); \
72 __asm__("adds %1, %2, %3; sbcccs %1, %1, %0; movcc %0, #0" \
73 : "=&r" (flag), "=&r" (sum) \
74 : "r" (addr), "Ir" (size), "0" (current_thread_info()->addr_limit) \
78 #define access_ok(type,addr,size) (__range_ok(addr,size) == 0)
80 /* this function will go away soon - use access_ok() instead */
81 static inline int __deprecated verify_area(int type, const void __user *addr, unsigned long size)
83 return access_ok(type, addr, size) ? 0 : -EFAULT;
87 * Single-value transfer routines. They automatically use the right
88 * size if we just have the right pointer type. Note that the functions
89 * which read from user space (*get_*) need to take care not to leak
90 * kernel data even if the calling code is buggy and fails to check
91 * the return value. This means zeroing out the destination variable
92 * or buffer on error. Normally this is done out of line by the
93 * fixup code, but there are a few places where it intrudes on the
94 * main code path. When we only write to user space, there is no
97 * The "__xxx" versions of the user access functions do not verify the
98 * address space - it must have been done previously with a separate
101 * The "xxx_error" versions set the third argument to EFAULT if an
102 * error occurs, and leave it unchanged on success. Note that these
103 * versions are void (ie, don't return a value as such).
106 extern int __get_user_1(void *);
107 extern int __get_user_2(void *);
108 extern int __get_user_4(void *);
109 extern int __get_user_8(void *);
110 extern int __get_user_bad(void);
112 #define __get_user_x(__r2,__p,__e,__s,__i...) \
113 __asm__ __volatile__ ( \
114 __asmeq("%0", "r0") __asmeq("%1", "r2") \
115 "bl __get_user_" #__s \
116 : "=&r" (__e), "=r" (__r2) \
120 #define get_user(x,p) \
122 const register typeof(*(p)) __user *__p asm("r0") = (p);\
123 register typeof(*(p)) __r2 asm("r2"); \
124 register int __e asm("r0"); \
125 switch (sizeof(*(__p))) { \
127 __get_user_x(__r2, __p, __e, 1, "lr"); \
130 __get_user_x(__r2, __p, __e, 2, "r3", "lr"); \
133 __get_user_x(__r2, __p, __e, 4, "lr"); \
136 __get_user_x(__r2, __p, __e, 8, "lr"); \
138 default: __e = __get_user_bad(); break; \
144 #define __get_user(x,ptr) \
147 __get_user_err((x),(ptr),__gu_err); \
151 #define __get_user_error(x,ptr,err) \
153 __get_user_err((x),(ptr),err); \
157 #define __get_user_err(x,ptr,err) \
159 unsigned long __gu_addr = (unsigned long)(ptr); \
160 unsigned long __gu_val; \
161 __chk_user_ptr(ptr); \
162 switch (sizeof(*(ptr))) { \
163 case 1: __get_user_asm_byte(__gu_val,__gu_addr,err); break; \
164 case 2: __get_user_asm_half(__gu_val,__gu_addr,err); break; \
165 case 4: __get_user_asm_word(__gu_val,__gu_addr,err); break; \
166 default: (__gu_val) = __get_user_bad(); \
168 (x) = (__typeof__(*(ptr)))__gu_val; \
171 #define __get_user_asm_byte(x,addr,err) \
172 __asm__ __volatile__( \
173 "1: ldrbt %1,[%2],#0\n" \
175 " .section .fixup,\"ax\"\n" \
181 " .section __ex_table,\"a\"\n" \
185 : "+r" (err), "=&r" (x) \
186 : "r" (addr), "i" (-EFAULT) \
190 #define __get_user_asm_half(x,__gu_addr,err) \
192 unsigned long __b1, __b2; \
193 __get_user_asm_byte(__b1, __gu_addr, err); \
194 __get_user_asm_byte(__b2, __gu_addr + 1, err); \
195 (x) = __b1 | (__b2 << 8); \
198 #define __get_user_asm_half(x,__gu_addr,err) \
200 unsigned long __b1, __b2; \
201 __get_user_asm_byte(__b1, __gu_addr, err); \
202 __get_user_asm_byte(__b2, __gu_addr + 1, err); \
203 (x) = (__b1 << 8) | __b2; \
207 #define __get_user_asm_word(x,addr,err) \
208 __asm__ __volatile__( \
209 "1: ldrt %1,[%2],#0\n" \
211 " .section .fixup,\"ax\"\n" \
217 " .section __ex_table,\"a\"\n" \
221 : "+r" (err), "=&r" (x) \
222 : "r" (addr), "i" (-EFAULT) \
225 extern int __put_user_1(void *, unsigned int);
226 extern int __put_user_2(void *, unsigned int);
227 extern int __put_user_4(void *, unsigned int);
228 extern int __put_user_8(void *, unsigned long long);
229 extern int __put_user_bad(void);
231 #define __put_user_x(__r2,__p,__e,__s) \
232 __asm__ __volatile__ ( \
233 __asmeq("%0", "r0") __asmeq("%2", "r2") \
234 "bl __put_user_" #__s \
236 : "0" (__p), "r" (__r2) \
239 #define put_user(x,p) \
241 const register typeof(*(p)) __r2 asm("r2") = (x); \
242 const register typeof(*(p)) __user *__p asm("r0") = (p);\
243 register int __e asm("r0"); \
244 switch (sizeof(*(__p))) { \
246 __put_user_x(__r2, __p, __e, 1); \
249 __put_user_x(__r2, __p, __e, 2); \
252 __put_user_x(__r2, __p, __e, 4); \
255 __put_user_x(__r2, __p, __e, 8); \
257 default: __e = __put_user_bad(); break; \
262 #define __put_user(x,ptr) \
265 __put_user_err((x),(ptr),__pu_err); \
269 #define __put_user_error(x,ptr,err) \
271 __put_user_err((x),(ptr),err); \
275 #define __put_user_err(x,ptr,err) \
277 unsigned long __pu_addr = (unsigned long)(ptr); \
278 __typeof__(*(ptr)) __pu_val = (x); \
279 __chk_user_ptr(ptr); \
280 switch (sizeof(*(ptr))) { \
281 case 1: __put_user_asm_byte(__pu_val,__pu_addr,err); break; \
282 case 2: __put_user_asm_half(__pu_val,__pu_addr,err); break; \
283 case 4: __put_user_asm_word(__pu_val,__pu_addr,err); break; \
284 case 8: __put_user_asm_dword(__pu_val,__pu_addr,err); break; \
285 default: __put_user_bad(); \
289 #define __put_user_asm_byte(x,__pu_addr,err) \
290 __asm__ __volatile__( \
291 "1: strbt %1,[%2],#0\n" \
293 " .section .fixup,\"ax\"\n" \
298 " .section __ex_table,\"a\"\n" \
303 : "r" (x), "r" (__pu_addr), "i" (-EFAULT) \
307 #define __put_user_asm_half(x,__pu_addr,err) \
309 unsigned long __temp = (unsigned long)(x); \
310 __put_user_asm_byte(__temp, __pu_addr, err); \
311 __put_user_asm_byte(__temp >> 8, __pu_addr + 1, err); \
314 #define __put_user_asm_half(x,__pu_addr,err) \
316 unsigned long __temp = (unsigned long)(x); \
317 __put_user_asm_byte(__temp >> 8, __pu_addr, err); \
318 __put_user_asm_byte(__temp, __pu_addr + 1, err); \
322 #define __put_user_asm_word(x,__pu_addr,err) \
323 __asm__ __volatile__( \
324 "1: strt %1,[%2],#0\n" \
326 " .section .fixup,\"ax\"\n" \
331 " .section __ex_table,\"a\"\n" \
336 : "r" (x), "r" (__pu_addr), "i" (-EFAULT) \
340 #define __reg_oper0 "%R2"
341 #define __reg_oper1 "%Q2"
343 #define __reg_oper0 "%Q2"
344 #define __reg_oper1 "%R2"
347 #define __put_user_asm_dword(x,__pu_addr,err) \
348 __asm__ __volatile__( \
349 "1: strt " __reg_oper1 ", [%1], #4\n" \
350 "2: strt " __reg_oper0 ", [%1], #0\n" \
352 " .section .fixup,\"ax\"\n" \
357 " .section __ex_table,\"a\"\n" \
362 : "+r" (err), "+r" (__pu_addr) \
363 : "r" (x), "i" (-EFAULT) \
366 extern unsigned long __arch_copy_from_user(void *to, const void __user *from, unsigned long n);
367 extern unsigned long __arch_copy_to_user(void __user *to, const void *from, unsigned long n);
368 extern unsigned long __arch_clear_user(void __user *addr, unsigned long n);
369 extern unsigned long __arch_strncpy_from_user(char *to, const char __user *from, unsigned long count);
370 extern unsigned long __arch_strnlen_user(const char __user *s, long n);
372 static inline unsigned long copy_from_user(void *to, const void __user *from, unsigned long n)
374 if (access_ok(VERIFY_READ, from, n))
375 n = __arch_copy_from_user(to, from, n);
376 else /* security hole - plug it */
381 static inline unsigned long __copy_from_user(void *to, const void __user *from, unsigned long n)
383 return __arch_copy_from_user(to, from, n);
386 static inline unsigned long copy_to_user(void __user *to, const void *from, unsigned long n)
388 if (access_ok(VERIFY_WRITE, to, n))
389 n = __arch_copy_to_user(to, from, n);
393 static inline unsigned long __copy_to_user(void __user *to, const void *from, unsigned long n)
395 return __arch_copy_to_user(to, from, n);
398 #define __copy_to_user_inatomic __copy_to_user
399 #define __copy_from_user_inatomic __copy_from_user
401 static inline unsigned long clear_user (void __user *to, unsigned long n)
403 if (access_ok(VERIFY_WRITE, to, n))
404 n = __arch_clear_user(to, n);
408 static inline unsigned long __clear_user (void __user *to, unsigned long n)
410 return __arch_clear_user(to, n);
413 static inline long strncpy_from_user (char *dst, const char __user *src, long count)
416 if (access_ok(VERIFY_READ, src, 1))
417 res = __arch_strncpy_from_user(dst, src, count);
421 static inline long __strncpy_from_user (char *dst, const char __user *src, long count)
423 return __arch_strncpy_from_user(dst, src, count);
426 #define strlen_user(s) strnlen_user(s, ~0UL >> 1)
428 static inline long strnlen_user(const char __user *s, long n)
430 unsigned long res = 0;
433 res = __arch_strnlen_user(s, n);
438 #endif /* _ASMARM_UACCESS_H */