summaryrefslogtreecommitdiff
path: root/arch/m32r/lib/usercopy.c
blob: 896cef1aca5f43b8da34b8d9d45b2858f3ce84e5 (plain) (blame)
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
 * User address space access functions.
 * The non inlined parts of asm-m32r/uaccess.h are here.
 *
 * Copyright 1997 Andi Kleen <ak@muc.de>
 * Copyright 1997 Linus Torvalds
 * Copyright 2001, 2002, 2004 Hirokazu Takata
 */
#include <linux/prefetch.h>
#include <linux/string.h>
#include <linux/thread_info.h>
#include <asm/uaccess.h>

unsigned long
__generic_copy_to_user(void __user *to, const void *from, unsigned long n)
{
	prefetch(from);
	if (access_ok(VERIFY_WRITE, to, n))
		__copy_user(to,from,n);
	return n;
}

unsigned long
__generic_copy_from_user(void *to, const void __user *from, unsigned long n)
{
	prefetchw(to);
	if (access_ok(VERIFY_READ, from, n))
		__copy_user_zeroing(to,from,n);
	else
		memset(to, 0, n);
	return n;
}


/*
 * Copy a null terminated string from userspace.
 */

#ifdef CONFIG_ISA_DUAL_ISSUE

#define __do_strncpy_from_user(dst,src,count,res)			\
do {									\
	int __d0, __d1, __d2;						\
	__asm__ __volatile__(						\
		"	beqz	%1, 2f\n"				\
		"	.fillinsn\n"					\
		"0:	ldb	r14, @%3    ||	addi	%3, #1\n"	\
		"	stb	r14, @%4    ||	addi	%4, #1\n"	\
		"	beqz	r14, 1f\n"				\
		"	addi	%1, #-1\n"				\
		"	bnez	%1, 0b\n"				\
		"	.fillinsn\n"					\
		"1:	sub	%0, %1\n"				\
		"	.fillinsn\n"					\
		"2:\n"							\
		".section .fixup,\"ax\"\n"				\
		"	.balign 4\n"					\
		"3:	seth	r14, #high(2b)\n"			\
		"	or3	r14, r14, #low(2b)\n"			\
		"	jmp	r14	    ||	ldi	%0, #%5\n"	\
		".previous\n"						\
		".section __ex_table,\"a\"\n"				\
		"	.balign 4\n"					\
		"	.long 0b,3b\n"					\
		".previous"						\
		: "=&r"(res), "=&r"(count), "=&r" (__d0), "=&r" (__d1),	\
		  "=&r" (__d2)						\
		: "i"(-EFAULT), "0"(count), "1"(count), "3"(src), 	\
		  "4"(dst)						\
		: "r14", "cbit", "memory");				\
} while (0)

#else /* not CONFIG_ISA_DUAL_ISSUE */

#define __do_strncpy_from_user(dst,src,count,res)			\
do {									\
	int __d0, __d1, __d2;						\
	__asm__ __volatile__(						\
		"	beqz	%1, 2f\n"				\
		"	.fillinsn\n"					\
		"0:	ldb	r14, @%3\n"				\
		"	stb	r14, @%4\n"				\
		"	addi	%3, #1\n"				\
		"	addi	%4, #1\n"				\
		"	beqz	r14, 1f\n"				\
		"	addi	%1, #-1\n"				\
		"	bnez	%1, 0b\n"				\
		"	.fillinsn\n"					\
		"1:	sub	%0, %1\n"				\
		"	.fillinsn\n"					\
		"2:\n"							\
		".section .fixup,\"ax\"\n"				\
		"	.balign 4\n"					\
		"3:	ldi	%0, #%5\n"				\
		"	seth	r14, #high(2b)\n"			\
		"	or3	r14, r14, #low(2b)\n"			\
		"	jmp	r14\n"					\
		".previous\n"						\
		".section __ex_table,\"a\"\n"				\
		"	.balign 4\n"					\
		"	.long 0b,3b\n"					\
		".previous"						\
		: "=&r"(res), "=&r"(count), "=&r" (__d0), "=&r" (__d1),	\
		  "=&r" (__d2)						\
		: "i"(-EFAULT), "0"(count), "1"(count), "3"(src),	\
		  "4"(dst)						\
		: "r14", "cbit", "memory");				\
} while (0)

#endif /* CONFIG_ISA_DUAL_ISSUE */

long
__strncpy_from_user(char *dst, const char __user *src, long count)
{
	long res;
	__do_strncpy_from_user(dst, src, count, res);
	return res;
}

long
strncpy_from_user(char *dst, const char __user *src, long count)
{
	long res = -EFAULT;
	if (access_ok(VERIFY_READ, src, 1))
		__do_strncpy_from_user(dst, src, count, res);
	return res;
}


/*
 * Zero Userspace
 */

#ifdef CONFIG_ISA_DUAL_ISSUE

