summaryrefslogtreecommitdiffstats
path: root/arch/frv/include/asm/uaccess.h
blob: 3ac9a59d65d4129142d06ba9516455e0c5dc96db (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
/* uaccess.h: userspace accessor functions
 *
 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 *
 * This program 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.
 */

#ifndef _ASM_UACCESS_H
#define _ASM_UACCESS_H

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

#define HAVE_ARCH_UNMAPPED_AREA	/* we decide where to put mmaps */

#define __ptr(x) ((unsigned long __force *)(x))

#define VERIFY_READ	0
#define VERIFY_WRITE	1

/*
 * check that a range of addresses falls within the current address limit
 */
static inline int ___range_ok(unsigned long addr, unsigned long size)
{
#ifdef CONFIG_MMU
	int flag = -EFAULT, tmp;

	asm volatile (
		"	addcc	%3,%2,%1,icc0	\n"	/* set C-flag if addr+size>4GB */
		"	subcc.p	%1,%4,gr0,icc1	\n"	/* jump if addr+size>limit */
		"	bc	icc0,#0,0f	\n"
		"	bhi	icc1,#0,0f	\n"
		"	setlos	#0,%0		\n"	/* mark okay */
		"0:				\n"
		: "=r"(flag), "=&r"(tmp)
		: "r"(addr), "r"(size), "r"(get_addr_limit()), "0"(flag)
		);

	return flag;

#else

	if (addr < memory_start ||
	    addr > memory_end ||
	    size > memory_end - memory_start ||
	    addr + size > memory_end)
		return -EFAULT;

	return 0;
#endif
}

#define __range_ok(addr,size) ___range_ok((unsigned long) (addr), (unsigned long) (size))

#define access_ok(type,addr,size) (__range_ok((void __user *)(addr), (size)) == 0)
#define __access_ok(addr,size) (__range_ok((addr), (size)) == 0)

/*
 * 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;
};

/* Returns 0 if exception not found and fixup otherwise.  */
extern unsigned long search_exception_table(unsigned long);


/*
 * These are the main single-value transfer routines.  They automatically
 * use the right size if we just have the right pointer type.
 */
#define __put_user(x, ptr)						\
({									\
	int __pu_err = 0;						\
									\
	typeof(*(ptr)) __pu_val = (x);					\
	__chk_user_ptr(ptr);						\
									\
	switch (sizeof (*(ptr))) {					\
	case 1:								\
		__put_user_asm(__pu_err, __pu_val, ptr, "b", "r");	\
		break;							\
	case 2:								\
		__put_user_asm(__pu_err, __pu_val, ptr, "h", "r");	\
		break;							\
	case 4:								\
		__put_user_asm(__pu_err, __pu_val, ptr, "",  "r");	\
		break;							\
	case 8:								\
		__put_user_asm(__pu_err, __pu_val, ptr, "d", "e");	\
		break;							\
	default:							\
		__pu_err = __put_user_bad();				\
		break;							\
	}								\
	__pu_err;							\
})

#define put_user(x, ptr)			\
({						\
	typeof(*(ptr)) __user *_p = (ptr);	\
	int _e;					\
						\
	_e = __range_ok(_p, sizeof(*_p));	\
	if (_e == 0)				\
		_e = __put_user((x), _p);	\
	_e;					\
})

extern int __put_user_bad(void);

/*
 * Tell gcc we read from memory instead of writing: this is because
 * we do not write to any memory gcc knows about, so there are no
 * aliasing issues.
 */

#ifdef CONFIG_MMU

#define __put_user_asm(err,x,ptr,dsize,constraint)					\
do {											\
	asm volatile("1:	st"dsize"%I1	%2,%M1	\n"				\
		     "2:				\n"				\
		     ".subsection 2			\n"				\
		     "3:	setlos		%3,%0	\n"				\
		     "		bra		2b	\n"				\
		     ".previous				\n"				\
		     ".section __ex_table,\"a\"		\n"				\
		     "		.balign		8	\n"				\
		     "		.long		1b,3b	\n"				\
		     ".previous"							\
		     : "=r" (err)							\
		     : "m" (*__ptr(ptr)), constraint (x), "i"(-EFAULT), "0"(err)	\
		     : "memory");							\
} while (0)

#else

#define __put_user_asm(err,x,ptr,bwl,con)	\
do {						\
	asm("	st"bwl"%I0	%1,%M0	\n"	\
	    "	membar			\n"	\
	    :					\
	    : "m" (*__ptr(ptr)), con (x)	\
	    : "memory");			\
} while (0)

#endif

/*****************************************************************************/
/*
 *
 */
#define __get_user(x, ptr)						\
({									\
	int __gu_err = 0;						\
	__chk_user_ptr(ptr);						\
									\
	switch (sizeof(*(ptr))) {					\
	case 1: {							\
		unsigned char __gu_val;					\
		__get_user_asm(__gu_err, __gu_val, ptr, "ub", "=r");	\
		(x) = *(__force __typeof__(*(ptr)) *) &__gu_val;	\
		break;							\
	}								\
	case 2: {							\
		unsigned short __gu_val;				\
		__get_user_asm(__gu_err, __gu_val, ptr, "uh", "=r");	\
		(x) = *(__force __typeof__(*(ptr)) *) &__gu_val;	\
		break;							\
	}								\
	case 4: {							\
		unsigned int __gu_val;					\
		__get_user_asm(__gu_err, __gu_val, ptr, "", "=r");	\
		(x) = *(__force __typeof__(*(ptr)) *) &__gu_val;	\
		break;							\
	}								\
	case 8: {							\
		unsigned long long __gu_val;				\
		__get_user_asm(__gu_err, __gu_val, ptr, "d", "=e");	\
		(x) = *(__force __typeof__(*(ptr)) *) &__gu_val;	\
		break;							\
	}								\
	default:							\
		__gu_err = __get_user_bad();				\
		break;							\
	}								\
	__gu_err;							\
})

#define get_user(x, ptr)			\
({						\
	const typeof(*(ptr)) __user *_p = (ptr);\
	int _e;					\
						\
	_e = __range_ok(_p, sizeof(*_p));	\
	if (likely(_e == 0))			\
		_e = __get_user((x), _p);	\
	else					\
		(x) = (typeof(x)) 0;		\
	_e;					\
})

extern int __get_user_bad(void);

#ifdef CONFIG_MMU

#define __get_user_asm(err,x,ptr,dtype,constraint)	\
do {							\
	asm("1:		ld"dtype"%I2	%M2,%1	\n"	\
	    "2:					\n"	\
	    ".subsection 2			\n"	\
	    "3:		setlos		%3,%0	\n"	\
	    "		setlos		#0,%1	\n"	\
	    "		bra		2b	\n"	\
	    ".previous				\n"	\
	    ".section __ex_table,\"a\"		\n"	\
	    "		.balign		8	\n"	\
	    "		.long		1b,3b	\n"	\
	    ".previous"					\
	    : "=r" (err), constraint (x)		\
	    : "m" (*__ptr(ptr)), "i"(-EFAULT), "0"(err)	\
	    );						\
} while(0)

#else

#define __get_user_asm(err,x,ptr,bwl,con)	\
	asm("	ld"bwl"%I1	%M1,%0	\n"	\
	    "	membar			\n"	\
	    : con(x)				\
	    : "m" (*__ptr(ptr)))

#endif

/*****************************************************************************/
/*
 *
 */
#define ____force(x) (__force void *)(void __user *)(x)
#ifdef CONFIG_MMU
extern long __memset_user(void *dst, unsigned long count);
extern long __memcpy_user(void *dst, const void *src, unsigned long count);

#define clear_user(dst,count)			__memset_user(____force(dst), (count))
#define __copy_from_user_inatomic(to, from, n)	__memcpy_user((to), ____force(from), (n))
#define __copy_to_user_inatomic(to, from, n)	__memcpy_user(____force(to), (from), (n))

#else

#define clear_user(dst,count)			(memset(____force(dst), 0, (count)), 0)
#define __copy_from_user_inatomic(to, from, n)	(memcpy((to), ____force(from), (n)), 0)
#define __copy_to_user_inatomic(to, from, n)	(memcpy(____force(to), (from), (n)), 0)

#endif

#define __clear_user clear_user

static inline unsigned long __must_check
__copy_to_user(void __user *to, const void *from, unsigned long n)
{
       might_fault();
       return __copy_to_user_inatomic(to, from, n);
}

static inline unsigned long
__copy_from_user(void *to, const void __user *from, unsigned long n)
{
       might_fault();
       return __copy_from_user_inatomic(to, from, n);
}

static inline long copy_from_user(void *to, const void __user *from, unsigned long n)
{
	unsigned long ret = n;

	if (likely(__access_ok(from, n)))
		ret = __copy_from_user(to, from, n);

	if (unlikely(ret != 0))
		memset(to + (n - ret), 0, ret);

	return ret;
}

static inline long copy_to_user(void __user *to, const void *from, unsigned long n)
{
	return likely(__access_ok(to, n)) ? __copy_to_user(to, from, n) : n;
}

extern long strncpy_from_user(char *dst, const char __user *src, long count);
extern long strnlen_user(const char __user *src, long count);

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

extern unsigned long search_exception_table(unsigned long addr);

#endif /* _ASM_UACCESS_H */
OpenPOWER on IntegriCloud