2 * User Space Access Routines
4 * Copyright (C) 2000-2002 Hewlett-Packard (John Marvin)
5 * Copyright (C) 2000 Richard Hirst <rhirst with parisc-linux.org>
6 * Copyright (C) 2001 Matthieu Delahaye <delahaym at esiee.fr>
7 * Copyright (C) 2003 Randolph Chung <tausq with parisc-linux.org>
8 * Copyright (C) 2017 Helge Deller <deller@gmx.de>
9 * Copyright (C) 2017 John David Anglin <dave.anglin@bell.net>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * These routines still have plenty of room for optimization
29 * (word & doubleword load/store, dual issue, store hints, etc.).
33 * The following routines assume that space register 3 (sr3) contains
34 * the space id associated with the current users address space.
40 #include <asm/assembly.h>
41 #include <asm/errno.h>
42 #include <linux/linkage.h>
45 * get_sr gets the appropriate space value into
46 * sr1 for kernel/user space access, depending
47 * on the flag stored in the task structure.
52 ldw TI_SEGMENT(%r1),%r22
60 * unsigned long lclear_user(void *to, unsigned long n)
62 * Returns 0 for success.
63 * otherwise, returns number of bytes not transferred.
66 ENTRY_CFI(lclear_user)
70 comib,=,n 0,%r25,$lclu_done
73 addib,<> -1,%r25,$lclu_loop
74 1: stbs,ma %r0,1(%sr1,%r26)
83 ASM_EXCEPTIONTABLE_ENTRY(1b,2b)
86 ENDPROC_CFI(lclear_user)
92 * long lstrnlen_user(char *s, long n)
94 * Returns 0 if exception before zero byte or reaching N,
95 * N+1 if N would be exceeded,
96 * else strlen + 1 (i.e. includes zero byte).
99 ENTRY_CFI(lstrnlen_user)
103 comib,= 0,%r25,$lslen_nzero
106 1: ldbs,ma 1(%sr1,%r26),%r1
108 comib,=,n 0,%r1,$lslen_done
109 addib,<> -1,%r25,$lslen_loop
110 2: ldbs,ma 1(%sr1,%r26),%r1
118 ldo 1(%r26),%r26 /* special case for N == 0 */
121 copy %r24,%r26 /* reset r26 so 0 is returned on fault */
123 ASM_EXCEPTIONTABLE_ENTRY(1b,3b)
124 ASM_EXCEPTIONTABLE_ENTRY(2b,3b)
126 ENDPROC_CFI(lstrnlen_user)
133 * unsigned long pa_memcpy(void *dstp, const void *srcp, unsigned long len)
136 * - sr1 already contains space of source region
137 * - sr2 already contains space of destination region
140 * - number of bytes that could not be copied.
141 * On success, this will be zero.
143 * This code is based on a C-implementation of a copy routine written by
144 * Randolph Chung, which in turn was derived from the glibc.
146 * Several strategies are tried to try to get the best performance for various
147 * conditions. In the optimal case, we copy by loops that copy 32- or 16-bytes
148 * at a time using general registers. Unaligned copies are handled either by
149 * aligning the destination and then using shift-and-write method, or in a few
150 * cases by falling back to a byte-at-a-time copy.
152 * Testing with various alignments and buffer sizes shows that this code is
153 * often >10x faster than a simple byte-at-a-time copy, even for strangely
154 * aligned operands. It is interesting to note that the glibc version of memcpy
155 * (written in C) is actually quite fast already. This routine is able to beat
156 * it by 30-40% for aligned copies because of the loop unrolling, but in some
157 * cases the glibc version is still slightly faster. This lends more
158 * credibility that gcc can generate very good code as long as we are careful.
160 * Possible optimizations:
161 * - add cache prefetching
162 * - try not to use the post-increment address modifiers; they may create
163 * additional interlocks. Assumption is that those were only efficient on old
164 * machines (pre PA8000 processors)
193 /* Last destination address */
196 /* short copy with less than 16 bytes? */
197 cmpib,COND(>>=),n 15,len,.Lbyte_loop
199 /* same alignment? */
202 cmpib,<>,n 0,t1,.Lunaligned_copy
205 /* only do 64-bit copies if we can get aligned. */
207 cmpib,<>,n 0,t1,.Lalign_loop32
209 /* loop until we are 64-bit aligned */
212 cmpib,=,n 0,t1,.Lcopy_loop_16_start
213 20: ldb,ma 1(srcspc,src),t1
214 21: stb,ma t1,1(dstspc,dst)
218 ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
219 ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
221 .Lcopy_loop_16_start:
224 cmpb,COND(>>=),n t0,len,.Lword_loop
226 10: ldd 0(srcspc,src),t1
227 11: ldd 8(srcspc,src),t2
229 12: std,ma t1,8(dstspc,dst)
230 13: std,ma t2,8(dstspc,dst)
231 14: ldd 0(srcspc,src),t1
232 15: ldd 8(srcspc,src),t2
234 16: std,ma t1,8(dstspc,dst)
235 17: std,ma t2,8(dstspc,dst)
237 ASM_EXCEPTIONTABLE_ENTRY(10b,.Lcopy_done)
238 ASM_EXCEPTIONTABLE_ENTRY(11b,.Lcopy16_fault)
239 ASM_EXCEPTIONTABLE_ENTRY(12b,.Lcopy_done)
240 ASM_EXCEPTIONTABLE_ENTRY(13b,.Lcopy_done)
241 ASM_EXCEPTIONTABLE_ENTRY(14b,.Lcopy_done)
242 ASM_EXCEPTIONTABLE_ENTRY(15b,.Lcopy16_fault)
243 ASM_EXCEPTIONTABLE_ENTRY(16b,.Lcopy_done)
244 ASM_EXCEPTIONTABLE_ENTRY(17b,.Lcopy_done)
250 cmpib,COND(>>=),n 3,len,.Lbyte_loop
251 20: ldw,ma 4(srcspc,src),t1
252 21: stw,ma t1,4(dstspc,dst)
256 ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
257 ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
259 #endif /* CONFIG_64BIT */
261 /* loop until we are 32-bit aligned */
264 cmpib,=,n 0,t1,.Lcopy_loop_8
265 20: ldb,ma 1(srcspc,src),t1
266 21: stb,ma t1,1(dstspc,dst)
270 ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
271 ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
275 cmpib,COND(>>=),n 15,len,.Lbyte_loop
277 10: ldw 0(srcspc,src),t1
278 11: ldw 4(srcspc,src),t2
279 12: stw,ma t1,4(dstspc,dst)
280 13: stw,ma t2,4(dstspc,dst)
281 14: ldw 8(srcspc,src),t1
282 15: ldw 12(srcspc,src),t2
284 16: stw,ma t1,4(dstspc,dst)
285 17: stw,ma t2,4(dstspc,dst)
287 ASM_EXCEPTIONTABLE_ENTRY(10b,.Lcopy_done)
288 ASM_EXCEPTIONTABLE_ENTRY(11b,.Lcopy8_fault)
289 ASM_EXCEPTIONTABLE_ENTRY(12b,.Lcopy_done)
290 ASM_EXCEPTIONTABLE_ENTRY(13b,.Lcopy_done)
291 ASM_EXCEPTIONTABLE_ENTRY(14b,.Lcopy_done)
292 ASM_EXCEPTIONTABLE_ENTRY(15b,.Lcopy8_fault)
293 ASM_EXCEPTIONTABLE_ENTRY(16b,.Lcopy_done)
294 ASM_EXCEPTIONTABLE_ENTRY(17b,.Lcopy_done)
300 cmpclr,COND(<>) len,%r0,%r0
302 20: ldb 0(srcspc,src),t1
304 21: stb,ma t1,1(dstspc,dst)
308 ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
309 ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
316 /* src and dst are not aligned the same way. */
317 /* need to go the hard way */
319 /* align until dst is 32bit-word-aligned */
321 cmpib,=,n 0,t1,.Lcopy_dstaligned
322 20: ldb 0(srcspc,src),t1
324 21: stb,ma t1,1(dstspc,dst)
328 ASM_EXCEPTIONTABLE_ENTRY(20b,.Lcopy_done)
329 ASM_EXCEPTIONTABLE_ENTRY(21b,.Lcopy_done)
333 /* store src, dst and len in safe place */
338 /* len now needs give number of words to copy */
342 * Copy from a not-aligned src to an aligned dst using shifts.
343 * Handles 4 words per loop.
351 /* Make src aligned by rounding it down. */
359 cmpb,COND(=) %r0,len,.Lcda_finish
362 1: ldw,ma 4(srcspc,src), a3
363 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
364 1: ldw,ma 4(srcspc,src), a0
365 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
368 1: ldw,ma 4(srcspc,src), a2
369 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
370 1: ldw,ma 4(srcspc,src), a3
371 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
373 cmpb,COND(=),n %r0,len,.Ldo0
375 1: ldw,ma 4(srcspc,src), a0
376 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
377 shrpw a2, a3, %sar, t0
378 1: stw,ma t0, 4(dstspc,dst)
379 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
381 1: ldw,ma 4(srcspc,src), a1
382 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
383 shrpw a3, a0, %sar, t0
384 1: stw,ma t0, 4(dstspc,dst)
385 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
387 1: ldw,ma 4(srcspc,src), a2
388 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
389 shrpw a0, a1, %sar, t0
390 1: stw,ma t0, 4(dstspc,dst)
391 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
393 1: ldw,ma 4(srcspc,src), a3
394 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
395 shrpw a1, a2, %sar, t0
396 1: stw,ma t0, 4(dstspc,dst)
397 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
399 cmpb,COND(<>) %r0,len,.Ldo4
402 shrpw a2, a3, %sar, t0
403 1: stw,ma t0, 4(dstspc,dst)
404 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcopy_done)
408 /* calculate new src, dst and len and jump to byte-copy loop */
415 1: ldw,ma 4(srcspc,src), a0
416 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
417 1: ldw,ma 4(srcspc,src), a1
418 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
422 1: ldw,ma 4(srcspc,src), a1
423 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
424 1: ldw,ma 4(srcspc,src), a2
425 ASM_EXCEPTIONTABLE_ENTRY(1b,.Lcda_rdfault)
430 /* fault exception fixup handlers: */
434 10: std,ma t1,8(dstspc,dst)
435 ASM_EXCEPTIONTABLE_ENTRY(10b,.Lcopy_done)
440 10: stw,ma t1,4(dstspc,dst)
441 ASM_EXCEPTIONTABLE_ENTRY(10b,.Lcopy_done)
444 ENDPROC_CFI(pa_memcpy)