]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/blackfin/include/asm/uaccess.h
d9a91108964f6f9687e8d60d4314e232a143d312
[karo-tx-linux.git] / arch / blackfin / include / asm / uaccess.h
1 /*
2  * Copyright 2004-2009 Analog Devices Inc.
3  *
4  * Licensed under the GPL-2 or later.
5  *
6  * Based on: include/asm-m68knommu/uaccess.h
7  */
8
9 #ifndef __BLACKFIN_UACCESS_H
10 #define __BLACKFIN_UACCESS_H
11
12 /*
13  * User space memory access functions
14  */
15 #include <linux/sched.h>
16 #include <linux/mm.h>
17 #include <linux/string.h>
18
19 #include <asm/segment.h>
20 #include <asm/sections.h>
21
22 #define get_ds()        (KERNEL_DS)
23 #define get_fs()        (current_thread_info()->addr_limit)
24
25 static inline void set_fs(mm_segment_t fs)
26 {
27         current_thread_info()->addr_limit = fs;
28 }
29
30 #define segment_eq(a, b) ((a) == (b))
31
32 #define access_ok(type, addr, size) _access_ok((unsigned long)(addr), (size))
33
34 /*
35  * The fs value determines whether argument validity checking should be
36  * performed or not.  If get_fs() == USER_DS, checking is performed, with
37  * get_fs() == KERNEL_DS, checking is bypassed.
38  */
39
40 #ifndef CONFIG_ACCESS_CHECK
41 static inline int _access_ok(unsigned long addr, unsigned long size) { return 1; }
42 #else
43 extern int _access_ok(unsigned long addr, unsigned long size);
44 #endif
45
46 /*
47  * The exception table consists of pairs of addresses: the first is the
48  * address of an instruction that is allowed to fault, and the second is
49  * the address at which the program should continue.  No registers are
50  * modified, so it is entirely up to the continuation code to figure out
51  * what to do.
52  *
53  * All the routines below use bits of fixup code that are out of line
54  * with the main instruction path.  This means when everything is well,
55  * we don't even have to jump over them.  Further, they do not intrude
56  * on our cache or tlb entries.
57  */
58
59 struct exception_table_entry {
60         unsigned long insn, fixup;
61 };
62
63 /*
64  * These are the main single-value transfer routines.  They automatically
65  * use the right size if we just have the right pointer type.
66  */
67
68 #define put_user(x, p)                                          \
69         ({                                                      \
70                 int _err = 0;                                   \
71                 typeof(*(p)) _x = (x);                          \
72                 typeof(*(p)) __user *_p = (p);                  \
73                 if (!access_ok(VERIFY_WRITE, _p, sizeof(*(_p)))) {\
74                         _err = -EFAULT;                         \
75                 }                                               \
76                 else {                                          \
77                 switch (sizeof (*(_p))) {                       \
78                 case 1:                                         \
79                         __put_user_asm(_x, _p, B);              \
80                         break;                                  \
81                 case 2:                                         \
82                         __put_user_asm(_x, _p, W);              \
83                         break;                                  \
84                 case 4:                                         \
85                         __put_user_asm(_x, _p,  );              \
86                         break;                                  \
87                 case 8: {                                       \
88                         long _xl, _xh;                          \
89                         _xl = ((__force long *)&_x)[0];         \
90                         _xh = ((__force long *)&_x)[1];         \
91                         __put_user_asm(_xl, ((__force long __user *)_p)+0, );\
92                         __put_user_asm(_xh, ((__force long __user *)_p)+1, );\
93                 } break;                                        \
94                 default:                                        \
95                         _err = __put_user_bad();                \
96                         break;                                  \
97                 }                                               \
98                 }                                               \
99                 _err;                                           \
100         })
101
102 #define __put_user(x, p) put_user(x, p)
103 static inline int bad_user_access_length(void)
104 {
105         panic("bad_user_access_length");
106         return -1;
107 }
108
109 #define __put_user_bad() (printk(KERN_INFO "put_user_bad %s:%d %s\n",\
110                            __FILE__, __LINE__, __func__),\
111                            bad_user_access_length(), (-EFAULT))
112
113 /*
114  * Tell gcc we read from memory instead of writing: this is because
115  * we do not write to any memory gcc knows about, so there are no
116  * aliasing issues.
117  */
118
119 #define __ptr(x) ((unsigned long __force *)(x))
120
121 #define __put_user_asm(x, p, bhw)                       \
122         __asm__ (#bhw"[%1] = %0;\n\t"                   \
123                  : /* no outputs */                     \
124                  :"d" (x), "a" (__ptr(p)) : "memory")
125
126 #define get_user(x, ptr)                                        \
127 ({                                                              \
128         int _err = 0;                                           \
129         unsigned long _val = 0;                                 \
130         const typeof(*(ptr)) __user *_p = (ptr);                \
131         const size_t ptr_size = sizeof(*(_p));                  \
132         if (likely(access_ok(VERIFY_READ, _p, ptr_size))) {     \
133                 BUILD_BUG_ON(ptr_size >= 8);                    \
134                 switch (ptr_size) {                             \
135                 case 1:                                         \
136                         __get_user_asm(_val, _p, B, (Z));       \
137                         break;                                  \
138                 case 2:                                         \
139                         __get_user_asm(_val, _p, W, (Z));       \
140                         break;                                  \
141                 case 4:                                         \
142                         __get_user_asm(_val, _p,  , );          \
143                         break;                                  \
144                 }                                               \
145         } else                                                  \
146                 _err = -EFAULT;                                 \
147         x = (__force typeof(*(ptr)))_val;                       \
148         _err;                                                   \
149 })
150
151 #define __get_user(x, p) get_user(x, p)
152
153 #define __get_user_bad() (bad_user_access_length(), (-EFAULT))
154
155 #define __get_user_asm(x, ptr, bhw, option)     \
156 ({                                              \
157         __asm__ __volatile__ (                  \
158                 "%0 =" #bhw "[%1]" #option ";"  \
159                 : "=d" (x)                      \
160                 : "a" (__ptr(ptr)));            \
161 })
162
163 #define __copy_to_user_inatomic __copy_to_user
164 #define __copy_from_user_inatomic __copy_from_user
165
166 static inline unsigned long __must_check
167 __copy_from_user(void *to, const void __user *from, unsigned long n)
168 {
169         memcpy(to, (const void __force *)from, n);
170         return 0;
171 }
172
173 static inline unsigned long __must_check
174 __copy_to_user(void __user *to, const void *from, unsigned long n)
175 {
176         memcpy((void __force *)to, from, n);
177         SSYNC();
178         return 0;
179 }
180
181 static inline unsigned long __must_check
182 copy_from_user(void *to, const void __user *from, unsigned long n)
183 {
184         if (likely(access_ok(VERIFY_READ, from, n)))
185                 return __copy_from_user(to, from, n);
186         memset(to, 0, n);
187         return n;
188 }
189
190 static inline unsigned long __must_check
191 copy_to_user(void __user *to, const void *from, unsigned long n)
192 {
193         if (likely(access_ok(VERIFY_WRITE, to, n)))
194                 return __copy_to_user(to, from, n);
195         return n;
196 }
197
198 /*
199  * Copy a null terminated string from userspace.
200  */
201
202 static inline long __must_check
203 strncpy_from_user(char *dst, const char __user *src, long count)
204 {
205         char *tmp;
206         if (!access_ok(VERIFY_READ, src, 1))
207                 return -EFAULT;
208         strncpy(dst, (const char __force *)src, count);
209         for (tmp = dst; *tmp && count > 0; tmp++, count--) ;
210         return (tmp - dst);
211 }
212
213 /*
214  * Get the size of a string in user space.
215  *   src: The string to measure
216  *     n: The maximum valid length
217  *
218  * Get the size of a NUL-terminated string in user space.
219  *
220  * Returns the size of the string INCLUDING the terminating NUL.
221  * On exception, returns 0.
222  * If the string is too long, returns a value greater than n.
223  */
224 static inline long __must_check strnlen_user(const char __user *src, long n)
225 {
226         if (!access_ok(VERIFY_READ, src, 1))
227                 return 0;
228         return strnlen((const char __force *)src, n) + 1;
229 }
230
231 static inline long __must_check strlen_user(const char __user *src)
232 {
233         if (!access_ok(VERIFY_READ, src, 1))
234                 return 0;
235         return strlen((const char __force *)src) + 1;
236 }
237
238 /*
239  * Zero Userspace
240  */
241
242 static inline unsigned long __must_check
243 __clear_user(void __user *to, unsigned long n)
244 {
245         if (!access_ok(VERIFY_WRITE, to, n))
246                 return n;
247         memset((void __force *)to, 0, n);
248         return 0;
249 }
250
251 #define clear_user(to, n) __clear_user(to, n)
252
253 /* How to interpret these return values:
254  *      CORE:      can be accessed by core load or dma memcpy
255  *      CORE_ONLY: can only be accessed by core load
256  *      DMA:       can only be accessed by dma memcpy
257  *      IDMA:      can only be accessed by interprocessor dma memcpy (BF561)
258  *      ITEST:     can be accessed by isram memcpy or dma memcpy
259  */
260 enum {
261         BFIN_MEM_ACCESS_CORE = 0,
262         BFIN_MEM_ACCESS_CORE_ONLY,
263         BFIN_MEM_ACCESS_DMA,
264         BFIN_MEM_ACCESS_IDMA,
265         BFIN_MEM_ACCESS_ITEST,
266 };
267 /**
268  *      bfin_mem_access_type() - what kind of memory access is required
269  *      @addr:   the address to check
270  *      @size:   number of bytes needed
271  *      @return: <0 is error, >=0 is BFIN_MEM_ACCESS_xxx enum (see above)
272  */
273 int bfin_mem_access_type(unsigned long addr, unsigned long size);
274
275 #endif                          /* _BLACKFIN_UACCESS_H */