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
19 extern long not_a_user_address;
20 #define check_user_ptr(x) \
21 (void) ({ void __user * __userptr = (__typeof__(*(x)) *)¬_a_user_address; __userptr; })
24 * The fs value determines whether argument validity checking should be
25 * performed or not. If get_fs() == USER_DS, checking is performed, with
26 * get_fs() == KERNEL_DS, checking is bypassed.
28 * For historical reasons, these macros are grossly misnamed.
31 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
33 #define KERNEL_DS MAKE_MM_SEG(0UL)
34 #define USER_DS MAKE_MM_SEG(0xf000000000000000UL)
36 #define get_ds() (KERNEL_DS)
37 #define get_fs() (current->thread.fs)
38 #define set_fs(val) (current->thread.fs = (val))
40 #define segment_eq(a,b) ((a).seg == (b).seg)
43 * Use the alpha trick for checking ranges:
45 * Is a address valid? This does a straightforward calculation rather
49 * - "addr" doesn't have any high-bits set
50 * - AND "size" doesn't have any high-bits set
51 * - OR we are in kernel mode.
53 * We dont have to check for high bits in (addr+size) because the first
54 * two checks force the maximum result to be below the start of the
57 #define __access_ok(addr,size,segment) \
58 (((segment).seg & (addr | size )) == 0)
60 #define access_ok(type,addr,size) \
61 __access_ok(((__force unsigned long)(addr)),(size),get_fs())
63 static inline int verify_area(int type, const void __user *addr, unsigned long size)
65 return access_ok(type,addr,size) ? 0 : -EFAULT;
70 * The exception table consists of pairs of addresses: the first is the
71 * address of an instruction that is allowed to fault, and the second is
72 * the address at which the program should continue. No registers are
73 * modified, so it is entirely up to the continuation code to figure out
76 * All the routines below use bits of fixup code that are out of line
77 * with the main instruction path. This means when everything is well,
78 * we don't even have to jump over them. Further, they do not intrude
79 * on our cache or tlb entries.
82 struct exception_table_entry
84 unsigned long insn, fixup;
87 /* Returns 0 if exception not found and fixup otherwise. */
88 extern unsigned long search_exception_table(unsigned long);
91 * These are the main single-value transfer routines. They automatically
92 * use the right size if we just have the right pointer type.
94 * This gets kind of ugly. We want to return _two_ values in "get_user()"
95 * and yet we don't want to do any pointers, because that is too much
96 * of a performance impact. Thus we have a few rather ugly macros here,
97 * and hide all the ugliness from the user.
99 * The "__xxx" versions of the user access functions are versions that
100 * do not verify the address space, that must have been done previously
101 * with a separate "access_ok()" call (this is used when we do multiple
102 * accesses to the same area of user memory).
104 * As we use the same address space for kernel and user data on the
105 * PowerPC, we can just do these as direct assignments. (Of course, the
106 * exception handling means that it's no longer "just"...)
108 #define get_user(x,ptr) \
109 __get_user_check((x),(ptr),sizeof(*(ptr)))
110 #define put_user(x,ptr) \
111 __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
113 #define __get_user(x,ptr) \
114 __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
115 #define __put_user(x,ptr) \
116 __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
118 extern long __put_user_bad(void);
120 #define __put_user_nocheck(x,ptr,size) \
123 check_user_ptr(ptr); \
124 __put_user_size((x),(ptr),(size),__pu_err,-EFAULT); \
128 #define __put_user_check(x,ptr,size) \
130 long __pu_err = -EFAULT; \
131 void __user *__pu_addr = (ptr); \
132 if (access_ok(VERIFY_WRITE,__pu_addr,size)) \
133 __put_user_size((x),__pu_addr,(size),__pu_err,-EFAULT); \
137 #define __put_user_size(x,ptr,size,retval,errret) \
142 case 1: __put_user_asm(x,ptr,retval,"stb",errret); break; \
143 case 2: __put_user_asm(x,ptr,retval,"sth",errret); break; \
144 case 4: __put_user_asm(x,ptr,retval,"stw",errret); break; \
145 case 8: __put_user_asm(x,ptr,retval,"std",errret); break; \
146 default: __put_user_bad(); \
151 * We don't tell gcc that we are accessing memory, but this is OK
152 * because we do not write to any memory gcc knows about, so there
153 * are no aliasing issues.
155 #define __put_user_asm(x, addr, err, op, errret) \
156 __asm__ __volatile__( \
157 "1: "op" %1,0(%2) # put_user\n" \
159 ".section .fixup,\"ax\"\n" \
163 ".section __ex_table,\"a\"\n" \
168 : "r"(x), "b"(addr), "i"(errret), "0"(err))
171 #define __get_user_nocheck(x,ptr,size) \
173 long __gu_err, __gu_val; \
174 __get_user_size(__gu_val,(ptr),(size),__gu_err,-EFAULT);\
175 (x) = (__typeof__(*(ptr)))__gu_val; \
179 #define __get_user_check(x,ptr,size) \
181 long __gu_err = -EFAULT, __gu_val = 0; \
182 const __typeof__(*(ptr)) *__gu_addr = (ptr); \
183 if (access_ok(VERIFY_READ,__gu_addr,size)) \
184 __get_user_size(__gu_val,__gu_addr,(size),__gu_err,-EFAULT);\
185 (x) = (__typeof__(*(ptr)))__gu_val; \
189 extern long __get_user_bad(void);
191 #define __get_user_size(x,ptr,size,retval,errret) \
195 check_user_ptr(ptr); \
197 case 1: __get_user_asm(x,ptr,retval,"lbz",errret); break; \
198 case 2: __get_user_asm(x,ptr,retval,"lhz",errret); break; \
199 case 4: __get_user_asm(x,ptr,retval,"lwz",errret); break; \
200 case 8: __get_user_asm(x,ptr,retval,"ld",errret); break; \
201 default: (x) = __get_user_bad(); \
205 #define __get_user_asm(x, addr, err, op, errret) \
206 __asm__ __volatile__( \
207 "1: "op" %1,0(%2) # get_user\n" \
209 ".section .fixup,\"ax\"\n" \
214 ".section __ex_table,\"a\"\n" \
218 : "=r"(err), "=r"(x) \
219 : "b"(addr), "i"(errret), "0"(err))
221 /* more complex routines */
223 extern unsigned long __copy_tofrom_user(void __user *to, const void __user *from,
226 static inline unsigned long
227 __copy_from_user(void *to, const void __user *from, unsigned long n)
230 if (__builtin_constant_p(n)) {
235 __get_user_size(*(u8 *)to, from, 1, ret, 1);
238 __get_user_size(*(u16 *)to, from, 2, ret, 2);
241 __get_user_size(*(u32 *)to, from, 4, ret, 4);
244 __get_user_size(*(u64 *)to, from, 8, ret, 8);
248 return __copy_tofrom_user((void __user *) to, from, n);
251 static inline unsigned long
252 __copy_to_user(void __user *to, const void *from, unsigned long n)
255 if (__builtin_constant_p(n)) {
260 __put_user_size(*(u8 *)from, (u8 __user *)to, 1, ret, 1);
263 __put_user_size(*(u16 *)from, (u16 __user *)to, 2, ret, 2);
266 __put_user_size(*(u32 *)from, (u32 __user *)to, 4, ret, 4);
269 __put_user_size(*(u64 *)from, (u64 __user *)to, 8, ret, 8);
273 return __copy_tofrom_user(to, (const void __user *) from, n);
276 #define __copy_in_user(to, from, size) \
277 __copy_tofrom_user((to), (from), (size))
279 static inline unsigned long
280 copy_from_user(void *to, const void __user *from, unsigned long n)
282 if (likely(access_ok(VERIFY_READ, from, n)))
283 n = __copy_from_user(to, from, n);
289 static inline unsigned long
290 copy_to_user(void __user *to, const void *from, unsigned long n)
292 if (likely(access_ok(VERIFY_WRITE, to, n)))
293 n = __copy_to_user(to, from, n);
297 static inline unsigned long
298 copy_in_user(void __user *to, const void __user *from, unsigned long n)
301 if (likely(access_ok(VERIFY_READ, from, n) &&
302 access_ok(VERIFY_WRITE, to, n)))
303 n =__copy_tofrom_user(to, from, n);
307 extern unsigned long __clear_user(void __user *addr, unsigned long size);
309 static inline unsigned long
310 clear_user(void __user *addr, unsigned long size)
313 if (likely(access_ok(VERIFY_WRITE, addr, size)))
314 size = __clear_user(addr, size);
318 extern int __strncpy_from_user(char *dst, const char __user *src, long count);
321 strncpy_from_user(char *dst, const char __user *src, long count)
324 if (likely(access_ok(VERIFY_READ, src, 1)))
325 return __strncpy_from_user(dst, src, count);
330 * Return the size of a string (including the ending 0)
334 extern int __strnlen_user(const char __user *str, long len);
337 * Returns the length of the string at str (including the null byte),
338 * or 0 if we hit a page we can't access,
339 * or something > len if we didn't find a null byte.
341 static inline int strnlen_user(const char __user *str, long len)
344 if (likely(access_ok(VERIFY_READ, str, 1)))
345 return __strnlen_user(str, len);
349 #define strlen_user(str) strnlen_user((str), 0x7ffffffe)
351 #endif /* __ASSEMBLY__ */
353 #endif /* _PPC64_UACCESS_H */