Import 2.1.118
[davej-history.git] / include / asm-ppc / uaccess.h
blob65ca165b7ff2d2a019094d43d344743b621a6080
1 #ifndef _PPC_UACCESS_H
2 #define _PPC_UACCESS_H
4 #ifndef __ASSEMBLY__
5 #include <linux/sched.h>
6 #include <linux/errno.h>
7 #include <asm/processor.h>
9 #define VERIFY_READ 0
10 #define VERIFY_WRITE 1
13 * The fs value determines whether argument validity checking should be
14 * performed or not. If get_fs() == USER_DS, checking is performed, with
15 * get_fs() == KERNEL_DS, checking is bypassed.
17 * For historical reasons, these macros are grossly misnamed.
20 #define KERNEL_DS ((mm_segment_t) { 0 })
21 #define USER_DS ((mm_segment_t) { 1 })
23 #define get_ds() (KERNEL_DS)
24 #define get_fs() (current->tss.fs)
25 #define set_fs(val) (current->tss.fs = (val))
27 #define segment_eq(a,b) ((a).seg == (b).seg)
29 #define __kernel_ok (segment_eq(get_fs(), KERNEL_DS))
30 #define __user_ok(addr,size) (((size) <= 0x80000000)&&((addr) <= 0x80000000-(size)))
31 #define __access_ok(addr,size) (__kernel_ok || __user_ok((addr),(size)))
32 #define access_ok(type,addr,size) __access_ok((unsigned long)(addr),(size))
34 extern inline int verify_area(int type, const void * addr, unsigned long size)
36 return access_ok(type,addr,size) ? 0 : -EFAULT;
41 * The exception table consists of pairs of addresses: the first is the
42 * address of an instruction that is allowed to fault, and the second is
43 * the address at which the program should continue. No registers are
44 * modified, so it is entirely up to the continuation code to figure out
45 * what to do.
47 * All the routines below use bits of fixup code that are out of line
48 * with the main instruction path. This means when everything is well,
49 * we don't even have to jump over them. Further, they do not intrude
50 * on our cache or tlb entries.
53 struct exception_table_entry
55 unsigned long insn, fixup;
58 /* Returns 0 if exception not found and fixup otherwise. */
59 extern unsigned long search_exception_table(unsigned long);
63 * These are the main single-value transfer routines. They automatically
64 * use the right size if we just have the right pointer type.
66 * This gets kind of ugly. We want to return _two_ values in "get_user()"
67 * and yet we don't want to do any pointers, because that is too much
68 * of a performance impact. Thus we have a few rather ugly macros here,
69 * and hide all the uglyness from the user.
71 * The "__xxx" versions of the user access functions are versions that
72 * do not verify the address space, that must have been done previously
73 * with a separate "access_ok()" call (this is used when we do multiple
74 * accesses to the same area of user memory).
76 * As we use the same address space for kernel and user data on the
77 * PowerPC, we can just do these as direct assignments. (Of course, the
78 * exception handling means that it's no longer "just"...)
80 #define get_user(x,ptr) \
81 __get_user_check((x),(ptr),sizeof(*(ptr)))
82 #define put_user(x,ptr) \
83 __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
85 #define __get_user(x,ptr) \
86 __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
87 #define __put_user(x,ptr) \
88 __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
91 * The "xxx_ret" versions return constant specified in third argument, if
92 * something bad happens. These macros can be optimized for the
93 * case of just returning from the function xxx_ret is used.
96 #define put_user_ret(x,ptr,ret) ({ \
97 if (put_user(x,ptr)) return ret; })
99 #define get_user_ret(x,ptr,ret) ({ \
100 if (get_user(x,ptr)) return ret; })
102 #define __put_user_ret(x,ptr,ret) ({ \
103 if (__put_user(x,ptr)) return ret; })
105 #define __get_user_ret(x,ptr,ret) ({ \
106 if (__get_user(x,ptr)) return ret; })
109 extern long __put_user_bad(void);
111 #define __put_user_nocheck(x,ptr,size) \
112 ({ \
113 long __pu_err; \
114 __put_user_size((x),(ptr),(size),__pu_err); \
115 __pu_err; \
118 #define __put_user_check(x,ptr,size) \
119 ({ \
120 long __pu_err = -EFAULT; \
121 __typeof__(*(ptr)) *__pu_addr = (ptr); \
122 if (access_ok(VERIFY_WRITE,__pu_addr,size)) \
123 __put_user_size((x),__pu_addr,(size),__pu_err); \
124 __pu_err; \
127 #define __put_user_size(x,ptr,size,retval) \
128 do { \
129 retval = 0; \
130 switch (size) { \
131 case 1: __put_user_asm(x,ptr,retval,"stb"); break; \
132 case 2: __put_user_asm(x,ptr,retval,"sth"); break; \
133 case 4: __put_user_asm(x,ptr,retval,"stw"); break; \
134 default: __put_user_bad(); \
136 } while (0)
138 struct __large_struct { unsigned long buf[100]; };
139 #define __m(x) (*(struct __large_struct *)(x))
142 * We don't tell gcc that we are accessing memory, but this is OK
143 * because we do not write to any memory gcc knows about, so there
144 * are no aliasing issues.
146 #define __put_user_asm(x, addr, err, op) \
147 __asm__ __volatile__( \
148 "1: "op" %1,0(%2)\n" \
149 "2:\n" \
150 ".section .fixup,\"ax\"\n" \
151 "3: li %0,%3\n" \
152 " b 2b\n" \
153 ".section __ex_table,\"a\"\n" \
154 " .align 2\n" \
155 " .long 1b,3b\n" \
156 ".text" \
157 : "=r"(err) \
158 : "r"(x), "b"(addr), "i"(-EFAULT), "0"(err))
161 #define __get_user_nocheck(x,ptr,size) \
162 ({ \
163 long __gu_err, __gu_val; \
164 __get_user_size(__gu_val,(ptr),(size),__gu_err); \
165 (x) = (__typeof__(*(ptr)))__gu_val; \
166 __gu_err; \
169 #define __get_user_check(x,ptr,size) \
170 ({ \
171 long __gu_err = -EFAULT, __gu_val = 0; \
172 const __typeof__(*(ptr)) *__gu_addr = (ptr); \
173 if (access_ok(VERIFY_READ,__gu_addr,size)) \
174 __get_user_size(__gu_val,__gu_addr,(size),__gu_err); \
175 (x) = (__typeof__(*(ptr)))__gu_val; \
176 __gu_err; \
179 extern long __get_user_bad(void);
181 #define __get_user_size(x,ptr,size,retval) \
182 do { \
183 retval = 0; \
184 switch (size) { \
185 case 1: __get_user_asm(x,ptr,retval,"lbz"); break; \
186 case 2: __get_user_asm(x,ptr,retval,"lhz"); break; \
187 case 4: __get_user_asm(x,ptr,retval,"lwz"); break; \
188 default: (x) = __get_user_bad(); \
190 } while (0)
192 #define __get_user_asm(x, addr, err, op) \
193 __asm__ __volatile__( \
194 "1: "op" %1,0(%2)\n" \
195 "2:\n" \
196 ".section .fixup,\"ax\"\n" \
197 "3: li %0,%3\n" \
198 " li %1,0\n" \
199 " b 2b\n" \
200 ".section __ex_table,\"a\"\n" \
201 " .align 2\n" \
202 " .long 1b,3b\n" \
203 ".text" \
204 : "=r"(err), "=r"(x) \
205 : "b"(addr), "i"(-EFAULT), "0"(err))
207 /* more complex routines */
209 extern int __copy_tofrom_user(void *to, const void *from, unsigned long size);
211 extern inline unsigned long
212 copy_from_user(void *to, const void *from, unsigned long n)
214 unsigned long res = n;
215 if (access_ok(VERIFY_READ, from, n)) {
216 res = __copy_tofrom_user(to, from, n);
217 if (res) memset((char *)to + n - res, 0, res);
219 return res;
222 extern inline unsigned long
223 copy_to_user(void *to, const void *from, unsigned long n)
225 if (access_ok(VERIFY_WRITE, to, n))
226 return __copy_tofrom_user(to, from, n);
227 return n;
230 #define __copy_from_user(to, from, size) \
231 __copy_tofrom_user((to), (from), (size))
232 #define __copy_to_user(to, from, size) \
233 __copy_tofrom_user((to), (from), (size))
235 extern unsigned long __clear_user(void *addr, unsigned long size);
237 extern inline unsigned long
238 clear_user(void *addr, unsigned long size)
240 if (access_ok(VERIFY_WRITE, addr, size))
241 return __clear_user(addr, size);
242 return size? -EFAULT: 0;
245 extern int __strncpy_from_user(char *dst, const char *src, long count);
247 extern inline long
248 strncpy_from_user(char *dst, const char *src, long count)
250 if (access_ok(VERIFY_READ, src, 1))
251 return __strncpy_from_user(dst, src, count);
252 return -EFAULT;
256 * Return the size of a string (including the ending 0)
258 * Return 0 for error
261 extern long strlen_user(const char *);
263 #endif /* __ASSEMBLY__ */
265 #endif /* _PPC_UACCESS_H */