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