- pre2
[davej-history.git] / include / asm-ppc / uaccess.h
blobfcb22706b1e36c70c594b2eee17cfd72774ab2e2
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->thread.fs)
25 #define set_fs(val) (current->thread.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) <= TASK_SIZE)&&((addr) <= TASK_SIZE-(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)))
90 extern long __put_user_bad(void);
92 #define __put_user_nocheck(x,ptr,size) \
93 ({ \
94 long __pu_err; \
95 __put_user_size((x),(ptr),(size),__pu_err); \
96 __pu_err; \
99 #define __put_user_check(x,ptr,size) \
100 ({ \
101 long __pu_err = -EFAULT; \
102 __typeof__(*(ptr)) *__pu_addr = (ptr); \
103 if (access_ok(VERIFY_WRITE,__pu_addr,size)) \
104 __put_user_size((x),__pu_addr,(size),__pu_err); \
105 __pu_err; \
108 #define __put_user_size(x,ptr,size,retval) \
109 do { \
110 retval = 0; \
111 switch (size) { \
112 case 1: __put_user_asm(x,ptr,retval,"stb"); break; \
113 case 2: __put_user_asm(x,ptr,retval,"sth"); break; \
114 case 4: __put_user_asm(x,ptr,retval,"stw"); break; \
115 default: __put_user_bad(); \
117 } while (0)
119 struct __large_struct { unsigned long buf[100]; };
120 #define __m(x) (*(struct __large_struct *)(x))
123 * We don't tell gcc that we are accessing memory, but this is OK
124 * because we do not write to any memory gcc knows about, so there
125 * are no aliasing issues.
127 #define __put_user_asm(x, addr, err, op) \
128 __asm__ __volatile__( \
129 "1: "op" %1,0(%2)\n" \
130 "2:\n" \
131 ".section .fixup,\"ax\"\n" \
132 "3: li %0,%3\n" \
133 " b 2b\n" \
134 ".section __ex_table,\"a\"\n" \
135 " .align 2\n" \
136 " .long 1b,3b\n" \
137 ".text" \
138 : "=r"(err) \
139 : "r"(x), "b"(addr), "i"(-EFAULT), "0"(err))
142 #define __get_user_nocheck(x,ptr,size) \
143 ({ \
144 long __gu_err, __gu_val; \
145 __get_user_size(__gu_val,(ptr),(size),__gu_err); \
146 (x) = (__typeof__(*(ptr)))__gu_val; \
147 __gu_err; \
150 #define __get_user_check(x,ptr,size) \
151 ({ \
152 long __gu_err = -EFAULT, __gu_val = 0; \
153 const __typeof__(*(ptr)) *__gu_addr = (ptr); \
154 if (access_ok(VERIFY_READ,__gu_addr,size)) \
155 __get_user_size(__gu_val,__gu_addr,(size),__gu_err); \
156 (x) = (__typeof__(*(ptr)))__gu_val; \
157 __gu_err; \
160 extern long __get_user_bad(void);
162 #define __get_user_size(x,ptr,size,retval) \
163 do { \
164 retval = 0; \
165 switch (size) { \
166 case 1: __get_user_asm(x,ptr,retval,"lbz"); break; \
167 case 2: __get_user_asm(x,ptr,retval,"lhz"); break; \
168 case 4: __get_user_asm(x,ptr,retval,"lwz"); break; \
169 default: (x) = __get_user_bad(); \
171 } while (0)
173 #define __get_user_asm(x, addr, err, op) \
174 __asm__ __volatile__( \
175 "1: "op" %1,0(%2)\n" \
176 "2:\n" \
177 ".section .fixup,\"ax\"\n" \
178 "3: li %0,%3\n" \
179 " li %1,0\n" \
180 " b 2b\n" \
181 ".section __ex_table,\"a\"\n" \
182 " .align 2\n" \
183 " .long 1b,3b\n" \
184 ".text" \
185 : "=r"(err), "=r"(x) \
186 : "b"(addr), "i"(-EFAULT), "0"(err))
188 /* more complex routines */
190 extern int __copy_tofrom_user(void *to, const void *from, unsigned long size);
192 extern inline unsigned long
193 copy_from_user(void *to, const void *from, unsigned long n)
195 unsigned long over;
197 if (access_ok(VERIFY_READ, from, n))
198 return __copy_tofrom_user(to, from, n);
199 if ((unsigned long)from < TASK_SIZE) {
200 over = (unsigned long)from + n - TASK_SIZE;
201 return __copy_tofrom_user(to, from, n - over) + over;
203 return n;
206 extern inline unsigned long
207 copy_to_user(void *to, const void *from, unsigned long n)
209 unsigned long over;
211 if (access_ok(VERIFY_WRITE, to, n))
212 return __copy_tofrom_user(to, from, n);
213 if ((unsigned long)to < TASK_SIZE) {
214 over = (unsigned long)to + n - TASK_SIZE;
215 return __copy_tofrom_user(to, from, n - over) + over;
217 return n;
220 #define __copy_from_user(to, from, size) \
221 __copy_tofrom_user((to), (from), (size))
222 #define __copy_to_user(to, from, size) \
223 __copy_tofrom_user((to), (from), (size))
225 extern unsigned long __clear_user(void *addr, unsigned long size);
227 extern inline unsigned long
228 clear_user(void *addr, unsigned long size)
230 if (access_ok(VERIFY_WRITE, addr, size))
231 return __clear_user(addr, size);
232 return size? -EFAULT: 0;
235 extern int __strncpy_from_user(char *dst, const char *src, long count);
237 extern inline long
238 strncpy_from_user(char *dst, const char *src, long count)
240 if (access_ok(VERIFY_READ, src, 1))
241 return __strncpy_from_user(dst, src, count);
242 return -EFAULT;
246 * Return the size of a string (including the ending 0)
248 * Return 0 for error
251 extern int __strnlen_user(const char *str, long len, unsigned long top);
254 * Returns the length of the string at str (including the null byte),
255 * or 0 if we hit a page we can't access,
256 * or something > len if we didn't find a null byte.
258 * The `top' parameter to __strnlen_user is to make sure that
259 * we can never overflow from the user area into kernel space.
261 extern __inline__ int strnlen_user(const char *str, long len)
263 unsigned long top = __kernel_ok? ~0UL: TASK_SIZE - 1;
265 if ((unsigned long)str > top)
266 return 0;
267 return __strnlen_user(str, len, top);
270 #define strlen_user(str) strnlen_user((str), 0x7ffffffe)
272 #endif /* __ASSEMBLY__ */
274 #endif /* _PPC_UACCESS_H */