1 #ifndef _ASM_M32R_UACCESS_H
2 #define _ASM_M32R_UACCESS_H
5 * linux/include/asm-m32r/uaccess.h
8 * Copyright (C) 2004, 2006 Hirokazu Takata <takata at linux-m32r.org>
12 * User space memory access functions
15 #include <asm/setup.h>
16 #include <linux/prefetch.h>
19 * The fs value determines whether argument validity checking should be
20 * performed or not. If get_fs() == USER_DS, checking is performed, with
21 * get_fs() == KERNEL_DS, checking is bypassed.
23 * For historical reasons, these macros are grossly misnamed.
26 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
30 #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF)
31 #define USER_DS MAKE_MM_SEG(PAGE_OFFSET)
32 #define get_ds() (KERNEL_DS)
33 #define get_fs() (current_thread_info()->addr_limit)
34 #define set_fs(x) (current_thread_info()->addr_limit = (x))
36 #else /* not CONFIG_MMU */
38 #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF)
39 #define USER_DS MAKE_MM_SEG(0xFFFFFFFF)
40 #define get_ds() (KERNEL_DS)
42 static inline mm_segment_t get_fs(void)
47 static inline void set_fs(mm_segment_t s)
51 #endif /* not CONFIG_MMU */
53 #define segment_eq(a, b) ((a).seg == (b).seg)
55 #define __addr_ok(addr) \
56 ((unsigned long)(addr) < (current_thread_info()->addr_limit.seg))
59 * Test whether a block of memory is a valid user space address.
60 * Returns 0 if the range is valid, nonzero otherwise.
62 * This is equivalent to the following test:
63 * (u33)addr + (u33)size >= (u33)current->addr_limit.seg
65 * This needs 33-bit arithmetic. We have a carry...
67 #define __range_ok(addr, size) ({ \
68 unsigned long flag, roksum; \
69 __chk_user_ptr(addr); \
71 " cmpu %1, %1 ; clear cbit\n" \
72 " addx %1, %3 ; set cbit if overflow\n" \
76 : "=&r" (flag), "=r" (roksum) \
77 : "1" (addr), "r" ((int)(size)), \
78 "r" (current_thread_info()->addr_limit.seg), "r" (0) \
83 * access_ok: - Checks if a user space pointer is valid
84 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
85 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
86 * to write to a block, it is always safe to read from it.
87 * @addr: User space pointer to start of block to check
88 * @size: Size of block to check
90 * Context: User context only. This function may sleep if pagefaults are
93 * Checks if a pointer to a block of memory in user space is valid.
95 * Returns true (nonzero) if the memory block may be valid, false (zero)
96 * if it is definitely invalid.
98 * Note that, depending on architecture, this function probably just
99 * checks that the pointer is in the user space range - after calling
100 * this function, memory access functions may still return -EFAULT.
103 #define access_ok(type, addr, size) (likely(__range_ok(addr, size) == 0))
105 static inline int access_ok(int type, const void *addr, unsigned long size)
107 unsigned long val = (unsigned long)addr;
109 return ((val >= memory_start) && ((val + size) < memory_end));
111 #endif /* CONFIG_MMU */
113 #include <asm/extable.h>
116 * These are the main single-value transfer routines. They automatically
117 * use the right size if we just have the right pointer type.
119 * This gets kind of ugly. We want to return _two_ values in "get_user()"
120 * and yet we don't want to do any pointers, because that is too much
121 * of a performance impact. Thus we have a few rather ugly macros here,
122 * and hide all the uglyness from the user.
124 * The "__xxx" versions of the user access functions are versions that
125 * do not verify the address space, that must have been done previously
126 * with a separate "access_ok()" call (this is used when we do multiple
127 * accesses to the same area of user memory).
130 /* Careful: we have to cast the result to the type of the pointer for sign
133 * get_user: - Get a simple variable from user space.
134 * @x: Variable to store result.
135 * @ptr: Source address, in user space.
137 * Context: User context only. This function may sleep if pagefaults are
140 * This macro copies a single simple variable from user space to kernel
141 * space. It supports simple types like char and int, but not larger
142 * data types like structures or arrays.
144 * @ptr must have pointer-to-simple-variable type, and the result of
145 * dereferencing @ptr must be assignable to @x without a cast.
147 * Returns zero on success, or -EFAULT on error.
148 * On error, the variable @x is set to zero.
150 #define get_user(x, ptr) \
151 __get_user_check((x), (ptr), sizeof(*(ptr)))
154 * put_user: - Write a simple value into user space.
155 * @x: Value to copy to user space.
156 * @ptr: Destination address, in user space.
158 * Context: User context only. This function may sleep if pagefaults are
161 * This macro copies a single simple value from kernel space to user
162 * space. It supports simple types like char and int, but not larger
163 * data types like structures or arrays.
165 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
166 * to the result of dereferencing @ptr.
168 * Returns zero on success, or -EFAULT on error.
170 #define put_user(x, ptr) \
171 __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
174 * __get_user: - Get a simple variable from user space, with less checking.
175 * @x: Variable to store result.
176 * @ptr: Source address, in user space.
178 * Context: User context only. This function may sleep if pagefaults are
181 * This macro copies a single simple variable from user space to kernel
182 * space. It supports simple types like char and int, but not larger
183 * data types like structures or arrays.
185 * @ptr must have pointer-to-simple-variable type, and the result of
186 * dereferencing @ptr must be assignable to @x without a cast.
188 * Caller must check the pointer with access_ok() before calling this
191 * Returns zero on success, or -EFAULT on error.
192 * On error, the variable @x is set to zero.
194 #define __get_user(x, ptr) \
195 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
197 #define __get_user_nocheck(x, ptr, size) \
200 unsigned long __gu_val = 0; \
202 __get_user_size(__gu_val, (ptr), (size), __gu_err); \
203 (x) = (__force __typeof__(*(ptr)))__gu_val; \
207 #define __get_user_check(x, ptr, size) \
209 long __gu_err = -EFAULT; \
210 unsigned long __gu_val = 0; \
211 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
213 if (access_ok(VERIFY_READ, __gu_addr, size)) \
214 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
215 (x) = (__force __typeof__(*(ptr)))__gu_val; \
219 extern long __get_user_bad(void);
221 #define __get_user_size(x, ptr, size, retval) \
224 __chk_user_ptr(ptr); \
226 case 1: __get_user_asm(x, ptr, retval, "ub"); break; \
227 case 2: __get_user_asm(x, ptr, retval, "uh"); break; \
228 case 4: __get_user_asm(x, ptr, retval, ""); break; \
229 default: (x) = __get_user_bad(); \
233 #define __get_user_asm(x, addr, err, itype) \
234 __asm__ __volatile__( \
236 "1: ld"itype" %1,@%2\n" \
239 ".section .fixup,\"ax\"\n" \
242 " seth r14,#high(2b)\n" \
243 " or3 r14,r14,#low(2b)\n" \
246 ".section __ex_table,\"a\"\n" \
250 : "=&r" (err), "=&r" (x) \
251 : "r" (addr), "i" (-EFAULT), "0" (err) \
255 * __put_user: - Write a simple value into user space, with less checking.
256 * @x: Value to copy to user space.
257 * @ptr: Destination address, in user space.
259 * Context: User context only. This function may sleep if pagefaults are
262 * This macro copies a single simple value from kernel space to user
263 * space. It supports simple types like char and int, but not larger
264 * data types like structures or arrays.
266 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
267 * to the result of dereferencing @ptr.
269 * Caller must check the pointer with access_ok() before calling this
272 * Returns zero on success, or -EFAULT on error.
274 #define __put_user(x, ptr) \
275 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
278 #define __put_user_nocheck(x, ptr, size) \
282 __put_user_size((x), (ptr), (size), __pu_err); \
287 #define __put_user_check(x, ptr, size) \
289 long __pu_err = -EFAULT; \
290 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
292 if (access_ok(VERIFY_WRITE, __pu_addr, size)) \
293 __put_user_size((x), __pu_addr, (size), __pu_err); \
297 #if defined(__LITTLE_ENDIAN__)
298 #define __put_user_u64(x, addr, err) \
299 __asm__ __volatile__( \
303 "2: st %H1,@(4,%2)\n" \
306 ".section .fixup,\"ax\"\n" \
309 " seth r14,#high(3b)\n" \
310 " or3 r14,r14,#low(3b)\n" \
313 ".section __ex_table,\"a\"\n" \
319 : "r" (x), "r" (addr), "i" (-EFAULT), "0" (err) \
322 #elif defined(__BIG_ENDIAN__)
323 #define __put_user_u64(x, addr, err) \
324 __asm__ __volatile__( \
328 "2: st %L1,@(4,%2)\n" \
331 ".section .fixup,\"ax\"\n" \
334 " seth r14,#high(3b)\n" \
335 " or3 r14,r14,#low(3b)\n" \
338 ".section __ex_table,\"a\"\n" \
344 : "r" (x), "r" (addr), "i" (-EFAULT), "0" (err) \
347 #error no endian defined
350 extern void __put_user_bad(void);
352 #define __put_user_size(x, ptr, size, retval) \
355 __chk_user_ptr(ptr); \
357 case 1: __put_user_asm(x, ptr, retval, "b"); break; \
358 case 2: __put_user_asm(x, ptr, retval, "h"); break; \
359 case 4: __put_user_asm(x, ptr, retval, ""); break; \
360 case 8: __put_user_u64((__typeof__(*ptr))(x), ptr, retval); break;\
361 default: __put_user_bad(); \
365 struct __large_struct { unsigned long buf[100]; };
366 #define __m(x) (*(struct __large_struct *)(x))
369 * Tell gcc we read from memory instead of writing: this is because
370 * we do not write to any memory gcc knows about, so there are no
373 #define __put_user_asm(x, addr, err, itype) \
374 __asm__ __volatile__( \
376 "1: st"itype" %1,@%2\n" \
379 ".section .fixup,\"ax\"\n" \
382 " seth r14,#high(2b)\n" \
383 " or3 r14,r14,#low(2b)\n" \
386 ".section __ex_table,\"a\"\n" \
391 : "r" (x), "r" (addr), "i" (-EFAULT), "0" (err) \
395 * Here we special-case 1, 2 and 4-byte copy_*_user invocations. On a fault
396 * we return the initial request size (1, 2 or 4), as copy_*_user should do.
397 * If a store crosses a page boundary and gets a fault, the m32r will not write
398 * anything, so this is accurate.
402 * Copy To/From Userspace
405 /* Generic arbitrary sized copy. */
406 /* Return the number of bytes NOT copied. */
407 #define __copy_user(to, from, size) \
409 unsigned long __dst, __src, __c; \
410 __asm__ __volatile__ ( \
413 " beq %0, %1, 9f\n" \
415 " and3 r14, r14, #3\n" \
417 " and3 %2, %2, #3\n" \
419 " addi %0, #-4 ; word_copy \n" \
421 "0: ld r14, @%1+\n" \
424 "1: st r14, @+%0\n" \
429 "2: ldb r14, @%1 ; byte_copy \n" \
431 "3: stb r14, @%0\n" \
438 ".section .fixup,\"ax\"\n" \
447 "7: seth r14, #high(9b)\n" \
448 " or3 r14, r14, #low(9b)\n" \
451 ".section __ex_table,\"a\"\n" \
458 : "=&r" (__dst), "=&r" (__src), "=&r" (size), \
460 : "0" (to), "1" (from), "2" (size), "3" (size / 4) \
461 : "r14", "memory"); \
464 /* We let the __ versions of copy_from/to_user inline, because they're often
465 * used in fast paths and have only a small space overhead.
467 static inline unsigned long
468 raw_copy_from_user(void *to, const void __user *from, unsigned long n)
471 __copy_user(to, from, n);
475 static inline unsigned long
476 raw_copy_to_user(void __user *to, const void *from, unsigned long n)
479 __copy_user(to, from, n);
483 long __must_check strncpy_from_user(char *dst, const char __user *src,
485 long __must_check __strncpy_from_user(char *dst,
486 const char __user *src, long count);
489 * __clear_user: - Zero a block of memory in user space, with less checking.
490 * @to: Destination address, in user space.
491 * @n: Number of bytes to zero.
493 * Zero a block of memory in user space. Caller must check
494 * the specified block with access_ok() before calling this function.
496 * Returns number of bytes that could not be cleared.
497 * On success, this will be zero.
499 unsigned long __clear_user(void __user *mem, unsigned long len);
502 * clear_user: - Zero a block of memory in user space.
503 * @to: Destination address, in user space.
504 * @n: Number of bytes to zero.
506 * Zero a block of memory in user space. Caller must check
507 * the specified block with access_ok() before calling this function.
509 * Returns number of bytes that could not be cleared.
510 * On success, this will be zero.
512 unsigned long clear_user(void __user *mem, unsigned long len);
515 * strlen_user: - Get the size of a string in user space.
516 * @str: The string to measure.
518 * Context: User context only. This function may sleep if pagefaults are
521 * Get the size of a NUL-terminated string in user space.
523 * Returns the size of the string INCLUDING the terminating NUL.
524 * On exception, returns 0.
526 * If there is a limit on the length of a valid string, you may wish to
527 * consider using strnlen_user() instead.
529 #define strlen_user(str) strnlen_user(str, ~0UL >> 1)
530 long strnlen_user(const char __user *str, long n);
532 #endif /* _ASM_M32R_UACCESS_H */