tile: add <asm/word-at-a-time.h> and enable support functions
This change enables the generic strncpy_from_user() and strnlen_user() using word-at-a-time.h. The tile implementation is trivial since both tilepro and tilegx have SIMD operations that do byte-wise comparisons against immediate zero for each byte, and return an 0x01 byte in each position where there is a 0x00 byte. Signed-off-by: Chris Metcalf <cmetcalf@ezchip.com>
This commit is contained in:
@@ -64,6 +64,13 @@ static inline int is_arch_mappable_range(unsigned long addr,
|
||||
#define is_arch_mappable_range(addr, size) 0
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Note that using this definition ignores is_arch_mappable_range(),
|
||||
* so on tilepro code that uses user_addr_max() is constrained not
|
||||
* to reference the tilepro user-interrupt region.
|
||||
*/
|
||||
#define user_addr_max() (current_thread_info()->addr_limit.seg)
|
||||
|
||||
/*
|
||||
* Test whether a block of memory is a valid user space address.
|
||||
* Returns 0 if the range is valid, nonzero otherwise.
|
||||
@@ -465,62 +472,9 @@ copy_in_user(void __user *to, const void __user *from, unsigned long n)
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* strlen_user: - Get the size of a string in user space.
|
||||
* @str: The string to measure.
|
||||
*
|
||||
* Context: User context only. This function may sleep.
|
||||
*
|
||||
* Get the size of a NUL-terminated string in user space.
|
||||
*
|
||||
* Returns the size of the string INCLUDING the terminating NUL.
|
||||
* On exception, returns 0.
|
||||
*
|
||||
* If there is a limit on the length of a valid string, you may wish to
|
||||
* consider using strnlen_user() instead.
|
||||
*/
|
||||
extern long strnlen_user_asm(const char __user *str, long n);
|
||||
static inline long __must_check strnlen_user(const char __user *str, long n)
|
||||
{
|
||||
might_fault();
|
||||
return strnlen_user_asm(str, n);
|
||||
}
|
||||
#define strlen_user(str) strnlen_user(str, LONG_MAX)
|
||||
|
||||
/**
|
||||
* strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
|
||||
* @dst: Destination address, in kernel space. This buffer must be at
|
||||
* least @count bytes long.
|
||||
* @src: Source address, in user space.
|
||||
* @count: Maximum number of bytes to copy, including the trailing NUL.
|
||||
*
|
||||
* Copies a NUL-terminated string from userspace to kernel space.
|
||||
* Caller must check the specified block with access_ok() before calling
|
||||
* this function.
|
||||
*
|
||||
* On success, returns the length of the string (not including the trailing
|
||||
* NUL).
|
||||
*
|
||||
* If access to userspace fails, returns -EFAULT (some data may have been
|
||||
* copied).
|
||||
*
|
||||
* If @count is smaller than the length of the string, copies @count bytes
|
||||
* and returns @count.
|
||||
*/
|
||||
extern long strncpy_from_user_asm(char *dst, const char __user *src, long);
|
||||
static inline long __must_check __strncpy_from_user(
|
||||
char *dst, const char __user *src, long count)
|
||||
{
|
||||
might_fault();
|
||||
return strncpy_from_user_asm(dst, src, count);
|
||||
}
|
||||
static inline long __must_check strncpy_from_user(
|
||||
char *dst, const char __user *src, long count)
|
||||
{
|
||||
if (access_ok(VERIFY_READ, src, 1))
|
||||
return __strncpy_from_user(dst, src, count);
|
||||
return -EFAULT;
|
||||
}
|
||||
extern long strnlen_user(const char __user *str, long n);
|
||||
extern long strlen_user(const char __user *str);
|
||||
extern long strncpy_from_user(char *dst, const char __user *src, long);
|
||||
|
||||
/**
|
||||
* clear_user: - Zero a block of memory in user space.
|
||||
|
36
arch/tile/include/asm/word-at-a-time.h
Normal file
36
arch/tile/include/asm/word-at-a-time.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef _ASM_WORD_AT_A_TIME_H
|
||||
#define _ASM_WORD_AT_A_TIME_H
|
||||
|
||||
#include <asm/byteorder.h>
|
||||
|
||||
struct word_at_a_time { /* unused */ };
|
||||
#define WORD_AT_A_TIME_CONSTANTS {}
|
||||
|
||||
/* Generate 0x01 byte values for non-zero bytes using a SIMD instruction. */
|
||||
static inline unsigned long has_zero(unsigned long val, unsigned long *data,
|
||||
const struct word_at_a_time *c)
|
||||
{
|
||||
#ifdef __tilegx__
|
||||
unsigned long mask = __insn_v1cmpeqi(val, 0);
|
||||
#else /* tilepro */
|
||||
unsigned long mask = __insn_seqib(val, 0);
|
||||
#endif
|
||||
*data = mask;
|
||||
return mask;
|
||||
}
|
||||
|
||||
/* These operations are both nops. */
|
||||
#define prep_zero_mask(val, data, c) (data)
|
||||
#define create_zero_mask(data) (data)
|
||||
|
||||
/* And this operation just depends on endianness. */
|
||||
static inline long find_zero(unsigned long mask)
|
||||
{
|
||||
#ifdef __BIG_ENDIAN
|
||||
return __builtin_clzl(mask) >> 3;
|
||||
#else
|
||||
return __builtin_ctzl(mask) >> 3;
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif /* _ASM_WORD_AT_A_TIME_H */
|
Reference in New Issue
Block a user