blob: 12d95947636b2e3b57066159b701fa96f126f470 [file] [log] [blame]
Sam Ravnborgf5e706a2008-07-17 21:55:51 -07001#ifndef _ASM_UACCESS_H
2#define _ASM_UACCESS_H
3
4/*
5 * User space memory access functions
6 */
7
8#ifdef __KERNEL__
David S. Millerfb340352009-12-10 23:05:23 -08009#include <linux/errno.h>
Sam Ravnborgf5e706a2008-07-17 21:55:51 -070010#include <linux/compiler.h>
Sam Ravnborgf5e706a2008-07-17 21:55:51 -070011#include <linux/string.h>
Alexey Dobriyan8abf9192009-08-13 10:05:43 +000012#include <linux/thread_info.h>
Sam Ravnborgf5e706a2008-07-17 21:55:51 -070013#include <asm/asi.h>
Sam Ravnborgf5e706a2008-07-17 21:55:51 -070014#include <asm/spitfire.h>
Arnd Bergmann5b17e1c2009-05-13 22:56:30 +000015#include <asm-generic/uaccess-unaligned.h>
Sam Ravnborgf5e706a2008-07-17 21:55:51 -070016#endif
17
18#ifndef __ASSEMBLY__
19
David Miller2c66f622012-05-26 11:14:27 -070020#include <asm/processor.h>
21
Sam Ravnborgf5e706a2008-07-17 21:55:51 -070022/*
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
Al Virodff933d2012-09-26 01:21:14 -040041#define get_fs() ((mm_segment_t){(current_thread_info()->current_ds)})
Sam Ravnborgf5e706a2008-07-17 21:55:51 -070042#define get_ds() (KERNEL_DS)
43
Michael S. Tsirkin71858202015-01-06 14:32:17 +020044#define segment_eq(a, b) ((a).seg == (b).seg)
Sam Ravnborgf5e706a2008-07-17 21:55:51 -070045
46#define set_fs(val) \
47do { \
Michael S. Tsirkin71858202015-01-06 14:32:17 +020048 current_thread_info()->current_ds = (val).seg; \
Sam Ravnborgf5e706a2008-07-17 21:55:51 -070049 __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
62/*
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.
73 */
74
75struct exception_table_entry {
76 unsigned int insn, fixup;
77};
78
Sam Ravnborgf05a6862014-05-16 23:25:50 +020079void __ret_efault(void);
80void __retl_efault(void);
Sam Ravnborgf5e706a2008-07-17 21:55:51 -070081
82/* Uh, these should become the main single-value transfer routines..
83 * They automatically use the right size if we just have the right
84 * pointer type..
85 *
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.
90 */
Michael S. Tsirkin71858202015-01-06 14:32:17 +020091#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)));\
95})
Sam Ravnborgf5e706a2008-07-17 21:55:51 -070096
Michael S. Tsirkin71858202015-01-06 14:32:17 +020097#define get_user(x, ptr) ({ \
98 unsigned long __gu_addr = (unsigned long)(ptr); \
99 __chk_user_ptr(ptr); \
100 __get_user_nocheck((x), __gu_addr, sizeof(*(ptr)), __typeof__(*(ptr)));\
101})
Sam Ravnborgf5e706a2008-07-17 21:55:51 -0700102
Michael S. Tsirkin71858202015-01-06 14:32:17 +0200103#define __put_user(x, ptr) put_user(x, ptr)
104#define __get_user(x, ptr) get_user(x, ptr)
Sam Ravnborgf5e706a2008-07-17 21:55:51 -0700105
106struct __large_struct { unsigned long buf[100]; };
107#define __m(x) ((struct __large_struct *)(x))
108
Michael S. Tsirkin71858202015-01-06 14:32:17 +0200109#define __put_user_nocheck(data, addr, size) ({ \
110 register int __pu_ret; \
111 switch (size) { \
112 case 1: \
113 __put_user_asm(data, b, addr, __pu_ret); \
114 break; \
115 case 2: \
116 __put_user_asm(data, h, addr, __pu_ret); \
117 break; \
118 case 4: \
119 __put_user_asm(data, w, addr, __pu_ret); \
120 break; \
121 case 8: \
122 __put_user_asm(data, x, addr, __pu_ret); \
123 break; \
124 default: \
125 __pu_ret = __put_user_bad(); \
126 break; \
127 } \
128 __pu_ret; \
129})
Sam Ravnborgf5e706a2008-07-17 21:55:51 -0700130
Michael S. Tsirkin71858202015-01-06 14:32:17 +0200131#define __put_user_asm(x, size, addr, ret) \
Sam Ravnborgf5e706a2008-07-17 21:55:51 -0700132__asm__ __volatile__( \
Michael S. Tsirkin71858202015-01-06 14:32:17 +0200133 "/* Put user asm, inline. */\n" \
134 "1:\t" "st"#size "a %1, [%2] %%asi\n\t" \
135 "clr %0\n" \
136 "2:\n\n\t" \
137 ".section .fixup,#alloc,#execinstr\n\t" \
138 ".align 4\n" \
139 "3:\n\t" \
140 "sethi %%hi(2b), %0\n\t" \
141 "jmpl %0 + %%lo(2b), %%g0\n\t" \
142 " mov %3, %0\n\n\t" \
143 ".previous\n\t" \
144 ".section __ex_table,\"a\"\n\t" \
145 ".align 4\n\t" \
146 ".word 1b, 3b\n\t" \
147 ".previous\n\n\t" \
148 : "=r" (ret) : "r" (x), "r" (__m(addr)), \
149 "i" (-EFAULT))
Sam Ravnborgf5e706a2008-07-17 21:55:51 -0700150
Sam Ravnborgf05a6862014-05-16 23:25:50 +0200151int __put_user_bad(void);
Sam Ravnborgf5e706a2008-07-17 21:55:51 -0700152
Michael S. Tsirkin71858202015-01-06 14:32:17 +0200153#define __get_user_nocheck(data, addr, size, type) ({ \
154 register int __gu_ret; \
155 register unsigned long __gu_val; \
156 switch (size) { \
157 case 1: \
158 __get_user_asm(__gu_val, ub, addr, __gu_ret); \
159 break; \
160 case 2: \
161 __get_user_asm(__gu_val, uh, addr, __gu_ret); \
162 break; \
163 case 4: \
164 __get_user_asm(__gu_val, uw, addr, __gu_ret); \
165 break; \
166 case 8: \
167 __get_user_asm(__gu_val, x, addr, __gu_ret); \
168 break; \
169 default: \
170 __gu_val = 0; \
171 __gu_ret = __get_user_bad(); \
172 break; \
173 } \
174 data = (__force type) __gu_val; \
175 __gu_ret; \
176})
Sam Ravnborgf5e706a2008-07-17 21:55:51 -0700177
Michael S. Tsirkin71858202015-01-06 14:32:17 +0200178#define __get_user_nocheck_ret(data, addr, size, type, retval) ({ \
179 register unsigned long __gu_val __asm__ ("l1"); \
180 switch (size) { \
181 case 1: \
182 __get_user_asm_ret(__gu_val, ub, addr, retval); \
183 break; \
184 case 2: \
185 __get_user_asm_ret(__gu_val, uh, addr, retval); \
186 break; \
187 case 4: \
188 __get_user_asm_ret(__gu_val, uw, addr, retval); \
189 break; \
190 case 8: \
191 __get_user_asm_ret(__gu_val, x, addr, retval); \
192 break; \
193 default: \
194 if (__get_user_bad()) \
195 return retval; \
196 } \
197 data = (__force type) __gu_val; \
198})
Sam Ravnborgf5e706a2008-07-17 21:55:51 -0700199
Michael S. Tsirkin71858202015-01-06 14:32:17 +0200200#define __get_user_asm(x, size, addr, ret) \
Sam Ravnborgf5e706a2008-07-17 21:55:51 -0700201__asm__ __volatile__( \
Michael S. Tsirkin71858202015-01-06 14:32:17 +0200202 "/* Get user asm, inline. */\n" \
203 "1:\t" "ld"#size "a [%2] %%asi, %1\n\t" \
204 "clr %0\n" \
205 "2:\n\n\t" \
206 ".section .fixup,#alloc,#execinstr\n\t" \
207 ".align 4\n" \
208 "3:\n\t" \
209 "sethi %%hi(2b), %0\n\t" \
210 "clr %1\n\t" \
211 "jmpl %0 + %%lo(2b), %%g0\n\t" \
212 " mov %3, %0\n\n\t" \
213 ".previous\n\t" \
214 ".section __ex_table,\"a\"\n\t" \
215 ".align 4\n\t" \
216 ".word 1b, 3b\n\n\t" \
217 ".previous\n\t" \
218 : "=r" (ret), "=r" (x) : "r" (__m(addr)), \
219 "i" (-EFAULT))
Sam Ravnborgf5e706a2008-07-17 21:55:51 -0700220
Michael S. Tsirkin71858202015-01-06 14:32:17 +0200221#define __get_user_asm_ret(x, size, addr, retval) \
Sam Ravnborgf5e706a2008-07-17 21:55:51 -0700222if (__builtin_constant_p(retval) && retval == -EFAULT) \
Michael S. Tsirkin71858202015-01-06 14:32:17 +0200223 __asm__ __volatile__( \
224 "/* Get user asm ret, inline. */\n" \
225 "1:\t" "ld"#size "a [%1] %%asi, %0\n\n\t" \
226 ".section __ex_table,\"a\"\n\t" \
227 ".align 4\n\t" \
228 ".word 1b,__ret_efault\n\n\t" \
229 ".previous\n\t" \
230 : "=r" (x) : "r" (__m(addr))); \
Sam Ravnborgf5e706a2008-07-17 21:55:51 -0700231else \
Michael S. Tsirkin71858202015-01-06 14:32:17 +0200232 __asm__ __volatile__( \
233 "/* Get user asm ret, inline. */\n" \
234 "1:\t" "ld"#size "a [%1] %%asi, %0\n\n\t" \
235 ".section .fixup,#alloc,#execinstr\n\t" \
236 ".align 4\n" \
237 "3:\n\t" \
238 "ret\n\t" \
239 " restore %%g0, %2, %%o0\n\n\t" \
240 ".previous\n\t" \
241 ".section __ex_table,\"a\"\n\t" \
242 ".align 4\n\t" \
243 ".word 1b, 3b\n\n\t" \
244 ".previous\n\t" \
245 : "=r" (x) : "r" (__m(addr)), "i" (retval))
Sam Ravnborgf5e706a2008-07-17 21:55:51 -0700246
Sam Ravnborgf05a6862014-05-16 23:25:50 +0200247int __get_user_bad(void);
Sam Ravnborgf5e706a2008-07-17 21:55:51 -0700248
Sam Ravnborgf05a6862014-05-16 23:25:50 +0200249unsigned long __must_check ___copy_from_user(void *to,
250 const void __user *from,
251 unsigned long size);
252unsigned long copy_from_user_fixup(void *to, const void __user *from,
253 unsigned long size);
Sam Ravnborgf5e706a2008-07-17 21:55:51 -0700254static inline unsigned long __must_check
255copy_from_user(void *to, const void __user *from, unsigned long size)
256{
David S. Miller4cb60662010-08-09 00:45:46 -0700257 unsigned long ret = ___copy_from_user(to, from, size);
Sam Ravnborgf5e706a2008-07-17 21:55:51 -0700258
David S. Miller4cb60662010-08-09 00:45:46 -0700259 if (unlikely(ret))
260 ret = copy_from_user_fixup(to, from, size);
261
Sam Ravnborgf5e706a2008-07-17 21:55:51 -0700262 return ret;
263}
264#define __copy_from_user copy_from_user
265
Sam Ravnborgf05a6862014-05-16 23:25:50 +0200266unsigned long __must_check ___copy_to_user(void __user *to,
267 const void *from,
268 unsigned long size);
269unsigned long copy_to_user_fixup(void __user *to, const void *from,
270 unsigned long size);
Sam Ravnborgf5e706a2008-07-17 21:55:51 -0700271static inline unsigned long __must_check
272copy_to_user(void __user *to, const void *from, unsigned long size)
273{
274 unsigned long ret = ___copy_to_user(to, from, size);
275
276 if (unlikely(ret))
277 ret = copy_to_user_fixup(to, from, size);
278 return ret;
279}
280#define __copy_to_user copy_to_user
281
Sam Ravnborgf05a6862014-05-16 23:25:50 +0200282unsigned long __must_check ___copy_in_user(void __user *to,
283 const void __user *from,
284 unsigned long size);
285unsigned long copy_in_user_fixup(void __user *to, void __user *from,
286 unsigned long size);
Sam Ravnborgf5e706a2008-07-17 21:55:51 -0700287static inline unsigned long __must_check
288copy_in_user(void __user *to, void __user *from, unsigned long size)
289{
290 unsigned long ret = ___copy_in_user(to, from, size);
291
292 if (unlikely(ret))
293 ret = copy_in_user_fixup(to, from, size);
294 return ret;
295}
296#define __copy_in_user copy_in_user
297
Sam Ravnborgf05a6862014-05-16 23:25:50 +0200298unsigned long __must_check __clear_user(void __user *, unsigned long);
Sam Ravnborgf5e706a2008-07-17 21:55:51 -0700299
300#define clear_user __clear_user
301
Sam Ravnborgf05a6862014-05-16 23:25:50 +0200302__must_check long strlen_user(const char __user *str);
303__must_check long strnlen_user(const char __user *str, long n);
Sam Ravnborgf5e706a2008-07-17 21:55:51 -0700304
Dave Kleikamp16932232013-12-16 15:01:00 -0600305#define __copy_to_user_inatomic __copy_to_user
306#define __copy_from_user_inatomic __copy_from_user
Sam Ravnborgf5e706a2008-07-17 21:55:51 -0700307
David S. Millerf88620b2012-10-10 17:19:32 -0700308struct pt_regs;
Sam Ravnborgf05a6862014-05-16 23:25:50 +0200309unsigned long compute_effective_address(struct pt_regs *,
310 unsigned int insn,
311 unsigned int rd);
David S. Millerf88620b2012-10-10 17:19:32 -0700312
Sam Ravnborgf5e706a2008-07-17 21:55:51 -0700313#endif /* __ASSEMBLY__ */
314
315#endif /* _ASM_UACCESS_H */