1 #ifndef _PPC64_UACCESS_H
2 #define _PPC64_UACCESS_H
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
12 #include <linux/sched.h>
13 #include <linux/errno.h>
14 #include <asm/processor.h>
17 #define VERIFY_WRITE 1
20 * The fs value determines whether argument validity checking should be
21 * performed or not. If get_fs() == USER_DS, checking is performed, with
22 * get_fs() == KERNEL_DS, checking is bypassed.
24 * For historical reasons, these macros are grossly misnamed.
27 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
29 #define KERNEL_DS MAKE_MM_SEG(0UL)
30 #define USER_DS MAKE_MM_SEG(0xf000000000000000UL)
32 #define get_ds() (KERNEL_DS)
33 #define get_fs() (current->thread.fs)
34 #define set_fs(val) (current->thread.fs = (val))
36 #define segment_eq(a,b) ((a).seg == (b).seg)
39 * Use the alpha trick for checking ranges:
41 * Is a address valid? This does a straightforward calculation rather
45 * - "addr" doesn't have any high-bits set
46 * - AND "size" doesn't have any high-bits set
47 * - OR we are in kernel mode.
49 * We dont have to check for high bits in (addr+size) because the first
50 * two checks force the maximum result to be below the start of the
53 #define __access_ok(addr,size,segment) \
54 (((segment).seg & (addr | size )) == 0)
56 #define access_ok(type,addr,size) \
57 __access_ok(((__force unsigned long)(addr)),(size),get_fs())
59 /* this function will go away soon - use access_ok() instead */
60 static inline int __deprecated verify_area(int type, const void __user *addr, unsigned long size)
62 return access_ok(type,addr,size) ? 0 : -EFAULT;
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
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.
79 struct exception_table_entry
81 unsigned long insn, fixup;
84 /* Returns 0 if exception not found and fixup otherwise. */
85 extern unsigned long search_exception_table(unsigned long);
88 * These are the main single-value transfer routines. They automatically
89 * use the right size if we just have the right pointer type.
91 * This gets kind of ugly. We want to return _two_ values in "get_user()"
92 * and yet we don't want to do any pointers, because that is too much
93 * of a performance impact. Thus we have a few rather ugly macros here,
94 * and hide all the ugliness from the user.
96 * The "__xxx" versions of the user access functions are versions that
97 * do not verify the address space, that must have been done previously
98 * with a separate "access_ok()" call (this is used when we do multiple
99 * accesses to the same area of user memory).
101 * As we use the same address space for kernel and user data on the
102 * PowerPC, we can just do these as direct assignments. (Of course, the
103 * exception handling means that it's no longer "just"...)
105 #define get_user(x,ptr) \
106 __get_user_check((x),(ptr),sizeof(*(ptr)))
107 #define put_user(x,ptr) \
108 __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
110 #define __get_user(x,ptr) \
111 __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
112 #define __put_user(x,ptr) \
113 __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
115 #define __get_user_unaligned __get_user
116 #define __put_user_unaligned __put_user
118 extern long __put_user_bad(void);
120 #define __put_user_nocheck(x,ptr,size) \
124 __chk_user_ptr(ptr); \
125 __put_user_size((x),(ptr),(size),__pu_err,-EFAULT); \
129 #define __put_user_check(x,ptr,size) \
131 long __pu_err = -EFAULT; \
132 void __user *__pu_addr = (ptr); \
134 if (access_ok(VERIFY_WRITE,__pu_addr,size)) \
135 __put_user_size((x),__pu_addr,(size),__pu_err,-EFAULT); \
139 #define __put_user_size(x,ptr,size,retval,errret) \
143 case 1: __put_user_asm(x,ptr,retval,"stb",errret); break; \
144 case 2: __put_user_asm(x,ptr,retval,"sth",errret); break; \
145 case 4: __put_user_asm(x,ptr,retval,"stw",errret); break; \
146 case 8: __put_user_asm(x,ptr,retval,"std",errret); break; \
147 default: __put_user_bad(); \
152 * We don't tell gcc that we are accessing memory, but this is OK
153 * because we do not write to any memory gcc knows about, so there
154 * are no aliasing issues.
156 #define __put_user_asm(x, addr, err, op, errret) \
157 __asm__ __volatile__( \
158 "1: "op" %1,0(%2) # put_user\n" \
160 ".section .fixup,\"ax\"\n" \
164 ".section __ex_table,\"a\"\n" \
169 : "r"(x), "b"(addr), "i"(errret), "0"(err))
172 #define __get_user_nocheck(x,ptr,size) \
174 long __gu_err, __gu_val; \
176 __get_user_size(__gu_val,(ptr),(size),__gu_err,-EFAULT);\
177 (x) = (__typeof__(*(ptr)))__gu_val; \
181 #define __get_user_check(x,ptr,size) \
183 long __gu_err = -EFAULT, __gu_val = 0; \
184 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
186 if (access_ok(VERIFY_READ,__gu_addr,size)) \
187 __get_user_size(__gu_val,__gu_addr,(size),__gu_err,-EFAULT);\
188 (x) = (__typeof__(*(ptr)))__gu_val; \
192 extern long __get_user_bad(void);
194 #define __get_user_size(x,ptr,size,retval,errret) \
197 __chk_user_ptr(ptr); \
199 case 1: __get_user_asm(x,ptr,retval,"lbz",errret); break; \
200 case 2: __get_user_asm(x,ptr,retval,"lhz",errret); break; \
201 case 4: __get_user_asm(x,ptr,retval,"lwz",errret); break; \
202 case 8: __get_user_asm(x,ptr,retval,"ld",errret); break; \
203 default: (x) = __get_user_bad(); \
207 #define __get_user_asm(x, addr, err, op, errret) \
208 __asm__ __volatile__( \
209 "1: "op" %1,0(%2) # get_user\n" \
211 ".section .fixup,\"ax\"\n" \
216 ".section __ex_table,\"a\"\n" \
220 : "=r"(err), "=r"(x) \
221 : "b"(addr), "i"(errret), "0"(err))
223 /* more complex routines */
225 extern unsigned long __copy_tofrom_user(void __user *to, const void __user *from,
228 static inline unsigned long
229 __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
231 if (__builtin_constant_p(n)) {
236 __get_user_size(*(u8 *)to, from, 1, ret, 1);
239 __get_user_size(*(u16 *)to, from, 2, ret, 2);
242 __get_user_size(*(u32 *)to, from, 4, ret, 4);
245 __get_user_size(*(u64 *)to, from, 8, ret, 8);
249 return __copy_tofrom_user((__force void __user *) to, from, n);
252 static inline unsigned long
253 __copy_from_user(void *to, const void __user *from, unsigned long n)
256 return __copy_from_user_inatomic(to, from, n);
259 static inline unsigned long
260 __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
262 if (__builtin_constant_p(n)) {
267 __put_user_size(*(u8 *)from, (u8 __user *)to, 1, ret, 1);
270 __put_user_size(*(u16 *)from, (u16 __user *)to, 2, ret, 2);
273 __put_user_size(*(u32 *)from, (u32 __user *)to, 4, ret, 4);
276 __put_user_size(*(u64 *)from, (u64 __user *)to, 8, ret, 8);
280 return __copy_tofrom_user(to, (__force const void __user *) from, n);
283 static inline unsigned long
284 __copy_to_user(void __user *to, const void *from, unsigned long n)
287 return __copy_to_user_inatomic(to, from, n);
290 #define __copy_in_user(to, from, size) \
291 __copy_tofrom_user((to), (from), (size))
293 extern unsigned long copy_from_user(void *to, const void __user *from,
295 extern unsigned long copy_to_user(void __user *to, const void *from,
297 extern unsigned long copy_in_user(void __user *to, const void __user *from,
300 extern unsigned long __clear_user(void __user *addr, unsigned long size);
302 static inline unsigned long
303 clear_user(void __user *addr, unsigned long size)
306 if (likely(access_ok(VERIFY_WRITE, addr, size)))
307 size = __clear_user(addr, size);
311 extern int __strncpy_from_user(char *dst, const char __user *src, long count);
314 strncpy_from_user(char *dst, const char __user *src, long count)
317 if (likely(access_ok(VERIFY_READ, src, 1)))
318 return __strncpy_from_user(dst, src, count);
323 * Return the size of a string (including the ending 0)
327 extern int __strnlen_user(const char __user *str, long len);
330 * Returns the length of the string at str (including the null byte),
331 * or 0 if we hit a page we can't access,
332 * or something > len if we didn't find a null byte.
334 static inline int strnlen_user(const char __user *str, long len)
337 if (likely(access_ok(VERIFY_READ, str, 1)))
338 return __strnlen_user(str, len);
342 #define strlen_user(str) strnlen_user((str), 0x7ffffffe)
344 #endif /* __ASSEMBLY__ */
346 #endif /* _PPC64_UACCESS_H */