xref: /openbmc/linux/arch/sh/include/asm/uaccess.h (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2f15cbe6fSPaul Mundt #ifndef __ASM_SH_UACCESS_H
3f15cbe6fSPaul Mundt #define __ASM_SH_UACCESS_H
4f15cbe6fSPaul Mundt 
5bcd541d9SAl Viro #include <asm/extable.h>
6*12700c17SArnd Bergmann #include <asm-generic/access_ok.h>
7*12700c17SArnd Bergmann 
8f15cbe6fSPaul Mundt /*
9f15cbe6fSPaul Mundt  * Uh, these should become the main single-value transfer routines ...
10f15cbe6fSPaul Mundt  * They automatically use the right size if we just have the right
11f15cbe6fSPaul Mundt  * pointer type ...
12f15cbe6fSPaul Mundt  *
13f15cbe6fSPaul Mundt  * As SuperH uses the same address space for kernel and user data, we
14f15cbe6fSPaul Mundt  * can just do these as direct assignments.
15f15cbe6fSPaul Mundt  *
16f15cbe6fSPaul Mundt  * Careful to not
17f15cbe6fSPaul Mundt  * (a) re-use the arguments for side effects (sizeof is ok)
18f15cbe6fSPaul Mundt  * (b) require any knowledge of processes at this stage
19f15cbe6fSPaul Mundt  */
20f15cbe6fSPaul Mundt #define put_user(x,ptr)		__put_user_check((x), (ptr), sizeof(*(ptr)))
21f15cbe6fSPaul Mundt #define get_user(x,ptr)		__get_user_check((x), (ptr), sizeof(*(ptr)))
22f15cbe6fSPaul Mundt 
23f15cbe6fSPaul Mundt /*
24f15cbe6fSPaul Mundt  * The "__xxx" versions do not do address space checking, useful when
25f15cbe6fSPaul Mundt  * doing multiple accesses to the same area (the user has to do the
26f15cbe6fSPaul Mundt  * checks by hand with "access_ok()")
27f15cbe6fSPaul Mundt  */
28f15cbe6fSPaul Mundt #define __put_user(x,ptr)	__put_user_nocheck((x), (ptr), sizeof(*(ptr)))
29f15cbe6fSPaul Mundt #define __get_user(x,ptr)	__get_user_nocheck((x), (ptr), sizeof(*(ptr)))
30f15cbe6fSPaul Mundt 
31f15cbe6fSPaul Mundt struct __large_struct { unsigned long buf[100]; };
32f15cbe6fSPaul Mundt #define __m(x) (*(struct __large_struct __user *)(x))
33f15cbe6fSPaul Mundt 
34f15cbe6fSPaul Mundt #define __get_user_nocheck(x,ptr,size)				\
35f15cbe6fSPaul Mundt ({								\
36f15cbe6fSPaul Mundt 	long __gu_err;						\
37f15cbe6fSPaul Mundt 	unsigned long __gu_val;					\
38f15cbe6fSPaul Mundt 	const __typeof__(*(ptr)) __user *__gu_addr = (ptr);	\
39f15cbe6fSPaul Mundt 	__chk_user_ptr(ptr);					\
40f15cbe6fSPaul Mundt 	__get_user_size(__gu_val, __gu_addr, (size), __gu_err);	\
41cad1c0dfSMichael S. Tsirkin 	(x) = (__force __typeof__(*(ptr)))__gu_val;		\
42f15cbe6fSPaul Mundt 	__gu_err;						\
43f15cbe6fSPaul Mundt })
44f15cbe6fSPaul Mundt 
45f15cbe6fSPaul Mundt #define __get_user_check(x,ptr,size)					\
46f15cbe6fSPaul Mundt ({									\
47f15cbe6fSPaul Mundt 	long __gu_err = -EFAULT;					\
48f15cbe6fSPaul Mundt 	unsigned long __gu_val = 0;					\
49ca42bc4bSAl Viro 	const __typeof__(*(ptr)) __user *__gu_addr = (ptr);			\
5096d4f267SLinus Torvalds 	if (likely(access_ok(__gu_addr, (size))))		\
51f15cbe6fSPaul Mundt 		__get_user_size(__gu_val, __gu_addr, (size), __gu_err);	\
52cad1c0dfSMichael S. Tsirkin 	(x) = (__force __typeof__(*(ptr)))__gu_val;			\
53f15cbe6fSPaul Mundt 	__gu_err;							\
54f15cbe6fSPaul Mundt })
55f15cbe6fSPaul Mundt 
56f15cbe6fSPaul Mundt #define __put_user_nocheck(x,ptr,size)				\
57f15cbe6fSPaul Mundt ({								\
58f15cbe6fSPaul Mundt 	long __pu_err;						\
59f15cbe6fSPaul Mundt 	__typeof__(*(ptr)) __user *__pu_addr = (ptr);		\
606de9c648SOGAWA Hirofumi 	__typeof__(*(ptr)) __pu_val = x;			\
61f15cbe6fSPaul Mundt 	__chk_user_ptr(ptr);					\
626de9c648SOGAWA Hirofumi 	__put_user_size(__pu_val, __pu_addr, (size), __pu_err);	\
63f15cbe6fSPaul Mundt 	__pu_err;						\
64f15cbe6fSPaul Mundt })
65f15cbe6fSPaul Mundt 
66f15cbe6fSPaul Mundt #define __put_user_check(x,ptr,size)				\
67f15cbe6fSPaul Mundt ({								\
68f15cbe6fSPaul Mundt 	long __pu_err = -EFAULT;				\
69f15cbe6fSPaul Mundt 	__typeof__(*(ptr)) __user *__pu_addr = (ptr);		\
706de9c648SOGAWA Hirofumi 	__typeof__(*(ptr)) __pu_val = x;			\
7196d4f267SLinus Torvalds 	if (likely(access_ok(__pu_addr, size)))	\
726de9c648SOGAWA Hirofumi 		__put_user_size(__pu_val, __pu_addr, (size),	\
73f15cbe6fSPaul Mundt 				__pu_err);			\
74f15cbe6fSPaul Mundt 	__pu_err;						\
75f15cbe6fSPaul Mundt })
76f15cbe6fSPaul Mundt 
77a1ce3928SDavid Howells # include <asm/uaccess_32.h>
78f15cbe6fSPaul Mundt 
790e100e11SPaul Mundt extern long strncpy_from_user(char *dest, const char __user *src, long count);
800e100e11SPaul Mundt 
81cba8df4bSPaul Mundt extern __must_check long strnlen_user(const char __user *str, long n);
82cba8df4bSPaul Mundt 
83f15cbe6fSPaul Mundt /* Generic arbitrary sized copy.  */
84f15cbe6fSPaul Mundt /* Return the number of bytes NOT copied */
85f15cbe6fSPaul Mundt __kernel_size_t __copy_user(void *to, const void *from, __kernel_size_t n);
86f15cbe6fSPaul Mundt 
87f15cbe6fSPaul Mundt static __always_inline unsigned long
raw_copy_from_user(void * to,const void __user * from,unsigned long n)88f98f48eeSAl Viro raw_copy_from_user(void *to, const void __user *from, unsigned long n)
89f15cbe6fSPaul Mundt {
90f15cbe6fSPaul Mundt 	return __copy_user(to, (__force void *)from, n);
91f15cbe6fSPaul Mundt }
92f15cbe6fSPaul Mundt 
93f15cbe6fSPaul Mundt static __always_inline unsigned long __must_check
raw_copy_to_user(void __user * to,const void * from,unsigned long n)94f98f48eeSAl Viro raw_copy_to_user(void __user *to, const void *from, unsigned long n)
95f15cbe6fSPaul Mundt {
96f15cbe6fSPaul Mundt 	return __copy_user((__force void *)to, from, n);
97f15cbe6fSPaul Mundt }
98f98f48eeSAl Viro #define INLINE_COPY_FROM_USER
99f98f48eeSAl Viro #define INLINE_COPY_TO_USER
100f15cbe6fSPaul Mundt 
101f15cbe6fSPaul Mundt /*
102f15cbe6fSPaul Mundt  * Clear the area and return remaining number of bytes
103f15cbe6fSPaul Mundt  * (on failure.  Usually it's 0.)
104f15cbe6fSPaul Mundt  */
105ca42bc4bSAl Viro __kernel_size_t __clear_user(void __user *addr, __kernel_size_t size);
106f15cbe6fSPaul Mundt 
107f15cbe6fSPaul Mundt #define clear_user(addr,n)						\
108f15cbe6fSPaul Mundt ({									\
109f15cbe6fSPaul Mundt 	void __user * __cl_addr = (addr);				\
110f15cbe6fSPaul Mundt 	unsigned long __cl_size = (n);					\
111f15cbe6fSPaul Mundt 									\
11296d4f267SLinus Torvalds 	if (__cl_size && access_ok(__cl_addr, __cl_size))		\
113f15cbe6fSPaul Mundt 		__cl_size = __clear_user(__cl_addr, __cl_size);		\
114f15cbe6fSPaul Mundt 									\
115f15cbe6fSPaul Mundt 	__cl_size;							\
116f15cbe6fSPaul Mundt })
117f15cbe6fSPaul Mundt 
118e839ca52SDavid Howells extern void *set_exception_table_vec(unsigned int vec, void *handler);
119e839ca52SDavid Howells 
set_exception_table_evt(unsigned int evt,void * handler)120e839ca52SDavid Howells static inline void *set_exception_table_evt(unsigned int evt, void *handler)
121e839ca52SDavid Howells {
122e839ca52SDavid Howells 	return set_exception_table_vec(evt >> 5, handler);
123e839ca52SDavid Howells }
124e839ca52SDavid Howells 
125e839ca52SDavid Howells struct mem_access {
126e839ca52SDavid Howells 	unsigned long (*from)(void *dst, const void __user *src, unsigned long cnt);
127e839ca52SDavid Howells 	unsigned long (*to)(void __user *dst, const void *src, unsigned long cnt);
128e839ca52SDavid Howells };
129e839ca52SDavid Howells 
130e839ca52SDavid Howells int handle_unaligned_access(insn_size_t instruction, struct pt_regs *regs,
131e839ca52SDavid Howells 			    struct mem_access *ma, int, unsigned long address);
132f15cbe6fSPaul Mundt 
133f15cbe6fSPaul Mundt #endif /* __ASM_SH_UACCESS_H */
134