1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_M32R_UACCESS_H
3 #define _ASM_M32R_UACCESS_H
6 * linux/include/asm-m32r/uaccess.h
9 * Copyright (C) 2004, 2006 Hirokazu Takata <takata at linux-m32r.org>
13 * User space memory access functions
16 #include <asm/setup.h>
17 #include <linux/prefetch.h>
20 * The fs value determines whether argument validity checking should be
21 * performed or not. If get_fs() == USER_DS, checking is performed, with
22 * get_fs() == KERNEL_DS, checking is bypassed.
24 * For historical reasons, these macros are grossly misnamed.
27 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
31 #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF)
32 #define USER_DS MAKE_MM_SEG(PAGE_OFFSET)
33 #define get_ds() (KERNEL_DS)
34 #define get_fs() (current_thread_info()->addr_limit)
35 #define set_fs(x) (current_thread_info()->addr_limit = (x))
37 #else /* not CONFIG_MMU */
39 #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF)
40 #define USER_DS MAKE_MM_SEG(0xFFFFFFFF)
41 #define get_ds() (KERNEL_DS)
43 static inline mm_segment_t
get_fs(void)
48 static inline void set_fs(mm_segment_t s
)
52 #endif /* not CONFIG_MMU */
54 #define segment_eq(a, b) ((a).seg == (b).seg)
56 #define __addr_ok(addr) \
57 ((unsigned long)(addr) < (current_thread_info()->addr_limit.seg))
60 * Test whether a block of memory is a valid user space address.
61 * Returns 0 if the range is valid, nonzero otherwise.
63 * This is equivalent to the following test:
64 * (u33)addr + (u33)size >= (u33)current->addr_limit.seg
66 * This needs 33-bit arithmetic. We have a carry...
68 #define __range_ok(addr, size) ({ \
69 unsigned long flag, roksum; \
70 __chk_user_ptr(addr); \
72 " cmpu %1, %1 ; clear cbit\n" \
73 " addx %1, %3 ; set cbit if overflow\n" \
77 : "=&r" (flag), "=r" (roksum) \
78 : "1" (addr), "r" ((int)(size)), \
79 "r" (current_thread_info()->addr_limit.seg), "r" (0) \
84 * access_ok: - Checks if a user space pointer is valid
85 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
86 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
87 * to write to a block, it is always safe to read from it.
88 * @addr: User space pointer to start of block to check
89 * @size: Size of block to check
91 * Context: User context only. This function may sleep if pagefaults are
94 * Checks if a pointer to a block of memory in user space is valid.
96 * Returns true (nonzero) if the memory block may be valid, false (zero)
97 * if it is definitely invalid.
99 * Note that, depending on architecture, this function probably just
100 * checks that the pointer is in the user space range - after calling
101 * this function, memory access functions may still return -EFAULT.
104 #define access_ok(type, addr, size) (likely(__range_ok(addr, size) == 0))
106 static inline int access_ok(int type
, const void *addr
, unsigned long size
)
108 unsigned long val
= (unsigned long)addr
;
110 return ((val
>= memory_start
) && ((val
+ size
) < memory_end
));
112 #endif /* CONFIG_MMU */
114 #include <asm/extable.h>
117 * These are the main single-value transfer routines. They automatically
118 * use the right size if we just have the right pointer type.
120 * This gets kind of ugly. We want to return _two_ values in "get_user()"
121 * and yet we don't want to do any pointers, because that is too much
122 * of a performance impact. Thus we have a few rather ugly macros here,
123 * and hide all the uglyness from the user.
125 * The "__xxx" versions of the user access functions are versions that
126 * do not verify the address space, that must have been done previously
127 * with a separate "access_ok()" call (this is used when we do multiple
128 * accesses to the same area of user memory).
131 /* Careful: we have to cast the result to the type of the pointer for sign
134 * get_user: - Get a simple variable from user space.
135 * @x: Variable to store result.
136 * @ptr: Source address, in user space.
138 * Context: User context only. This function may sleep if pagefaults are
141 * This macro copies a single simple variable from user space to kernel
142 * space. It supports simple types like char and int, but not larger
143 * data types like structures or arrays.
145 * @ptr must have pointer-to-simple-variable type, and the result of
146 * dereferencing @ptr must be assignable to @x without a cast.
148 * Returns zero on success, or -EFAULT on error.
149 * On error, the variable @x is set to zero.
151 #define get_user(x, ptr) \
152 __get_user_check((x), (ptr), sizeof(*(ptr)))
155 * put_user: - Write a simple value into user space.
156 * @x: Value to copy to user space.
157 * @ptr: Destination address, in user space.
159 * Context: User context only. This function may sleep if pagefaults are
162 * This macro copies a single simple value from kernel space to user
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 @x must be assignable
167 * to the result of dereferencing @ptr.
169 * Returns zero on success, or -EFAULT on error.
171 #define put_user(x, ptr) \
172 __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
175 * __get_user: - Get a simple variable from user space, with less checking.
176 * @x: Variable to store result.
177 * @ptr: Source address, in user space.
179 * Context: User context only. This function may sleep if pagefaults are
182 * This macro copies a single simple variable from user space to kernel
183 * space. It supports simple types like char and int, but not larger
184 * data types like structures or arrays.
186 * @ptr must have pointer-to-simple-variable type, and the result of
187 * dereferencing @ptr must be assignable to @x without a cast.
189 * Caller must check the pointer with access_ok() before calling this
192 * Returns zero on success, or -EFAULT on error.
193 * On error, the variable @x is set to zero.
195 #define __get_user(x, ptr) \
196 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
198 #define __get_user_nocheck(x, ptr, size) \
201 unsigned long __gu_val = 0; \
203 __get_user_size(__gu_val, (ptr), (size), __gu_err); \
204 (x) = (__force __typeof__(*(ptr)))__gu_val; \
208 #define __get_user_check(x, ptr, size) \
210 long __gu_err = -EFAULT; \
211 unsigned long __gu_val = 0; \
212 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
214 if (access_ok(VERIFY_READ, __gu_addr, size)) \
215 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
216 (x) = (__force __typeof__(*(ptr)))__gu_val; \
220 extern long __get_user_bad(void);
222 #define __get_user_size(x, ptr, size, retval) \
225 __chk_user_ptr(ptr); \
227 case 1: __get_user_asm(x, ptr, retval, "ub"); break; \
228 case 2: __get_user_asm(x, ptr, retval, "uh"); break; \
229 case 4: __get_user_asm(x, ptr, retval, ""); break; \
230 default: (x) = __get_user_bad(); \
234 #define __get_user_asm(x, addr, err, itype) \
235 __asm__ __volatile__( \
237 "1: ld"itype" %1,@%2\n" \
240 ".section .fixup,\"ax\"\n" \
243 " seth r14,#high(2b)\n" \
244 " or3 r14,r14,#low(2b)\n" \
247 ".section __ex_table,\"a\"\n" \
251 : "=&r" (err), "=&r" (x) \
252 : "r" (addr), "i" (-EFAULT), "0" (err) \
256 * __put_user: - Write a simple value into user space, with less checking.
257 * @x: Value to copy to user space.
258 * @ptr: Destination address, in user space.
260 * Context: User context only. This function may sleep if pagefaults are
263 * This macro copies a single simple value from kernel space to user
264 * space. It supports simple types like char and int, but not larger
265 * data types like structures or arrays.
267 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
268 * to the result of dereferencing @ptr.
270 * Caller must check the pointer with access_ok() before calling this
273 * Returns zero on success, or -EFAULT on error.
275 #define __put_user(x, ptr) \
276 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
279 #define __put_user_nocheck(x, ptr, size) \
283 __put_user_size((x), (ptr), (size), __pu_err); \
288 #define __put_user_check(x, ptr, size) \
290 long __pu_err = -EFAULT; \
291 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
293 if (access_ok(VERIFY_WRITE, __pu_addr, size)) \
294 __put_user_size((x), __pu_addr, (size), __pu_err); \
298 #if defined(__LITTLE_ENDIAN__)
299 #define __put_user_u64(x, addr, err) \
300 __asm__ __volatile__( \
304 "2: st %H1,@(4,%2)\n" \
307 ".section .fixup,\"ax\"\n" \
310 " seth r14,#high(3b)\n" \
311 " or3 r14,r14,#low(3b)\n" \
314 ".section __ex_table,\"a\"\n" \
320 : "r" (x), "r" (addr), "i" (-EFAULT), "0" (err) \
323 #elif defined(__BIG_ENDIAN__)
324 #define __put_user_u64(x, addr, err) \
325 __asm__ __volatile__( \
329 "2: st %L1,@(4,%2)\n" \
332 ".section .fixup,\"ax\"\n" \
335 " seth r14,#high(3b)\n" \
336 " or3 r14,r14,#low(3b)\n" \
339 ".section __ex_table,\"a\"\n" \
345 : "r" (x), "r" (addr), "i" (-EFAULT), "0" (err) \
348 #error no endian defined
351 extern void __put_user_bad(void);
353 #define __put_user_size(x, ptr, size, retval) \
356 __chk_user_ptr(ptr); \
358 case 1: __put_user_asm(x, ptr, retval, "b"); break; \
359 case 2: __put_user_asm(x, ptr, retval, "h"); break; \
360 case 4: __put_user_asm(x, ptr, retval, ""); break; \
361 case 8: __put_user_u64((__typeof__(*ptr))(x), ptr, retval); break;\
362 default: __put_user_bad(); \
366 struct __large_struct
{ unsigned long buf
[100]; };
367 #define __m(x) (*(struct __large_struct *)(x))
370 * Tell gcc we read from memory instead of writing: this is because
371 * we do not write to any memory gcc knows about, so there are no
374 #define __put_user_asm(x, addr, err, itype) \
375 __asm__ __volatile__( \
377 "1: st"itype" %1,@%2\n" \
380 ".section .fixup,\"ax\"\n" \
383 " seth r14,#high(2b)\n" \
384 " or3 r14,r14,#low(2b)\n" \
387 ".section __ex_table,\"a\"\n" \
392 : "r" (x), "r" (addr), "i" (-EFAULT), "0" (err) \
396 * Here we special-case 1, 2 and 4-byte copy_*_user invocations. On a fault
397 * we return the initial request size (1, 2 or 4), as copy_*_user should do.
398 * If a store crosses a page boundary and gets a fault, the m32r will not write
399 * anything, so this is accurate.
403 * Copy To/From Userspace
406 /* Generic arbitrary sized copy. */
407 /* Return the number of bytes NOT copied. */
408 #define __copy_user(to, from, size) \
410 unsigned long __dst, __src, __c; \
411 __asm__ __volatile__ ( \
414 " beq %0, %1, 9f\n" \
416 " and3 r14, r14, #3\n" \
418 " and3 %2, %2, #3\n" \
420 " addi %0, #-4 ; word_copy \n" \
422 "0: ld r14, @%1+\n" \
425 "1: st r14, @+%0\n" \
430 "2: ldb r14, @%1 ; byte_copy \n" \
432 "3: stb r14, @%0\n" \
439 ".section .fixup,\"ax\"\n" \
448 "7: seth r14, #high(9b)\n" \
449 " or3 r14, r14, #low(9b)\n" \
452 ".section __ex_table,\"a\"\n" \
459 : "=&r" (__dst), "=&r" (__src), "=&r" (size), \
461 : "0" (to), "1" (from), "2" (size), "3" (size / 4) \
462 : "r14", "memory"); \
465 /* We let the __ versions of copy_from/to_user inline, because they're often
466 * used in fast paths and have only a small space overhead.
468 static inline unsigned long
469 raw_copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
472 __copy_user(to
, from
, n
);
476 static inline unsigned long
477 raw_copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
480 __copy_user(to
, from
, n
);
484 long __must_check
strncpy_from_user(char *dst
, const char __user
*src
,
488 * __clear_user: - Zero a block of memory in user space, with less checking.
489 * @to: Destination address, in user space.
490 * @n: Number of bytes to zero.
492 * Zero a block of memory in user space. Caller must check
493 * the specified block with access_ok() before calling this function.
495 * Returns number of bytes that could not be cleared.
496 * On success, this will be zero.
498 unsigned long __clear_user(void __user
*mem
, unsigned long len
);
501 * clear_user: - Zero a block of memory in user space.
502 * @to: Destination address, in user space.
503 * @n: Number of bytes to zero.
505 * Zero a block of memory in user space. Caller must check
506 * the specified block with access_ok() before calling this function.
508 * Returns number of bytes that could not be cleared.
509 * On success, this will be zero.
511 unsigned long clear_user(void __user
*mem
, unsigned long len
);
513 long strnlen_user(const char __user
*str
, long n
);
515 #endif /* _ASM_M32R_UACCESS_H */