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, 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 2.96 20000427 (experimental)".
19 Note that the PC saved at a bus-fault is the address *after* the
20 faulting instruction, which means the branch-target for instructions in
21 delay-slots for taken branches. Note also that the postincrement in
22 the instruction is performed regardless of bus-fault; the register is
23 seen updated in fault handlers.
25 Oh, and on the code formatting issue, to whomever feels like "fixing
26 it" to Conformity: I'm too "lazy", but why don't you go ahead and "fix"
27 string.c too. I just don't think too many people will hack this file
28 for the code format to be an issue. */
31 /* Copy to userspace. This is based on the memcpy used for
32 kernel-to-kernel copying; see "string.c". */
34 unsigned long __copy_user(void __user
*pdst
, const void *psrc
, unsigned long pn
)
36 /* We want the parameters put in special registers.
37 Make sure the compiler is able to make something useful of this.
38 As it is now: r10 -> r13; r11 -> r11 (nop); r12 -> r12 (nop).
40 FIXME: Comment for old gcc version. Check.
41 If gcc was alright, it really would need no temporaries, and no
42 stack space to save stuff on. */
44 register char *dst
__asm__ ("r13") = pdst
;
45 register const char *src
__asm__ ("r11") = psrc
;
46 register int n
__asm__ ("r12") = pn
;
47 register int retn
__asm__ ("r10") = 0;
50 /* When src is aligned but not dst, this makes a few extra needless
51 cycles. I believe it would take as many to check that the
52 re-alignment was unnecessary. */
53 if (((unsigned long) dst
& 3) != 0
54 /* Don't align if we wouldn't copy more than a few bytes; so we
55 don't have to check further for overflows. */
58 if ((unsigned long) dst
& 1)
60 __asm_copy_to_user_1 (dst
, src
, retn
);
64 if ((unsigned long) dst
& 2)
66 __asm_copy_to_user_2 (dst
, src
, retn
);
71 /* Decide which copying method to use. */
72 if (n
>= 44*2) /* Break even between movem and
73 move16 is at 38.7*2, but modulo 44. */
75 /* For large copies we use 'movem'. */
77 /* It is not optimal to tell the compiler about clobbering any
78 registers; that will move the saving/restoring of those registers
79 to the function prologue/epilogue, and make non-movem sizes
82 This method is not foolproof; it assumes that the "asm reg"
83 declarations at the beginning of the function really are used
84 here (beware: they may be moved to temporary registers).
85 This way, we do not have to save/move the registers around into
86 temporaries; we can safely use them straight away.
88 If you want to check that the allocation was right; then
89 check the equalities in the first comment. It should say
90 "r13=r13, r11=r11, r12=r12". */
92 .ifnc %0%1%2%3,$r13$r11$r12$r10 \n\
96 ;; Save the registers we'll use in the movem process \n\
101 ;; Now we've got this: \n\
106 ;; Update n for the first loop \n\
109 ; Since the noted PC of a faulting instruction in a delay-slot of a taken \n\
110 ; branch, is that of the branch target, we actually point at the from-movem \n\
111 ; for this case. There is no ambiguity here; if there was a fault in that \n\
112 ; instruction (meaning a kernel oops), the faulted PC would be the address \n\
113 ; after *that* movem. \n\
116 movem [$r11+],$r10 \n\
119 movem $r10,[$r13+] \n\
121 addq 44,$r12 ;; compensate for last loop underflowing n \n\
123 ;; Restore registers from stack \n\
124 movem [$sp+],$r10 \n\
126 .section .fixup,\"ax\" \n\
128 ; To provide a correct count in r10 of bytes that failed to be copied, \n\
129 ; we jump back into the loop if the loop-branch was taken. There is no \n\
130 ; performance penalty for sany use; the program will segfault soon enough.\n\
133 move.d [$sp],$r10 \n\
135 move.d $r10,[$sp] \n\
138 movem [$sp+],$r10 \n\
144 .section __ex_table,\"a\" \n\
149 /* Outputs */ : "=r" (dst
), "=r" (src
), "=r" (n
), "=r" (retn
)
150 /* Inputs */ : "0" (dst
), "1" (src
), "2" (n
), "3" (retn
));
154 /* Either we directly start copying, using dword copying in a loop, or
155 we copy as much as possible with 'movem' and then the last block (<44
156 bytes) is copied here. This will work since 'movem' will have
157 updated SRC, DST and N. */
161 __asm_copy_to_user_16 (dst
, src
, retn
);
165 /* Having a separate by-four loops cuts down on cache footprint.
166 FIXME: Test with and without; increasing switch to be 0..15. */
169 __asm_copy_to_user_4 (dst
, src
, retn
);
178 __asm_copy_to_user_1 (dst
, src
, retn
);
181 __asm_copy_to_user_2 (dst
, src
, retn
);
184 __asm_copy_to_user_3 (dst
, src
, retn
);
190 EXPORT_SYMBOL(__copy_user
);
192 /* Copy from user to kernel. The return-value is the number of bytes that were
195 unsigned long __copy_user_in(void *pdst
, const void __user
*psrc
,
198 /* We want the parameters put in special registers.
199 Make sure the compiler is able to make something useful of this.
200 As it is now: r10 -> r13; r11 -> r11 (nop); r12 -> r12 (nop).
202 FIXME: Comment for old gcc version. Check.
203 If gcc was alright, it really would need no temporaries, and no
204 stack space to save stuff on. */
206 register char *dst
__asm__ ("r13") = pdst
;
207 register const char *src
__asm__ ("r11") = psrc
;
208 register int n
__asm__ ("r12") = pn
;
209 register int retn
__asm__ ("r10") = 0;
211 /* The best reason to align src is that we then know that a read-fault
212 was for aligned bytes; there's no 1..3 remaining good bytes to
214 if (((unsigned long) src
& 3) != 0)
216 if (((unsigned long) src
& 1) && n
!= 0)
218 __asm_copy_from_user_1 (dst
, src
, retn
);
224 if (((unsigned long) src
& 2) && n
>= 2)
226 __asm_copy_from_user_2 (dst
, src
, retn
);
233 /* Decide which copying method to use. */
234 if (n
>= 44*2) /* Break even between movem and
235 move16 is at 38.7*2, but modulo 44.
236 FIXME: We use move4 now. */
238 /* For large copies we use 'movem' */
240 /* It is not optimal to tell the compiler about clobbering any
241 registers; that will move the saving/restoring of those registers
242 to the function prologue/epilogue, and make non-movem sizes
245 This method is not foolproof; it assumes that the "asm reg"
246 declarations at the beginning of the function really are used
247 here (beware: they may be moved to temporary registers).
248 This way, we do not have to save/move the registers around into
249 temporaries; we can safely use them straight away.
251 If you want to check that the allocation was right; then
252 check the equalities in the first comment. It should say
253 "r13=r13, r11=r11, r12=r12" */
254 __asm__
volatile ("\n\
255 .ifnc %0%1%2%3,$r13$r11$r12$r10 \n\
259 ;; Save the registers we'll use in the movem process \n\
264 ;; Now we've got this: \n\
269 ;; Update n for the first loop \n\
272 movem [$r11+],$r10 \n\
276 movem $r10,[$r13+] \n\
278 addq 44,$r12 ;; compensate for last loop underflowing n \n\
280 ;; Restore registers from stack \n\
281 movem [$sp+],$r10 \n\
283 .section .fixup,\"ax\" \n\
285 ;; Do not jump back into the loop if we fail. For some uses, we get a \n\
286 ;; page fault somewhere on the line. Without checking for page limits, \n\
287 ;; we don't know where, but we need to copy accurately and keep an \n\
288 ;; accurate count; not just clear the whole line. To do that, we fall \n\
289 ;; down in the code below, proceeding with smaller amounts. It should \n\
290 ;; be kept in mind that we have to cater to code like what at one time \n\
291 ;; was in fs/super.c: \n\
292 ;; i = size - copy_from_user((void *)page, data, size); \n\
293 ;; which would cause repeated faults while clearing the remainder of \n\
294 ;; the SIZE bytes at PAGE after the first fault. \n\
295 ;; A caveat here is that we must not fall through from a failing page \n\
296 ;; to a valid page. \n\
299 movem [$sp+],$r10 \n\
300 addq 44,$r12 ;; Get back count before faulting point. \n\
301 subq 44,$r11 ;; Get back pointer to faulting movem-line. \n\
302 jump 4b ;; Fall through, pretending the fault didn't happen.\n\
305 .section __ex_table,\"a\" \n\
309 /* Outputs */ : "=r" (dst
), "=r" (src
), "=r" (n
), "=r" (retn
)
310 /* Inputs */ : "0" (dst
), "1" (src
), "2" (n
), "3" (retn
));
314 /* Either we directly start copying here, using dword copying in a loop,
315 or we copy as much as possible with 'movem' and then the last block
316 (<44 bytes) is copied here. This will work since 'movem' will have
317 updated src, dst and n. (Except with failing src.)
319 Since we want to keep src accurate, we can't use
320 __asm_copy_from_user_N with N != (1, 2, 4); it updates dst and
321 retn, but not src (by design; it's value is ignored elsewhere). */
325 __asm_copy_from_user_4 (dst
, src
, retn
);
332 /* If we get here, there were no memory read faults. */
335 /* These copies are at least "naturally aligned" (so we don't have
336 to check each byte), due to the src alignment code before the
337 movem loop. The *_3 case *will* get the correct count for retn. */
339 /* This case deliberately left in (if you have doubts check the
340 generated assembly code). */
343 __asm_copy_from_user_1 (dst
, src
, retn
);
346 __asm_copy_from_user_2 (dst
, src
, retn
);
349 __asm_copy_from_user_3 (dst
, src
, retn
);
353 /* If we get here, retn correctly reflects the number of failing
360 EXPORT_SYMBOL(__copy_user_in
);
362 /* Zero userspace. */
363 unsigned long __do_clear_user(void __user
*pto
, unsigned long pn
)
365 /* We want the parameters put in special registers.
366 Make sure the compiler is able to make something useful of this.
367 As it is now: r10 -> r13; r11 -> r11 (nop); r12 -> r12 (nop).
369 FIXME: Comment for old gcc version. Check.
370 If gcc was alright, it really would need no temporaries, and no
371 stack space to save stuff on. */
373 register char *dst
__asm__ ("r13") = pto
;
374 register int n
__asm__ ("r12") = pn
;
375 register int retn
__asm__ ("r10") = 0;
378 if (((unsigned long) dst
& 3) != 0
379 /* Don't align if we wouldn't copy more than a few bytes. */
382 if ((unsigned long) dst
& 1)
384 __asm_clear_1 (dst
, retn
);
388 if ((unsigned long) dst
& 2)
390 __asm_clear_2 (dst
, retn
);
395 /* Decide which copying method to use.
396 FIXME: This number is from the "ordinary" kernel memset. */
399 /* For large clears we use 'movem' */
401 /* It is not optimal to tell the compiler about clobbering any
402 call-saved registers; that will move the saving/restoring of
403 those registers to the function prologue/epilogue, and make
404 non-movem sizes suboptimal.
406 This method is not foolproof; it assumes that the "asm reg"
407 declarations at the beginning of the function really are used
408 here (beware: they may be moved to temporary registers).
409 This way, we do not have to save/move the registers around into
410 temporaries; we can safely use them straight away.
412 If you want to check that the allocation was right; then
413 check the equalities in the first comment. It should say
414 something like "r13=r13, r11=r11, r12=r12". */
415 __asm__
volatile ("\n\
416 .ifnc %0%1%2,$r13$r12$r10 \n\
420 ;; Save the registers we'll clobber in the movem process \n\
421 ;; on the stack. Don't mention them to gcc, it will only be \n\
439 ;; Now we've got this: \n\
443 ;; Update n for the first loop \n\
448 movem $r11,[$r13+] \n\
450 addq 12*4,$r12 ;; compensate for last loop underflowing n\n\
452 ;; Restore registers from stack \n\
453 movem [$sp+],$r10 \n\
455 .section .fixup,\"ax\" \n\
457 move.d [$sp],$r10 \n\
459 move.d $r10,[$sp] \n\
464 movem [$sp+],$r10 \n\
470 .section __ex_table,\"a\" \n\
475 /* Outputs */ : "=r" (dst
), "=r" (n
), "=r" (retn
)
476 /* Inputs */ : "0" (dst
), "1" (n
), "2" (retn
)
477 /* Clobber */ : "r11");
482 __asm_clear_16 (dst
, retn
);
486 /* Having a separate by-four loops cuts down on cache footprint.
487 FIXME: Test with and without; increasing switch to be 0..15. */
490 __asm_clear_4 (dst
, retn
);
499 __asm_clear_1 (dst
, retn
);
502 __asm_clear_2 (dst
, retn
);
505 __asm_clear_3 (dst
, retn
);
511 EXPORT_SYMBOL(__do_clear_user
);