Commit | Line | Data |
---|---|---|
2660663f | 1 | /* |
0d6de953 MS |
2 | * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu> |
3 | * Copyright (C) 2008-2009 PetaLogix | |
2660663f MS |
4 | * Copyright (C) 2006 Atmark Techno, Inc. |
5 | * | |
6 | * This file is subject to the terms and conditions of the GNU General Public | |
7 | * License. See the file "COPYING" in the main directory of this archive | |
8 | * for more details. | |
9 | */ | |
10 | ||
11 | #ifndef _ASM_MICROBLAZE_UACCESS_H | |
12 | #define _ASM_MICROBLAZE_UACCESS_H | |
13 | ||
14 | #ifdef __KERNEL__ | |
15 | #ifndef __ASSEMBLY__ | |
16 | ||
17 | #include <linux/kernel.h> | |
18 | #include <linux/errno.h> | |
19 | #include <linux/sched.h> /* RLIMIT_FSIZE */ | |
20 | #include <linux/mm.h> | |
21 | ||
22 | #include <asm/mmu.h> | |
23 | #include <asm/page.h> | |
24 | #include <asm/pgtable.h> | |
2660663f MS |
25 | #include <linux/string.h> |
26 | ||
27 | #define VERIFY_READ 0 | |
28 | #define VERIFY_WRITE 1 | |
29 | ||
40db0834 MS |
30 | /* |
31 | * On Microblaze the fs value is actually the top of the corresponding | |
32 | * address space. | |
33 | * | |
34 | * The fs value determines whether argument validity checking should be | |
35 | * performed or not. If get_fs() == USER_DS, checking is performed, with | |
36 | * get_fs() == KERNEL_DS, checking is bypassed. | |
37 | * | |
38 | * For historical reasons, these macros are grossly misnamed. | |
39 | * | |
40 | * For non-MMU arch like Microblaze, KERNEL_DS and USER_DS is equal. | |
41 | */ | |
42 | # define MAKE_MM_SEG(s) ((mm_segment_t) { (s) }) | |
43 | ||
44 | # ifndef CONFIG_MMU | |
45 | # define KERNEL_DS MAKE_MM_SEG(0) | |
46 | # define USER_DS KERNEL_DS | |
47 | # else | |
48 | # define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF) | |
49 | # define USER_DS MAKE_MM_SEG(TASK_SIZE - 1) | |
50 | # endif | |
51 | ||
52 | # define get_ds() (KERNEL_DS) | |
53 | # define get_fs() (current_thread_info()->addr_limit) | |
54 | # define set_fs(val) (current_thread_info()->addr_limit = (val)) | |
55 | ||
56 | # define segment_eq(a, b) ((a).seg == (b).seg) | |
57 | ||
357bc3c9 MS |
58 | /* |
59 | * The exception table consists of pairs of addresses: the first is the | |
60 | * address of an instruction that is allowed to fault, and the second is | |
61 | * the address at which the program should continue. No registers are | |
62 | * modified, so it is entirely up to the continuation code to figure out | |
63 | * what to do. | |
64 | * | |
65 | * All the routines below use bits of fixup code that are out of line | |
66 | * with the main instruction path. This means when everything is well, | |
67 | * we don't even have to jump over them. Further, they do not intrude | |
68 | * on our cache or tlb entries. | |
69 | */ | |
70 | struct exception_table_entry { | |
71 | unsigned long insn, fixup; | |
72 | }; | |
40db0834 | 73 | |
0d6de953 MS |
74 | #ifndef CONFIG_MMU |
75 | ||
60a729f7 MS |
76 | /* Check against bounds of physical memory */ |
77 | static inline int ___range_ok(unsigned long addr, unsigned long size) | |
78 | { | |
79 | return ((addr < memory_start) || | |
80 | ((addr + size) > memory_end)); | |
81 | } | |
2660663f MS |
82 | |
83 | #define __range_ok(addr, size) \ | |
84 | ___range_ok((unsigned long)(addr), (unsigned long)(size)) | |
85 | ||
86 | #define access_ok(type, addr, size) (__range_ok((addr), (size)) == 0) | |
40b1156d MS |
87 | |
88 | #else | |
89 | ||
90 | /* | |
91 | * Address is valid if: | |
92 | * - "addr", "addr + size" and "size" are all below the limit | |
93 | */ | |
94 | #define access_ok(type, addr, size) \ | |
95 | (get_fs().seg > (((unsigned long)(addr)) | \ | |
96 | (size) | ((unsigned long)(addr) + (size)))) | |
97 | ||
98 | /* || printk("access_ok failed for %s at 0x%08lx (size %d), seg 0x%08x\n", | |
99 | type?"WRITE":"READ",addr,size,get_fs().seg)) */ | |
100 | ||
101 | #endif | |
102 | ||
103 | #ifdef CONFIG_MMU | |
104 | # define __FIXUP_SECTION ".section .fixup,\"ax\"\n" | |
105 | # define __EX_TABLE_SECTION ".section __ex_table,\"a\"\n" | |
106 | #else | |
107 | # define __FIXUP_SECTION ".section .discard,\"ax\"\n" | |
108 | # define __EX_TABLE_SECTION ".section .discard,\"a\"\n" | |
109 | #endif | |
110 | ||
111 | #ifndef CONFIG_MMU | |
2660663f | 112 | |
838d2406 AB |
113 | /* Undefined function to trigger linker error */ |
114 | extern int bad_user_access_length(void); | |
115 | ||
2660663f | 116 | /* FIXME this is function for optimalization -> memcpy */ |
0d6de953 MS |
117 | #define __get_user(var, ptr) \ |
118 | ({ \ | |
119 | int __gu_err = 0; \ | |
120 | switch (sizeof(*(ptr))) { \ | |
121 | case 1: \ | |
122 | case 2: \ | |
123 | case 4: \ | |
124 | (var) = *(ptr); \ | |
125 | break; \ | |
126 | case 8: \ | |
127 | memcpy((void *) &(var), (ptr), 8); \ | |
128 | break; \ | |
129 | default: \ | |
130 | (var) = 0; \ | |
131 | __gu_err = __get_user_bad(); \ | |
132 | break; \ | |
133 | } \ | |
134 | __gu_err; \ | |
135 | }) | |
2660663f MS |
136 | |
137 | #define __get_user_bad() (bad_user_access_length(), (-EFAULT)) | |
138 | ||
0d6de953 | 139 | /* FIXME is not there defined __pu_val */ |
2660663f | 140 | #define __put_user(var, ptr) \ |
0d6de953 MS |
141 | ({ \ |
142 | int __pu_err = 0; \ | |
143 | switch (sizeof(*(ptr))) { \ | |
144 | case 1: \ | |
145 | case 2: \ | |
146 | case 4: \ | |
147 | *(ptr) = (var); \ | |
148 | break; \ | |
149 | case 8: { \ | |
150 | typeof(*(ptr)) __pu_val = (var); \ | |
151 | memcpy(ptr, &__pu_val, sizeof(__pu_val)); \ | |
152 | } \ | |
153 | break; \ | |
154 | default: \ | |
155 | __pu_err = __put_user_bad(); \ | |
156 | break; \ | |
157 | } \ | |
158 | __pu_err; \ | |
159 | }) | |
2660663f MS |
160 | |
161 | #define __put_user_bad() (bad_user_access_length(), (-EFAULT)) | |
162 | ||
0d6de953 MS |
163 | #define put_user(x, ptr) __put_user((x), (ptr)) |
164 | #define get_user(x, ptr) __get_user((x), (ptr)) | |
2660663f | 165 | |
0d6de953 MS |
166 | #define copy_to_user(to, from, n) (memcpy((to), (from), (n)), 0) |
167 | #define copy_from_user(to, from, n) (memcpy((to), (from), (n)), 0) | |
2660663f | 168 | |
0d6de953 MS |
169 | #define __copy_to_user(to, from, n) (copy_to_user((to), (from), (n))) |
170 | #define __copy_from_user(to, from, n) (copy_from_user((to), (from), (n))) | |
171 | #define __copy_to_user_inatomic(to, from, n) \ | |
172 | (__copy_to_user((to), (from), (n))) | |
173 | #define __copy_from_user_inatomic(to, from, n) \ | |
174 | (__copy_from_user((to), (from), (n))) | |
2660663f | 175 | |
40b1156d MS |
176 | #define __clear_user(addr, n) (memset((void *)(addr), 0, (n)), 0) |
177 | ||
178 | /* stejne s MMU */ | |
2660663f MS |
179 | static inline unsigned long clear_user(void *addr, unsigned long size) |
180 | { | |
181 | if (access_ok(VERIFY_WRITE, addr, size)) | |
182 | size = __clear_user(addr, size); | |
183 | return size; | |
184 | } | |
185 | ||
0d6de953 | 186 | /* Returns 0 if exception not found and fixup otherwise. */ |
2660663f MS |
187 | extern unsigned long search_exception_table(unsigned long); |
188 | ||
0d6de953 MS |
189 | extern long strncpy_from_user(char *dst, const char *src, long count); |
190 | extern long strnlen_user(const char *src, long count); | |
191 | ||
192 | #else /* CONFIG_MMU */ | |
193 | ||
0dcb409d MS |
194 | /* Return: number of not copied bytes, i.e. 0 if OK or non-zero if fail. */ |
195 | static inline unsigned long __must_check __clear_user(void __user *to, | |
196 | unsigned long n) | |
197 | { | |
198 | /* normal memset with two words to __ex_table */ | |
199 | __asm__ __volatile__ ( \ | |
200 | "1: sb r0, %2, r0;" \ | |
201 | " addik %0, %0, -1;" \ | |
202 | " bneid %0, 1b;" \ | |
203 | " addik %2, %2, 1;" \ | |
204 | "2: " \ | |
205 | __EX_TABLE_SECTION \ | |
206 | ".word 1b,2b;" \ | |
207 | ".previous;" \ | |
208 | : "=r"(n) \ | |
209 | : "0"(n), "r"(to) | |
210 | ); | |
211 | return n; | |
212 | } | |
213 | ||
214 | static inline unsigned long __must_check clear_user(void __user *to, | |
215 | unsigned long n) | |
216 | { | |
217 | might_sleep(); | |
218 | if (unlikely(!access_ok(VERIFY_WRITE, to, n))) | |
219 | return n; | |
220 | return __clear_user(to, n); | |
221 | } | |
222 | ||
223 | /* put_user and get_user macros */ | |
224 | ||
225 | extern long __user_bad(void); | |
0d6de953 | 226 | |
8b651aa4 MS |
227 | #define __get_user_asm(insn, __gu_ptr, __gu_val, __gu_err) \ |
228 | ({ \ | |
229 | __asm__ __volatile__ ( \ | |
230 | "1:" insn " %1, %2, r0;" \ | |
231 | " addk %0, r0, r0;" \ | |
232 | "2: " \ | |
233 | __FIXUP_SECTION \ | |
234 | "3: brid 2b; " \ | |
235 | " addik %0, r0, %3;" \ | |
236 | ".previous;" \ | |
237 | __EX_TABLE_SECTION \ | |
238 | ".word 1b,3b;" \ | |
239 | ".previous;" \ | |
240 | : "=&r"(__gu_err), "=r"(__gu_val) \ | |
241 | : "r"(__gu_ptr), "i"(-EFAULT) \ | |
242 | ); \ | |
0d6de953 MS |
243 | }) |
244 | ||
245 | #define __get_user(x, ptr) \ | |
246 | ({ \ | |
247 | unsigned long __gu_val; \ | |
248 | /*unsigned long __gu_ptr = (unsigned long)(ptr);*/ \ | |
249 | long __gu_err; \ | |
250 | switch (sizeof(*(ptr))) { \ | |
251 | case 1: \ | |
252 | __get_user_asm("lbu", (ptr), __gu_val, __gu_err); \ | |
253 | break; \ | |
254 | case 2: \ | |
255 | __get_user_asm("lhu", (ptr), __gu_val, __gu_err); \ | |
256 | break; \ | |
257 | case 4: \ | |
258 | __get_user_asm("lw", (ptr), __gu_val, __gu_err); \ | |
259 | break; \ | |
260 | default: \ | |
0dcb409d | 261 | /* __gu_val = 0; __gu_err = -EINVAL;*/ __gu_err = __user_bad();\ |
0d6de953 MS |
262 | } \ |
263 | x = (__typeof__(*(ptr))) __gu_val; \ | |
264 | __gu_err; \ | |
265 | }) | |
266 | ||
0dcb409d MS |
267 | /** |
268 | * get_user: - Get a simple variable from user space. | |
269 | * @x: Variable to store result. | |
270 | * @ptr: Source address, in user space. | |
271 | * | |
272 | * Context: User context only. This function may sleep. | |
273 | * | |
274 | * This macro copies a single simple variable from user space to kernel | |
275 | * space. It supports simple types like char and int, but not larger | |
276 | * data types like structures or arrays. | |
277 | * | |
278 | * @ptr must have pointer-to-simple-variable type, and the result of | |
279 | * dereferencing @ptr must be assignable to @x without a cast. | |
280 | * | |
281 | * Returns zero on success, or -EFAULT on error. | |
282 | * On error, the variable @x is set to zero. | |
283 | */ | |
284 | ||
8b651aa4 MS |
285 | #define get_user(x, ptr) \ |
286 | ({ \ | |
287 | access_ok(VERIFY_READ, (ptr), sizeof(*(ptr))) \ | |
288 | ? __get_user((x), (ptr)) : -EFAULT; \ | |
289 | }) | |
290 | ||
0dcb409d MS |
291 | /** |
292 | * put_user: - Write a simple value into user space. | |
293 | * @x: Value to copy to user space. | |
294 | * @ptr: Destination address, in user space. | |
295 | * | |
296 | * Context: User context only. This function may sleep. | |
297 | * | |
298 | * This macro copies a single simple value from kernel space to user | |
299 | * space. It supports simple types like char and int, but not larger | |
300 | * data types like structures or arrays. | |
301 | * | |
302 | * @ptr must have pointer-to-simple-variable type, and @x must be assignable | |
303 | * to the result of dereferencing @ptr. | |
304 | * | |
305 | * Returns zero on success, or -EFAULT on error. | |
306 | */ | |
307 | ||
8b651aa4 | 308 | #define __put_user_asm(insn, __gu_ptr, __gu_val, __gu_err) \ |
c77a9c4b MS |
309 | ({ \ |
310 | __asm__ __volatile__ ( \ | |
311 | "1:" insn " %1, %2, r0;" \ | |
312 | " addk %0, r0, r0;" \ | |
313 | "2: " \ | |
314 | __FIXUP_SECTION \ | |
8b651aa4 | 315 | "3: brid 2b;" \ |
c77a9c4b MS |
316 | " addik %0, r0, %3;" \ |
317 | ".previous;" \ | |
318 | __EX_TABLE_SECTION \ | |
319 | ".word 1b,3b;" \ | |
320 | ".previous;" \ | |
8b651aa4 MS |
321 | : "=&r"(__gu_err) \ |
322 | : "r"(__gu_val), "r"(__gu_ptr), "i"(-EFAULT) \ | |
c77a9c4b | 323 | ); \ |
0d6de953 MS |
324 | }) |
325 | ||
8b651aa4 MS |
326 | #define __put_user_asm_8(__gu_ptr, __gu_val, __gu_err) \ |
327 | ({ \ | |
328 | __asm__ __volatile__ (" lwi %0, %1, 0;" \ | |
329 | "1: swi %0, %2, 0;" \ | |
330 | " lwi %0, %1, 4;" \ | |
331 | "2: swi %0, %2, 4;" \ | |
332 | " addk %0, r0, r0;" \ | |
333 | "3: " \ | |
334 | __FIXUP_SECTION \ | |
335 | "4: brid 3b;" \ | |
336 | " addik %0, r0, %3;" \ | |
337 | ".previous;" \ | |
338 | __EX_TABLE_SECTION \ | |
339 | ".word 1b,4b,2b,4b;" \ | |
340 | ".previous;" \ | |
341 | : "=&r"(__gu_err) \ | |
342 | : "r"(&__gu_val), "r"(__gu_ptr), "i"(-EFAULT) \ | |
343 | ); \ | |
344 | }) | |
c77a9c4b | 345 | |
0d6de953 MS |
346 | #define __put_user(x, ptr) \ |
347 | ({ \ | |
7bcb63b2 | 348 | __typeof__(*(ptr)) volatile __gu_val = (x); \ |
0d6de953 MS |
349 | long __gu_err = 0; \ |
350 | switch (sizeof(__gu_val)) { \ | |
351 | case 1: \ | |
352 | __put_user_asm("sb", (ptr), __gu_val, __gu_err); \ | |
353 | break; \ | |
354 | case 2: \ | |
355 | __put_user_asm("sh", (ptr), __gu_val, __gu_err); \ | |
356 | break; \ | |
357 | case 4: \ | |
358 | __put_user_asm("sw", (ptr), __gu_val, __gu_err); \ | |
359 | break; \ | |
360 | case 8: \ | |
361 | __put_user_asm_8((ptr), __gu_val, __gu_err); \ | |
362 | break; \ | |
363 | default: \ | |
0dcb409d | 364 | /*__gu_err = -EINVAL;*/ __gu_err = __user_bad(); \ |
0d6de953 MS |
365 | } \ |
366 | __gu_err; \ | |
367 | }) | |
368 | ||
8b651aa4 MS |
369 | #define put_user(x, ptr) \ |
370 | ({ \ | |
371 | access_ok(VERIFY_WRITE, (ptr), sizeof(*(ptr))) \ | |
372 | ? __put_user((x), (ptr)) : -EFAULT; \ | |
0d6de953 MS |
373 | }) |
374 | ||
cc5a428b MS |
375 | extern unsigned long __copy_tofrom_user(void __user *to, |
376 | const void __user *from, unsigned long size); | |
377 | ||
95dfbbe4 JW |
378 | #define __copy_from_user(to, from, n) copy_from_user((to), (from), (n)) |
379 | #define __copy_from_user_inatomic(to, from, n) \ | |
380 | copy_from_user((to), (from), (n)) | |
0d6de953 | 381 | |
cc5a428b MS |
382 | #define __copy_to_user(to, from, n) \ |
383 | __copy_tofrom_user((void __user *)(to), \ | |
384 | (__force const void __user *)(from), (n)) | |
0d6de953 MS |
385 | #define __copy_to_user_inatomic(to, from, n) copy_to_user((to), (from), (n)) |
386 | ||
cc5a428b MS |
387 | static inline long copy_to_user(void __user *to, |
388 | const void *from, unsigned long n) | |
389 | { | |
390 | might_sleep(); | |
391 | if (access_ok(VERIFY_WRITE, to, n)) | |
392 | return __copy_to_user(to, from, n); | |
393 | else | |
394 | return n; | |
395 | } | |
396 | ||
0d6de953 MS |
397 | #define copy_from_user(to, from, n) \ |
398 | (access_ok(VERIFY_READ, (from), (n)) ? \ | |
399 | __copy_tofrom_user((__force void __user *)(to), \ | |
400 | (void __user *)(from), (n)) \ | |
401 | : -EFAULT) | |
402 | ||
0d6de953 MS |
403 | extern int __strncpy_user(char *to, const char __user *from, int len); |
404 | extern int __strnlen_user(const char __user *sstr, int len); | |
405 | ||
406 | #define strncpy_from_user(to, from, len) \ | |
407 | (access_ok(VERIFY_READ, from, 1) ? \ | |
408 | __strncpy_user(to, from, len) : -EFAULT) | |
409 | #define strnlen_user(str, len) \ | |
410 | (access_ok(VERIFY_READ, str, 1) ? __strnlen_user(str, len) : 0) | |
2660663f | 411 | |
0d6de953 | 412 | #endif /* CONFIG_MMU */ |
2660663f | 413 | |
95dfbbe4 | 414 | |
2660663f MS |
415 | #endif /* __ASSEMBLY__ */ |
416 | #endif /* __KERNEL__ */ | |
417 | ||
418 | #endif /* _ASM_MICROBLAZE_UACCESS_H */ |