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 * 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.
76 * Uhhuh, this needs 33-bit arithmetic. We have a carry..
78 * sum := addr + size; carry? --> flag = true;
79 * if (sum >= addr_limit) flag = true;
81 static inline int __access_ok(unsigned long addr
, unsigned long size
)
83 unsigned long flag
, sum
;
90 :"=&r" (flag
), "=r" (sum
)
91 :"1" (addr
), "r" (size
),
92 "r" (current_thread_info()->addr_limit
.seg
)
96 #endif /* CONFIG_MMU */
98 #define access_ok(type, addr, size) \
99 (__chk_user_ptr(addr), \
100 __access_ok((unsigned long __force)(addr), (size)))
103 * Uh, these should become the main single-value transfer routines ...
104 * They automatically use the right size if we just have the right
107 * As SuperH uses the same address space for kernel and user data, we
108 * can just do these as direct assignments.
111 * (a) re-use the arguments for side effects (sizeof is ok)
112 * (b) require any knowledge of processes at this stage
114 #define put_user(x,ptr) __put_user_check((x), (ptr), sizeof(*(ptr)))
115 #define get_user(x,ptr) __get_user_check((x), (ptr), sizeof(*(ptr)))
118 * The "__xxx" versions do not do address space checking, useful when
119 * doing multiple accesses to the same area (the user has to do the
120 * checks by hand with "access_ok()")
122 #define __put_user(x,ptr) __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
123 #define __get_user(x,ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
125 struct __large_struct
{ unsigned long buf
[100]; };
126 #define __m(x) (*(struct __large_struct __user *)(x))
128 #define __get_user_size(x,ptr,size,retval) \
133 __get_user_asm(x, ptr, retval, "b"); \
136 __get_user_asm(x, ptr, retval, "w"); \
139 __get_user_asm(x, ptr, retval, "l"); \
142 __get_user_unknown(); \
147 #define __get_user_nocheck(x,ptr,size) \
150 unsigned long __gu_val; \
151 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
152 __chk_user_ptr(ptr); \
153 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
154 (x) = (__typeof__(*(ptr)))__gu_val; \
158 #define __get_user_check(x,ptr,size) \
160 long __gu_err = -EFAULT; \
161 unsigned long __gu_val = 0; \
162 const __typeof__(*(ptr)) *__gu_addr = (ptr); \
163 if (likely(access_ok(VERIFY_READ, __gu_addr, (size)))) \
164 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
165 (x) = (__typeof__(*(ptr)))__gu_val; \
169 #define __get_user_asm(x, addr, err, insn) \
171 __asm__ __volatile__( \
173 "mov." insn " %2, %1\n\t" \
175 ".section .fixup,\"ax\"\n" \
184 ".section __ex_table,\"a\"\n\t" \
187 :"=&r" (err), "=&r" (x) \
188 :"m" (__m(addr)), "i" (-EFAULT), "0" (err)); })
190 extern void __get_user_unknown(void);
192 #define __put_user_size(x,ptr,size,retval) \
197 __put_user_asm(x, ptr, retval, "b"); \
200 __put_user_asm(x, ptr, retval, "w"); \
203 __put_user_asm(x, ptr, retval, "l"); \
206 __put_user_u64(x, ptr, retval); \
209 __put_user_unknown(); \
213 #define __put_user_nocheck(x,ptr,size) \
216 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
217 __chk_user_ptr(ptr); \
218 __put_user_size((x), __pu_addr, (size), __pu_err); \
222 #define __put_user_check(x,ptr,size) \
224 long __pu_err = -EFAULT; \
225 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
226 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) \
227 __put_user_size((x), __pu_addr, (size), \
232 #define __put_user_asm(x, addr, err, insn) \
234 __asm__ __volatile__( \
236 "mov." insn " %1, %2\n\t" \
238 ".section .fixup,\"ax\"\n" \
246 ".section __ex_table,\"a\"\n\t" \
250 :"r" (x), "m" (__m(addr)), "i" (-EFAULT), "0" (err) \
253 #if defined(CONFIG_CPU_LITTLE_ENDIAN)
254 #define __put_user_u64(val,addr,retval) \
256 __asm__ __volatile__( \
259 "mov.l %S1,%T2\n\t" \
261 ".section .fixup,\"ax\"\n" \
269 ".section __ex_table,\"a\"\n\t" \
273 : "r" (val), "m" (__m(addr)), "i" (-EFAULT), "0" (retval) \
276 #define __put_user_u64(val,addr,retval) \
278 __asm__ __volatile__( \
281 "mov.l %R1,%T2\n\t" \
283 ".section .fixup,\"ax\"\n" \
291 ".section __ex_table,\"a\"\n\t" \
295 : "r" (val), "m" (__m(addr)), "i" (-EFAULT), "0" (retval) \
299 extern void __put_user_unknown(void);
301 /* Generic arbitrary sized copy. */
302 /* Return the number of bytes NOT copied */
303 __kernel_size_t
__copy_user(void *to
, const void *from
, __kernel_size_t n
);
306 static __always_inline
unsigned long
307 __copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
309 return __copy_user(to
, (__force
void *)from
, n
);
312 static __always_inline
unsigned long __must_check
313 __copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
315 return __copy_user((__force
void *)to
, from
, n
);
318 #define __copy_to_user_inatomic __copy_to_user
319 #define __copy_from_user_inatomic __copy_from_user
322 * Clear the area and return remaining number of bytes
323 * (on failure. Usually it's 0.)
325 extern __kernel_size_t
__clear_user(void *addr
, __kernel_size_t size
);
327 #define clear_user(addr,n) ({ \
328 void * __cl_addr = (addr); \
329 unsigned long __cl_size = (n); \
330 if (__cl_size && __access_ok(((unsigned long)(__cl_addr)), __cl_size)) \
331 __cl_size = __clear_user(__cl_addr, __cl_size); \
334 static __inline__
int
335 __strncpy_from_user(unsigned long __dest
, unsigned long __user __src
, int __count
)
338 unsigned long __dummy
, _d
, _s
, _c
;
340 __asm__
__volatile__(
353 ".section .fixup,\"ax\"\n"
361 ".section __ex_table,\"a\"\n"
365 : "=r" (res
), "=&z" (__dummy
), "=r" (_s
), "=r" (_d
), "=r"(_c
)
366 : "0" (__count
), "2" (__src
), "3" (__dest
), "4" (__count
),
374 * strncpy_from_user: - Copy a NUL terminated string from userspace.
375 * @dst: Destination address, in kernel space. This buffer must be at
376 * least @count bytes long.
377 * @src: Source address, in user space.
378 * @count: Maximum number of bytes to copy, including the trailing NUL.
380 * Copies a NUL-terminated string from userspace to kernel space.
382 * On success, returns the length of the string (not including the trailing
385 * If access to userspace fails, returns -EFAULT (some data may have been
388 * If @count is smaller than the length of the string, copies @count bytes
389 * and returns @count.
391 #define strncpy_from_user(dest,src,count) ({ \
392 unsigned long __sfu_src = (unsigned long) (src); \
393 int __sfu_count = (int) (count); \
394 long __sfu_res = -EFAULT; \
395 if(__access_ok(__sfu_src, __sfu_count)) { \
396 __sfu_res = __strncpy_from_user((unsigned long) (dest), __sfu_src, __sfu_count); \
400 * Return the size of a string (including the ending 0 even when we have
401 * exceeded the maximum string length).
403 static __inline__
long __strnlen_user(const char __user
*__s
, long __n
)
406 unsigned long __dummy
;
408 __asm__
__volatile__(
410 "mov.b @(%0,%3), %1\n\t"
417 ".section .fixup,\"ax\"\n"
425 ".section __ex_table,\"a\"\n"
429 : "=z" (res
), "=&r" (__dummy
)
430 : "0" (0), "r" (__s
), "r" (__n
)
436 * strnlen_user: - Get the size of a string in user space.
437 * @s: The string to measure.
438 * @n: The maximum valid length
440 * Context: User context only. This function may sleep.
442 * Get the size of a NUL-terminated string in user space.
444 * Returns the size of the string INCLUDING the terminating NUL.
445 * On exception, returns 0.
446 * If the string is too long, returns a value greater than @n.
448 static __inline__
long strnlen_user(const char __user
*s
, long n
)
453 return __strnlen_user(s
, n
);
457 * strlen_user: - Get the size of a string in user space.
458 * @str: The string to measure.
460 * Context: User context only. This function may sleep.
462 * Get the size of a NUL-terminated string in user space.
464 * Returns the size of the string INCLUDING the terminating NUL.
465 * On exception, returns 0.
467 * If there is a limit on the length of a valid string, you may wish to
468 * consider using strnlen_user() instead.
470 #define strlen_user(str) strnlen_user(str, ~0UL >> 1)
473 * The exception table consists of pairs of addresses: the first is the
474 * address of an instruction that is allowed to fault, and the second is
475 * the address at which the program should continue. No registers are
476 * modified, so it is entirely up to the continuation code to figure out
479 * All the routines below use bits of fixup code that are out of line
480 * with the main instruction path. This means when everything is well,
481 * we don't even have to jump over them. Further, they do not intrude
482 * on our cache or tlb entries.
485 struct exception_table_entry
487 unsigned long insn
, fixup
;
490 extern int fixup_exception(struct pt_regs
*regs
);
492 #endif /* __ASM_SH_UACCESS_32_H */