blob: b079ac31a6712a76928a16aa0019648fabf51f0b [file] [log] [blame]
Michal Simek26606632009-03-27 14:25:23 +01001/*
Michal Simek0d6de952009-05-26 16:30:23 +02002 * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
3 * Copyright (C) 2008-2009 PetaLogix
Michal Simek26606632009-03-27 14:25:23 +01004 * 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>
Michal Simek26606632009-03-27 14:25:23 +010025#include <linux/string.h>
26
27#define VERIFY_READ 0
28#define VERIFY_WRITE 1
29
Michal Simek40db0832010-03-05 15:34:12 +010030/*
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
Michal Simek357bc3c2010-03-05 15:37:57 +010058/*
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 */
70struct exception_table_entry {
71 unsigned long insn, fixup;
72};
Michal Simek40db0832010-03-05 15:34:12 +010073
Michal Simek527bdb52010-03-22 16:02:59 +010074/* Returns 0 if exception not found and fixup otherwise. */
75extern unsigned long search_exception_table(unsigned long);
76
Michal Simek0d6de952009-05-26 16:30:23 +020077#ifndef CONFIG_MMU
78
Michal Simek60a729f2010-03-05 15:49:53 +010079/* Check against bounds of physical memory */
80static inline int ___range_ok(unsigned long addr, unsigned long size)
81{
82 return ((addr < memory_start) ||
83 ((addr + size) > memory_end));
84}
Michal Simek26606632009-03-27 14:25:23 +010085
86#define __range_ok(addr, size) \
87 ___range_ok((unsigned long)(addr), (unsigned long)(size))
88
89#define access_ok(type, addr, size) (__range_ok((addr), (size)) == 0)
Michal Simek40b11562010-03-05 16:50:01 +010090
91#else
92
93/*
94 * Address is valid if:
95 * - "addr", "addr + size" and "size" are all below the limit
96 */
97#define access_ok(type, addr, size) \
98 (get_fs().seg > (((unsigned long)(addr)) | \
99 (size) | ((unsigned long)(addr) + (size))))
100
101/* || printk("access_ok failed for %s at 0x%08lx (size %d), seg 0x%08x\n",
102 type?"WRITE":"READ",addr,size,get_fs().seg)) */
103
104#endif
105
106#ifdef CONFIG_MMU
107# define __FIXUP_SECTION ".section .fixup,\"ax\"\n"
108# define __EX_TABLE_SECTION ".section __ex_table,\"a\"\n"
109#else
110# define __FIXUP_SECTION ".section .discard,\"ax\"\n"
111# define __EX_TABLE_SECTION ".section .discard,\"a\"\n"
112#endif
113
Michal Simek527bdb52010-03-22 16:02:59 +0100114extern unsigned long __copy_tofrom_user(void __user *to,
115 const void __user *from, unsigned long size);
116
117/* Return: number of not copied bytes, i.e. 0 if OK or non-zero if fail. */
118static inline unsigned long __must_check __clear_user(void __user *to,
119 unsigned long n)
120{
121 /* normal memset with two words to __ex_table */
122 __asm__ __volatile__ ( \
123 "1: sb r0, %2, r0;" \
124 " addik %0, %0, -1;" \
125 " bneid %0, 1b;" \
126 " addik %2, %2, 1;" \
127 "2: " \
128 __EX_TABLE_SECTION \
129 ".word 1b,2b;" \
130 ".previous;" \
131 : "=r"(n) \
132 : "0"(n), "r"(to)
133 );
134 return n;
135}
136
137static inline unsigned long __must_check clear_user(void __user *to,
138 unsigned long n)
139{
140 might_sleep();
141 if (unlikely(!access_ok(VERIFY_WRITE, to, n)))
142 return n;
143
144 return __clear_user(to, n);
145}
146
Michal Simek40b11562010-03-05 16:50:01 +0100147#ifndef CONFIG_MMU
Michal Simek26606632009-03-27 14:25:23 +0100148
Michal Simek3a6d7722010-03-08 10:52:24 +0100149extern long __user_bad(void);
Arnd Bergmann838d2402009-05-01 13:36:13 +0000150
Michal Simek3a6d7722010-03-08 10:52:24 +0100151#define __get_user_asm(insn, __gu_ptr, __gu_val, __gu_err) \
152({ \
153 __asm__ __volatile__ ( \
154 "1:" insn " %1, %2, r0;" \
155 " addk %0, r0, r0;" \
156 "2: " \
157 __FIXUP_SECTION \
158 "3: brid 2b;" \
159 " addik %0, r0, %3;" \
160 ".previous;" \
161 __EX_TABLE_SECTION \
162 ".word 1b,3b;" \
163 ".previous;" \
164 : "=&r"(__gu_err), "=r"(__gu_val) \
165 : "r"(__gu_ptr), "i"(-EFAULT) \
166 ); \
Michal Simek0d6de952009-05-26 16:30:23 +0200167})
Michal Simek26606632009-03-27 14:25:23 +0100168
Michal Simek3a6d7722010-03-08 10:52:24 +0100169/**
170 * get_user: - Get a simple variable from user space.
171 * @x: Variable to store result.
172 * @ptr: Source address, in user space.
173 *
174 * Context: User context only. This function may sleep.
175 *
176 * This macro copies a single simple variable from user space to kernel
177 * space. It supports simple types like char and int, but not larger
178 * data types like structures or arrays.
179 *
180 * @ptr must have pointer-to-simple-variable type, and the result of
181 * dereferencing @ptr must be assignable to @x without a cast.
182 *
183 * Returns zero on success, or -EFAULT on error.
184 * On error, the variable @x is set to zero.
185 */
186
187#define __get_user(x, ptr) \
188({ \
189 unsigned long __gu_val; \
190 /*unsigned long __gu_ptr = (unsigned long)(ptr);*/ \
191 long __gu_err; \
192 switch (sizeof(*(ptr))) { \
193 case 1: \
194 __get_user_asm("lbu", (ptr), __gu_val, __gu_err); \
195 break; \
196 case 2: \
197 __get_user_asm("lhu", (ptr), __gu_val, __gu_err); \
198 break; \
199 case 4: \
200 __get_user_asm("lw", (ptr), __gu_val, __gu_err); \
201 break; \
202 default: \
203 /* __gu_val = 0; __gu_err = -EINVAL;*/ __gu_err = __user_bad();\
204 } \
205 x = (__typeof__(*(ptr))) __gu_val; \
206 __gu_err; \
207})
208
209
210#define get_user(x, ptr) \
211({ \
212 access_ok(VERIFY_READ, (ptr), sizeof(*(ptr))) \
213 ? __get_user((x), (ptr)) : -EFAULT; \
214})
215
216/* Undefined function to trigger linker error */
217extern int bad_user_access_length(void);
Michal Simek26606632009-03-27 14:25:23 +0100218
Michal Simek0d6de952009-05-26 16:30:23 +0200219/* FIXME is not there defined __pu_val */
Michal Simek26606632009-03-27 14:25:23 +0100220#define __put_user(var, ptr) \
Michal Simek0d6de952009-05-26 16:30:23 +0200221({ \
222 int __pu_err = 0; \
223 switch (sizeof(*(ptr))) { \
224 case 1: \
225 case 2: \
226 case 4: \
227 *(ptr) = (var); \
228 break; \
229 case 8: { \
230 typeof(*(ptr)) __pu_val = (var); \
231 memcpy(ptr, &__pu_val, sizeof(__pu_val)); \
232 } \
233 break; \
234 default: \
235 __pu_err = __put_user_bad(); \
236 break; \
237 } \
238 __pu_err; \
239})
Michal Simek26606632009-03-27 14:25:23 +0100240
241#define __put_user_bad() (bad_user_access_length(), (-EFAULT))
242
Michal Simek0d6de952009-05-26 16:30:23 +0200243#define put_user(x, ptr) __put_user((x), (ptr))
Michal Simek26606632009-03-27 14:25:23 +0100244
Michal Simek0d6de952009-05-26 16:30:23 +0200245#define copy_to_user(to, from, n) (memcpy((to), (from), (n)), 0)
246#define copy_from_user(to, from, n) (memcpy((to), (from), (n)), 0)
Michal Simek26606632009-03-27 14:25:23 +0100247
Michal Simek0d6de952009-05-26 16:30:23 +0200248#define __copy_to_user(to, from, n) (copy_to_user((to), (from), (n)))
249#define __copy_from_user(to, from, n) (copy_from_user((to), (from), (n)))
250#define __copy_to_user_inatomic(to, from, n) \
251 (__copy_to_user((to), (from), (n)))
252#define __copy_from_user_inatomic(to, from, n) \
253 (__copy_from_user((to), (from), (n)))
Michal Simek26606632009-03-27 14:25:23 +0100254
Michal Simek0d6de952009-05-26 16:30:23 +0200255extern long strncpy_from_user(char *dst, const char *src, long count);
256extern long strnlen_user(const char *src, long count);
Michal Simek26606632009-03-27 14:25:23 +0100257
Michal Simek0d6de952009-05-26 16:30:23 +0200258#else /* CONFIG_MMU */
259
Michal Simek0dcb4092010-03-22 15:46:56 +0100260/* put_user and get_user macros */
261
262extern long __user_bad(void);
Michal Simek0d6de952009-05-26 16:30:23 +0200263
Michal Simek8b651aa2010-03-22 15:25:12 +0100264#define __get_user_asm(insn, __gu_ptr, __gu_val, __gu_err) \
265({ \
266 __asm__ __volatile__ ( \
267 "1:" insn " %1, %2, r0;" \
268 " addk %0, r0, r0;" \
269 "2: " \
270 __FIXUP_SECTION \
271 "3: brid 2b; " \
272 " addik %0, r0, %3;" \
273 ".previous;" \
274 __EX_TABLE_SECTION \
275 ".word 1b,3b;" \
276 ".previous;" \
277 : "=&r"(__gu_err), "=r"(__gu_val) \
278 : "r"(__gu_ptr), "i"(-EFAULT) \
279 ); \
Michal Simek0d6de952009-05-26 16:30:23 +0200280})
281
282#define __get_user(x, ptr) \
283({ \
284 unsigned long __gu_val; \
285 /*unsigned long __gu_ptr = (unsigned long)(ptr);*/ \
286 long __gu_err; \
287 switch (sizeof(*(ptr))) { \
288 case 1: \
289 __get_user_asm("lbu", (ptr), __gu_val, __gu_err); \
290 break; \
291 case 2: \
292 __get_user_asm("lhu", (ptr), __gu_val, __gu_err); \
293 break; \
294 case 4: \
295 __get_user_asm("lw", (ptr), __gu_val, __gu_err); \
296 break; \
297 default: \
Michal Simek0dcb4092010-03-22 15:46:56 +0100298 /* __gu_val = 0; __gu_err = -EINVAL;*/ __gu_err = __user_bad();\
Michal Simek0d6de952009-05-26 16:30:23 +0200299 } \
300 x = (__typeof__(*(ptr))) __gu_val; \
301 __gu_err; \
302})
303
Michal Simek0dcb4092010-03-22 15:46:56 +0100304/**
305 * get_user: - Get a simple variable from user space.
306 * @x: Variable to store result.
307 * @ptr: Source address, in user space.
308 *
309 * Context: User context only. This function may sleep.
310 *
311 * This macro copies a single simple variable from user space to kernel
312 * space. It supports simple types like char and int, but not larger
313 * data types like structures or arrays.
314 *
315 * @ptr must have pointer-to-simple-variable type, and the result of
316 * dereferencing @ptr must be assignable to @x without a cast.
317 *
318 * Returns zero on success, or -EFAULT on error.
319 * On error, the variable @x is set to zero.
320 */
321
Michal Simek8b651aa2010-03-22 15:25:12 +0100322#define get_user(x, ptr) \
323({ \
324 access_ok(VERIFY_READ, (ptr), sizeof(*(ptr))) \
325 ? __get_user((x), (ptr)) : -EFAULT; \
326})
327
Michal Simek0dcb4092010-03-22 15:46:56 +0100328/**
329 * put_user: - Write a simple value into user space.
330 * @x: Value to copy to user space.
331 * @ptr: Destination address, in user space.
332 *
333 * Context: User context only. This function may sleep.
334 *
335 * This macro copies a single simple value from kernel space to user
336 * space. It supports simple types like char and int, but not larger
337 * data types like structures or arrays.
338 *
339 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
340 * to the result of dereferencing @ptr.
341 *
342 * Returns zero on success, or -EFAULT on error.
343 */
344
Michal Simek8b651aa2010-03-22 15:25:12 +0100345#define __put_user_asm(insn, __gu_ptr, __gu_val, __gu_err) \
Michal Simekc77a9c42010-03-05 18:03:53 +0100346({ \
347 __asm__ __volatile__ ( \
348 "1:" insn " %1, %2, r0;" \
349 " addk %0, r0, r0;" \
350 "2: " \
351 __FIXUP_SECTION \
Michal Simek8b651aa2010-03-22 15:25:12 +0100352 "3: brid 2b;" \
Michal Simekc77a9c42010-03-05 18:03:53 +0100353 " addik %0, r0, %3;" \
354 ".previous;" \
355 __EX_TABLE_SECTION \
356 ".word 1b,3b;" \
357 ".previous;" \
Michal Simek8b651aa2010-03-22 15:25:12 +0100358 : "=&r"(__gu_err) \
359 : "r"(__gu_val), "r"(__gu_ptr), "i"(-EFAULT) \
Michal Simekc77a9c42010-03-05 18:03:53 +0100360 ); \
Michal Simek0d6de952009-05-26 16:30:23 +0200361})
362
Michal Simek8b651aa2010-03-22 15:25:12 +0100363#define __put_user_asm_8(__gu_ptr, __gu_val, __gu_err) \
364({ \
365 __asm__ __volatile__ (" lwi %0, %1, 0;" \
366 "1: swi %0, %2, 0;" \
367 " lwi %0, %1, 4;" \
368 "2: swi %0, %2, 4;" \
369 " addk %0, r0, r0;" \
370 "3: " \
371 __FIXUP_SECTION \
372 "4: brid 3b;" \
373 " addik %0, r0, %3;" \
374 ".previous;" \
375 __EX_TABLE_SECTION \
376 ".word 1b,4b,2b,4b;" \
377 ".previous;" \
378 : "=&r"(__gu_err) \
379 : "r"(&__gu_val), "r"(__gu_ptr), "i"(-EFAULT) \
380 ); \
381})
Michal Simekc77a9c42010-03-05 18:03:53 +0100382
Michal Simek0d6de952009-05-26 16:30:23 +0200383#define __put_user(x, ptr) \
384({ \
Michal Simek7bcb63b2009-07-13 16:46:54 +0200385 __typeof__(*(ptr)) volatile __gu_val = (x); \
Michal Simek0d6de952009-05-26 16:30:23 +0200386 long __gu_err = 0; \
387 switch (sizeof(__gu_val)) { \
388 case 1: \
389 __put_user_asm("sb", (ptr), __gu_val, __gu_err); \
390 break; \
391 case 2: \
392 __put_user_asm("sh", (ptr), __gu_val, __gu_err); \
393 break; \
394 case 4: \
395 __put_user_asm("sw", (ptr), __gu_val, __gu_err); \
396 break; \
397 case 8: \
398 __put_user_asm_8((ptr), __gu_val, __gu_err); \
399 break; \
400 default: \
Michal Simek0dcb4092010-03-22 15:46:56 +0100401 /*__gu_err = -EINVAL;*/ __gu_err = __user_bad(); \
Michal Simek0d6de952009-05-26 16:30:23 +0200402 } \
403 __gu_err; \
404})
405
Michal Simek8b651aa2010-03-22 15:25:12 +0100406#define put_user(x, ptr) \
407({ \
408 access_ok(VERIFY_WRITE, (ptr), sizeof(*(ptr))) \
409 ? __put_user((x), (ptr)) : -EFAULT; \
Michal Simek0d6de952009-05-26 16:30:23 +0200410})
411
Michal Simek42706902010-03-22 15:56:32 +0100412#define __copy_from_user(to, from, n) \
413 __copy_tofrom_user((__force void __user *)(to), \
414 (void __user *)(from), (n))
John Williams95dfbbe42009-08-14 12:06:46 +1000415#define __copy_from_user_inatomic(to, from, n) \
416 copy_from_user((to), (from), (n))
Michal Simek0d6de952009-05-26 16:30:23 +0200417
Michal Simek42706902010-03-22 15:56:32 +0100418static inline long copy_from_user(void *to,
419 const void __user *from, unsigned long n)
420{
421 might_sleep();
422 if (access_ok(VERIFY_READ, from, n))
423 return __copy_from_user(to, from, n);
424 else
425 return n;
426}
427
Michal Simekcc5a4282010-03-22 15:52:53 +0100428#define __copy_to_user(to, from, n) \
Michal Simek42706902010-03-22 15:56:32 +0100429 __copy_tofrom_user((void __user *)(to), \
Michal Simekcc5a4282010-03-22 15:52:53 +0100430 (__force const void __user *)(from), (n))
Michal Simek0d6de952009-05-26 16:30:23 +0200431#define __copy_to_user_inatomic(to, from, n) copy_to_user((to), (from), (n))
432
Michal Simekcc5a4282010-03-22 15:52:53 +0100433static inline long copy_to_user(void __user *to,
434 const void *from, unsigned long n)
435{
436 might_sleep();
437 if (access_ok(VERIFY_WRITE, to, n))
438 return __copy_to_user(to, from, n);
439 else
440 return n;
441}
442
Michal Simek0d6de952009-05-26 16:30:23 +0200443extern int __strncpy_user(char *to, const char __user *from, int len);
Michal Simek40e11e32010-03-08 09:38:02 +0100444
445#define __strncpy_from_user __strncpy_user
446
447static inline long
448strncpy_from_user(char *dst, const char __user *src, long count)
449{
450 if (!access_ok(VERIFY_READ, src, 1))
451 return -EFAULT;
452 return __strncpy_from_user(dst, src, count);
453}
454
Michal Simek0d6de952009-05-26 16:30:23 +0200455extern int __strnlen_user(const char __user *sstr, int len);
456
Michal Simek0d6de952009-05-26 16:30:23 +0200457#define strnlen_user(str, len) \
458 (access_ok(VERIFY_READ, str, 1) ? __strnlen_user(str, len) : 0)
459
460#endif /* CONFIG_MMU */
Michal Simek26606632009-03-27 14:25:23 +0100461
Michal Simek26606632009-03-27 14:25:23 +0100462#endif /* __ASSEMBLY__ */
463#endif /* __KERNEL__ */
464
465#endif /* _ASM_MICROBLAZE_UACCESS_H */