1 /* $Id: uaccess.h,v 1.11 2003/10/13 07:21:20 lethal Exp $
3 * User space memory access functions
5 * Copyright (C) 1999, 2002 Niibe Yutaka
6 * Copyright (C) 2003 Paul Mundt
9 * MIPS implementation version 1.15 by
10 * Copyright (C) 1996, 1997, 1998 by Ralf Baechle
13 #ifndef __ASM_SH_UACCESS_H
14 #define __ASM_SH_UACCESS_H
16 #include <linux/errno.h>
17 #include <linux/sched.h>
20 #define VERIFY_WRITE 1
23 * The fs value determines whether argument validity checking should be
24 * performed or not. If get_fs() == USER_DS, checking is performed, with
25 * get_fs() == KERNEL_DS, checking is bypassed.
27 * For historical reasons (Data Segment Register?), these macros are misnamed.
30 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
32 #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFFUL)
33 #define USER_DS MAKE_MM_SEG(PAGE_OFFSET)
35 #define segment_eq(a,b) ((a).seg == (b).seg)
37 #define get_ds() (KERNEL_DS)
39 #if !defined(CONFIG_MMU)
40 /* NOMMU is always true */
41 #define __addr_ok(addr) (1)
43 static inline mm_segment_t
get_fs(void)
48 static inline void set_fs(mm_segment_t s
)
53 * __access_ok: Check if address with size is OK or not.
55 * If we don't have an MMU (or if its disabled) the only thing we really have
56 * to look out for is if the address resides somewhere outside of what
57 * available RAM we have.
59 * TODO: This check could probably also stand to be restricted somewhat more..
60 * though it still does the Right Thing(tm) for the time being.
62 static inline int __access_ok(unsigned long addr
, unsigned long size
)
64 return ((addr
>= memory_start
) && ((addr
+ size
) < memory_end
));
66 #else /* CONFIG_MMU */
67 #define __addr_ok(addr) \
68 ((unsigned long)(addr) < (current_thread_info()->addr_limit.seg))
70 #define get_fs() (current_thread_info()->addr_limit)
71 #define set_fs(x) (current_thread_info()->addr_limit = (x))
74 * __access_ok: Check if address with size is OK or not.
77 * (1) is it user space?
78 * (2) addr + size --> carry?
79 * (3) addr + size >= 0x80000000 (PAGE_OFFSET)
81 * (1) (2) (3) | RESULT
91 static inline int __access_ok(unsigned long addr
, unsigned long size
)
93 unsigned long flag
, tmp
;
95 __asm__("stc r7_bank, %0\n\t"
96 "mov.l @(8,%0), %0\n\t"
103 : "=&z" (flag
), "=r" (tmp
)
104 : "r" (addr
), "1" (size
)
109 #endif /* CONFIG_MMU */
111 static inline int access_ok(int type
, const void __user
*p
, unsigned long size
)
113 unsigned long addr
= (unsigned long)p
;
114 return __access_ok(addr
, size
);
118 * Uh, these should become the main single-value transfer routines ...
119 * They automatically use the right size if we just have the right
122 * As SuperH uses the same address space for kernel and user data, we
123 * can just do these as direct assignments.
126 * (a) re-use the arguments for side effects (sizeof is ok)
127 * (b) require any knowledge of processes at this stage
129 #define put_user(x,ptr) __put_user_check((x),(ptr),sizeof(*(ptr)))
130 #define get_user(x,ptr) __get_user_check((x),(ptr),sizeof(*(ptr)))
133 * The "__xxx" versions do not do address space checking, useful when
134 * doing multiple accesses to the same area (the user has to do the
135 * checks by hand with "access_ok()")
137 #define __put_user(x,ptr) \
138 __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
139 #define __get_user(x,ptr) \
140 __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
142 struct __large_struct
{ unsigned long buf
[100]; };
143 #define __m(x) (*(struct __large_struct __user *)(x))
145 #define __get_user_size(x,ptr,size,retval) \
148 __chk_user_ptr(ptr); \
151 __get_user_asm(x, ptr, retval, "b"); \
154 __get_user_asm(x, ptr, retval, "w"); \
157 __get_user_asm(x, ptr, retval, "l"); \
160 __get_user_unknown(); \
165 #define __get_user_nocheck(x,ptr,size) \
167 long __gu_err, __gu_val; \
168 __get_user_size(__gu_val, (ptr), (size), __gu_err); \
169 (x) = (__typeof__(*(ptr)))__gu_val; \
174 #define __get_user_check(x,ptr,size) \
176 long __gu_err, __gu_val; \
177 __chk_user_ptr(ptr); \
180 __get_user_1(__gu_val, (ptr), __gu_err); \
183 __get_user_2(__gu_val, (ptr), __gu_err); \
186 __get_user_4(__gu_val, (ptr), __gu_err); \
189 __get_user_unknown(); \
193 (x) = (__typeof__(*(ptr)))__gu_val; \
197 #define __get_user_1(x,addr,err) ({ \
198 __asm__("stc r7_bank, %1\n\t" \
199 "mov.l @(8,%1), %1\n\t" \
209 "mov.b @%2, %1\n\t" \
212 ".section __ex_table,\"a\"\n\t" \
215 : "=&r" (err), "=&r" (x) \
220 #define __get_user_2(x,addr,err) ({ \
221 __asm__("stc r7_bank, %1\n\t" \
222 "mov.l @(8,%1), %1\n\t" \
232 "mov.w @%2, %1\n\t" \
235 ".section __ex_table,\"a\"\n\t" \
238 : "=&r" (err), "=&r" (x) \
243 #define __get_user_4(x,addr,err) ({ \
244 __asm__("stc r7_bank, %1\n\t" \
245 "mov.l @(8,%1), %1\n\t" \
255 "mov.l @%2, %1\n\t" \
257 ".section __ex_table,\"a\"\n\t" \
260 : "=&r" (err), "=&r" (x) \
264 #else /* CONFIG_MMU */
265 #define __get_user_check(x,ptr,size) \
267 long __gu_err, __gu_val; \
268 if (__access_ok((unsigned long)(ptr), (size))) { \
269 __get_user_size(__gu_val, (ptr), (size), __gu_err); \
270 (x) = (__typeof__(*(ptr)))__gu_val; \
272 __gu_err = -EFAULT; \
277 #define __get_user_asm(x, addr, err, insn) \
279 __asm__ __volatile__( \
281 "mov." insn " %2, %1\n\t" \
284 ".section .fixup,\"ax\"\n" \
292 ".section __ex_table,\"a\"\n\t" \
295 :"=&r" (err), "=&r" (x) \
296 :"m" (__m(addr)), "i" (-EFAULT)); })
298 extern void __get_user_unknown(void);
300 #define __put_user_size(x,ptr,size,retval) \
303 __chk_user_ptr(ptr); \
306 __put_user_asm(x, ptr, retval, "b"); \
309 __put_user_asm(x, ptr, retval, "w"); \
312 __put_user_asm(x, ptr, retval, "l"); \
315 __put_user_u64(x, ptr, retval); \
318 __put_user_unknown(); \
322 #define __put_user_nocheck(x,ptr,size) \
325 __put_user_size((x),(ptr),(size),__pu_err); \
329 #define __put_user_check(x,ptr,size) \
331 long __pu_err = -EFAULT; \
332 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
334 if (__access_ok((unsigned long)__pu_addr,size)) \
335 __put_user_size((x),__pu_addr,(size),__pu_err); \
339 #define __put_user_asm(x, addr, err, insn) \
341 __asm__ __volatile__( \
343 "mov." insn " %1, %2\n\t" \
346 ".section .fixup,\"ax\"\n" \
354 ".section __ex_table,\"a\"\n\t" \
358 :"r" (x), "m" (__m(addr)), "i" (-EFAULT) \
361 #if defined(__LITTLE_ENDIAN__)
362 #define __put_user_u64(val,addr,retval) \
364 __asm__ __volatile__( \
367 "mov.l %S1,%T2\n\t" \
370 ".section .fixup,\"ax\"\n" \
378 ".section __ex_table,\"a\"\n\t" \
382 : "r" (val), "m" (__m(addr)), "i" (-EFAULT) \
385 #define __put_user_u64(val,addr,retval) \
387 __asm__ __volatile__( \
390 "mov.l %R1,%T2\n\t" \
393 ".section .fixup,\"ax\"\n" \
401 ".section __ex_table,\"a\"\n\t" \
405 : "r" (val), "m" (__m(addr)), "i" (-EFAULT) \
409 extern void __put_user_unknown(void);
411 /* Generic arbitrary sized copy. */
412 /* Return the number of bytes NOT copied */
413 __kernel_size_t
__copy_user(void *to
, const void *from
, __kernel_size_t n
);
415 #define copy_to_user(to,from,n) ({ \
416 void *__copy_to = (void *) (to); \
417 __kernel_size_t __copy_size = (__kernel_size_t) (n); \
418 __kernel_size_t __copy_res; \
419 if(__copy_size && __access_ok((unsigned long)__copy_to, __copy_size)) { \
420 __copy_res = __copy_user(__copy_to, (void *) (from), __copy_size); \
421 } else __copy_res = __copy_size; \
424 #define copy_from_user(to,from,n) ({ \
425 void *__copy_to = (void *) (to); \
426 void *__copy_from = (void *) (from); \
427 __kernel_size_t __copy_size = (__kernel_size_t) (n); \
428 __kernel_size_t __copy_res; \
429 if(__copy_size && __access_ok((unsigned long)__copy_from, __copy_size)) { \
430 __copy_res = __copy_user(__copy_to, __copy_from, __copy_size); \
431 } else __copy_res = __copy_size; \
434 static __always_inline
unsigned long
435 __copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
437 return __copy_user(to
, (__force
void *)from
, n
);
440 static __always_inline
unsigned long __must_check
441 __copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
443 return __copy_user((__force
void *)to
, from
, n
);
446 #define __copy_to_user_inatomic __copy_to_user
447 #define __copy_from_user_inatomic __copy_from_user
450 * Clear the area and return remaining number of bytes
451 * (on failure. Usually it's 0.)
453 extern __kernel_size_t
__clear_user(void *addr
, __kernel_size_t size
);
455 #define clear_user(addr,n) ({ \
456 void * __cl_addr = (addr); \
457 unsigned long __cl_size = (n); \
458 if (__cl_size && __access_ok(((unsigned long)(__cl_addr)), __cl_size)) \
459 __cl_size = __clear_user(__cl_addr, __cl_size); \
462 static __inline__
int
463 __strncpy_from_user(unsigned long __dest
, unsigned long __user __src
, int __count
)
466 unsigned long __dummy
, _d
, _s
;
468 __asm__
__volatile__(
481 ".section .fixup,\"ax\"\n"
489 ".section __ex_table,\"a\"\n"
493 : "=r" (res
), "=&z" (__dummy
), "=r" (_s
), "=r" (_d
)
494 : "0" (__count
), "2" (__src
), "3" (__dest
), "r" (__count
),
501 #define strncpy_from_user(dest,src,count) ({ \
502 unsigned long __sfu_src = (unsigned long) (src); \
503 int __sfu_count = (int) (count); \
504 long __sfu_res = -EFAULT; \
505 if(__access_ok(__sfu_src, __sfu_count)) { \
506 __sfu_res = __strncpy_from_user((unsigned long) (dest), __sfu_src, __sfu_count); \
510 * Return the size of a string (including the ending 0!)
512 static __inline__
long __strnlen_user(const char __user
*__s
, long __n
)
515 unsigned long __dummy
;
517 __asm__
__volatile__(
522 "mov.b @(%0,%3), %1\n\t"
527 ".section .fixup,\"ax\"\n"
535 ".section __ex_table,\"a\"\n"
539 : "=z" (res
), "=&r" (__dummy
)
540 : "0" (0), "r" (__s
), "r" (__n
)
545 static __inline__
long strnlen_user(const char __user
*s
, long n
)
550 return __strnlen_user(s
, n
);
553 #define strlen_user(str) strnlen_user(str, ~0UL >> 1)
556 * The exception table consists of pairs of addresses: the first is the
557 * address of an instruction that is allowed to fault, and the second is
558 * the address at which the program should continue. No registers are
559 * modified, so it is entirely up to the continuation code to figure out
562 * All the routines below use bits of fixup code that are out of line
563 * with the main instruction path. This means when everything is well,
564 * we don't even have to jump over them. Further, they do not intrude
565 * on our cache or tlb entries.
568 struct exception_table_entry
570 unsigned long insn
, fixup
;
573 extern int fixup_exception(struct pt_regs
*regs
);
575 #endif /* __ASM_SH_UACCESS_H */