1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#ifndef _ASM_TILE_UACCESS_H
16#define _ASM_TILE_UACCESS_H
17
18
19
20
21#include <linux/sched.h>
22#include <linux/mm.h>
23#include <asm-generic/uaccess-unaligned.h>
24#include <asm/processor.h>
25#include <asm/page.h>
26
27#define VERIFY_READ 0
28#define VERIFY_WRITE 1
29
30
31
32
33
34
35
36
37#define MAKE_MM_SEG(a) ((mm_segment_t) { (a) })
38
39#define KERNEL_DS MAKE_MM_SEG(-1UL)
40#define USER_DS MAKE_MM_SEG(PAGE_OFFSET)
41
42#define get_ds() (KERNEL_DS)
43#define get_fs() (current_thread_info()->addr_limit)
44#define set_fs(x) (current_thread_info()->addr_limit = (x))
45
46#define segment_eq(a, b) ((a).seg == (b).seg)
47
48#ifndef __tilegx__
49
50
51
52
53
54
55static inline int is_arch_mappable_range(unsigned long addr,
56 unsigned long size)
57{
58 return (addr >= MEM_USER_INTRPT &&
59 addr < (MEM_USER_INTRPT + INTRPT_SIZE) &&
60 size <= (MEM_USER_INTRPT + INTRPT_SIZE) - addr);
61}
62#define is_arch_mappable_range is_arch_mappable_range
63#else
64#define is_arch_mappable_range(addr, size) 0
65#endif
66
67
68
69
70
71int __range_ok(unsigned long addr, unsigned long size);
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92#define access_ok(type, addr, size) ({ \
93 __chk_user_ptr(addr); \
94 likely(__range_ok((unsigned long)(addr), (size)) == 0); \
95})
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110struct exception_table_entry {
111 unsigned long insn, fixup;
112};
113
114extern int fixup_exception(struct pt_regs *regs);
115
116
117
118
119
120
121
122
123
124
125
126struct __get_user {
127 unsigned long long val;
128 int err;
129};
130
131
132
133
134
135
136
137
138
139extern struct __get_user __get_user_1(const void __user *);
140extern struct __get_user __get_user_2(const void __user *);
141extern struct __get_user __get_user_4(const void __user *);
142extern struct __get_user __get_user_8(const void __user *);
143extern int __put_user_1(long, void __user *);
144extern int __put_user_2(long, void __user *);
145extern int __put_user_4(long, void __user *);
146extern int __put_user_8(long long, void __user *);
147
148
149extern struct __get_user __get_user_bad(void);
150extern int __put_user_bad(void);
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176#define __get_user(x, ptr) \
177({ struct __get_user __ret; \
178 __typeof__(*(ptr)) const __user *__gu_addr = (ptr); \
179 __chk_user_ptr(__gu_addr); \
180 switch (sizeof(*(__gu_addr))) { \
181 case 1: \
182 __ret = __get_user_1(__gu_addr); \
183 break; \
184 case 2: \
185 __ret = __get_user_2(__gu_addr); \
186 break; \
187 case 4: \
188 __ret = __get_user_4(__gu_addr); \
189 break; \
190 case 8: \
191 __ret = __get_user_8(__gu_addr); \
192 break; \
193 default: \
194 __ret = __get_user_bad(); \
195 break; \
196 } \
197 (x) = (__typeof__(*__gu_addr)) (__typeof__(*__gu_addr - *__gu_addr)) \
198 __ret.val; \
199 __ret.err; \
200})
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227#define __put_user(x, ptr) \
228({ \
229 int __pu_err = 0; \
230 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
231 typeof(*__pu_addr) __pu_val = (x); \
232 __chk_user_ptr(__pu_addr); \
233 switch (sizeof(__pu_val)) { \
234 case 1: \
235 __pu_err = __put_user_1((long)__pu_val, __pu_addr); \
236 break; \
237 case 2: \
238 __pu_err = __put_user_2((long)__pu_val, __pu_addr); \
239 break; \
240 case 4: \
241 __pu_err = __put_user_4((long)__pu_val, __pu_addr); \
242 break; \
243 case 8: \
244 __pu_err = \
245 __put_user_8((__typeof__(__pu_val - __pu_val))__pu_val,\
246 __pu_addr); \
247 break; \
248 default: \
249 __pu_err = __put_user_bad(); \
250 break; \
251 } \
252 __pu_err; \
253})
254
255
256
257
258
259
260#define put_user(x, ptr) \
261({ \
262 __typeof__(*(ptr)) __user *__Pu_addr = (ptr); \
263 access_ok(VERIFY_WRITE, (__Pu_addr), sizeof(*(__Pu_addr))) ? \
264 __put_user((x), (__Pu_addr)) : \
265 -EFAULT; \
266})
267
268#define get_user(x, ptr) \
269({ \
270 __typeof__(*(ptr)) const __user *__Gu_addr = (ptr); \
271 access_ok(VERIFY_READ, (__Gu_addr), sizeof(*(__Gu_addr))) ? \
272 __get_user((x), (__Gu_addr)) : \
273 ((x) = 0, -EFAULT); \
274})
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294extern unsigned long __must_check __copy_to_user_inatomic(
295 void __user *to, const void *from, unsigned long n);
296
297static inline unsigned long __must_check
298__copy_to_user(void __user *to, const void *from, unsigned long n)
299{
300 might_fault();
301 return __copy_to_user_inatomic(to, from, n);
302}
303
304static inline unsigned long __must_check
305copy_to_user(void __user *to, const void *from, unsigned long n)
306{
307 if (access_ok(VERIFY_WRITE, to, n))
308 n = __copy_to_user(to, from, n);
309 return n;
310}
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334extern unsigned long __must_check __copy_from_user_inatomic(
335 void *to, const void __user *from, unsigned long n);
336extern unsigned long __must_check __copy_from_user_zeroing(
337 void *to, const void __user *from, unsigned long n);
338
339static inline unsigned long __must_check
340__copy_from_user(void *to, const void __user *from, unsigned long n)
341{
342 might_fault();
343 return __copy_from_user_zeroing(to, from, n);
344}
345
346static inline unsigned long __must_check
347_copy_from_user(void *to, const void __user *from, unsigned long n)
348{
349 if (access_ok(VERIFY_READ, from, n))
350 n = __copy_from_user(to, from, n);
351 else
352 memset(to, 0, n);
353 return n;
354}
355
356#ifdef CONFIG_DEBUG_COPY_FROM_USER
357extern void copy_from_user_overflow(void)
358 __compiletime_warning("copy_from_user() size is not provably correct");
359
360static inline unsigned long __must_check copy_from_user(void *to,
361 const void __user *from,
362 unsigned long n)
363{
364 int sz = __compiletime_object_size(to);
365
366 if (likely(sz == -1 || sz >= n))
367 n = _copy_from_user(to, from, n);
368 else
369 copy_from_user_overflow();
370
371 return n;
372}
373#else
374#define copy_from_user _copy_from_user
375#endif
376
377#ifdef __tilegx__
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392extern unsigned long __copy_in_user_inatomic(
393 void __user *to, const void __user *from, unsigned long n);
394
395static inline unsigned long __must_check
396__copy_in_user(void __user *to, const void __user *from, unsigned long n)
397{
398 might_sleep();
399 return __copy_in_user_inatomic(to, from, n);
400}
401
402static inline unsigned long __must_check
403copy_in_user(void __user *to, const void __user *from, unsigned long n)
404{
405 if (access_ok(VERIFY_WRITE, to, n) && access_ok(VERIFY_READ, from, n))
406 n = __copy_in_user(to, from, n);
407 return n;
408}
409#endif
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426extern long strnlen_user_asm(const char __user *str, long n);
427static inline long __must_check strnlen_user(const char __user *str, long n)
428{
429 might_fault();
430 return strnlen_user_asm(str, n);
431}
432#define strlen_user(str) strnlen_user(str, LONG_MAX)
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454extern long strncpy_from_user_asm(char *dst, const char __user *src, long);
455static inline long __must_check __strncpy_from_user(
456 char *dst, const char __user *src, long count)
457{
458 might_fault();
459 return strncpy_from_user_asm(dst, src, count);
460}
461static inline long __must_check strncpy_from_user(
462 char *dst, const char __user *src, long count)
463{
464 if (access_ok(VERIFY_READ, src, 1))
465 return __strncpy_from_user(dst, src, count);
466 return -EFAULT;
467}
468
469
470
471
472
473
474
475
476
477
478
479extern unsigned long clear_user_asm(void __user *mem, unsigned long len);
480static inline unsigned long __must_check __clear_user(
481 void __user *mem, unsigned long len)
482{
483 might_fault();
484 return clear_user_asm(mem, len);
485}
486static inline unsigned long __must_check clear_user(
487 void __user *mem, unsigned long len)
488{
489 if (access_ok(VERIFY_WRITE, mem, len))
490 return __clear_user(mem, len);
491 return len;
492}
493
494
495
496
497
498
499
500
501
502extern unsigned long flush_user_asm(void __user *mem, unsigned long len);
503static inline unsigned long __must_check __flush_user(
504 void __user *mem, unsigned long len)
505{
506 int retval;
507
508 might_fault();
509 retval = flush_user_asm(mem, len);
510 mb_incoherent();
511 return retval;
512}
513
514static inline unsigned long __must_check flush_user(
515 void __user *mem, unsigned long len)
516{
517 if (access_ok(VERIFY_WRITE, mem, len))
518 return __flush_user(mem, len);
519 return len;
520}
521
522
523
524
525
526
527
528
529
530
531
532
533
534extern unsigned long inv_user_asm(void __user *mem, unsigned long len);
535static inline unsigned long __must_check __inv_user(
536 void __user *mem, unsigned long len)
537{
538 int retval;
539
540 might_fault();
541 retval = inv_user_asm(mem, len);
542 mb_incoherent();
543 return retval;
544}
545static inline unsigned long __must_check inv_user(
546 void __user *mem, unsigned long len)
547{
548 if (access_ok(VERIFY_WRITE, mem, len))
549 return __inv_user(mem, len);
550 return len;
551}
552
553
554
555
556
557
558
559
560
561extern unsigned long finv_user_asm(void __user *mem, unsigned long len);
562static inline unsigned long __must_check __finv_user(
563 void __user *mem, unsigned long len)
564{
565 int retval;
566
567 might_fault();
568 retval = finv_user_asm(mem, len);
569 mb_incoherent();
570 return retval;
571}
572static inline unsigned long __must_check finv_user(
573 void __user *mem, unsigned long len)
574{
575 if (access_ok(VERIFY_WRITE, mem, len))
576 return __finv_user(mem, len);
577 return len;
578}
579
580#endif
581