2 * Copyright 2004-2009 Analog Devices Inc.
4 * Licensed under the GPL-2 or later.
6 * Based on: include/asm-m68knommu/uaccess.h
9 #ifndef __BLACKFIN_UACCESS_H
10 #define __BLACKFIN_UACCESS_H
13 * User space memory access functions
16 #include <linux/string.h>
18 #include <asm/segment.h>
19 #include <asm/sections.h>
21 #define get_ds() (KERNEL_DS)
22 #define get_fs() (current_thread_info()->addr_limit)
24 static inline void set_fs(mm_segment_t fs
)
26 current_thread_info()->addr_limit
= fs
;
29 #define segment_eq(a, b) ((a) == (b))
31 #define access_ok(type, addr, size) _access_ok((unsigned long)(addr), (size))
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.
39 #ifndef CONFIG_ACCESS_CHECK
40 static inline int _access_ok(unsigned long addr
, unsigned long size
) { return 1; }
42 extern int _access_ok(unsigned long addr
, unsigned long size
);
45 #include <asm/extable.h>
48 * These are the main single-value transfer routines. They automatically
49 * use the right size if we just have the right pointer type.
52 #define put_user(x, p) \
55 typeof(*(p)) _x = (x); \
56 typeof(*(p)) __user *_p = (p); \
57 if (!access_ok(VERIFY_WRITE, _p, sizeof(*(_p)))) {\
61 switch (sizeof (*(_p))) { \
63 __put_user_asm(_x, _p, B); \
66 __put_user_asm(_x, _p, W); \
69 __put_user_asm(_x, _p, ); \
73 _xl = ((__force long *)&_x)[0]; \
74 _xh = ((__force long *)&_x)[1]; \
75 __put_user_asm(_xl, ((__force long __user *)_p)+0, );\
76 __put_user_asm(_xh, ((__force long __user *)_p)+1, );\
79 _err = __put_user_bad(); \
86 #define __put_user(x, p) put_user(x, p)
87 static inline int bad_user_access_length(void)
89 panic("bad_user_access_length");
93 #define __put_user_bad() (printk(KERN_INFO "put_user_bad %s:%d %s\n",\
94 __FILE__, __LINE__, __func__),\
95 bad_user_access_length(), (-EFAULT))
98 * Tell gcc we read from memory instead of writing: this is because
99 * we do not write to any memory gcc knows about, so there are no
103 #define __ptr(x) ((unsigned long __force *)(x))
105 #define __put_user_asm(x, p, bhw) \
106 __asm__ (#bhw"[%1] = %0;\n\t" \
108 :"d" (x), "a" (__ptr(p)) : "memory")
110 #define get_user(x, ptr) \
113 unsigned long _val = 0; \
114 const typeof(*(ptr)) __user *_p = (ptr); \
115 const size_t ptr_size = sizeof(*(_p)); \
116 if (likely(access_ok(VERIFY_READ, _p, ptr_size))) { \
117 BUILD_BUG_ON(ptr_size >= 8); \
118 switch (ptr_size) { \
120 __get_user_asm(_val, _p, B, (Z)); \
123 __get_user_asm(_val, _p, W, (Z)); \
126 __get_user_asm(_val, _p, , ); \
131 x = (__force typeof(*(ptr)))_val; \
135 #define __get_user(x, p) get_user(x, p)
137 #define __get_user_bad() (bad_user_access_length(), (-EFAULT))
139 #define __get_user_asm(x, ptr, bhw, option) \
141 __asm__ __volatile__ ( \
142 "%0 =" #bhw "[%1]" #option ";" \
144 : "a" (__ptr(ptr))); \
147 static inline unsigned long __must_check
148 raw_copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
150 memcpy(to
, (const void __force
*)from
, n
);
154 static inline unsigned long __must_check
155 raw_copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
157 memcpy((void __force
*)to
, from
, n
);
162 #define INLINE_COPY_FROM_USER
163 #define INLINE_COPY_TO_USER
165 * Copy a null terminated string from userspace.
168 static inline long __must_check
169 strncpy_from_user(char *dst
, const char __user
*src
, long count
)
172 if (!access_ok(VERIFY_READ
, src
, 1))
174 strncpy(dst
, (const char __force
*)src
, count
);
175 for (tmp
= dst
; *tmp
&& count
> 0; tmp
++, count
--) ;
180 * Get the size of a string in user space.
181 * src: The string to measure
182 * n: The maximum valid length
184 * Get the size of a NUL-terminated string in user space.
186 * Returns the size of the string INCLUDING the terminating NUL.
187 * On exception, returns 0.
188 * If the string is too long, returns a value greater than n.
190 static inline long __must_check
strnlen_user(const char __user
*src
, long n
)
192 if (!access_ok(VERIFY_READ
, src
, 1))
194 return strnlen((const char __force
*)src
, n
) + 1;
201 static inline unsigned long __must_check
202 __clear_user(void __user
*to
, unsigned long n
)
204 if (!access_ok(VERIFY_WRITE
, to
, n
))
206 memset((void __force
*)to
, 0, n
);
210 #define clear_user(to, n) __clear_user(to, n)
212 /* How to interpret these return values:
213 * CORE: can be accessed by core load or dma memcpy
214 * CORE_ONLY: can only be accessed by core load
215 * DMA: can only be accessed by dma memcpy
216 * IDMA: can only be accessed by interprocessor dma memcpy (BF561)
217 * ITEST: can be accessed by isram memcpy or dma memcpy
220 BFIN_MEM_ACCESS_CORE
= 0,
221 BFIN_MEM_ACCESS_CORE_ONLY
,
223 BFIN_MEM_ACCESS_IDMA
,
224 BFIN_MEM_ACCESS_ITEST
,
227 * bfin_mem_access_type() - what kind of memory access is required
228 * @addr: the address to check
229 * @size: number of bytes needed
230 * @return: <0 is error, >=0 is BFIN_MEM_ACCESS_xxx enum (see above)
232 int bfin_mem_access_type(unsigned long addr
, unsigned long size
);
234 #endif /* _BLACKFIN_UACCESS_H */