1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* copy_user.S: Sparc optimized copy_from_user and copy_to_user code.
4 * Copyright(C) 1995 Linus Torvalds
5 * Copyright(C) 1996 David S. Miller
6 * Copyright(C) 1996 Eddie C. Dost
7 * Copyright(C) 1996,1998 Jakub Jelinek
10 * e-mail between David and Eddie.
12 * Returns 0 if successful, otherwise count of bytes not copied yet
15 #include <asm/ptrace.h>
16 #include <asm/asmmacro.h>
18 #include <asm/thread_info.h>
19 #include <asm/export.h>
21 /* Work around cpp -rob */
23 #define EXECINSTR #execinstr
26 .section .fixup,ALLOC,EXECINSTR; \
30 .section __ex_table,ALLOC; \
36 #define EX2(x,y,c,d,e,a,b) \
38 .section .fixup,ALLOC,EXECINSTR; \
43 .section __ex_table,ALLOC; \
51 .section __ex_table,ALLOC; \
57 #define EXT(start,end,handler) \
58 .section __ex_table,ALLOC; \
60 .word start, 0, end, handler; \
64 /* Please do not change following macros unless you change logic used
65 * in .fixup at the end of this file as well
68 /* Both these macros have to start with exactly the same insn */
69 #define MOVE_BIGCHUNK(src, dst, offset, t0, t1, t2, t3, t4, t5, t6, t7) \
70 ldd [%src + (offset) + 0x00], %t0; \
71 ldd [%src + (offset) + 0x08], %t2; \
72 ldd [%src + (offset) + 0x10], %t4; \
73 ldd [%src + (offset) + 0x18], %t6; \
74 st %t0, [%dst + (offset) + 0x00]; \
75 st %t1, [%dst + (offset) + 0x04]; \
76 st %t2, [%dst + (offset) + 0x08]; \
77 st %t3, [%dst + (offset) + 0x0c]; \
78 st %t4, [%dst + (offset) + 0x10]; \
79 st %t5, [%dst + (offset) + 0x14]; \
80 st %t6, [%dst + (offset) + 0x18]; \
81 st %t7, [%dst + (offset) + 0x1c];
83 #define MOVE_BIGALIGNCHUNK(src, dst, offset, t0, t1, t2, t3, t4, t5, t6, t7) \
84 ldd [%src + (offset) + 0x00], %t0; \
85 ldd [%src + (offset) + 0x08], %t2; \
86 ldd [%src + (offset) + 0x10], %t4; \
87 ldd [%src + (offset) + 0x18], %t6; \
88 std %t0, [%dst + (offset) + 0x00]; \
89 std %t2, [%dst + (offset) + 0x08]; \
90 std %t4, [%dst + (offset) + 0x10]; \
91 std %t6, [%dst + (offset) + 0x18];
93 #define MOVE_LASTCHUNK(src, dst, offset, t0, t1, t2, t3) \
94 ldd [%src - (offset) - 0x10], %t0; \
95 ldd [%src - (offset) - 0x08], %t2; \
96 st %t0, [%dst - (offset) - 0x10]; \
97 st %t1, [%dst - (offset) - 0x0c]; \
98 st %t2, [%dst - (offset) - 0x08]; \
99 st %t3, [%dst - (offset) - 0x04];
101 #define MOVE_HALFCHUNK(src, dst, offset, t0, t1, t2, t3) \
102 lduh [%src + (offset) + 0x00], %t0; \
103 lduh [%src + (offset) + 0x02], %t1; \
104 lduh [%src + (offset) + 0x04], %t2; \
105 lduh [%src + (offset) + 0x06], %t3; \
106 sth %t0, [%dst + (offset) + 0x00]; \
107 sth %t1, [%dst + (offset) + 0x02]; \
108 sth %t2, [%dst + (offset) + 0x04]; \
109 sth %t3, [%dst + (offset) + 0x06];
111 #define MOVE_SHORTCHUNK(src, dst, offset, t0, t1) \
112 ldub [%src - (offset) - 0x02], %t0; \
113 ldub [%src - (offset) - 0x01], %t1; \
114 stb %t0, [%dst - (offset) - 0x02]; \
115 stb %t1, [%dst - (offset) - 0x01];
120 .globl __copy_user_begin
124 EXPORT_SYMBOL(__copy_user)
130 EXO2(ldub [%o1], %g2)
137 EXO2(lduh [%o1], %g2)
144 EXO2(lduh [%o1], %g2)
151 __copy_user: /* %o0=dst %o1=src %o2=len */
159 bleu short_aligned_end
175 andcc %g1, 0xffffff80, %g7
181 MOVE_BIGCHUNK(o1, o0, 0x00, o2, o3, o4, o5, g2, g3, g4, g5)
182 MOVE_BIGCHUNK(o1, o0, 0x20, o2, o3, o4, o5, g2, g3, g4, g5)
183 MOVE_BIGCHUNK(o1, o0, 0x40, o2, o3, o4, o5, g2, g3, g4, g5)
184 MOVE_BIGCHUNK(o1, o0, 0x60, o2, o3, o4, o5, g2, g3, g4, g5)
193 be copy_user_table_end
196 sethi %hi(copy_user_table_end), %o5
201 jmpl %o5 + %lo(copy_user_table_end), %g0
205 MOVE_LASTCHUNK(o1, o0, 0x60, g2, g3, g4, g5)
206 MOVE_LASTCHUNK(o1, o0, 0x50, g2, g3, g4, g5)
207 MOVE_LASTCHUNK(o1, o0, 0x40, g2, g3, g4, g5)
208 MOVE_LASTCHUNK(o1, o0, 0x30, g2, g3, g4, g5)
209 MOVE_LASTCHUNK(o1, o0, 0x20, g2, g3, g4, g5)
210 MOVE_LASTCHUNK(o1, o0, 0x10, g2, g3, g4, g5)
211 MOVE_LASTCHUNK(o1, o0, 0x00, g2, g3, g4, g5)
213 EXT(copy_user_table, copy_user_table_end, 51f)
217 EX(ldd [%o1], %g2, and %g1, 0xf)
220 EX(st %g2, [%o0 - 0x08], and %g1, 0xf)
221 EX2(st %g3, [%o0 - 0x04], and %g1, 0xf, %g1, sub %g1, 4)
226 EX(ld [%o1], %g2, and %g1, 7)
228 EX(st %g2, [%o0], and %g1, 7)
234 EX(lduh [%o1], %g2, and %g1, 3)
236 EX(sth %g2, [%o0], and %g1, 3)
242 EX(ldub [%o1], %g2, add %g0, 1)
243 EX(stb %g2, [%o0], add %g0, 1)
249 MOVE_BIGALIGNCHUNK(o1, o0, 0x00, o2, o3, o4, o5, g2, g3, g4, g5)
250 MOVE_BIGALIGNCHUNK(o1, o0, 0x20, o2, o3, o4, o5, g2, g3, g4, g5)
251 MOVE_BIGALIGNCHUNK(o1, o0, 0x40, o2, o3, o4, o5, g2, g3, g4, g5)
252 MOVE_BIGALIGNCHUNK(o1, o0, 0x60, o2, o3, o4, o5, g2, g3, g4, g5)
254 EXT(ldd_std, 81b, 52f)
261 be copy_user_table_end
264 sethi %hi(copy_user_table_end), %o5
269 jmpl %o5 + %lo(copy_user_table_end), %g0
277 and %o2, 0xfffffff0, %o3
283 EXO2(ldub [%o1], %g2)
287 andcc %o2, 0xfffffff0, %o3
291 MOVE_HALFCHUNK(o1, o0, 0x00, g2, g3, g4, g5)
292 MOVE_HALFCHUNK(o1, o0, 0x08, g2, g3, g4, g5)
303 MOVE_SHORTCHUNK(o1, o0, -0x02, g2, g3)
304 MOVE_SHORTCHUNK(o1, o0, -0x04, g2, g3)
305 MOVE_SHORTCHUNK(o1, o0, -0x06, g2, g3)
306 MOVE_SHORTCHUNK(o1, o0, -0x08, g2, g3)
307 MOVE_SHORTCHUNK(o1, o0, -0x0a, g2, g3)
308 MOVE_SHORTCHUNK(o1, o0, -0x0c, g2, g3)
309 MOVE_SHORTCHUNK(o1, o0, -0x0e, g2, g3)
310 MOVE_SHORTCHUNK(o1, o0, -0x10, g2, g3)
312 EXT(byte_chunk, 83b, 54f)
321 sethi %hi(short_table_end), %o5
326 jmpl %o5 + %lo(short_table_end), %g0
329 MOVE_SHORTCHUNK(o1, o0, 0x0c, g2, g3)
330 MOVE_SHORTCHUNK(o1, o0, 0x0a, g2, g3)
331 MOVE_SHORTCHUNK(o1, o0, 0x08, g2, g3)
332 MOVE_SHORTCHUNK(o1, o0, 0x06, g2, g3)
333 MOVE_SHORTCHUNK(o1, o0, 0x04, g2, g3)
334 MOVE_SHORTCHUNK(o1, o0, 0x02, g2, g3)
335 MOVE_SHORTCHUNK(o1, o0, 0x00, g2, g3)
337 EXT(84b, short_table_end, 55f)
340 EX(ldub [%o1], %g2, add %g0, 1)
341 EX(stb %g2, [%o0], add %g0, 1)
353 EXO2(ld [%o1 + 0x00], %g2)
354 EXO2(ld [%o1 + 0x04], %g3)
356 EXO2(st %g2, [%o0 + 0x00])
357 EX(st %g3, [%o0 + 0x04], sub %o2, 4)
363 .section .fixup,#alloc,#execinstr
371 /* exception routine sets %g2 to (broken_insn - first_insn)>>2 */
373 /* This magic counts how many bytes are left when crash in MOVE_BIGCHUNK
374 * happens. This is derived from the amount ldd reads, st stores, etc.
376 * g3 = g1 + g7 - ((g2 / 12) * 32 + (x < 4) ? 0 : (x - 4) * 4);
377 * o0 += (g2 / 12) * 32;
398 60: and %g1, 0x7f, %g3
404 /* i = 41 - g2; j = i % 6;
405 * g3 = (g1 & 15) + (i / 6) * 16 + (j < 4) ? (j + 1) * 4 : 16;
406 * o0 -= (i / 6) * 16 + 16;
426 /* g3 = g1 + g7 - (g2 / 8) * 32 + (g2 & 4) ? (g2 & 3) * 8 : 0;
427 o0 += (g2 / 8) * 32 */
439 /* g3 = o3 + (o2 & 15) - (g2 & 8) - (g2 & 4) ? (g2 & 3) * 2 : 0;
453 /* g3 = o3 + (o2 & 15) - (g2 / 4) * 2 - (g2 & 2) ? (g2 & 1) : 0;
454 o0 += (g2 / 4) * 2 */
468 g3 = (o2 & 1) + i / 4 * 2 + !(i & 3);
469 o0 -= i / 4 * 2 + 1 */
484 .globl __copy_user_end