blob: c2462ef04eaf5dde9db8e5b5db18d6bbeff9abc7 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/processor.h>
19#include <asm/page.h>
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021/*
22 * The fs value determines whether argument validity checking should be
23 * performed or not. If get_fs() == USER_DS, checking is performed, with
24 * get_fs() == KERNEL_DS, checking is bypassed.
25 *
26 * For historical reasons, these macros are grossly misnamed.
27 */
28
29#define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
30
31/* addr_limit is the maximum accessible address for the task. we misuse
Jesper Nilsson9987c192015-02-09 13:02:38 +010032 * the KERNEL_DS and USER_DS values to both assign and compare the
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 * addr_limit values through the equally misnamed get/set_fs macros.
34 * (see above)
35 */
36
37#define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF)
38#define USER_DS MAKE_MM_SEG(TASK_SIZE)
39
40#define get_ds() (KERNEL_DS)
41#define get_fs() (current_thread_info()->addr_limit)
42#define set_fs(x) (current_thread_info()->addr_limit = (x))
43
Michael S. Tsirkin83f15882015-01-27 17:29:18 +010044#define segment_eq(a, b) ((a).seg == (b).seg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#define __kernel_ok (segment_eq(get_fs(), KERNEL_DS))
Michael S. Tsirkin83f15882015-01-27 17:29:18 +010047#define __user_ok(addr, size) \
Jesper Nilsson9987c192015-02-09 13:02:38 +010048 (((size) <= TASK_SIZE) && ((addr) <= TASK_SIZE-(size)))
Michael S. Tsirkin83f15882015-01-27 17:29:18 +010049#define __access_ok(addr, size) (__kernel_ok || __user_ok((addr), (size)))
50#define access_ok(type, addr, size) __access_ok((unsigned long)(addr), (size))
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Jesper Nilsson556dcee2008-10-21 17:45:58 +020052#include <arch/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/*
55 * The exception table consists of pairs of addresses: the first is the
56 * address of an instruction that is allowed to fault, and the second is
57 * the address at which the program should continue. No registers are
58 * modified, so it is entirely up to the continuation code to figure out
59 * what to do.
60 *
61 * All the routines below use bits of fixup code that are out of line
62 * with the main instruction path. This means when everything is well,
63 * we don't even have to jump over them. Further, they do not intrude
64 * on our cache or tlb entries.
65 */
66
Jesper Nilsson9987c192015-02-09 13:02:38 +010067struct exception_table_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 unsigned long insn, fixup;
69};
70
71/*
72 * These are the main single-value transfer routines. They automatically
73 * use the right size if we just have the right pointer type.
74 *
75 * This gets kind of ugly. We want to return _two_ values in "get_user()"
76 * and yet we don't want to do any pointers, because that is too much
77 * of a performance impact. Thus we have a few rather ugly macros here,
78 * and hide all the ugliness from the user.
79 *
80 * The "__xxx" versions of the user access functions are versions that
81 * do not verify the address space, that must have been done previously
82 * with a separate "access_ok()" call (this is used when we do multiple
83 * accesses to the same area of user memory).
84 *
85 * As we use the same address space for kernel and user data on
86 * CRIS, we can just do these as direct assignments. (Of course, the
87 * exception handling means that it's no longer "just"...)
88 */
Michael S. Tsirkin83f15882015-01-27 17:29:18 +010089#define get_user(x, ptr) \
Jesper Nilsson9987c192015-02-09 13:02:38 +010090 __get_user_check((x), (ptr), sizeof(*(ptr)))
Michael S. Tsirkin83f15882015-01-27 17:29:18 +010091#define put_user(x, ptr) \
Jesper Nilsson9987c192015-02-09 13:02:38 +010092 __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Michael S. Tsirkin83f15882015-01-27 17:29:18 +010094#define __get_user(x, ptr) \
Jesper Nilsson9987c192015-02-09 13:02:38 +010095 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
Michael S. Tsirkin83f15882015-01-27 17:29:18 +010096#define __put_user(x, ptr) \
Jesper Nilsson9987c192015-02-09 13:02:38 +010097 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99extern long __put_user_bad(void);
100
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100101#define __put_user_size(x, ptr, size, retval) \
102do { \
103 retval = 0; \
104 switch (size) { \
Jesper Nilsson9987c192015-02-09 13:02:38 +0100105 case 1: \
106 __put_user_asm(x, ptr, retval, "move.b"); \
107 break; \
108 case 2: \
109 __put_user_asm(x, ptr, retval, "move.w"); \
110 break; \
111 case 4: \
112 __put_user_asm(x, ptr, retval, "move.d"); \
113 break; \
114 case 8: \
115 __put_user_asm_64(x, ptr, retval); \
116 break; \
117 default: \
118 __put_user_bad(); \
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100119 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120} while (0)
121
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100122#define __get_user_size(x, ptr, size, retval) \
123do { \
124 retval = 0; \
125 switch (size) { \
Jesper Nilsson9987c192015-02-09 13:02:38 +0100126 case 1: \
127 __get_user_asm(x, ptr, retval, "move.b"); \
128 break; \
129 case 2: \
130 __get_user_asm(x, ptr, retval, "move.w"); \
131 break; \
132 case 4: \
133 __get_user_asm(x, ptr, retval, "move.d"); \
134 break; \
135 case 8: \
136 __get_user_asm_64(x, ptr, retval); \
137 break; \
138 default: \
139 (x) = __get_user_bad(); \
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100140 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141} while (0)
142
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100143#define __put_user_nocheck(x, ptr, size) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144({ \
145 long __pu_err; \
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100146 __put_user_size((x), (ptr), (size), __pu_err); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 __pu_err; \
148})
149
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100150#define __put_user_check(x, ptr, size) \
151({ \
152 long __pu_err = -EFAULT; \
153 __typeof__(*(ptr)) *__pu_addr = (ptr); \
154 if (access_ok(VERIFY_WRITE, __pu_addr, size)) \
155 __put_user_size((x), __pu_addr, (size), __pu_err); \
156 __pu_err; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157})
158
159struct __large_struct { unsigned long buf[100]; };
160#define __m(x) (*(struct __large_struct *)(x))
161
162
163
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100164#define __get_user_nocheck(x, ptr, size) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165({ \
166 long __gu_err, __gu_val; \
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100167 __get_user_size(__gu_val, (ptr), (size), __gu_err); \
Michael S. Tsirkin458e3192015-01-27 17:20:10 +0100168 (x) = (__force __typeof__(*(ptr)))__gu_val; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 __gu_err; \
170})
171
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100172#define __get_user_check(x, ptr, size) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173({ \
174 long __gu_err = -EFAULT, __gu_val = 0; \
175 const __typeof__(*(ptr)) *__gu_addr = (ptr); \
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100176 if (access_ok(VERIFY_READ, __gu_addr, size)) \
177 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
Michael S. Tsirkin458e3192015-01-27 17:20:10 +0100178 (x) = (__force __typeof__(*(ptr)))__gu_val; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 __gu_err; \
180})
181
182extern long __get_user_bad(void);
183
184/* More complex functions. Most are inline, but some call functions that
185 live in lib/usercopy.c */
186
Jesper Nilsson07f24022008-03-04 14:29:23 -0800187extern unsigned long __copy_user(void __user *to, const void *from, unsigned long n);
188extern unsigned long __copy_user_zeroing(void *to, const void __user *from, unsigned long n);
189extern unsigned long __do_clear_user(void __user *to, unsigned long n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Adrian Bunkd9b54442005-11-07 00:58:44 -0800191static inline long
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192__strncpy_from_user(char *dst, const char __user *src, long count)
193{
194 return __do_strncpy_from_user(dst, src, count);
195}
196
Adrian Bunkd9b54442005-11-07 00:58:44 -0800197static inline long
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198strncpy_from_user(char *dst, const char __user *src, long count)
199{
200 long res = -EFAULT;
Jesper Nilsson9987c192015-02-09 13:02:38 +0100201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (access_ok(VERIFY_READ, src, 1))
203 res = __do_strncpy_from_user(dst, src, count);
204 return res;
205}
206
207
Jesper Nilsson07f24022008-03-04 14:29:23 -0800208/* Note that these expand awfully if made into switch constructs, so
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 don't do that. */
210
Adrian Bunkd9b54442005-11-07 00:58:44 -0800211static inline unsigned long
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212__constant_copy_from_user(void *to, const void __user *from, unsigned long n)
213{
214 unsigned long ret = 0;
Jesper Nilsson9987c192015-02-09 13:02:38 +0100215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (n == 0)
217 ;
218 else if (n == 1)
219 __asm_copy_from_user_1(to, from, ret);
220 else if (n == 2)
221 __asm_copy_from_user_2(to, from, ret);
222 else if (n == 3)
223 __asm_copy_from_user_3(to, from, ret);
224 else if (n == 4)
225 __asm_copy_from_user_4(to, from, ret);
226 else if (n == 5)
227 __asm_copy_from_user_5(to, from, ret);
228 else if (n == 6)
229 __asm_copy_from_user_6(to, from, ret);
230 else if (n == 7)
231 __asm_copy_from_user_7(to, from, ret);
232 else if (n == 8)
233 __asm_copy_from_user_8(to, from, ret);
234 else if (n == 9)
235 __asm_copy_from_user_9(to, from, ret);
236 else if (n == 10)
237 __asm_copy_from_user_10(to, from, ret);
238 else if (n == 11)
239 __asm_copy_from_user_11(to, from, ret);
240 else if (n == 12)
241 __asm_copy_from_user_12(to, from, ret);
242 else if (n == 13)
243 __asm_copy_from_user_13(to, from, ret);
244 else if (n == 14)
245 __asm_copy_from_user_14(to, from, ret);
246 else if (n == 15)
247 __asm_copy_from_user_15(to, from, ret);
248 else if (n == 16)
249 __asm_copy_from_user_16(to, from, ret);
250 else if (n == 20)
251 __asm_copy_from_user_20(to, from, ret);
252 else if (n == 24)
253 __asm_copy_from_user_24(to, from, ret);
254 else
Al Viroeb47e0292016-08-18 19:34:00 -0400255 ret = __copy_user_zeroing(to, from, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 return ret;
258}
259
260/* Ditto, don't make a switch out of this. */
261
Adrian Bunkd9b54442005-11-07 00:58:44 -0800262static inline unsigned long
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263__constant_copy_to_user(void __user *to, const void *from, unsigned long n)
264{
265 unsigned long ret = 0;
Jesper Nilsson9987c192015-02-09 13:02:38 +0100266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (n == 0)
268 ;
269 else if (n == 1)
270 __asm_copy_to_user_1(to, from, ret);
271 else if (n == 2)
272 __asm_copy_to_user_2(to, from, ret);
273 else if (n == 3)
274 __asm_copy_to_user_3(to, from, ret);
275 else if (n == 4)
276 __asm_copy_to_user_4(to, from, ret);
277 else if (n == 5)
278 __asm_copy_to_user_5(to, from, ret);
279 else if (n == 6)
280 __asm_copy_to_user_6(to, from, ret);
281 else if (n == 7)
282 __asm_copy_to_user_7(to, from, ret);
283 else if (n == 8)
284 __asm_copy_to_user_8(to, from, ret);
285 else if (n == 9)
286 __asm_copy_to_user_9(to, from, ret);
287 else if (n == 10)
288 __asm_copy_to_user_10(to, from, ret);
289 else if (n == 11)
290 __asm_copy_to_user_11(to, from, ret);
291 else if (n == 12)
292 __asm_copy_to_user_12(to, from, ret);
293 else if (n == 13)
294 __asm_copy_to_user_13(to, from, ret);
295 else if (n == 14)
296 __asm_copy_to_user_14(to, from, ret);
297 else if (n == 15)
298 __asm_copy_to_user_15(to, from, ret);
299 else if (n == 16)
300 __asm_copy_to_user_16(to, from, ret);
301 else if (n == 20)
302 __asm_copy_to_user_20(to, from, ret);
303 else if (n == 24)
304 __asm_copy_to_user_24(to, from, ret);
305 else
Al Viroeb47e0292016-08-18 19:34:00 -0400306 ret = __copy_user(to, from, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 return ret;
309}
310
311/* No switch, please. */
312
Adrian Bunkd9b54442005-11-07 00:58:44 -0800313static inline unsigned long
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314__constant_clear_user(void __user *to, unsigned long n)
315{
316 unsigned long ret = 0;
Jesper Nilsson9987c192015-02-09 13:02:38 +0100317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (n == 0)
319 ;
320 else if (n == 1)
321 __asm_clear_1(to, ret);
322 else if (n == 2)
323 __asm_clear_2(to, ret);
324 else if (n == 3)
325 __asm_clear_3(to, ret);
326 else if (n == 4)
327 __asm_clear_4(to, ret);
328 else if (n == 8)
329 __asm_clear_8(to, ret);
330 else if (n == 12)
331 __asm_clear_12(to, ret);
332 else if (n == 16)
333 __asm_clear_16(to, ret);
334 else if (n == 20)
335 __asm_clear_20(to, ret);
336 else if (n == 24)
337 __asm_clear_24(to, ret);
338 else
Al Viroeb47e0292016-08-18 19:34:00 -0400339 ret = __do_clear_user(to, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 return ret;
342}
343
344
Al Viroeb47e0292016-08-18 19:34:00 -0400345static inline size_t clear_user(void __user *to, size_t n)
346{
347 if (unlikely(!access_ok(VERIFY_WRITE, to, n)))
348 return n;
349 if (__builtin_constant_p(n))
350 return __constant_clear_user(to, n);
351 else
352 return __do_clear_user(to, n);
353}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Al Viroeb47e0292016-08-18 19:34:00 -0400355static inline size_t copy_from_user(void *to, const void __user *from, size_t n)
356{
357 if (unlikely(!access_ok(VERIFY_READ, from, n))) {
358 memset(to, 0, n);
359 return n;
360 }
361 if (__builtin_constant_p(n))
362 return __constant_copy_from_user(to, from, n);
363 else
364 return __copy_user_zeroing(to, from, n);
365}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Al Viroeb47e0292016-08-18 19:34:00 -0400367static inline size_t copy_to_user(void __user *to, const void *from, size_t n)
368{
369 if (unlikely(!access_ok(VERIFY_WRITE, to, n)))
370 return n;
371 if (__builtin_constant_p(n))
372 return __constant_copy_to_user(to, from, n);
373 else
374 return __copy_user(to, from, n);
375}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377/* We let the __ versions of copy_from/to_user inline, because they're often
378 * used in fast paths and have only a small space overhead.
379 */
380
Adrian Bunkd9b54442005-11-07 00:58:44 -0800381static inline unsigned long
Jesper Nilsson07f24022008-03-04 14:29:23 -0800382__generic_copy_from_user_nocheck(void *to, const void __user *from,
383 unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100385 return __copy_user_zeroing(to, from, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Adrian Bunkd9b54442005-11-07 00:58:44 -0800388static inline unsigned long
Jesper Nilsson07f24022008-03-04 14:29:23 -0800389__generic_copy_to_user_nocheck(void __user *to, const void *from,
390 unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100392 return __copy_user(to, from, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
Adrian Bunkd9b54442005-11-07 00:58:44 -0800395static inline unsigned long
Jesper Nilsson07f24022008-03-04 14:29:23 -0800396__generic_clear_user_nocheck(void __user *to, unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100398 return __do_clear_user(to, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
401/* without checking */
402
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100403#define __copy_to_user(to, from, n) \
404 __generic_copy_to_user_nocheck((to), (from), (n))
405#define __copy_from_user(to, from, n) \
406 __generic_copy_from_user_nocheck((to), (from), (n))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407#define __copy_to_user_inatomic __copy_to_user
408#define __copy_from_user_inatomic __copy_from_user
Michael S. Tsirkin83f15882015-01-27 17:29:18 +0100409#define __clear_user(to, n) __generic_clear_user_nocheck((to), (n))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411#define strlen_user(str) strnlen_user((str), 0x7ffffffe)
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413#endif /* _CRIS_UACCESS_H */