This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / include / asm-frv / uaccess.h
1 /* uaccess.h: userspace accessor functions
2  *
3  * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11
12 #ifndef _ASM_UACCESS_H
13 #define _ASM_UACCESS_H
14
15 /*
16  * User space memory access functions
17  */
18 #include <linux/sched.h>
19 #include <linux/mm.h>
20 #include <asm/segment.h>
21 #include <asm/sections.h>
22
23 #define HAVE_ARCH_UNMAPPED_AREA /* we decide where to put mmaps */
24
25 #define __ptr(x) ((unsigned long *)(x))
26
27 #define VERIFY_READ     0
28 #define VERIFY_WRITE    1
29
30 #define __addr_ok(addr) ((unsigned long)(addr) < get_addr_limit())
31
32 /*
33  * check that a range of addresses falls within the current address limit
34  */
35 static inline int ___range_ok(unsigned long addr, unsigned long size)
36 {
37 #ifdef CONFIG_MMU
38         int flag = -EFAULT, tmp;
39
40         asm volatile (
41                 "       addcc   %3,%2,%1,icc0   \n"     /* set C-flag if addr+size>4GB */
42                 "       subcc.p %1,%4,gr0,icc1  \n"     /* jump if addr+size>limit */
43                 "       bc      icc0,#0,0f      \n"
44                 "       bhi     icc1,#0,0f      \n"
45                 "       setlos  #0,%0           \n"     /* mark okay */
46                 "0:                             \n"
47                 : "=r"(flag), "=&r"(tmp)
48                 : "r"(addr), "r"(size), "r"(get_addr_limit()), "0"(flag)
49                 );
50
51         return flag;
52
53 #else
54
55         if (addr < memory_start ||
56             addr > memory_end ||
57             size > memory_end - memory_start ||
58             addr + size > memory_end)
59                 return -EFAULT;
60
61         return 0;
62 #endif
63 }
64
65 #define __range_ok(addr,size) ___range_ok((unsigned long) (addr), (unsigned long) (size))
66
67 #define access_ok(type,addr,size) (__range_ok((addr), (size)) == 0)
68 #define __access_ok(addr,size) (__range_ok((addr), (size)) == 0)
69
70 static inline int verify_area(int type, const void * addr, unsigned long size)
71 {
72         return __range_ok(addr, size);
73 }
74
75 /*
76  * The exception table consists of pairs of addresses: the first is the
77  * address of an instruction that is allowed to fault, and the second is
78  * the address at which the program should continue.  No registers are
79  * modified, so it is entirely up to the continuation code to figure out
80  * what to do.
81  *
82  * All the routines below use bits of fixup code that are out of line
83  * with the main instruction path.  This means when everything is well,
84  * we don't even have to jump over them.  Further, they do not intrude
85  * on our cache or tlb entries.
86  */
87 struct exception_table_entry
88 {
89         unsigned long insn, fixup;
90 };
91
92 /* Returns 0 if exception not found and fixup otherwise.  */
93 extern unsigned long search_exception_table(unsigned long);
94
95
96 /*
97  * These are the main single-value transfer routines.  They automatically
98  * use the right size if we just have the right pointer type.
99  */
100 #define __put_user(x, ptr)                                              \
101 ({                                                                      \
102         int __pu_err = 0;                                               \
103                                                                         \
104         typeof(*(ptr)) __pu_val = (x);                                  \
105                                                                         \
106         switch (sizeof (*(ptr))) {                                      \
107         case 1:                                                         \
108                 __put_user_asm(__pu_err, __pu_val, ptr, "b", "r");      \
109                 break;                                                  \
110         case 2:                                                         \
111                 __put_user_asm(__pu_err, __pu_val, ptr, "h", "r");      \
112                 break;                                                  \
113         case 4:                                                         \
114                 __put_user_asm(__pu_err, __pu_val, ptr, "",  "r");      \
115                 break;                                                  \
116         case 8:                                                         \
117                 __put_user_asm(__pu_err, __pu_val, ptr, "d", "e");      \
118                 break;                                                  \
119         default:                                                        \
120                 __pu_err = __put_user_bad();                            \
121                 break;                                                  \
122         }                                                               \
123         __pu_err;                                                       \
124 })
125
126 #define put_user(x, ptr)                        \
127 ({                                              \
128         typeof(&*ptr) _p = (ptr);               \
129         int _e;                                 \
130                                                 \
131         _e = __range_ok(_p, sizeof(*_p));       \
132         if (_e == 0)                            \
133                 _e = __put_user((x), _p);       \
134         _e;                                     \
135 })
136
137 extern int __put_user_bad(void);
138
139 /*
140  * Tell gcc we read from memory instead of writing: this is because
141  * we do not write to any memory gcc knows about, so there are no
142  * aliasing issues.
143  */
144
145 #ifdef CONFIG_MMU
146
147 #define __put_user_asm(err,x,ptr,dsize,constraint)                                      \
148 do {                                                                                    \
149         asm volatile("1:        st"dsize"%I1    %2,%M1  \n"                             \
150                      "2:                                \n"                             \
151                      ".subsection 2                     \n"                             \
152                      "3:        setlos          %3,%0   \n"                             \
153                      "          bra             2b      \n"                             \
154                      ".previous                         \n"                             \
155                      ".section __ex_table,\"a\"         \n"                             \
156                      "          .balign         8       \n"                             \
157                      "          .long           1b,3b   \n"                             \
158                      ".previous"                                                        \
159                      : "=r" (err)                                                       \
160                      : "m" (*__ptr(ptr)), constraint (x), "i"(-EFAULT), "0"(err)        \
161                      : "memory");                                                       \
162 } while (0)
163
164 #else
165
166 #define __put_user_asm(err,x,ptr,bwl,con)       \
167 do {                                            \
168         asm("   st"bwl"%I0      %1,%M0  \n"     \
169             "   membar                  \n"     \
170             :                                   \
171             : "m" (*__ptr(ptr)), con (x)        \
172             : "memory");                        \
173 } while (0)
174
175 #endif
176
177 /*****************************************************************************/
178 /*
179  *
180  */
181 #define __get_user(x, ptr)                                              \
182 ({                                                                      \
183         typeof(*(ptr)) __gu_val = 0;                                    \
184         int __gu_err = 0;                                               \
185                                                                         \
186         switch (sizeof(*(ptr))) {                                       \
187         case 1:                                                         \
188                 __get_user_asm(__gu_err, __gu_val, ptr, "ub", "=r");    \
189                 break;                                                  \
190         case 2:                                                         \
191                 __get_user_asm(__gu_err, __gu_val, ptr, "uh", "=r");    \
192                 break;                                                  \
193         case 4:                                                         \
194                 __get_user_asm(__gu_err, __gu_val, ptr, "", "=r");      \
195                 break;                                                  \
196         case 8:                                                         \
197                 __get_user_asm(__gu_err, __gu_val, ptr, "d", "=e");     \
198                 break;                                                  \
199         default:                                                        \
200                 __gu_err = __get_user_bad();                            \
201                 break;                                                  \
202         }                                                               \
203         (x) = __gu_val;                                                 \
204         __gu_err;                                                       \
205 })
206
207 #define get_user(x, ptr)                        \
208 ({                                              \
209         typeof(&*ptr) _p = (ptr);               \
210         int _e;                                 \
211                                                 \
212         _e = __range_ok(_p, sizeof(*_p));       \
213         if (likely(_e == 0))                    \
214                 _e = __get_user((x), _p);       \
215         else                                    \
216                 (x) = (typeof(x)) 0;            \
217         _e;                                     \
218 })
219
220 extern int __get_user_bad(void);
221
222 #ifdef CONFIG_MMU
223
224 #define __get_user_asm(err,x,ptr,dtype,constraint)      \
225 do {                                                    \
226         asm("1:         ld"dtype"%I2    %M2,%1  \n"     \
227             "2:                                 \n"     \
228             ".subsection 2                      \n"     \
229             "3:         setlos          %3,%0   \n"     \
230             "           setlos          #0,%1   \n"     \
231             "           bra             2b      \n"     \
232             ".previous                          \n"     \
233             ".section __ex_table,\"a\"          \n"     \
234             "           .balign         8       \n"     \
235             "           .long           1b,3b   \n"     \
236             ".previous"                                 \
237             : "=r" (err), constraint (x)                \
238             : "m" (*__ptr(ptr)), "i"(-EFAULT), "0"(err) \
239             );                                          \
240 } while(0)
241
242 #else
243
244 #define __get_user_asm(err,x,ptr,bwl,con)       \
245         asm("   ld"bwl"%I1      %M1,%0  \n"     \
246             "   membar                  \n"     \
247             : con(x)                            \
248             : "m" (*__ptr(ptr)))
249
250 #endif
251
252 /*****************************************************************************/
253 /*
254  *
255  */
256 #ifdef CONFIG_MMU
257 extern long __memset_user(void *dst, unsigned long count);
258 extern long __memcpy_user(void *dst, const void *src, unsigned long count);
259
260 #define clear_user(dst,count)                   __memset_user((dst), (count))
261 #define __copy_from_user_inatomic(to, from, n)  __memcpy_user((to), (from), (n))
262 #define __copy_to_user_inatomic(to, from, n)    __memcpy_user((to), (from), (n))
263
264 #else
265
266 #define clear_user(dst,count)                   (memset((dst), 0, (count)), 0)
267 #define __copy_from_user_inatomic(to, from, n)  (memcpy((to), (from), (n)), 0)
268 #define __copy_to_user_inatomic(to, from, n)    (memcpy((to), (from), (n)), 0)
269
270 #endif
271
272 static inline unsigned long __must_check
273 __copy_to_user(void __user *to, const void *from, unsigned long n)
274 {
275        might_sleep();
276        return __copy_to_user_inatomic(to, from, n);
277 }
278
279 static inline unsigned long
280 __copy_from_user(void *to, const void __user *from, unsigned long n)
281 {
282        might_sleep();
283        return __copy_from_user_inatomic(to, from, n);
284 }
285
286 static inline long copy_from_user(void *to, const void *from, unsigned long n)
287 {
288         unsigned long ret = n;
289
290         if (likely(__access_ok(from, n)))
291                 ret = __copy_from_user(to, from, n);
292
293         if (unlikely(ret != 0))
294                 memset(to + (n - ret), 0, ret);
295
296         return ret;
297 }
298
299 static inline long copy_to_user(void *to, const void *from, unsigned long n)
300 {
301         return likely(__access_ok(to, n)) ? __copy_to_user(to, from, n) : n;
302 }
303
304 #define copy_to_user_ret(to,from,n,retval)      ({ if (copy_to_user(to,from,n)) return retval; })
305 #define copy_from_user_ret(to,from,n,retval)    ({ if (copy_from_user(to,from,n)) return retval; })
306
307 extern long strncpy_from_user(char *dst, const char *src, long count);
308 extern long strnlen_user(const char *src, long count);
309
310 #define strlen_user(str) strnlen_user(str, 32767)
311
312 extern unsigned long search_exception_table(unsigned long addr);
313
314 #define copy_to_user_page(vma, page, vaddr, dst, src, len)      memcpy(dst, src, len)
315 #define copy_from_user_page(vma, page, vaddr, dst, src, len)    memcpy(dst, src, len)
316
317 #endif /* _ASM_UACCESS_H */