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_8(void *);
104 extern int __get_user_bad(void);
106 #define __get_user_x(__r2,__p,__e,__s,__i...) \
107 __asm__ __volatile__ ( \
108 __asmeq("%0", "r0") __asmeq("%1", "r2") \
109 "bl __get_user_" #__s \
110 : "=&r" (__e), "=r" (__r2) \
114 #define get_user(x,p) \
116 const register typeof(*(p)) __user *__p asm("r0") = (p);\
117 register typeof(*(p)) __r2 asm("r2"); \
118 register int __e asm("r0"); \
119 switch (sizeof(*(__p))) { \
121 __get_user_x(__r2, __p, __e, 1, "lr"); \
124 __get_user_x(__r2, __p, __e, 2, "r3", "lr"); \
127 __get_user_x(__r2, __p, __e, 4, "lr"); \
130 __get_user_x(__r2, __p, __e, 8, "lr"); \
132 default: __e = __get_user_bad(); break; \
138 #define __get_user(x,ptr) \
141 __get_user_err((x),(ptr),__gu_err); \
145 #define __get_user_error(x,ptr,err) \
147 __get_user_err((x),(ptr),err); \
151 #define __get_user_err(x,ptr,err) \
153 unsigned long __gu_addr = (unsigned long)(ptr); \
154 unsigned long __gu_val; \
155 __chk_user_ptr(ptr); \
156 switch (sizeof(*(ptr))) { \
157 case 1: __get_user_asm_byte(__gu_val,__gu_addr,err); break; \
158 case 2: __get_user_asm_half(__gu_val,__gu_addr,err); break; \
159 case 4: __get_user_asm_word(__gu_val,__gu_addr,err); break; \
160 default: (__gu_val) = __get_user_bad(); \
162 (x) = (__typeof__(*(ptr)))__gu_val; \
165 #define __get_user_asm_byte(x,addr,err) \
166 __asm__ __volatile__( \
167 "1: ldrbt %1,[%2],#0\n" \
169 " .section .fixup,\"ax\"\n" \
175 " .section __ex_table,\"a\"\n" \
179 : "+r" (err), "=&r" (x) \
180 : "r" (addr), "i" (-EFAULT) \
184 #define __get_user_asm_half(x,__gu_addr,err) \
186 unsigned long __b1, __b2; \
187 __get_user_asm_byte(__b1, __gu_addr, err); \
188 __get_user_asm_byte(__b2, __gu_addr + 1, err); \
189 (x) = __b1 | (__b2 << 8); \
192 #define __get_user_asm_half(x,__gu_addr,err) \
194 unsigned long __b1, __b2; \
195 __get_user_asm_byte(__b1, __gu_addr, err); \
196 __get_user_asm_byte(__b2, __gu_addr + 1, err); \
197 (x) = (__b1 << 8) | __b2; \
201 #define __get_user_asm_word(x,addr,err) \
202 __asm__ __volatile__( \
203 "1: ldrt %1,[%2],#0\n" \
205 " .section .fixup,\"ax\"\n" \
211 " .section __ex_table,\"a\"\n" \
215 : "+r" (err), "=&r" (x) \
216 : "r" (addr), "i" (-EFAULT) \
219 extern int __put_user_1(void *, unsigned int);
220 extern int __put_user_2(void *, unsigned int);
221 extern int __put_user_4(void *, unsigned int);
222 extern int __put_user_8(void *, unsigned long long);
223 extern int __put_user_bad(void);
225 #define __put_user_x(__r2,__p,__e,__s) \
226 __asm__ __volatile__ ( \
227 __asmeq("%0", "r0") __asmeq("%2", "r2") \
228 "bl __put_user_" #__s \
230 : "0" (__p), "r" (__r2) \
233 #define put_user(x,p) \
235 const register typeof(*(p)) __r2 asm("r2") = (x); \
236 const register typeof(*(p)) __user *__p asm("r0") = (p);\
237 register int __e asm("r0"); \
238 switch (sizeof(*(__p))) { \
240 __put_user_x(__r2, __p, __e, 1); \
243 __put_user_x(__r2, __p, __e, 2); \
246 __put_user_x(__r2, __p, __e, 4); \
249 __put_user_x(__r2, __p, __e, 8); \
251 default: __e = __put_user_bad(); break; \
256 #define __put_user(x,ptr) \
259 __put_user_err((x),(ptr),__pu_err); \
263 #define __put_user_error(x,ptr,err) \
265 __put_user_err((x),(ptr),err); \
269 #define __put_user_err(x,ptr,err) \
271 unsigned long __pu_addr = (unsigned long)(ptr); \
272 __typeof__(*(ptr)) __pu_val = (x); \
273 __chk_user_ptr(ptr); \
274 switch (sizeof(*(ptr))) { \
275 case 1: __put_user_asm_byte(__pu_val,__pu_addr,err); break; \
276 case 2: __put_user_asm_half(__pu_val,__pu_addr,err); break; \
277 case 4: __put_user_asm_word(__pu_val,__pu_addr,err); break; \
278 case 8: __put_user_asm_dword(__pu_val,__pu_addr,err); break; \
279 default: __put_user_bad(); \
283 #define __put_user_asm_byte(x,__pu_addr,err) \
284 __asm__ __volatile__( \
285 "1: strbt %1,[%2],#0\n" \
287 " .section .fixup,\"ax\"\n" \
292 " .section __ex_table,\"a\"\n" \
297 : "r" (x), "r" (__pu_addr), "i" (-EFAULT) \
301 #define __put_user_asm_half(x,__pu_addr,err) \
303 unsigned long __temp = (unsigned long)(x); \
304 __put_user_asm_byte(__temp, __pu_addr, err); \
305 __put_user_asm_byte(__temp >> 8, __pu_addr + 1, err); \
308 #define __put_user_asm_half(x,__pu_addr,err) \
310 unsigned long __temp = (unsigned long)(x); \
311 __put_user_asm_byte(__temp >> 8, __pu_addr, err); \
312 __put_user_asm_byte(__temp, __pu_addr + 1, err); \
316 #define __put_user_asm_word(x,__pu_addr,err) \
317 __asm__ __volatile__( \
318 "1: strt %1,[%2],#0\n" \
320 " .section .fixup,\"ax\"\n" \
325 " .section __ex_table,\"a\"\n" \
330 : "r" (x), "r" (__pu_addr), "i" (-EFAULT) \
334 #define __reg_oper0 "%R2"
335 #define __reg_oper1 "%Q2"
337 #define __reg_oper0 "%Q2"
338 #define __reg_oper1 "%R2"
341 #define __put_user_asm_dword(x,__pu_addr,err) \
342 __asm__ __volatile__( \
343 "1: strt " __reg_oper1 ", [%1], #4\n" \
344 "2: strt " __reg_oper0 ", [%1], #0\n" \
346 " .section .fixup,\"ax\"\n" \
351 " .section __ex_table,\"a\"\n" \
356 : "+r" (err), "+r" (__pu_addr) \
357 : "r" (x), "i" (-EFAULT) \
360 extern unsigned long __arch_copy_from_user(void *to
, const void __user
*from
, unsigned long n
);
361 extern unsigned long __arch_copy_to_user(void __user
*to
, const void *from
, unsigned long n
);
362 extern unsigned long __arch_clear_user(void __user
*addr
, unsigned long n
);
363 extern unsigned long __arch_strncpy_from_user(char *to
, const char __user
*from
, unsigned long count
);
364 extern unsigned long __arch_strnlen_user(const char __user
*s
, long n
);
366 static inline unsigned long copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
368 if (access_ok(VERIFY_READ
, from
, n
))
369 n
= __arch_copy_from_user(to
, from
, n
);
370 else /* security hole - plug it */
375 static inline unsigned long __copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
377 return __arch_copy_from_user(to
, from
, n
);
380 static inline unsigned long copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
382 if (access_ok(VERIFY_WRITE
, to
, n
))
383 n
= __arch_copy_to_user(to
, from
, n
);
387 static inline unsigned long __copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
389 return __arch_copy_to_user(to
, from
, n
);
392 #define __copy_to_user_inatomic __copy_to_user
393 #define __copy_from_user_inatomic __copy_from_user
395 static inline unsigned long clear_user (void __user
*to
, unsigned long n
)
397 if (access_ok(VERIFY_WRITE
, to
, n
))
398 n
= __arch_clear_user(to
, n
);
402 static inline unsigned long __clear_user (void __user
*to
, unsigned long n
)
404 return __arch_clear_user(to
, n
);
407 static inline long strncpy_from_user (char *dst
, const char __user
*src
, long count
)
410 if (access_ok(VERIFY_READ
, src
, 1))
411 res
= __arch_strncpy_from_user(dst
, src
, count
);
415 static inline long __strncpy_from_user (char *dst
, const char __user
*src
, long count
)
417 return __arch_strncpy_from_user(dst
, src
, count
);
420 #define strlen_user(s) strnlen_user(s, ~0UL >> 1)
422 static inline long strnlen_user(const char __user
*s
, long n
)
424 unsigned long res
= 0;
427 res
= __arch_strnlen_user(s
, n
);
432 #endif /* _ASMARM_UACCESS_H */