blob: 20b6080269139736529c2c098d87b2db1ba5355c [file] [log] [blame]
Mikael Starvik51533b62005-07-27 11:44:44 -07001/*
2 * User address space access functions.
3 * The non-inlined parts of asm-cris/uaccess.h are here.
4 *
5 * Copyright (C) 2000, 2003 Axis Communications AB.
6 *
7 * Written by Hans-Peter Nilsson.
8 * Pieces used from memcpy, originally by Kenny Ranerup long time ago.
9 */
10
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080011#include <linux/uaccess.h>
Mikael Starvik51533b62005-07-27 11:44:44 -070012
13/* Asm:s have been tweaked (within the domain of correctness) to give
14 satisfactory results for "gcc version 3.2.1 Axis release R53/1.53-v32".
15
16 Check regularly...
17
18 Note that for CRISv32, the PC saved at a bus-fault is the address
19 *at* the faulting instruction, with a special case for instructions
20 in delay slots: then it's the address of the branch. Note also that
21 in contrast to v10, a postincrement in the instruction is *not*
22 performed at a bus-fault; the register is seen having the original
23 value in fault handlers. */
24
25
26/* Copy to userspace. This is based on the memcpy used for
27 kernel-to-kernel copying; see "string.c". */
28
Jesper Nilssondbd3c7e2014-10-07 12:20:47 +020029unsigned long __copy_user(void __user *pdst, const void *psrc, unsigned long pn)
Mikael Starvik51533b62005-07-27 11:44:44 -070030{
31 /* We want the parameters put in special registers.
32 Make sure the compiler is able to make something useful of this.
33 As it is now: r10 -> r13; r11 -> r11 (nop); r12 -> r12 (nop).
34
35 FIXME: Comment for old gcc version. Check.
Simon Arlott49b4ff32007-10-20 01:08:50 +020036 If gcc was alright, it really would need no temporaries, and no
Mikael Starvik51533b62005-07-27 11:44:44 -070037 stack space to save stuff on. */
38
39 register char *dst __asm__ ("r13") = pdst;
40 register const char *src __asm__ ("r11") = psrc;
41 register int n __asm__ ("r12") = pn;
42 register int retn __asm__ ("r10") = 0;
43
44
45 /* When src is aligned but not dst, this makes a few extra needless
46 cycles. I believe it would take as many to check that the
47 re-alignment was unnecessary. */
48 if (((unsigned long) dst & 3) != 0
49 /* Don't align if we wouldn't copy more than a few bytes; so we
50 don't have to check further for overflows. */
51 && n >= 3)
52 {
53 if ((unsigned long) dst & 1)
54 {
55 __asm_copy_to_user_1 (dst, src, retn);
56 n--;
57 }
58
59 if ((unsigned long) dst & 2)
60 {
61 __asm_copy_to_user_2 (dst, src, retn);
62 n -= 2;
63 }
64 }
65
66 /* Movem is dirt cheap. The overheap is low enough to always use the
67 minimum possible block size as the threshold. */
68 if (n >= 44)
69 {
70 /* For large copies we use 'movem'. */
71
72 /* It is not optimal to tell the compiler about clobbering any
73 registers; that will move the saving/restoring of those registers
74 to the function prologue/epilogue, and make non-movem sizes
75 suboptimal. */
76 __asm__ volatile ("\
77 ;; Check that the register asm declaration got right. \n\
78 ;; The GCC manual explicitly says TRT will happen. \n\
79 .ifnc %0%1%2%3,$r13$r11$r12$r10 \n\
80 .err \n\
81 .endif \n\
82 \n\
83 ;; Save the registers we'll use in the movem process \n\
84 ;; on the stack. \n\
85 subq 11*4,$sp \n\
86 movem $r10,[$sp] \n\
87 \n\
88 ;; Now we've got this: \n\
89 ;; r11 - src \n\
90 ;; r13 - dst \n\
91 ;; r12 - n \n\
92 \n\
93 ;; Update n for the first loop \n\
94 subq 44,$r12 \n\
950: \n\
96 movem [$r11+],$r10 \n\
97 subq 44,$r12 \n\
981: bge 0b \n\
99 movem $r10,[$r13+] \n\
1003: \n\
101 addq 44,$r12 ;; compensate for last loop underflowing n \n\
102 \n\
103 ;; Restore registers from stack \n\
104 movem [$sp+],$r10 \n\
1052: \n\
106 .section .fixup,\"ax\" \n\
1074: \n\
108; When failing on any of the 1..44 bytes in a chunk, we adjust back the \n\
109; source pointer and just drop through to the by-16 and by-4 loops to \n\
110; get the correct number of failing bytes. This necessarily means a \n\
111; few extra exceptions, but invalid user pointers shouldn't happen in \n\
112; time-critical code anyway. \n\
113 jump 3b \n\
114 subq 44,$r11 \n\
115 \n\
116 .previous \n\
117 .section __ex_table,\"a\" \n\
118 .dword 1b,4b \n\
119 .previous"
120
121 /* Outputs */ : "=r" (dst), "=r" (src), "=r" (n), "=r" (retn)
122 /* Inputs */ : "0" (dst), "1" (src), "2" (n), "3" (retn));
123
124 }
125
126 while (n >= 16)
127 {
128 __asm_copy_to_user_16 (dst, src, retn);
129 n -= 16;
130 }
131
132 /* Having a separate by-four loops cuts down on cache footprint.
133 FIXME: Test with and without; increasing switch to be 0..15. */
134 while (n >= 4)
135 {
136 __asm_copy_to_user_4 (dst, src, retn);
137 n -= 4;
138 }
139
140 switch (n)
141 {
142 case 0:
143 break;
144 case 1:
145 __asm_copy_to_user_1 (dst, src, retn);
146 break;
147 case 2:
148 __asm_copy_to_user_2 (dst, src, retn);
149 break;
150 case 3:
151 __asm_copy_to_user_3 (dst, src, retn);
152 break;
153 }
154
155 return retn;
156}
Jesper Nilssondbd3c7e2014-10-07 12:20:47 +0200157EXPORT_SYMBOL(__copy_user);
Mikael Starvik51533b62005-07-27 11:44:44 -0700158
Al Virob71f1bf2017-03-19 15:28:30 -0400159/* Copy from user to kernel. The return-value is the number of bytes that were
Mikael Starvik51533b62005-07-27 11:44:44 -0700160 inaccessible. */
Al Virob71f1bf2017-03-19 15:28:30 -0400161unsigned long __copy_user_in(void *pdst, const void __user *psrc,
Jesper Nilssondbd3c7e2014-10-07 12:20:47 +0200162 unsigned long pn)
Mikael Starvik51533b62005-07-27 11:44:44 -0700163{
164 /* We want the parameters put in special registers.
165 Make sure the compiler is able to make something useful of this.
166 As it is now: r10 -> r13; r11 -> r11 (nop); r12 -> r12 (nop).
167
168 FIXME: Comment for old gcc version. Check.
Simon Arlott49b4ff32007-10-20 01:08:50 +0200169 If gcc was alright, it really would need no temporaries, and no
Mikael Starvik51533b62005-07-27 11:44:44 -0700170 stack space to save stuff on. */
171
172 register char *dst __asm__ ("r13") = pdst;
173 register const char *src __asm__ ("r11") = psrc;
174 register int n __asm__ ("r12") = pn;
175 register int retn __asm__ ("r10") = 0;
176
177 /* The best reason to align src is that we then know that a read-fault
178 was for aligned bytes; there's no 1..3 remaining good bytes to
179 pickle. */
180 if (((unsigned long) src & 3) != 0)
181 {
182 if (((unsigned long) src & 1) && n != 0)
183 {
184 __asm_copy_from_user_1 (dst, src, retn);
185 n--;
Al Virode09be32017-03-19 15:25:35 -0400186 if (retn != 0)
187 goto exception;
Mikael Starvik51533b62005-07-27 11:44:44 -0700188 }
189
190 if (((unsigned long) src & 2) && n >= 2)
191 {
192 __asm_copy_from_user_2 (dst, src, retn);
193 n -= 2;
Al Virode09be32017-03-19 15:25:35 -0400194 if (retn != 0)
195 goto exception;
Mikael Starvik51533b62005-07-27 11:44:44 -0700196 }
197
Mikael Starvik51533b62005-07-27 11:44:44 -0700198 }
199
200 /* Movem is dirt cheap. The overheap is low enough to always use the
201 minimum possible block size as the threshold. */
202 if (n >= 44)
203 {
204 /* It is not optimal to tell the compiler about clobbering any
205 registers; that will move the saving/restoring of those registers
206 to the function prologue/epilogue, and make non-movem sizes
207 suboptimal. */
208 __asm__ volatile ("\
209 .ifnc %0%1%2%3,$r13$r11$r12$r10 \n\
210 .err \n\
211 .endif \n\
212 \n\
213 ;; Save the registers we'll use in the movem process \n\
214 ;; on the stack. \n\
215 subq 11*4,$sp \n\
216 movem $r10,[$sp] \n\
217 \n\
218 ;; Now we've got this: \n\
219 ;; r11 - src \n\
220 ;; r13 - dst \n\
221 ;; r12 - n \n\
222 \n\
223 ;; Update n for the first loop \n\
224 subq 44,$r12 \n\
2250: \n\
226 movem [$r11+],$r10 \n\
227 \n\
228 subq 44,$r12 \n\
229 bge 0b \n\
230 movem $r10,[$r13+] \n\
231 \n\
2324: \n\
233 addq 44,$r12 ;; compensate for last loop underflowing n \n\
234 \n\
235 ;; Restore registers from stack \n\
236 movem [$sp+],$r10 \n\
237 .section .fixup,\"ax\" \n\
238 \n\
239;; Do not jump back into the loop if we fail. For some uses, we get a \n\
240;; page fault somewhere on the line. Without checking for page limits, \n\
241;; we don't know where, but we need to copy accurately and keep an \n\
242;; accurate count; not just clear the whole line. To do that, we fall \n\
243;; down in the code below, proceeding with smaller amounts. It should \n\
244;; be kept in mind that we have to cater to code like what at one time \n\
245;; was in fs/super.c: \n\
246;; i = size - copy_from_user((void *)page, data, size); \n\
247;; which would cause repeated faults while clearing the remainder of \n\
248;; the SIZE bytes at PAGE after the first fault. \n\
249;; A caveat here is that we must not fall through from a failing page \n\
250;; to a valid page. \n\
251 \n\
2523: \n\
253 jump 4b ;; Fall through, pretending the fault didn't happen. \n\
254 nop \n\
255 \n\
256 .previous \n\
257 .section __ex_table,\"a\" \n\
258 .dword 0b,3b \n\
259 .previous"
260
261 /* Outputs */ : "=r" (dst), "=r" (src), "=r" (n), "=r" (retn)
262 /* Inputs */ : "0" (dst), "1" (src), "2" (n), "3" (retn));
263 }
264
265 /* Either we directly start copying here, using dword copying in a loop,
266 or we copy as much as possible with 'movem' and then the last block
267 (<44 bytes) is copied here. This will work since 'movem' will have
268 updated src, dst and n. (Except with failing src.)
269
270 Since we want to keep src accurate, we can't use
271 __asm_copy_from_user_N with N != (1, 2, 4); it updates dst and
272 retn, but not src (by design; it's value is ignored elsewhere). */
273
274 while (n >= 4)
275 {
276 __asm_copy_from_user_4 (dst, src, retn);
277 n -= 4;
278
279 if (retn)
Al Virode09be32017-03-19 15:25:35 -0400280 goto exception;
Mikael Starvik51533b62005-07-27 11:44:44 -0700281 }
282
283 /* If we get here, there were no memory read faults. */
284 switch (n)
285 {
286 /* These copies are at least "naturally aligned" (so we don't have
287 to check each byte), due to the src alignment code before the
288 movem loop. The *_3 case *will* get the correct count for retn. */
289 case 0:
290 /* This case deliberately left in (if you have doubts check the
291 generated assembly code). */
292 break;
293 case 1:
294 __asm_copy_from_user_1 (dst, src, retn);
295 break;
296 case 2:
297 __asm_copy_from_user_2 (dst, src, retn);
298 break;
299 case 3:
300 __asm_copy_from_user_3 (dst, src, retn);
301 break;
302 }
303
304 /* If we get here, retn correctly reflects the number of failing
305 bytes. */
306 return retn;
307
Al Virode09be32017-03-19 15:25:35 -0400308exception:
Mikael Starvik51533b62005-07-27 11:44:44 -0700309 return retn + n;
310}
Al Virob71f1bf2017-03-19 15:28:30 -0400311EXPORT_SYMBOL(__copy_user_in);
Mikael Starvik51533b62005-07-27 11:44:44 -0700312
313/* Zero userspace. */
Jesper Nilssondbd3c7e2014-10-07 12:20:47 +0200314unsigned long __do_clear_user(void __user *pto, unsigned long pn)
Mikael Starvik51533b62005-07-27 11:44:44 -0700315{
316 /* We want the parameters put in special registers.
317 Make sure the compiler is able to make something useful of this.
318 As it is now: r10 -> r13; r11 -> r11 (nop); r12 -> r12 (nop).
319
320 FIXME: Comment for old gcc version. Check.
Simon Arlott49b4ff32007-10-20 01:08:50 +0200321 If gcc was alright, it really would need no temporaries, and no
Mikael Starvik51533b62005-07-27 11:44:44 -0700322 stack space to save stuff on. */
323
324 register char *dst __asm__ ("r13") = pto;
325 register int n __asm__ ("r12") = pn;
326 register int retn __asm__ ("r10") = 0;
327
328
329 if (((unsigned long) dst & 3) != 0
330 /* Don't align if we wouldn't copy more than a few bytes. */
331 && n >= 3)
332 {
333 if ((unsigned long) dst & 1)
334 {
335 __asm_clear_1 (dst, retn);
336 n--;
337 }
338
339 if ((unsigned long) dst & 2)
340 {
341 __asm_clear_2 (dst, retn);
342 n -= 2;
343 }
344 }
345
346 /* Decide which copying method to use.
347 FIXME: This number is from the "ordinary" kernel memset. */
348 if (n >= 48)
349 {
350 /* For large clears we use 'movem' */
351
352 /* It is not optimal to tell the compiler about clobbering any
353 call-saved registers; that will move the saving/restoring of
354 those registers to the function prologue/epilogue, and make
355 non-movem sizes suboptimal.
356
357 This method is not foolproof; it assumes that the "asm reg"
358 declarations at the beginning of the function really are used
359 here (beware: they may be moved to temporary registers).
360 This way, we do not have to save/move the registers around into
361 temporaries; we can safely use them straight away.
362
363 If you want to check that the allocation was right; then
364 check the equalities in the first comment. It should say
365 something like "r13=r13, r11=r11, r12=r12". */
366 __asm__ volatile ("\
367 .ifnc %0%1%2,$r13$r12$r10 \n\
368 .err \n\
369 .endif \n\
370 \n\
371 ;; Save the registers we'll clobber in the movem process \n\
372 ;; on the stack. Don't mention them to gcc, it will only be \n\
373 ;; upset. \n\
374 subq 11*4,$sp \n\
375 movem $r10,[$sp] \n\
376 \n\
377 clear.d $r0 \n\
378 clear.d $r1 \n\
379 clear.d $r2 \n\
380 clear.d $r3 \n\
381 clear.d $r4 \n\
382 clear.d $r5 \n\
383 clear.d $r6 \n\
384 clear.d $r7 \n\
385 clear.d $r8 \n\
386 clear.d $r9 \n\
387 clear.d $r10 \n\
388 clear.d $r11 \n\
389 \n\
390 ;; Now we've got this: \n\
391 ;; r13 - dst \n\
392 ;; r12 - n \n\
393 \n\
394 ;; Update n for the first loop \n\
395 subq 12*4,$r12 \n\
3960: \n\
397 subq 12*4,$r12 \n\
3981: \n\
399 bge 0b \n\
400 movem $r11,[$r13+] \n\
401 \n\
402 addq 12*4,$r12 ;; compensate for last loop underflowing n \n\
403 \n\
404 ;; Restore registers from stack \n\
405 movem [$sp+],$r10 \n\
4062: \n\
407 .section .fixup,\"ax\" \n\
4083: \n\
409 movem [$sp],$r10 \n\
410 addq 12*4,$r10 \n\
411 addq 12*4,$r13 \n\
412 movem $r10,[$sp] \n\
413 jump 0b \n\
414 clear.d $r10 \n\
415 \n\
416 .previous \n\
417 .section __ex_table,\"a\" \n\
418 .dword 1b,3b \n\
419 .previous"
420
421 /* Outputs */ : "=r" (dst), "=r" (n), "=r" (retn)
422 /* Inputs */ : "0" (dst), "1" (n), "2" (retn)
423 /* Clobber */ : "r11");
424 }
425
426 while (n >= 16)
427 {
428 __asm_clear_16 (dst, retn);
429 n -= 16;
430 }
431
432 /* Having a separate by-four loops cuts down on cache footprint.
433 FIXME: Test with and without; increasing switch to be 0..15. */
434 while (n >= 4)
435 {
436 __asm_clear_4 (dst, retn);
437 n -= 4;
438 }
439
440 switch (n)
441 {
442 case 0:
443 break;
444 case 1:
445 __asm_clear_1 (dst, retn);
446 break;
447 case 2:
448 __asm_clear_2 (dst, retn);
449 break;
450 case 3:
451 __asm_clear_3 (dst, retn);
452 break;
453 }
454
455 return retn;
456}
Jesper Nilssondbd3c7e2014-10-07 12:20:47 +0200457EXPORT_SYMBOL(__do_clear_user);