aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/ubicom32/files/arch/ubicom32/include/asm/uaccess.h
blob: eef739d129deefc6734f73de06c99ba94505fdd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
 * arch/ubicom32/include/asm/uaccess.h
 *   User space memory access functions for Ubicom32 architecture.
 *
 * (C) Copyright 2009, Ubicom, Inc.
 *
 * This file is part of the Ubicom32 Linux Kernel Port.
 *
 * The Ubicom32 Linux Kernel Port is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 2 of the
 * License, or (at your option) any later version.
 *
 * The Ubicom32 Linux Kernel Port is distributed in the hope that it
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 * the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Ubicom32 Linux Kernel Port.  If not,
 * see <http://www.gnu.org/licenses/>.
 *
 * Ubicom32 implementation derived from (with many thanks):
 *   arch/m68knommu
 *   arch/blackfin
 *   arch/parisc
 *   arch/alpha
 */
#ifndef _ASM_UBICOM32_UACCESS_H
#define _ASM_UBICOM32_UACCESS_H

/*
 * User space memory access functions
 */
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/string.h>

#include <asm/segment.h>

#define VERIFY_READ	0
#define VERIFY_WRITE	1

/*
 * The exception table consists of pairs of addresses: the first is the
 * address of an instruction that is allowed to fault, and the second is
 * the address at which the program should continue.  No registers are
 * modified, so it is entirely up to the continuation code to figure out
 * what to do.
 *
 * All the routines below use bits of fixup code that are out of line
 * with the main instruction path.  This means when everything is well,
 * we don't even have to jump over them.  Further, they do not intrude
 * on our cache or tlb entries.
 */
struct exception_table_entry
{
	unsigned long insn, fixup;
};

/*
 * Ubicom32 does not currently support the exception table handling.
 */
extern unsigned long search_exception_table(unsigned long);


#if defined(CONFIG_ACCESS_OK_CHECKS_ENABLED)
extern int __access_ok(unsigned long addr, unsigned long size);
#else
static inline int __access_ok(unsigned long addr, unsigned long size)
{
	return 1;
}
#endif
#define access_ok(type, addr, size) \
	likely(__access_ok((unsigned long)(addr), (size)))

/*
 * The following functions do not exist.  They keep callers
 * of put_user and get_user from passing unsupported argument
 * types.  They result in a link time error.
 */
extern int __put_user_bad(void);
extern int __get_user_bad(void);

/*
 * __put_user_no_check()
 *	Put the requested data into the user space verifying the address
 *
 * Careful to not
 * (a) re-use the arguments for side effects (sizeof/typeof is ok)
 * (b) require any knowledge of processes at this stage
 */
#define __put_user_no_check(x, ptr, size)		\
({							\
	int __pu_err = 0;				\
	__typeof__(*(ptr)) __user *__pu_addr = (ptr);	\
	switch (size) {					\
	case 1:						\
	case 2:						\
	case 4:						\
	case 8:						\
		*__pu_addr = (__typeof__(*(ptr)))x;	\
		break;					\
	default:					\
		__pu_err = __put_user_bad();		\
		break;					\
	}						\
	__pu_err;					\
})

/*
 * __put_user_check()
 *	Put the requested data into the user space verifying the address
 *
 * Careful to not
 * (a) re-use the arguments for side effects (sizeof/typeof is ok)
 * (b) require any knowledge of processes at this stage
 *
 * If requested, access_ok() will verify that ptr is a valid user
 * pointer.
 */
#define __put_user_check(x, ptr, size)				\
({								\
	int __pu_err = -EFAULT;					\
	__typeof__(*(ptr)) __user *__pu_addr = (ptr);		\
	if (access_ok(VERIFY_WRITE, __pu_addr, size)) {		\
		__pu_err = 0;					\
		switch (size) {					\
		case 1:						\
		case 2:						\
		case 4:						\
		case 8:						\
			*__pu_addr = (__typeof__(*(ptr)))x;	\
			break;					\
		default:					\
			__pu_err = __put_user_bad();		\
			break;					\
		}						\
	}							\
	__pu_err;						\
})

/*
 * __get_user_no_check()
 *	Read the value at ptr into x.
 *
 * If requested, access_ok() will verify that ptr is a valid user
 * pointer.  If the caller passes a modifying argument for ptr (e.g. x++)
 * this macro will not work.
 */
#define __get_user_no_check(x, ptr, size)			\
({								\
	int __gu_err = 0;					\
	__typeof__((x)) __gu_val = 0;				\
	const __typeof__(*(ptr)) __user *__gu_addr = (ptr);	\
	switch (size) {						\
	case 1:							\
	case 2:							\
	case 4:							\
	case 8:							\
		__gu_val = (__typeof__((x)))*(__gu_addr);	\
		break;						\
	default:						\
		__gu_err = __get_user_bad();			\
		(x) = 0;					\
		break;						\
	}							\
	(x) = __gu_val;						\
	__gu_err;						\
})

/*
 * __get_user_check()
 *	Read the value at ptr into x.
 *
 * If requested, access_ok() will verify that ptr is a valid user
 * pointer.
 */
