blob: c181a60d868c7ee82e79db9ce1d57f9817a8d8d4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _PPC64_UACCESS_H
2#define _PPC64_UACCESS_H
3
4/*
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.
9 */
10
11#ifndef __ASSEMBLY__
12#include <linux/sched.h>
13#include <linux/errno.h>
14#include <asm/processor.h>
15
16#define VERIFY_READ 0
17#define VERIFY_WRITE 1
18
19/*
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.
23 *
24 * For historical reasons, these macros are grossly misnamed.
25 */
26
27#define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
28
29#define KERNEL_DS MAKE_MM_SEG(0UL)
30#define USER_DS MAKE_MM_SEG(0xf000000000000000UL)
31
32#define get_ds() (KERNEL_DS)
33#define get_fs() (current->thread.fs)
34#define set_fs(val) (current->thread.fs = (val))
35
36#define segment_eq(a,b) ((a).seg == (b).seg)
37
38/*
39 * Use the alpha trick for checking ranges:
40 *
41 * Is a address valid? This does a straightforward calculation rather
42 * than tests.
43 *
44 * Address valid if:
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.
48 *
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
51 * kernel region.
52 */
53#define __access_ok(addr,size,segment) \
54 (((segment).seg & (addr | size )) == 0)
55
56#define access_ok(type,addr,size) \
57 __access_ok(((__force unsigned long)(addr)),(size),get_fs())
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/*
60 * The exception table consists of pairs of addresses: the first is the
61 * address of an instruction that is allowed to fault, and the second is
62 * the address at which the program should continue. No registers are
63 * modified, so it is entirely up to the continuation code to figure out
64 * what to do.
65 *
66 * All the routines below use bits of fixup code that are out of line
67 * with the main instruction path. This means when everything is well,
68 * we don't even have to jump over them. Further, they do not intrude
69 * on our cache or tlb entries.
70 */
71
72struct exception_table_entry
73{
74 unsigned long insn, fixup;
75};
76
77/* Returns 0 if exception not found and fixup otherwise. */
78extern unsigned long search_exception_table(unsigned long);
79
80/*
81 * These are the main single-value transfer routines. They automatically
82 * use the right size if we just have the right pointer type.
83 *
84 * This gets kind of ugly. We want to return _two_ values in "get_user()"
85 * and yet we don't want to do any pointers, because that is too much
86 * of a performance impact. Thus we have a few rather ugly macros here,
87 * and hide all the ugliness from the user.
88 *
89 * The "__xxx" versions of the user access functions are versions that
90 * do not verify the address space, that must have been done previously
91 * with a separate "access_ok()" call (this is used when we do multiple
92 * accesses to the same area of user memory).
93 *
94 * As we use the same address space for kernel and user data on the
95 * PowerPC, we can just do these as direct assignments. (Of course, the
96 * exception handling means that it's no longer "just"...)
97 */
98#define get_user(x,ptr) \
99 __get_user_check((x),(ptr),sizeof(*(ptr)))
100#define put_user(x,ptr) \
101 __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
102
103#define __get_user(x,ptr) \
104 __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
105#define __put_user(x,ptr) \
106 __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
107
108#define __get_user_unaligned __get_user
109#define __put_user_unaligned __put_user
110
111extern long __put_user_bad(void);
112
113#define __put_user_nocheck(x,ptr,size) \
114({ \
115 long __pu_err; \
116 might_sleep(); \
117 __chk_user_ptr(ptr); \
118 __put_user_size((x),(ptr),(size),__pu_err,-EFAULT); \
119 __pu_err; \
120})
121
122#define __put_user_check(x,ptr,size) \
123({ \
124 long __pu_err = -EFAULT; \
125 void __user *__pu_addr = (ptr); \
126 might_sleep(); \
127 if (access_ok(VERIFY_WRITE,__pu_addr,size)) \
128 __put_user_size((x),__pu_addr,(size),__pu_err,-EFAULT); \
129 __pu_err; \
130})
131
132#define __put_user_size(x,ptr,size,retval,errret) \
133do { \
134 retval = 0; \
135 switch (size) { \
136 case 1: __put_user_asm(x,ptr,retval,"stb",errret); break; \
137 case 2: __put_user_asm(x,ptr,retval,"sth",errret); break; \
138 case 4: __put_user_asm(x,ptr,retval,"stw",errret); break; \
139 case 8: __put_user_asm(x,ptr,retval,"std",errret); break; \
140 default: __put_user_bad(); \
141 } \
142} while (0)
143
144/*
145 * We don't tell gcc that we are accessing memory, but this is OK
146 * because we do not write to any memory gcc knows about, so there
147 * are no aliasing issues.
148 */
149#define __put_user_asm(x, addr, err, op, errret) \
150 __asm__ __volatile__( \
151 "1: "op" %1,0(%2) # put_user\n" \
152 "2:\n" \
153 ".section .fixup,\"ax\"\n" \
154 "3: li %0,%3\n" \
155 " b 2b\n" \
156 ".previous\n" \
157 ".section __ex_table,\"a\"\n" \
158 " .align 3\n" \
159 " .llong 1b,3b\n" \
160 ".previous" \
161 : "=r"(err) \
162 : "r"(x), "b"(addr), "i"(errret), "0"(err))
163
164
165#define __get_user_nocheck(x,ptr,size) \
166({ \
167 long __gu_err, __gu_val; \
168 might_sleep(); \
169 __get_user_size(__gu_val,(ptr),(size),__gu_err,-EFAULT);\
170 (x) = (__typeof__(*(ptr)))__gu_val; \
171 __gu_err; \
172})
173
174#define __get_user_check(x,ptr,size) \
175({ \
176 long __gu_err = -EFAULT, __gu_val = 0; \
177 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
178 might_sleep(); \
179 if (access_ok(VERIFY_READ,__gu_addr,size)) \
180 __get_user_size(__gu_val,__gu_addr,(size),__gu_err,-EFAULT);\
181 (x) = (__typeof__(*(ptr)))__gu_val; \
182 __gu_err; \
183})
184
185extern long __get_user_bad(void);
186
187#define __get_user_size(x,ptr,size,retval,errret) \
188do { \
189 retval = 0; \
190 __chk_user_ptr(ptr); \
191 switch (size) { \
192 case 1: __get_user_asm(x,ptr,retval,"lbz",errret); break; \
193 case 2: __get_user_asm(x,ptr,retval,"lhz",errret); break; \
194 case 4: __get_user_asm(x,ptr,retval,"lwz",errret); break; \
195 case 8: __get_user_asm(x,ptr,retval,"ld",errret); break; \
196 default: (x) = __get_user_bad(); \
197 } \
198} while (0)
199
200#define __get_user_asm(x, addr, err, op, errret) \
201 __asm__ __volatile__( \
202 "1: "op" %1,0(%2) # get_user\n" \
203 "2:\n" \
204 ".section .fixup,\"ax\"\n" \
205 "3: li %0,%3\n" \
206 " li %1,0\n" \
207 " b 2b\n" \
208 ".previous\n" \
209 ".section __ex_table,\"a\"\n" \
210 " .align 3\n" \
211 " .llong 1b,3b\n" \
212 ".previous" \
213 : "=r"(err), "=r"(x) \
214 : "b"(addr), "i"(errret), "0"(err))
215
216/* more complex routines */
217
218extern unsigned long __copy_tofrom_user(void __user *to, const void __user *from,
219 unsigned long size);
220
221static inline unsigned long
222__copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
223{
224 if (__builtin_constant_p(n)) {
225 unsigned long ret;
226
227 switch (n) {
228 case 1:
229 __get_user_size(*(u8 *)to, from, 1, ret, 1);
230 return ret;
231 case 2:
232 __get_user_size(*(u16 *)to, from, 2, ret, 2);
233 return ret;
234 case 4:
235 __get_user_size(*(u32 *)to, from, 4, ret, 4);
236 return ret;
237 case 8:
238 __get_user_size(*(u64 *)to, from, 8, ret, 8);
239 return ret;
240 }
241 }
242 return __copy_tofrom_user((__force void __user *) to, from, n);
243}
244
245static inline unsigned long
246__copy_from_user(void *to, const void __user *from, unsigned long n)
247{
248 might_sleep();
249 return __copy_from_user_inatomic(to, from, n);
250}
251
252static inline unsigned long
253__copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
254{
255 if (__builtin_constant_p(n)) {
256 unsigned long ret;
257
258 switch (n) {
259 case 1:
260 __put_user_size(*(u8 *)from, (u8 __user *)to, 1, ret, 1);
261 return ret;
262 case 2:
263 __put_user_size(*(u16 *)from, (u16 __user *)to, 2, ret, 2);
264 return ret;
265 case 4:
266 __put_user_size(*(u32 *)from, (u32 __user *)to, 4, ret, 4);
267 return ret;
268 case 8:
269 __put_user_size(*(u64 *)from, (u64 __user *)to, 8, ret, 8);
270 return ret;
271 }
272 }
273 return __copy_tofrom_user(to, (__force const void __user *) from, n);
274}
275
276static inline unsigned long
277__copy_to_user(void __user *to, const void *from, unsigned long n)
278{
279 might_sleep();
280 return __copy_to_user_inatomic(to, from, n);
281}
282
283#define __copy_in_user(to, from, size) \
284 __copy_tofrom_user((to), (from), (size))
285
286extern unsigned long copy_from_user(void *to, const void __user *from,
287 unsigned long n);
288extern unsigned long copy_to_user(void __user *to, const void *from,
289 unsigned long n);
290extern unsigned long copy_in_user(void __user *to, const void __user *from,
291 unsigned long n);
292
293extern unsigned long __clear_user(void __user *addr, unsigned long size);
294
295static inline unsigned long
296clear_user(void __user *addr, unsigned long size)
297{
298 might_sleep();
299 if (likely(access_ok(VERIFY_WRITE, addr, size)))
300 size = __clear_user(addr, size);
301 return size;
302}
303
304extern int __strncpy_from_user(char *dst, const char __user *src, long count);
305
306static inline long
307strncpy_from_user(char *dst, const char __user *src, long count)
308{
309 might_sleep();
310 if (likely(access_ok(VERIFY_READ, src, 1)))
311 return __strncpy_from_user(dst, src, count);
312 return -EFAULT;
313}
314
315/*
316 * Return the size of a string (including the ending 0)
317 *
318 * Return 0 for error
319 */
320extern int __strnlen_user(const char __user *str, long len);
321
322/*
323 * Returns the length of the string at str (including the null byte),
324 * or 0 if we hit a page we can't access,
325 * or something > len if we didn't find a null byte.
326 */
327static inline int strnlen_user(const char __user *str, long len)
328{
329 might_sleep();
330 if (likely(access_ok(VERIFY_READ, str, 1)))
331 return __strnlen_user(str, len);
332 return 0;
333}
334
335#define strlen_user(str) strnlen_user((str), 0x7ffffffe)
336
337#endif /* __ASSEMBLY__ */
338
339#endif /* _PPC64_UACCESS_H */