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 * Test whether a block of memory is a valid user space address.
69 * Returns 0 if the range is valid, nonzero otherwise.
71 int __range_ok(unsigned long addr
, unsigned long size
);
74 * access_ok: - Checks if a user space pointer is valid
75 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
76 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
77 * to write to a block, it is always safe to read from it.
78 * @addr: User space pointer to start of block to check
79 * @size: Size of block to check
81 * Context: User context only. This function may sleep.
83 * Checks if a pointer to a block of memory in user space is valid.
85 * Returns true (nonzero) if the memory block may be valid, false (zero)
86 * if it is definitely invalid.
88 * Note that, depending on architecture, this function probably just
89 * checks that the pointer is in the user space range - after calling
90 * this function, memory access functions may still return -EFAULT.
92 #define access_ok(type, addr, size) ({ \
93 __chk_user_ptr(addr); \
94 likely(__range_ok((unsigned long)(addr), (size)) == 0); \
98 * The exception table consists of pairs of addresses: the first is the
99 * address of an instruction that is allowed to fault, and the second is
100 * the address at which the program should continue. No registers are
101 * modified, so it is entirely up to the continuation code to figure out
104 * All the routines below use bits of fixup code that are out of line
105 * with the main instruction path. This means when everything is well,
106 * we don't even have to jump over them. Further, they do not intrude
107 * on our cache or tlb entries.
110 struct exception_table_entry
{
111 unsigned long insn
, fixup
;
114 extern int fixup_exception(struct pt_regs
*regs
);
117 * We return the __get_user_N function results in a structure,
118 * thus in r0 and r1. If "err" is zero, "val" is the result
119 * of the read; otherwise, "err" is -EFAULT.
121 * We rarely need 8-byte values on a 32-bit architecture, but
122 * we size the structure to accommodate. In practice, for the
123 * the smaller reads, we can zero the high word for free, and
124 * the caller will ignore it by virtue of casting anyway.
127 unsigned long long val
;
132 * FIXME: we should express these as inline extended assembler, since
133 * they're fundamentally just a variable dereference and some
134 * supporting exception_table gunk. Note that (a la i386) we can
135 * extend the copy_to_user and copy_from_user routines to call into
136 * such extended assembler routines, though we will have to use a
137 * different return code in that case (1, 2, or 4, rather than -EFAULT).
139 extern struct __get_user
__get_user_1(const void __user
*);
140 extern struct __get_user
__get_user_2(const void __user
*);
141 extern struct __get_user
__get_user_4(const void __user
*);
142 extern struct __get_user
__get_user_8(const void __user
*);
143 extern int __put_user_1(long, void __user
*);
144 extern int __put_user_2(long, void __user
*);
145 extern int __put_user_4(long, void __user
*);
146 extern int __put_user_8(long long, void __user
*);
148 /* Unimplemented routines to cause linker failures */
149 extern struct __get_user
__get_user_bad(void);
150 extern int __put_user_bad(void);
153 * Careful: we have to cast the result to the type of the pointer
157 * __get_user: - Get a simple variable from user space, with less checking.
158 * @x: Variable to store result.
159 * @ptr: Source address, in user space.
161 * Context: User context only. This function may sleep.
163 * This macro copies a single simple variable from user space to kernel
164 * space. It supports simple types like char and int, but not larger
165 * data types like structures or arrays.
167 * @ptr must have pointer-to-simple-variable type, and the result of
168 * dereferencing @ptr must be assignable to @x without a cast.
170 * Returns zero on success, or -EFAULT on error.
171 * On error, the variable @x is set to zero.
173 * Caller must check the pointer with access_ok() before calling this
176 #define __get_user(x, ptr) \
177 ({ struct __get_user __ret; \
178 __typeof__(*(ptr)) const __user *__gu_addr = (ptr); \
179 __chk_user_ptr(__gu_addr); \
180 switch (sizeof(*(__gu_addr))) { \
182 __ret = __get_user_1(__gu_addr); \
185 __ret = __get_user_2(__gu_addr); \
188 __ret = __get_user_4(__gu_addr); \
191 __ret = __get_user_8(__gu_addr); \
194 __ret = __get_user_bad(); \
197 (x) = (__typeof__(*__gu_addr)) (__typeof__(*__gu_addr - *__gu_addr)) \
203 * __put_user: - Write a simple value into user space, with less checking.
204 * @x: Value to copy to user space.
205 * @ptr: Destination address, in user space.
207 * Context: User context only. This function may sleep.
209 * This macro copies a single simple value from kernel space to user
210 * space. It supports simple types like char and int, but not larger
211 * data types like structures or arrays.
213 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
214 * to the result of dereferencing @ptr.
216 * Caller must check the pointer with access_ok() before calling this
219 * Returns zero on success, or -EFAULT on error.
221 * Implementation note: The "case 8" logic of casting to the type of
222 * the result of subtracting the value from itself is basically a way
223 * of keeping all integer types the same, but casting any pointers to
224 * ptrdiff_t, i.e. also an integer type. This way there are no
225 * questionable casts seen by the compiler on an ILP32 platform.
227 #define __put_user(x, ptr) \
230 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
231 typeof(*__pu_addr) __pu_val = (x); \
232 __chk_user_ptr(__pu_addr); \
233 switch (sizeof(__pu_val)) { \
235 __pu_err = __put_user_1((long)__pu_val, __pu_addr); \
238 __pu_err = __put_user_2((long)__pu_val, __pu_addr); \
241 __pu_err = __put_user_4((long)__pu_val, __pu_addr); \
245 __put_user_8((__typeof__(__pu_val - __pu_val))__pu_val,\
249 __pu_err = __put_user_bad(); \
256 * The versions of get_user and put_user without initial underscores
257 * check the address of their arguments to make sure they are not
260 #define put_user(x, ptr) \
262 __typeof__(*(ptr)) __user *__Pu_addr = (ptr); \
263 access_ok(VERIFY_WRITE, (__Pu_addr), sizeof(*(__Pu_addr))) ? \
264 __put_user((x), (__Pu_addr)) : \
268 #define get_user(x, ptr) \
270 __typeof__(*(ptr)) const __user *__Gu_addr = (ptr); \
271 access_ok(VERIFY_READ, (__Gu_addr), sizeof(*(__Gu_addr))) ? \
272 __get_user((x), (__Gu_addr)) : \
273 ((x) = 0, -EFAULT); \
277 * __copy_to_user() - copy data into user space, with less checking.
278 * @to: Destination address, in user space.
279 * @from: Source address, in kernel space.
280 * @n: Number of bytes to copy.
282 * Context: User context only. This function may sleep.
284 * Copy data from kernel space to user space. Caller must check
285 * the specified block with access_ok() before calling this function.
287 * Returns number of bytes that could not be copied.
288 * On success, this will be zero.
290 * An alternate version - __copy_to_user_inatomic() - is designed
291 * to be called from atomic context, typically bracketed by calls
292 * to pagefault_disable() and pagefault_enable().
294 extern unsigned long __must_check
__copy_to_user_inatomic(
295 void __user
*to
, const void *from
, unsigned long n
);
297 static inline unsigned long __must_check
298 __copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
301 return __copy_to_user_inatomic(to
, from
, n
);
304 static inline unsigned long __must_check
305 copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
307 if (access_ok(VERIFY_WRITE
, to
, n
))
308 n
= __copy_to_user(to
, from
, n
);
313 * __copy_from_user() - copy data from user space, with less checking.
314 * @to: Destination address, in kernel space.
315 * @from: Source address, in user space.
316 * @n: Number of bytes to copy.
318 * Context: User context only. This function may sleep.
320 * Copy data from user space to kernel space. Caller must check
321 * the specified block with access_ok() before calling this function.
323 * Returns number of bytes that could not be copied.
324 * On success, this will be zero.
326 * If some data could not be copied, this function will pad the copied
327 * data to the requested size using zero bytes.
329 * An alternate version - __copy_from_user_inatomic() - is designed
330 * to be called from atomic context, typically bracketed by calls
331 * to pagefault_disable() and pagefault_enable(). This version
332 * does *NOT* pad with zeros.
334 extern unsigned long __must_check
__copy_from_user_inatomic(
335 void *to
, const void __user
*from
, unsigned long n
);
336 extern unsigned long __must_check
__copy_from_user_zeroing(
337 void *to
, const void __user
*from
, unsigned long n
);
339 static inline unsigned long __must_check
340 __copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
343 return __copy_from_user_zeroing(to
, from
, n
);
346 static inline unsigned long __must_check
347 _copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
349 if (access_ok(VERIFY_READ
, from
, n
))
350 n
= __copy_from_user(to
, from
, n
);
356 #ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
358 * There are still unprovable places in the generic code as of 2.6.34, so this
359 * option is not really compatible with -Werror, which is more useful in
362 extern void copy_from_user_overflow(void)
363 __compiletime_warning("copy_from_user() size is not provably correct");
365 static inline unsigned long __must_check
copy_from_user(void *to
,
366 const void __user
*from
,
369 int sz
= __compiletime_object_size(to
);
371 if (likely(sz
== -1 || sz
>= n
))
372 n
= _copy_from_user(to
, from
, n
);
374 copy_from_user_overflow();
379 #define copy_from_user _copy_from_user
384 * __copy_in_user() - copy data within user space, with less checking.
385 * @to: Destination address, in user space.
386 * @from: Source address, in kernel space.
387 * @n: Number of bytes to copy.
389 * Context: User context only. This function may sleep.
391 * Copy data from user space to user space. Caller must check
392 * the specified blocks with access_ok() before calling this function.
394 * Returns number of bytes that could not be copied.
395 * On success, this will be zero.
397 extern unsigned long __copy_in_user_inatomic(
398 void __user
*to
, const void __user
*from
, unsigned long n
);
400 static inline unsigned long __must_check
401 __copy_in_user(void __user
*to
, const void __user
*from
, unsigned long n
)
404 return __copy_in_user_inatomic(to
, from
, n
);
407 static inline unsigned long __must_check
408 copy_in_user(void __user
*to
, const void __user
*from
, unsigned long n
)
410 if (access_ok(VERIFY_WRITE
, to
, n
) && access_ok(VERIFY_READ
, from
, n
))
411 n
= __copy_in_user(to
, from
, n
);
418 * strlen_user: - Get the size of a string in user space.
419 * @str: The string to measure.
421 * Context: User context only. This function may sleep.
423 * Get the size of a NUL-terminated string in user space.
425 * Returns the size of the string INCLUDING the terminating NUL.
426 * On exception, returns 0.
428 * If there is a limit on the length of a valid string, you may wish to
429 * consider using strnlen_user() instead.
431 extern long strnlen_user_asm(const char __user
*str
, long n
);
432 static inline long __must_check
strnlen_user(const char __user
*str
, long n
)
435 return strnlen_user_asm(str
, n
);
437 #define strlen_user(str) strnlen_user(str, LONG_MAX)
440 * strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
441 * @dst: Destination address, in kernel space. This buffer must be at
442 * least @count bytes long.
443 * @src: Source address, in user space.
444 * @count: Maximum number of bytes to copy, including the trailing NUL.
446 * Copies a NUL-terminated string from userspace to kernel space.
447 * Caller must check the specified block with access_ok() before calling
450 * On success, returns the length of the string (not including the trailing
453 * If access to userspace fails, returns -EFAULT (some data may have been
456 * If @count is smaller than the length of the string, copies @count bytes
457 * and returns @count.
459 extern long strncpy_from_user_asm(char *dst
, const char __user
*src
, long);
460 static inline long __must_check
__strncpy_from_user(
461 char *dst
, const char __user
*src
, long count
)
464 return strncpy_from_user_asm(dst
, src
, count
);
466 static inline long __must_check
strncpy_from_user(
467 char *dst
, const char __user
*src
, long count
)
469 if (access_ok(VERIFY_READ
, src
, 1))
470 return __strncpy_from_user(dst
, src
, count
);
475 * clear_user: - Zero a block of memory in user space.
476 * @mem: Destination address, in user space.
477 * @len: Number of bytes to zero.
479 * Zero a block of memory in user space.
481 * Returns number of bytes that could not be cleared.
482 * On success, this will be zero.
484 extern unsigned long clear_user_asm(void __user
*mem
, unsigned long len
);
485 static inline unsigned long __must_check
__clear_user(
486 void __user
*mem
, unsigned long len
)
489 return clear_user_asm(mem
, len
);
491 static inline unsigned long __must_check
clear_user(
492 void __user
*mem
, unsigned long len
)
494 if (access_ok(VERIFY_WRITE
, mem
, len
))
495 return __clear_user(mem
, len
);
500 * flush_user: - Flush a block of memory in user space from cache.
501 * @mem: Destination address, in user space.
502 * @len: Number of bytes to flush.
504 * Returns number of bytes that could not be flushed.
505 * On success, this will be zero.
507 extern unsigned long flush_user_asm(void __user
*mem
, unsigned long len
);
508 static inline unsigned long __must_check
__flush_user(
509 void __user
*mem
, unsigned long len
)
514 retval
= flush_user_asm(mem
, len
);
519 static inline unsigned long __must_check
flush_user(
520 void __user
*mem
, unsigned long len
)
522 if (access_ok(VERIFY_WRITE
, mem
, len
))
523 return __flush_user(mem
, len
);
528 * inv_user: - Invalidate a block of memory in user space from cache.
529 * @mem: Destination address, in user space.
530 * @len: Number of bytes to invalidate.
532 * Returns number of bytes that could not be invalidated.
533 * On success, this will be zero.
535 * Note that on Tile64, the "inv" operation is in fact a
536 * "flush and invalidate", so cache write-backs will occur prior
537 * to the cache being marked invalid.
539 extern unsigned long inv_user_asm(void __user
*mem
, unsigned long len
);
540 static inline unsigned long __must_check
__inv_user(
541 void __user
*mem
, unsigned long len
)
546 retval
= inv_user_asm(mem
, len
);
550 static inline unsigned long __must_check
inv_user(
551 void __user
*mem
, unsigned long len
)
553 if (access_ok(VERIFY_WRITE
, mem
, len
))
554 return __inv_user(mem
, len
);
559 * finv_user: - Flush-inval a block of memory in user space from cache.
560 * @mem: Destination address, in user space.
561 * @len: Number of bytes to invalidate.
563 * Returns number of bytes that could not be flush-invalidated.
564 * On success, this will be zero.
566 extern unsigned long finv_user_asm(void __user
*mem
, unsigned long len
);
567 static inline unsigned long __must_check
__finv_user(
568 void __user
*mem
, unsigned long len
)
573 retval
= finv_user_asm(mem
, len
);
577 static inline unsigned long __must_check
finv_user(
578 void __user
*mem
, unsigned long len
)
580 if (access_ok(VERIFY_WRITE
, mem
, len
))
581 return __finv_user(mem
, len
);
585 #endif /* _ASM_TILE_UACCESS_H */