1 /* uaccess.h: userspace accessor functions
2 *
3 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12 #ifndef _ASM_UACCESS_H
13 #define _ASM_UACCESS_H
14
15 /*
16 * User space memory access functions
17 */
18 #include <linux/sched.h>
19 #include <linux/mm.h>
20 #include <asm/segment.h>
21 #include <asm/sections.h>
22
23 #define __ptr(x) ((unsigned long __force *)(x))
24
25 #define VERIFY_READ 0
26 #define VERIFY_WRITE 1
27
28 /*
29 * check that a range of addresses falls within the current address limit
30 */
___range_ok(unsigned long addr,unsigned long size)31 static inline int ___range_ok(unsigned long addr, unsigned long size)
32 {
33 #ifdef CONFIG_MMU
34 int flag = -EFAULT, tmp;
35
36 asm volatile (
37 " addcc %3,%2,%1,icc0 \n" /* set C-flag if addr+size>4GB */
38 " subcc.p %1,%4,gr0,icc1 \n" /* jump if addr+size>limit */
39 " bc icc0,#0,0f \n"
40 " bhi icc1,#0,0f \n"
41 " setlos #0,%0 \n" /* mark okay */
42 "0: \n"
43 : "=r"(flag), "=&r"(tmp)
44 : "r"(addr), "r"(size), "r"(get_addr_limit()), "0"(flag)
45 );
46
47 return flag;
48
49 #else
50
51 if (addr < memory_start ||
52 addr > memory_end ||
53 size > memory_end - memory_start ||
54 addr + size > memory_end)
55 return -EFAULT;
56
57 return 0;
58 #endif
59 }
60
61 #define __range_ok(addr,size) ___range_ok((unsigned long) (addr), (unsigned long) (size))
62
63 #define access_ok(type,addr,size) (__range_ok((void __user *)(addr), (size)) == 0)
64 #define __access_ok(addr,size) (__range_ok((addr), (size)) == 0)
65
66 /*
67 * The exception table consists of pairs of addresses: the first is the
68 * address of an instruction that is allowed to fault, and the second is
69 * the address at which the program should continue. No registers are
70 * modified, so it is entirely up to the continuation code to figure out
71 * what to do.
72 *
73 * All the routines below use bits of fixup code that are out of line
74 * with the main instruction path. This means when everything is well,
75 * we don't even have to jump over them. Further, they do not intrude
76 * on our cache or tlb entries.
77 */
78 struct exception_table_entry
79 {
80 unsigned long insn, fixup;
81 };
82
83 /* Returns 0 if exception not found and fixup otherwise. */
84 extern unsigned long search_exception_table(unsigned long);
85
86
87 /*
88 * These are the main single-value transfer routines. They automatically
89 * use the right size if we just have the right pointer type.
90 */
91 #define __put_user(x, ptr) \
92 ({ \
93 int __pu_err = 0; \
94 \
95 typeof(*(ptr)) __pu_val = (x); \
96 __chk_user_ptr(ptr); \
97 \
98 switch (sizeof (*(ptr))) { \
99 case 1: \
100 __put_user_asm(__pu_err, __pu_val, ptr, "b", "r"); \
101 break; \
102 case 2: \
103 __put_user_asm(__pu_err, __pu_val, ptr, "h", "r"); \
104 break; \
105 case 4: \
106 __put_user_asm(__pu_err, __pu_val, ptr, "", "r"); \
107 break; \
108 case 8: \
109 __put_user_asm(__pu_err, __pu_val, ptr, "d", "e"); \
110 break; \
111 default: \
112 __pu_err = __put_user_bad(); \
113 break; \
114 } \
115 __pu_err; \
116 })
117
118 #define put_user(x, ptr) \
119 ({ \
120 typeof(*(ptr)) __user *_p = (ptr); \
121 int _e; \
122 \
123 _e = __range_ok(_p, sizeof(*_p)); \
124 if (_e == 0) \
125 _e = __put_user((x), _p); \
126 _e; \
127 })
128
129 extern int __put_user_bad(void);
130
131 /*
132 * Tell gcc we read from memory instead of writing: this is because
133 * we do not write to any memory gcc knows about, so there are no
134 * aliasing issues.
135 */
136
137 #ifdef CONFIG_MMU
138
139 #define __put_user_asm(err,x,ptr,dsize,constraint) \
140 do { \
141 asm volatile("1: st"dsize"%I1 %2,%M1 \n" \
142 "2: \n" \
143 ".subsection 2 \n" \
144 "3: setlos %3,%0 \n" \
145 " bra 2b \n" \
146 ".previous \n" \
147 ".section __ex_table,\"a\" \n" \
148 " .balign 8 \n" \
149 " .long 1b,3b \n" \
150 ".previous" \
151 : "=r" (err) \
152 : "m" (*__ptr(ptr)), constraint (x), "i"(-EFAULT), "0"(err) \
153 : "memory"); \
154 } while (0)
155
156 #else
157
158 #define __put_user_asm(err,x,ptr,bwl,con) \
159 do { \
160 asm(" st"bwl"%I0 %1,%M0 \n" \
161 " membar \n" \
162 : \
163 : "m" (*__ptr(ptr)), con (x) \
164 : "memory"); \
165 } while (0)
166
167 #endif
168
169 /*****************************************************************************/
170 /*
171 *
172 */
173 #define __get_user(x, ptr) \
174 ({ \
175 int __gu_err = 0; \
176 __chk_user_ptr(ptr); \
177 \
178 switch (sizeof(*(ptr))) { \
179 case 1: { \
180 unsigned char __gu_val; \
181 __get_user_asm(__gu_err, __gu_val, ptr, "ub", "=r"); \
182 (x) = *(__force __typeof__(*(ptr)) *) &__gu_val; \
183 break; \
184 } \
185 case 2: { \
186 unsigned short __gu_val; \
187 __get_user_asm(__gu_err, __gu_val, ptr, "uh", "=r"); \
188 (x) = *(__force __typeof__(*(ptr)) *) &__gu_val; \
189 break; \
190 } \
191 case 4: { \
192 unsigned int __gu_val; \
193 __get_user_asm(__gu_err, __gu_val, ptr, "", "=r"); \
194 (x) = *(__force __typeof__(*(ptr)) *) &__gu_val; \
195 break; \
196 } \
197 case 8: { \
198 unsigned long long __gu_val; \
199 __get_user_asm(__gu_err, __gu_val, ptr, "d", "=e"); \
200 (x) = *(__force __typeof__(*(ptr)) *) &__gu_val; \
201 break; \
202 } \
203 default: \
204 __gu_err = __get_user_bad(); \
205 break; \
206 } \
207 __gu_err; \
208 })
209
210 #define get_user(x, ptr) \
211 ({ \
212 const typeof(*(ptr)) __user *_p = (ptr);\
213 int _e; \
214 \
215 _e = __range_ok(_p, sizeof(*_p)); \
216 if (likely(_e == 0)) \
217 _e = __get_user((x), _p); \
218 else \
219 (x) = (typeof(x)) 0; \
220 _e; \
221 })
222
223 extern int __get_user_bad(void);
224
225 #ifdef CONFIG_MMU
226
227 #define __get_user_asm(err,x,ptr,dtype,constraint) \
228 do { \
229 asm("1: ld"dtype"%I2 %M2,%1 \n" \
230 "2: \n" \
231 ".subsection 2 \n" \
232 "3: setlos %3,%0 \n" \
233 " setlos #0,%1 \n" \
234 " bra 2b \n" \
235 ".previous \n" \
236 ".section __ex_table,\"a\" \n" \
237 " .balign 8 \n" \
238 " .long 1b,3b \n" \
239 ".previous" \
240 : "=r" (err), constraint (x) \
241 : "m" (*__ptr(ptr)), "i"(-EFAULT), "0"(err) \
242 ); \
243 } while(0)
244
245 #else
246
247 #define __get_user_asm(err,x,ptr,bwl,con) \
248 asm(" ld"bwl"%I1 %M1,%0 \n" \
249 " membar \n" \
250 : con(x) \
251 : "m" (*__ptr(ptr)))
252
253 #endif
254
255 /*****************************************************************************/
256 /*
257 *
258 */
259 #define ____force(x) (__force void *)(void __user *)(x)
260 #ifdef CONFIG_MMU
261 extern long __memset_user(void *dst, unsigned long count);
262 extern long __memcpy_user(void *dst, const void *src, unsigned long count);
263
264 #define __clear_user(dst,count) __memset_user(____force(dst), (count))
265 #define __copy_from_user_inatomic(to, from, n) __memcpy_user((to), ____force(from), (n))
266 #define __copy_to_user_inatomic(to, from, n) __memcpy_user(____force(to), (from), (n))
267
268 #else
269
270 #define __clear_user(dst,count) (memset(____force(dst), 0, (count)), 0)
271 #define __copy_from_user_inatomic(to, from, n) (memcpy((to), ____force(from), (n)), 0)
272 #define __copy_to_user_inatomic(to, from, n) (memcpy(____force(to), (from), (n)), 0)
273
274 #endif
275
276 static inline unsigned long __must_check
clear_user(void __user * to,unsigned long n)277 clear_user(void __user *to, unsigned long n)
278 {
279 if (likely(__access_ok(to, n)))
280 n = __clear_user(to, n);
281 return n;
282 }
283
284 static inline unsigned long __must_check
__copy_to_user(void __user * to,const void * from,unsigned long n)285 __copy_to_user(void __user *to, const void *from, unsigned long n)
286 {
287 might_fault();
288 return __copy_to_user_inatomic(to, from, n);
289 }
290
291 static inline unsigned long
__copy_from_user(void * to,const void __user * from,unsigned long n)292 __copy_from_user(void *to, const void __user *from, unsigned long n)
293 {
294 might_fault();
295 return __copy_from_user_inatomic(to, from, n);
296 }
297
copy_from_user(void * to,const void __user * from,unsigned long n)298 static inline long copy_from_user(void *to, const void __user *from, unsigned long n)
299 {
300 unsigned long ret = n;
301
302 if (likely(__access_ok(from, n)))
303 ret = __copy_from_user(to, from, n);
304
305 if (unlikely(ret != 0))
306 memset(to + (n - ret), 0, ret);
307
308 return ret;
309 }
310
copy_to_user(void __user * to,const void * from,unsigned long n)311 static inline long copy_to_user(void __user *to, const void *from, unsigned long n)
312 {
313 return likely(__access_ok(to, n)) ? __copy_to_user(to, from, n) : n;
314 }
315
316 extern long strncpy_from_user(char *dst, const char __user *src, long count);
317 extern long strnlen_user(const char __user *src, long count);
318
319 #define strlen_user(str) strnlen_user(str, 32767)
320
321 extern unsigned long search_exception_table(unsigned long addr);
322
323 #endif /* _ASM_UACCESS_H */
324