#define __do_clear_user(addr,size)					\
do {									\
	int __dst, __c;							\
  	__asm__ __volatile__(						\
		"	beqz	%1, 9f\n"				\
		"	and3	r14, %0, #3\n"				\
		"	bnez	r14, 2f\n"				\
		"	and3	r14, %1, #3\n"				\
		"	bnez	r14, 2f\n"				\
		"	and3	%1, %1, #3\n"				\
		"	beqz	%2, 2f\n"				\
		"	addi	%0, #-4\n"				\
		"	.fillinsn\n"					\
		"0:	; word clear \n"				\
		"	st	%6, @+%0    ||	addi	%2, #-1\n"	\
		"	bnez	%2, 0b\n"				\
		"	beqz	%1, 9f\n"				\
		"	.fillinsn\n"					\
		"2:	; byte clear \n"				\
		"	stb	%6, @%0	    ||	addi	%1, #-1\n"	\
		"	addi	%0, #1\n"				\
		"	bnez	%1, 2b\n"				\
		"	.fillinsn\n"					\
		"9:\n"							\
		".section .fixup,\"ax\"\n"				\
		"	.balign 4\n"					\
		"4:	slli	%2, #2\n"				\
		"	seth	r14, #high(9b)\n"			\
		"	or3	r14, r14, #low(9b)\n"			\
		"	jmp	r14	    ||	add	%1, %2\n"	\
		".previous\n"						\
		".section __ex_table,\"a\"\n"				\
		"	.balign 4\n"					\
		"	.long 0b,4b\n"					\
		"	.long 2b,9b\n"					\
		".previous\n"						\
		: "=&r"(__dst), "=&r"(size), "=&r"(__c)			\
		: "0"(addr), "1"(size), "2"(size / 4), "r"(0)		\
		: "r14", "cbit", "memory");				\
} while (0)

#else /* not CONFIG_ISA_DUAL_ISSUE */

#define __do_clear_user(addr,size)					\
do {									\
	int __dst, __c;							\
  	__asm__ __volatile__(						\
		"	beqz	%1, 9f\n"				\
		"	and3	r14, %0, #3\n"				\
		"	bnez	r14, 2f\n"				\
		"	and3	r14, %1, #3\n"				\
		"	bnez	r14, 2f\n"				\
		"	and3	%1, %1, #3\n"				\
		"	beqz	%2, 2f\n"				\
		"	addi	%0, #-4\n"				\
		"	.fillinsn\n"					\
		"0:	st	%6, @+%0	; word clear \n"	\
		"	addi	%2, #-1\n"				\
		"	bnez	%2, 0b\n"				\
		"	beqz	%1, 9f\n"				\
		"	.fillinsn\n"					\
		"2:	stb	%6, @%0		; byte clear \n"	\
		"	addi	%1, #-1\n"				\
		"	addi	%0, #1\n"				\
		"	bnez	%1, 2b\n"				\
		"	.fillinsn\n"					\
		"9:\n"							\
		".section .fixup,\"ax\"\n"				\
		"	.balign 4\n"					\
		"4:	slli	%2, #2\n"				\
		"	add	%1, %2\n"				\
		"	seth	r14, #high(9b)\n"			\
		"	or3	r14, r14, #low(9b)\n"			\
		"	jmp	r14\n"					\
		".previous\n"						\
		".section __ex_table,\"a\"\n"				\
		"	.balign 4\n"					\
		"	.long 0b,4b\n"					\
		"	.long 2b,9b\n"					\
		".previous\n"						\
		: "=&r"(__dst), "=&r"(size), "=&r"(__c)			\
		: "0"(addr), "1"(size), "2"(size / 4), "r"(0)		\
		: "r14", "cbit", "memory");				\
} while (0)

#endif /* not CONFIG_ISA_DUAL_ISSUE */

unsigned long
clear_user(void __user *to, unsigned long n)
{
	if (access_ok(VERIFY_WRITE, to, n))
		__do_clear_user(to, n);
	return n;
}

unsigned long
__clear_user(void __user *to, unsigned long n)
{
	__do_clear_user(to, n);
	return n;
}

/*
 * Return the size of a string (including the ending 0)
 *
 * Return 0 on exception, a value greater than N if too long
 */

#ifdef CONFIG_ISA_DUAL_ISSUE

