patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / include / asm-ppc64 / uaccess.h
1 #ifndef _PPC64_UACCESS_H
2 #define _PPC64_UACCESS_H
3
4 /* 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version
8  * 2 of the License, or (at your option) any later version.
9  */
10
11 #ifndef __ASSEMBLY__
12 #include <linux/sched.h>
13 #include <linux/errno.h>
14 #include <asm/processor.h>
15
16 #define VERIFY_READ     0
17 #define VERIFY_WRITE    1
18
19 /*
20  * The fs value determines whether argument validity checking should be
21  * performed or not.  If get_fs() == USER_DS, checking is performed, with
22  * get_fs() == KERNEL_DS, checking is bypassed.
23  *
24  * For historical reasons, these macros are grossly misnamed.
25  */
26
27 #define MAKE_MM_SEG(s)  ((mm_segment_t) { (s) })
28
29 #define KERNEL_DS       MAKE_MM_SEG(0UL)
30 #define USER_DS         MAKE_MM_SEG(0xf000000000000000UL)
31
32 #define get_ds()        (KERNEL_DS)
33 #define get_fs()        (current->thread.fs)
34 #define set_fs(val)     (current->thread.fs = (val))
35
36 #define segment_eq(a,b) ((a).seg == (b).seg)
37
38 /*
39  * Use the alpha trick for checking ranges:
40  *
41  * Is a address valid? This does a straightforward calculation rather
42  * than tests.
43  *
44  * Address valid if:
45  *  - "addr" doesn't have any high-bits set
46  *  - AND "size" doesn't have any high-bits set
47  *  - OR we are in kernel mode.
48  *
49  * We dont have to check for high bits in (addr+size) because the first
50  * two checks force the maximum result to be below the start of the
51  * kernel region.
52  */
53 #define __access_ok(addr,size,segment) \
54         (((segment).seg & (addr | size )) == 0)
55
56 #define access_ok(type,addr,size) \
57         __access_ok(((__force unsigned long)(addr)),(size),get_fs())
58
59 static inline int verify_area(int type, const void __user *addr, unsigned long size)
60 {
61         return access_ok(type,addr,size) ? 0 : -EFAULT;
62 }
63
64
65 /*
66  * The exception table consists of pairs of addresses: the first is the
67  * address of an instruction that is allowed to fault, and the second is
68  * the address at which the program should continue.  No registers are
69  * modified, so it is entirely up to the continuation code to figure out
70  * what to do.
71  *
72  * All the routines below use bits of fixup code that are out of line
73  * with the main instruction path.  This means when everything is well,
74  * we don't even have to jump over them.  Further, they do not intrude
75  * on our cache or tlb entries.
76  */
77
78 struct exception_table_entry
79 {
80         unsigned long insn, fixup;
81 };
82
83 /* Returns 0 if exception not found and fixup otherwise.  */
84 extern unsigned long search_exception_table(unsigned long);
85
86 /*
87  * These are the main single-value transfer routines.  They automatically
88  * use the right size if we just have the right pointer type.
89  *
90  * This gets kind of ugly. We want to return _two_ values in "get_user()"
91  * and yet we don't want to do any pointers, because that is too much
92  * of a performance impact. Thus we have a few rather ugly macros here,
93  * and hide all the ugliness from the user.
94  *
95  * The "__xxx" versions of the user access functions are versions that
96  * do not verify the address space, that must have been done previously
97  * with a separate "access_ok()" call (this is used when we do multiple
98  * accesses to the same area of user memory).
99  *
100  * As we use the same address space for kernel and user data on the
101  * PowerPC, we can just do these as direct assignments.  (Of course, the
102  * exception handling means that it's no longer "just"...)
103  */
104 #define get_user(x,ptr) \
105   __get_user_check((x),(ptr),sizeof(*(ptr)))
106 #define put_user(x,ptr) \
107   __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
108
109 #define __get_user(x,ptr) \
110   __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
111 #define __put_user(x,ptr) \
112   __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
113
114 extern long __put_user_bad(void);
115
116 #define __put_user_nocheck(x,ptr,size)                          \
117 ({                                                              \
118         long __pu_err;                                          \
119         __chk_user_ptr(ptr);                                    \
120         __put_user_size((x),(ptr),(size),__pu_err,-EFAULT);     \
121         __pu_err;                                               \
122 })
123
124 #define __put_user_check(x,ptr,size)                                    \
125 ({                                                                      \
126         long __pu_err = -EFAULT;                                        \
127         void __user *__pu_addr = (ptr);                                 \
128         if (access_ok(VERIFY_WRITE,__pu_addr,size))                     \
129                 __put_user_size((x),__pu_addr,(size),__pu_err,-EFAULT); \
130         __pu_err;                                                       \
131 })
132
133 #define __put_user_size(x,ptr,size,retval,errret)                       \
134 do {                                                                    \
135         might_sleep();                                                  \
136         retval = 0;                                                     \
137         switch (size) {                                                 \
138           case 1: __put_user_asm(x,ptr,retval,"stb",errret); break;     \
139           case 2: __put_user_asm(x,ptr,retval,"sth",errret); break;     \
140           case 4: __put_user_asm(x,ptr,retval,"stw",errret); break;     \
141           case 8: __put_user_asm(x,ptr,retval,"std",errret); break;     \
142           default: __put_user_bad();                                    \
143         }                                                               \
144 } while (0)
145
146 /*
147  * We don't tell gcc that we are accessing memory, but this is OK
148  * because we do not write to any memory gcc knows about, so there
149  * are no aliasing issues.
150  */
151 #define __put_user_asm(x, addr, err, op, errret)                \
152         __asm__ __volatile__(                                   \
153                 "1:     "op" %1,0(%2)   # put_user\n"           \
154                 "2:\n"                                          \
155                 ".section .fixup,\"ax\"\n"                      \
156                 "3:     li %0,%3\n"                             \
157                 "       b 2b\n"                                 \
158                 ".previous\n"                                   \
159                 ".section __ex_table,\"a\"\n"                   \
160                 "       .align 3\n"                             \
161                 "       .llong 1b,3b\n"                         \
162                 ".previous"                                     \
163                 : "=r"(err)                                     \
164                 : "r"(x), "b"(addr), "i"(errret), "0"(err))
165
166
167 #define __get_user_nocheck(x,ptr,size)                          \
168 ({                                                              \
169         long __gu_err, __gu_val;                                \
170         __get_user_size(__gu_val,(ptr),(size),__gu_err,-EFAULT);\
171         (x) = (__typeof__(*(ptr)))__gu_val;                     \
172         __gu_err;                                               \
173 })
174
175 #define __get_user_check(x,ptr,size)                                    \
176 ({                                                                      \
177         long __gu_err = -EFAULT, __gu_val = 0;                          \
178         const __typeof__(*(ptr)) *__gu_addr = (ptr);                    \
179         if (access_ok(VERIFY_READ,__gu_addr,size))                      \
180                 __get_user_size(__gu_val,__gu_addr,(size),__gu_err,-EFAULT);\
181         (x) = (__typeof__(*(ptr)))__gu_val;                             \
182         __gu_err;                                                       \
183 })
184
185 extern long __get_user_bad(void);
186
187 #define __get_user_size(x,ptr,size,retval,errret)                       \
188 do {                                                                    \
189         might_sleep();                                                  \
190         retval = 0;                                                     \
191         __chk_user_ptr(ptr);                                            \
192         switch (size) {                                                 \
193           case 1: __get_user_asm(x,ptr,retval,"lbz",errret); break;     \
194           case 2: __get_user_asm(x,ptr,retval,"lhz",errret); break;     \
195           case 4: __get_user_asm(x,ptr,retval,"lwz",errret); break;     \
196           case 8: __get_user_asm(x,ptr,retval,"ld",errret);  break;     \
197           default: (x) = __get_user_bad();                              \
198         }                                                               \
199 } while (0)
200
201 #define __get_user_asm(x, addr, err, op, errret)        \
202         __asm__ __volatile__(                           \
203                 "1:     "op" %1,0(%2)   # get_user\n"   \
204                 "2:\n"                                  \
205                 ".section .fixup,\"ax\"\n"              \
206                 "3:     li %0,%3\n"                     \
207                 "       li %1,0\n"                      \
208                 "       b 2b\n"                         \
209                 ".previous\n"                           \
210                 ".section __ex_table,\"a\"\n"           \
211                 "       .align 3\n"                     \
212                 "       .llong 1b,3b\n"                 \
213                 ".previous"                             \
214                 : "=r"(err), "=r"(x)                    \
215                 : "b"(addr), "i"(errret), "0"(err))
216
217 /* more complex routines */
218
219 extern unsigned long __copy_tofrom_user(void __user *to, const void __user *from,
220                                         unsigned long size);
221
222 static inline unsigned long
223 __copy_from_user(void *to, const void __user *from, unsigned long n)
224 {
225         might_sleep();
226         if (__builtin_constant_p(n)) {
227                 unsigned long ret;
228
229                 switch (n) {
230                 case 1:
231                         __get_user_size(*(u8 *)to, from, 1, ret, 1);
232                         return ret;
233                 case 2:
234                         __get_user_size(*(u16 *)to, from, 2, ret, 2);
235                         return ret;
236                 case 4:
237                         __get_user_size(*(u32 *)to, from, 4, ret, 4);
238                         return ret;
239                 case 8:
240                         __get_user_size(*(u64 *)to, from, 8, ret, 8);
241                         return ret;
242                 }
243         }
244         return __copy_tofrom_user((void __user *) to, from, n);
245 }
246
247 static inline unsigned long
248 __copy_to_user(void __user *to, const void *from, unsigned long n)
249 {
250         might_sleep();
251         if (__builtin_constant_p(n)) {
252                 unsigned long ret;
253
254                 switch (n) {
255                 case 1:
256                         __put_user_size(*(u8 *)from, (u8 __user *)to, 1, ret, 1);
257                         return ret;
258                 case 2:
259                         __put_user_size(*(u16 *)from, (u16 __user *)to, 2, ret, 2);
260                         return ret;
261                 case 4:
262                         __put_user_size(*(u32 *)from, (u32 __user *)to, 4, ret, 4);
263                         return ret;
264                 case 8:
265                         __put_user_size(*(u64 *)from, (u64 __user *)to, 8, ret, 8);
266                         return ret;
267                 }
268         }
269         return __copy_tofrom_user(to, (const void __user *) from, n);
270 }
271
272 #define __copy_in_user(to, from, size) \
273         __copy_tofrom_user((to), (from), (size))
274
275 static inline unsigned long
276 copy_from_user(void *to, const void __user *from, unsigned long n)
277 {
278         if (likely(access_ok(VERIFY_READ, from, n)))
279                 n = __copy_from_user(to, from, n);
280         else
281                 memset(to, 0, n);
282         return n;
283 }
284
285 static inline unsigned long
286 copy_to_user(void __user *to, const void *from, unsigned long n)
287 {
288         if (likely(access_ok(VERIFY_WRITE, to, n)))
289                 n = __copy_to_user(to, from, n);
290         return n;
291 }
292
293 static inline unsigned long
294 copy_in_user(void __user *to, const void __user *from, unsigned long n)
295 {
296         might_sleep();
297         if (likely(access_ok(VERIFY_READ, from, n) &&
298             access_ok(VERIFY_WRITE, to, n)))
299                 n =__copy_tofrom_user(to, from, n);
300         return n;
301 }
302
303 extern unsigned long __clear_user(void __user *addr, unsigned long size);
304
305 static inline unsigned long
306 clear_user(void __user *addr, unsigned long size)
307 {
308         might_sleep();
309         if (likely(access_ok(VERIFY_WRITE, addr, size)))
310                 size = __clear_user(addr, size);
311         return size;
312 }
313
314 extern int __strncpy_from_user(char *dst, const char __user *src, long count);
315
316 static inline long
317 strncpy_from_user(char *dst, const char __user *src, long count)
318 {
319         might_sleep();
320         if (likely(access_ok(VERIFY_READ, src, 1)))
321                 return __strncpy_from_user(dst, src, count);
322         return -EFAULT;
323 }
324
325 /*
326  * Return the size of a string (including the ending 0)
327  *
328  * Return 0 for error
329  */
330 extern int __strnlen_user(const char __user *str, long len);
331
332 /*
333  * Returns the length of the string at str (including the null byte),
334  * or 0 if we hit a page we can't access,
335  * or something > len if we didn't find a null byte.
336  */
337 static inline int strnlen_user(const char __user *str, long len)
338 {
339         might_sleep();
340         if (likely(access_ok(VERIFY_READ, str, 1)))
341                 return __strnlen_user(str, len);
342         return 0;
343 }
344
345 #define strlen_user(str)        strnlen_user((str), 0x7ffffffe)
346
347 #endif  /* __ASSEMBLY__ */
348
349 #endif  /* _PPC64_UACCESS_H */