2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
15 #ifndef _ASM_TILE_UACCESS_H
16 #define _ASM_TILE_UACCESS_H
19 * User space memory access functions
21 #include <linux/sched.h>
23 #include <asm-generic/uaccess-unaligned.h>
24 #include <asm/processor.h>
28 #define VERIFY_WRITE 1
31 * The fs value determines whether argument validity checking should be
32 * performed or not. If get_fs() == USER_DS, checking is performed, with
33 * get_fs() == KERNEL_DS, checking is bypassed.
35 * For historical reasons, these macros are grossly misnamed.
37 #define MAKE_MM_SEG(a) ((mm_segment_t) { (a) })
39 #define KERNEL_DS MAKE_MM_SEG(-1UL)
40 #define USER_DS MAKE_MM_SEG(PAGE_OFFSET)
42 #define get_ds() (KERNEL_DS)
43 #define get_fs() (current_thread_info()->addr_limit)
44 #define set_fs(x) (current_thread_info()->addr_limit = (x))
46 #define segment_eq(a, b) ((a).seg == (b).seg)
50 * We could allow mapping all 16 MB at 0xfc000000, but we set up a
51 * special hack in arch_setup_additional_pages() to auto-create a mapping
52 * for the first 16 KB, and it would seem strange to have different
53 * user-accessible semantics for memory at 0xfc000000 and above 0xfc004000.
55 static inline int is_arch_mappable_range(unsigned long addr,
58 return (addr >= MEM_USER_INTRPT &&
59 addr < (MEM_USER_INTRPT + INTRPT_SIZE) &&
60 size <= (MEM_USER_INTRPT + INTRPT_SIZE) - addr);
62 #define is_arch_mappable_range is_arch_mappable_range
64 #define is_arch_mappable_range(addr, size) 0
68 * Note that using this definition ignores is_arch_mappable_range(),
69 * so on tilepro code that uses user_addr_max() is constrained not
70 * to reference the tilepro user-interrupt region.
72 #define user_addr_max() (current_thread_info()->addr_limit.seg)
75 * Test whether a block of memory is a valid user space address.
76 * Returns 0 if the range is valid, nonzero otherwise.
78 int __range_ok(unsigned long addr, unsigned long size);
81 * access_ok: - Checks if a user space pointer is valid
82 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
83 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
84 * to write to a block, it is always safe to read from it.
85 * @addr: User space pointer to start of block to check
86 * @size: Size of block to check
88 * Context: User context only. This function may sleep if pagefaults are
91 * Checks if a pointer to a block of memory in user space is valid.
93 * Returns true (nonzero) if the memory block may be valid, false (zero)
94 * if it is definitely invalid.
96 * Note that, depending on architecture, this function probably just
97 * checks that the pointer is in the user space range - after calling
98 * this function, memory access functions may still return -EFAULT.
100 #define access_ok(type, addr, size) ({ \
101 __chk_user_ptr(addr); \
102 likely(__range_ok((unsigned long)(addr), (size)) == 0); \
106 * The exception table consists of pairs of addresses: the first is the
107 * address of an instruction that is allowed to fault, and the second is
108 * the address at which the program should continue. No registers are
109 * modified, so it is entirely up to the continuation code to figure out
112 * All the routines below use bits of fixup code that are out of line
113 * with the main instruction path. This means when everything is well,
114 * we don't even have to jump over them. Further, they do not intrude
115 * on our cache or tlb entries.
118 struct exception_table_entry {
119 unsigned long insn, fixup;
122 extern int fixup_exception(struct pt_regs *regs);
125 * This is a type: either unsigned long, if the argument fits into
126 * that type, or otherwise unsigned long long.
128 #define __inttype(x) \
129 __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
132 * Support macros for __get_user().
133 * Note that __get_user() and __put_user() assume proper alignment.
137 #define _ASM_PTR ".quad"
138 #define _ASM_ALIGN ".align 8"
140 #define _ASM_PTR ".long"
141 #define _ASM_ALIGN ".align 4"
144 #define __get_user_asm(OP, x, ptr, ret) \
145 asm volatile("1: {" #OP " %1, %2; movei %0, 0 }\n" \
146 ".pushsection .fixup,\"ax\"\n" \
147 "0: { movei %1, 0; movei %0, %3 }\n" \
149 ".section __ex_table,\"a\"\n" \
151 _ASM_PTR " 1b, 0b\n" \
154 : "=r" (ret), "=r" (x) \
155 : "r" (ptr), "i" (-EFAULT))
158 #define __get_user_1(x, ptr, ret) __get_user_asm(ld1u, x, ptr, ret)
159 #define __get_user_2(x, ptr, ret) __get_user_asm(ld2u, x, ptr, ret)
160 #define __get_user_4(x, ptr, ret) __get_user_asm(ld4s, x, ptr, ret)
161 #define __get_user_8(x, ptr, ret) __get_user_asm(ld, x, ptr, ret)
163 #define __get_user_1(x, ptr, ret) __get_user_asm(lb_u, x, ptr, ret)
164 #define __get_user_2(x, ptr, ret) __get_user_asm(lh_u, x, ptr, ret)
165 #define __get_user_4(x, ptr, ret) __get_user_asm(lw, x, ptr, ret)
166 #ifdef __LITTLE_ENDIAN
167 #define __lo32(a, b) a
168 #define __hi32(a, b) b
170 #define __lo32(a, b) b
171 #define __hi32(a, b) a
173 #define __get_user_8(x, ptr, ret) \
175 unsigned int __a, __b; \
176 asm volatile("1: { lw %1, %3; addi %2, %3, 4 }\n" \
177 "2: { lw %2, %2; movei %0, 0 }\n" \
178 ".pushsection .fixup,\"ax\"\n" \
179 "0: { movei %1, 0; movei %2, 0 }\n" \
180 "{ movei %0, %4; j 9f }\n" \
181 ".section __ex_table,\"a\"\n" \
187 : "=r" (ret), "=r" (__a), "=&r" (__b) \
188 : "r" (ptr), "i" (-EFAULT)); \
189 (x) = (__force __typeof(x))(__inttype(x)) \
190 (((u64)__hi32(__a, __b) << 32) | \
195 extern int __get_user_bad(void)
196 __attribute__((warning("sizeof __get_user argument not 1, 2, 4 or 8")));
199 * __get_user: - Get a simple variable from user space, with less checking.
200 * @x: Variable to store result.
201 * @ptr: Source address, in user space.
203 * Context: User context only. This function may sleep if pagefaults are
206 * This macro copies a single simple variable from user space to kernel
207 * space. It supports simple types like char and int, but not larger
208 * data types like structures or arrays.
210 * @ptr must have pointer-to-simple-variable type, and the result of
211 * dereferencing @ptr must be assignable to @x without a cast.
213 * Returns zero on success, or -EFAULT on error.
214 * On error, the variable @x is set to zero.
216 * Caller must check the pointer with access_ok() before calling this
219 #define __get_user(x, ptr) \
223 __chk_user_ptr(ptr); \
224 switch (sizeof(*(ptr))) { \
225 case 1: __get_user_1(_x, ptr, __ret); break; \
226 case 2: __get_user_2(_x, ptr, __ret); break; \
227 case 4: __get_user_4(_x, ptr, __ret); break; \
228 case 8: __get_user_8(_x, ptr, __ret); break; \
229 default: __ret = __get_user_bad(); break; \
231 (x) = (typeof(*(ptr))) _x; \
235 /* Support macros for __put_user(). */
237 #define __put_user_asm(OP, x, ptr, ret) \
238 asm volatile("1: {" #OP " %1, %2; movei %0, 0 }\n" \
239 ".pushsection .fixup,\"ax\"\n" \
240 "0: { movei %0, %3; j 9f }\n" \
241 ".section __ex_table,\"a\"\n" \
243 _ASM_PTR " 1b, 0b\n" \
247 : "r" (ptr), "r" (x), "i" (-EFAULT))
250 #define __put_user_1(x, ptr, ret) __put_user_asm(st1, x, ptr, ret)
251 #define __put_user_2(x, ptr, ret) __put_user_asm(st2, x, ptr, ret)
252 #define __put_user_4(x, ptr, ret) __put_user_asm(st4, x, ptr, ret)
253 #define __put_user_8(x, ptr, ret) __put_user_asm(st, x, ptr, ret)
255 #define __put_user_1(x, ptr, ret) __put_user_asm(sb, x, ptr, ret)
256 #define __put_user_2(x, ptr, ret) __put_user_asm(sh, x, ptr, ret)
257 #define __put_user_4(x, ptr, ret) __put_user_asm(sw, x, ptr, ret)
258 #define __put_user_8(x, ptr, ret) \
260 u64 __x = (__force __inttype(x))(x); \
261 int __lo = (int) __x, __hi = (int) (__x >> 32); \
262 asm volatile("1: { sw %1, %2; addi %0, %1, 4 }\n" \
263 "2: { sw %0, %3; movei %0, 0 }\n" \
264 ".pushsection .fixup,\"ax\"\n" \
265 "0: { movei %0, %4; j 9f }\n" \
266 ".section __ex_table,\"a\"\n" \
273 : "r" (ptr), "r" (__lo32(__lo, __hi)), \
274 "r" (__hi32(__lo, __hi)), "i" (-EFAULT)); \
278 extern int __put_user_bad(void)
279 __attribute__((warning("sizeof __put_user argument not 1, 2, 4 or 8")));
282 * __put_user: - Write a simple value into user space, with less checking.
283 * @x: Value to copy to user space.
284 * @ptr: Destination address, in user space.
286 * Context: User context only. This function may sleep if pagefaults are
289 * This macro copies a single simple value from kernel space to user
290 * space. It supports simple types like char and int, but not larger
291 * data types like structures or arrays.
293 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
294 * to the result of dereferencing @ptr.
296 * Caller must check the pointer with access_ok() before calling this
299 * Returns zero on success, or -EFAULT on error.
301 #define __put_user(x, ptr) \
304 typeof(*(ptr)) _x = (x); \
305 __chk_user_ptr(ptr); \
306 switch (sizeof(*(ptr))) { \
307 case 1: __put_user_1(_x, ptr, __ret); break; \
308 case 2: __put_user_2(_x, ptr, __ret); break; \
309 case 4: __put_user_4(_x, ptr, __ret); break; \
310 case 8: __put_user_8(_x, ptr, __ret); break; \
311 default: __ret = __put_user_bad(); break; \
317 * The versions of get_user and put_user without initial underscores
318 * check the address of their arguments to make sure they are not
321 #define put_user(x, ptr) \
323 __typeof__(*(ptr)) __user *__Pu_addr = (ptr); \
324 access_ok(VERIFY_WRITE, (__Pu_addr), sizeof(*(__Pu_addr))) ? \
325 __put_user((x), (__Pu_addr)) : \
329 #define get_user(x, ptr) \
331 __typeof__(*(ptr)) const __user *__Gu_addr = (ptr); \
332 access_ok(VERIFY_READ, (__Gu_addr), sizeof(*(__Gu_addr))) ? \
333 __get_user((x), (__Gu_addr)) : \
334 ((x) = 0, -EFAULT); \
338 * __copy_to_user() - copy data into user space, with less checking.
339 * @to: Destination address, in user space.
340 * @from: Source address, in kernel space.
341 * @n: Number of bytes to copy.
343 * Context: User context only. This function may sleep if pagefaults are
346 * Copy data from kernel space to user space. Caller must check
347 * the specified block with access_ok() before calling this function.
349 * Returns number of bytes that could not be copied.
350 * On success, this will be zero.
352 * An alternate version - __copy_to_user_inatomic() - is designed
353 * to be called from atomic context, typically bracketed by calls
354 * to pagefault_disable() and pagefault_enable().
356 extern unsigned long __must_check __copy_to_user_inatomic(
357 void __user *to, const void *from, unsigned long n);
359 static inline unsigned long __must_check
360 __copy_to_user(void __user *to, const void *from, unsigned long n)
363 return __copy_to_user_inatomic(to, from, n);
366 static inline unsigned long __must_check
367 copy_to_user(void __user *to, const void *from, unsigned long n)
369 if (access_ok(VERIFY_WRITE, to, n))
370 n = __copy_to_user(to, from, n);
375 * __copy_from_user() - copy data from user space, with less checking.
376 * @to: Destination address, in kernel space.
377 * @from: Source address, in user space.
378 * @n: Number of bytes to copy.
380 * Context: User context only. This function may sleep if pagefaults are
383 * Copy data from user space to kernel space. Caller must check
384 * the specified block with access_ok() before calling this function.
386 * Returns number of bytes that could not be copied.
387 * On success, this will be zero.
389 * If some data could not be copied, this function will pad the copied
390 * data to the requested size using zero bytes.
392 * An alternate version - __copy_from_user_inatomic() - is designed
393 * to be called from atomic context, typically bracketed by calls
394 * to pagefault_disable() and pagefault_enable(). This version
395 * does *NOT* pad with zeros.
397 extern unsigned long __must_check __copy_from_user_inatomic(
398 void *to, const void __user *from, unsigned long n);
399 extern unsigned long __must_check __copy_from_user_zeroing(
400 void *to, const void __user *from, unsigned long n);
402 static inline unsigned long __must_check
403 __copy_from_user(void *to, const void __user *from, unsigned long n)
406 return __copy_from_user_zeroing(to, from, n);
409 static inline unsigned long __must_check
410 _copy_from_user(void *to, const void __user *from, unsigned long n)
412 if (access_ok(VERIFY_READ, from, n))
413 n = __copy_from_user(to, from, n);
419 #ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
421 * There are still unprovable places in the generic code as of 2.6.34, so this
422 * option is not really compatible with -Werror, which is more useful in
425 extern void copy_from_user_overflow(void)
426 __compiletime_warning("copy_from_user() size is not provably correct");
428 static inline unsigned long __must_check copy_from_user(void *to,
429 const void __user *from,
432 int sz = __compiletime_object_size(to);
434 if (likely(sz == -1 || sz >= n))
435 n = _copy_from_user(to, from, n);
437 copy_from_user_overflow();
442 #define copy_from_user _copy_from_user
447 * __copy_in_user() - copy data within user space, with less checking.
448 * @to: Destination address, in user space.
449 * @from: Source address, in user space.
450 * @n: Number of bytes to copy.
452 * Context: User context only. This function may sleep if pagefaults are
455 * Copy data from user space to user space. Caller must check
456 * the specified blocks with access_ok() before calling this function.
458 * Returns number of bytes that could not be copied.
459 * On success, this will be zero.
461 extern unsigned long __copy_in_user_inatomic(
462 void __user *to, const void __user *from, unsigned long n);
464 static inline unsigned long __must_check
465 __copy_in_user(void __user *to, const void __user *from, unsigned long n)
468 return __copy_in_user_inatomic(to, from, n);
471 static inline unsigned long __must_check
472 copy_in_user(void __user *to, const void __user *from, unsigned long n)
474 if (access_ok(VERIFY_WRITE, to, n) && access_ok(VERIFY_READ, from, n))
475 n = __copy_in_user(to, from, n);
481 extern long strnlen_user(const char __user *str, long n);
482 extern long strlen_user(const char __user *str);
483 extern long strncpy_from_user(char *dst, const char __user *src, long);
486 * clear_user: - Zero a block of memory in user space.
487 * @mem: Destination address, in user space.
488 * @len: Number of bytes to zero.
490 * Zero a block of memory in user space.
492 * Returns number of bytes that could not be cleared.
493 * On success, this will be zero.
495 extern unsigned long clear_user_asm(void __user *mem, unsigned long len);
496 static inline unsigned long __must_check __clear_user(
497 void __user *mem, unsigned long len)
500 return clear_user_asm(mem, len);
502 static inline unsigned long __must_check clear_user(
503 void __user *mem, unsigned long len)
505 if (access_ok(VERIFY_WRITE, mem, len))
506 return __clear_user(mem, len);
511 * flush_user: - Flush a block of memory in user space from cache.
512 * @mem: Destination address, in user space.
513 * @len: Number of bytes to flush.
515 * Returns number of bytes that could not be flushed.
516 * On success, this will be zero.
518 extern unsigned long flush_user_asm(void __user *mem, unsigned long len);
519 static inline unsigned long __must_check __flush_user(
520 void __user *mem, unsigned long len)
525 retval = flush_user_asm(mem, len);
530 static inline unsigned long __must_check flush_user(
531 void __user *mem, unsigned long len)
533 if (access_ok(VERIFY_WRITE, mem, len))
534 return __flush_user(mem, len);
539 * finv_user: - Flush-inval a block of memory in user space from cache.
540 * @mem: Destination address, in user space.
541 * @len: Number of bytes to invalidate.
543 * Returns number of bytes that could not be flush-invalidated.
544 * On success, this will be zero.
546 extern unsigned long finv_user_asm(void __user *mem, unsigned long len);
547 static inline unsigned long __must_check __finv_user(
548 void __user *mem, unsigned long len)
553 retval = finv_user_asm(mem, len);
557 static inline unsigned long __must_check finv_user(
558 void __user *mem, unsigned long len)
560 if (access_ok(VERIFY_WRITE, mem, len))
561 return __finv_user(mem, len);
565 #endif /* _ASM_TILE_UACCESS_H */