blob: 12f5d6851bbcb3e28c7ba5421a849df3cabbf4fd [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>
Mike Frysingerbbc51e92009-10-09 07:34:00 +000020#include <asm/sections.h>
Bryan Wu1394f032007-05-06 14:50:22 -070021
22#define get_ds() (KERNEL_DS)
23#define get_fs() (current_thread_info()->addr_limit)
24
25static inline void set_fs(mm_segment_t fs)
26{
27 current_thread_info()->addr_limit = fs;
28}
29
Michael S. Tsirkin323ae3c2015-01-06 15:11:13 +020030#define segment_eq(a, b) ((a) == (b))
Bryan Wu1394f032007-05-06 14:50:22 -070031
32#define VERIFY_READ 0
33#define VERIFY_WRITE 1
34
Bernd Schmidt3ca32c12007-12-24 12:40:29 +080035#define access_ok(type, addr, size) _access_ok((unsigned long)(addr), (size))
Bryan Wu1394f032007-05-06 14:50:22 -070036
Bryan Wu1394f032007-05-06 14:50:22 -070037/*
38 * The fs value determines whether argument validity checking should be
39 * performed or not. If get_fs() == USER_DS, checking is performed, with
40 * get_fs() == KERNEL_DS, checking is bypassed.
41 */
42
Sonic Zhangbde7db82007-05-21 18:09:34 +080043#ifndef CONFIG_ACCESS_CHECK
Bryan Wu1394f032007-05-06 14:50:22 -070044static inline int _access_ok(unsigned long addr, unsigned long size) { return 1; }
45#else
Bryan Wu1394f032007-05-06 14:50:22 -070046extern int _access_ok(unsigned long addr, unsigned long size);
47#endif
Bryan Wu1394f032007-05-06 14:50:22 -070048
49/*
50 * The exception table consists of pairs of addresses: the first is the
51 * address of an instruction that is allowed to fault, and the second is
52 * the address at which the program should continue. No registers are
53 * modified, so it is entirely up to the continuation code to figure out
54 * what to do.
55 *
56 * All the routines below use bits of fixup code that are out of line
57 * with the main instruction path. This means when everything is well,
58 * we don't even have to jump over them. Further, they do not intrude
59 * on our cache or tlb entries.
60 */
61
62struct exception_table_entry {
63 unsigned long insn, fixup;
64};
65
Bryan Wu1394f032007-05-06 14:50:22 -070066/*
67 * These are the main single-value transfer routines. They automatically
68 * use the right size if we just have the right pointer type.
69 */
70
Michael S. Tsirkin323ae3c2015-01-06 15:11:13 +020071#define put_user(x, p) \
Bryan Wu1394f032007-05-06 14:50:22 -070072 ({ \
73 int _err = 0; \
74 typeof(*(p)) _x = (x); \
Michael S. Tsirkin323ae3c2015-01-06 15:11:13 +020075 typeof(*(p)) __user *_p = (p); \
Bryan Wu1394f032007-05-06 14:50:22 -070076 if (!access_ok(VERIFY_WRITE, _p, sizeof(*(_p)))) {\
77 _err = -EFAULT; \
78 } \
79 else { \
80 switch (sizeof (*(_p))) { \
81 case 1: \
82 __put_user_asm(_x, _p, B); \
83 break; \
84 case 2: \
85 __put_user_asm(_x, _p, W); \
86 break; \
87 case 4: \
88 __put_user_asm(_x, _p, ); \
89 break; \
90 case 8: { \
91 long _xl, _xh; \
Michael S. Tsirkin1734bff2015-01-06 14:37:22 +020092 _xl = ((__force long *)&_x)[0]; \
93 _xh = ((__force long *)&_x)[1]; \
94 __put_user_asm(_xl, ((__force long __user *)_p)+0, );\
95 __put_user_asm(_xh, ((__force long __user *)_p)+1, );\
Bryan Wu1394f032007-05-06 14:50:22 -070096 } break; \
97 default: \
98 _err = __put_user_bad(); \
99 break; \
100 } \
101 } \
102 _err; \
103 })
104
Michael S. Tsirkin323ae3c2015-01-06 15:11:13 +0200105#define __put_user(x, p) put_user(x, p)
Bryan Wu1394f032007-05-06 14:50:22 -0700106static inline int bad_user_access_length(void)
107{
108 panic("bad_user_access_length");
109 return -1;
110}
111
112#define __put_user_bad() (printk(KERN_INFO "put_user_bad %s:%d %s\n",\
Harvey Harrisonb85d8582008-04-23 09:39:01 +0800113 __FILE__, __LINE__, __func__),\
Bryan Wu1394f032007-05-06 14:50:22 -0700114 bad_user_access_length(), (-EFAULT))
115
116/*
117 * Tell gcc we read from memory instead of writing: this is because
118 * we do not write to any memory gcc knows about, so there are no
119 * aliasing issues.
120 */
121
Lars-Peter Clausenaff06632012-10-22 16:09:03 +0200122#define __ptr(x) ((unsigned long __force *)(x))
Bryan Wu1394f032007-05-06 14:50:22 -0700123
Michael S. Tsirkin323ae3c2015-01-06 15:11:13 +0200124#define __put_user_asm(x, p, bhw) \
Bryan Wu1394f032007-05-06 14:50:22 -0700125 __asm__ (#bhw"[%1] = %0;\n\t" \
126 : /* no outputs */ \
Michael S. Tsirkin323ae3c2015-01-06 15:11:13 +0200127 :"d" (x), "a" (__ptr(p)) : "memory")
Bryan Wu1394f032007-05-06 14:50:22 -0700128
Mike Frysinger5ff294f2008-11-18 17:48:22 +0800129#define get_user(x, ptr) \
130({ \
131 int _err = 0; \
132 unsigned long _val = 0; \
133 const typeof(*(ptr)) __user *_p = (ptr); \
134 const size_t ptr_size = sizeof(*(_p)); \
135 if (likely(access_ok(VERIFY_READ, _p, ptr_size))) { \
136 BUILD_BUG_ON(ptr_size >= 8); \
137 switch (ptr_size) { \
138 case 1: \
Michael S. Tsirkin323ae3c2015-01-06 15:11:13 +0200139 __get_user_asm(_val, _p, B, (Z)); \
Mike Frysinger5ff294f2008-11-18 17:48:22 +0800140 break; \
141 case 2: \
Michael S. Tsirkin323ae3c2015-01-06 15:11:13 +0200142 __get_user_asm(_val, _p, W, (Z)); \
Mike Frysinger5ff294f2008-11-18 17:48:22 +0800143 break; \
144 case 4: \
145 __get_user_asm(_val, _p, , ); \
146 break; \
147 } \
148 } else \
149 _err = -EFAULT; \
Michael S. Tsirkinf5e0c472014-12-12 01:56:04 +0200150 x = (__force typeof(*(ptr)))_val; \
Mike Frysinger5ff294f2008-11-18 17:48:22 +0800151 _err; \
152})
Bryan Wu1394f032007-05-06 14:50:22 -0700153
Michael S. Tsirkin323ae3c2015-01-06 15:11:13 +0200154#define __get_user(x, p) get_user(x, p)
Bryan Wu1394f032007-05-06 14:50:22 -0700155
156#define __get_user_bad() (bad_user_access_length(), (-EFAULT))
157
Mike Frysinger5ff294f2008-11-18 17:48:22 +0800158#define __get_user_asm(x, ptr, bhw, option) \
159({ \
160 __asm__ __volatile__ ( \
161 "%0 =" #bhw "[%1]" #option ";" \
162 : "=d" (x) \
163 : "a" (__ptr(ptr))); \
164})
Bryan Wu1394f032007-05-06 14:50:22 -0700165
166#define __copy_from_user(to, from, n) copy_from_user(to, from, n)
167#define __copy_to_user(to, from, n) copy_to_user(to, from, n)
168#define __copy_to_user_inatomic __copy_to_user
169#define __copy_from_user_inatomic __copy_from_user
170
Mike Frysinger75aca612009-01-07 23:14:38 +0800171static inline unsigned long __must_check
172copy_from_user(void *to, const void __user *from, unsigned long n)
Bryan Wu1394f032007-05-06 14:50:22 -0700173{
174 if (access_ok(VERIFY_READ, from, n))
Mikhail Gruzdevc91e09b2011-08-06 10:44:51 +0400175 memcpy(to, (const void __force *)from, n);
Bryan Wu1394f032007-05-06 14:50:22 -0700176 else
177 return n;
178 return 0;
179}
180
Mike Frysinger75aca612009-01-07 23:14:38 +0800181static inline unsigned long __must_check
Mikhail Gruzdevc91e09b2011-08-06 10:44:51 +0400182copy_to_user(void __user *to, const void *from, unsigned long n)
Bryan Wu1394f032007-05-06 14:50:22 -0700183{
184 if (access_ok(VERIFY_WRITE, to, n))
Mikhail Gruzdevc91e09b2011-08-06 10:44:51 +0400185 memcpy((void __force *)to, from, n);
Bryan Wu1394f032007-05-06 14:50:22 -0700186 else
187 return n;
Sonic Zhang293be8d2013-01-31 16:34:53 +0800188 SSYNC();
Bryan Wu1394f032007-05-06 14:50:22 -0700189 return 0;
190}
191
192/*
193 * Copy a null terminated string from userspace.
194 */
195
Mike Frysinger75aca612009-01-07 23:14:38 +0800196static inline long __must_check
Lars-Peter Clausend95dcaa2012-10-17 16:28:02 +0200197strncpy_from_user(char *dst, const char __user *src, long count)
Bryan Wu1394f032007-05-06 14:50:22 -0700198{
199 char *tmp;
200 if (!access_ok(VERIFY_READ, src, 1))
201 return -EFAULT;
Lars-Peter Clausend95dcaa2012-10-17 16:28:02 +0200202 strncpy(dst, (const char __force *)src, count);
Bryan Wu1394f032007-05-06 14:50:22 -0700203 for (tmp = dst; *tmp && count > 0; tmp++, count--) ;
204 return (tmp - dst);
205}
206
207/*
Robin Getza8372b52009-06-04 00:32:59 +0000208 * Get the size of a string in user space.
209 * src: The string to measure
210 * n: The maximum valid length
Bryan Wu1394f032007-05-06 14:50:22 -0700211 *
Robin Getza8372b52009-06-04 00:32:59 +0000212 * Get the size of a NUL-terminated string in user space.
213 *
214 * Returns the size of the string INCLUDING the terminating NUL.
215 * On exception, returns 0.
216 * If the string is too long, returns a value greater than n.
Bryan Wu1394f032007-05-06 14:50:22 -0700217 */
Lars-Peter Clausen2a7e0772012-10-22 16:09:05 +0200218static inline long __must_check strnlen_user(const char __user *src, long n)
Bryan Wu1394f032007-05-06 14:50:22 -0700219{
Robin Getza8372b52009-06-04 00:32:59 +0000220 if (!access_ok(VERIFY_READ, src, 1))
221 return 0;
Lars-Peter Clausen2a7e0772012-10-22 16:09:05 +0200222 return strnlen((const char __force *)src, n) + 1;
Bryan Wu1394f032007-05-06 14:50:22 -0700223}
224
Lars-Peter Clausen2a7e0772012-10-22 16:09:05 +0200225static inline long __must_check strlen_user(const char __user *src)
Robin Getza8372b52009-06-04 00:32:59 +0000226{
227 if (!access_ok(VERIFY_READ, src, 1))
228 return 0;
Lars-Peter Clausen2a7e0772012-10-22 16:09:05 +0200229 return strlen((const char __force *)src) + 1;
Robin Getza8372b52009-06-04 00:32:59 +0000230}
Bryan Wu1394f032007-05-06 14:50:22 -0700231
232/*
233 * Zero Userspace
234 */
235
Mike Frysinger75aca612009-01-07 23:14:38 +0800236static inline unsigned long __must_check
Lars-Peter Clausen10dc42b2012-10-22 16:09:04 +0200237__clear_user(void __user *to, unsigned long n)
Bryan Wu1394f032007-05-06 14:50:22 -0700238{
Robin Getza8372b52009-06-04 00:32:59 +0000239 if (!access_ok(VERIFY_WRITE, to, n))
240 return n;
Lars-Peter Clausen10dc42b2012-10-22 16:09:04 +0200241 memset((void __force *)to, 0, n);
Bryan Wu1394f032007-05-06 14:50:22 -0700242 return 0;
243}
244
245#define clear_user(to, n) __clear_user(to, n)
246
Mike Frysingere56e03b2009-06-07 16:31:52 -0400247/* How to interpret these return values:
248 * CORE: can be accessed by core load or dma memcpy
249 * CORE_ONLY: can only be accessed by core load
250 * DMA: can only be accessed by dma memcpy
251 * IDMA: can only be accessed by interprocessor dma memcpy (BF561)
252 * ITEST: can be accessed by isram memcpy or dma memcpy
253 */
254enum {
255 BFIN_MEM_ACCESS_CORE = 0,
256 BFIN_MEM_ACCESS_CORE_ONLY,
257 BFIN_MEM_ACCESS_DMA,
258 BFIN_MEM_ACCESS_IDMA,
259 BFIN_MEM_ACCESS_ITEST,
260};
261/**
262 * bfin_mem_access_type() - what kind of memory access is required
263 * @addr: the address to check
264 * @size: number of bytes needed
265 * @return: <0 is error, >=0 is BFIN_MEM_ACCESS_xxx enum (see above)
266 */
267int bfin_mem_access_type(unsigned long addr, unsigned long size);
268
Bryan Wu1394f032007-05-06 14:50:22 -0700269#endif /* _BLACKFIN_UACCESS_H */