#define __get_user_check(x, ptr, size)					\
({									\
	int __gu_err = -EFAULT;						\
	__typeof__(x) __gu_val = 0;					\
	const __typeof__(*(ptr)) __user *__gu_addr = (ptr);		\
	if (access_ok(VERIFY_READ, __gu_addr, size)) {			\
		__gu_err = 0;						\
		switch (size) {						\
		case 1:							\
		case 2:							\
		case 4:							\
		case 8:							\
			__gu_val = (__typeof__((x)))*(__gu_addr);	\
			break;						\
		default:						\
			__gu_err = __get_user_bad();			\
			(x) = 0;					\
			break;						\
		}							\
	}								\
	(x) = __gu_val;							\
	__gu_err;							\
})

/*
 * The "xxx" versions are allowed to perform some amount of address
 * space checking.  See access_ok().
 */
#define put_user(x,ptr) \
	__put_user_check((__typeof__(*(ptr)))(x),(ptr), sizeof(*(ptr)))
#define get_user(x,ptr) \
	__get_user_check((x), (ptr), sizeof(*(ptr)))

/*
 * The "__xxx" versions do not do address space checking, useful when
 * doing multiple accesses to the same area (the programmer has to do the
 * checks by hand with "access_ok()")
 */
#define __put_user(x,ptr) \
	__put_user_no_check((__typeof__(*(ptr)))(x),(ptr), sizeof(*(ptr)))
#define __get_user(x,ptr) \
	__get_user_no_check((x), (ptr), sizeof(*(ptr)))

/*
 * __copy_tofrom_user_no_check()
 *	Copy the data either to or from user space.
 *
 * Return the number of bytes NOT copied.
 */
static inline unsigned long
__copy_tofrom_user_no_check(void *to, const void *from, unsigned long n)
{
	memcpy(to, from, n);
	return 0;
}

/*
 * copy_to_user()
 * 	Copy the kernel data to user space.
 *
 * Return the number of bytes that were copied.
 */
static inline unsigned long
copy_to_user(void __user *to, const void *from, unsigned long n)
{
	if (!access_ok(VERIFY_WRITE, to, n)) {
		return n;
	}
	return __copy_tofrom_user_no_check((__force void *)to, from, n);
}

/*
 * copy_from_user()
 * 	Copy the user data to kernel space.
 *
 * Return the number of bytes that were copied.  On error, we zero
 * out the destination.
 */
static inline unsigned long
copy_from_user(void *to, const void __user *from, unsigned long n)
{
	if (!access_ok(VERIFY_READ, from, n)) {
		return n;
	}
	return __copy_tofrom_user_no_check(to, (__force void *)from, n);
}

#define __copy_to_user(to, from, n) \
	__copy_tofrom_user_no_check((__force void *)to, from, n)
#define __copy_from_user(to, from, n) \
	__copy_tofrom_user_no_check(to, (__force void *)from, n)
#define __copy_to_user_inatomic(to, from, n) \
	__copy_tofrom_user_no_check((__force void *)to, from, n)
#define __copy_from_user_inatomic(to, from, n) \
	__copy_tofrom_user_no_check(to, (__force void *)from, n)

#define copy_to_user_ret(to, from, n, retval) \
	({ if (copy_to_user(to, from, n)) return retval; })

#define copy_from_user_ret(to, from, n, retval) \
	({ if (copy_from_user(to, from, n)) return retval; })

/*
 * strncpy_from_user()
 *	Copy a null terminated string from userspace.
 *
 * dst - Destination in kernel space.  The buffer must be at least count.
 * src - Address of string in user space.
 * count - Maximum number of bytes to copy (including the trailing NULL).
 *
 * Returns the length of the string (not including the trailing NULL.  If
 * count is smaller than the length of the string, we copy count bytes
 * and return count.
 *
 */
static inline long strncpy_from_user(char *dst, const __user char *src, long count)
{
	char *tmp;
	if (!access_ok(VERIFY_READ, src, 1)) {
		return -EFAULT;
	}

	strncpy(dst, src, count);
	for (tmp = dst; *tmp && count > 0; tmp++, count--) {
		;
	}
	return(tmp - dst);
}

/*
 * strnlen_user()
 *	Return the size of a string (including the ending 0)
 *
 * Return -EFAULT on exception, a value greater than <n> if too long
 */
static inline long strnlen_user(const __user char *src, long n)
{
	if (!access_ok(VERIFY_READ, src, 1)) {
		return -EFAULT;
	}
	return(strlen(src) + 1);
}

#define strlen_user(str) strnlen_user(str, 32767)

/*
 * __clear_user()
 *	Zero Userspace
 */
static inline unsigned long __clear_user(__user void *to, unsigned long n)
{
	memset(to, 0, n);
	return 0;
}

/*
 * clear_user()
 *	Zero user space (check for valid addresses)
 */
static inline unsigned long clear_user(__user void *to, unsigned long n)
{
	if (!access_ok(VERIFY_WRITE, to, n)) {
		return -EFAULT;
	}
	return __clear_user(to, n);
}

#endif /* _ASM_UBICOM32_UACCESS_H */