1 #ifndef _ASM_IA64_UACCESS_H
2 #define _ASM_IA64_UACCESS_H
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
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.
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.
29 * Based on <asm-alpha/uaccess.h>.
31 * Copyright (C) 1998, 1999, 2001-2004 Hewlett-Packard Co
32 * David Mosberger-Tang <davidm@hpl.hp.com>
35 #include <linux/compiler.h>
36 #include <linux/errno.h>
37 #include <linux/sched.h>
39 #include <asm/intrinsics.h>
40 #include <asm/pgtable.h>
43 * For historical reasons, the following macros are grossly misnamed:
45 #define KERNEL_DS ((mm_segment_t) { ~0UL }) /* cf. access_ok() */
46 #define USER_DS ((mm_segment_t) { TASK_SIZE-1 }) /* cf. access_ok() */
49 #define VERIFY_WRITE 1
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))
55 #define segment_eq(a, b) ((a).seg == (b).seg)
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.
63 #define __access_ok(addr, size, segment) \
65 __chk_user_ptr(addr); \
66 (likely((unsigned long) (addr) <= (segment).seg) \
67 && ((segment).seg == KERNEL_DS.seg \
68 || likely(REGION_OFFSET((unsigned long) (addr)) < RGN_MAP_LIMIT))); \
70 #define access_ok(type, addr, size) __access_ok((addr), (size), get_fs())
73 verify_area (int type
, const void __user
*addr
, unsigned long size
)
75 return access_ok(type
, addr
, size
) ? 0 : -EFAULT
;
79 * These are the main single-value transfer routines. They automatically
80 * use the right size if we just have the right pointer type.
83 * (a) re-use the arguments for side effects (sizeof/typeof is ok)
84 * (b) require any knowledge of processes at this stage
86 #define put_user(x, ptr) __put_user_check((__typeof__(*(ptr))) (x), (ptr), sizeof(*(ptr)), get_fs())
87 #define get_user(x, ptr) __get_user_check((x), (ptr), sizeof(*(ptr)), get_fs())
90 * The "__xxx" versions do not do address space checking, useful when
91 * doing multiple accesses to the same area (the programmer has to do the
92 * checks by hand with "access_ok()")
94 #define __put_user(x, ptr) __put_user_nocheck((__typeof__(*(ptr))) (x), (ptr), sizeof(*(ptr)))
95 #define __get_user(x, ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
97 extern long __put_user_unaligned_unknown (void);
99 #define __put_user_unaligned(x, ptr) \
102 switch (sizeof(*(ptr))) { \
103 case 1: __ret = __put_user((x), (ptr)); break; \
104 case 2: __ret = (__put_user((x), (u8 __user *)(ptr))) \
105 | (__put_user((x) >> 8, ((u8 __user *)(ptr) + 1))); break; \
106 case 4: __ret = (__put_user((x), (u16 __user *)(ptr))) \
107 | (__put_user((x) >> 16, ((u16 __user *)(ptr) + 1))); break; \
108 case 8: __ret = (__put_user((x), (u32 __user *)(ptr))) \
109 | (__put_user((x) >> 32, ((u32 __user *)(ptr) + 1))); break; \
110 default: __ret = __put_user_unaligned_unknown(); \
115 extern long __get_user_unaligned_unknown (void);
117 #define __get_user_unaligned(x, ptr) \
120 switch (sizeof(*(ptr))) { \
121 case 1: __ret = __get_user((x), (ptr)); break; \
122 case 2: __ret = (__get_user((x), (u8 __user *)(ptr))) \
123 | (__get_user((x) >> 8, ((u8 __user *)(ptr) + 1))); break; \
124 case 4: __ret = (__get_user((x), (u16 __user *)(ptr))) \
125 | (__get_user((x) >> 16, ((u16 __user *)(ptr) + 1))); break; \
126 case 8: __ret = (__get_user((x), (u32 __user *)(ptr))) \
127 | (__get_user((x) >> 32, ((u32 __user *)(ptr) + 1))); break; \
128 default: __ret = __get_user_unaligned_unknown(); \
134 struct __large_struct
{ unsigned long buf
[100]; };
135 # define __m(x) (*(struct __large_struct *)(x))
137 /* We need to declare the __ex_table section before we can use it in .xdata. */
138 asm (".section \"__ex_table\", \"a\"\n\t.previous");
140 # define __get_user_size(val, addr, n, err) \
142 register long __gu_r8 asm ("r8") = 0; \
143 register long __gu_r9 asm ("r9"); \
144 asm ("\n[1:]\tld"#n" %0=%2%P2\t// %0 and %1 get overwritten by exception handler\n" \
145 "\t.xdata4 \"__ex_table\", 1b-., 1f-.+4\n" \
147 : "=r"(__gu_r9), "=r"(__gu_r8) : "m"(__m(addr)), "1"(__gu_r8)); \
153 * The "__put_user_size()" macro tells gcc it reads from memory instead of writing it. This
154 * is because they do not write to any memory gcc knows about, so there are no aliasing
157 # define __put_user_size(val, addr, n, err) \
159 register long __pu_r8 asm ("r8") = 0; \
160 asm volatile ("\n[1:]\tst"#n" %1=%r2%P1\t// %0 gets overwritten by exception handler\n" \
161 "\t.xdata4 \"__ex_table\", 1b-., 1f-.\n" \
163 : "=r"(__pu_r8) : "m"(__m(addr)), "rO"(val), "0"(__pu_r8)); \
167 #else /* !ASM_SUPPORTED */
168 # define RELOC_TYPE 2 /* ip-rel */
169 # define __get_user_size(val, addr, n, err) \
171 __ld_user("__ex_table", (unsigned long) addr, n, RELOC_TYPE); \
172 (err) = ia64_getreg(_IA64_REG_R8); \
173 (val) = ia64_getreg(_IA64_REG_R9); \
175 # define __put_user_size(val, addr, n, err) \
177 __st_user("__ex_table", (unsigned long) addr, n, RELOC_TYPE, (unsigned long) (val)); \
178 (err) = ia64_getreg(_IA64_REG_R8); \
180 #endif /* !ASM_SUPPORTED */
182 extern void __get_user_unknown (void);
185 * Evaluating arguments X, PTR, SIZE, and SEGMENT may involve subroutine-calls, which
186 * could clobber r8 and r9 (among others). Thus, be careful not to evaluate it while
189 #define __do_get_user(check, x, ptr, size, segment) \
191 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
192 __typeof__ (size) __gu_size = (size); \
193 long __gu_err = -EFAULT, __gu_val = 0; \
195 if (!check || __access_ok(__gu_ptr, size, segment)) \
196 switch (__gu_size) { \
197 case 1: __get_user_size(__gu_val, __gu_ptr, 1, __gu_err); break; \
198 case 2: __get_user_size(__gu_val, __gu_ptr, 2, __gu_err); break; \
199 case 4: __get_user_size(__gu_val, __gu_ptr, 4, __gu_err); break; \
200 case 8: __get_user_size(__gu_val, __gu_ptr, 8, __gu_err); break; \
201 default: __get_user_unknown(); break; \
203 (x) = (__typeof__(*(__gu_ptr))) __gu_val; \
207 #define __get_user_nocheck(x, ptr, size) __do_get_user(0, x, ptr, size, KERNEL_DS)
208 #define __get_user_check(x, ptr, size, segment) __do_get_user(1, x, ptr, size, segment)
210 extern void __put_user_unknown (void);
213 * Evaluating arguments X, PTR, SIZE, and SEGMENT may involve subroutine-calls, which
214 * could clobber r8 (among others). Thus, be careful not to evaluate them while using r8.
216 #define __do_put_user(check, x, ptr, size, segment) \
218 __typeof__ (x) __pu_x = (x); \
219 __typeof__ (*(ptr)) __user *__pu_ptr = (ptr); \
220 __typeof__ (size) __pu_size = (size); \
221 long __pu_err = -EFAULT; \
223 if (!check || __access_ok(__pu_ptr, __pu_size, segment)) \
224 switch (__pu_size) { \
225 case 1: __put_user_size(__pu_x, __pu_ptr, 1, __pu_err); break; \
226 case 2: __put_user_size(__pu_x, __pu_ptr, 2, __pu_err); break; \
227 case 4: __put_user_size(__pu_x, __pu_ptr, 4, __pu_err); break; \
228 case 8: __put_user_size(__pu_x, __pu_ptr, 8, __pu_err); break; \
229 default: __put_user_unknown(); break; \
234 #define __put_user_nocheck(x, ptr, size) __do_put_user(0, x, ptr, size, KERNEL_DS)
235 #define __put_user_check(x, ptr, size, segment) __do_put_user(1, x, ptr, size, segment)
238 * Complex access routines
240 extern unsigned long __must_check
__copy_user (void __user
*to
, const void __user
*from
,
241 unsigned long count
);
243 static inline unsigned long
244 __copy_to_user (void __user
*to
, const void *from
, unsigned long count
)
246 return __copy_user(to
, (void __user
*) from
, count
);
249 static inline unsigned long
250 __copy_from_user (void *to
, const void __user
*from
, unsigned long count
)
252 return __copy_user((void __user
*) to
, from
, count
);
255 #define __copy_to_user_inatomic __copy_to_user
256 #define __copy_from_user_inatomic __copy_from_user
257 #define copy_to_user(to, from, n) \
259 void __user *__cu_to = (to); \
260 const void *__cu_from = (from); \
261 long __cu_len = (n); \
263 if (__access_ok(__cu_to, __cu_len, get_fs())) \
264 __cu_len = __copy_user(__cu_to, (void __user *) __cu_from, __cu_len); \
268 #define copy_from_user(to, from, n) \
270 void *__cu_to = (to); \
271 const void __user *__cu_from = (from); \
272 long __cu_len = (n); \
274 __chk_user_ptr(__cu_from); \
275 if (__access_ok(__cu_from, __cu_len, get_fs())) \
276 __cu_len = __copy_user((void __user *) __cu_to, __cu_from, __cu_len); \
280 #define __copy_in_user(to, from, size) __copy_user((to), (from), (size))
282 static inline unsigned long
283 copy_in_user (void __user
*to
, const void __user
*from
, unsigned long n
)
285 if (likely(access_ok(VERIFY_READ
, from
, n
) && access_ok(VERIFY_WRITE
, to
, n
)))
286 n
= __copy_user(to
, from
, n
);
290 extern unsigned long __do_clear_user (void __user
*, unsigned long);
292 #define __clear_user(to, n) __do_clear_user(to, n)
294 #define clear_user(to, n) \
296 unsigned long __cu_len = (n); \
297 if (__access_ok(to, __cu_len, get_fs())) \
298 __cu_len = __do_clear_user(to, __cu_len); \
304 * Returns: -EFAULT if exception before terminator, N if the entire buffer filled, else
307 extern long __must_check
__strncpy_from_user (char *to
, const char __user
*from
, long to_len
);
309 #define strncpy_from_user(to, from, n) \
311 const char __user * __sfu_from = (from); \
312 long __sfu_ret = -EFAULT; \
313 if (__access_ok(__sfu_from, 0, get_fs())) \
314 __sfu_ret = __strncpy_from_user((to), __sfu_from, (n)); \
318 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
319 extern unsigned long __strlen_user (const char __user
*);
321 #define strlen_user(str) \
323 const char __user *__su_str = (str); \
324 unsigned long __su_ret = 0; \
325 if (__access_ok(__su_str, 0, get_fs())) \
326 __su_ret = __strlen_user(__su_str); \
331 * Returns: 0 if exception before NUL or reaching the supplied limit
332 * (N), a value greater than N if the limit would be exceeded, else
335 extern unsigned long __strnlen_user (const char __user
*, long);
337 #define strnlen_user(str, len) \
339 const char __user *__su_str = (str); \
340 unsigned long __su_ret = 0; \
341 if (__access_ok(__su_str, 0, get_fs())) \
342 __su_ret = __strnlen_user(__su_str, len); \
346 /* Generic code can't deal with the location-relative format that we use for compactness. */
347 #define ARCH_HAS_SORT_EXTABLE
348 #define ARCH_HAS_SEARCH_EXTABLE
350 struct exception_table_entry
{
351 int addr
; /* location-relative address of insn this fixup is for */
352 int cont
; /* location-relative continuation addr.; if bit 2 is set, r9 is set to 0 */
355 extern void ia64_handle_exception (struct pt_regs
*regs
, const struct exception_table_entry
*e
);
356 extern const struct exception_table_entry
*search_exception_tables (unsigned long addr
);
359 ia64_done_with_exception (struct pt_regs
*regs
)
361 const struct exception_table_entry
*e
;
362 e
= search_exception_tables(regs
->cr_iip
+ ia64_psr(regs
)->ri
);
364 ia64_handle_exception(regs
, e
);
370 #endif /* _ASM_IA64_UACCESS_H */