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 * NOTE: Macro/functions in this file depends on threads_info.h implementation.
24 * USER_ADDR_LIMIT == 0x80000000
28 #define VERIFY_WRITE 1
31 unsigned int is_user_space
;
35 * The fs value determines whether argument validity checking should be
36 * performed or not. If get_fs() == USER_DS, checking is performed, with
37 * get_fs() == KERNEL_DS, checking is bypassed.
39 * For historical reasons (Data Segment Register?), these macros are misnamed.
42 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
43 #define segment_eq(a,b) ((a).is_user_space == (b).is_user_space)
45 #define USER_ADDR_LIMIT 0x80000000
47 #define KERNEL_DS MAKE_MM_SEG(0)
48 #define USER_DS MAKE_MM_SEG(1)
50 #define get_ds() (KERNEL_DS)
52 #if !defined(CONFIG_MMU)
53 static inline mm_segment_t
get_fs(void)
58 static inline void set_fs(mm_segment_t s
)
63 * __access_ok: Check if address with size is OK or not.
65 * If we don't have an MMU (or if its disabled) the only thing we really have
66 * to look out for is if the address resides somewhere outside of what
67 * available RAM we have.
69 * TODO: This check could probably also stand to be restricted somewhat more..
70 * though it still does the Right Thing(tm) for the time being.
72 static inline int __access_ok(unsigned long addr
, unsigned long size
)
74 extern unsigned long memory_start
, memory_end
;
76 return ((addr
>= memory_start
) && ((addr
+ size
) < memory_end
));
78 #else /* CONFIG_MMU */
79 static inline mm_segment_t
get_fs(void)
81 return MAKE_MM_SEG(test_thread_flag(TIF_USERSPACE
));
84 static inline void set_fs(mm_segment_t s
)
86 unsigned long ti
, flag
;
89 "mov.l @(8,%0), %1\n\t"
94 : "=&r" (ti
), "=&r" (flag
)
95 : "r" (s
.is_user_space
)
99 set_thread_flag(TIF_USERSPACE);
101 clear_thread_flag(TIF_USERSPACE);
106 * __access_ok: Check if address with size is OK or not.
108 * We do three checks:
109 * (1) is it user space?
110 * (2) addr + size --> carry?
111 * (3) addr + size >= 0x80000000 (USER_ADDR_LIMIT)
113 * (1) (2) (3) | RESULT
123 static inline int __access_ok(unsigned long addr
, unsigned long size
)
125 unsigned long flag
, tmp
;
127 __asm__("stc r7_bank, %0\n\t"
128 "mov.l @(8,%0), %0\n\t"
135 : "=&z" (flag
), "=r" (tmp
)
136 : "r" (addr
), "1" (size
)
141 #endif /* CONFIG_MMU */
143 static inline int access_ok(int type
, const void __user
*p
, unsigned long size
)
145 unsigned long addr
= (unsigned long)p
;
146 return __access_ok(addr
, size
);
150 * Uh, these should become the main single-value transfer routines ...
151 * They automatically use the right size if we just have the right
154 * As SuperH uses the same address space for kernel and user data, we
155 * can just do these as direct assignments.
158 * (a) re-use the arguments for side effects (sizeof is ok)
159 * (b) require any knowledge of processes at this stage
161 #define put_user(x,ptr) __put_user_check((x),(ptr),sizeof(*(ptr)))
162 #define get_user(x,ptr) __get_user_check((x),(ptr),sizeof(*(ptr)))
165 * The "__xxx" versions do not do address space checking, useful when
166 * doing multiple accesses to the same area (the user has to do the
167 * checks by hand with "access_ok()")
169 #define __put_user(x,ptr) \
170 __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
171 #define __get_user(x,ptr) \
172 __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
174 struct __large_struct
{ unsigned long buf
[100]; };
175 #define __m(x) (*(struct __large_struct *)(x))
177 #define __get_user_size(x,ptr,size,retval) \
182 __get_user_asm(x, ptr, retval, "b"); \
185 __get_user_asm(x, ptr, retval, "w"); \
188 __get_user_asm(x, ptr, retval, "l"); \
191 __get_user_unknown(); \
196 #define __get_user_nocheck(x,ptr,size) \
198 long __gu_err, __gu_val; \
199 __get_user_size(__gu_val, (ptr), (size), __gu_err); \
200 (x) = (__typeof__(*(ptr)))__gu_val; \
204 #define __get_user_check(x,ptr,size) \
206 long __gu_err, __gu_val; \
209 __get_user_1(__gu_val, (ptr), __gu_err); \
212 __get_user_2(__gu_val, (ptr), __gu_err); \
215 __get_user_4(__gu_val, (ptr), __gu_err); \
218 __get_user_unknown(); \
222 (x) = (__typeof__(*(ptr)))__gu_val; \
226 #define __get_user_1(x,addr,err) ({ \
227 __asm__("stc r7_bank, %1\n\t" \
228 "mov.l @(8,%1), %1\n\t" \
238 "mov.b @%2, %1\n\t" \
241 ".section __ex_table,\"a\"\n\t" \
244 : "=&r" (err), "=&r" (x) \
249 #define __get_user_2(x,addr,err) ({ \
250 __asm__("stc r7_bank, %1\n\t" \
251 "mov.l @(8,%1), %1\n\t" \
261 "mov.w @%2, %1\n\t" \
264 ".section __ex_table,\"a\"\n\t" \
267 : "=&r" (err), "=&r" (x) \
272 #define __get_user_4(x,addr,err) ({ \
273 __asm__("stc r7_bank, %1\n\t" \
274 "mov.l @(8,%1), %1\n\t" \
284 "mov.l @%2, %1\n\t" \
286 ".section __ex_table,\"a\"\n\t" \
289 : "=&r" (err), "=&r" (x) \
294 #define __get_user_asm(x, addr, err, insn) \
296 __asm__ __volatile__( \
298 "mov." insn " %2, %1\n\t" \
301 ".section .fixup,\"ax\"\n" \
309 ".section __ex_table,\"a\"\n\t" \
312 :"=&r" (err), "=&r" (x) \
313 :"m" (__m(addr)), "i" (-EFAULT)); })
315 extern void __get_user_unknown(void);
317 #define __put_user_size(x,ptr,size,retval) \
322 __put_user_asm(x, ptr, retval, "b"); \
325 __put_user_asm(x, ptr, retval, "w"); \
328 __put_user_asm(x, ptr, retval, "l"); \
331 __put_user_u64(x, ptr, retval); \
334 __put_user_unknown(); \
338 #define __put_user_nocheck(x,ptr,size) \
341 __put_user_size((x),(ptr),(size),__pu_err); \
345 #define __put_user_check(x,ptr,size) \
347 long __pu_err = -EFAULT; \
348 __typeof__(*(ptr)) *__pu_addr = (ptr); \
350 if (__access_ok((unsigned long)__pu_addr,size)) \
351 __put_user_size((x),__pu_addr,(size),__pu_err); \
355 #define __put_user_asm(x, addr, err, insn) \
357 __asm__ __volatile__( \
359 "mov." insn " %1, %2\n\t" \
362 ".section .fixup,\"ax\"\n" \
370 ".section __ex_table,\"a\"\n\t" \
374 :"r" (x), "m" (__m(addr)), "i" (-EFAULT) \
377 #if defined(__LITTLE_ENDIAN__)
378 #define __put_user_u64(val,addr,retval) \
380 __asm__ __volatile__( \
383 "mov.l %S1,%T2\n\t" \
386 ".section .fixup,\"ax\"\n" \
394 ".section __ex_table,\"a\"\n\t" \
398 : "r" (val), "m" (__m(addr)), "i" (-EFAULT) \
401 #define __put_user_u64(val,addr,retval) \
403 __asm__ __volatile__( \
406 "mov.l %R1,%T2\n\t" \
409 ".section .fixup,\"ax\"\n" \
417 ".section __ex_table,\"a\"\n\t" \
421 : "r" (val), "m" (__m(addr)), "i" (-EFAULT) \
425 extern void __put_user_unknown(void);
427 /* Generic arbitrary sized copy. */
428 /* Return the number of bytes NOT copied */
429 extern __kernel_size_t
__copy_user(void *to
, const void *from
, __kernel_size_t n
);
431 #define copy_to_user(to,from,n) ({ \
432 void *__copy_to = (void *) (to); \
433 __kernel_size_t __copy_size = (__kernel_size_t) (n); \
434 __kernel_size_t __copy_res; \
435 if(__copy_size && __access_ok((unsigned long)__copy_to, __copy_size)) { \
436 __copy_res = __copy_user(__copy_to, (void *) (from), __copy_size); \
437 } else __copy_res = __copy_size; \
440 #define __copy_to_user(to,from,n) \
441 __copy_user((void *)(to), \
444 #define __copy_to_user_inatomic __copy_to_user
445 #define __copy_from_user_inatomic __copy_from_user
448 #define copy_from_user(to,from,n) ({ \
449 void *__copy_to = (void *) (to); \
450 void *__copy_from = (void *) (from); \
451 __kernel_size_t __copy_size = (__kernel_size_t) (n); \
452 __kernel_size_t __copy_res; \
453 if(__copy_size && __access_ok((unsigned long)__copy_from, __copy_size)) { \
454 __copy_res = __copy_user(__copy_to, __copy_from, __copy_size); \
455 } else __copy_res = __copy_size; \
458 #define __copy_from_user(to,from,n) \
459 __copy_user((void *)(to), \
463 * Clear the area and return remaining number of bytes
464 * (on failure. Usually it's 0.)
466 extern __kernel_size_t
__clear_user(void *addr
, __kernel_size_t size
);
468 #define clear_user(addr,n) ({ \
469 void * __cl_addr = (addr); \
470 unsigned long __cl_size = (n); \
471 if (__cl_size && __access_ok(((unsigned long)(__cl_addr)), __cl_size)) \
472 __cl_size = __clear_user(__cl_addr, __cl_size); \
475 static __inline__
int
476 __strncpy_from_user(unsigned long __dest
, unsigned long __user __src
, int __count
)
479 unsigned long __dummy
, _d
, _s
;
481 __asm__
__volatile__(
494 ".section .fixup,\"ax\"\n"
502 ".section __ex_table,\"a\"\n"
506 : "=r" (res
), "=&z" (__dummy
), "=r" (_s
), "=r" (_d
)
507 : "0" (__count
), "2" (__src
), "3" (__dest
), "r" (__count
),
514 #define strncpy_from_user(dest,src,count) ({ \
515 unsigned long __sfu_src = (unsigned long) (src); \
516 int __sfu_count = (int) (count); \
517 long __sfu_res = -EFAULT; \
518 if(__access_ok(__sfu_src, __sfu_count)) { \
519 __sfu_res = __strncpy_from_user((unsigned long) (dest), __sfu_src, __sfu_count); \
523 * Return the size of a string (including the ending 0!)
525 static __inline__
long __strnlen_user(const char __user
*__s
, long __n
)
528 unsigned long __dummy
;
530 __asm__
__volatile__(
535 "mov.b @(%0,%3), %1\n\t"
540 ".section .fixup,\"ax\"\n"
548 ".section __ex_table,\"a\"\n"
552 : "=z" (res
), "=&r" (__dummy
)
553 : "0" (0), "r" (__s
), "r" (__n
), "i" (-EFAULT
)
558 static __inline__
long strnlen_user(const char __user
*s
, long n
)
560 if (!access_ok(VERIFY_READ
, s
, n
))
563 return __strnlen_user(s
, n
);
566 static __inline__
long strlen_user(const char __user
*s
)
568 if (!access_ok(VERIFY_READ
, s
, 0))
571 return __strnlen_user(s
, ~0UL >> 1);
575 * The exception table consists of pairs of addresses: the first is the
576 * address of an instruction that is allowed to fault, and the second is
577 * the address at which the program should continue. No registers are
578 * modified, so it is entirely up to the continuation code to figure out
581 * All the routines below use bits of fixup code that are out of line
582 * with the main instruction path. This means when everything is well,
583 * we don't even have to jump over them. Further, they do not intrude
584 * on our cache or tlb entries.
587 struct exception_table_entry
589 unsigned long insn
, fixup
;
592 extern int fixup_exception(struct pt_regs
*regs
);
594 #endif /* __ASM_SH_UACCESS_H */