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 saftey 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 * Copyright (C) 1998, 1999, 2001-2003 Hewlett-Packard Co
30 * David Mosberger-Tang <davidm@hpl.hp.com>
33 #include <linux/errno.h>
34 #include <linux/sched.h>
36 #include <asm/pgtable.h>
39 * For historical reasons, the following macros are grossly misnamed:
41 #define KERNEL_DS ((mm_segment_t) { ~0UL }) /* cf. access_ok() */
42 #define USER_DS ((mm_segment_t) { TASK_SIZE-1 }) /* cf. access_ok() */
45 #define VERIFY_WRITE 1
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))
51 #define segment_eq(a,b) ((a).seg == (b).seg)
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.
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())
65 verify_area (int type, const void *addr, unsigned long size)
67 return access_ok(type,addr,size) ? 0 : -EFAULT;
71 * These are the main single-value transfer routines. They automatically
72 * use the right size if we just have the right pointer type.
75 * (a) re-use the arguments for side effects (sizeof/typeof is ok)
76 * (b) require any knowledge of processes at this stage
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())
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()")
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)))
89 extern void __get_user_unknown (void);
91 #define __get_user_nocheck(x,ptr,size) \
93 register long __gu_err asm ("r8") = 0; \
94 register long __gu_val asm ("r9") = 0; \
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; \
102 (x) = (__typeof__(*(ptr))) __gu_val; \
106 #define __get_user_check(x,ptr,size,segment) \
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)) { \
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; \
121 (x) = (__typeof__(*(ptr))) __gu_val; \
125 struct __large_struct { unsigned long buf[100]; };
126 #define __m(x) (*(struct __large_struct *)(x))
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");
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" \
135 : "=r"(__gu_val), "=r"(__gu_err) : "m"(__m(addr)), "1"(__gu_err));
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" \
141 : "=r"(__gu_val), "=r"(__gu_err) : "m"(__m(addr)), "1"(__gu_err));
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" \
147 : "=r"(__gu_val), "=r"(__gu_err) : "m"(__m(addr)), "1"(__gu_err));
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" \
153 : "=r"(__gu_val), "=r"(__gu_err) : "m"(__m(addr)), "1"(__gu_err));
155 extern void __put_user_unknown (void);
157 #define __put_user_nocheck(x,ptr,size) \
159 register long __pu_err asm ("r8") = 0; \
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; \
170 #define __put_user_check(x,ptr,size,segment) \
172 register long __pu_err asm ("r8") = -EFAULT; \
173 __typeof__(*(ptr)) *__pu_addr = (ptr); \
174 if (__access_ok((long)__pu_addr,size,segment)) { \
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; \
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
192 #define __put_user_64(x,addr) \
194 "\n[1:]\tst8 %1=%r2%P1\t// %0 gets overwritten by exception handler\n" \
195 "\t.xdata4 \"__ex_table\", 1b-., 1f-.\n" \
197 : "=r"(__pu_err) : "m"(__m(addr)), "rO"(x), "0"(__pu_err))
199 #define __put_user_32(x,addr) \
201 "\n[1:]\tst4 %1=%r2%P1\t// %0 gets overwritten by exception handler\n" \
202 "\t.xdata4 \"__ex_table\", 1b-., 1f-.\n" \
204 : "=r"(__pu_err) : "m"(__m(addr)), "rO"(x), "0"(__pu_err))
206 #define __put_user_16(x,addr) \
208 "\n[1:]\tst2 %1=%r2%P1\t// %0 gets overwritten by exception handler\n" \
209 "\t.xdata4 \"__ex_table\", 1b-., 1f-.\n" \
211 : "=r"(__pu_err) : "m"(__m(addr)), "rO"(x), "0"(__pu_err))
213 #define __put_user_8(x,addr) \
215 "\n[1:]\tst1 %1=%r2%P1\t// %0 gets overwritten by exception handler\n" \
216 "\t.xdata4 \"__ex_table\", 1b-., 1f-.\n" \
218 : "=r"(__pu_err) : "m"(__m(addr)), "rO"(x), "0"(__pu_err))
221 * Complex access routines
223 extern unsigned long __copy_user (void *to, const void *from, unsigned long count);
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))
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)
231 #define __copy_tofrom_user(to,from,n,check_to) \
233 void *__cu_to = (to); \
234 const void *__cu_from = (from); \
235 long __cu_len = (n); \
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); \
243 extern unsigned long __do_clear_user (void *, unsigned long);
245 #define __clear_user(to,n) \
247 __do_clear_user(to,n); \
250 #define clear_user(to,n) \
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); \
260 /* Returns: -EFAULT if exception before terminator, N if the entire
261 buffer filled, else strlen. */
263 extern long __strncpy_from_user (char *to, const char *from, long to_len);
265 #define strncpy_from_user(to,from,n) \
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)); \
274 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
275 extern unsigned long __strlen_user (const char *);
277 #define strlen_user(str) \
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); \
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
291 extern unsigned long __strnlen_user (const char *, long);
293 #define strnlen_user(str, len) \
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); \
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 */
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);
310 #ifdef GAS_HAS_LOCAL_TAGS
311 # define SEARCH_EXCEPTION_TABLE(regs) search_exception_tables(regs->cr_iip + ia64_psr(regs)->ri)
313 # define SEARCH_EXCEPTION_TABLE(regs) search_exception_tables(regs->cr_iip)
317 done_with_exception (struct pt_regs *regs)
319 const struct exception_table_entry *e;
320 e = SEARCH_EXCEPTION_TABLE(regs);
322 handle_exception(regs, e);
328 #endif /* _ASM_IA64_UACCESS_H */