ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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 safety 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  * Based on <asm-alpha/uaccess.h>.
30  *
31  * Copyright (C) 1998, 1999, 2001-2004 Hewlett-Packard Co
32  *      David Mosberger-Tang <davidm@hpl.hp.com>
33  */
34
35 #include <linux/compiler.h>
36 #include <linux/errno.h>
37 #include <linux/sched.h>
38
39 #include <asm/intrinsics.h>
40 #include <asm/pgtable.h>
41
42 /*
43  * For historical reasons, the following macros are grossly misnamed:
44  */
45 #define KERNEL_DS       ((mm_segment_t) { ~0UL })               /* cf. access_ok() */
46 #define USER_DS         ((mm_segment_t) { TASK_SIZE-1 })        /* cf. access_ok() */
47
48 #define VERIFY_READ     0
49 #define VERIFY_WRITE    1
50
51 #define get_ds()  (KERNEL_DS)
52 #define get_fs()  (current_thread_info()->addr_limit)
53 #define set_fs(x) (current_thread_info()->addr_limit = (x))
54
55 #define segment_eq(a, b)        ((a).seg == (b).seg)
56
57 /*
58  * When accessing user memory, we need to make sure the entire area really is in
59  * user-level space.  In order to do this efficiently, we make sure that the page at
60  * address TASK_SIZE is never valid.  We also need to make sure that the address doesn't
61  * point inside the virtually mapped linear page table.
62  */
63 #define __access_ok(addr, size, segment)                                        \
64         (likely((unsigned long) (addr) <= (segment).seg)                        \
65          && ((segment).seg == KERNEL_DS.seg                                     \
66              || likely(REGION_OFFSET((unsigned long) (addr)) < RGN_MAP_LIMIT)))
67 #define access_ok(type, addr, size)     __access_ok((addr), (size), get_fs())
68
69 static inline int
70 verify_area (int type, const void *addr, unsigned long size)
71 {
72         return access_ok(type, addr, size) ? 0 : -EFAULT;
73 }
74
75 /*
76  * These are the main single-value transfer routines.  They automatically
77  * use the right size if we just have the right pointer type.
78  *
79  * Careful to not
80  * (a) re-use the arguments for side effects (sizeof/typeof is ok)
81  * (b) require any knowledge of processes at this stage
82  */
83 #define put_user(x, ptr)        __put_user_check((__typeof__(*(ptr))) (x), (ptr), sizeof(*(ptr)), get_fs())
84 #define get_user(x, ptr)        __get_user_check((x), (ptr), sizeof(*(ptr)), get_fs())
85
86 /*
87  * The "__xxx" versions do not do address space checking, useful when
88  * doing multiple accesses to the same area (the programmer has to do the
89  * checks by hand with "access_ok()")
90  */
91 #define __put_user(x, ptr)      __put_user_nocheck((__typeof__(*(ptr))) (x), (ptr), sizeof(*(ptr)))
92 #define __get_user(x, ptr)      __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
93
94 #ifdef ASM_SUPPORTED
95   struct __large_struct { unsigned long buf[100]; };
96 # define __m(x) (*(struct __large_struct *)(x))
97
98 /* We need to declare the __ex_table section before we can use it in .xdata.  */
99 asm (".section \"__ex_table\", \"a\"\n\t.previous");
100
101 # define __get_user_size(val, addr, n, err)                                                     \
102 do {                                                                                            \
103         register long __gu_r8 asm ("r8") = 0;                                                   \
104         register long __gu_r9 asm ("r9");                                                       \
105         asm ("\n[1:]\tld"#n" %0=%2%P2\t// %0 and %1 get overwritten by exception handler\n"     \
106              "\t.xdata4 \"__ex_table\", 1b-., 1f-.+4\n"                                         \
107              "[1:]"                                                                             \
108              : "=r"(__gu_r9), "=r"(__gu_r8) : "m"(__m(addr)), "1"(__gu_r8));                    \
109         (err) = __gu_r8;                                                                        \
110         (val) = __gu_r9;                                                                        \
111 } while (0)
112
113 /*
114  * The "__put_user_size()" macro tells gcc it reads from memory instead of writing it.  This
115  * is because they do not write to any memory gcc knows about, so there are no aliasing
116  * issues.
117  */
118 # define __put_user_size(val, addr, n, err)                                                     \
119 do {                                                                                            \
120         register long __pu_r8 asm ("r8") = 0;                                                   \
121         asm volatile ("\n[1:]\tst"#n" %1=%r2%P1\t// %0 gets overwritten by exception handler\n" \
122                       "\t.xdata4 \"__ex_table\", 1b-., 1f-.\n"                                  \
123                       "[1:]"                                                                    \
124                       : "=r"(__pu_r8) : "m"(__m(addr)), "rO"(val), "0"(__pu_r8));               \
125         (err) = __pu_r8;                                                                        \
126 } while (0)
127
128 #else /* !ASM_SUPPORTED */
129 # define RELOC_TYPE     2       /* ip-rel */
130 # define __get_user_size(val, addr, n, err)                             \
131 do {                                                                    \
132         __ld_user("__ex_table", (unsigned long) addr, n, RELOC_TYPE);   \
133         (err) = ia64_getreg(_IA64_REG_R8);                              \
134         (val) = ia64_getreg(_IA64_REG_R9);                              \
135 } while (0)
136 # define __put_user_size(val, addr, n, err)                                                     \
137 do {                                                                                            \
138         __st_user("__ex_table", (unsigned long) addr, n, RELOC_TYPE, (unsigned long) (val));    \
139         (err) = ia64_getreg(_IA64_REG_R8);                                                      \
140 } while (0)
141 #endif /* !ASM_SUPPORTED */
142
143 extern void __get_user_unknown (void);
144
145 /*
146  * Evaluating arguments X, PTR, SIZE, and SEGMENT may involve subroutine-calls, which
147  * could clobber r8 and r9 (among others).  Thus, be careful not to evaluate it while
148  * using r8/r9.
149  */
150 #define __do_get_user(check, x, ptr, size, segment)                                     \
151 ({                                                                                      \
152         const __typeof__(*(ptr)) *__gu_ptr = (ptr);                                     \
153         __typeof__ (size) __gu_size = (size);                                           \
154         long __gu_err = -EFAULT, __gu_val = 0;                                          \
155                                                                                         \
156         if (!check || __access_ok((long) __gu_ptr, size, segment))                      \
157                 switch (__gu_size) {                                                    \
158                       case 1: __get_user_size(__gu_val, __gu_ptr, 1, __gu_err); break;  \
159                       case 2: __get_user_size(__gu_val, __gu_ptr, 2, __gu_err); break;  \
160                       case 4: __get_user_size(__gu_val, __gu_ptr, 4, __gu_err); break;  \
161                       case 8: __get_user_size(__gu_val, __gu_ptr, 8, __gu_err); break;  \
162                       default: __get_user_unknown(); break;                             \
163                 }                                                                       \
164         (x) = (__typeof__(*(__gu_ptr))) __gu_val;                                       \
165         __gu_err;                                                                       \
166 })
167
168 #define __get_user_nocheck(x, ptr, size)        __do_get_user(0, x, ptr, size, KERNEL_DS)
169 #define __get_user_check(x, ptr, size, segment) __do_get_user(1, x, ptr, size, segment)
170
171 extern void __put_user_unknown (void);
172
173 /*
174  * Evaluating arguments X, PTR, SIZE, and SEGMENT may involve subroutine-calls, which
175  * could clobber r8 (among others).  Thus, be careful not to evaluate them while using r8.
176  */
177 #define __do_put_user(check, x, ptr, size, segment)                                     \
178 ({                                                                                      \
179         __typeof__ (x) __pu_x = (x);                                                    \
180         __typeof__ (*(ptr)) *__pu_ptr = (ptr);                                          \
181         __typeof__ (size) __pu_size = (size);                                           \
182         long __pu_err = -EFAULT;                                                        \
183                                                                                         \
184         if (!check || __access_ok((long) __pu_ptr, __pu_size, segment))                 \
185                 switch (__pu_size) {                                                    \
186                       case 1: __put_user_size(__pu_x, __pu_ptr, 1, __pu_err); break;    \
187                       case 2: __put_user_size(__pu_x, __pu_ptr, 2, __pu_err); break;    \
188                       case 4: __put_user_size(__pu_x, __pu_ptr, 4, __pu_err); break;    \
189                       case 8: __put_user_size(__pu_x, __pu_ptr, 8, __pu_err); break;    \
190                       default: __put_user_unknown(); break;                             \
191                 }                                                                       \
192         __pu_err;                                                                       \
193 })
194
195 #define __put_user_nocheck(x, ptr, size)        __do_put_user(0, x, ptr, size, KERNEL_DS)
196 #define __put_user_check(x, ptr, size, segment) __do_put_user(1, x, ptr, size, segment)
197
198 /*
199  * Complex access routines
200  */
201 extern unsigned long __copy_user (void *to, const void *from, unsigned long count);
202
203 #define __copy_to_user(to, from, n)     __copy_user((to), (from), (n))
204 #define __copy_from_user(to, from, n)   __copy_user((to), (from), (n))
205
206 #define copy_to_user(to, from, n)       __copy_tofrom_user((to), (from), (n), 1)
207 #define copy_from_user(to, from, n)     __copy_tofrom_user((to), (from), (n), 0)
208
209 #define __copy_tofrom_user(to, from, n, check_to)                                       \
210 ({                                                                                      \
211         void *__cu_to = (to);                                                           \
212         const void *__cu_from = (from);                                                 \
213         long __cu_len = (n);                                                            \
214                                                                                         \
215         if (__access_ok((long) ((check_to) ? __cu_to : __cu_from), __cu_len, get_fs())) \
216                 __cu_len = __copy_user(__cu_to, __cu_from, __cu_len);                   \
217         __cu_len;                                                                       \
218 })
219
220 #define __copy_in_user(to, from, size)  __copy_user((to), (from), (size))
221
222 static inline unsigned long
223 copy_in_user (void *to, const void *from, unsigned long n)
224 {
225         if (likely(access_ok(VERIFY_READ, from, n) && access_ok(VERIFY_WRITE, to, n)))
226                 n = __copy_user(to, from, n);
227         return n;
228 }
229
230 extern unsigned long __do_clear_user (void *, unsigned long);
231
232 #define __clear_user(to, n)             __do_clear_user(to, n)
233
234 #define clear_user(to, n)                                       \
235 ({                                                              \
236         unsigned long __cu_len = (n);                           \
237         if (__access_ok((long) to, __cu_len, get_fs()))         \
238                 __cu_len = __do_clear_user(to, __cu_len);       \
239         __cu_len;                                               \
240 })
241
242
243 /*
244  * Returns: -EFAULT if exception before terminator, N if the entire buffer filled, else
245  * strlen.
246  */
247 extern long __strncpy_from_user (char *to, const char *from, long to_len);
248
249 #define strncpy_from_user(to, from, n)                                  \
250 ({                                                                      \
251         const char * __sfu_from = (from);                               \
252         long __sfu_ret = -EFAULT;                                       \
253         if (__access_ok((long) __sfu_from, 0, get_fs()))                \
254                 __sfu_ret = __strncpy_from_user((to), __sfu_from, (n)); \
255         __sfu_ret;                                                      \
256 })
257
258 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
259 extern unsigned long __strlen_user (const char *);
260
261 #define strlen_user(str)                                \
262 ({                                                      \
263         const char *__su_str = (str);                   \
264         unsigned long __su_ret = 0;                     \
265         if (__access_ok((long) __su_str, 0, get_fs()))  \
266                 __su_ret = __strlen_user(__su_str);     \
267         __su_ret;                                       \
268 })
269
270 /*
271  * Returns: 0 if exception before NUL or reaching the supplied limit
272  * (N), a value greater than N if the limit would be exceeded, else
273  * strlen.
274  */
275 extern unsigned long __strnlen_user (const char *, long);
276
277 #define strnlen_user(str, len)                                  \
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 = __strnlen_user(__su_str, len);       \
283         __su_ret;                                               \
284 })
285
286 /* Generic code can't deal with the location-relative format that we use for compactness.  */
287 #define ARCH_HAS_SORT_EXTABLE
288 #define ARCH_HAS_SEARCH_EXTABLE
289
290 struct exception_table_entry {
291         int addr;       /* location-relative address of insn this fixup is for */
292         int cont;       /* location-relative continuation addr.; if bit 2 is set, r9 is set to 0 */
293 };
294
295 extern void ia64_handle_exception (struct pt_regs *regs, const struct exception_table_entry *e);
296 extern const struct exception_table_entry *search_exception_tables (unsigned long addr);
297
298 static inline int
299 ia64_done_with_exception (struct pt_regs *regs)
300 {
301         const struct exception_table_entry *e;
302         e = search_exception_tables(regs->cr_iip + ia64_psr(regs)->ri);
303         if (e) {
304                 ia64_handle_exception(regs, e);
305                 return 1;
306         }
307         return 0;
308 }
309
310 #endif /* _ASM_IA64_UACCESS_H */