]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/ia64/include/asm/uaccess.h
Merge commit 'b4fb8f66f1ae2e167d06c12d018025a8d4d3ba7e' into uaccess.ia64
[karo-tx-linux.git] / arch / ia64 / include / asm / 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/page-flags.h>
37 #include <linux/mm.h>
38
39 #include <asm/intrinsics.h>
40 #include <asm/pgtable.h>
41 #include <asm/io.h>
42
43 /*
44  * For historical reasons, the following macros are grossly misnamed:
45  */
46 #define KERNEL_DS       ((mm_segment_t) { ~0UL })               /* cf. access_ok() */
47 #define USER_DS         ((mm_segment_t) { TASK_SIZE-1 })        /* cf. access_ok() */
48
49 #define get_ds()  (KERNEL_DS)
50 #define get_fs()  (current_thread_info()->addr_limit)
51 #define set_fs(x) (current_thread_info()->addr_limit = (x))
52
53 #define segment_eq(a, b)        ((a).seg == (b).seg)
54
55 /*
56  * When accessing user memory, we need to make sure the entire area really is in
57  * user-level space.  In order to do this efficiently, we make sure that the page at
58  * address TASK_SIZE is never valid.  We also need to make sure that the address doesn't
59  * point inside the virtually mapped linear page table.
60  */
61 #define __access_ok(addr, size, segment)                                                \
62 ({                                                                                      \
63         __chk_user_ptr(addr);                                                           \
64         (likely((unsigned long) (addr) <= (segment).seg)                                \
65          && ((segment).seg == KERNEL_DS.seg                                             \
66              || likely(REGION_OFFSET((unsigned long) (addr)) < RGN_MAP_LIMIT)));        \
67 })
68 #define access_ok(type, addr, size)     __access_ok((addr), (size), get_fs())
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 long __put_user_unaligned_unknown (void);
90
91 #define __put_user_unaligned(x, ptr)                                                            \
92 ({                                                                                              \
93         long __ret;                                                                             \
94         switch (sizeof(*(ptr))) {                                                               \
95                 case 1: __ret = __put_user((x), (ptr)); break;                                  \
96                 case 2: __ret = (__put_user((x), (u8 __user *)(ptr)))                           \
97                         | (__put_user((x) >> 8, ((u8 __user *)(ptr) + 1))); break;              \
98                 case 4: __ret = (__put_user((x), (u16 __user *)(ptr)))                          \
99                         | (__put_user((x) >> 16, ((u16 __user *)(ptr) + 1))); break;            \
100                 case 8: __ret = (__put_user((x), (u32 __user *)(ptr)))                          \
101                         | (__put_user((x) >> 32, ((u32 __user *)(ptr) + 1))); break;            \
102                 default: __ret = __put_user_unaligned_unknown();                                \
103         }                                                                                       \
104         __ret;                                                                                  \
105 })
106
107 extern long __get_user_unaligned_unknown (void);
108
109 #define __get_user_unaligned(x, ptr)                                                            \
110 ({                                                                                              \
111         long __ret;                                                                             \
112         switch (sizeof(*(ptr))) {                                                               \
113                 case 1: __ret = __get_user((x), (ptr)); break;                                  \
114                 case 2: __ret = (__get_user((x), (u8 __user *)(ptr)))                           \
115                         | (__get_user((x) >> 8, ((u8 __user *)(ptr) + 1))); break;              \
116                 case 4: __ret = (__get_user((x), (u16 __user *)(ptr)))                          \
117                         | (__get_user((x) >> 16, ((u16 __user *)(ptr) + 1))); break;            \
118                 case 8: __ret = (__get_user((x), (u32 __user *)(ptr)))                          \
119                         | (__get_user((x) >> 32, ((u32 __user *)(ptr) + 1))); break;            \
120                 default: __ret = __get_user_unaligned_unknown();                                \
121         }                                                                                       \
122         __ret;                                                                                  \
123 })
124
125 #ifdef ASM_SUPPORTED
126   struct __large_struct { unsigned long buf[100]; };
127 # define __m(x) (*(struct __large_struct __user *)(x))
128
129 /* We need to declare the __ex_table section before we can use it in .xdata.  */
130 asm (".section \"__ex_table\", \"a\"\n\t.previous");
131
132 # define __get_user_size(val, addr, n, err)                                                     \
133 do {                                                                                            \
134         register long __gu_r8 asm ("r8") = 0;                                                   \
135         register long __gu_r9 asm ("r9");                                                       \
136         asm ("\n[1:]\tld"#n" %0=%2%P2\t// %0 and %1 get overwritten by exception handler\n"     \
137              "\t.xdata4 \"__ex_table\", 1b-., 1f-.+4\n"                                         \
138              "[1:]"                                                                             \
139              : "=r"(__gu_r9), "=r"(__gu_r8) : "m"(__m(addr)), "1"(__gu_r8));                    \
140         (err) = __gu_r8;                                                                        \
141         (val) = __gu_r9;                                                                        \
142 } while (0)
143
144 /*
145  * The "__put_user_size()" macro tells gcc it reads from memory instead of writing it.  This
146  * is because they do not write to any memory gcc knows about, so there are no aliasing
147  * issues.
148  */
149 # define __put_user_size(val, addr, n, err)                                                     \
150 do {                                                                                            \
151         register long __pu_r8 asm ("r8") = 0;                                                   \
152         asm volatile ("\n[1:]\tst"#n" %1=%r2%P1\t// %0 gets overwritten by exception handler\n" \
153                       "\t.xdata4 \"__ex_table\", 1b-., 1f-.\n"                                  \
154                       "[1:]"                                                                    \
155                       : "=r"(__pu_r8) : "m"(__m(addr)), "rO"(val), "0"(__pu_r8));               \
156         (err) = __pu_r8;                                                                        \
157 } while (0)
158
159 #else /* !ASM_SUPPORTED */
160 # define RELOC_TYPE     2       /* ip-rel */
161 # define __get_user_size(val, addr, n, err)                             \
162 do {                                                                    \
163         __ld_user("__ex_table", (unsigned long) addr, n, RELOC_TYPE);   \
164         (err) = ia64_getreg(_IA64_REG_R8);                              \
165         (val) = ia64_getreg(_IA64_REG_R9);                              \
166 } while (0)
167 # define __put_user_size(val, addr, n, err)                             \
168 do {                                                                    \
169         __st_user("__ex_table", (unsigned long) addr, n, RELOC_TYPE,    \
170                   (__force unsigned long) (val));                       \
171         (err) = ia64_getreg(_IA64_REG_R8);                              \
172 } while (0)
173 #endif /* !ASM_SUPPORTED */
174
175 extern void __get_user_unknown (void);
176
177 /*
178  * Evaluating arguments X, PTR, SIZE, and SEGMENT may involve subroutine-calls, which
179  * could clobber r8 and r9 (among others).  Thus, be careful not to evaluate it while
180  * using r8/r9.
181  */
182 #define __do_get_user(check, x, ptr, size, segment)                                     \
183 ({                                                                                      \
184         const __typeof__(*(ptr)) __user *__gu_ptr = (ptr);                              \
185         __typeof__ (size) __gu_size = (size);                                           \
186         long __gu_err = -EFAULT;                                                        \
187         unsigned long __gu_val = 0;                                                     \
188         if (!check || __access_ok(__gu_ptr, size, segment))                             \
189                 switch (__gu_size) {                                                    \
190                       case 1: __get_user_size(__gu_val, __gu_ptr, 1, __gu_err); break;  \
191                       case 2: __get_user_size(__gu_val, __gu_ptr, 2, __gu_err); break;  \
192                       case 4: __get_user_size(__gu_val, __gu_ptr, 4, __gu_err); break;  \
193                       case 8: __get_user_size(__gu_val, __gu_ptr, 8, __gu_err); break;  \
194                       default: __get_user_unknown(); break;                             \
195                 }                                                                       \
196         (x) = (__force __typeof__(*(__gu_ptr))) __gu_val;                               \
197         __gu_err;                                                                       \
198 })
199
200 #define __get_user_nocheck(x, ptr, size)        __do_get_user(0, x, ptr, size, KERNEL_DS)
201 #define __get_user_check(x, ptr, size, segment) __do_get_user(1, x, ptr, size, segment)
202
203 extern void __put_user_unknown (void);
204
205 /*
206  * Evaluating arguments X, PTR, SIZE, and SEGMENT may involve subroutine-calls, which
207  * could clobber r8 (among others).  Thus, be careful not to evaluate them while using r8.
208  */
209 #define __do_put_user(check, x, ptr, size, segment)                                     \
210 ({                                                                                      \
211         __typeof__ (x) __pu_x = (x);                                                    \
212         __typeof__ (*(ptr)) __user *__pu_ptr = (ptr);                                   \
213         __typeof__ (size) __pu_size = (size);                                           \
214         long __pu_err = -EFAULT;                                                        \
215                                                                                         \
216         if (!check || __access_ok(__pu_ptr, __pu_size, segment))                        \
217                 switch (__pu_size) {                                                    \
218                       case 1: __put_user_size(__pu_x, __pu_ptr, 1, __pu_err); break;    \
219                       case 2: __put_user_size(__pu_x, __pu_ptr, 2, __pu_err); break;    \
220                       case 4: __put_user_size(__pu_x, __pu_ptr, 4, __pu_err); break;    \
221                       case 8: __put_user_size(__pu_x, __pu_ptr, 8, __pu_err); break;    \
222                       default: __put_user_unknown(); break;                             \
223                 }                                                                       \
224         __pu_err;                                                                       \
225 })
226
227 #define __put_user_nocheck(x, ptr, size)        __do_put_user(0, x, ptr, size, KERNEL_DS)
228 #define __put_user_check(x, ptr, size, segment) __do_put_user(1, x, ptr, size, segment)
229
230 /*
231  * Complex access routines
232  */
233 extern unsigned long __must_check __copy_user (void __user *to, const void __user *from,
234                                                unsigned long count);
235
236 static inline unsigned long
237 __copy_to_user (void __user *to, const void *from, unsigned long count)
238 {
239         check_object_size(from, count, true);
240
241         return __copy_user(to, (__force void __user *) from, count);
242 }
243
244 static inline unsigned long
245 __copy_from_user (void *to, const void __user *from, unsigned long count)
246 {
247         check_object_size(to, count, false);
248
249         return __copy_user((__force void __user *) to, from, count);
250 }
251
252 #define __copy_to_user_inatomic         __copy_to_user
253 #define __copy_from_user_inatomic       __copy_from_user
254 #define copy_to_user(to, from, n)                                                       \
255 ({                                                                                      \
256         void __user *__cu_to = (to);                                                    \
257         const void *__cu_from = (from);                                                 \
258         long __cu_len = (n);                                                            \
259                                                                                         \
260         if (__access_ok(__cu_to, __cu_len, get_fs())) {                                 \
261                 check_object_size(__cu_from, __cu_len, true);                   \
262                 __cu_len = __copy_user(__cu_to, (__force void __user *)  __cu_from, __cu_len);  \
263         }                                                                               \
264         __cu_len;                                                                       \
265 })
266
267 static inline unsigned long
268 copy_from_user(void *to, const void __user *from, unsigned long n)
269 {
270         check_object_size(to, n, false);
271         if (likely(__access_ok(from, n, get_fs())))
272                 n = __copy_user((__force void __user *) to, from, n);
273         else
274                 memset(to, 0, n);
275         return n;
276 }
277
278 #define __copy_in_user(to, from, size)  __copy_user((to), (from), (size))
279
280 static inline unsigned long
281 copy_in_user (void __user *to, const void __user *from, unsigned long n)
282 {
283         if (likely(access_ok(VERIFY_READ, from, n) && access_ok(VERIFY_WRITE, to, n)))
284                 n = __copy_user(to, from, n);
285         return n;
286 }
287
288 extern unsigned long __do_clear_user (void __user *, unsigned long);
289
290 #define __clear_user(to, n)             __do_clear_user(to, n)
291
292 #define clear_user(to, n)                                       \
293 ({                                                              \
294         unsigned long __cu_len = (n);                           \
295         if (__access_ok(to, __cu_len, get_fs()))                \
296                 __cu_len = __do_clear_user(to, __cu_len);       \
297         __cu_len;                                               \
298 })
299
300
301 /*
302  * Returns: -EFAULT if exception before terminator, N if the entire buffer filled, else
303  * strlen.
304  */
305 extern long __must_check __strncpy_from_user (char *to, const char __user *from, long to_len);
306
307 #define strncpy_from_user(to, from, n)                                  \
308 ({                                                                      \
309         const char __user * __sfu_from = (from);                        \
310         long __sfu_ret = -EFAULT;                                       \
311         if (__access_ok(__sfu_from, 0, get_fs()))                       \
312                 __sfu_ret = __strncpy_from_user((to), __sfu_from, (n)); \
313         __sfu_ret;                                                      \
314 })
315
316 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
317 extern unsigned long __strlen_user (const char __user *);
318
319 #define strlen_user(str)                                \
320 ({                                                      \
321         const char __user *__su_str = (str);            \
322         unsigned long __su_ret = 0;                     \
323         if (__access_ok(__su_str, 0, get_fs()))         \
324                 __su_ret = __strlen_user(__su_str);     \
325         __su_ret;                                       \
326 })
327
328 /*
329  * Returns: 0 if exception before NUL or reaching the supplied limit
330  * (N), a value greater than N if the limit would be exceeded, else
331  * strlen.
332  */
333 extern unsigned long __strnlen_user (const char __user *, long);
334
335 #define strnlen_user(str, len)                                  \
336 ({                                                              \
337         const char __user *__su_str = (str);                    \
338         unsigned long __su_ret = 0;                             \
339         if (__access_ok(__su_str, 0, get_fs()))                 \
340                 __su_ret = __strnlen_user(__su_str, len);       \
341         __su_ret;                                               \
342 })
343
344 #define ARCH_HAS_RELATIVE_EXTABLE
345
346 struct exception_table_entry {
347         int insn;       /* location-relative address of insn this fixup is for */
348         int fixup;      /* location-relative continuation addr.; if bit 2 is set, r9 is set to 0 */
349 };
350
351 #define ARCH_HAS_TRANSLATE_MEM_PTR      1
352 static __inline__ void *
353 xlate_dev_mem_ptr(phys_addr_t p)
354 {
355         struct page *page;
356         void *ptr;
357
358         page = pfn_to_page(p >> PAGE_SHIFT);
359         if (PageUncached(page))
360                 ptr = (void *)p + __IA64_UNCACHED_OFFSET;
361         else
362                 ptr = __va(p);
363
364         return ptr;
365 }
366
367 /*
368  * Convert a virtual cached kernel memory pointer to an uncached pointer
369  */
370 static __inline__ void *
371 xlate_dev_kmem_ptr(void *p)
372 {
373         struct page *page;
374         void *ptr;
375
376         page = virt_to_page((unsigned long)p);
377         if (PageUncached(page))
378                 ptr = (void *)__pa(p) + __IA64_UNCACHED_OFFSET;
379         else
380                 ptr = p;
381
382         return ptr;
383 }
384
385 #endif /* _ASM_IA64_UACCESS_H */