1 // SPDX-License-Identifier: GPL-2.0
3 * User address space access functions.
4 * The non-inlined parts of asm-cris/uaccess.h are here.
6 * Copyright (C) 2000, 2003 Axis Communications AB.
8 * Written by Hans-Peter Nilsson.
9 * Pieces used from memcpy, originally by Kenny Ranerup long time ago.
12 #include <linux/uaccess.h>
14 /* Asm:s have been tweaked (within the domain of correctness) to give
15 satisfactory results for "gcc version 3.2.1 Axis release R53/1.53-v32".
19 Note that for CRISv32, the PC saved at a bus-fault is the address
20 *at* the faulting instruction, with a special case for instructions
21 in delay slots: then it's the address of the branch. Note also that
22 in contrast to v10, a postincrement in the instruction is *not*
23 performed at a bus-fault; the register is seen having the original
24 value in fault handlers. */
27 /* Copy to userspace. This is based on the memcpy used for
28 kernel-to-kernel copying; see "string.c". */
30 unsigned long __copy_user(void __user
*pdst
, const void *psrc
, unsigned long pn
)
32 /* We want the parameters put in special registers.
33 Make sure the compiler is able to make something useful of this.
34 As it is now: r10 -> r13; r11 -> r11 (nop); r12 -> r12 (nop).
36 FIXME: Comment for old gcc version. Check.
37 If gcc was alright, it really would need no temporaries, and no
38 stack space to save stuff on. */
40 register char *dst
__asm__ ("r13") = pdst
;
41 register const char *src
__asm__ ("r11") = psrc
;
42 register int n
__asm__ ("r12") = pn
;
43 register int retn
__asm__ ("r10") = 0;
46 /* When src is aligned but not dst, this makes a few extra needless
47 cycles. I believe it would take as many to check that the
48 re-alignment was unnecessary. */
49 if (((unsigned long) dst
& 3) != 0
50 /* Don't align if we wouldn't copy more than a few bytes; so we
51 don't have to check further for overflows. */
54 if ((unsigned long) dst
& 1)
56 __asm_copy_to_user_1 (dst
, src
, retn
);
60 if ((unsigned long) dst
& 2)
62 __asm_copy_to_user_2 (dst
, src
, retn
);
67 /* Movem is dirt cheap. The overheap is low enough to always use the
68 minimum possible block size as the threshold. */
71 /* For large copies we use 'movem'. */
73 /* It is not optimal to tell the compiler about clobbering any
74 registers; that will move the saving/restoring of those registers
75 to the function prologue/epilogue, and make non-movem sizes
78 ;; Check that the register asm declaration got right. \n\
79 ;; The GCC manual explicitly says TRT will happen. \n\
80 .ifnc %0%1%2%3,$r13$r11$r12$r10 \n\
84 ;; Save the registers we'll use in the movem process \n\
89 ;; Now we've got this: \n\
94 ;; Update n for the first loop \n\
97 movem [$r11+],$r10 \n\
100 movem $r10,[$r13+] \n\
102 addq 44,$r12 ;; compensate for last loop underflowing n \n\
104 ;; Restore registers from stack \n\
105 movem [$sp+],$r10 \n\
107 .section .fixup,\"ax\" \n\
109 ; When failing on any of the 1..44 bytes in a chunk, we adjust back the \n\
110 ; source pointer and just drop through to the by-16 and by-4 loops to \n\
111 ; get the correct number of failing bytes. This necessarily means a \n\
112 ; few extra exceptions, but invalid user pointers shouldn't happen in \n\
113 ; time-critical code anyway. \n\
118 .section __ex_table,\"a\" \n\
122 /* Outputs */ : "=r" (dst
), "=r" (src
), "=r" (n
), "=r" (retn
)
123 /* Inputs */ : "0" (dst
), "1" (src
), "2" (n
), "3" (retn
));
129 __asm_copy_to_user_16 (dst
, src
, retn
);
133 /* Having a separate by-four loops cuts down on cache footprint.
134 FIXME: Test with and without; increasing switch to be 0..15. */
137 __asm_copy_to_user_4 (dst
, src
, retn
);
146 __asm_copy_to_user_1 (dst
, src
, retn
);
149 __asm_copy_to_user_2 (dst
, src
, retn
);
152 __asm_copy_to_user_3 (dst
, src
, retn
);
158 EXPORT_SYMBOL(__copy_user
);
160 /* Copy from user to kernel. The return-value is the number of bytes that were
162 unsigned long __copy_user_in(void *pdst
, const void __user
*psrc
,
165 /* We want the parameters put in special registers.
166 Make sure the compiler is able to make something useful of this.
167 As it is now: r10 -> r13; r11 -> r11 (nop); r12 -> r12 (nop).
169 FIXME: Comment for old gcc version. Check.
170 If gcc was alright, it really would need no temporaries, and no
171 stack space to save stuff on. */
173 register char *dst
__asm__ ("r13") = pdst
;
174 register const char *src
__asm__ ("r11") = psrc
;
175 register int n
__asm__ ("r12") = pn
;
176 register int retn
__asm__ ("r10") = 0;
178 /* The best reason to align src is that we then know that a read-fault
179 was for aligned bytes; there's no 1..3 remaining good bytes to
181 if (((unsigned long) src
& 3) != 0)
183 if (((unsigned long) src
& 1) && n
!= 0)
185 __asm_copy_from_user_1 (dst
, src
, retn
);
191 if (((unsigned long) src
& 2) && n
>= 2)
193 __asm_copy_from_user_2 (dst
, src
, retn
);
201 /* Movem is dirt cheap. The overheap is low enough to always use the
202 minimum possible block size as the threshold. */
205 /* It is not optimal to tell the compiler about clobbering any
206 registers; that will move the saving/restoring of those registers
207 to the function prologue/epilogue, and make non-movem sizes
210 .ifnc %0%1%2%3,$r13$r11$r12$r10 \n\
214 ;; Save the registers we'll use in the movem process \n\
219 ;; Now we've got this: \n\
224 ;; Update n for the first loop \n\
227 movem [$r11+],$r10 \n\
231 movem $r10,[$r13+] \n\
234 addq 44,$r12 ;; compensate for last loop underflowing n \n\
236 ;; Restore registers from stack \n\
237 movem [$sp+],$r10 \n\
238 .section .fixup,\"ax\" \n\
240 ;; Do not jump back into the loop if we fail. For some uses, we get a \n\
241 ;; page fault somewhere on the line. Without checking for page limits, \n\
242 ;; we don't know where, but we need to copy accurately and keep an \n\
243 ;; accurate count; not just clear the whole line. To do that, we fall \n\
244 ;; down in the code below, proceeding with smaller amounts. It should \n\
245 ;; be kept in mind that we have to cater to code like what at one time \n\
246 ;; was in fs/super.c: \n\
247 ;; i = size - copy_from_user((void *)page, data, size); \n\
248 ;; which would cause repeated faults while clearing the remainder of \n\
249 ;; the SIZE bytes at PAGE after the first fault. \n\
250 ;; A caveat here is that we must not fall through from a failing page \n\
251 ;; to a valid page. \n\
254 jump 4b ;; Fall through, pretending the fault didn't happen. \n\
258 .section __ex_table,\"a\" \n\
262 /* Outputs */ : "=r" (dst
), "=r" (src
), "=r" (n
), "=r" (retn
)
263 /* Inputs */ : "0" (dst
), "1" (src
), "2" (n
), "3" (retn
));
266 /* Either we directly start copying here, using dword copying in a loop,
267 or we copy as much as possible with 'movem' and then the last block
268 (<44 bytes) is copied here. This will work since 'movem' will have
269 updated src, dst and n. (Except with failing src.)
271 Since we want to keep src accurate, we can't use
272 __asm_copy_from_user_N with N != (1, 2, 4); it updates dst and
273 retn, but not src (by design; it's value is ignored elsewhere). */
277 __asm_copy_from_user_4 (dst
, src
, retn
);
284 /* If we get here, there were no memory read faults. */
287 /* These copies are at least "naturally aligned" (so we don't have
288 to check each byte), due to the src alignment code before the
289 movem loop. The *_3 case *will* get the correct count for retn. */
291 /* This case deliberately left in (if you have doubts check the
292 generated assembly code). */
295 __asm_copy_from_user_1 (dst
, src
, retn
);
298 __asm_copy_from_user_2 (dst
, src
, retn
);
301 __asm_copy_from_user_3 (dst
, src
, retn
);
305 /* If we get here, retn correctly reflects the number of failing
312 EXPORT_SYMBOL(__copy_user_in
);
314 /* Zero userspace. */
315 unsigned long __do_clear_user(void __user
*pto
, unsigned long pn
)
317 /* We want the parameters put in special registers.
318 Make sure the compiler is able to make something useful of this.
319 As it is now: r10 -> r13; r11 -> r11 (nop); r12 -> r12 (nop).
321 FIXME: Comment for old gcc version. Check.
322 If gcc was alright, it really would need no temporaries, and no
323 stack space to save stuff on. */
325 register char *dst
__asm__ ("r13") = pto
;
326 register int n
__asm__ ("r12") = pn
;
327 register int retn
__asm__ ("r10") = 0;
330 if (((unsigned long) dst
& 3) != 0
331 /* Don't align if we wouldn't copy more than a few bytes. */
334 if ((unsigned long) dst
& 1)
336 __asm_clear_1 (dst
, retn
);
340 if ((unsigned long) dst
& 2)
342 __asm_clear_2 (dst
, retn
);
347 /* Decide which copying method to use.
348 FIXME: This number is from the "ordinary" kernel memset. */
351 /* For large clears we use 'movem' */
353 /* It is not optimal to tell the compiler about clobbering any
354 call-saved registers; that will move the saving/restoring of
355 those registers to the function prologue/epilogue, and make
356 non-movem sizes suboptimal.
358 This method is not foolproof; it assumes that the "asm reg"
359 declarations at the beginning of the function really are used
360 here (beware: they may be moved to temporary registers).
361 This way, we do not have to save/move the registers around into
362 temporaries; we can safely use them straight away.
364 If you want to check that the allocation was right; then
365 check the equalities in the first comment. It should say
366 something like "r13=r13, r11=r11, r12=r12". */
368 .ifnc %0%1%2,$r13$r12$r10 \n\
372 ;; Save the registers we'll clobber in the movem process \n\
373 ;; on the stack. Don't mention them to gcc, it will only be \n\
391 ;; Now we've got this: \n\
395 ;; Update n for the first loop \n\
401 movem $r11,[$r13+] \n\
403 addq 12*4,$r12 ;; compensate for last loop underflowing n \n\
405 ;; Restore registers from stack \n\
406 movem [$sp+],$r10 \n\
408 .section .fixup,\"ax\" \n\
418 .section __ex_table,\"a\" \n\
422 /* Outputs */ : "=r" (dst
), "=r" (n
), "=r" (retn
)
423 /* Inputs */ : "0" (dst
), "1" (n
), "2" (retn
)
424 /* Clobber */ : "r11");
429 __asm_clear_16 (dst
, retn
);
433 /* Having a separate by-four loops cuts down on cache footprint.
434 FIXME: Test with and without; increasing switch to be 0..15. */
437 __asm_clear_4 (dst
, retn
);
446 __asm_clear_1 (dst
, retn
);
449 __asm_clear_2 (dst
, retn
);
452 __asm_clear_3 (dst
, retn
);
458 EXPORT_SYMBOL(__do_clear_user
);