blob: e3530d0f13ee74e9c4b2bdfbaf10e226499af2e9 [file] [log] [blame]
Jesper Nilsson9987c192015-02-09 13:02:38 +01001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Authors: Bjorn Wesen (bjornw@axis.com)
3 * Hans-Peter Nilsson (hp@axis.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5
6/* Asm:s have been tweaked (within the domain of correctness) to give
7 satisfactory results for "gcc version 2.96 20000427 (experimental)".
8
9 Check regularly...
10
11 Register $r9 is chosen for temporaries, being a call-clobbered register
12 first in line to be used (notably for local blocks), not colliding with
13 parameter registers. */
14
15#ifndef _CRIS_UACCESS_H
16#define _CRIS_UACCESS_H
17
18#ifndef __ASSEMBLY__
19#include <linux/sched.h>
20#include <linux/errno.h>
21#include <asm/processor.h>
22#include <asm/page.h>
23
24#define VERIFY_READ 0
25#define VERIFY_WRITE 1
26
27/*
28 * The fs value determines whether argument validity checking should be
29 * performed or not. If get_fs() == USER_DS, checking is performed, with
30 * get_fs() == KERNEL_DS, checking is bypassed.
31 *
32 * For historical reasons, these macros are grossly misnamed.
33 */
34
35#define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
36
37/* addr_limit is the maximum accessible address for the task. we misuse
Jesper Nilsson9987c192015-02-09 13:02:38 +010038 * the KERNEL_DS and USER_DS values to both assign and compare the
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * addr_limit values through the equally misnamed get/set_fs macros.
40 * (see above)
41 */
42
43#define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF)
44#define USER_DS MAKE_MM_SEG(TASK_SIZE)
45
46#define get_ds() (KERNEL_DS)
47#define get_fs() (current_thread_info()->addr_limit)
48#define set_fs(x) (current_thread_info()->addr_limit = (x))
49
Michael S. Tsirkin83f15882015-01-27 17:29:18 +010050#define segment_eq(a, b) ((a).seg == (b).seg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#define __kernel_ok (segment_eq(get_fs(), KERNEL_DS))
Michael S. Tsirkin83f15882015-01-27 17:29:18 +010053#define __user_ok(addr, size) \
Jesper Nilsson9987c192015-02-09 13:02:38 +010054 (((size) <= TASK_SIZE) && ((addr) <= TASK_SIZE-(size)))
Michael S. Tsirkin83f15882015-01-27 17:29:18 +010055#define __access_ok(addr, size) (__kernel_ok || __user_ok((addr), (size)))
56#define access_ok(type, addr, size) __access_ok((unsigned long)(addr), (size))
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Jesper Nilsson556dcee2008-10-21 17:45:58 +020058#include <arch/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/*
61 * The exception table consists of pairs of addresses: the first is the
62 * address of an instruction that is allowed to fault, and the second is
63 * the address at which the program should continue. No registers are
64 * modified, so it is entirely up to the continuation code to figure out
65 * what to do.
66 *
67 * All the routines below use bits of fixup code that are out of line
68 * with the main instruction path. This means when everything is well,
69 * we don't even have to jump over them. Further, they do not intrude
70 * on our cache or tlb entries.
71 */
72
Jesper Nilsson9987c192015-02-09 13:02:38 +010073struct exception_table_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 unsigned long insn, fixup;
75};
76
77/*
78 * These are the main single-value transfer routines. They automatically
79 * use the right size if we just have the right pointer type.
80 *
81 * This gets kind of ugly. We want to return _two_ values in "get_user()"
82 * and yet we don't want to do any pointers, because that is too much
83 * of a performance impact. Thus we have a few rather ugly macros here,
84 * and hide all the ugliness from the user.
85 *
86 * The "__xxx" versions of the user access functions are versions that
87 * do not verify the address space, that must have been done previously
88 * with a separate "access_ok()" call (this is used when we do multiple
89 * accesses to the same area of user memory).
90 *
91 * As we use the same address space for kernel and user data on
92 * CRIS, we can just do these as direct assignments. (Of course, the
93 * exception handling means that it's no longer "just"...)
94 */
Michael S. Tsirkin83f15882015-01-27 17:29:18 +010095#define get_user(x, ptr) \
Jesper Nilsson9987c192015-02-09 13:02:38 +010096 __get_user_check((x), (ptr), sizeof(*(ptr)))
Michael S. Tsirkin83f15882015-01-27 17:29:18 +010097#define put_user(x, ptr) \
Jesper Nilsson9987c192015-02-09 13:02:38 +010098 __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100100#define __get_user(x, ptr) \
Jesper Nilsson9987c192015-02-09 13:02:38 +0100101 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100102#define __put_user(x, ptr) \
Jesper Nilsson9987c192015-02-09 13:02:38 +0100103 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105extern long __put_user_bad(void);
106
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100107#define __put_user_size(x, ptr, size, retval) \
108do { \
109 retval = 0; \
110 switch (size) { \
Jesper Nilsson9987c192015-02-09 13:02:38 +0100111 case 1: \
112 __put_user_asm(x, ptr, retval, "move.b"); \
113 break; \
114 case 2: \
115 __put_user_asm(x, ptr, retval, "move.w"); \
116 break; \
117 case 4: \
118 __put_user_asm(x, ptr, retval, "move.d"); \
119 break; \
120 case 8: \
121 __put_user_asm_64(x, ptr, retval); \
122 break; \
123 default: \
124 __put_user_bad(); \
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100125 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126} while (0)
127
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100128#define __get_user_size(x, ptr, size, retval) \
129do { \
130 retval = 0; \
131 switch (size) { \
Jesper Nilsson9987c192015-02-09 13:02:38 +0100132 case 1: \
133 __get_user_asm(x, ptr, retval, "move.b"); \
134 break; \
135 case 2: \
136 __get_user_asm(x, ptr, retval, "move.w"); \
137 break; \
138 case 4: \
139 __get_user_asm(x, ptr, retval, "move.d"); \
140 break; \
141 case 8: \
142 __get_user_asm_64(x, ptr, retval); \
143 break; \
144 default: \
145 (x) = __get_user_bad(); \
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100146 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147} while (0)
148
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100149#define __put_user_nocheck(x, ptr, size) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150({ \
151 long __pu_err; \
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100152 __put_user_size((x), (ptr), (size), __pu_err); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 __pu_err; \
154})
155
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100156#define __put_user_check(x, ptr, size) \
157({ \
158 long __pu_err = -EFAULT; \
159 __typeof__(*(ptr)) *__pu_addr = (ptr); \
160 if (access_ok(VERIFY_WRITE, __pu_addr, size)) \
161 __put_user_size((x), __pu_addr, (size), __pu_err); \
162 __pu_err; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163})
164
165struct __large_struct { unsigned long buf[100]; };
166#define __m(x) (*(struct __large_struct *)(x))
167
168
169
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100170#define __get_user_nocheck(x, ptr, size) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171({ \
172 long __gu_err, __gu_val; \
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100173 __get_user_size(__gu_val, (ptr), (size), __gu_err); \
Michael S. Tsirkin458e3192015-01-27 17:20:10 +0100174 (x) = (__force __typeof__(*(ptr)))__gu_val; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 __gu_err; \
176})
177
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100178#define __get_user_check(x, ptr, size) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179({ \
180 long __gu_err = -EFAULT, __gu_val = 0; \
181 const __typeof__(*(ptr)) *__gu_addr = (ptr); \
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100182 if (access_ok(VERIFY_READ, __gu_addr, size)) \
183 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
Michael S. Tsirkin458e3192015-01-27 17:20:10 +0100184 (x) = (__force __typeof__(*(ptr)))__gu_val; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 __gu_err; \
186})
187
188extern long __get_user_bad(void);
189
190/* More complex functions. Most are inline, but some call functions that
191 live in lib/usercopy.c */
192
Jesper Nilsson07f24022008-03-04 14:29:23 -0800193extern unsigned long __copy_user(void __user *to, const void *from, unsigned long n);
194extern unsigned long __copy_user_zeroing(void *to, const void __user *from, unsigned long n);
195extern unsigned long __do_clear_user(void __user *to, unsigned long n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Adrian Bunkd9b54442005-11-07 00:58:44 -0800197static inline unsigned long
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198__generic_copy_to_user(void __user *to, const void *from, unsigned long n)
199{
200 if (access_ok(VERIFY_WRITE, to, n))
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100201 return __copy_user(to, from, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return n;
203}
204
Adrian Bunkd9b54442005-11-07 00:58:44 -0800205static inline unsigned long
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206__generic_copy_from_user(void *to, const void __user *from, unsigned long n)
207{
208 if (access_ok(VERIFY_READ, from, n))
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100209 return __copy_user_zeroing(to, from, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return n;
211}
212
Adrian Bunkd9b54442005-11-07 00:58:44 -0800213static inline unsigned long
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214__generic_clear_user(void __user *to, unsigned long n)
215{
216 if (access_ok(VERIFY_WRITE, to, n))
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100217 return __do_clear_user(to, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return n;
219}
220
Adrian Bunkd9b54442005-11-07 00:58:44 -0800221static inline long
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222__strncpy_from_user(char *dst, const char __user *src, long count)
223{
224 return __do_strncpy_from_user(dst, src, count);
225}
226
Adrian Bunkd9b54442005-11-07 00:58:44 -0800227static inline long
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228strncpy_from_user(char *dst, const char __user *src, long count)
229{
230 long res = -EFAULT;
Jesper Nilsson9987c192015-02-09 13:02:38 +0100231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (access_ok(VERIFY_READ, src, 1))
233 res = __do_strncpy_from_user(dst, src, count);
234 return res;
235}
236
237
Jesper Nilsson07f24022008-03-04 14:29:23 -0800238/* Note that these expand awfully if made into switch constructs, so
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 don't do that. */
240
Adrian Bunkd9b54442005-11-07 00:58:44 -0800241static inline unsigned long
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242__constant_copy_from_user(void *to, const void __user *from, unsigned long n)
243{
244 unsigned long ret = 0;
Jesper Nilsson9987c192015-02-09 13:02:38 +0100245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (n == 0)
247 ;
248 else if (n == 1)
249 __asm_copy_from_user_1(to, from, ret);
250 else if (n == 2)
251 __asm_copy_from_user_2(to, from, ret);
252 else if (n == 3)
253 __asm_copy_from_user_3(to, from, ret);
254 else if (n == 4)
255 __asm_copy_from_user_4(to, from, ret);
256 else if (n == 5)
257 __asm_copy_from_user_5(to, from, ret);
258 else if (n == 6)
259 __asm_copy_from_user_6(to, from, ret);
260 else if (n == 7)
261 __asm_copy_from_user_7(to, from, ret);
262 else if (n == 8)
263 __asm_copy_from_user_8(to, from, ret);
264 else if (n == 9)
265 __asm_copy_from_user_9(to, from, ret);
266 else if (n == 10)
267 __asm_copy_from_user_10(to, from, ret);
268 else if (n == 11)
269 __asm_copy_from_user_11(to, from, ret);
270 else if (n == 12)
271 __asm_copy_from_user_12(to, from, ret);
272 else if (n == 13)
273 __asm_copy_from_user_13(to, from, ret);
274 else if (n == 14)
275 __asm_copy_from_user_14(to, from, ret);
276 else if (n == 15)
277 __asm_copy_from_user_15(to, from, ret);
278 else if (n == 16)
279 __asm_copy_from_user_16(to, from, ret);
280 else if (n == 20)
281 __asm_copy_from_user_20(to, from, ret);
282 else if (n == 24)
283 __asm_copy_from_user_24(to, from, ret);
284 else
285 ret = __generic_copy_from_user(to, from, n);
286
287 return ret;
288}
289
290/* Ditto, don't make a switch out of this. */
291
Adrian Bunkd9b54442005-11-07 00:58:44 -0800292static inline unsigned long
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293__constant_copy_to_user(void __user *to, const void *from, unsigned long n)
294{
295 unsigned long ret = 0;
Jesper Nilsson9987c192015-02-09 13:02:38 +0100296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (n == 0)
298 ;
299 else if (n == 1)
300 __asm_copy_to_user_1(to, from, ret);
301 else if (n == 2)
302 __asm_copy_to_user_2(to, from, ret);
303 else if (n == 3)
304 __asm_copy_to_user_3(to, from, ret);
305 else if (n == 4)
306 __asm_copy_to_user_4(to, from, ret);
307 else if (n == 5)
308 __asm_copy_to_user_5(to, from, ret);
309 else if (n == 6)
310 __asm_copy_to_user_6(to, from, ret);
311 else if (n == 7)
312 __asm_copy_to_user_7(to, from, ret);
313 else if (n == 8)
314 __asm_copy_to_user_8(to, from, ret);
315 else if (n == 9)
316 __asm_copy_to_user_9(to, from, ret);
317 else if (n == 10)
318 __asm_copy_to_user_10(to, from, ret);
319 else if (n == 11)
320 __asm_copy_to_user_11(to, from, ret);
321 else if (n == 12)
322 __asm_copy_to_user_12(to, from, ret);
323 else if (n == 13)
324 __asm_copy_to_user_13(to, from, ret);
325 else if (n == 14)
326 __asm_copy_to_user_14(to, from, ret);
327 else if (n == 15)
328 __asm_copy_to_user_15(to, from, ret);
329 else if (n == 16)
330 __asm_copy_to_user_16(to, from, ret);
331 else if (n == 20)
332 __asm_copy_to_user_20(to, from, ret);
333 else if (n == 24)
334 __asm_copy_to_user_24(to, from, ret);
335 else
336 ret = __generic_copy_to_user(to, from, n);
337
338 return ret;
339}
340
341/* No switch, please. */
342
Adrian Bunkd9b54442005-11-07 00:58:44 -0800343static inline unsigned long
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344__constant_clear_user(void __user *to, unsigned long n)
345{
346 unsigned long ret = 0;
Jesper Nilsson9987c192015-02-09 13:02:38 +0100347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (n == 0)
349 ;
350 else if (n == 1)
351 __asm_clear_1(to, ret);
352 else if (n == 2)
353 __asm_clear_2(to, ret);
354 else if (n == 3)
355 __asm_clear_3(to, ret);
356 else if (n == 4)
357 __asm_clear_4(to, ret);
358 else if (n == 8)
359 __asm_clear_8(to, ret);
360 else if (n == 12)
361 __asm_clear_12(to, ret);
362 else if (n == 16)
363 __asm_clear_16(to, ret);
364 else if (n == 20)
365 __asm_clear_20(to, ret);
366 else if (n == 24)
367 __asm_clear_24(to, ret);
368 else
369 ret = __generic_clear_user(to, n);
370
371 return ret;
372}
373
374
Jesper Nilsson9987c192015-02-09 13:02:38 +0100375#define clear_user(to, n) \
376 (__builtin_constant_p(n) ? \
377 __constant_clear_user(to, n) : \
378 __generic_clear_user(to, n))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Jesper Nilsson9987c192015-02-09 13:02:38 +0100380#define copy_from_user(to, from, n) \
381 (__builtin_constant_p(n) ? \
382 __constant_copy_from_user(to, from, n) : \
383 __generic_copy_from_user(to, from, n))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Jesper Nilsson9987c192015-02-09 13:02:38 +0100385#define copy_to_user(to, from, n) \
386 (__builtin_constant_p(n) ? \
387 __constant_copy_to_user(to, from, n) : \
388 __generic_copy_to_user(to, from, n))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390/* We let the __ versions of copy_from/to_user inline, because they're often
391 * used in fast paths and have only a small space overhead.
392 */
393
Adrian Bunkd9b54442005-11-07 00:58:44 -0800394static inline unsigned long
Jesper Nilsson07f24022008-03-04 14:29:23 -0800395__generic_copy_from_user_nocheck(void *to, const void __user *from,
396 unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100398 return __copy_user_zeroing(to, from, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Adrian Bunkd9b54442005-11-07 00:58:44 -0800401static inline unsigned long
Jesper Nilsson07f24022008-03-04 14:29:23 -0800402__generic_copy_to_user_nocheck(void __user *to, const void *from,
403 unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100405 return __copy_user(to, from, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Adrian Bunkd9b54442005-11-07 00:58:44 -0800408static inline unsigned long
Jesper Nilsson07f24022008-03-04 14:29:23 -0800409__generic_clear_user_nocheck(void __user *to, unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100411 return __do_clear_user(to, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
414/* without checking */
415
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100416#define __copy_to_user(to, from, n) \
417 __generic_copy_to_user_nocheck((to), (from), (n))
418#define __copy_from_user(to, from, n) \
419 __generic_copy_from_user_nocheck((to), (from), (n))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420#define __copy_to_user_inatomic __copy_to_user
421#define __copy_from_user_inatomic __copy_from_user
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100422#define __clear_user(to, n) __generic_clear_user_nocheck((to), (n))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424#define strlen_user(str) strnlen_user((str), 0x7ffffffe)
425
426#endif /* __ASSEMBLY__ */
427
428#endif /* _CRIS_UACCESS_H */