01f42b02e26cbc286e6c91f8ea1e35e0c2d8af5b
[deliverable/linux.git] / arch / blackfin / include / asm / uaccess.h
1 /* Changes made by Lineo Inc. May 2001
2 *
3 * Based on: include/asm-m68knommu/uaccess.h
4 */
5
6 #ifndef __BLACKFIN_UACCESS_H
7 #define __BLACKFIN_UACCESS_H
8
9 /*
10 * User space memory access functions
11 */
12 #include <linux/sched.h>
13 #include <linux/mm.h>
14 #include <linux/string.h>
15
16 #include <asm/segment.h>
17 #ifdef CONFIG_ACCESS_CHECK
18 # include <asm/bfin-global.h>
19 #endif
20
21 #define get_ds() (KERNEL_DS)
22 #define get_fs() (current_thread_info()->addr_limit)
23
24 static inline void set_fs(mm_segment_t fs)
25 {
26 current_thread_info()->addr_limit = fs;
27 }
28
29 #define segment_eq(a,b) ((a) == (b))
30
31 #define VERIFY_READ 0
32 #define VERIFY_WRITE 1
33
34 #define access_ok(type, addr, size) _access_ok((unsigned long)(addr), (size))
35
36 static inline int is_in_rom(unsigned long addr)
37 {
38 /*
39 * What we are really trying to do is determine if addr is
40 * in an allocated kernel memory region. If not then assume
41 * we cannot free it or otherwise de-allocate it. Ideally
42 * we could restrict this to really being in a ROM or flash,
43 * but that would need to be done on a board by board basis,
44 * not globally.
45 */
46 if ((addr < _ramstart) || (addr >= _ramend))
47 return (1);
48
49 /* Default case, not in ROM */
50 return (0);
51 }
52
53 /*
54 * The fs value determines whether argument validity checking should be
55 * performed or not. If get_fs() == USER_DS, checking is performed, with
56 * get_fs() == KERNEL_DS, checking is bypassed.
57 */
58
59 #ifndef CONFIG_ACCESS_CHECK
60 static inline int _access_ok(unsigned long addr, unsigned long size) { return 1; }
61 #else
62 #ifdef CONFIG_ACCESS_OK_L1
63 extern int _access_ok(unsigned long addr, unsigned long size)__attribute__((l1_text));
64 #else
65 extern int _access_ok(unsigned long addr, unsigned long size);
66 #endif
67 #endif
68
69 /*
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
74 * what to do.
75 *
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.
80 */
81
82 struct exception_table_entry {
83 unsigned long insn, fixup;
84 };
85
86 /*
87 * These are the main single-value transfer routines. They automatically
88 * use the right size if we just have the right pointer type.
89 */
90
91 #define put_user(x,p) \
92 ({ \
93 int _err = 0; \
94 typeof(*(p)) _x = (x); \
95 typeof(*(p)) *_p = (p); \
96 if (!access_ok(VERIFY_WRITE, _p, sizeof(*(_p)))) {\
97 _err = -EFAULT; \
98 } \
99 else { \
100 switch (sizeof (*(_p))) { \
101 case 1: \
102 __put_user_asm(_x, _p, B); \
103 break; \
104 case 2: \
105 __put_user_asm(_x, _p, W); \
106 break; \
107 case 4: \
108 __put_user_asm(_x, _p, ); \
109 break; \
110 case 8: { \
111 long _xl, _xh; \
112 _xl = ((long *)&_x)[0]; \
113 _xh = ((long *)&_x)[1]; \
114 __put_user_asm(_xl, ((long *)_p)+0, ); \
115 __put_user_asm(_xh, ((long *)_p)+1, ); \
116 } break; \
117 default: \
118 _err = __put_user_bad(); \
119 break; \
120 } \
121 } \
122 _err; \
123 })
124
125 #define __put_user(x,p) put_user(x,p)
126 static inline int bad_user_access_length(void)
127 {
128 panic("bad_user_access_length");
129 return -1;
130 }
131
132 #define __put_user_bad() (printk(KERN_INFO "put_user_bad %s:%d %s\n",\
133 __FILE__, __LINE__, __func__),\
134 bad_user_access_length(), (-EFAULT))
135
136 /*
137 * Tell gcc we read from memory instead of writing: this is because
138 * we do not write to any memory gcc knows about, so there are no
139 * aliasing issues.
140 */
141
142 #define __ptr(x) ((unsigned long *)(x))
143
144 #define __put_user_asm(x,p,bhw) \
145 __asm__ (#bhw"[%1] = %0;\n\t" \
146 : /* no outputs */ \
147 :"d" (x),"a" (__ptr(p)) : "memory")
148
149 #define get_user(x, ptr) \
150 ({ \
151 int _err = 0; \
152 unsigned long _val = 0; \
153 const typeof(*(ptr)) __user *_p = (ptr); \
154 const size_t ptr_size = sizeof(*(_p)); \
155 if (likely(access_ok(VERIFY_READ, _p, ptr_size))) { \
156 BUILD_BUG_ON(ptr_size >= 8); \
157 switch (ptr_size) { \
158 case 1: \
159 __get_user_asm(_val, _p, B,(Z)); \
160 break; \
161 case 2: \
162 __get_user_asm(_val, _p, W,(Z)); \
163 break; \
164 case 4: \
165 __get_user_asm(_val, _p, , ); \
166 break; \
167 } \
168 } else \
169 _err = -EFAULT; \
170 x = (typeof(*(ptr)))_val; \
171 _err; \
172 })
173
174 #define __get_user(x,p) get_user(x,p)
175
176 #define __get_user_bad() (bad_user_access_length(), (-EFAULT))
177
178 #define __get_user_asm(x, ptr, bhw, option) \
179 ({ \
180 __asm__ __volatile__ ( \
181 "%0 =" #bhw "[%1]" #option ";" \
182 : "=d" (x) \
183 : "a" (__ptr(ptr))); \
184 })
185
186 #define __copy_from_user(to, from, n) copy_from_user(to, from, n)
187 #define __copy_to_user(to, from, n) copy_to_user(to, from, n)
188 #define __copy_to_user_inatomic __copy_to_user
189 #define __copy_from_user_inatomic __copy_from_user
190
191 #define copy_to_user_ret(to,from,n,retval) ({ if (copy_to_user(to,from,n))\
192 return retval; })
193
194 #define copy_from_user_ret(to,from,n,retval) ({ if (copy_from_user(to,from,n))\
195 return retval; })
196
197 static inline unsigned long __must_check
198 copy_from_user(void *to, const void __user *from, unsigned long n)
199 {
200 if (access_ok(VERIFY_READ, from, n))
201 memcpy(to, from, n);
202 else
203 return n;
204 return 0;
205 }
206
207 static inline unsigned long __must_check
208 copy_to_user(void *to, const void __user *from, unsigned long n)
209 {
210 if (access_ok(VERIFY_WRITE, to, n))
211 memcpy(to, from, n);
212 else
213 return n;
214 return 0;
215 }
216
217 /*
218 * Copy a null terminated string from userspace.
219 */
220
221 static inline long __must_check
222 strncpy_from_user(char *dst, const char *src, long count)
223 {
224 char *tmp;
225 if (!access_ok(VERIFY_READ, src, 1))
226 return -EFAULT;
227 strncpy(dst, src, count);
228 for (tmp = dst; *tmp && count > 0; tmp++, count--) ;
229 return (tmp - dst);
230 }
231
232 /*
233 * Get the size of a string in user space.
234 * src: The string to measure
235 * n: The maximum valid length
236 *
237 * Get the size of a NUL-terminated string in user space.
238 *
239 * Returns the size of the string INCLUDING the terminating NUL.
240 * On exception, returns 0.
241 * If the string is too long, returns a value greater than n.
242 */
243 static inline long __must_check strnlen_user(const char *src, long n)
244 {
245 if (!access_ok(VERIFY_READ, src, 1))
246 return 0;
247 return strnlen(src, n) + 1;
248 }
249
250 static inline long __must_check strlen_user(const char *src)
251 {
252 if (!access_ok(VERIFY_READ, src, 1))
253 return 0;
254 return strlen(src) + 1;
255 }
256
257 /*
258 * Zero Userspace
259 */
260
261 static inline unsigned long __must_check
262 __clear_user(void *to, unsigned long n)
263 {
264 if (!access_ok(VERIFY_WRITE, to, n))
265 return n;
266 memset(to, 0, n);
267 return 0;
268 }
269
270 #define clear_user(to, n) __clear_user(to, n)
271
272 #endif /* _BLACKFIN_UACCESS_H */
This page took 0.05647 seconds and 4 git commands to generate.