6 #include <linux/sched.h>
7 #include <linux/errno.h>
8 #include <asm/processor.h>
11 #define VERIFY_WRITE 1
14 * The fs value determines whether argument validity checking should be
15 * performed or not. If get_fs() == USER_DS, checking is performed, with
16 * get_fs() == KERNEL_DS, checking is bypassed.
18 * For historical reasons, these macros are grossly misnamed.
20 * The fs/ds values are now the highest legal address in the "segment".
21 * This simplifies the checking in the routines below.
24 #define KERNEL_DS ((mm_segment_t) { ~0UL })
25 #define USER_DS ((mm_segment_t) { TASK_SIZE - 1 })
27 #define get_ds() (KERNEL_DS)
28 #define get_fs() (current->thread.fs)
29 #define set_fs(val) (current->thread.fs = (val))
31 #define segment_eq(a,b) ((a).seg == (b).seg)
33 #define __access_ok(addr,size) \
34 ((addr) <= current->thread.fs.seg \
35 && ((size) == 0 || (size) - 1 <= current->thread.fs.seg - (addr)))
37 #define access_ok(type, addr, size) __access_ok((unsigned long)(addr),(size))
39 extern inline int verify_area(int type, const void __user * addr, unsigned long size)
41 return access_ok(type, addr, size) ? 0 : -EFAULT;
46 * The exception table consists of pairs of addresses: the first is the
47 * address of an instruction that is allowed to fault, and the second is
48 * the address at which the program should continue. No registers are
49 * modified, so it is entirely up to the continuation code to figure out
52 * All the routines below use bits of fixup code that are out of line
53 * with the main instruction path. This means when everything is well,
54 * we don't even have to jump over them. Further, they do not intrude
55 * on our cache or tlb entries.
58 struct exception_table_entry
60 unsigned long insn, fixup;
64 * These are the main single-value transfer routines. They automatically
65 * use the right size if we just have the right pointer type.
67 * This gets kind of ugly. We want to return _two_ values in "get_user()"
68 * and yet we don't want to do any pointers, because that is too much
69 * of a performance impact. Thus we have a few rather ugly macros here,
70 * and hide all the ugliness from the user.
72 * The "__xxx" versions of the user access functions are versions that
73 * do not verify the address space, that must have been done previously
74 * with a separate "access_ok()" call (this is used when we do multiple
75 * accesses to the same area of user memory).
77 * As we use the same address space for kernel and user data on the
78 * PowerPC, we can just do these as direct assignments. (Of course, the
79 * exception handling means that it's no longer "just"...)
81 * The "user64" versions of the user access functions are versions that
82 * allow access of 64-bit data. The "get_user" functions do not
83 * properly handle 64-bit data because the value gets down cast to a long.
84 * The "put_user" functions already handle 64-bit data properly but we add
85 * "user64" versions for completeness
87 #define get_user(x,ptr) \
88 __get_user_check((x),(ptr),sizeof(*(ptr)))
89 #define get_user64(x,ptr) \
90 __get_user64_check((x),(ptr),sizeof(*(ptr)))
91 #define put_user(x,ptr) \
92 __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
93 #define put_user64(x,ptr) put_user(x,ptr)
95 #define __get_user(x,ptr) \
96 __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
97 #define __get_user64(x,ptr) \
98 __get_user64_nocheck((x),(ptr),sizeof(*(ptr)))
99 #define __put_user(x,ptr) \
100 __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
101 #define __put_user64(x,ptr) __put_user(x,ptr)
103 extern long __put_user_bad(void);
105 #define __put_user_nocheck(x,ptr,size) \
108 __put_user_size((x),(ptr),(size),__pu_err); \
112 #define __put_user_check(x,ptr,size) \
114 long __pu_err = -EFAULT; \
115 __typeof__(*(ptr)) *__pu_addr = (ptr); \
116 if (access_ok(VERIFY_WRITE,__pu_addr,size)) \
117 __put_user_size((x),__pu_addr,(size),__pu_err); \
121 #define __put_user_size(x,ptr,size,retval) \
126 __put_user_asm(x, ptr, retval, "stb"); \
129 __put_user_asm(x, ptr, retval, "sth"); \
132 __put_user_asm(x, ptr, retval, "stw"); \
135 __put_user_asm2(x, ptr, retval); \
143 * We don't tell gcc that we are accessing memory, but this is OK
144 * because we do not write to any memory gcc knows about, so there
145 * are no aliasing issues.
147 #define __put_user_asm(x, addr, err, op) \
148 __asm__ __volatile__( \
149 "1: "op" %1,0(%2)\n" \
151 ".section .fixup,\"ax\"\n" \
155 ".section __ex_table,\"a\"\n" \
160 : "r" (x), "b" (addr), "i" (-EFAULT), "0" (err))
162 #define __put_user_asm2(x, addr, err) \
163 __asm__ __volatile__( \
164 "1: stw %1,0(%2)\n" \
165 "2: stw %1+1,4(%2)\n" \
167 ".section .fixup,\"ax\"\n" \
171 ".section __ex_table,\"a\"\n" \
177 : "r" (x), "b" (addr), "i" (-EFAULT), "0" (err))
179 #define __get_user_nocheck(x, ptr, size) \
181 long __gu_err, __gu_val; \
182 __get_user_size(__gu_val, (ptr), (size), __gu_err); \
183 (x) = (__typeof__(*(ptr)))__gu_val; \
187 #define __get_user64_nocheck(x, ptr, size) \
190 long long __gu_val; \
191 __get_user_size64(__gu_val, (ptr), (size), __gu_err); \
192 (x) = (__typeof__(*(ptr)))__gu_val; \
196 #define __get_user_check(x, ptr, size) \
198 long __gu_err = -EFAULT, __gu_val = 0; \
199 const __typeof__(*(ptr)) *__gu_addr = (ptr); \
200 if (access_ok(VERIFY_READ, __gu_addr, (size))) \
201 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
202 (x) = (__typeof__(*(ptr)))__gu_val; \
206 #define __get_user64_check(x, ptr, size) \
208 long __gu_err = -EFAULT; \
209 long long __gu_val = 0; \
210 const __typeof__(*(ptr)) *__gu_addr = (ptr); \
211 if (access_ok(VERIFY_READ, __gu_addr, (size))) \
212 __get_user_size64(__gu_val, __gu_addr, (size), __gu_err); \
213 (x) = (__typeof__(*(ptr)))__gu_val; \
217 extern long __get_user_bad(void);
219 #define __get_user_size(x, ptr, size, retval) \
224 __get_user_asm(x, ptr, retval, "lbz"); \
227 __get_user_asm(x, ptr, retval, "lhz"); \
230 __get_user_asm(x, ptr, retval, "lwz"); \
233 x = __get_user_bad(); \
237 #define __get_user_size64(x, ptr, size, retval) \
242 __get_user_asm(x, ptr, retval, "lbz"); \
245 __get_user_asm(x, ptr, retval, "lhz"); \
248 __get_user_asm(x, ptr, retval, "lwz"); \
251 __get_user_asm2(x, ptr, retval); \
254 x = __get_user_bad(); \
258 #define __get_user_asm(x, addr, err, op) \
259 __asm__ __volatile__( \
260 "1: "op" %1,0(%2)\n" \
262 ".section .fixup,\"ax\"\n" \
267 ".section __ex_table,\"a\"\n" \
271 : "=r"(err), "=r"(x) \
272 : "b"(addr), "i"(-EFAULT), "0"(err))
274 #define __get_user_asm2(x, addr, err) \
275 __asm__ __volatile__( \
276 "1: lwz %1,0(%2)\n" \
277 "2: lwz %1+1,4(%2)\n" \
279 ".section .fixup,\"ax\"\n" \
285 ".section __ex_table,\"a\"\n" \
290 : "=r"(err), "=&r"(x) \
291 : "b"(addr), "i"(-EFAULT), "0"(err))
293 /* more complex routines */
295 extern int __copy_tofrom_user(void __user *to, const void __user *from,
298 extern inline unsigned long
299 copy_from_user(void *to, const void __user *from, unsigned long n)
303 if (access_ok(VERIFY_READ, from, n))
304 return __copy_tofrom_user((void __user *)to, from, n);
305 if ((unsigned long)from < TASK_SIZE) {
306 over = (unsigned long)from + n - TASK_SIZE;
307 return __copy_tofrom_user((void __user *)to, from, n - over) + over;
312 extern inline unsigned long
313 copy_to_user(void __user *to, const void *from, unsigned long n)
317 if (access_ok(VERIFY_WRITE, to, n))
318 return __copy_tofrom_user(to, (void __user *) from, n);
319 if ((unsigned long)to < TASK_SIZE) {
320 over = (unsigned long)to + n - TASK_SIZE;
321 return __copy_tofrom_user(to, (void __user *) from, n - over) + over;
326 #define __copy_from_user(to, from, size) \
327 __copy_tofrom_user((void __user *)(to), (from), (size))
328 #define __copy_to_user(to, from, size) \
329 __copy_tofrom_user((to), (void __user *)(from), (size))
330 #define __copy_to_user_inatomic __copy_to_user
331 #define __copy_from_user_inatomic __copy_from_user
333 extern unsigned long __clear_user(void __user *addr, unsigned long size);
335 extern inline unsigned long
336 clear_user(void __user *addr, unsigned long size)
338 if (access_ok(VERIFY_WRITE, addr, size))
339 return __clear_user(addr, size);
340 if ((unsigned long)addr < TASK_SIZE) {
341 unsigned long over = (unsigned long)addr + size - TASK_SIZE;
342 return __clear_user(addr, size - over) + over;
347 extern int __strncpy_from_user(char *dst, const char __user *src, long count);
350 strncpy_from_user(char *dst, const char __user *src, long count)
352 if (access_ok(VERIFY_READ, src, 1))
353 return __strncpy_from_user(dst, src, count);
358 * Return the size of a string (including the ending 0)
363 extern int __strnlen_user(const char __user *str, long len, unsigned long top);
366 * Returns the length of the string at str (including the null byte),
367 * or 0 if we hit a page we can't access,
368 * or something > len if we didn't find a null byte.
370 * The `top' parameter to __strnlen_user is to make sure that
371 * we can never overflow from the user area into kernel space.
373 extern __inline__ int strnlen_user(const char __user *str, long len)
375 unsigned long top = current->thread.fs.seg;
377 if ((unsigned long)str > top)
379 return __strnlen_user(str, len, top);
382 #define strlen_user(str) strnlen_user((str), 0x7ffffffe)
384 #endif /* __ASSEMBLY__ */
386 #endif /* _PPC_UACCESS_H */
387 #endif /* __KERNEL__ */