1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * User Space Access Routines
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>
14 * These routines still have plenty of room for optimization
15 * (word & doubleword load/store, dual issue, store hints, etc.).
19 * The following routines assume that space register 3 (sr3) contains
20 * the space id associated with the current users address space.
26 #include <asm/assembly.h>
27 #include <asm/errno.h>
28 #include <linux/linkage.h>
31 * unsigned long lclear_user(void *to, unsigned long n)
33 * Returns 0 for success.
34 * otherwise, returns number of bytes not transferred.
37 ENTRY_CFI(lclear_user)
38 comib,=,n 0,%r25,$lclu_done
40 addib,<> -1,%r25,$lclu_loop
41 1: stbs,ma %r0,1(%sr3,%r26)
50 ASM_EXCEPTIONTABLE_ENTRY(1b,2b)
51 ENDPROC_CFI(lclear_user)
55 * unsigned long pa_memcpy(void *dstp, const void *srcp, unsigned long len)
58 * - sr1 already contains space of source region
59 * - sr2 already contains space of destination region
62 * - number of bytes that could not be copied.
63 * On success, this will be zero.
65 * This code is based on a C-implementation of a copy routine written by
66 * Randolph Chung, which in turn was derived from the glibc.
68 * Several strategies are tried to try to get the best performance for various
69 * conditions. In the optimal case, we copy by loops that copy 32- or 16-bytes
70 * at a time using general registers. Unaligned copies are handled either by
71 * aligning the destination and then using shift-and-write method, or in a few
72 * cases by falling back to a byte-at-a-time copy.
74 * Testing with various alignments and buffer sizes shows that this code is
75 * often >10x faster than a simple byte-at-a-time copy, even for strangely
76 * aligned operands. It is interesting to note that the glibc version of memcpy
77 * (written in C) is actually quite fast already. This routine is able to beat
78 * it by 30-40% for aligned copies because of the loop unrolling, but in some
79 * cases the glibc version is still slightly faster. This lends more
80 * credibility that gcc can generate very good code as long as we are careful.
82 * Possible optimizations:
83 * - add cache prefetching
84 * - try not to use the post-increment address modifiers; they may create
85 * additional interlocks. Assumption is that those were only efficient on old
86 * machines (pre PA8000 processors)
111 /* Last destination address */
114 /* short copy with less than 16 bytes? */
115 cmpib,COND(>>=),n 15,len,.Lbyte_loop
117 /* same alignment? */
120 cmpib,<>,n 0,t1,.Lunaligned_copy
123 /* only do 64-bit copies if we can get aligned. */
125 cmpib,<>,n 0,t1,.Lalign_loop32
127 /* loop until we are 64-bit aligned */
130 cmpib,=,n 0,t1,.Lcopy_loop_16_start
131 20: ldb,ma 1(srcspc,src),t1
132 21: stb,ma t1,1(dstspc,dst)
136 ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
137 ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
139 .Lcopy_loop_16_start:
142 cmpb,COND(>>=),n t0,len,.Lword_loop
144 10: ldd 0(srcspc,src),t1
145 11: ldd 8(srcspc,src),t2
147 12: std,ma t1,8(dstspc,dst)
148 13: std,ma t2,8(dstspc,dst)
149 14: ldd 0(srcspc,src),t1
150 15: ldd 8(srcspc,src),t2
152 16: std,ma t1,8(dstspc,dst)
153 17: std,ma t2,8(dstspc,dst)
155 ASM_EXCEPTIONTABLE_ENTRY(10b,.Lcopy_done)
156 ASM_EXCEPTIONTABLE_ENTRY(11b,.Lcopy16_fault)
157 ASM_EXCEPTIONTABLE_ENTRY(12b,.Lcopy_done)
158 ASM_EXCEPTIONTABLE_ENTRY(13b,.Lcopy_done)
159 ASM_EXCEPTIONTABLE_ENTRY(14b,.Lcopy_done)
160 ASM_EXCEPTIONTABLE_ENTRY(15b,.Lcopy16_fault)
161 ASM_EXCEPTIONTABLE_ENTRY(16b,.Lcopy_done)
162 ASM_EXCEPTIONTABLE_ENTRY(17b,.Lcopy_done)
168 cmpib,COND(>>=),n 3,len,.Lbyte_loop
169 20: ldw,ma 4(srcspc,src),t1
170 21: stw,ma t1,4(dstspc,dst)
174 ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
175 ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
177 #endif /* CONFIG_64BIT */
179 /* loop until we are 32-bit aligned */
182 cmpib,=,n 0,t1,.Lcopy_loop_8
183 20: ldb,ma 1(srcspc,src),t1
184 21: stb,ma t1,1(dstspc,dst)
188 ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
189 ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
193 cmpib,COND(>>=),n 15,len,.Lbyte_loop
195 10: ldw 0(srcspc,src),t1
196 11: ldw 4(srcspc,src),t2
197 12: stw,ma t1,4(dstspc,dst)
198 13: stw,ma t2,4(dstspc,dst)
199 14: ldw 8(srcspc,src),t1
200 15: ldw 12(srcspc,src),t2
202 16: stw,ma t1,4(dstspc,dst)
203 17: stw,ma t2,4(dstspc,dst)
205 ASM_EXCEPTIONTABLE_ENTRY(10b,.Lcopy_done)
206 ASM_EXCEPTIONTABLE_ENTRY(11b,.Lcopy8_fault)
207 ASM_EXCEPTIONTABLE_ENTRY(12b,.Lcopy_done)
208 ASM_EXCEPTIONTABLE_ENTRY(13b,.Lcopy_done)
209 ASM_EXCEPTIONTABLE_ENTRY(14b,.Lcopy_done)
210 ASM_EXCEPTIONTABLE_ENTRY(15b,.Lcopy8_fault)
211 ASM_EXCEPTIONTABLE_ENTRY(16b,.Lcopy_done)
212 ASM_EXCEPTIONTABLE_ENTRY(17b,.Lcopy_done)
218 cmpclr,COND(<>) len,%r0,%r0
220 20: ldb 0(srcspc,src),t1
222 21: stb,ma t1,1(dstspc,dst)
226 ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
227 ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
234 /* src and dst are not aligned the same way. */
235 /* need to go the hard way */
237 /* align until dst is 32bit-word-aligned */
239 cmpib,=,n 0,t1,.Lcopy_dstaligned
240 20: ldb 0(srcspc,src),t1
242 21: stb,ma t1,1(dstspc,dst)
246 ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
247 ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
251 /* store src, dst and len in safe place */
256 /* len now needs give number of words to copy */
260 * Copy from a not-aligned src to an aligned dst using shifts.
261 * Handles 4 words per loop.
269 /* Make src aligned by rounding it down. */
277 cmpb,COND(=) %r0,len,.Lcda_finish
280 1: ldw,ma 4(srcspc,src), a3
281 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
282 1: ldw,ma 4(srcspc,src), a0
283 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
286 1: ldw,ma 4(srcspc,src), a2
287 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
288 1: ldw,ma 4(srcspc,src), a3
289 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
291 cmpb,COND(=),n %r0,len,.Ldo0
293 1: ldw,ma 4(srcspc,src), a0
294 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
295 shrpw a2, a3, %sar, t0
296 1: stw,ma t0, 4(dstspc,dst)
297 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
299 1: ldw,ma 4(srcspc,src), a1
300 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
301 shrpw a3, a0, %sar, t0
302 1: stw,ma t0, 4(dstspc,dst)
303 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
305 1: ldw,ma 4(srcspc,src), a2
306 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
307 shrpw a0, a1, %sar, t0
308 1: stw,ma t0, 4(dstspc,dst)
309 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
311 1: ldw,ma 4(srcspc,src), a3
312 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
313 shrpw a1, a2, %sar, t0
314 1: stw,ma t0, 4(dstspc,dst)
315 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
317 cmpb,COND(<>) %r0,len,.Ldo4
320 shrpw a2, a3, %sar, t0
321 1: stw,ma t0, 4(dstspc,dst)
322 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
326 /* calculate new src, dst and len and jump to byte-copy loop */
333 1: ldw,ma 4(srcspc,src), a0
334 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
335 1: ldw,ma 4(srcspc,src), a1
336 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
340 1: ldw,ma 4(srcspc,src), a1
341 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
342 1: ldw,ma 4(srcspc,src), a2
343 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
348 /* fault exception fixup handlers: */
352 10: std,ma t1,8(dstspc,dst)
353 ASM_EXCEPTIONTABLE_ENTRY(10b,.Lcopy_done)
358 10: stw,ma t1,4(dstspc,dst)
359 ASM_EXCEPTIONTABLE_ENTRY(10b,.Lcopy_done)
360 ENDPROC_CFI(pa_memcpy)