[SCSI] sd: udev accessing an uninitialized scsi_disk field results in a crash
[linux-2.6] / include / asm-avr32 / uaccess.h
1 /*
2  * Copyright (C) 2004-2006 Atmel Corporation
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8 #ifndef __ASM_AVR32_UACCESS_H
9 #define __ASM_AVR32_UACCESS_H
10
11 #include <linux/errno.h>
12 #include <linux/sched.h>
13
14 #define VERIFY_READ     0
15 #define VERIFY_WRITE    1
16
17 typedef struct {
18         unsigned int is_user_space;
19 } mm_segment_t;
20
21 /*
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 (Data Segment Register?), these macros are misnamed.
27  */
28 #define MAKE_MM_SEG(s)  ((mm_segment_t) { (s) })
29 #define segment_eq(a,b) ((a).is_user_space == (b).is_user_space)
30
31 #define USER_ADDR_LIMIT 0x80000000
32
33 #define KERNEL_DS       MAKE_MM_SEG(0)
34 #define USER_DS         MAKE_MM_SEG(1)
35
36 #define get_ds()        (KERNEL_DS)
37
38 static inline mm_segment_t get_fs(void)
39 {
40         return MAKE_MM_SEG(test_thread_flag(TIF_USERSPACE));
41 }
42
43 static inline void set_fs(mm_segment_t s)
44 {
45         if (s.is_user_space)
46                 set_thread_flag(TIF_USERSPACE);
47         else
48                 clear_thread_flag(TIF_USERSPACE);
49 }
50
51 /*
52  * Test whether a block of memory is a valid user space address.
53  * Returns 0 if the range is valid, nonzero otherwise.
54  *
55  * We do the following checks:
56  *   1. Is the access from kernel space?
57  *   2. Does (addr + size) set the carry bit?
58  *   3. Is (addr + size) a negative number (i.e. >= 0x80000000)?
59  *
60  * If yes on the first check, access is granted.
61  * If no on any of the others, access is denied.
62  */
63 #define __range_ok(addr, size)                                          \
64         (test_thread_flag(TIF_USERSPACE)                                \
65          && (((unsigned long)(addr) >= 0x80000000)                      \
66              || ((unsigned long)(size) > 0x80000000)                    \
67              || (((unsigned long)(addr) + (unsigned long)(size)) > 0x80000000)))
68
69 #define access_ok(type, addr, size) (likely(__range_ok(addr, size) == 0))
70
71 static inline int
72 verify_area(int type, const void __user *addr, unsigned long size)
73 {
74         return access_ok(type, addr, size) ? 0 : -EFAULT;
75 }
76
77 /* Generic arbitrary sized copy. Return the number of bytes NOT copied */
78 extern __kernel_size_t __copy_user(void *to, const void *from,
79                                    __kernel_size_t n);
80
81 extern __kernel_size_t copy_to_user(void __user *to, const void *from,
82                                     __kernel_size_t n);
83 extern __kernel_size_t copy_from_user(void *to, const void __user *from,
84                                       __kernel_size_t n);
85
86 static inline __kernel_size_t __copy_to_user(void __user *to, const void *from,
87                                              __kernel_size_t n)
88 {
89         return __copy_user((void __force *)to, from, n);
90 }
91 static inline __kernel_size_t __copy_from_user(void *to,
92                                                const void __user *from,
93                                                __kernel_size_t n)
94 {
95         return __copy_user(to, (const void __force *)from, n);
96 }
97
98 #define __copy_to_user_inatomic __copy_to_user
99 #define __copy_from_user_inatomic __copy_from_user
100
101 /*
102  * put_user: - Write a simple value into user space.
103  * @x:   Value to copy to user space.
104  * @ptr: Destination address, in user space.
105  *
106  * Context: User context only.  This function may sleep.
107  *
108  * This macro copies a single simple value from kernel space to user
109  * space.  It supports simple types like char and int, but not larger
110  * data types like structures or arrays.
111  *
112  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
113  * to the result of dereferencing @ptr.
114  *
115  * Returns zero on success, or -EFAULT on error.
116  */
117 #define put_user(x,ptr) \
118         __put_user_check((x),(ptr),sizeof(*(ptr)))
119
120 /*
121  * get_user: - Get a simple variable from user space.
122  * @x:   Variable to store result.
123  * @ptr: Source address, in user space.
124  *
125  * Context: User context only.  This function may sleep.
126  *
127  * This macro copies a single simple variable from user space to kernel
128  * space.  It supports simple types like char and int, but not larger
129  * data types like structures or arrays.
130  *
131  * @ptr must have pointer-to-simple-variable type, and the result of
132  * dereferencing @ptr must be assignable to @x without a cast.
133  *
134  * Returns zero on success, or -EFAULT on error.
135  * On error, the variable @x is set to zero.
136  */
137 #define get_user(x,ptr) \
138         __get_user_check((x),(ptr),sizeof(*(ptr)))
139
140 /*
141  * __put_user: - Write a simple value into user space, with less checking.
142  * @x:   Value to copy to user space.
143  * @ptr: Destination address, in user space.
144  *
145  * Context: User context only.  This function may sleep.
146  *
147  * This macro copies a single simple value from kernel space to user
148  * space.  It supports simple types like char and int, but not larger
149  * data types like structures or arrays.
150  *
151  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
152  * to the result of dereferencing @ptr.
153  *
154  * Caller must check the pointer with access_ok() before calling this
155  * function.
156  *
157  * Returns zero on success, or -EFAULT on error.
158  */
159 #define __put_user(x,ptr) \
160         __put_user_nocheck((x),(ptr),sizeof(*(ptr)))
161
162 /*
163  * __get_user: - Get a simple variable from user space, with less checking.
164  * @x:   Variable to store result.
165  * @ptr: Source address, in user space.
166  *
167  * Context: User context only.  This function may sleep.
168  *
169  * This macro copies a single simple variable from user space to kernel
170  * space.  It supports simple types like char and int, but not larger
171  * data types like structures or arrays.
172  *
173  * @ptr must have pointer-to-simple-variable type, and the result of
174  * dereferencing @ptr must be assignable to @x without a cast.
175  *
176  * Caller must check the pointer with access_ok() before calling this
177  * function.
178  *
179  * Returns zero on success, or -EFAULT on error.
180  * On error, the variable @x is set to zero.
181  */
182 #define __get_user(x,ptr) \
183         __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
184
185 extern int __get_user_bad(void);
186 extern int __put_user_bad(void);
187
188 #define __get_user_nocheck(x, ptr, size)                                \
189 ({                                                                      \
190         typeof(*(ptr)) __gu_val = (typeof(*(ptr)) __force)0;            \
191         int __gu_err = 0;                                               \
192                                                                         \
193         switch (size) {                                                 \
194         case 1: __get_user_asm("ub", __gu_val, ptr, __gu_err); break;   \
195         case 2: __get_user_asm("uh", __gu_val, ptr, __gu_err); break;   \
196         case 4: __get_user_asm("w", __gu_val, ptr, __gu_err); break;    \
197         case 8: __get_user_asm("d", __gu_val, ptr, __gu_err); break;    \
198         default: __gu_err = __get_user_bad(); break;                    \
199         }                                                               \
200                                                                         \
201         x = __gu_val;                                                   \
202         __gu_err;                                                       \
203 })
204
205 #define __get_user_check(x, ptr, size)                                  \
206 ({                                                                      \
207         typeof(*(ptr)) __gu_val = (typeof(*(ptr)) __force)0;            \
208         const typeof(*(ptr)) __user * __gu_addr = (ptr);                \
209         int __gu_err = 0;                                               \
210                                                                         \
211         if (access_ok(VERIFY_READ, __gu_addr, size)) {                  \
212                 switch (size) {                                         \
213                 case 1:                                                 \
214                         __get_user_asm("ub", __gu_val, __gu_addr,       \
215                                        __gu_err);                       \
216                         break;                                          \
217                 case 2:                                                 \
218                         __get_user_asm("uh", __gu_val, __gu_addr,       \
219                                        __gu_err);                       \
220                         break;                                          \
221                 case 4:                                                 \
222                         __get_user_asm("w", __gu_val, __gu_addr,        \
223                                        __gu_err);                       \
224                         break;                                          \
225                 case 8:                                                 \
226                         __get_user_asm("d", __gu_val, __gu_addr,        \
227                                        __gu_err);                       \
228                         break;                                          \
229                 default:                                                \
230                         __gu_err = __get_user_bad();                    \
231                         break;                                          \
232                 }                                                       \
233         } else {                                                        \
234                 __gu_err = -EFAULT;                                     \
235         }                                                               \
236         x = __gu_val;                                                   \
237         __gu_err;                                                       \
238 })
239
240 #define __get_user_asm(suffix, __gu_val, ptr, __gu_err)                 \
241         asm volatile(                                                   \
242                 "1:     ld." suffix "   %1, %3                  \n"     \
243                 "2:                                             \n"     \
244                 "       .section .fixup, \"ax\"                 \n"     \
245                 "3:     mov     %0, %4                          \n"     \
246                 "       rjmp    2b                              \n"     \
247                 "       .previous                               \n"     \
248                 "       .section __ex_table, \"a\"              \n"     \
249                 "       .long   1b, 3b                          \n"     \
250                 "       .previous                               \n"     \
251                 : "=r"(__gu_err), "=r"(__gu_val)                        \
252                 : "0"(__gu_err), "m"(*(ptr)), "i"(-EFAULT))
253
254 #define __put_user_nocheck(x, ptr, size)                                \
255 ({                                                                      \
256         typeof(*(ptr)) __pu_val;                                        \
257         int __pu_err = 0;                                               \
258                                                                         \
259         __pu_val = (x);                                                 \
260         switch (size) {                                                 \
261         case 1: __put_user_asm("b", ptr, __pu_val, __pu_err); break;    \
262         case 2: __put_user_asm("h", ptr, __pu_val, __pu_err); break;    \
263         case 4: __put_user_asm("w", ptr, __pu_val, __pu_err); break;    \
264         case 8: __put_user_asm("d", ptr, __pu_val, __pu_err); break;    \
265         default: __pu_err = __put_user_bad(); break;                    \
266         }                                                               \
267         __pu_err;                                                       \
268 })
269
270 #define __put_user_check(x, ptr, size)                                  \
271 ({                                                                      \
272         typeof(*(ptr)) __pu_val;                                        \
273         typeof(*(ptr)) __user *__pu_addr = (ptr);                       \
274         int __pu_err = 0;                                               \
275                                                                         \
276         __pu_val = (x);                                                 \
277         if (access_ok(VERIFY_WRITE, __pu_addr, size)) {                 \
278                 switch (size) {                                         \
279                 case 1:                                                 \
280                         __put_user_asm("b", __pu_addr, __pu_val,        \
281                                        __pu_err);                       \
282                         break;                                          \
283                 case 2:                                                 \
284                         __put_user_asm("h", __pu_addr, __pu_val,        \
285                                        __pu_err);                       \
286                         break;                                          \
287                 case 4:                                                 \
288                         __put_user_asm("w", __pu_addr, __pu_val,        \
289                                        __pu_err);                       \
290                         break;                                          \
291                 case 8:                                                 \
292                         __put_user_asm("d", __pu_addr, __pu_val,                \
293                                        __pu_err);                       \
294                         break;                                          \
295                 default:                                                \
296                         __pu_err = __put_user_bad();                    \
297                         break;                                          \
298                 }                                                       \
299         } else {                                                        \
300                 __pu_err = -EFAULT;                                     \
301         }                                                               \
302         __pu_err;                                                       \
303 })
304
305 #define __put_user_asm(suffix, ptr, __pu_val, __gu_err)                 \
306         asm volatile(                                                   \
307                 "1:     st." suffix "   %1, %3                  \n"     \
308                 "2:                                             \n"     \
309                 "       .section .fixup, \"ax\"                 \n"     \
310                 "3:     mov     %0, %4                          \n"     \
311                 "       rjmp    2b                              \n"     \
312                 "       .previous                               \n"     \
313                 "       .section __ex_table, \"a\"              \n"     \
314                 "       .long   1b, 3b                          \n"     \
315                 "       .previous                               \n"     \
316                 : "=r"(__gu_err), "=m"(*(ptr))                          \
317                 : "0"(__gu_err), "r"(__pu_val), "i"(-EFAULT))
318
319 extern __kernel_size_t clear_user(void __user *addr, __kernel_size_t size);
320 extern __kernel_size_t __clear_user(void __user *addr, __kernel_size_t size);
321
322 extern long strncpy_from_user(char *dst, const char __user *src, long count);
323 extern long __strncpy_from_user(char *dst, const char __user *src, long count);
324
325 extern long strnlen_user(const char __user *__s, long __n);
326 extern long __strnlen_user(const char __user *__s, long __n);
327
328 #define strlen_user(s) strnlen_user(s, ~0UL >> 1)
329
330 struct exception_table_entry
331 {
332         unsigned long insn, fixup;
333 };
334
335 #endif /* __ASM_AVR32_UACCESS_H */