xref: /linux/arch/sh/include/asm/uaccess.h (revision 7cefa5a05dbda1f0bbbd98e9d2861b09a35cc6ea)
1 #ifndef __ASM_SH_UACCESS_H
2 #define __ASM_SH_UACCESS_H
3 
4 #include <asm/segment.h>
5 
6 #define __addr_ok(addr) \
7 	((unsigned long __force)(addr) < current_thread_info()->addr_limit.seg)
8 
9 /*
10  * __access_ok: Check if address with size is OK or not.
11  *
12  * Uhhuh, this needs 33-bit arithmetic. We have a carry..
13  *
14  * sum := addr + size;  carry? --> flag = true;
15  * if (sum >= addr_limit) flag = true;
16  */
17 #define __access_ok(addr, size)		\
18 	(__addr_ok((addr) + (size)))
19 #define access_ok(type, addr, size)	\
20 	(__chk_user_ptr(addr),		\
21 	 __access_ok((unsigned long __force)(addr), (size)))
22 
23 #define user_addr_max()	(current_thread_info()->addr_limit.seg)
24 
25 /*
26  * Uh, these should become the main single-value transfer routines ...
27  * They automatically use the right size if we just have the right
28  * pointer type ...
29  *
30  * As SuperH uses the same address space for kernel and user data, we
31  * can just do these as direct assignments.
32  *
33  * Careful to not
34  * (a) re-use the arguments for side effects (sizeof is ok)
35  * (b) require any knowledge of processes at this stage
36  */
37 #define put_user(x,ptr)		__put_user_check((x), (ptr), sizeof(*(ptr)))
38 #define get_user(x,ptr)		__get_user_check((x), (ptr), sizeof(*(ptr)))
39 
40 /*
41  * The "__xxx" versions do not do address space checking, useful when
42  * doing multiple accesses to the same area (the user has to do the
43  * checks by hand with "access_ok()")
44  */
45 #define __put_user(x,ptr)	__put_user_nocheck((x), (ptr), sizeof(*(ptr)))
46 #define __get_user(x,ptr)	__get_user_nocheck((x), (ptr), sizeof(*(ptr)))
47 
48 struct __large_struct { unsigned long buf[100]; };
49 #define __m(x) (*(struct __large_struct __user *)(x))
50 
51 #define __get_user_nocheck(x,ptr,size)				\
52 ({								\
53 	long __gu_err;						\
54 	unsigned long __gu_val;					\
55 	const __typeof__(*(ptr)) __user *__gu_addr = (ptr);	\
56 	__chk_user_ptr(ptr);					\
57 	__get_user_size(__gu_val, __gu_addr, (size), __gu_err);	\
58 	(x) = (__force __typeof__(*(ptr)))__gu_val;		\
59 	__gu_err;						\
60 })
61 
62 #define __get_user_check(x,ptr,size)					\
63 ({									\
64 	long __gu_err = -EFAULT;					\
65 	unsigned long __gu_val = 0;					\
66 	const __typeof__(*(ptr)) *__gu_addr = (ptr);			\
67 	if (likely(access_ok(VERIFY_READ, __gu_addr, (size))))		\
68 		__get_user_size(__gu_val, __gu_addr, (size), __gu_err);	\
69 	(x) = (__force __typeof__(*(ptr)))__gu_val;			\
70 	__gu_err;							\
71 })
72 
73 #define __put_user_nocheck(x,ptr,size)				\
74 ({								\
75 	long __pu_err;						\
76 	__typeof__(*(ptr)) __user *__pu_addr = (ptr);		\
77 	__typeof__(*(ptr)) __pu_val = x;			\
78 	__chk_user_ptr(ptr);					\
79 	__put_user_size(__pu_val, __pu_addr, (size), __pu_err);	\
80 	__pu_err;						\
81 })
82 
83 #define __put_user_check(x,ptr,size)				\
84 ({								\
85 	long __pu_err = -EFAULT;				\
86 	__typeof__(*(ptr)) __user *__pu_addr = (ptr);		\
87 	__typeof__(*(ptr)) __pu_val = x;			\
88 	if (likely(access_ok(VERIFY_WRITE, __pu_addr, size)))	\
89 		__put_user_size(__pu_val, __pu_addr, (size),	\
90 				__pu_err);			\
91 	__pu_err;						\
92 })
93 
94 #ifdef CONFIG_SUPERH32
95 # include <asm/uaccess_32.h>
96 #else
97 # include <asm/uaccess_64.h>
98 #endif
99 
100 extern long strncpy_from_user(char *dest, const char __user *src, long count);
101 
102 extern __must_check long strlen_user(const char __user *str);
103 extern __must_check long strnlen_user(const char __user *str, long n);
104 
105 /* Generic arbitrary sized copy.  */
106 /* Return the number of bytes NOT copied */
107 __kernel_size_t __copy_user(void *to, const void *from, __kernel_size_t n);
108 
109 static __always_inline unsigned long
110 __copy_from_user(void *to, const void __user *from, unsigned long n)
111 {
112 	return __copy_user(to, (__force void *)from, n);
113 }
114 
115 static __always_inline unsigned long __must_check
116 __copy_to_user(void __user *to, const void *from, unsigned long n)
117 {
118 	return __copy_user((__force void *)to, from, n);
119 }
120 
121 #define __copy_to_user_inatomic __copy_to_user
122 #define __copy_from_user_inatomic __copy_from_user
123 
124 /*
125  * Clear the area and return remaining number of bytes
126  * (on failure.  Usually it's 0.)
127  */
128 __kernel_size_t __clear_user(void *addr, __kernel_size_t size);
129 
130 #define clear_user(addr,n)						\
131 ({									\
132 	void __user * __cl_addr = (addr);				\
133 	unsigned long __cl_size = (n);					\
134 									\
135 	if (__cl_size && access_ok(VERIFY_WRITE,			\
136 		((unsigned long)(__cl_addr)), __cl_size))		\
137 		__cl_size = __clear_user(__cl_addr, __cl_size);		\
138 									\
139 	__cl_size;							\
140 })
141 
142 static inline unsigned long
143 copy_from_user(void *to, const void __user *from, unsigned long n)
144 {
145 	unsigned long __copy_from = (unsigned long) from;
146 	__kernel_size_t __copy_size = (__kernel_size_t) n;
147 
148 	if (__copy_size && __access_ok(__copy_from, __copy_size))
149 		__copy_size = __copy_user(to, from, __copy_size);
150 
151 	if (unlikely(__copy_size))
152 		memset(to + (n - __copy_size), 0, __copy_size);
153 
154 	return __copy_size;
155 }
156 
157 static inline unsigned long
158 copy_to_user(void __user *to, const void *from, unsigned long n)
159 {
160 	unsigned long __copy_to = (unsigned long) to;
161 	__kernel_size_t __copy_size = (__kernel_size_t) n;
162 
163 	if (__copy_size && __access_ok(__copy_to, __copy_size))
164 		return __copy_user(to, from, __copy_size);
165 
166 	return __copy_size;
167 }
168 
169 /*
170  * The exception table consists of pairs of addresses: the first is the
171  * address of an instruction that is allowed to fault, and the second is
172  * the address at which the program should continue.  No registers are
173  * modified, so it is entirely up to the continuation code to figure out
174  * what to do.
175  *
176  * All the routines below use bits of fixup code that are out of line
177  * with the main instruction path.  This means when everything is well,
178  * we don't even have to jump over them.  Further, they do not intrude
179  * on our cache or tlb entries.
180  */
181 struct exception_table_entry {
182 	unsigned long insn, fixup;
183 };
184 
185 #if defined(CONFIG_SUPERH64) && defined(CONFIG_MMU)
186 #define ARCH_HAS_SEARCH_EXTABLE
187 #endif
188 
189 int fixup_exception(struct pt_regs *regs);
190 
191 extern void *set_exception_table_vec(unsigned int vec, void *handler);
192 
193 static inline void *set_exception_table_evt(unsigned int evt, void *handler)
194 {
195 	return set_exception_table_vec(evt >> 5, handler);
196 }
197 
198 struct mem_access {
199 	unsigned long (*from)(void *dst, const void __user *src, unsigned long cnt);
200 	unsigned long (*to)(void __user *dst, const void *src, unsigned long cnt);
201 };
202 
203 int handle_unaligned_access(insn_size_t instruction, struct pt_regs *regs,
204 			    struct mem_access *ma, int, unsigned long address);
205 
206 #endif /* __ASM_SH_UACCESS_H */
207