added some suse-specific patches to the kernel.
[linux-flexiantxendom0-3.2.10.git] / include / asm-ia64 / uaccess.h
1 #ifndef _ASM_IA64_UACCESS_H
2 #define _ASM_IA64_UACCESS_H
3
4 /*
5  * This file defines various macros to transfer memory areas across
6  * the user/kernel boundary.  This needs to be done carefully because
7  * this code is executed in kernel mode and uses user-specified
8  * addresses.  Thus, we need to be careful not to let the user to
9  * trick us into accessing kernel memory that would normally be
10  * inaccessible.  This code is also fairly performance sensitive,
11  * so we want to spend as little time doing saftey checks as
12  * possible.
13  *
14  * To make matters a bit more interesting, these macros sometimes also
15  * called from within the kernel itself, in which case the address
16  * validity check must be skipped.  The get_fs() macro tells us what
17  * to do: if get_fs()==USER_DS, checking is performed, if
18  * get_fs()==KERNEL_DS, checking is bypassed.
19  *
20  * Note that even if the memory area specified by the user is in a
21  * valid address range, it is still possible that we'll get a page
22  * fault while accessing it.  This is handled by filling out an
23  * exception handler fixup entry for each instruction that has the
24  * potential to fault.  When such a fault occurs, the page fault
25  * handler checks to see whether the faulting instruction has a fixup
26  * associated and, if so, sets r8 to -EFAULT and clears r9 to 0 and
27  * then resumes execution at the continuation point.
28  *
29  * Copyright (C) 1998, 1999, 2001-2003 Hewlett-Packard Co
30  *      David Mosberger-Tang <davidm@hpl.hp.com>
31  */
32
33 #include <linux/errno.h>
34 #include <linux/sched.h>
35
36 #include <asm/pgtable.h>
37
38 /*
39  * For historical reasons, the following macros are grossly misnamed:
40  */
41 #define KERNEL_DS       ((mm_segment_t) { ~0UL })               /* cf. access_ok() */
42 #define USER_DS         ((mm_segment_t) { TASK_SIZE-1 })        /* cf. access_ok() */
43
44 #define VERIFY_READ     0
45 #define VERIFY_WRITE    1
46
47 #define get_ds()  (KERNEL_DS)
48 #define get_fs()  (current_thread_info()->addr_limit)
49 #define set_fs(x) (current_thread_info()->addr_limit = (x))
50
51 #define segment_eq(a,b) ((a).seg == (b).seg)
52
53 /*
54  * When accessing user memory, we need to make sure the entire area really is in
55  * user-level space.  In order to do this efficiently, we make sure that the page at
56  * address TASK_SIZE is never valid.  We also need to make sure that the address doesn't
57  * point inside the virtually mapped linear page table.
58  */
59 #define __access_ok(addr,size,segment)  (((unsigned long) (addr)) <= (segment).seg      \
60          && ((segment).seg == KERNEL_DS.seg                                             \
61              || REGION_OFFSET((unsigned long) (addr)) < RGN_MAP_LIMIT))
62 #define access_ok(type,addr,size)       __access_ok((addr),(size),get_fs())
63
64 static inline int
65 verify_area (int type, const void *addr, unsigned long size)
66 {
67         return access_ok(type,addr,size) ? 0 : -EFAULT;
68 }
69
70 /*
71  * These are the main single-value transfer routines.  They automatically
72  * use the right size if we just have the right pointer type.
73  *
74  * Careful to not
75  * (a) re-use the arguments for side effects (sizeof/typeof is ok)
76  * (b) require any knowledge of processes at this stage
77  */
78 #define put_user(x,ptr) __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)),get_fs())
79 #define get_user(x,ptr) __get_user_check((x),(ptr),sizeof(*(ptr)),get_fs())
80
81 /*
82  * The "__xxx" versions do not do address space checking, useful when
83  * doing multiple accesses to the same area (the programmer has to do the
84  * checks by hand with "access_ok()")
85  */
86 #define __put_user(x,ptr)       __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
87 #define __get_user(x,ptr)       __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
88
89 extern void __get_user_unknown (void);
90
91 #define __get_user_nocheck(x,ptr,size)          \
92 ({                                              \
93         register long __gu_err asm ("r8") = 0;  \
94         register long __gu_val asm ("r9") = 0;  \
95         switch (size) {                         \
96           case 1: __get_user_8(ptr); break;     \
97           case 2: __get_user_16(ptr); break;    \
98           case 4: __get_user_32(ptr); break;    \
99           case 8: __get_user_64(ptr); break;    \
100           default: __get_user_unknown(); break; \
101         }                                       \
102         (x) = (__typeof__(*(ptr))) __gu_val;    \
103         __gu_err;                               \
104 })
105
106 #define __get_user_check(x,ptr,size,segment)                    \
107 ({                                                              \
108         register long __gu_err asm ("r8") = -EFAULT;            \
109         register long __gu_val asm ("r9") = 0;                  \
110         const __typeof__(*(ptr)) *__gu_addr = (ptr);            \
111         if (__access_ok((long)__gu_addr,size,segment)) {        \
112                 __gu_err = 0;                                   \
113                 switch (size) {                                 \
114                   case 1: __get_user_8(__gu_addr); break;       \
115                   case 2: __get_user_16(__gu_addr); break;      \
116                   case 4: __get_user_32(__gu_addr); break;      \
117                   case 8: __get_user_64(__gu_addr); break;      \
118                   default: __get_user_unknown(); break;         \
119                 }                                               \
120         }                                                       \
121         (x) = (__typeof__(*(ptr))) __gu_val;                    \
122         __gu_err;                                               \
123 })
124
125 struct __large_struct { unsigned long buf[100]; };
126 #define __m(x) (*(struct __large_struct *)(x))
127
128 /* We need to declare the __ex_table section before we can use it in .xdata.  */
129 asm (".section \"__ex_table\", \"a\"\n\t.previous");
130
131 #define __get_user_64(addr)                                                                     \
132         asm ("\n[1:]\tld8 %0=%2%P2\t// %0 and %1 get overwritten by exception handler\n"        \
133              "\t.xdata4 \"__ex_table\", 1b-., 1f-.+4\n"                                         \
134              "[1:]"                                                                             \
135              : "=r"(__gu_val), "=r"(__gu_err) : "m"(__m(addr)), "1"(__gu_err));
136
137 #define __get_user_32(addr)                                                                     \
138         asm ("\n[1:]\tld4 %0=%2%P2\t// %0 and %1 get overwritten by exception handler\n"        \
139              "\t.xdata4 \"__ex_table\", 1b-., 1f-.+4\n"                                         \
140              "[1:]"                                                                             \
141              : "=r"(__gu_val), "=r"(__gu_err) : "m"(__m(addr)), "1"(__gu_err));
142
143 #define __get_user_16(addr)                                                                     \
144         asm ("\n[1:]\tld2 %0=%2%P2\t// %0 and %1 get overwritten by exception handler\n"        \
145              "\t.xdata4 \"__ex_table\", 1b-., 1f-.+4\n"                                         \
146              "[1:]"                                                                             \
147              : "=r"(__gu_val), "=r"(__gu_err) : "m"(__m(addr)), "1"(__gu_err));
148
149 #define __get_user_8(addr)                                                                      \
150         asm ("\n[1:]\tld1 %0=%2%P2\t// %0 and %1 get overwritten by exception handler\n"        \
151              "\t.xdata4 \"__ex_table\", 1b-., 1f-.+4\n"                                         \
152              "[1:]"                                                                             \
153              : "=r"(__gu_val), "=r"(__gu_err) : "m"(__m(addr)), "1"(__gu_err));
154
155 extern void __put_user_unknown (void);
156
157 #define __put_user_nocheck(x,ptr,size)          \
158 ({                                              \
159         register long __pu_err asm ("r8") = 0;  \
160         switch (size) {                         \
161           case 1: __put_user_8(x,ptr); break;   \
162           case 2: __put_user_16(x,ptr); break;  \
163           case 4: __put_user_32(x,ptr); break;  \
164           case 8: __put_user_64(x,ptr); break;  \
165           default: __put_user_unknown(); break; \
166         }                                       \
167         __pu_err;                               \
168 })
169
170 #define __put_user_check(x,ptr,size,segment)                    \
171 ({                                                              \
172         register long __pu_err asm ("r8") = -EFAULT;            \
173         __typeof__(*(ptr)) *__pu_addr = (ptr);                  \
174         if (__access_ok((long)__pu_addr,size,segment)) {        \
175                 __pu_err = 0;                                   \
176                 switch (size) {                                 \
177                   case 1: __put_user_8(x,__pu_addr); break;     \
178                   case 2: __put_user_16(x,__pu_addr); break;    \
179                   case 4: __put_user_32(x,__pu_addr); break;    \
180                   case 8: __put_user_64(x,__pu_addr); break;    \
181                   default: __put_user_unknown(); break;         \
182                 }                                               \
183         }                                                       \
184         __pu_err;                                               \
185 })
186
187 /*
188  * The "__put_user_xx()" macros tell gcc they read from memory
189  * instead of writing: this is because they do not write to
190  * any memory gcc knows about, so there are no aliasing issues
191  */
192 #define __put_user_64(x,addr)                                                           \
193         asm volatile (                                                                  \
194                 "\n[1:]\tst8 %1=%r2%P1\t// %0 gets overwritten by exception handler\n"  \
195                 "\t.xdata4 \"__ex_table\", 1b-., 1f-.\n"                                \
196                 "[1:]"                                                                  \
197                 : "=r"(__pu_err) : "m"(__m(addr)), "rO"(x), "0"(__pu_err))
198
199 #define __put_user_32(x,addr)                                                           \
200         asm volatile (                                                                  \
201                 "\n[1:]\tst4 %1=%r2%P1\t// %0 gets overwritten by exception handler\n"  \
202                 "\t.xdata4 \"__ex_table\", 1b-., 1f-.\n"                                \
203                 "[1:]"                                                                  \
204                 : "=r"(__pu_err) : "m"(__m(addr)), "rO"(x), "0"(__pu_err))
205
206 #define __put_user_16(x,addr)                                                           \
207         asm volatile (                                                                  \
208                 "\n[1:]\tst2 %1=%r2%P1\t// %0 gets overwritten by exception handler\n"  \
209                 "\t.xdata4 \"__ex_table\", 1b-., 1f-.\n"                                \
210                 "[1:]"                                                                  \
211                 : "=r"(__pu_err) : "m"(__m(addr)), "rO"(x), "0"(__pu_err))
212
213 #define __put_user_8(x,addr)                                                            \
214         asm volatile (                                                                  \
215                 "\n[1:]\tst1 %1=%r2%P1\t// %0 gets overwritten by exception handler\n"  \
216                 "\t.xdata4 \"__ex_table\", 1b-., 1f-.\n"                                \
217                 "[1:]"                                                                  \
218                 : "=r"(__pu_err) : "m"(__m(addr)), "rO"(x), "0"(__pu_err))
219
220 /*
221  * Complex access routines
222  */
223 extern unsigned long __copy_user (void *to, const void *from, unsigned long count);
224
225 #define __copy_to_user(to,from,n)       __copy_user((to), (from), (n))
226 #define __copy_from_user(to,from,n)     __copy_user((to), (from), (n))
227
228 #define copy_to_user(to,from,n)   __copy_tofrom_user((to), (from), (n), 1)
229 #define copy_from_user(to,from,n) __copy_tofrom_user((to), (from), (n), 0)
230
231 #define __copy_tofrom_user(to,from,n,check_to)                                                  \
232 ({                                                                                              \
233         void *__cu_to = (to);                                                                   \
234         const void *__cu_from = (from);                                                         \
235         long __cu_len = (n);                                                                    \
236                                                                                                 \
237         if (__access_ok((long) ((check_to) ? __cu_to : __cu_from), __cu_len, get_fs())) {       \
238                 __cu_len = __copy_user(__cu_to, __cu_from, __cu_len);                           \
239         }                                                                                       \
240         __cu_len;                                                                               \
241 })
242
243 extern unsigned long __do_clear_user (void *, unsigned long);
244
245 #define __clear_user(to,n)                      \
246 ({                                              \
247         __do_clear_user(to,n);                  \
248 })
249
250 #define clear_user(to,n)                                        \
251 ({                                                              \
252         unsigned long __cu_len = (n);                           \
253         if (__access_ok((long) to, __cu_len, get_fs())) {       \
254                 __cu_len = __do_clear_user(to, __cu_len);       \
255         }                                                       \
256         __cu_len;                                               \
257 })
258
259
260 /* Returns: -EFAULT if exception before terminator, N if the entire
261    buffer filled, else strlen.  */
262
263 extern long __strncpy_from_user (char *to, const char *from, long to_len);
264
265 #define strncpy_from_user(to,from,n)                                    \
266 ({                                                                      \
267         const char * __sfu_from = (from);                               \
268         long __sfu_ret = -EFAULT;                                       \
269         if (__access_ok((long) __sfu_from, 0, get_fs()))                \
270                 __sfu_ret = __strncpy_from_user((to), __sfu_from, (n)); \
271         __sfu_ret;                                                      \
272 })
273
274 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
275 extern unsigned long __strlen_user (const char *);
276
277 #define strlen_user(str)                                \
278 ({                                                      \
279         const char *__su_str = (str);                   \
280         unsigned long __su_ret = 0;                     \
281         if (__access_ok((long) __su_str, 0, get_fs()))  \
282                 __su_ret = __strlen_user(__su_str);     \
283         __su_ret;                                       \
284 })
285
286 /*
287  * Returns: 0 if exception before NUL or reaching the supplied limit
288  * (N), a value greater than N if the limit would be exceeded, else
289  * strlen.
290  */
291 extern unsigned long __strnlen_user (const char *, long);
292
293 #define strnlen_user(str, len)                                  \
294 ({                                                              \
295         const char *__su_str = (str);                           \
296         unsigned long __su_ret = 0;                             \
297         if (__access_ok((long) __su_str, 0, get_fs()))          \
298                 __su_ret = __strnlen_user(__su_str, len);       \
299         __su_ret;                                               \
300 })
301
302 struct exception_table_entry {
303         int addr;       /* gp-relative address of insn this fixup is for */
304         int cont;       /* gp-relative continuation address; if bit 2 is set, r9 is set to 0 */
305 };
306
307 extern void handle_exception (struct pt_regs *regs, const struct exception_table_entry *e);
308 extern const struct exception_table_entry *search_exception_tables (unsigned long addr);
309
310 #ifdef GAS_HAS_LOCAL_TAGS
311 # define SEARCH_EXCEPTION_TABLE(regs) search_exception_tables(regs->cr_iip + ia64_psr(regs)->ri)
312 #else
313 # define SEARCH_EXCEPTION_TABLE(regs) search_exception_tables(regs->cr_iip)
314 #endif
315
316 static inline int
317 done_with_exception (struct pt_regs *regs)
318 {
319         const struct exception_table_entry *e;
320         e = SEARCH_EXCEPTION_TABLE(regs);
321         if (e) {
322                 handle_exception(regs, e);
323                 return 1;
324         }
325         return 0;
326 }
327
328 #endif /* _ASM_IA64_UACCESS_H */