blob: c03b8532aad398cef7bcfcee584a9765c0bf9dc2 [file] [log] [blame]
Robin Getz96f10502009-09-24 14:11:24 +00001/*
2 * Copyright 2004-2009 Analog Devices Inc.
3 *
4 * Licensed under the GPL-2 or later.
Bryan Wu1394f032007-05-06 14:50:22 -07005 *
6 * Based on: include/asm-m68knommu/uaccess.h
7 */
8
9#ifndef __BLACKFIN_UACCESS_H
10#define __BLACKFIN_UACCESS_H
11
12/*
13 * User space memory access functions
14 */
15#include <linux/sched.h>
16#include <linux/mm.h>
17#include <linux/string.h>
18
19#include <asm/segment.h>
Sonic Zhangbde7db82007-05-21 18:09:34 +080020#ifdef CONFIG_ACCESS_CHECK
Bryan Wu1394f032007-05-06 14:50:22 -070021# include <asm/bfin-global.h>
22#endif
23
24#define get_ds() (KERNEL_DS)
25#define get_fs() (current_thread_info()->addr_limit)
26
27static inline void set_fs(mm_segment_t fs)
28{
29 current_thread_info()->addr_limit = fs;
30}
31
32#define segment_eq(a,b) ((a) == (b))
33
34#define VERIFY_READ 0
35#define VERIFY_WRITE 1
36
Bernd Schmidt3ca32c12007-12-24 12:40:29 +080037#define access_ok(type, addr, size) _access_ok((unsigned long)(addr), (size))
Bryan Wu1394f032007-05-06 14:50:22 -070038
39static inline int is_in_rom(unsigned long addr)
40{
41 /*
42 * What we are really trying to do is determine if addr is
43 * in an allocated kernel memory region. If not then assume
44 * we cannot free it or otherwise de-allocate it. Ideally
45 * we could restrict this to really being in a ROM or flash,
46 * but that would need to be done on a board by board basis,
47 * not globally.
48 */
49 if ((addr < _ramstart) || (addr >= _ramend))
50 return (1);
51
52 /* Default case, not in ROM */
53 return (0);
54}
55
56/*
57 * The fs value determines whether argument validity checking should be
58 * performed or not. If get_fs() == USER_DS, checking is performed, with
59 * get_fs() == KERNEL_DS, checking is bypassed.
60 */
61
Sonic Zhangbde7db82007-05-21 18:09:34 +080062#ifndef CONFIG_ACCESS_CHECK
Bryan Wu1394f032007-05-06 14:50:22 -070063static inline int _access_ok(unsigned long addr, unsigned long size) { return 1; }
64#else
Bryan Wu1394f032007-05-06 14:50:22 -070065extern int _access_ok(unsigned long addr, unsigned long size);
66#endif
Bryan Wu1394f032007-05-06 14:50:22 -070067
68/*
69 * The exception table consists of pairs of addresses: the first is the
70 * address of an instruction that is allowed to fault, and the second is
71 * the address at which the program should continue. No registers are
72 * modified, so it is entirely up to the continuation code to figure out
73 * what to do.
74 *
75 * All the routines below use bits of fixup code that are out of line
76 * with the main instruction path. This means when everything is well,
77 * we don't even have to jump over them. Further, they do not intrude
78 * on our cache or tlb entries.
79 */
80
81struct exception_table_entry {
82 unsigned long insn, fixup;
83};
84
Bryan Wu1394f032007-05-06 14:50:22 -070085/*
86 * These are the main single-value transfer routines. They automatically
87 * use the right size if we just have the right pointer type.
88 */
89
90#define put_user(x,p) \
91 ({ \
92 int _err = 0; \
93 typeof(*(p)) _x = (x); \
94 typeof(*(p)) *_p = (p); \
95 if (!access_ok(VERIFY_WRITE, _p, sizeof(*(_p)))) {\
96 _err = -EFAULT; \
97 } \
98 else { \
99 switch (sizeof (*(_p))) { \
100 case 1: \
101 __put_user_asm(_x, _p, B); \
102 break; \
103 case 2: \
104 __put_user_asm(_x, _p, W); \
105 break; \
106 case 4: \
107 __put_user_asm(_x, _p, ); \
108 break; \
109 case 8: { \
110 long _xl, _xh; \
111 _xl = ((long *)&_x)[0]; \
112 _xh = ((long *)&_x)[1]; \
113 __put_user_asm(_xl, ((long *)_p)+0, ); \
114 __put_user_asm(_xh, ((long *)_p)+1, ); \
115 } break; \
116 default: \
117 _err = __put_user_bad(); \
118 break; \
119 } \
120 } \
121 _err; \
122 })
123
124#define __put_user(x,p) put_user(x,p)
125static inline int bad_user_access_length(void)
126{
127 panic("bad_user_access_length");
128 return -1;
129}
130
131#define __put_user_bad() (printk(KERN_INFO "put_user_bad %s:%d %s\n",\
Harvey Harrisonb85d8582008-04-23 09:39:01 +0800132 __FILE__, __LINE__, __func__),\
Bryan Wu1394f032007-05-06 14:50:22 -0700133 bad_user_access_length(), (-EFAULT))
134
135/*
136 * Tell gcc we read from memory instead of writing: this is because
137 * we do not write to any memory gcc knows about, so there are no
138 * aliasing issues.
139 */
140
141#define __ptr(x) ((unsigned long *)(x))
142
143#define __put_user_asm(x,p,bhw) \
144 __asm__ (#bhw"[%1] = %0;\n\t" \
145 : /* no outputs */ \
146 :"d" (x),"a" (__ptr(p)) : "memory")
147
Mike Frysinger5ff294f2008-11-18 17:48:22 +0800148#define get_user(x, ptr) \
149({ \
150 int _err = 0; \
151 unsigned long _val = 0; \
152 const typeof(*(ptr)) __user *_p = (ptr); \
153 const size_t ptr_size = sizeof(*(_p)); \
154 if (likely(access_ok(VERIFY_READ, _p, ptr_size))) { \
155 BUILD_BUG_ON(ptr_size >= 8); \
156 switch (ptr_size) { \
157 case 1: \
158 __get_user_asm(_val, _p, B,(Z)); \
159 break; \
160 case 2: \
161 __get_user_asm(_val, _p, W,(Z)); \
162 break; \
163 case 4: \
164 __get_user_asm(_val, _p, , ); \
165 break; \
166 } \
167 } else \
168 _err = -EFAULT; \
169 x = (typeof(*(ptr)))_val; \
170 _err; \
171})
Bryan Wu1394f032007-05-06 14:50:22 -0700172
173#define __get_user(x,p) get_user(x,p)
174
175#define __get_user_bad() (bad_user_access_length(), (-EFAULT))
176
Mike Frysinger5ff294f2008-11-18 17:48:22 +0800177#define __get_user_asm(x, ptr, bhw, option) \
178({ \
179 __asm__ __volatile__ ( \
180 "%0 =" #bhw "[%1]" #option ";" \
181 : "=d" (x) \
182 : "a" (__ptr(ptr))); \
183})
Bryan Wu1394f032007-05-06 14:50:22 -0700184
185#define __copy_from_user(to, from, n) copy_from_user(to, from, n)
186#define __copy_to_user(to, from, n) copy_to_user(to, from, n)
187#define __copy_to_user_inatomic __copy_to_user
188#define __copy_from_user_inatomic __copy_from_user
189
190#define copy_to_user_ret(to,from,n,retval) ({ if (copy_to_user(to,from,n))\
191 return retval; })
192
193#define copy_from_user_ret(to,from,n,retval) ({ if (copy_from_user(to,from,n))\
194 return retval; })
195
Mike Frysinger75aca612009-01-07 23:14:38 +0800196static inline unsigned long __must_check
197copy_from_user(void *to, const void __user *from, unsigned long n)
Bryan Wu1394f032007-05-06 14:50:22 -0700198{
199 if (access_ok(VERIFY_READ, from, n))
200 memcpy(to, from, n);
201 else
202 return n;
203 return 0;
204}
205
Mike Frysinger75aca612009-01-07 23:14:38 +0800206static inline unsigned long __must_check
207copy_to_user(void *to, const void __user *from, unsigned long n)
Bryan Wu1394f032007-05-06 14:50:22 -0700208{
209 if (access_ok(VERIFY_WRITE, to, n))
210 memcpy(to, from, n);
211 else
212 return n;
213 return 0;
214}
215
216/*
217 * Copy a null terminated string from userspace.
218 */
219
Mike Frysinger75aca612009-01-07 23:14:38 +0800220static inline long __must_check
221strncpy_from_user(char *dst, const char *src, long count)
Bryan Wu1394f032007-05-06 14:50:22 -0700222{
223 char *tmp;
224 if (!access_ok(VERIFY_READ, src, 1))
225 return -EFAULT;
226 strncpy(dst, src, count);
227 for (tmp = dst; *tmp && count > 0; tmp++, count--) ;
228 return (tmp - dst);
229}
230
231/*
Robin Getza8372b52009-06-04 00:32:59 +0000232 * Get the size of a string in user space.
233 * src: The string to measure
234 * n: The maximum valid length
Bryan Wu1394f032007-05-06 14:50:22 -0700235 *
Robin Getza8372b52009-06-04 00:32:59 +0000236 * Get the size of a NUL-terminated string in user space.
237 *
238 * Returns the size of the string INCLUDING the terminating NUL.
239 * On exception, returns 0.
240 * If the string is too long, returns a value greater than n.
Bryan Wu1394f032007-05-06 14:50:22 -0700241 */
Robin Getza8372b52009-06-04 00:32:59 +0000242static inline long __must_check strnlen_user(const char *src, long n)
Bryan Wu1394f032007-05-06 14:50:22 -0700243{
Robin Getza8372b52009-06-04 00:32:59 +0000244 if (!access_ok(VERIFY_READ, src, 1))
245 return 0;
246 return strnlen(src, n) + 1;
Bryan Wu1394f032007-05-06 14:50:22 -0700247}
248
Robin Getza8372b52009-06-04 00:32:59 +0000249static inline long __must_check strlen_user(const char *src)
250{
251 if (!access_ok(VERIFY_READ, src, 1))
252 return 0;
253 return strlen(src) + 1;
254}
Bryan Wu1394f032007-05-06 14:50:22 -0700255
256/*
257 * Zero Userspace
258 */
259
Mike Frysinger75aca612009-01-07 23:14:38 +0800260static inline unsigned long __must_check
261__clear_user(void *to, unsigned long n)
Bryan Wu1394f032007-05-06 14:50:22 -0700262{
Robin Getza8372b52009-06-04 00:32:59 +0000263 if (!access_ok(VERIFY_WRITE, to, n))
264 return n;
Bryan Wu1394f032007-05-06 14:50:22 -0700265 memset(to, 0, n);
266 return 0;
267}
268
269#define clear_user(to, n) __clear_user(to, n)
270
Mike Frysingere56e03b2009-06-07 16:31:52 -0400271/* How to interpret these return values:
272 * CORE: can be accessed by core load or dma memcpy
273 * CORE_ONLY: can only be accessed by core load
274 * DMA: can only be accessed by dma memcpy
275 * IDMA: can only be accessed by interprocessor dma memcpy (BF561)
276 * ITEST: can be accessed by isram memcpy or dma memcpy
277 */
278enum {
279 BFIN_MEM_ACCESS_CORE = 0,
280 BFIN_MEM_ACCESS_CORE_ONLY,
281 BFIN_MEM_ACCESS_DMA,
282 BFIN_MEM_ACCESS_IDMA,
283 BFIN_MEM_ACCESS_ITEST,
284};
285/**
286 * bfin_mem_access_type() - what kind of memory access is required
287 * @addr: the address to check
288 * @size: number of bytes needed
289 * @return: <0 is error, >=0 is BFIN_MEM_ACCESS_xxx enum (see above)
290 */
291int bfin_mem_access_type(unsigned long addr, unsigned long size);
292
Bryan Wu1394f032007-05-06 14:50:22 -0700293#endif /* _BLACKFIN_UACCESS_H */