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_32_H
14 #define __ASM_SH_UACCESS_32_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 static inline int __access_ok(unsigned long addr, unsigned long size)
63 #else /* CONFIG_MMU */
64 #define __addr_ok(addr) \
65 ((unsigned long)(addr) < (current_thread_info()->addr_limit.seg))
67 #define get_fs() (current_thread_info()->addr_limit)
68 #define set_fs(x) (current_thread_info()->addr_limit = (x))
71 * __access_ok: Check if address with size is OK or not.
73 * Uhhuh, this needs 33-bit arithmetic. We have a carry..
75 * sum := addr + size; carry? --> flag = true;
76 * if (sum >= addr_limit) flag = true;
78 static inline int __access_ok(unsigned long addr, unsigned long size)
80 unsigned long flag, sum;
87 :"=&r" (flag), "=r" (sum)
88 :"1" (addr), "r" (size),
89 "r" (current_thread_info()->addr_limit.seg)
93 #endif /* CONFIG_MMU */
95 #define access_ok(type, addr, size) \
96 (__chk_user_ptr(addr), \
97 __access_ok((unsigned long __force)(addr), (size)))
100 * Uh, these should become the main single-value transfer routines ...
101 * They automatically use the right size if we just have the right
104 * As SuperH uses the same address space for kernel and user data, we
105 * can just do these as direct assignments.
108 * (a) re-use the arguments for side effects (sizeof is ok)
109 * (b) require any knowledge of processes at this stage
111 #define put_user(x,ptr) __put_user_check((x), (ptr), sizeof(*(ptr)))
112 #define get_user(x,ptr) __get_user_check((x), (ptr), sizeof(*(ptr)))
115 * The "__xxx" versions do not do address space checking, useful when
116 * doing multiple accesses to the same area (the user has to do the
117 * checks by hand with "access_ok()")
119 #define __put_user(x,ptr) __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
120 #define __get_user(x,ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
122 struct __large_struct { unsigned long buf[100]; };
123 #define __m(x) (*(struct __large_struct __user *)(x))
125 #define __get_user_size(x,ptr,size,retval) \
130 __get_user_asm(x, ptr, retval, "b"); \
133 __get_user_asm(x, ptr, retval, "w"); \
136 __get_user_asm(x, ptr, retval, "l"); \
139 __get_user_unknown(); \
144 #define __get_user_nocheck(x,ptr,size) \
147 unsigned long __gu_val; \
148 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
149 __chk_user_ptr(ptr); \
150 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
151 (x) = (__typeof__(*(ptr)))__gu_val; \
155 #define __get_user_check(x,ptr,size) \
157 long __gu_err = -EFAULT; \
158 unsigned long __gu_val = 0; \
159 const __typeof__(*(ptr)) *__gu_addr = (ptr); \
160 if (likely(access_ok(VERIFY_READ, __gu_addr, (size)))) \
161 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
162 (x) = (__typeof__(*(ptr)))__gu_val; \
166 #define __get_user_asm(x, addr, err, insn) \
168 __asm__ __volatile__( \
170 "mov." insn " %2, %1\n\t" \
172 ".section .fixup,\"ax\"\n" \
181 ".section __ex_table,\"a\"\n\t" \
184 :"=&r" (err), "=&r" (x) \
185 :"m" (__m(addr)), "i" (-EFAULT), "0" (err)); })
187 extern void __get_user_unknown(void);
189 #define __put_user_size(x,ptr,size,retval) \
194 __put_user_asm(x, ptr, retval, "b"); \
197 __put_user_asm(x, ptr, retval, "w"); \
200 __put_user_asm(x, ptr, retval, "l"); \
203 __put_user_u64(x, ptr, retval); \
206 __put_user_unknown(); \
210 #define __put_user_nocheck(x,ptr,size) \
213 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
214 __chk_user_ptr(ptr); \
215 __put_user_size((x), __pu_addr, (size), __pu_err); \
219 #define __put_user_check(x,ptr,size) \
221 long __pu_err = -EFAULT; \
222 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
223 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) \
224 __put_user_size((x), __pu_addr, (size), \
229 #define __put_user_asm(x, addr, err, insn) \
231 __asm__ __volatile__( \
233 "mov." insn " %1, %2\n\t" \
235 ".section .fixup,\"ax\"\n" \
243 ".section __ex_table,\"a\"\n\t" \
247 :"r" (x), "m" (__m(addr)), "i" (-EFAULT), "0" (err) \
250 #if defined(CONFIG_CPU_LITTLE_ENDIAN)
251 #define __put_user_u64(val,addr,retval) \
253 __asm__ __volatile__( \
256 "mov.l %S1,%T2\n\t" \
258 ".section .fixup,\"ax\"\n" \
266 ".section __ex_table,\"a\"\n\t" \
270 : "r" (val), "m" (__m(addr)), "i" (-EFAULT), "0" (retval) \
273 #define __put_user_u64(val,addr,retval) \
275 __asm__ __volatile__( \
278 "mov.l %R1,%T2\n\t" \
280 ".section .fixup,\"ax\"\n" \
288 ".section __ex_table,\"a\"\n\t" \
292 : "r" (val), "m" (__m(addr)), "i" (-EFAULT), "0" (retval) \
296 extern void __put_user_unknown(void);
298 /* Generic arbitrary sized copy. */
299 /* Return the number of bytes NOT copied */
300 __kernel_size_t __copy_user(void *to, const void *from, __kernel_size_t n);
303 static __always_inline unsigned long
304 __copy_from_user(void *to, const void __user *from, unsigned long n)
306 return __copy_user(to, (__force void *)from, n);
309 static __always_inline unsigned long __must_check
310 __copy_to_user(void __user *to, const void *from, unsigned long n)
312 return __copy_user((__force void *)to, from, n);
315 #define __copy_to_user_inatomic __copy_to_user
316 #define __copy_from_user_inatomic __copy_from_user
319 * Clear the area and return remaining number of bytes
320 * (on failure. Usually it's 0.)
322 extern __kernel_size_t __clear_user(void *addr, __kernel_size_t size);
324 #define clear_user(addr,n) ({ \
325 void * __cl_addr = (addr); \
326 unsigned long __cl_size = (n); \
327 if (__cl_size && __access_ok(((unsigned long)(__cl_addr)), __cl_size)) \
328 __cl_size = __clear_user(__cl_addr, __cl_size); \
331 static __inline__ int
332 __strncpy_from_user(unsigned long __dest, unsigned long __user __src, int __count)
335 unsigned long __dummy, _d, _s, _c;
337 __asm__ __volatile__(
350 ".section .fixup,\"ax\"\n"
358 ".section __ex_table,\"a\"\n"
362 : "=r" (res), "=&z" (__dummy), "=r" (_s), "=r" (_d), "=r"(_c)
363 : "0" (__count), "2" (__src), "3" (__dest), "4" (__count),
371 * strncpy_from_user: - Copy a NUL terminated string from userspace.
372 * @dst: Destination address, in kernel space. This buffer must be at
373 * least @count bytes long.
374 * @src: Source address, in user space.
375 * @count: Maximum number of bytes to copy, including the trailing NUL.
377 * Copies a NUL-terminated string from userspace to kernel space.
379 * On success, returns the length of the string (not including the trailing
382 * If access to userspace fails, returns -EFAULT (some data may have been
385 * If @count is smaller than the length of the string, copies @count bytes
386 * and returns @count.
388 #define strncpy_from_user(dest,src,count) ({ \
389 unsigned long __sfu_src = (unsigned long) (src); \
390 int __sfu_count = (int) (count); \
391 long __sfu_res = -EFAULT; \
392 if(__access_ok(__sfu_src, __sfu_count)) { \
393 __sfu_res = __strncpy_from_user((unsigned long) (dest), __sfu_src, __sfu_count); \
397 * Return the size of a string (including the ending 0 even when we have
398 * exceeded the maximum string length).
400 static __inline__ long __strnlen_user(const char __user *__s, long __n)
403 unsigned long __dummy;
405 __asm__ __volatile__(
407 "mov.b @(%0,%3), %1\n\t"
414 ".section .fixup,\"ax\"\n"
422 ".section __ex_table,\"a\"\n"
426 : "=z" (res), "=&r" (__dummy)
427 : "0" (0), "r" (__s), "r" (__n)
433 * strnlen_user: - Get the size of a string in user space.
434 * @s: The string to measure.
435 * @n: The maximum valid length
437 * Context: User context only. This function may sleep.
439 * Get the size of a NUL-terminated string in user space.
441 * Returns the size of the string INCLUDING the terminating NUL.
442 * On exception, returns 0.
443 * If the string is too long, returns a value greater than @n.
445 static __inline__ long strnlen_user(const char __user *s, long n)
450 return __strnlen_user(s, n);
454 * strlen_user: - Get the size of a string in user space.
455 * @str: The string to measure.
457 * Context: User context only. This function may sleep.
459 * Get the size of a NUL-terminated string in user space.
461 * Returns the size of the string INCLUDING the terminating NUL.
462 * On exception, returns 0.
464 * If there is a limit on the length of a valid string, you may wish to
465 * consider using strnlen_user() instead.
467 #define strlen_user(str) strnlen_user(str, ~0UL >> 1)
470 * The exception table consists of pairs of addresses: the first is the
471 * address of an instruction that is allowed to fault, and the second is
472 * the address at which the program should continue. No registers are
473 * modified, so it is entirely up to the continuation code to figure out
476 * All the routines below use bits of fixup code that are out of line
477 * with the main instruction path. This means when everything is well,
478 * we don't even have to jump over them. Further, they do not intrude
479 * on our cache or tlb entries.
482 struct exception_table_entry
484 unsigned long insn, fixup;
487 extern int fixup_exception(struct pt_regs *regs);
489 #endif /* __ASM_SH_UACCESS_32_H */