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
14 #include <linux/errno.h>
15 #include <linux/thread_info.h>
17 #include <asm/setup.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, these macros are grossly misnamed.
30 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
34 #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF)
35 #define USER_DS MAKE_MM_SEG(PAGE_OFFSET)
36 #define get_ds() (KERNEL_DS)
37 #define get_fs() (current_thread_info()->addr_limit)
38 #define set_fs(x) (current_thread_info()->addr_limit = (x))
40 #else /* not CONFIG_MMU */
42 #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF)
43 #define USER_DS MAKE_MM_SEG(0xFFFFFFFF)
44 #define get_ds() (KERNEL_DS)
46 static inline mm_segment_t
get_fs(void)
51 static inline void set_fs(mm_segment_t s
)
55 #endif /* not CONFIG_MMU */
57 #define segment_eq(a, b) ((a).seg == (b).seg)
59 #define __addr_ok(addr) \
60 ((unsigned long)(addr) < (current_thread_info()->addr_limit.seg))
63 * Test whether a block of memory is a valid user space address.
64 * Returns 0 if the range is valid, nonzero otherwise.
66 * This is equivalent to the following test:
67 * (u33)addr + (u33)size >= (u33)current->addr_limit.seg
69 * This needs 33-bit arithmetic. We have a carry...
71 #define __range_ok(addr, size) ({ \
72 unsigned long flag, roksum; \
73 __chk_user_ptr(addr); \
75 " cmpu %1, %1 ; clear cbit\n" \
76 " addx %1, %3 ; set cbit if overflow\n" \
80 : "=&r" (flag), "=r" (roksum) \
81 : "1" (addr), "r" ((int)(size)), \
82 "r" (current_thread_info()->addr_limit.seg), "r" (0) \
87 * access_ok: - Checks if a user space pointer is valid
88 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
89 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
90 * to write to a block, it is always safe to read from it.
91 * @addr: User space pointer to start of block to check
92 * @size: Size of block to check
94 * Context: User context only. This function may sleep if pagefaults are
97 * Checks if a pointer to a block of memory in user space is valid.
99 * Returns true (nonzero) if the memory block may be valid, false (zero)
100 * if it is definitely invalid.
102 * Note that, depending on architecture, this function probably just
103 * checks that the pointer is in the user space range - after calling
104 * this function, memory access functions may still return -EFAULT.
107 #define access_ok(type, addr, size) (likely(__range_ok(addr, size) == 0))
109 static inline int access_ok(int type
, const void *addr
, unsigned long size
)
111 unsigned long val
= (unsigned long)addr
;
113 return ((val
>= memory_start
) && ((val
+ size
) < memory_end
));
115 #endif /* CONFIG_MMU */
118 * The exception table consists of pairs of addresses: the first is the
119 * address of an instruction that is allowed to fault, and the second is
120 * the address at which the program should continue. No registers are
121 * modified, so it is entirely up to the continuation code to figure out
124 * All the routines below use bits of fixup code that are out of line
125 * with the main instruction path. This means when everything is well,
126 * we don't even have to jump over them. Further, they do not intrude
127 * on our cache or tlb entries.
130 struct exception_table_entry
132 unsigned long insn
, fixup
;
135 extern int fixup_exception(struct pt_regs
*regs
);
138 * These are the main single-value transfer routines. They automatically
139 * use the right size if we just have the right pointer type.
141 * This gets kind of ugly. We want to return _two_ values in "get_user()"
142 * and yet we don't want to do any pointers, because that is too much
143 * of a performance impact. Thus we have a few rather ugly macros here,
144 * and hide all the uglyness from the user.
146 * The "__xxx" versions of the user access functions are versions that
147 * do not verify the address space, that must have been done previously
148 * with a separate "access_ok()" call (this is used when we do multiple
149 * accesses to the same area of user memory).
152 /* Careful: we have to cast the result to the type of the pointer for sign
155 * get_user: - Get a simple variable from user space.
156 * @x: Variable to store result.
157 * @ptr: Source address, in user space.
159 * Context: User context only. This function may sleep if pagefaults are
162 * This macro copies a single simple variable from user space to kernel
163 * space. It supports simple types like char and int, but not larger
164 * data types like structures or arrays.
166 * @ptr must have pointer-to-simple-variable type, and the result of
167 * dereferencing @ptr must be assignable to @x without a cast.
169 * Returns zero on success, or -EFAULT on error.
170 * On error, the variable @x is set to zero.
172 #define get_user(x, ptr) \
173 __get_user_check((x), (ptr), sizeof(*(ptr)))
176 * put_user: - Write a simple value into user space.
177 * @x: Value to copy to user space.
178 * @ptr: Destination address, in user space.
180 * Context: User context only. This function may sleep if pagefaults are
183 * This macro copies a single simple value from kernel space to user
184 * space. It supports simple types like char and int, but not larger
185 * data types like structures or arrays.
187 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
188 * to the result of dereferencing @ptr.
190 * Returns zero on success, or -EFAULT on error.
192 #define put_user(x, ptr) \
193 __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
196 * __get_user: - Get a simple variable from user space, with less checking.
197 * @x: Variable to store result.
198 * @ptr: Source address, in user space.
200 * Context: User context only. This function may sleep if pagefaults are
203 * This macro copies a single simple variable from user space to kernel
204 * space. It supports simple types like char and int, but not larger
205 * data types like structures or arrays.
207 * @ptr must have pointer-to-simple-variable type, and the result of
208 * dereferencing @ptr must be assignable to @x without a cast.
210 * Caller must check the pointer with access_ok() before calling this
213 * Returns zero on success, or -EFAULT on error.
214 * On error, the variable @x is set to zero.
216 #define __get_user(x, ptr) \
217 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
219 #define __get_user_nocheck(x, ptr, size) \
222 unsigned long __gu_val; \
224 __get_user_size(__gu_val, (ptr), (size), __gu_err); \
225 (x) = (__force __typeof__(*(ptr)))__gu_val; \
229 #define __get_user_check(x, ptr, size) \
231 long __gu_err = -EFAULT; \
232 unsigned long __gu_val = 0; \
233 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
235 if (access_ok(VERIFY_READ, __gu_addr, size)) \
236 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
237 (x) = (__force __typeof__(*(ptr)))__gu_val; \
241 extern long __get_user_bad(void);
243 #define __get_user_size(x, ptr, size, retval) \
246 __chk_user_ptr(ptr); \
248 case 1: __get_user_asm(x, ptr, retval, "ub"); break; \
249 case 2: __get_user_asm(x, ptr, retval, "uh"); break; \
250 case 4: __get_user_asm(x, ptr, retval, ""); break; \
251 default: (x) = __get_user_bad(); \
255 #define __get_user_asm(x, addr, err, itype) \
256 __asm__ __volatile__( \
258 "1: ld"itype" %1,@%2\n" \
261 ".section .fixup,\"ax\"\n" \
264 " seth r14,#high(2b)\n" \
265 " or3 r14,r14,#low(2b)\n" \
268 ".section __ex_table,\"a\"\n" \
272 : "=&r" (err), "=&r" (x) \
273 : "r" (addr), "i" (-EFAULT), "0" (err) \
277 * __put_user: - Write a simple value into user space, with less checking.
278 * @x: Value to copy to user space.
279 * @ptr: Destination address, in user space.
281 * Context: User context only. This function may sleep if pagefaults are
284 * This macro copies a single simple value from kernel space to user
285 * space. It supports simple types like char and int, but not larger
286 * data types like structures or arrays.
288 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
289 * to the result of dereferencing @ptr.
291 * Caller must check the pointer with access_ok() before calling this
294 * Returns zero on success, or -EFAULT on error.
296 #define __put_user(x, ptr) \
297 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
300 #define __put_user_nocheck(x, ptr, size) \
304 __put_user_size((x), (ptr), (size), __pu_err); \
309 #define __put_user_check(x, ptr, size) \
311 long __pu_err = -EFAULT; \
312 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
314 if (access_ok(VERIFY_WRITE, __pu_addr, size)) \
315 __put_user_size((x), __pu_addr, (size), __pu_err); \
319 #if defined(__LITTLE_ENDIAN__)
320 #define __put_user_u64(x, addr, err) \
321 __asm__ __volatile__( \
325 "2: st %H1,@(4,%2)\n" \
328 ".section .fixup,\"ax\"\n" \
331 " seth r14,#high(3b)\n" \
332 " or3 r14,r14,#low(3b)\n" \
335 ".section __ex_table,\"a\"\n" \
341 : "r" (x), "r" (addr), "i" (-EFAULT), "0" (err) \
344 #elif defined(__BIG_ENDIAN__)
345 #define __put_user_u64(x, addr, err) \
346 __asm__ __volatile__( \
350 "2: st %L1,@(4,%2)\n" \
353 ".section .fixup,\"ax\"\n" \
356 " seth r14,#high(3b)\n" \
357 " or3 r14,r14,#low(3b)\n" \
360 ".section __ex_table,\"a\"\n" \
366 : "r" (x), "r" (addr), "i" (-EFAULT), "0" (err) \
369 #error no endian defined
372 extern void __put_user_bad(void);
374 #define __put_user_size(x, ptr, size, retval) \
377 __chk_user_ptr(ptr); \
379 case 1: __put_user_asm(x, ptr, retval, "b"); break; \
380 case 2: __put_user_asm(x, ptr, retval, "h"); break; \
381 case 4: __put_user_asm(x, ptr, retval, ""); break; \
382 case 8: __put_user_u64((__typeof__(*ptr))(x), ptr, retval); break;\
383 default: __put_user_bad(); \
387 struct __large_struct
{ unsigned long buf
[100]; };
388 #define __m(x) (*(struct __large_struct *)(x))
391 * Tell gcc we read from memory instead of writing: this is because
392 * we do not write to any memory gcc knows about, so there are no
395 #define __put_user_asm(x, addr, err, itype) \
396 __asm__ __volatile__( \
398 "1: st"itype" %1,@%2\n" \
401 ".section .fixup,\"ax\"\n" \
404 " seth r14,#high(2b)\n" \
405 " or3 r14,r14,#low(2b)\n" \
408 ".section __ex_table,\"a\"\n" \
413 : "r" (x), "r" (addr), "i" (-EFAULT), "0" (err) \
417 * Here we special-case 1, 2 and 4-byte copy_*_user invocations. On a fault
418 * we return the initial request size (1, 2 or 4), as copy_*_user should do.
419 * If a store crosses a page boundary and gets a fault, the m32r will not write
420 * anything, so this is accurate.
424 * Copy To/From Userspace
427 /* Generic arbitrary sized copy. */
428 /* Return the number of bytes NOT copied. */
429 #define __copy_user(to, from, size) \
431 unsigned long __dst, __src, __c; \
432 __asm__ __volatile__ ( \
435 " beq %0, %1, 9f\n" \
437 " and3 r14, r14, #3\n" \
439 " and3 %2, %2, #3\n" \
441 " addi %0, #-4 ; word_copy \n" \
443 "0: ld r14, @%1+\n" \
446 "1: st r14, @+%0\n" \
451 "2: ldb r14, @%1 ; byte_copy \n" \
453 "3: stb r14, @%0\n" \
460 ".section .fixup,\"ax\"\n" \
469 "7: seth r14, #high(9b)\n" \
470 " or3 r14, r14, #low(9b)\n" \
473 ".section __ex_table,\"a\"\n" \
480 : "=&r" (__dst), "=&r" (__src), "=&r" (size), \
482 : "0" (to), "1" (from), "2" (size), "3" (size / 4) \
483 : "r14", "memory"); \
486 #define __copy_user_zeroing(to, from, size) \
488 unsigned long __dst, __src, __c; \
489 __asm__ __volatile__ ( \
492 " beq %0, %1, 9f\n" \
494 " and3 r14, r14, #3\n" \
496 " and3 %2, %2, #3\n" \
498 " addi %0, #-4 ; word_copy \n" \
500 "0: ld r14, @%1+\n" \
503 "1: st r14, @+%0\n" \
508 "2: ldb r14, @%1 ; byte_copy \n" \
510 "3: stb r14, @%0\n" \
517 ".section .fixup,\"ax\"\n" \
526 "7: ldi r14, #0 ; store zero \n" \
528 "8: addi %2, #-1\n" \
529 " stb r14, @%0 ; ACE? \n" \
532 " seth r14, #high(9b)\n" \
533 " or3 r14, r14, #low(9b)\n" \
536 ".section __ex_table,\"a\"\n" \
543 : "=&r" (__dst), "=&r" (__src), "=&r" (size), \
545 : "0" (to), "1" (from), "2" (size), "3" (size / 4) \
546 : "r14", "memory"); \
550 /* We let the __ versions of copy_from/to_user inline, because they're often
551 * used in fast paths and have only a small space overhead.
553 static inline unsigned long __generic_copy_from_user_nocheck(void *to
,
554 const void __user
*from
, unsigned long n
)
556 __copy_user_zeroing(to
, from
, n
);
560 static inline unsigned long __generic_copy_to_user_nocheck(void __user
*to
,
561 const void *from
, unsigned long n
)
563 __copy_user(to
, from
, n
);
567 unsigned long __generic_copy_to_user(void __user
*, const void *, unsigned long);
568 unsigned long __generic_copy_from_user(void *, const void __user
*, unsigned long);
571 * __copy_to_user: - Copy a block of data into user space, with less checking.
572 * @to: Destination address, in user space.
573 * @from: Source address, in kernel space.
574 * @n: Number of bytes to copy.
576 * Context: User context only. This function may sleep if pagefaults are
579 * Copy data from kernel space to user space. Caller must check
580 * the specified block with access_ok() before calling this function.
582 * Returns number of bytes that could not be copied.
583 * On success, this will be zero.
585 #define __copy_to_user(to, from, n) \
586 __generic_copy_to_user_nocheck((to), (from), (n))
588 #define __copy_to_user_inatomic __copy_to_user
589 #define __copy_from_user_inatomic __copy_from_user
592 * copy_to_user: - Copy a block of data into user space.
593 * @to: Destination address, in user space.
594 * @from: Source address, in kernel space.
595 * @n: Number of bytes to copy.
597 * Context: User context only. This function may sleep if pagefaults are
600 * Copy data from kernel space to user space.
602 * Returns number of bytes that could not be copied.
603 * On success, this will be zero.
605 #define copy_to_user(to, from, n) \
608 __generic_copy_to_user((to), (from), (n)); \
612 * __copy_from_user: - Copy a block of data from user space, with less checking. * @to: Destination address, in kernel space.
613 * @from: Source address, in user space.
614 * @n: Number of bytes to copy.
616 * Context: User context only. This function may sleep if pagefaults are
619 * Copy data from user space to kernel space. Caller must check
620 * the specified block with access_ok() before calling this function.
622 * Returns number of bytes that could not be copied.
623 * On success, this will be zero.
625 * If some data could not be copied, this function will pad the copied
626 * data to the requested size using zero bytes.
628 #define __copy_from_user(to, from, n) \
629 __generic_copy_from_user_nocheck((to), (from), (n))
632 * copy_from_user: - Copy a block of data from user space.
633 * @to: Destination address, in kernel space.
634 * @from: Source address, in user space.
635 * @n: Number of bytes to copy.
637 * Context: User context only. This function may sleep if pagefaults are
640 * Copy data from user space to kernel space.
642 * Returns number of bytes that could not be copied.
643 * On success, this will be zero.
645 * If some data could not be copied, this function will pad the copied
646 * data to the requested size using zero bytes.
648 #define copy_from_user(to, from, n) \
651 __generic_copy_from_user((to), (from), (n)); \
654 long __must_check
strncpy_from_user(char *dst
, const char __user
*src
,
656 long __must_check
__strncpy_from_user(char *dst
,
657 const char __user
*src
, long count
);
660 * __clear_user: - Zero a block of memory in user space, with less checking.
661 * @to: Destination address, in user space.
662 * @n: Number of bytes to zero.
664 * Zero a block of memory in user space. Caller must check
665 * the specified block with access_ok() before calling this function.
667 * Returns number of bytes that could not be cleared.
668 * On success, this will be zero.
670 unsigned long __clear_user(void __user
*mem
, unsigned long len
);
673 * clear_user: - Zero a block of memory in user space.
674 * @to: Destination address, in user space.
675 * @n: Number of bytes to zero.
677 * Zero a block of memory in user space. Caller must check
678 * the specified block with access_ok() before calling this function.
680 * Returns number of bytes that could not be cleared.
681 * On success, this will be zero.
683 unsigned long clear_user(void __user
*mem
, unsigned long len
);
686 * strlen_user: - Get the size of a string in user space.
687 * @str: The string to measure.
689 * Context: User context only. This function may sleep if pagefaults are
692 * Get the size of a NUL-terminated string in user space.
694 * Returns the size of the string INCLUDING the terminating NUL.
695 * On exception, returns 0.
697 * If there is a limit on the length of a valid string, you may wish to
698 * consider using strnlen_user() instead.
700 #define strlen_user(str) strnlen_user(str, ~0UL >> 1)
701 long strnlen_user(const char __user
*str
, long n
);
703 #endif /* _ASM_M32R_UACCESS_H */