0aad5ce89f4d0a37c2810d72113263f53b096764
[linux-2.6-microblaze.git] / arch / parisc / lib / lusercopy.S
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *    User Space Access Routines
4  *
5  *    Copyright (C) 2000-2002 Hewlett-Packard (John Marvin)
6  *    Copyright (C) 2000 Richard Hirst <rhirst with parisc-linux.org>
7  *    Copyright (C) 2001 Matthieu Delahaye <delahaym at esiee.fr>
8  *    Copyright (C) 2003 Randolph Chung <tausq with parisc-linux.org>
9  *    Copyright (C) 2017 Helge Deller <deller@gmx.de>
10  *    Copyright (C) 2017 John David Anglin <dave.anglin@bell.net>
11  */
12
13 /*
14  * These routines still have plenty of room for optimization
15  * (word & doubleword load/store, dual issue, store hints, etc.).
16  */
17
18 /*
19  * The following routines assume that space register 3 (sr3) contains
20  * the space id associated with the current users address space.
21  */
22
23
24         .text
25         
26 #include <asm/assembly.h>
27 #include <asm/errno.h>
28 #include <linux/linkage.h>
29
30         /*
31          * get_sr gets the appropriate space value into
32          * sr1 for kernel/user space access, depending
33          * on the flag stored in the task structure.
34          */
35
36         .macro  get_sr
37         mfctl       %cr30,%r1
38         ldw         TI_SEGMENT(%r1),%r22
39         mfsp        %sr3,%r1
40         or,<>       %r22,%r0,%r0
41         copy        %r0,%r1
42         mtsp        %r1,%sr1
43         .endm
44
45         /*
46          * unsigned long lclear_user(void *to, unsigned long n)
47          *
48          * Returns 0 for success.
49          * otherwise, returns number of bytes not transferred.
50          */
51
52 ENTRY_CFI(lclear_user)
53         comib,=,n   0,%r25,$lclu_done
54         get_sr
55 $lclu_loop:
56         addib,<>    -1,%r25,$lclu_loop
57 1:      stbs,ma     %r0,1(%sr1,%r26)
58
59 $lclu_done:
60         bv          %r0(%r2)
61         copy        %r25,%r28
62
63 2:      b           $lclu_done
64         ldo         1(%r25),%r25
65
66         ASM_EXCEPTIONTABLE_ENTRY(1b,2b)
67 ENDPROC_CFI(lclear_user)
68
69
70 /*
71  * unsigned long pa_memcpy(void *dstp, const void *srcp, unsigned long len)
72  *
73  * Inputs:
74  * - sr1 already contains space of source region
75  * - sr2 already contains space of destination region
76  *
77  * Returns:
78  * - number of bytes that could not be copied.
79  *   On success, this will be zero.
80  *
81  * This code is based on a C-implementation of a copy routine written by
82  * Randolph Chung, which in turn was derived from the glibc.
83  *
84  * Several strategies are tried to try to get the best performance for various
85  * conditions. In the optimal case, we copy by loops that copy 32- or 16-bytes
86  * at a time using general registers.  Unaligned copies are handled either by
87  * aligning the destination and then using shift-and-write method, or in a few
88  * cases by falling back to a byte-at-a-time copy.
89  *
90  * Testing with various alignments and buffer sizes shows that this code is
91  * often >10x faster than a simple byte-at-a-time copy, even for strangely
92  * aligned operands. It is interesting to note that the glibc version of memcpy
93  * (written in C) is actually quite fast already. This routine is able to beat
94  * it by 30-40% for aligned copies because of the loop unrolling, but in some
95  * cases the glibc version is still slightly faster. This lends more
96  * credibility that gcc can generate very good code as long as we are careful.
97  *
98  * Possible optimizations:
99  * - add cache prefetching
100  * - try not to use the post-increment address modifiers; they may create
101  *   additional interlocks. Assumption is that those were only efficient on old
102  *   machines (pre PA8000 processors)
103  */
104
105         dst = arg0
106         src = arg1
107         len = arg2
108         end = arg3
109         t1  = r19
110         t2  = r20
111         t3  = r21
112         t4  = r22
113         srcspc = sr1
114         dstspc = sr2
115
116         t0 = r1
117         a1 = t1
118         a2 = t2
119         a3 = t3
120         a0 = t4
121
122         save_src = ret0
123         save_dst = ret1
124         save_len = r31
125
126 ENTRY_CFI(pa_memcpy)
127         /* Last destination address */
128         add     dst,len,end
129
130         /* short copy with less than 16 bytes? */
131         cmpib,COND(>>=),n 15,len,.Lbyte_loop
132
133         /* same alignment? */
134         xor     src,dst,t0
135         extru   t0,31,2,t1
136         cmpib,<>,n  0,t1,.Lunaligned_copy
137
138 #ifdef CONFIG_64BIT
139         /* only do 64-bit copies if we can get aligned. */
140         extru   t0,31,3,t1
141         cmpib,<>,n  0,t1,.Lalign_loop32
142
143         /* loop until we are 64-bit aligned */
144 .Lalign_loop64:
145         extru   dst,31,3,t1
146         cmpib,=,n       0,t1,.Lcopy_loop_16_start
147 20:     ldb,ma  1(srcspc,src),t1
148 21:     stb,ma  t1,1(dstspc,dst)
149         b       .Lalign_loop64
150         ldo     -1(len),len
151
152         ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
153         ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
154
155 .Lcopy_loop_16_start:
156         ldi     31,t0
157 .Lcopy_loop_16:
158         cmpb,COND(>>=),n t0,len,.Lword_loop
159
160 10:     ldd     0(srcspc,src),t1
161 11:     ldd     8(srcspc,src),t2
162         ldo     16(src),src
163 12:     std,ma  t1,8(dstspc,dst)
164 13:     std,ma  t2,8(dstspc,dst)
165 14:     ldd     0(srcspc,src),t1
166 15:     ldd     8(srcspc,src),t2
167         ldo     16(src),src
168 16:     std,ma  t1,8(dstspc,dst)
169 17:     std,ma  t2,8(dstspc,dst)
170
171         ASM_EXCEPTIONTABLE_ENTRY(10b,.Lcopy_done)
172         ASM_EXCEPTIONTABLE_ENTRY(11b,.Lcopy16_fault)
173         ASM_EXCEPTIONTABLE_ENTRY(12b,.Lcopy_done)
174         ASM_EXCEPTIONTABLE_ENTRY(13b,.Lcopy_done)
175         ASM_EXCEPTIONTABLE_ENTRY(14b,.Lcopy_done)
176         ASM_EXCEPTIONTABLE_ENTRY(15b,.Lcopy16_fault)
177         ASM_EXCEPTIONTABLE_ENTRY(16b,.Lcopy_done)
178         ASM_EXCEPTIONTABLE_ENTRY(17b,.Lcopy_done)
179
180         b       .Lcopy_loop_16
181         ldo     -32(len),len
182
183 .Lword_loop:
184         cmpib,COND(>>=),n 3,len,.Lbyte_loop
185 20:     ldw,ma  4(srcspc,src),t1
186 21:     stw,ma  t1,4(dstspc,dst)
187         b       .Lword_loop
188         ldo     -4(len),len
189
190         ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
191         ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
192
193 #endif /* CONFIG_64BIT */
194
195         /* loop until we are 32-bit aligned */
196 .Lalign_loop32:
197         extru   dst,31,2,t1
198         cmpib,=,n       0,t1,.Lcopy_loop_8
199 20:     ldb,ma  1(srcspc,src),t1
200 21:     stb,ma  t1,1(dstspc,dst)
201         b       .Lalign_loop32
202         ldo     -1(len),len
203
204         ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
205         ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
206
207
208 .Lcopy_loop_8:
209         cmpib,COND(>>=),n 15,len,.Lbyte_loop
210
211 10:     ldw     0(srcspc,src),t1
212 11:     ldw     4(srcspc,src),t2
213 12:     stw,ma  t1,4(dstspc,dst)
214 13:     stw,ma  t2,4(dstspc,dst)
215 14:     ldw     8(srcspc,src),t1
216 15:     ldw     12(srcspc,src),t2
217         ldo     16(src),src
218 16:     stw,ma  t1,4(dstspc,dst)
219 17:     stw,ma  t2,4(dstspc,dst)
220
221         ASM_EXCEPTIONTABLE_ENTRY(10b,.Lcopy_done)
222         ASM_EXCEPTIONTABLE_ENTRY(11b,.Lcopy8_fault)
223         ASM_EXCEPTIONTABLE_ENTRY(12b,.Lcopy_done)
224         ASM_EXCEPTIONTABLE_ENTRY(13b,.Lcopy_done)
225         ASM_EXCEPTIONTABLE_ENTRY(14b,.Lcopy_done)
226         ASM_EXCEPTIONTABLE_ENTRY(15b,.Lcopy8_fault)
227         ASM_EXCEPTIONTABLE_ENTRY(16b,.Lcopy_done)
228         ASM_EXCEPTIONTABLE_ENTRY(17b,.Lcopy_done)
229
230         b       .Lcopy_loop_8
231         ldo     -16(len),len
232
233 .Lbyte_loop:
234         cmpclr,COND(<>) len,%r0,%r0
235         b,n     .Lcopy_done
236 20:     ldb     0(srcspc,src),t1
237         ldo     1(src),src
238 21:     stb,ma  t1,1(dstspc,dst)
239         b       .Lbyte_loop
240         ldo     -1(len),len
241
242         ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
243         ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
244
245 .Lcopy_done:
246         bv      %r0(%r2)
247         sub     end,dst,ret0
248
249
250         /* src and dst are not aligned the same way. */
251         /* need to go the hard way */
252 .Lunaligned_copy:
253         /* align until dst is 32bit-word-aligned */
254         extru   dst,31,2,t1
255         cmpib,=,n       0,t1,.Lcopy_dstaligned
256 20:     ldb     0(srcspc,src),t1
257         ldo     1(src),src
258 21:     stb,ma  t1,1(dstspc,dst)
259         b       .Lunaligned_copy
260         ldo     -1(len),len
261
262         ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
263         ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
264
265 .Lcopy_dstaligned:
266
267         /* store src, dst and len in safe place */
268         copy    src,save_src
269         copy    dst,save_dst
270         copy    len,save_len
271
272         /* len now needs give number of words to copy */
273         SHRREG  len,2,len
274
275         /*
276          * Copy from a not-aligned src to an aligned dst using shifts.
277          * Handles 4 words per loop.
278          */
279
280         depw,z src,28,2,t0
281         subi 32,t0,t0
282         mtsar t0
283         extru len,31,2,t0
284         cmpib,= 2,t0,.Lcase2
285         /* Make src aligned by rounding it down.  */
286         depi 0,31,2,src
287
288         cmpiclr,<> 3,t0,%r0
289         b,n .Lcase3
290         cmpiclr,<> 1,t0,%r0
291         b,n .Lcase1
292 .Lcase0:
293         cmpb,COND(=) %r0,len,.Lcda_finish
294         nop
295
296 1:      ldw,ma 4(srcspc,src), a3
297         ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
298 1:      ldw,ma 4(srcspc,src), a0
299         ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
300         b,n .Ldo3
301 .Lcase1:
302 1:      ldw,ma 4(srcspc,src), a2
303         ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
304 1:      ldw,ma 4(srcspc,src), a3
305         ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
306         ldo -1(len),len
307         cmpb,COND(=),n %r0,len,.Ldo0
308 .Ldo4:
309 1:      ldw,ma 4(srcspc,src), a0
310         ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
311         shrpw a2, a3, %sar, t0
312 1:      stw,ma t0, 4(dstspc,dst)
313         ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
314 .Ldo3:
315 1:      ldw,ma 4(srcspc,src), a1
316         ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
317         shrpw a3, a0, %sar, t0
318 1:      stw,ma t0, 4(dstspc,dst)
319         ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
320 .Ldo2:
321 1:      ldw,ma 4(srcspc,src), a2
322         ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
323         shrpw a0, a1, %sar, t0
324 1:      stw,ma t0, 4(dstspc,dst)
325         ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
326 .Ldo1:
327 1:      ldw,ma 4(srcspc,src), a3
328         ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
329         shrpw a1, a2, %sar, t0
330 1:      stw,ma t0, 4(dstspc,dst)
331         ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
332         ldo -4(len),len
333         cmpb,COND(<>) %r0,len,.Ldo4
334         nop
335 .Ldo0:
336         shrpw a2, a3, %sar, t0
337 1:      stw,ma t0, 4(dstspc,dst)
338         ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
339
340 .Lcda_rdfault:
341 .Lcda_finish:
342         /* calculate new src, dst and len and jump to byte-copy loop */
343         sub     dst,save_dst,t0
344         add     save_src,t0,src
345         b       .Lbyte_loop
346         sub     save_len,t0,len
347
348 .Lcase3:
349 1:      ldw,ma 4(srcspc,src), a0
350         ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
351 1:      ldw,ma 4(srcspc,src), a1
352         ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
353         b .Ldo2
354         ldo 1(len),len
355 .Lcase2:
356 1:      ldw,ma 4(srcspc,src), a1
357         ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
358 1:      ldw,ma 4(srcspc,src), a2
359         ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
360         b .Ldo1
361         ldo 2(len),len
362
363
364         /* fault exception fixup handlers: */
365 #ifdef CONFIG_64BIT
366 .Lcopy16_fault:
367         b       .Lcopy_done
368 10:     std,ma  t1,8(dstspc,dst)
369         ASM_EXCEPTIONTABLE_ENTRY(10b,.Lcopy_done)
370 #endif
371
372 .Lcopy8_fault:
373         b       .Lcopy_done
374 10:     stw,ma  t1,4(dstspc,dst)
375         ASM_EXCEPTIONTABLE_ENTRY(10b,.Lcopy_done)
376 ENDPROC_CFI(pa_memcpy)
377
378         .end