1 /* $Id: uaccess.h,v 1.35 2002/02/09 19:49:31 davem Exp $ */
6 * User space memory access functions
10 #include <linux/compiler.h>
11 #include <linux/sched.h>
12 #include <linux/string.h>
13 #include <asm/a.out.h>
15 #include <asm/system.h>
16 #include <asm/spitfire.h>
17 #include <asm-generic/uaccess.h>
23 * Sparc64 is segmented, though more like the M68K than the I386.
24 * We use the secondary ASI to address user memory, which references a
25 * completely different VM map, thus there is zero chance of the user
26 * doing something queer and tricking us into poking kernel memory.
28 * What is left here is basically what is needed for the other parts of
29 * the kernel that expect to be able to manipulate, erum, "segments".
30 * Or perhaps more properly, permissions.
32 * "For historical reasons, these macros are grossly misnamed." -Linus
35 #define KERNEL_DS ((mm_segment_t) { ASI_P })
36 #define USER_DS ((mm_segment_t) { ASI_AIUS }) /* har har har */
39 #define VERIFY_WRITE 1
41 #define get_fs() ((mm_segment_t) { get_thread_current_ds() })
42 #define get_ds() (KERNEL_DS)
44 #define segment_eq(a,b) ((a).seg == (b).seg)
48 set_thread_current_ds((val).seg); \
49 __asm__ __volatile__ ("wr %%g0, %0, %%asi" : : "r" ((val).seg)); \
52 static inline int __access_ok(const void __user * addr, unsigned long size)
57 static inline int access_ok(int type, const void __user * addr, unsigned long size)
63 * The exception table consists of pairs of addresses: the first is the
64 * address of an instruction that is allowed to fault, and the second is
65 * the address at which the program should continue. No registers are
66 * modified, so it is entirely up to the continuation code to figure out
69 * All the routines below use bits of fixup code that are out of line
70 * with the main instruction path. This means when everything is well,
71 * we don't even have to jump over them. Further, they do not intrude
72 * on our cache or tlb entries.
75 struct exception_table_entry {
76 unsigned int insn, fixup;
79 extern void __ret_efault(void);
80 extern void __retl_efault(void);
82 /* Uh, these should become the main single-value transfer routines..
83 * They automatically use the right size if we just have the right
86 * This gets kind of ugly. We want to return _two_ values in "get_user()"
87 * and yet we don't want to do any pointers, because that is too much
88 * of a performance impact. Thus we have a few rather ugly macros here,
89 * and hide all the ugliness from the user.
91 #define put_user(x,ptr) ({ \
92 unsigned long __pu_addr = (unsigned long)(ptr); \
93 __chk_user_ptr(ptr); \
94 __put_user_nocheck((__typeof__(*(ptr)))(x),__pu_addr,sizeof(*(ptr))); })
96 #define get_user(x,ptr) ({ \
97 unsigned long __gu_addr = (unsigned long)(ptr); \
98 __chk_user_ptr(ptr); \
99 __get_user_nocheck((x),__gu_addr,sizeof(*(ptr)),__typeof__(*(ptr))); })
101 #define __put_user(x,ptr) put_user(x,ptr)
102 #define __get_user(x,ptr) get_user(x,ptr)
104 struct __large_struct { unsigned long buf[100]; };
105 #define __m(x) ((struct __large_struct *)(x))
107 #define __put_user_nocheck(data,addr,size) ({ \
108 register int __pu_ret; \
110 case 1: __put_user_asm(data,b,addr,__pu_ret); break; \
111 case 2: __put_user_asm(data,h,addr,__pu_ret); break; \
112 case 4: __put_user_asm(data,w,addr,__pu_ret); break; \
113 case 8: __put_user_asm(data,x,addr,__pu_ret); break; \
114 default: __pu_ret = __put_user_bad(); break; \
117 #define __put_user_asm(x,size,addr,ret) \
118 __asm__ __volatile__( \
119 "/* Put user asm, inline. */\n" \
120 "1:\t" "st"#size "a %1, [%2] %%asi\n\t" \
123 ".section .fixup,#alloc,#execinstr\n\t" \
127 " mov %3, %0\n\n\t" \
129 ".section __ex_table,\"a\"\n\t" \
133 : "=r" (ret) : "r" (x), "r" (__m(addr)), \
136 extern int __put_user_bad(void);
138 #define __get_user_nocheck(data,addr,size,type) ({ \
139 register int __gu_ret; \
140 register unsigned long __gu_val; \
142 case 1: __get_user_asm(__gu_val,ub,addr,__gu_ret); break; \
143 case 2: __get_user_asm(__gu_val,uh,addr,__gu_ret); break; \
144 case 4: __get_user_asm(__gu_val,uw,addr,__gu_ret); break; \
145 case 8: __get_user_asm(__gu_val,x,addr,__gu_ret); break; \
146 default: __gu_val = 0; __gu_ret = __get_user_bad(); break; \
147 } data = (type) __gu_val; __gu_ret; })
149 #define __get_user_nocheck_ret(data,addr,size,type,retval) ({ \
150 register unsigned long __gu_val __asm__ ("l1"); \
152 case 1: __get_user_asm_ret(__gu_val,ub,addr,retval); break; \
153 case 2: __get_user_asm_ret(__gu_val,uh,addr,retval); break; \
154 case 4: __get_user_asm_ret(__gu_val,uw,addr,retval); break; \
155 case 8: __get_user_asm_ret(__gu_val,x,addr,retval); break; \
156 default: if (__get_user_bad()) return retval; \
157 } data = (type) __gu_val; })
159 #define __get_user_asm(x,size,addr,ret) \
160 __asm__ __volatile__( \
161 "/* Get user asm, inline. */\n" \
162 "1:\t" "ld"#size "a [%2] %%asi, %1\n\t" \
165 ".section .fixup,#alloc,#execinstr\n\t" \
170 " mov %3, %0\n\n\t" \
172 ".section __ex_table,\"a\"\n\t" \
174 ".word 1b, 3b\n\n\t" \
176 : "=r" (ret), "=r" (x) : "r" (__m(addr)), \
179 #define __get_user_asm_ret(x,size,addr,retval) \
180 if (__builtin_constant_p(retval) && retval == -EFAULT) \
181 __asm__ __volatile__( \
182 "/* Get user asm ret, inline. */\n" \
183 "1:\t" "ld"#size "a [%1] %%asi, %0\n\n\t" \
184 ".section __ex_table,\"a\"\n\t" \
186 ".word 1b,__ret_efault\n\n\t" \
188 : "=r" (x) : "r" (__m(addr))); \
190 __asm__ __volatile__( \
191 "/* Get user asm ret, inline. */\n" \
192 "1:\t" "ld"#size "a [%1] %%asi, %0\n\n\t" \
193 ".section .fixup,#alloc,#execinstr\n\t" \
197 " restore %%g0, %2, %%o0\n\n\t" \
199 ".section __ex_table,\"a\"\n\t" \
201 ".word 1b, 3b\n\n\t" \
203 : "=r" (x) : "r" (__m(addr)), "i" (retval))
205 extern int __get_user_bad(void);
207 extern unsigned long __must_check ___copy_from_user(void *to,
208 const void __user *from,
210 extern unsigned long copy_from_user_fixup(void *to, const void __user *from,
212 static inline unsigned long __must_check
213 copy_from_user(void *to, const void __user *from, unsigned long size)
215 unsigned long ret = ___copy_from_user(to, from, size);
218 ret = copy_from_user_fixup(to, from, size);
221 #define __copy_from_user copy_from_user
223 extern unsigned long __must_check ___copy_to_user(void __user *to,
226 extern unsigned long copy_to_user_fixup(void __user *to, const void *from,
228 static inline unsigned long __must_check
229 copy_to_user(void __user *to, const void *from, unsigned long size)
231 unsigned long ret = ___copy_to_user(to, from, size);
234 ret = copy_to_user_fixup(to, from, size);
237 #define __copy_to_user copy_to_user
239 extern unsigned long __must_check ___copy_in_user(void __user *to,
240 const void __user *from,
242 extern unsigned long copy_in_user_fixup(void __user *to, void __user *from,
244 static inline unsigned long __must_check
245 copy_in_user(void __user *to, void __user *from, unsigned long size)
247 unsigned long ret = ___copy_in_user(to, from, size);
250 ret = copy_in_user_fixup(to, from, size);
253 #define __copy_in_user copy_in_user
255 extern unsigned long __must_check __clear_user(void __user *, unsigned long);
257 #define clear_user __clear_user
259 extern long __must_check __strncpy_from_user(char *dest, const char __user *src, long count);
261 #define strncpy_from_user __strncpy_from_user
263 extern long __strlen_user(const char __user *);
264 extern long __strnlen_user(const char __user *, long len);
266 #define strlen_user __strlen_user
267 #define strnlen_user __strnlen_user
268 #define __copy_to_user_inatomic __copy_to_user
269 #define __copy_from_user_inatomic __copy_from_user
271 #endif /* __ASSEMBLY__ */
273 #endif /* _ASM_UACCESS_H */