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)
81 * Single-value transfer routines. They automatically use the right
82 * size if we just have the right pointer type. Note that the functions
83 * which read from user space (*get_*) need to take care not to leak
84 * kernel data even if the calling code is buggy and fails to check
85 * the return value. This means zeroing out the destination variable
86 * or buffer on error. Normally this is done out of line by the
87 * fixup code, but there are a few places where it intrudes on the
88 * main code path. When we only write to user space, there is no
91 * The "__xxx" versions of the user access functions do not verify the
92 * address space - it must have been done previously with a separate
95 * The "xxx_error" versions set the third argument to EFAULT if an
96 * error occurs, and leave it unchanged on success. Note that these
97 * versions are void (ie, don't return a value as such).
100 extern int __get_user_1(void *);
101 extern int __get_user_2(void *);
102 extern int __get_user_4(void *);
103 extern int __get_user_bad(void);
105 #define __get_user_x(__r2,__p,__e,__s,__i...) \
106 __asm__ __volatile__ ( \
107 __asmeq("%0", "r0") __asmeq("%1", "r2") \
108 "bl __get_user_" #__s \
109 : "=&r" (__e), "=r" (__r2) \
113 #define get_user(x,p) \
115 const register typeof(*(p)) __user *__p asm("r0") = (p);\
116 register unsigned int __r2 asm("r2"); \
117 register int __e asm("r0"); \
118 switch (sizeof(*(__p))) { \
120 __get_user_x(__r2, __p, __e, 1, "lr"); \
123 __get_user_x(__r2, __p, __e, 2, "r3", "lr"); \
126 __get_user_x(__r2, __p, __e, 4, "lr"); \
128 default: __e = __get_user_bad(); break; \
130 x = (typeof(*(p))) __r2; \
134 #define __get_user(x,ptr) \
137 __get_user_err((x),(ptr),__gu_err); \
141 #define __get_user_error(x,ptr,err) \
143 __get_user_err((x),(ptr),err); \
147 #define __get_user_err(x,ptr,err) \
149 unsigned long __gu_addr = (unsigned long)(ptr); \
150 unsigned long __gu_val; \
151 __chk_user_ptr(ptr); \
152 switch (sizeof(*(ptr))) { \
153 case 1: __get_user_asm_byte(__gu_val,__gu_addr,err); break; \
154 case 2: __get_user_asm_half(__gu_val,__gu_addr,err); break; \
155 case 4: __get_user_asm_word(__gu_val,__gu_addr,err); break; \
156 default: (__gu_val) = __get_user_bad(); \
158 (x) = (__typeof__(*(ptr)))__gu_val; \
161 #define __get_user_asm_byte(x,addr,err) \
162 __asm__ __volatile__( \
163 "1: ldrbt %1,[%2],#0\n" \
165 " .section .fixup,\"ax\"\n" \
171 " .section __ex_table,\"a\"\n" \
175 : "+r" (err), "=&r" (x) \
176 : "r" (addr), "i" (-EFAULT) \
180 #define __get_user_asm_half(x,__gu_addr,err) \
182 unsigned long __b1, __b2; \
183 __get_user_asm_byte(__b1, __gu_addr, err); \
184 __get_user_asm_byte(__b2, __gu_addr + 1, err); \
185 (x) = __b1 | (__b2 << 8); \
188 #define __get_user_asm_half(x,__gu_addr,err) \
190 unsigned long __b1, __b2; \
191 __get_user_asm_byte(__b1, __gu_addr, err); \
192 __get_user_asm_byte(__b2, __gu_addr + 1, err); \
193 (x) = (__b1 << 8) | __b2; \
197 #define __get_user_asm_word(x,addr,err) \
198 __asm__ __volatile__( \
199 "1: ldrt %1,[%2],#0\n" \
201 " .section .fixup,\"ax\"\n" \
207 " .section __ex_table,\"a\"\n" \
211 : "+r" (err), "=&r" (x) \
212 : "r" (addr), "i" (-EFAULT) \
215 extern int __put_user_1(void *, unsigned int);
216 extern int __put_user_2(void *, unsigned int);
217 extern int __put_user_4(void *, unsigned int);
218 extern int __put_user_8(void *, unsigned long long);
219 extern int __put_user_bad(void);
221 #define __put_user_x(__r2,__p,__e,__s) \
222 __asm__ __volatile__ ( \
223 __asmeq("%0", "r0") __asmeq("%2", "r2") \
224 "bl __put_user_" #__s \
226 : "0" (__p), "r" (__r2) \
229 #define put_user(x,p) \
231 const register typeof(*(p)) __r2 asm("r2") = (x); \
232 const register typeof(*(p)) __user *__p asm("r0") = (p);\
233 register int __e asm("r0"); \
234 switch (sizeof(*(__p))) { \
236 __put_user_x(__r2, __p, __e, 1); \
239 __put_user_x(__r2, __p, __e, 2); \
242 __put_user_x(__r2, __p, __e, 4); \
245 __put_user_x(__r2, __p, __e, 8); \
247 default: __e = __put_user_bad(); break; \
252 #define __put_user(x,ptr) \
255 __put_user_err((x),(ptr),__pu_err); \
259 #define __put_user_error(x,ptr,err) \
261 __put_user_err((x),(ptr),err); \
265 #define __put_user_err(x,ptr,err) \
267 unsigned long __pu_addr = (unsigned long)(ptr); \
268 __typeof__(*(ptr)) __pu_val = (x); \
269 __chk_user_ptr(ptr); \
270 switch (sizeof(*(ptr))) { \
271 case 1: __put_user_asm_byte(__pu_val,__pu_addr,err); break; \
272 case 2: __put_user_asm_half(__pu_val,__pu_addr,err); break; \
273 case 4: __put_user_asm_word(__pu_val,__pu_addr,err); break; \
274 case 8: __put_user_asm_dword(__pu_val,__pu_addr,err); break; \
275 default: __put_user_bad(); \
279 #define __put_user_asm_byte(x,__pu_addr,err) \
280 __asm__ __volatile__( \
281 "1: strbt %1,[%2],#0\n" \
283 " .section .fixup,\"ax\"\n" \
288 " .section __ex_table,\"a\"\n" \
293 : "r" (x), "r" (__pu_addr), "i" (-EFAULT) \
297 #define __put_user_asm_half(x,__pu_addr,err) \
299 unsigned long __temp = (unsigned long)(x); \
300 __put_user_asm_byte(__temp, __pu_addr, err); \
301 __put_user_asm_byte(__temp >> 8, __pu_addr + 1, err); \
304 #define __put_user_asm_half(x,__pu_addr,err) \
306 unsigned long __temp = (unsigned long)(x); \
307 __put_user_asm_byte(__temp >> 8, __pu_addr, err); \
308 __put_user_asm_byte(__temp, __pu_addr + 1, err); \
312 #define __put_user_asm_word(x,__pu_addr,err) \
313 __asm__ __volatile__( \
314 "1: strt %1,[%2],#0\n" \
316 " .section .fixup,\"ax\"\n" \
321 " .section __ex_table,\"a\"\n" \
326 : "r" (x), "r" (__pu_addr), "i" (-EFAULT) \
330 #define __reg_oper0 "%R2"
331 #define __reg_oper1 "%Q2"
333 #define __reg_oper0 "%Q2"
334 #define __reg_oper1 "%R2"
337 #define __put_user_asm_dword(x,__pu_addr,err) \
338 __asm__ __volatile__( \
339 "1: strt " __reg_oper1 ", [%1], #4\n" \
340 "2: strt " __reg_oper0 ", [%1], #0\n" \
342 " .section .fixup,\"ax\"\n" \
347 " .section __ex_table,\"a\"\n" \
352 : "+r" (err), "+r" (__pu_addr) \
353 : "r" (x), "i" (-EFAULT) \
356 extern unsigned long __arch_copy_from_user(void *to, const void __user *from, unsigned long n);
357 extern unsigned long __arch_copy_to_user(void __user *to, const void *from, unsigned long n);
358 extern unsigned long __arch_clear_user(void __user *addr, unsigned long n);
359 extern unsigned long __arch_strncpy_from_user(char *to, const char __user *from, unsigned long count);
360 extern unsigned long __arch_strnlen_user(const char __user *s, long n);
362 static inline unsigned long copy_from_user(void *to, const void __user *from, unsigned long n)
364 if (access_ok(VERIFY_READ, from, n))
365 n = __arch_copy_from_user(to, from, n);
366 else /* security hole - plug it */
371 static inline unsigned long __copy_from_user(void *to, const void __user *from, unsigned long n)
373 return __arch_copy_from_user(to, from, n);
376 static inline unsigned long copy_to_user(void __user *to, const void *from, unsigned long n)
378 if (access_ok(VERIFY_WRITE, to, n))
379 n = __arch_copy_to_user(to, from, n);
383 static inline unsigned long __copy_to_user(void __user *to, const void *from, unsigned long n)
385 return __arch_copy_to_user(to, from, n);
388 #define __copy_to_user_inatomic __copy_to_user
389 #define __copy_from_user_inatomic __copy_from_user
391 static inline unsigned long clear_user (void __user *to, unsigned long n)
393 if (access_ok(VERIFY_WRITE, to, n))
394 n = __arch_clear_user(to, n);
398 static inline unsigned long __clear_user (void __user *to, unsigned long n)
400 return __arch_clear_user(to, n);
403 static inline long strncpy_from_user (char *dst, const char __user *src, long count)
406 if (access_ok(VERIFY_READ, src, 1))
407 res = __arch_strncpy_from_user(dst, src, count);
411 static inline long __strncpy_from_user (char *dst, const char __user *src, long count)
413 return __arch_strncpy_from_user(dst, src, count);
416 #define strlen_user(s) strnlen_user(s, ~0UL >> 1)
418 static inline long strnlen_user(const char __user *s, long n)
420 unsigned long res = 0;
423 res = __arch_strnlen_user(s, n);
428 #endif /* _ASMARM_UACCESS_H */