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