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