blob: c099aa339784f5416a025f28a4ca8cf982626dc2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: uaccess.h,v 1.35 2002/02/09 19:49:31 davem Exp $ */
2#ifndef _ASM_UACCESS_H
3#define _ASM_UACCESS_H
4
5/*
6 * User space memory access functions
7 */
8
9#ifdef __KERNEL__
10#include <linux/compiler.h>
11#include <linux/sched.h>
12#include <linux/string.h>
13#include <asm/a.out.h>
14#include <asm/asi.h>
15#include <asm/system.h>
16#include <asm/spitfire.h>
17#include <asm-generic/uaccess.h>
18#endif
19
20#ifndef __ASSEMBLY__
21
22/*
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.
27 *
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.
31 *
32 * "For historical reasons, these macros are grossly misnamed." -Linus
33 */
34
35#define KERNEL_DS ((mm_segment_t) { ASI_P })
36#define USER_DS ((mm_segment_t) { ASI_AIUS }) /* har har har */
37
38#define VERIFY_READ 0
39#define VERIFY_WRITE 1
40
41#define get_fs() ((mm_segment_t) { get_thread_current_ds() })
42#define get_ds() (KERNEL_DS)
43
44#define segment_eq(a,b) ((a).seg == (b).seg)
45
46#define set_fs(val) \
47do { \
48 set_thread_current_ds((val).seg); \
49 __asm__ __volatile__ ("wr %%g0, %0, %%asi" : : "r" ((val).seg)); \
50} while(0)
51
52static inline int __access_ok(const void __user * addr, unsigned long size)
53{
54 return 1;
55}
56
57static inline int access_ok(int type, const void __user * addr, unsigned long size)
58{
59 return 1;
60}
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
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
67 * what to do.
68 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
74
David S. Miller8cf14af2005-09-28 20:21:11 -070075struct exception_table_entry {
76 unsigned int insn, fixup;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079extern void __ret_efault(void);
80
81/* Uh, these should become the main single-value transfer routines..
82 * They automatically use the right size if we just have the right
83 * pointer type..
84 *
85 * This gets kind of ugly. We want to return _two_ values in "get_user()"
86 * and yet we don't want to do any pointers, because that is too much
87 * of a performance impact. Thus we have a few rather ugly macros here,
88 * and hide all the ugliness from the user.
89 */
90#define put_user(x,ptr) ({ \
91unsigned long __pu_addr = (unsigned long)(ptr); \
92__chk_user_ptr(ptr); \
93__put_user_nocheck((__typeof__(*(ptr)))(x),__pu_addr,sizeof(*(ptr))); })
94
95#define get_user(x,ptr) ({ \
96unsigned long __gu_addr = (unsigned long)(ptr); \
97__chk_user_ptr(ptr); \
98__get_user_nocheck((x),__gu_addr,sizeof(*(ptr)),__typeof__(*(ptr))); })
99
100#define __put_user(x,ptr) put_user(x,ptr)
101#define __get_user(x,ptr) get_user(x,ptr)
102
103struct __large_struct { unsigned long buf[100]; };
104#define __m(x) ((struct __large_struct *)(x))
105
106#define __put_user_nocheck(data,addr,size) ({ \
107register int __pu_ret; \
108switch (size) { \
109case 1: __put_user_asm(data,b,addr,__pu_ret); break; \
110case 2: __put_user_asm(data,h,addr,__pu_ret); break; \
111case 4: __put_user_asm(data,w,addr,__pu_ret); break; \
112case 8: __put_user_asm(data,x,addr,__pu_ret); break; \
113default: __pu_ret = __put_user_bad(); break; \
114} __pu_ret; })
115
116#define __put_user_nocheck_ret(data,addr,size,retval) ({ \
117register int __foo __asm__ ("l1"); \
118switch (size) { \
119case 1: __put_user_asm_ret(data,b,addr,retval,__foo); break; \
120case 2: __put_user_asm_ret(data,h,addr,retval,__foo); break; \
121case 4: __put_user_asm_ret(data,w,addr,retval,__foo); break; \
122case 8: __put_user_asm_ret(data,x,addr,retval,__foo); break; \
123default: if (__put_user_bad()) return retval; break; \
124} })
125
126#define __put_user_asm(x,size,addr,ret) \
127__asm__ __volatile__( \
128 "/* Put user asm, inline. */\n" \
129"1:\t" "st"#size "a %1, [%2] %%asi\n\t" \
130 "clr %0\n" \
131"2:\n\n\t" \
132 ".section .fixup,#alloc,#execinstr\n\t" \
133 ".align 4\n" \
134"3:\n\t" \
135 "b 2b\n\t" \
136 " mov %3, %0\n\n\t" \
137 ".previous\n\t" \
138 ".section __ex_table,#alloc\n\t" \
139 ".align 4\n\t" \
140 ".word 1b, 3b\n\t" \
141 ".previous\n\n\t" \
142 : "=r" (ret) : "r" (x), "r" (__m(addr)), \
143 "i" (-EFAULT))
144
145#define __put_user_asm_ret(x,size,addr,ret,foo) \
146if (__builtin_constant_p(ret) && ret == -EFAULT) \
147__asm__ __volatile__( \
148 "/* Put user asm ret, inline. */\n" \
149"1:\t" "st"#size "a %1, [%2] %%asi\n\n\t" \
150 ".section __ex_table,#alloc\n\t" \
151 ".align 4\n\t" \
152 ".word 1b, __ret_efault\n\n\t" \
153 ".previous\n\n\t" \
154 : "=r" (foo) : "r" (x), "r" (__m(addr))); \
155else \
156__asm__ __volatile__( \
157 "/* Put user asm ret, inline. */\n" \
158"1:\t" "st"#size "a %1, [%2] %%asi\n\n\t" \
159 ".section .fixup,#alloc,#execinstr\n\t" \
160 ".align 4\n" \
161"3:\n\t" \
162 "ret\n\t" \
163 " restore %%g0, %3, %%o0\n\n\t" \
164 ".previous\n\t" \
165 ".section __ex_table,#alloc\n\t" \
166 ".align 4\n\t" \
167 ".word 1b, 3b\n\n\t" \
168 ".previous\n\n\t" \
169 : "=r" (foo) : "r" (x), "r" (__m(addr)), \
170 "i" (ret))
171
172extern int __put_user_bad(void);
173
174#define __get_user_nocheck(data,addr,size,type) ({ \
175register int __gu_ret; \
176register unsigned long __gu_val; \
177switch (size) { \
178case 1: __get_user_asm(__gu_val,ub,addr,__gu_ret); break; \
179case 2: __get_user_asm(__gu_val,uh,addr,__gu_ret); break; \
180case 4: __get_user_asm(__gu_val,uw,addr,__gu_ret); break; \
181case 8: __get_user_asm(__gu_val,x,addr,__gu_ret); break; \
182default: __gu_val = 0; __gu_ret = __get_user_bad(); break; \
183} data = (type) __gu_val; __gu_ret; })
184
185#define __get_user_nocheck_ret(data,addr,size,type,retval) ({ \
186register unsigned long __gu_val __asm__ ("l1"); \
187switch (size) { \
188case 1: __get_user_asm_ret(__gu_val,ub,addr,retval); break; \
189case 2: __get_user_asm_ret(__gu_val,uh,addr,retval); break; \
190case 4: __get_user_asm_ret(__gu_val,uw,addr,retval); break; \
191case 8: __get_user_asm_ret(__gu_val,x,addr,retval); break; \
192default: if (__get_user_bad()) return retval; \
193} data = (type) __gu_val; })
194
195#define __get_user_asm(x,size,addr,ret) \
196__asm__ __volatile__( \
197 "/* Get user asm, inline. */\n" \
198"1:\t" "ld"#size "a [%2] %%asi, %1\n\t" \
199 "clr %0\n" \
200"2:\n\n\t" \
201 ".section .fixup,#alloc,#execinstr\n\t" \
202 ".align 4\n" \
203"3:\n\t" \
204 "clr %1\n\t" \
205 "b 2b\n\t" \
206 " mov %3, %0\n\n\t" \
207 ".previous\n\t" \
208 ".section __ex_table,#alloc\n\t" \
209 ".align 4\n\t" \
210 ".word 1b, 3b\n\n\t" \
211 ".previous\n\t" \
212 : "=r" (ret), "=r" (x) : "r" (__m(addr)), \
213 "i" (-EFAULT))
214
215#define __get_user_asm_ret(x,size,addr,retval) \
216if (__builtin_constant_p(retval) && retval == -EFAULT) \
217__asm__ __volatile__( \
218 "/* Get user asm ret, inline. */\n" \
219"1:\t" "ld"#size "a [%1] %%asi, %0\n\n\t" \
220 ".section __ex_table,#alloc\n\t" \
221 ".align 4\n\t" \
222 ".word 1b,__ret_efault\n\n\t" \
223 ".previous\n\t" \
224 : "=r" (x) : "r" (__m(addr))); \
225else \
226__asm__ __volatile__( \
227 "/* Get user asm ret, inline. */\n" \
228"1:\t" "ld"#size "a [%1] %%asi, %0\n\n\t" \
229 ".section .fixup,#alloc,#execinstr\n\t" \
230 ".align 4\n" \
231"3:\n\t" \
232 "ret\n\t" \
233 " restore %%g0, %2, %%o0\n\n\t" \
234 ".previous\n\t" \
235 ".section __ex_table,#alloc\n\t" \
236 ".align 4\n\t" \
237 ".word 1b, 3b\n\n\t" \
238 ".previous\n\t" \
239 : "=r" (x) : "r" (__m(addr)), "i" (retval))
240
241extern int __get_user_bad(void);
242
243extern unsigned long __must_check ___copy_from_user(void *to,
244 const void __user *from,
245 unsigned long size);
246extern unsigned long copy_from_user_fixup(void *to, const void __user *from,
247 unsigned long size);
248static inline unsigned long __must_check
249copy_from_user(void *to, const void __user *from, unsigned long size)
250{
251 unsigned long ret = ___copy_from_user(to, from, size);
252
253 if (ret)
254 ret = copy_from_user_fixup(to, from, size);
255 return ret;
256}
257#define __copy_from_user copy_from_user
258
259extern unsigned long __must_check ___copy_to_user(void __user *to,
260 const void *from,
261 unsigned long size);
262extern unsigned long copy_to_user_fixup(void __user *to, const void *from,
263 unsigned long size);
264static inline unsigned long __must_check
265copy_to_user(void __user *to, const void *from, unsigned long size)
266{
267 unsigned long ret = ___copy_to_user(to, from, size);
268
269 if (ret)
270 ret = copy_to_user_fixup(to, from, size);
271 return ret;
272}
273#define __copy_to_user copy_to_user
274
275extern unsigned long __must_check ___copy_in_user(void __user *to,
276 const void __user *from,
277 unsigned long size);
278extern unsigned long copy_in_user_fixup(void __user *to, void __user *from,
279 unsigned long size);
280static inline unsigned long __must_check
281copy_in_user(void __user *to, void __user *from, unsigned long size)
282{
283 unsigned long ret = ___copy_in_user(to, from, size);
284
285 if (ret)
286 ret = copy_in_user_fixup(to, from, size);
287 return ret;
288}
289#define __copy_in_user copy_in_user
290
291extern unsigned long __must_check __bzero_noasi(void __user *, unsigned long);
292
293static inline unsigned long __must_check
294__clear_user(void __user *addr, unsigned long size)
295{
296
297 return __bzero_noasi(addr, size);
298}
299
300#define clear_user __clear_user
301
302extern long __must_check __strncpy_from_user(char *dest, const char __user *src, long count);
303
304#define strncpy_from_user __strncpy_from_user
305
306extern long __strlen_user(const char __user *);
307extern long __strnlen_user(const char __user *, long len);
308
309#define strlen_user __strlen_user
310#define strnlen_user __strnlen_user
311#define __copy_to_user_inatomic __copy_to_user
312#define __copy_from_user_inatomic __copy_from_user
313
314#endif /* __ASSEMBLY__ */
315
316#endif /* _ASM_UACCESS_H */