1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#ifndef _XTENSA_UACCESS_H
17#define _XTENSA_UACCESS_H
18
19#include <linux/prefetch.h>
20#include <asm/types.h>
21#include <asm/extable.h>
22
23
24
25
26
27
28
29
30
31
32#define KERNEL_DS ((mm_segment_t) { 0 })
33#define USER_DS ((mm_segment_t) { 1 })
34
35#define get_ds() (KERNEL_DS)
36#define get_fs() (current->thread.current_ds)
37#define set_fs(val) (current->thread.current_ds = (val))
38
39#define segment_eq(a, b) ((a).seg == (b).seg)
40
41#define __kernel_ok (uaccess_kernel())
42#define __user_ok(addr, size) \
43 (((size) <= TASK_SIZE)&&((addr) <= TASK_SIZE-(size)))
44#define __access_ok(addr, size) (__kernel_ok || __user_ok((addr), (size)))
45#define access_ok(type, addr, size) __access_ok((unsigned long)(addr), (size))
46
47#define user_addr_max() (uaccess_kernel() ? ~0UL : TASK_SIZE)
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63#define put_user(x, ptr) __put_user_check((x), (ptr), sizeof(*(ptr)))
64#define get_user(x, ptr) __get_user_check((x), (ptr), sizeof(*(ptr)))
65
66
67
68
69
70
71
72#define __put_user(x, ptr) __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
73#define __get_user(x, ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
74
75
76extern long __put_user_bad(void);
77
78#define __put_user_nocheck(x, ptr, size) \
79({ \
80 long __pu_err; \
81 __put_user_size((x), (ptr), (size), __pu_err); \
82 __pu_err; \
83})
84
85#define __put_user_check(x, ptr, size) \
86({ \
87 long __pu_err = -EFAULT; \
88 __typeof__(*(ptr)) *__pu_addr = (ptr); \
89 if (access_ok(VERIFY_WRITE, __pu_addr, size)) \
90 __put_user_size((x), __pu_addr, (size), __pu_err); \
91 __pu_err; \
92})
93
94#define __put_user_size(x, ptr, size, retval) \
95do { \
96 int __cb; \
97 retval = 0; \
98 switch (size) { \
99 case 1: __put_user_asm(x, ptr, retval, 1, "s8i", __cb); break; \
100 case 2: __put_user_asm(x, ptr, retval, 2, "s16i", __cb); break; \
101 case 4: __put_user_asm(x, ptr, retval, 4, "s32i", __cb); break; \
102 case 8: { \
103 __typeof__(*ptr) __v64 = x; \
104 retval = __copy_to_user(ptr, &__v64, 8); \
105 break; \
106 } \
107 default: __put_user_bad(); \
108 } \
109} while (0)
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133#define __check_align_1 ""
134
135#define __check_align_2 \
136 " _bbci.l %3, 0, 1f \n" \
137 " movi %0, %4 \n" \
138 " _j 2f \n"
139
140#define __check_align_4 \
141 " _bbsi.l %3, 0, 0f \n" \
142 " _bbci.l %3, 1, 1f \n" \
143 "0: movi %0, %4 \n" \
144 " _j 2f \n"
145
146
147
148
149
150
151
152
153
154
155#define __put_user_asm(x, addr, err, align, insn, cb) \
156__asm__ __volatile__( \
157 __check_align_##align \
158 "1: "insn" %2, %3, 0 \n" \
159 "2: \n" \
160 " .section .fixup,\"ax\" \n" \
161 " .align 4 \n" \
162 "4: \n" \
163 " .long 2b \n" \
164 "5: \n" \
165 " l32r %1, 4b \n" \
166 " movi %0, %4 \n" \
167 " jx %1 \n" \
168 " .previous \n" \
169 " .section __ex_table,\"a\" \n" \
170 " .long 1b, 5b \n" \
171 " .previous" \
172 :"=r" (err), "=r" (cb) \
173 :"r" ((int)(x)), "r" (addr), "i" (-EFAULT), "0" (err))
174
175#define __get_user_nocheck(x, ptr, size) \
176({ \
177 long __gu_err, __gu_val; \
178 __get_user_size(__gu_val, (ptr), (size), __gu_err); \
179 (x) = (__force __typeof__(*(ptr)))__gu_val; \
180 __gu_err; \
181})
182
183#define __get_user_check(x, ptr, size) \
184({ \
185 long __gu_err = -EFAULT, __gu_val = 0; \
186 const __typeof__(*(ptr)) *__gu_addr = (ptr); \
187 if (access_ok(VERIFY_READ, __gu_addr, size)) \
188 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
189 (x) = (__force __typeof__(*(ptr)))__gu_val; \
190 __gu_err; \
191})
192
193extern long __get_user_bad(void);
194
195#define __get_user_size(x, ptr, size, retval) \
196do { \
197 int __cb; \
198 retval = 0; \
199 switch (size) { \
200 case 1: __get_user_asm(x, ptr, retval, 1, "l8ui", __cb); break;\
201 case 2: __get_user_asm(x, ptr, retval, 2, "l16ui", __cb); break;\
202 case 4: __get_user_asm(x, ptr, retval, 4, "l32i", __cb); break;\
203 case 8: retval = __copy_from_user(&x, ptr, 8); break; \
204 default: (x) = __get_user_bad(); \
205 } \
206} while (0)
207
208
209
210
211
212
213#define __get_user_asm(x, addr, err, align, insn, cb) \
214__asm__ __volatile__( \
215 __check_align_##align \
216 "1: "insn" %2, %3, 0 \n" \
217 "2: \n" \
218 " .section .fixup,\"ax\" \n" \
219 " .align 4 \n" \
220 "4: \n" \
221 " .long 2b \n" \
222 "5: \n" \
223 " l32r %1, 4b \n" \
224 " movi %2, 0 \n" \
225 " movi %0, %4 \n" \
226 " jx %1 \n" \
227 " .previous \n" \
228 " .section __ex_table,\"a\" \n" \
229 " .long 1b, 5b \n" \
230 " .previous" \
231 :"=r" (err), "=r" (cb), "=r" (x) \
232 :"r" (addr), "i" (-EFAULT), "0" (err))
233
234
235
236
237
238
239extern unsigned __xtensa_copy_user(void *to, const void *from, unsigned n);
240
241static inline unsigned long
242raw_copy_from_user(void *to, const void __user *from, unsigned long n)
243{
244 prefetchw(to);
245 return __xtensa_copy_user(to, (__force const void *)from, n);
246}
247static inline unsigned long
248raw_copy_to_user(void __user *to, const void *from, unsigned long n)
249{
250 prefetch(from);
251 return __xtensa_copy_user((__force void *)to, from, n);
252}
253#define INLINE_COPY_FROM_USER
254#define INLINE_COPY_TO_USER
255
256
257
258
259
260
261
262
263static inline unsigned long
264__xtensa_clear_user(void *addr, unsigned long size)
265{
266 if (!__memset(addr, 0, size))
267 return size;
268 return 0;
269}
270
271static inline unsigned long
272clear_user(void *addr, unsigned long size)
273{
274 if (access_ok(VERIFY_WRITE, addr, size))
275 return __xtensa_clear_user(addr, size);
276 return size ? -EFAULT : 0;
277}
278
279#define __clear_user __xtensa_clear_user
280
281
282#ifndef CONFIG_GENERIC_STRNCPY_FROM_USER
283
284extern long __strncpy_user(char *, const char *, long);
285
286static inline long
287strncpy_from_user(char *dst, const char *src, long count)
288{
289 if (access_ok(VERIFY_READ, src, 1))
290 return __strncpy_user(dst, src, count);
291 return -EFAULT;
292}
293#else
294long strncpy_from_user(char *dst, const char *src, long count);
295#endif
296
297
298
299
300extern long __strnlen_user(const char *, long);
301
302static inline long strnlen_user(const char *str, long len)
303{
304 unsigned long top = __kernel_ok ? ~0UL : TASK_SIZE - 1;
305
306 if ((unsigned long)str > top)
307 return 0;
308 return __strnlen_user(str, len);
309}
310
311#endif
312