long strnlen_user(const char __user *s, long n)
{
	unsigned long mask = -__addr_ok(s);
	unsigned long res;

	__asm__ __volatile__(
		"	and	%0, %5	    ||	mv	r1, %1\n"
		"	beqz	%0, strnlen_exit\n"
		"	and3	r0, %1, #3\n"
		"	bnez	r0, strnlen_byte_loop\n"
		"	cmpui	%0, #4\n"
		"	bc	strnlen_byte_loop\n"
		"strnlen_word_loop:\n"
		"0:	ld	r0, @%1+\n"
		"	pcmpbz	r0\n"
		"	bc	strnlen_last_bytes_fixup\n"
		"	addi	%0, #-4\n"
		"	beqz	%0, strnlen_exit\n"
		"	bgtz	%0, strnlen_word_loop\n"
		"strnlen_last_bytes:\n"
		"	mv	%0, %4\n"
		"strnlen_last_bytes_fixup:\n"
		"	addi	%1, #-4\n"
		"strnlen_byte_loop:\n"
		"1:	ldb	r0, @%1	    ||	addi	%0, #-1\n"
		"	beqz	r0, strnlen_exit\n"
		"	addi	%1, #1\n"
		"	bnez	%0, strnlen_byte_loop\n"
		"strnlen_exit:\n"
		"	sub	%1, r1\n"
		"	add3	%0, %1, #1\n"
		"	.fillinsn\n"
		"9:\n"
		".section .fixup,\"ax\"\n"
		"	.balign 4\n"
		"4:	addi	%1, #-4\n"
		"	.fillinsn\n"
		"5:	seth	r1, #high(9b)\n"
		"	or3	r1, r1, #low(9b)\n"
		"	jmp	r1	    ||	ldi	%0, #0\n"
		".previous\n"
		".section __ex_table,\"a\"\n"
		"	.balign 4\n"
		"	.long 0b,4b\n"
		"	.long 1b,5b\n"
		".previous"
		: "=&r" (res), "=r" (s)
		: "0" (n), "1" (s), "r" (n & 3), "r" (mask), "r"(0x01010101)
		: "r0", "r1", "cbit");

	/* NOTE: strnlen_user() algorism:
	 * {
	 *   char *p;
	 *   for (p = s; n-- && *p != '\0'; ++p)
	 *     ;
	 *   return p - s + 1;
	 * }
	 */

	/* NOTE: If a null char. exists, return 0.
	 * if ((x - 0x01010101) & ~x & 0x80808080)\n"
	 *   return 0;\n"
	 */

	return res & mask;
}

#else /* not CONFIG_ISA_DUAL_ISSUE */

long strnlen_user(const char __user *s, long n)
{
	unsigned long mask = -__addr_ok(s);
	unsigned long res;

	__asm__ __volatile__(
		"	and	%0, %5\n"
		"	mv	r1, %1\n"
		"	beqz	%0, strnlen_exit\n"
		"	and3	r0, %1, #3\n"
		"	bnez	r0, strnlen_byte_loop\n"
		"	cmpui	%0, #4\n"
		"	bc	strnlen_byte_loop\n"
		"	sll3	r3, %6, #7\n"
		"strnlen_word_loop:\n"
		"0:	ld	r0, @%1+\n"
		"	not	r2, r0\n"
		"	sub	r0, %6\n"
		"	and	r2, r3\n"
		"	and	r2, r0\n"
		"	bnez	r2, strnlen_last_bytes_fixup\n"
		"	addi	%0, #-4\n"
		"	beqz	%0, strnlen_exit\n"
		"	bgtz	%0, strnlen_word_loop\n"
		"strnlen_last_bytes:\n"
		"	mv	%0, %4\n"
		"strnlen_last_bytes_fixup:\n"
		"	addi	%1, #-4\n"
		"strnlen_byte_loop:\n"
		"1:	ldb	r0, @%1\n"
		"	addi	%0, #-1\n"
		"	beqz	r0, strnlen_exit\n"
		"	addi	%1, #1\n"
		"	bnez	%0, strnlen_byte_loop\n"
		"strnlen_exit:\n"
		"	sub	%1, r1\n"
		"	add3	%0, %1, #1\n"
		"	.fillinsn\n"
		"9:\n"
		".section .fixup,\"ax\"\n"
		"	.balign 4\n"
		"4:	addi	%1, #-4\n"
		"	.fillinsn\n"
		"5:	ldi	%0, #0\n"
		"	seth	r1, #high(9b)\n"
		"	or3	r1, r1, #low(9b)\n"
		"	jmp	r1\n"
		".previous\n"
		".section __ex_table,\"a\"\n"
		"	.balign 4\n"
		"	.long 0b,4b\n"
		"	.long 1b,5b\n"
		".previous"
		: "=&r" (res), "=r" (s)
		: "0" (n), "1" (s), "r" (n & 3), "r" (mask), "r"(0x01010101)
		: "r0", "r1", "r2", "r3", "cbit");

	/* NOTE: strnlen_user() algorism:
	 * {
	 *   char *p;
	 *   for (p = s; n-- && *p != '\0'; ++p)
	 *     ;
	 *   return p - s + 1;
	 * }
	 */

	/* NOTE: If a null char. exists, return 0.
	 * if ((x - 0x01010101) & ~x & 0x80808080)\n"
	 *   return 0;\n"
	 */

	return res & mask;
}

#endif /* CONFIG_ISA_DUAL_ISSUE */